autosync_backend2 1.1.68 → 1.1.69

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" | "ProductSale";
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" | "ProductSale";
3974
3976
  id: string;
3975
3977
  createdAt: string;
3976
3978
  updatedAt: string;
@@ -4100,12 +4102,13 @@ export declare const app: Elysia<"", {
4100
4102
  companyId: string;
4101
4103
  companyBranchId: string;
4102
4104
  customerId: string | null;
4103
- vehicleId: string;
4105
+ vehicleId: string | null;
4104
4106
  orderId: string;
4105
4107
  state: "CREATED" | "PROGRESSING" | "COMPLETE";
4106
4108
  isNoat: boolean | null;
4107
4109
  timeCompleted: string | null;
4108
4110
  km: number;
4111
+ type: "Service" | "ProductSale";
4109
4112
  id: string;
4110
4113
  createdAt: string;
4111
4114
  updatedAt: string;
@@ -4130,11 +4133,12 @@ export declare const app: Elysia<"", {
4130
4133
  ":id": {
4131
4134
  put: {
4132
4135
  body: {
4136
+ type?: "Service" | "ProductSale" | undefined;
4133
4137
  companyId?: string | undefined;
4134
4138
  isNoat?: boolean | null | undefined;
4135
4139
  companyBranchId?: string | undefined;
4136
4140
  customerId?: string | null | undefined;
4137
- vehicleId?: string | undefined;
4141
+ vehicleId?: string | null | undefined;
4138
4142
  km?: number | undefined;
4139
4143
  orderId?: string | undefined;
4140
4144
  state?: "CREATED" | "PROGRESSING" | "COMPLETE" | undefined;
@@ -4150,12 +4154,13 @@ export declare const app: Elysia<"", {
4150
4154
  companyId: string;
4151
4155
  companyBranchId: string;
4152
4156
  customerId: string | null;
4153
- vehicleId: string;
4157
+ vehicleId: string | null;
4154
4158
  orderId: string;
4155
4159
  state: "CREATED" | "PROGRESSING" | "COMPLETE";
4156
4160
  isNoat: boolean | null;
4157
4161
  timeCompleted: string | null;
4158
4162
  km: number;
4163
+ type: "Service" | "ProductSale";
4159
4164
  id: string;
4160
4165
  createdAt: string;
4161
4166
  updatedAt: string;
@@ -6206,12 +6211,13 @@ export declare const app: Elysia<"", {
6206
6211
  companyId: string;
6207
6212
  companyBranchId: string;
6208
6213
  customerId: string | null;
6209
- vehicleId: string;
6214
+ vehicleId: string | null;
6210
6215
  orderId: string;
6211
6216
  state: "CREATED" | "PROGRESSING" | "COMPLETE";
6212
6217
  isNoat: boolean | null;
6213
6218
  timeCompleted: string | null;
6214
6219
  km: number;
6220
+ type: "Service" | "ProductSale";
6215
6221
  id: string;
6216
6222
  createdAt: string;
6217
6223
  updatedAt: string;
@@ -6433,12 +6439,13 @@ export declare const app: Elysia<"", {
6433
6439
  companyId: string;
6434
6440
  companyBranchId: string;
6435
6441
  customerId: string | null;
6436
- vehicleId: string;
6442
+ vehicleId: string | null;
6437
6443
  orderId: string;
6438
6444
  state: "CREATED" | "PROGRESSING" | "COMPLETE";
6439
6445
  isNoat: boolean | null;
6440
6446
  timeCompleted: string | null;
6441
6447
  km: number;
6448
+ type: "Service" | "ProductSale";
6442
6449
  id: string;
6443
6450
  createdAt: string;
6444
6451
  updatedAt: string;
@@ -6470,6 +6477,170 @@ export declare const app: Elysia<"", {
6470
6477
  };
6471
6478
  };
6472
6479
  };
6480
+ } & {
6481
+ reports: {
6482
+ sales: {
6483
+ csv: {
6484
+ get: {
6485
+ body: unknown;
6486
+ params: {};
6487
+ query: {
6488
+ employeeId?: string | undefined;
6489
+ method?: string | undefined;
6490
+ companyBranchId?: string | undefined;
6491
+ state?: "CREATED" | "PROGRESSING" | "COMPLETE" | undefined;
6492
+ limit?: number | undefined;
6493
+ startDate?: string | undefined;
6494
+ endDate?: string | undefined;
6495
+ };
6496
+ headers: unknown;
6497
+ response: {
6498
+ 200: string;
6499
+ 422: {
6500
+ type: "validation";
6501
+ on: string;
6502
+ summary?: string;
6503
+ message?: string;
6504
+ found?: unknown;
6505
+ property?: string;
6506
+ expected?: string;
6507
+ };
6508
+ };
6509
+ };
6510
+ };
6511
+ };
6512
+ };
6513
+ } & {
6514
+ reports: {
6515
+ sales: {
6516
+ summary: {
6517
+ csv: {
6518
+ get: {
6519
+ body: unknown;
6520
+ params: {};
6521
+ query: {
6522
+ companyBranchId?: string | undefined;
6523
+ startDate?: string | undefined;
6524
+ endDate?: string | undefined;
6525
+ };
6526
+ headers: unknown;
6527
+ response: {
6528
+ 200: string;
6529
+ 422: {
6530
+ type: "validation";
6531
+ on: string;
6532
+ summary?: string;
6533
+ message?: string;
6534
+ found?: unknown;
6535
+ property?: string;
6536
+ expected?: string;
6537
+ };
6538
+ };
6539
+ };
6540
+ };
6541
+ };
6542
+ };
6543
+ };
6544
+ } & {
6545
+ reports: {
6546
+ sales: {
6547
+ "by-period": {
6548
+ csv: {
6549
+ get: {
6550
+ body: unknown;
6551
+ params: {};
6552
+ query: {
6553
+ companyBranchId?: string | undefined;
6554
+ startDate?: string | undefined;
6555
+ endDate?: string | undefined;
6556
+ groupBy: "day" | "week" | "month" | "year";
6557
+ };
6558
+ headers: unknown;
6559
+ response: {
6560
+ 200: string;
6561
+ 422: {
6562
+ type: "validation";
6563
+ on: string;
6564
+ summary?: string;
6565
+ message?: string;
6566
+ found?: unknown;
6567
+ property?: string;
6568
+ expected?: string;
6569
+ };
6570
+ };
6571
+ };
6572
+ };
6573
+ };
6574
+ };
6575
+ };
6576
+ } & {
6577
+ reports: {
6578
+ sales: {
6579
+ "by-employee": {
6580
+ csv: {
6581
+ get: {
6582
+ body: unknown;
6583
+ params: {};
6584
+ query: {
6585
+ employeeId?: string | undefined;
6586
+ method?: string | undefined;
6587
+ companyBranchId?: string | undefined;
6588
+ state?: "CREATED" | "PROGRESSING" | "COMPLETE" | undefined;
6589
+ limit?: number | undefined;
6590
+ startDate?: string | undefined;
6591
+ endDate?: string | undefined;
6592
+ };
6593
+ headers: unknown;
6594
+ response: {
6595
+ 200: string;
6596
+ 422: {
6597
+ type: "validation";
6598
+ on: string;
6599
+ summary?: string;
6600
+ message?: string;
6601
+ found?: unknown;
6602
+ property?: string;
6603
+ expected?: string;
6604
+ };
6605
+ };
6606
+ };
6607
+ };
6608
+ };
6609
+ };
6610
+ };
6611
+ } & {
6612
+ reports: {
6613
+ payments: {
6614
+ csv: {
6615
+ get: {
6616
+ body: unknown;
6617
+ params: {};
6618
+ query: {
6619
+ employeeId?: string | undefined;
6620
+ method?: string | undefined;
6621
+ companyBranchId?: string | undefined;
6622
+ state?: "CREATED" | "PROGRESSING" | "COMPLETE" | undefined;
6623
+ limit?: number | undefined;
6624
+ startDate?: string | undefined;
6625
+ endDate?: string | undefined;
6626
+ };
6627
+ headers: unknown;
6628
+ response: {
6629
+ 200: string;
6630
+ 422: {
6631
+ type: "validation";
6632
+ on: string;
6633
+ summary?: string;
6634
+ message?: string;
6635
+ found?: unknown;
6636
+ property?: string;
6637
+ expected?: string;
6638
+ };
6639
+ };
6640
+ };
6641
+ };
6642
+ };
6643
+ };
6473
6644
  };
6474
6645
  };
6475
6646
  } & {
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
+ "ProductSale"
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),
@@ -155178,9 +155184,11 @@ var CrmCpOrderLogic;
155178
155184
  if (!order) {
155179
155185
  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
155186
  }
155181
- await db2.update(crmVehicleTable).set({
155182
- km: body.km
155183
- }).where(eq(crmVehicleTable.id, order.vehicleId));
155187
+ if (order.vehicleId) {
155188
+ await db2.update(crmVehicleTable).set({
155189
+ km: body.km
155190
+ }).where(eq(crmVehicleTable.id, order.vehicleId));
155191
+ }
155184
155192
  return order;
155185
155193
  });
155186
155194
  };
@@ -156189,43 +156197,33 @@ var CrmReportsLogic;
156189
156197
  };
156190
156198
  CrmReportsLogic.salesSummary = async (query, user2) => {
156191
156199
  const filter = buildCommonFilters(query, user2);
156200
+ const orderItems = db_default.$with("order_items").as(db_default.select({
156201
+ cpOrderId: crmCpOrderItemTable.cpOrderId,
156202
+ totalItems: sum(crmCpOrderItemTable.priceTotal).as("total_items")
156203
+ }).from(crmCpOrderItemTable).where(isNull2(crmCpOrderItemTable.deletedAt)).groupBy((t2) => t2.cpOrderId));
156204
+ const orderPayments = db_default.$with("order_payments").as(db_default.select({
156205
+ cpOrderId: crmCpOrderPaymentTable.cpOrderId,
156206
+ totalPaid: sum(crmCpOrderPaymentTable.amount).as("total_paid")
156207
+ }).from(crmCpOrderPaymentTable).where(and(isNull2(crmCpOrderPaymentTable.deletedAt), eq(crmCpOrderPaymentTable.state, "PAID"))).groupBy((t2) => t2.cpOrderId));
156192
156208
  const [summary] = await db_default.select({
156193
156209
  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
- `,
156210
+ totalRevenue: sql3`COALESCE(SUM(${orderItems.totalItems}),0)`.as("total_revenue"),
156202
156211
  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
- `,
156212
+ COALESCE(SUM(${orderPayments.totalPaid}), 0)
156213
+ `.as("total_paid_amount"),
156211
156214
  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
- `,
156215
+ COALESCE(AVG(${orderItems.totalItems}), 0)
156216
+ `.as("average_order_value"),
156219
156217
  ordersCompleted: sql3`
156220
156218
  COUNT(CASE WHEN ${crmCpOrderTable.state} = 'COMPLETE' THEN 1 END)
156221
- `,
156219
+ `.as("orders_completed"),
156222
156220
  ordersPending: sql3`
156223
156221
  COUNT(CASE WHEN ${crmCpOrderTable.state} = 'CREATED' THEN 1 END)
156224
- `,
156222
+ `.as("orders_pending"),
156225
156223
  ordersCancelled: sql3`
156226
156224
  COUNT(CASE WHEN ${crmCpOrderTable.state} = 'CANCELLED' THEN 1 END)
156227
- `
156228
- }).from(crmCpOrderTable).where(filter);
156225
+ `.as("orders_cancelled")
156226
+ }).from(crmCpOrderTable).leftJoin(orderItems, eq(crmCpOrderTable.id, orderItems.cpOrderId)).leftJoin(orderPayments, eq(crmCpOrderTable.id, orderPayments.cpOrderId)).where(filter);
156229
156227
  return {
156230
156228
  totalOrders: summary.totalOrders,
156231
156229
  totalRevenue: summary.totalRevenue,
@@ -156296,6 +156294,154 @@ var CrmReportsLogic;
156296
156294
  }).from(crmCpOrderPaymentTable).innerJoin(crmCpOrderTable, eq(crmCpOrderTable.id, crmCpOrderPaymentTable.cpOrderId)).leftJoin(crmCustomerTable, eq(crmCustomerTable.id, crmCpOrderTable.customerId)).where(filter).orderBy(desc(crmCpOrderPaymentTable.createdAt)).$dynamic();
156297
156295
  return await pagination_helper_default(baseQuery, query.pagination);
156298
156296
  };
156297
+ const formatCSV = (headers, rows) => {
156298
+ const csvHeaders = headers.map((h) => `"${h}"`).join(",");
156299
+ const csvRows = rows.map((row) => row.map((cell) => {
156300
+ if (cell === null || cell === undefined)
156301
+ return '""';
156302
+ return `"${String(cell).replace(/"/g, '""')}"`;
156303
+ }).join(","));
156304
+ return [csvHeaders, ...csvRows].join(`
156305
+ `);
156306
+ };
156307
+ CrmReportsLogic.exportSalesCSV = async (query, user2) => {
156308
+ const filter = and(buildCommonFilters(query, user2), eq(crmCpOrderTable.state, query.state).if(query.state));
156309
+ const result = await db_default.select({
156310
+ orderId: crmCpOrderTable.orderId,
156311
+ customerName: sql3`CONCAT(${crmCustomerTable.firstname}, ' ', ${crmCustomerTable.lastname})`,
156312
+ customerPhone: crmCustomerTable.phoneNumber,
156313
+ vehiclePlate: crmVehicleTable.licensePlate,
156314
+ vehicleKind: techdocVehicleKindTable.name,
156315
+ orderState: crmCpOrderTable.state,
156316
+ createdAt: crmCpOrderTable.createdAt,
156317
+ totalAmount: sql3`
156318
+ COALESCE(
156319
+ (SELECT SUM(${crmCpOrderItemTable.priceTotal})
156320
+ FROM ${crmCpOrderItemTable}
156321
+ WHERE ${crmCpOrderItemTable.cpOrderId} = ${crmCpOrderTable.id}
156322
+ AND ${crmCpOrderItemTable.deletedAt} IS NULL),
156323
+ 0
156324
+ )
156325
+ `,
156326
+ paidAmount: sql3`
156327
+ COALESCE(
156328
+ (SELECT SUM(${crmCpOrderPaymentTable.amount})
156329
+ FROM ${crmCpOrderPaymentTable}
156330
+ WHERE ${crmCpOrderPaymentTable.cpOrderId} = ${crmCpOrderTable.id}
156331
+ AND ${crmCpOrderPaymentTable.state} = 'PAID'
156332
+ AND ${crmCpOrderPaymentTable.deletedAt} IS NULL),
156333
+ 0
156334
+ )
156335
+ `
156336
+ }).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);
156337
+ const headers = [
156338
+ "\u0417\u0430\u0445\u0438\u0430\u043B\u0433\u044B\u043D \u0434\u0443\u0433\u0430\u0430\u0440",
156339
+ "\u04AE\u0439\u043B\u0447\u043B\u04AF\u04AF\u043B\u044D\u0433\u0447",
156340
+ "\u0423\u0442\u0430\u0441",
156341
+ "\u0422\u044D\u044D\u0432\u0440\u0438\u0439\u043D \u0445\u044D\u0440\u044D\u0433\u0441\u043B\u0438\u0439\u043D \u0434\u0443\u0433\u0430\u0430\u0440",
156342
+ "\u041C\u0430\u0440\u043A \u0437\u0430\u0433\u0432\u0430\u0440",
156343
+ "\u0422\u04E9\u043B\u04E9\u0432",
156344
+ "\u041E\u0433\u043D\u043E\u043E",
156345
+ "\u041D\u0438\u0439\u0442 \u0434\u04AF\u043D",
156346
+ "\u0422\u04E9\u043B\u0441\u04E9\u043D \u0434\u04AF\u043D",
156347
+ "\u04AE\u043B\u0434\u044D\u0433\u0434\u044D\u043B"
156348
+ ];
156349
+ const rows = result.map((order) => [
156350
+ order.orderId,
156351
+ order.customerName || "\u041C\u044D\u0434\u044D\u0433\u0434\u044D\u0445\u0433\u04AF\u0439",
156352
+ order.customerPhone || "",
156353
+ order.vehiclePlate || "",
156354
+ order.vehicleKind || "",
156355
+ order.orderState,
156356
+ order.createdAt,
156357
+ order.totalAmount,
156358
+ order.paidAmount,
156359
+ order.totalAmount - order.paidAmount
156360
+ ]);
156361
+ return formatCSV(headers, rows);
156362
+ };
156363
+ CrmReportsLogic.exportSalesSummaryCSV = async (query, user2) => {
156364
+ const summary = await CrmReportsLogic.salesSummary(query, user2);
156365
+ const headers = ["\u041C\u0435\u0442\u0440\u0438\u043A", "\u0423\u0442\u0433\u0430"];
156366
+ const rows = [
156367
+ ["\u041D\u0438\u0439\u0442 \u0437\u0430\u0445\u0438\u0430\u043B\u0433\u0430", summary.totalOrders],
156368
+ ["\u041D\u0438\u0439\u0442 \u043E\u0440\u043B\u043E\u0433\u043E", summary.totalRevenue],
156369
+ ["\u041D\u0438\u0439\u0442 \u0442\u04E9\u043B\u0441\u04E9\u043D \u0434\u04AF\u043D", summary.totalPaidAmount],
156370
+ ["\u0422\u04E9\u043B\u04E9\u04E9\u0433\u04AF\u0439 \u0434\u04AF\u043D", summary.totalPendingAmount],
156371
+ ["\u0414\u0443\u043D\u0434\u0430\u0436 \u0437\u0430\u0445\u0438\u0430\u043B\u0433\u044B\u043D \u0434\u04AF\u043D", summary.averageOrderValue],
156372
+ ["\u0414\u0443\u0443\u0441\u0433\u0430\u0441\u0430\u043D \u0437\u0430\u0445\u0438\u0430\u043B\u0433\u0430", summary.ordersCompleted],
156373
+ ["\u0425\u04AF\u043B\u044D\u044D\u0433\u0434\u044D\u0436 \u0431\u0443\u0439 \u0437\u0430\u0445\u0438\u0430\u043B\u0433\u0430", summary.ordersPending],
156374
+ ["\u0426\u0443\u0446\u043B\u0430\u0433\u0434\u0441\u0430\u043D \u0437\u0430\u0445\u0438\u0430\u043B\u0433\u0430", summary.ordersCancelled]
156375
+ ];
156376
+ return formatCSV(headers, rows);
156377
+ };
156378
+ CrmReportsLogic.exportSalesByPeriodCSV = async (query, user2) => {
156379
+ const result = await CrmReportsLogic.salesByPeriod(query, user2);
156380
+ 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"];
156381
+ const rows = result.map((period) => [
156382
+ period.period,
156383
+ period.totalOrders,
156384
+ period.totalRevenue,
156385
+ period.totalPaidAmount
156386
+ ]);
156387
+ return formatCSV(headers, rows);
156388
+ };
156389
+ CrmReportsLogic.exportSalesByEmployeeCSV = async (query, user2) => {
156390
+ const filter = buildCommonFilters(query, user2);
156391
+ const result = await db_default.select({
156392
+ employeeName: sql3`CONCAT(${companyEmployeeTable.firstname}, ' ', ${companyEmployeeTable.lastname})`,
156393
+ totalOrders: count(sql3`DISTINCT ${crmCpOrderTable.id}`),
156394
+ totalRevenue: sql3`COALESCE(SUM(${crmCpOrderItemEmployeeTable.amount}), 0)`,
156395
+ totalCommission: sql3`COALESCE(SUM(${crmCpOrderItemEmployeeTable.amount}) * 0.1, 0)`
156396
+ }).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);
156397
+ const headers = [
156398
+ "\u0410\u0436\u0438\u043B\u0442\u043D\u044B \u043D\u044D\u0440",
156399
+ "\u0417\u0430\u0445\u0438\u0430\u043B\u0433\u044B\u043D \u0442\u043E\u043E",
156400
+ "\u041D\u0438\u0439\u0442 \u0431\u043E\u0440\u043B\u0443\u0443\u043B\u0430\u043B\u0442",
156401
+ "\u041A\u043E\u043C\u0438\u0441\u0441"
156402
+ ];
156403
+ const rows = result.map((employee) => [
156404
+ employee.employeeName,
156405
+ employee.totalOrders,
156406
+ employee.totalRevenue,
156407
+ employee.totalCommission
156408
+ ]);
156409
+ return formatCSV(headers, rows);
156410
+ };
156411
+ CrmReportsLogic.exportPaymentsCSV = async (query, user2) => {
156412
+ const filter = and(buildCommonFilters(query, user2), eq(crmCpOrderPaymentTable.paymentMethod, query.method).if(query.method), softDeletedFilter(crmCpOrderPaymentTable));
156413
+ const result = await db_default.select({
156414
+ orderId: crmCpOrderTable.orderId,
156415
+ customerName: sql3`CONCAT(${crmCustomerTable.firstname}, ' ', ${crmCustomerTable.lastname})`,
156416
+ paymentMethod: crmCpOrderPaymentTable.paymentMethod,
156417
+ paymentChannel: crmCpOrderPaymentTable.paymentChannel,
156418
+ amount: crmCpOrderPaymentTable.amount,
156419
+ state: crmCpOrderPaymentTable.state,
156420
+ invoiceNumber: crmCpOrderPaymentTable.invoiceNumber,
156421
+ createdAt: crmCpOrderPaymentTable.createdAt
156422
+ }).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);
156423
+ const headers = [
156424
+ "\u0417\u0430\u0445\u0438\u0430\u043B\u0433\u044B\u043D \u0434\u0443\u0433\u0430\u0430\u0440",
156425
+ "\u04AE\u0439\u043B\u0447\u043B\u04AF\u04AF\u043B\u044D\u0433\u0447",
156426
+ "\u0422\u04E9\u043B\u0431\u04E9\u0440\u0438\u0439\u043D \u0445\u044D\u0440\u044D\u0433\u0441\u044D\u043B",
156427
+ "\u0422\u04E9\u043B\u0431\u04E9\u0440\u0438\u0439\u043D \u0441\u0443\u0432\u0430\u0433",
156428
+ "\u0414\u04AF\u043D",
156429
+ "\u0422\u04E9\u043B\u04E9\u0432",
156430
+ "\u041D\u044D\u0445\u044D\u043C\u0436\u043B\u044D\u0445 \u0434\u0443\u0433\u0430\u0430\u0440",
156431
+ "\u041E\u0433\u043D\u043E\u043E"
156432
+ ];
156433
+ const rows = result.map((payment) => [
156434
+ payment.orderId,
156435
+ payment.customerName || "\u041C\u044D\u0434\u044D\u0433\u0434\u044D\u0445\u0433\u04AF\u0439",
156436
+ payment.paymentMethod,
156437
+ payment.paymentChannel,
156438
+ payment.amount,
156439
+ payment.state,
156440
+ payment.invoiceNumber || "",
156441
+ payment.createdAt
156442
+ ]);
156443
+ return formatCSV(headers, rows);
156444
+ };
156299
156445
  })(CrmReportsLogic ||= {});
156300
156446
  var logic_default15 = CrmReportsLogic;
156301
156447
 
@@ -156338,6 +156484,17 @@ var CrmReportsModel;
156338
156484
  state: t.String()
156339
156485
  }))
156340
156486
  ]);
156487
+ CrmReportsModel.csvExportQuery = t.Composite([
156488
+ CrmReportsModel.baseDateFilter,
156489
+ t.Partial(t.Object({
156490
+ state: t.UnionEnum(crmCpOrderStateEnum.enumValues, {
156491
+ default: undefined
156492
+ }),
156493
+ employeeId: t.String({ format: "uuid" }),
156494
+ method: t.String(),
156495
+ limit: t.Number({ default: 1e4, maximum: 50000 })
156496
+ }))
156497
+ ]);
156341
156498
  CrmReportsModel.salesSummaryResponse = t.Object({
156342
156499
  totalOrders: t.Number(),
156343
156500
  totalRevenue: t.Number(),
@@ -156388,6 +156545,41 @@ var reportsRoutes = new Elysia({
156388
156545
  query: model_default13.salesByEmployeeQuery
156389
156546
  }).get("/payments", async ({ query, user: user2 }) => logic_default15.paymentsReport(query, user2), {
156390
156547
  query: model_default13.paymentsReportQuery
156548
+ }).get("/sales/csv", async ({ query, user: user2, set: set3 }) => {
156549
+ const csvData = await logic_default15.exportSalesCSV(query, user2);
156550
+ set3.headers["Content-Type"] = "text/csv";
156551
+ set3.headers["Content-Disposition"] = 'attachment; filename="sales-report.csv"';
156552
+ return csvData;
156553
+ }, {
156554
+ query: model_default13.csvExportQuery
156555
+ }).get("/sales/summary/csv", async ({ query, user: user2, set: set3 }) => {
156556
+ const csvData = await logic_default15.exportSalesSummaryCSV(query, user2);
156557
+ set3.headers["Content-Type"] = "text/csv";
156558
+ set3.headers["Content-Disposition"] = 'attachment; filename="sales-summary.csv"';
156559
+ return csvData;
156560
+ }, {
156561
+ query: model_default13.baseDateFilter
156562
+ }).get("/sales/by-period/csv", async ({ query, user: user2, set: set3 }) => {
156563
+ const csvData = await logic_default15.exportSalesByPeriodCSV(query, user2);
156564
+ set3.headers["Content-Type"] = "text/csv";
156565
+ set3.headers["Content-Disposition"] = 'attachment; filename="sales-by-period.csv"';
156566
+ return csvData;
156567
+ }, {
156568
+ query: model_default13.salesByPeriodQuery
156569
+ }).get("/sales/by-employee/csv", async ({ query, user: user2, set: set3 }) => {
156570
+ const csvData = await logic_default15.exportSalesByEmployeeCSV(query, user2);
156571
+ set3.headers["Content-Type"] = "text/csv";
156572
+ set3.headers["Content-Disposition"] = 'attachment; filename="sales-by-employee.csv"';
156573
+ return csvData;
156574
+ }, {
156575
+ query: model_default13.csvExportQuery
156576
+ }).get("/payments/csv", async ({ query, user: user2, set: set3 }) => {
156577
+ const csvData = await logic_default15.exportPaymentsCSV(query, user2);
156578
+ set3.headers["Content-Type"] = "text/csv";
156579
+ set3.headers["Content-Disposition"] = 'attachment; filename="payments-report.csv"';
156580
+ return csvData;
156581
+ }, {
156582
+ query: model_default13.csvExportQuery
156391
156583
  });
156392
156584
  var reports_default = reportsRoutes;
156393
156585
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autosync_backend2",
3
- "version": "1.1.68",
3
+ "version": "1.1.69",
4
4
  "module": "src/index.ts",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",