autosync_backend2 1.1.68 → 1.1.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -3854,12 +3854,13 @@ export declare const app: Elysia<"", {
3854
3854
  companyId: string;
3855
3855
  companyBranchId: string;
3856
3856
  customerId: string | null;
3857
- vehicleId: string;
3857
+ vehicleId: string | null;
3858
3858
  orderId: string;
3859
3859
  state: "CREATED" | "PROGRESSING" | "COMPLETE";
3860
3860
  isNoat: boolean | null;
3861
3861
  timeCompleted: string | null;
3862
3862
  km: number;
3863
+ type: "Service" | "ProductSell";
3863
3864
  id: string;
3864
3865
  createdAt: string;
3865
3866
  updatedAt: string;
@@ -3965,12 +3966,13 @@ export declare const app: Elysia<"", {
3965
3966
  companyId: string;
3966
3967
  companyBranchId: string;
3967
3968
  customerId: string | null;
3968
- vehicleId: string;
3969
+ vehicleId: string | null;
3969
3970
  orderId: string;
3970
3971
  state: "CREATED" | "PROGRESSING" | "COMPLETE";
3971
3972
  isNoat: boolean | null;
3972
3973
  timeCompleted: string | null;
3973
3974
  km: number;
3975
+ type: "Service" | "ProductSell";
3974
3976
  id: string;
3975
3977
  createdAt: string;
3976
3978
  updatedAt: string;
@@ -4055,6 +4057,35 @@ export declare const app: Elysia<"", {
4055
4057
  };
4056
4058
  };
4057
4059
  };
4060
+ } & {
4061
+ "cp-order": {
4062
+ sell: {
4063
+ post: {
4064
+ body: unknown;
4065
+ params: {};
4066
+ query: unknown;
4067
+ headers: unknown;
4068
+ response: {
4069
+ 200: {
4070
+ type: "Service" | "ProductSell";
4071
+ id: string;
4072
+ createdAt: string;
4073
+ updatedAt: string;
4074
+ companyId: string;
4075
+ deletedAt: string | null;
4076
+ isNoat: boolean | null;
4077
+ companyBranchId: string;
4078
+ customerId: string | null;
4079
+ vehicleId: string | null;
4080
+ km: number;
4081
+ orderId: string;
4082
+ state: "CREATED" | "PROGRESSING" | "COMPLETE";
4083
+ timeCompleted: string | null;
4084
+ };
4085
+ };
4086
+ };
4087
+ };
4088
+ };
4058
4089
  } & {
4059
4090
  "cp-order": {
4060
4091
  package: {
@@ -4100,12 +4131,13 @@ export declare const app: Elysia<"", {
4100
4131
  companyId: string;
4101
4132
  companyBranchId: string;
4102
4133
  customerId: string | null;
4103
- vehicleId: string;
4134
+ vehicleId: string | null;
4104
4135
  orderId: string;
4105
4136
  state: "CREATED" | "PROGRESSING" | "COMPLETE";
4106
4137
  isNoat: boolean | null;
4107
4138
  timeCompleted: string | null;
4108
4139
  km: number;
4140
+ type: "Service" | "ProductSell";
4109
4141
  id: string;
4110
4142
  createdAt: string;
4111
4143
  updatedAt: string;
@@ -4130,11 +4162,12 @@ export declare const app: Elysia<"", {
4130
4162
  ":id": {
4131
4163
  put: {
4132
4164
  body: {
4165
+ type?: "Service" | "ProductSell" | undefined;
4133
4166
  companyId?: string | undefined;
4134
4167
  isNoat?: boolean | null | undefined;
4135
4168
  companyBranchId?: string | undefined;
4136
4169
  customerId?: string | null | undefined;
4137
- vehicleId?: string | undefined;
4170
+ vehicleId?: string | null | undefined;
4138
4171
  km?: number | undefined;
4139
4172
  orderId?: string | undefined;
4140
4173
  state?: "CREATED" | "PROGRESSING" | "COMPLETE" | undefined;
@@ -4150,12 +4183,13 @@ export declare const app: Elysia<"", {
4150
4183
  companyId: string;
4151
4184
  companyBranchId: string;
4152
4185
  customerId: string | null;
4153
- vehicleId: string;
4186
+ vehicleId: string | null;
4154
4187
  orderId: string;
4155
4188
  state: "CREATED" | "PROGRESSING" | "COMPLETE";
4156
4189
  isNoat: boolean | null;
4157
4190
  timeCompleted: string | null;
4158
4191
  km: number;
4192
+ type: "Service" | "ProductSell";
4159
4193
  id: string;
4160
4194
  createdAt: string;
4161
4195
  updatedAt: string;
@@ -6206,12 +6240,13 @@ export declare const app: Elysia<"", {
6206
6240
  companyId: string;
6207
6241
  companyBranchId: string;
6208
6242
  customerId: string | null;
6209
- vehicleId: string;
6243
+ vehicleId: string | null;
6210
6244
  orderId: string;
6211
6245
  state: "CREATED" | "PROGRESSING" | "COMPLETE";
6212
6246
  isNoat: boolean | null;
6213
6247
  timeCompleted: string | null;
6214
6248
  km: number;
6249
+ type: "Service" | "ProductSell";
6215
6250
  id: string;
6216
6251
  createdAt: string;
6217
6252
  updatedAt: string;
@@ -6433,12 +6468,13 @@ export declare const app: Elysia<"", {
6433
6468
  companyId: string;
6434
6469
  companyBranchId: string;
6435
6470
  customerId: string | null;
6436
- vehicleId: string;
6471
+ vehicleId: string | null;
6437
6472
  orderId: string;
6438
6473
  state: "CREATED" | "PROGRESSING" | "COMPLETE";
6439
6474
  isNoat: boolean | null;
6440
6475
  timeCompleted: string | null;
6441
6476
  km: number;
6477
+ type: "Service" | "ProductSell";
6442
6478
  id: string;
6443
6479
  createdAt: string;
6444
6480
  updatedAt: string;
@@ -6470,6 +6506,170 @@ export declare const app: Elysia<"", {
6470
6506
  };
6471
6507
  };
6472
6508
  };
6509
+ } & {
6510
+ reports: {
6511
+ sales: {
6512
+ csv: {
6513
+ get: {
6514
+ body: unknown;
6515
+ params: {};
6516
+ query: {
6517
+ employeeId?: string | undefined;
6518
+ method?: string | undefined;
6519
+ companyBranchId?: string | undefined;
6520
+ state?: "CREATED" | "PROGRESSING" | "COMPLETE" | undefined;
6521
+ limit?: number | undefined;
6522
+ startDate?: string | undefined;
6523
+ endDate?: string | undefined;
6524
+ };
6525
+ headers: unknown;
6526
+ response: {
6527
+ 200: string;
6528
+ 422: {
6529
+ type: "validation";
6530
+ on: string;
6531
+ summary?: string;
6532
+ message?: string;
6533
+ found?: unknown;
6534
+ property?: string;
6535
+ expected?: string;
6536
+ };
6537
+ };
6538
+ };
6539
+ };
6540
+ };
6541
+ };
6542
+ } & {
6543
+ reports: {
6544
+ sales: {
6545
+ summary: {
6546
+ csv: {
6547
+ get: {
6548
+ body: unknown;
6549
+ params: {};
6550
+ query: {
6551
+ companyBranchId?: string | undefined;
6552
+ startDate?: string | undefined;
6553
+ endDate?: string | undefined;
6554
+ };
6555
+ headers: unknown;
6556
+ response: {
6557
+ 200: string;
6558
+ 422: {
6559
+ type: "validation";
6560
+ on: string;
6561
+ summary?: string;
6562
+ message?: string;
6563
+ found?: unknown;
6564
+ property?: string;
6565
+ expected?: string;
6566
+ };
6567
+ };
6568
+ };
6569
+ };
6570
+ };
6571
+ };
6572
+ };
6573
+ } & {
6574
+ reports: {
6575
+ sales: {
6576
+ "by-period": {
6577
+ csv: {
6578
+ get: {
6579
+ body: unknown;
6580
+ params: {};
6581
+ query: {
6582
+ companyBranchId?: string | undefined;
6583
+ startDate?: string | undefined;
6584
+ endDate?: string | undefined;
6585
+ groupBy: "day" | "week" | "month" | "year";
6586
+ };
6587
+ headers: unknown;
6588
+ response: {
6589
+ 200: string;
6590
+ 422: {
6591
+ type: "validation";
6592
+ on: string;
6593
+ summary?: string;
6594
+ message?: string;
6595
+ found?: unknown;
6596
+ property?: string;
6597
+ expected?: string;
6598
+ };
6599
+ };
6600
+ };
6601
+ };
6602
+ };
6603
+ };
6604
+ };
6605
+ } & {
6606
+ reports: {
6607
+ sales: {
6608
+ "by-employee": {
6609
+ csv: {
6610
+ get: {
6611
+ body: unknown;
6612
+ params: {};
6613
+ query: {
6614
+ employeeId?: string | undefined;
6615
+ method?: string | undefined;
6616
+ companyBranchId?: string | undefined;
6617
+ state?: "CREATED" | "PROGRESSING" | "COMPLETE" | undefined;
6618
+ limit?: number | undefined;
6619
+ startDate?: string | undefined;
6620
+ endDate?: string | undefined;
6621
+ };
6622
+ headers: unknown;
6623
+ response: {
6624
+ 200: string;
6625
+ 422: {
6626
+ type: "validation";
6627
+ on: string;
6628
+ summary?: string;
6629
+ message?: string;
6630
+ found?: unknown;
6631
+ property?: string;
6632
+ expected?: string;
6633
+ };
6634
+ };
6635
+ };
6636
+ };
6637
+ };
6638
+ };
6639
+ };
6640
+ } & {
6641
+ reports: {
6642
+ payments: {
6643
+ csv: {
6644
+ get: {
6645
+ body: unknown;
6646
+ params: {};
6647
+ query: {
6648
+ employeeId?: string | undefined;
6649
+ method?: string | undefined;
6650
+ companyBranchId?: string | undefined;
6651
+ state?: "CREATED" | "PROGRESSING" | "COMPLETE" | undefined;
6652
+ limit?: number | undefined;
6653
+ startDate?: string | undefined;
6654
+ endDate?: string | undefined;
6655
+ };
6656
+ headers: unknown;
6657
+ response: {
6658
+ 200: string;
6659
+ 422: {
6660
+ type: "validation";
6661
+ on: string;
6662
+ summary?: string;
6663
+ message?: string;
6664
+ found?: unknown;
6665
+ property?: string;
6666
+ expected?: string;
6667
+ };
6668
+ };
6669
+ };
6670
+ };
6671
+ };
6672
+ };
6473
6673
  };
6474
6674
  };
6475
6675
  } & {
package/dist/index.js CHANGED
@@ -147067,6 +147067,7 @@ __export(exports_schema, {
147067
147067
  crmCustomerVehicleTable: () => crmCustomerVehicleTable,
147068
147068
  crmCustomerTable: () => crmCustomerTable,
147069
147069
  crmCustomerRelations: () => crmCustomerRelations,
147070
+ crmCpOrderTypeEnum: () => crmCpOrderTypeEnum,
147070
147071
  crmCpOrderTable: () => crmCpOrderTable,
147071
147072
  crmCpOrderStateEnum: () => crmCpOrderStateEnum,
147072
147073
  crmCpOrderSpPackageTable: () => crmCpOrderSpPackageTable,
@@ -147390,12 +147391,16 @@ var crmCpOrderStateEnum = pgEnum("cp_order_state", [
147390
147391
  "PROGRESSING",
147391
147392
  "COMPLETE"
147392
147393
  ]);
147394
+ var crmCpOrderTypeEnum = pgEnum("cp_order_type", [
147395
+ "Service",
147396
+ "ProductSell"
147397
+ ]);
147393
147398
  var crmCpOrderTable = crmSchema.table("cp_order", {
147394
147399
  ...base_schema_helper_default,
147395
147400
  companyId: uuid3().notNull(),
147396
147401
  companyBranchId: uuid3().notNull(),
147397
147402
  customerId: uuid3(),
147398
- vehicleId: uuid3().notNull(),
147403
+ vehicleId: uuid3(),
147399
147404
  orderId: varchar().notNull(),
147400
147405
  state: crmCpOrderStateEnum("state").notNull().default("CREATED"),
147401
147406
  isNoat: boolean4().default(false),
@@ -147403,7 +147408,8 @@ var crmCpOrderTable = crmSchema.table("cp_order", {
147403
147408
  withTimezone: true,
147404
147409
  mode: "string"
147405
147410
  }),
147406
- km: integer2().notNull().default(0)
147411
+ km: integer2().notNull().default(0),
147412
+ type: crmCpOrderTypeEnum().default("Service").notNull()
147407
147413
  }, (t2) => [
147408
147414
  index().on(t2.companyId),
147409
147415
  index().on(t2.companyBranchId),
@@ -155079,21 +155085,32 @@ var CrmCpOrderLogic;
155079
155085
  if (vehicle.customerId && !customerId) {
155080
155086
  customerId = vehicle.customerId;
155081
155087
  }
155082
- const orderId = await getOrderId(user2);
155088
+ const orderId = await getOrderId("Service", user2);
155083
155089
  const [result] = await db2.insert(crmCpOrderTable).values({
155084
155090
  companyId: user2.companyId,
155085
155091
  companyBranchId: user2.branchId,
155086
155092
  customerId,
155087
155093
  vehicleId: vehicle.id,
155088
- orderId
155094
+ orderId,
155095
+ type: "Service"
155089
155096
  }).returning();
155090
155097
  return result;
155091
155098
  });
155092
155099
  };
155093
- const getOrderId = async (user2) => {
155100
+ CrmCpOrderLogic.createSell = async (user2) => {
155101
+ const orderId = await getOrderId("ProductSell", user2);
155102
+ const [result] = await db_default.insert(crmCpOrderTable).values({
155103
+ companyBranchId: user2.branchId,
155104
+ companyId: user2.companyId,
155105
+ orderId,
155106
+ type: "Service"
155107
+ }).returning();
155108
+ return result;
155109
+ };
155110
+ const getOrderId = async (type, user2) => {
155094
155111
  const today = new Date;
155095
155112
  const formatted = `${today.getFullYear()}${(today.getMonth() + 1).toString().padStart(2, "0")}${today.getDate().toString().padStart(2, "0")}`;
155096
- const cacheKey = `cp_order_id:${user2.companyId}:${user2.branchId}:${formatted}`;
155113
+ const cacheKey = `cp_order${type === "ProductSell" ? "_sell_" : ""}_id:${user2.companyId}:${user2.branchId}:${formatted}`;
155097
155114
  const cached3 = await redis_client_default.get(cacheKey);
155098
155115
  if (cached3) {
155099
155116
  const count2 = cached3 + 1;
@@ -155103,7 +155120,7 @@ var CrmCpOrderLogic;
155103
155120
  }
155104
155121
  const [latest] = await db_default.select({
155105
155122
  orderId: crmCpOrderTable.orderId
155106
- }).from(crmCpOrderTable).where(and(ilike(crmCpOrderTable.orderId, `${formatted}%`), eq(crmCpOrderTable.companyId, user2.companyId), eq(crmCpOrderTable.companyBranchId, user2.branchId))).orderBy(desc(crmCpOrderTable.orderId)).limit(1);
155123
+ }).from(crmCpOrderTable).where(and(ilike(crmCpOrderTable.orderId, `${formatted}%`), eq(crmCpOrderTable.companyId, user2.companyId), eq(crmCpOrderTable.companyBranchId, user2.branchId), eq(crmCpOrderTable.type, type))).orderBy(desc(crmCpOrderTable.orderId)).limit(1);
155107
155124
  if (latest) {
155108
155125
  const count2 = +latest.orderId.split("-")[1] + 1;
155109
155126
  await redis_client_default.set(cacheKey, count2);
@@ -155178,9 +155195,11 @@ var CrmCpOrderLogic;
155178
155195
  if (!order) {
155179
155196
  return status("Not Found", "\u0417\u0430\u0441\u0432\u0430\u0440 \u04AF\u0439\u043B\u0447\u0438\u043B\u0433\u044D\u044D \u043E\u043B\u0434\u0441\u043E\u043D\u0433\u04AF\u0439.");
155180
155197
  }
155181
- await db2.update(crmVehicleTable).set({
155182
- km: body.km
155183
- }).where(eq(crmVehicleTable.id, order.vehicleId));
155198
+ if (order.vehicleId) {
155199
+ await db2.update(crmVehicleTable).set({
155200
+ km: body.km
155201
+ }).where(eq(crmVehicleTable.id, order.vehicleId));
155202
+ }
155184
155203
  return order;
155185
155204
  });
155186
155205
  };
@@ -155804,7 +155823,7 @@ var cpOrderRoutes = new Elysia({
155804
155823
  return result;
155805
155824
  }, {
155806
155825
  body: model_default8.create
155807
- }).post("/package", async ({ body, user: user2 }) => logic_default12.addPackage(body, user2), {
155826
+ }).post("/sell", async ({ user: user2 }) => logic_default12.createSell(user2)).post("/package", async ({ body, user: user2 }) => logic_default12.addPackage(body, user2), {
155808
155827
  body: model_default8.addPackage
155809
155828
  }).post("/complete", async ({ user: user2, body }) => logic_default12.complete(body, user2), {
155810
155829
  body: model_default8.complete
@@ -156189,43 +156208,33 @@ var CrmReportsLogic;
156189
156208
  };
156190
156209
  CrmReportsLogic.salesSummary = async (query, user2) => {
156191
156210
  const filter = buildCommonFilters(query, user2);
156211
+ const orderItems = db_default.$with("order_items").as(db_default.select({
156212
+ cpOrderId: crmCpOrderItemTable.cpOrderId,
156213
+ totalItems: sum(crmCpOrderItemTable.priceTotal).as("total_items")
156214
+ }).from(crmCpOrderItemTable).where(isNull2(crmCpOrderItemTable.deletedAt)).groupBy((t2) => t2.cpOrderId));
156215
+ const orderPayments = db_default.$with("order_payments").as(db_default.select({
156216
+ cpOrderId: crmCpOrderPaymentTable.cpOrderId,
156217
+ totalPaid: sum(crmCpOrderPaymentTable.amount).as("total_paid")
156218
+ }).from(crmCpOrderPaymentTable).where(and(isNull2(crmCpOrderPaymentTable.deletedAt), eq(crmCpOrderPaymentTable.state, "PAID"))).groupBy((t2) => t2.cpOrderId));
156192
156219
  const [summary] = await db_default.select({
156193
156220
  totalOrders: count(crmCpOrderTable.id),
156194
- totalRevenue: sql3`
156195
- COALESCE(SUM(
156196
- (SELECT SUM(${crmCpOrderItemTable.priceTotal})
156197
- FROM ${crmCpOrderItemTable}
156198
- WHERE ${crmCpOrderItemTable.cpOrderId} = ${crmCpOrderTable.id}
156199
- AND ${crmCpOrderItemTable.deletedAt} IS NULL)
156200
- ), 0)
156201
- `,
156221
+ totalRevenue: sql3`COALESCE(SUM(${orderItems.totalItems}),0)`.as("total_revenue"),
156202
156222
  totalPaidAmount: sql3`
156203
- COALESCE(SUM(
156204
- (SELECT SUM(${crmCpOrderPaymentTable.amount})
156205
- FROM ${crmCpOrderPaymentTable}
156206
- WHERE ${crmCpOrderPaymentTable.cpOrderId} = ${crmCpOrderTable.id}
156207
- AND ${crmCpOrderPaymentTable.state} = 'PAID'
156208
- AND ${crmCpOrderPaymentTable.deletedAt} IS NULL)
156209
- ), 0)
156210
- `,
156223
+ COALESCE(SUM(${orderPayments.totalPaid}), 0)
156224
+ `.as("total_paid_amount"),
156211
156225
  averageOrderValue: sql3`
156212
- COALESCE(AVG(
156213
- (SELECT SUM(${crmCpOrderItemTable.priceTotal})
156214
- FROM ${crmCpOrderItemTable}
156215
- WHERE ${crmCpOrderItemTable.cpOrderId} = ${crmCpOrderTable.id}
156216
- AND ${crmCpOrderItemTable.deletedAt} IS NULL)
156217
- ), 0)
156218
- `,
156226
+ COALESCE(AVG(${orderItems.totalItems}), 0)
156227
+ `.as("average_order_value"),
156219
156228
  ordersCompleted: sql3`
156220
156229
  COUNT(CASE WHEN ${crmCpOrderTable.state} = 'COMPLETE' THEN 1 END)
156221
- `,
156230
+ `.as("orders_completed"),
156222
156231
  ordersPending: sql3`
156223
156232
  COUNT(CASE WHEN ${crmCpOrderTable.state} = 'CREATED' THEN 1 END)
156224
- `,
156233
+ `.as("orders_pending"),
156225
156234
  ordersCancelled: sql3`
156226
156235
  COUNT(CASE WHEN ${crmCpOrderTable.state} = 'CANCELLED' THEN 1 END)
156227
- `
156228
- }).from(crmCpOrderTable).where(filter);
156236
+ `.as("orders_cancelled")
156237
+ }).from(crmCpOrderTable).leftJoin(orderItems, eq(crmCpOrderTable.id, orderItems.cpOrderId)).leftJoin(orderPayments, eq(crmCpOrderTable.id, orderPayments.cpOrderId)).where(filter);
156229
156238
  return {
156230
156239
  totalOrders: summary.totalOrders,
156231
156240
  totalRevenue: summary.totalRevenue,
@@ -156296,6 +156305,154 @@ var CrmReportsLogic;
156296
156305
  }).from(crmCpOrderPaymentTable).innerJoin(crmCpOrderTable, eq(crmCpOrderTable.id, crmCpOrderPaymentTable.cpOrderId)).leftJoin(crmCustomerTable, eq(crmCustomerTable.id, crmCpOrderTable.customerId)).where(filter).orderBy(desc(crmCpOrderPaymentTable.createdAt)).$dynamic();
156297
156306
  return await pagination_helper_default(baseQuery, query.pagination);
156298
156307
  };
156308
+ const formatCSV = (headers, rows) => {
156309
+ const csvHeaders = headers.map((h) => `"${h}"`).join(",");
156310
+ const csvRows = rows.map((row) => row.map((cell) => {
156311
+ if (cell === null || cell === undefined)
156312
+ return '""';
156313
+ return `"${String(cell).replace(/"/g, '""')}"`;
156314
+ }).join(","));
156315
+ return [csvHeaders, ...csvRows].join(`
156316
+ `);
156317
+ };
156318
+ CrmReportsLogic.exportSalesCSV = async (query, user2) => {
156319
+ const filter = and(buildCommonFilters(query, user2), eq(crmCpOrderTable.state, query.state).if(query.state));
156320
+ const result = await db_default.select({
156321
+ orderId: crmCpOrderTable.orderId,
156322
+ customerName: sql3`CONCAT(${crmCustomerTable.firstname}, ' ', ${crmCustomerTable.lastname})`,
156323
+ customerPhone: crmCustomerTable.phoneNumber,
156324
+ vehiclePlate: crmVehicleTable.licensePlate,
156325
+ vehicleKind: techdocVehicleKindTable.name,
156326
+ orderState: crmCpOrderTable.state,
156327
+ createdAt: crmCpOrderTable.createdAt,
156328
+ totalAmount: sql3`
156329
+ COALESCE(
156330
+ (SELECT SUM(${crmCpOrderItemTable.priceTotal})
156331
+ FROM ${crmCpOrderItemTable}
156332
+ WHERE ${crmCpOrderItemTable.cpOrderId} = ${crmCpOrderTable.id}
156333
+ AND ${crmCpOrderItemTable.deletedAt} IS NULL),
156334
+ 0
156335
+ )
156336
+ `,
156337
+ paidAmount: sql3`
156338
+ COALESCE(
156339
+ (SELECT SUM(${crmCpOrderPaymentTable.amount})
156340
+ FROM ${crmCpOrderPaymentTable}
156341
+ WHERE ${crmCpOrderPaymentTable.cpOrderId} = ${crmCpOrderTable.id}
156342
+ AND ${crmCpOrderPaymentTable.state} = 'PAID'
156343
+ AND ${crmCpOrderPaymentTable.deletedAt} IS NULL),
156344
+ 0
156345
+ )
156346
+ `
156347
+ }).from(crmCpOrderTable).leftJoin(crmCustomerTable, eq(crmCustomerTable.id, crmCpOrderTable.customerId)).leftJoin(crmVehicleTable, eq(crmVehicleTable.id, crmCpOrderTable.vehicleId)).leftJoin(techdocVehicleKindTable, eq(techdocVehicleKindTable.id, crmVehicleTable.vehicleKindId)).where(filter).orderBy(desc(crmCpOrderTable.createdAt)).limit(query.limit || 1e4);
156348
+ const headers = [
156349
+ "\u0417\u0430\u0445\u0438\u0430\u043B\u0433\u044B\u043D \u0434\u0443\u0433\u0430\u0430\u0440",
156350
+ "\u04AE\u0439\u043B\u0447\u043B\u04AF\u04AF\u043B\u044D\u0433\u0447",
156351
+ "\u0423\u0442\u0430\u0441",
156352
+ "\u0422\u044D\u044D\u0432\u0440\u0438\u0439\u043D \u0445\u044D\u0440\u044D\u0433\u0441\u043B\u0438\u0439\u043D \u0434\u0443\u0433\u0430\u0430\u0440",
156353
+ "\u041C\u0430\u0440\u043A \u0437\u0430\u0433\u0432\u0430\u0440",
156354
+ "\u0422\u04E9\u043B\u04E9\u0432",
156355
+ "\u041E\u0433\u043D\u043E\u043E",
156356
+ "\u041D\u0438\u0439\u0442 \u0434\u04AF\u043D",
156357
+ "\u0422\u04E9\u043B\u0441\u04E9\u043D \u0434\u04AF\u043D",
156358
+ "\u04AE\u043B\u0434\u044D\u0433\u0434\u044D\u043B"
156359
+ ];
156360
+ const rows = result.map((order) => [
156361
+ order.orderId,
156362
+ order.customerName || "\u041C\u044D\u0434\u044D\u0433\u0434\u044D\u0445\u0433\u04AF\u0439",
156363
+ order.customerPhone || "",
156364
+ order.vehiclePlate || "",
156365
+ order.vehicleKind || "",
156366
+ order.orderState,
156367
+ order.createdAt,
156368
+ order.totalAmount,
156369
+ order.paidAmount,
156370
+ order.totalAmount - order.paidAmount
156371
+ ]);
156372
+ return formatCSV(headers, rows);
156373
+ };
156374
+ CrmReportsLogic.exportSalesSummaryCSV = async (query, user2) => {
156375
+ const summary = await CrmReportsLogic.salesSummary(query, user2);
156376
+ const headers = ["\u041C\u0435\u0442\u0440\u0438\u043A", "\u0423\u0442\u0433\u0430"];
156377
+ const rows = [
156378
+ ["\u041D\u0438\u0439\u0442 \u0437\u0430\u0445\u0438\u0430\u043B\u0433\u0430", summary.totalOrders],
156379
+ ["\u041D\u0438\u0439\u0442 \u043E\u0440\u043B\u043E\u0433\u043E", summary.totalRevenue],
156380
+ ["\u041D\u0438\u0439\u0442 \u0442\u04E9\u043B\u0441\u04E9\u043D \u0434\u04AF\u043D", summary.totalPaidAmount],
156381
+ ["\u0422\u04E9\u043B\u04E9\u04E9\u0433\u04AF\u0439 \u0434\u04AF\u043D", summary.totalPendingAmount],
156382
+ ["\u0414\u0443\u043D\u0434\u0430\u0436 \u0437\u0430\u0445\u0438\u0430\u043B\u0433\u044B\u043D \u0434\u04AF\u043D", summary.averageOrderValue],
156383
+ ["\u0414\u0443\u0443\u0441\u0433\u0430\u0441\u0430\u043D \u0437\u0430\u0445\u0438\u0430\u043B\u0433\u0430", summary.ordersCompleted],
156384
+ ["\u0425\u04AF\u043B\u044D\u044D\u0433\u0434\u044D\u0436 \u0431\u0443\u0439 \u0437\u0430\u0445\u0438\u0430\u043B\u0433\u0430", summary.ordersPending],
156385
+ ["\u0426\u0443\u0446\u043B\u0430\u0433\u0434\u0441\u0430\u043D \u0437\u0430\u0445\u0438\u0430\u043B\u0433\u0430", summary.ordersCancelled]
156386
+ ];
156387
+ return formatCSV(headers, rows);
156388
+ };
156389
+ CrmReportsLogic.exportSalesByPeriodCSV = async (query, user2) => {
156390
+ const result = await CrmReportsLogic.salesByPeriod(query, user2);
156391
+ const headers = ["\u0425\u0443\u0433\u0430\u0446\u0430\u0430", "\u0417\u0430\u0445\u0438\u0430\u043B\u0433\u044B\u043D \u0442\u043E\u043E", "\u041D\u0438\u0439\u0442 \u043E\u0440\u043B\u043E\u0433\u043E", "\u0422\u04E9\u043B\u0441\u04E9\u043D \u0434\u04AF\u043D"];
156392
+ const rows = result.map((period) => [
156393
+ period.period,
156394
+ period.totalOrders,
156395
+ period.totalRevenue,
156396
+ period.totalPaidAmount
156397
+ ]);
156398
+ return formatCSV(headers, rows);
156399
+ };
156400
+ CrmReportsLogic.exportSalesByEmployeeCSV = async (query, user2) => {
156401
+ const filter = buildCommonFilters(query, user2);
156402
+ const result = await db_default.select({
156403
+ employeeName: sql3`CONCAT(${companyEmployeeTable.firstname}, ' ', ${companyEmployeeTable.lastname})`,
156404
+ totalOrders: count(sql3`DISTINCT ${crmCpOrderTable.id}`),
156405
+ totalRevenue: sql3`COALESCE(SUM(${crmCpOrderItemEmployeeTable.amount}), 0)`,
156406
+ totalCommission: sql3`COALESCE(SUM(${crmCpOrderItemEmployeeTable.amount}) * 0.1, 0)`
156407
+ }).from(crmCpOrderItemEmployeeTable).innerJoin(crmCpOrderItemTable, eq(crmCpOrderItemTable.id, crmCpOrderItemEmployeeTable.cpOrderItemId)).innerJoin(crmCpOrderTable, eq(crmCpOrderTable.id, crmCpOrderItemTable.cpOrderId)).innerJoin(companyEmployeeTable, eq(companyEmployeeTable.id, crmCpOrderItemEmployeeTable.employeeId)).where(and(filter, softDeletedFilter(crmCpOrderItemEmployeeTable), softDeletedFilter(crmCpOrderItemTable), softDeletedFilter(companyEmployeeTable))).groupBy(crmCpOrderItemEmployeeTable.employeeId, companyEmployeeTable.firstname, companyEmployeeTable.lastname).orderBy(desc(sql3`SUM(${crmCpOrderItemEmployeeTable.amount})`)).limit(query.limit || 1e4);
156408
+ const headers = [
156409
+ "\u0410\u0436\u0438\u043B\u0442\u043D\u044B \u043D\u044D\u0440",
156410
+ "\u0417\u0430\u0445\u0438\u0430\u043B\u0433\u044B\u043D \u0442\u043E\u043E",
156411
+ "\u041D\u0438\u0439\u0442 \u0431\u043E\u0440\u043B\u0443\u0443\u043B\u0430\u043B\u0442",
156412
+ "\u041A\u043E\u043C\u0438\u0441\u0441"
156413
+ ];
156414
+ const rows = result.map((employee) => [
156415
+ employee.employeeName,
156416
+ employee.totalOrders,
156417
+ employee.totalRevenue,
156418
+ employee.totalCommission
156419
+ ]);
156420
+ return formatCSV(headers, rows);
156421
+ };
156422
+ CrmReportsLogic.exportPaymentsCSV = async (query, user2) => {
156423
+ const filter = and(buildCommonFilters(query, user2), eq(crmCpOrderPaymentTable.paymentMethod, query.method).if(query.method), softDeletedFilter(crmCpOrderPaymentTable));
156424
+ const result = await db_default.select({
156425
+ orderId: crmCpOrderTable.orderId,
156426
+ customerName: sql3`CONCAT(${crmCustomerTable.firstname}, ' ', ${crmCustomerTable.lastname})`,
156427
+ paymentMethod: crmCpOrderPaymentTable.paymentMethod,
156428
+ paymentChannel: crmCpOrderPaymentTable.paymentChannel,
156429
+ amount: crmCpOrderPaymentTable.amount,
156430
+ state: crmCpOrderPaymentTable.state,
156431
+ invoiceNumber: crmCpOrderPaymentTable.invoiceNumber,
156432
+ createdAt: crmCpOrderPaymentTable.createdAt
156433
+ }).from(crmCpOrderPaymentTable).innerJoin(crmCpOrderTable, eq(crmCpOrderTable.id, crmCpOrderPaymentTable.cpOrderId)).leftJoin(crmCustomerTable, eq(crmCustomerTable.id, crmCpOrderTable.customerId)).where(filter).orderBy(desc(crmCpOrderPaymentTable.createdAt)).limit(query.limit || 1e4);
156434
+ const headers = [
156435
+ "\u0417\u0430\u0445\u0438\u0430\u043B\u0433\u044B\u043D \u0434\u0443\u0433\u0430\u0430\u0440",
156436
+ "\u04AE\u0439\u043B\u0447\u043B\u04AF\u04AF\u043B\u044D\u0433\u0447",
156437
+ "\u0422\u04E9\u043B\u0431\u04E9\u0440\u0438\u0439\u043D \u0445\u044D\u0440\u044D\u0433\u0441\u044D\u043B",
156438
+ "\u0422\u04E9\u043B\u0431\u04E9\u0440\u0438\u0439\u043D \u0441\u0443\u0432\u0430\u0433",
156439
+ "\u0414\u04AF\u043D",
156440
+ "\u0422\u04E9\u043B\u04E9\u0432",
156441
+ "\u041D\u044D\u0445\u044D\u043C\u0436\u043B\u044D\u0445 \u0434\u0443\u0433\u0430\u0430\u0440",
156442
+ "\u041E\u0433\u043D\u043E\u043E"
156443
+ ];
156444
+ const rows = result.map((payment) => [
156445
+ payment.orderId,
156446
+ payment.customerName || "\u041C\u044D\u0434\u044D\u0433\u0434\u044D\u0445\u0433\u04AF\u0439",
156447
+ payment.paymentMethod,
156448
+ payment.paymentChannel,
156449
+ payment.amount,
156450
+ payment.state,
156451
+ payment.invoiceNumber || "",
156452
+ payment.createdAt
156453
+ ]);
156454
+ return formatCSV(headers, rows);
156455
+ };
156299
156456
  })(CrmReportsLogic ||= {});
156300
156457
  var logic_default15 = CrmReportsLogic;
156301
156458
 
@@ -156338,6 +156495,17 @@ var CrmReportsModel;
156338
156495
  state: t.String()
156339
156496
  }))
156340
156497
  ]);
156498
+ CrmReportsModel.csvExportQuery = t.Composite([
156499
+ CrmReportsModel.baseDateFilter,
156500
+ t.Partial(t.Object({
156501
+ state: t.UnionEnum(crmCpOrderStateEnum.enumValues, {
156502
+ default: undefined
156503
+ }),
156504
+ employeeId: t.String({ format: "uuid" }),
156505
+ method: t.String(),
156506
+ limit: t.Number({ default: 1e4, maximum: 50000 })
156507
+ }))
156508
+ ]);
156341
156509
  CrmReportsModel.salesSummaryResponse = t.Object({
156342
156510
  totalOrders: t.Number(),
156343
156511
  totalRevenue: t.Number(),
@@ -156388,6 +156556,41 @@ var reportsRoutes = new Elysia({
156388
156556
  query: model_default13.salesByEmployeeQuery
156389
156557
  }).get("/payments", async ({ query, user: user2 }) => logic_default15.paymentsReport(query, user2), {
156390
156558
  query: model_default13.paymentsReportQuery
156559
+ }).get("/sales/csv", async ({ query, user: user2, set: set3 }) => {
156560
+ const csvData = await logic_default15.exportSalesCSV(query, user2);
156561
+ set3.headers["Content-Type"] = "text/csv";
156562
+ set3.headers["Content-Disposition"] = 'attachment; filename="sales-report.csv"';
156563
+ return csvData;
156564
+ }, {
156565
+ query: model_default13.csvExportQuery
156566
+ }).get("/sales/summary/csv", async ({ query, user: user2, set: set3 }) => {
156567
+ const csvData = await logic_default15.exportSalesSummaryCSV(query, user2);
156568
+ set3.headers["Content-Type"] = "text/csv";
156569
+ set3.headers["Content-Disposition"] = 'attachment; filename="sales-summary.csv"';
156570
+ return csvData;
156571
+ }, {
156572
+ query: model_default13.baseDateFilter
156573
+ }).get("/sales/by-period/csv", async ({ query, user: user2, set: set3 }) => {
156574
+ const csvData = await logic_default15.exportSalesByPeriodCSV(query, user2);
156575
+ set3.headers["Content-Type"] = "text/csv";
156576
+ set3.headers["Content-Disposition"] = 'attachment; filename="sales-by-period.csv"';
156577
+ return csvData;
156578
+ }, {
156579
+ query: model_default13.salesByPeriodQuery
156580
+ }).get("/sales/by-employee/csv", async ({ query, user: user2, set: set3 }) => {
156581
+ const csvData = await logic_default15.exportSalesByEmployeeCSV(query, user2);
156582
+ set3.headers["Content-Type"] = "text/csv";
156583
+ set3.headers["Content-Disposition"] = 'attachment; filename="sales-by-employee.csv"';
156584
+ return csvData;
156585
+ }, {
156586
+ query: model_default13.csvExportQuery
156587
+ }).get("/payments/csv", async ({ query, user: user2, set: set3 }) => {
156588
+ const csvData = await logic_default15.exportPaymentsCSV(query, user2);
156589
+ set3.headers["Content-Type"] = "text/csv";
156590
+ set3.headers["Content-Disposition"] = 'attachment; filename="payments-report.csv"';
156591
+ return csvData;
156592
+ }, {
156593
+ query: model_default13.csvExportQuery
156391
156594
  });
156392
156595
  var reports_default = reportsRoutes;
156393
156596
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autosync_backend2",
3
- "version": "1.1.68",
3
+ "version": "1.1.70",
4
4
  "module": "src/index.ts",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",