@pisell/pisellos 2.3.6 → 2.3.8

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.
Files changed (38) hide show
  1. package/dist/modules/BookingContext/utils/buildCacheItemFromOrderLine.js +17 -13
  2. package/dist/modules/Order/index.d.ts +18 -2
  3. package/dist/modules/Order/index.js +392 -222
  4. package/dist/modules/Order/types.d.ts +15 -2
  5. package/dist/modules/Order/utils/bundleDiscountHydration.d.ts +5 -0
  6. package/dist/modules/Order/utils/bundleDiscountHydration.js +144 -0
  7. package/dist/modules/Order/utils.d.ts +4 -0
  8. package/dist/modules/Order/utils.js +114 -28
  9. package/dist/modules/Product/index.d.ts +1 -1
  10. package/dist/modules/Rules/index.d.ts +3 -3
  11. package/dist/modules/Rules/index.js +8 -3
  12. package/dist/modules/Rules/types.d.ts +2 -1
  13. package/dist/server/index.js +1 -0
  14. package/dist/server/modules/order/index.d.ts +4 -4
  15. package/dist/server/modules/order/index.js +4 -4
  16. package/dist/solution/BaseSales/index.d.ts +8 -1
  17. package/dist/solution/BaseSales/index.js +145 -104
  18. package/dist/solution/BaseSales/utils/transformBaseProductToOrderProduct.js +4 -2
  19. package/dist/solution/BookingByStep/index.d.ts +1 -1
  20. package/lib/modules/BookingContext/utils/buildCacheItemFromOrderLine.js +11 -1
  21. package/lib/modules/Order/index.d.ts +18 -2
  22. package/lib/modules/Order/index.js +229 -40
  23. package/lib/modules/Order/types.d.ts +15 -2
  24. package/lib/modules/Order/utils/bundleDiscountHydration.d.ts +5 -0
  25. package/lib/modules/Order/utils/bundleDiscountHydration.js +139 -0
  26. package/lib/modules/Order/utils.d.ts +4 -0
  27. package/lib/modules/Order/utils.js +84 -2
  28. package/lib/modules/Product/index.d.ts +1 -1
  29. package/lib/modules/Rules/index.d.ts +3 -3
  30. package/lib/modules/Rules/index.js +4 -3
  31. package/lib/modules/Rules/types.d.ts +2 -1
  32. package/lib/server/modules/order/index.d.ts +4 -4
  33. package/lib/server/modules/order/index.js +4 -4
  34. package/lib/solution/BaseSales/index.d.ts +8 -1
  35. package/lib/solution/BaseSales/index.js +58 -22
  36. package/lib/solution/BaseSales/utils/transformBaseProductToOrderProduct.js +4 -5
  37. package/lib/solution/BookingByStep/index.d.ts +1 -1
  38. package/package.json +1 -1
@@ -142,6 +142,14 @@ function resolveMainProductPrice(product, fallback) {
142
142
  const parsed = Number(value);
143
143
  return Number.isFinite(parsed) ? parsed : fallback;
144
144
  }
145
+ function mapOrderBundleToOtherBundle(bundle) {
146
+ return bundle.map((item) => {
147
+ return {
148
+ ...item,
149
+ original_price: item.original_price ?? item.bundle_selling_price ?? item.price
150
+ };
151
+ });
152
+ }
145
153
  function synthesizeProductResourceFromBooking(booking) {
146
154
  const rows = booking == null ? void 0 : booking.resources;
147
155
  if (!Array.isArray(rows) || rows.length === 0)
@@ -191,7 +199,9 @@ function buildCacheItemFromOrderLine(input) {
191
199
  const other = {
192
200
  product_variant_id: product.product_variant_id ?? 0,
193
201
  option: (0, import_utils.getProductSkuOptions)(product),
194
- bundle: Array.isArray(product.product_bundle) ? [...product.product_bundle] : []
202
+ bundle: mapOrderBundleToOtherBundle(
203
+ Array.isArray(product.product_bundle) ? product.product_bundle : []
204
+ )
195
205
  };
196
206
  const extend = {
197
207
  start_date: startDate ? (0, import_dayjs.default)(startDate) : void 0,
@@ -312,13 +312,27 @@ export declare class OrderModule extends BaseModule implements Module, OrderModu
312
312
  updateOrderPayment(paymentIdentity: string | number, updates: Partial<OrderPaymentData> & Record<string, any>): OrderPaymentData[];
313
313
  /** 从当前订单支付项中移除指定支付项。 */
314
314
  deleteOrderPayment(paymentIdentity: string | number): OrderPaymentData[];
315
+ private confirmPendingVoucherPaymentsInList;
316
+ private discardPendingVoucherPaymentsFromList;
317
+ private prepareVoucherPaymentsForSubmit;
318
+ private applyPaymentLifecycleChange;
319
+ confirmPendingVoucherPayments(options?: {
320
+ persist?: boolean;
321
+ recalculateSummary?: boolean;
322
+ }): OrderPaymentData[];
323
+ discardPendingVoucherPayments(options?: {
324
+ persist?: boolean;
325
+ recalculateSummary?: boolean;
326
+ }): OrderPaymentData[];
315
327
  /**
316
328
  * 覆盖当前订单中的 wallet pass 支付项。
317
329
  *
318
330
  * 判断口径与 PaymentModal 保持一致:voucher_id 存在且不为 0 的支付项
319
331
  * 视为 wallet pass;其它支付项保持不变。
320
332
  */
321
- updateVoucherOrderPayments(payments: OrderPaymentSource[]): OrderPaymentData[];
333
+ updateVoucherOrderPayments(payments: OrderPaymentSource[], options?: {
334
+ status?: 'paid' | 'payment_pending';
335
+ }): OrderPaymentData[];
322
336
  private shouldMergeProductToOrder;
323
337
  private hasOrderProductLineNote;
324
338
  /**
@@ -418,6 +432,7 @@ export declare class OrderModule extends BaseModule implements Module, OrderModu
418
432
  payments?: OrderPaymentSource[];
419
433
  paymentStatus?: SubmitSalesOrderParams['query']['payment_status'];
420
434
  smallTicketDataFlag?: number;
435
+ confirmPendingVoucherPayments?: boolean;
421
436
  enhancePayload?: SubmitPayloadEnhancer;
422
437
  }): Promise<T>;
423
438
  submitTempOrderAsync<T = any>(params?: {
@@ -429,6 +444,7 @@ export declare class OrderModule extends BaseModule implements Module, OrderModu
429
444
  payments?: OrderPaymentSource[];
430
445
  paymentStatus?: SubmitSalesOrderParams['query']['payment_status'];
431
446
  smallTicketDataFlag?: number;
447
+ confirmPendingVoucherPayments?: boolean;
432
448
  enhancePayload?: SubmitPayloadEnhancer;
433
449
  }): Promise<T>;
434
450
  private runSubmitTempOrder;
@@ -442,7 +458,7 @@ export declare class OrderModule extends BaseModule implements Module, OrderModu
442
458
  generateIdempotencyToken(): string;
443
459
  syncPaymentsToOrder<T = any>(params: SyncPaymentsToOrderParams): Promise<SyncPaymentsToOrderResult<T>>;
444
460
  createOrder(params: CommitOrderParams['query']): {
445
- type: "virtual" | "appointment_booking";
461
+ type: "appointment_booking" | "virtual";
446
462
  platform: string;
447
463
  sales_channel: string;
448
464
  order_sales_channel: string;
@@ -40,6 +40,7 @@ var import_dayjs = __toESM(require("dayjs"));
40
40
  var import_cartPromotion = require("../../solution/BaseSales/utils/cartPromotion");
41
41
  var import_utils3 = require("../../solution/ScanOrder/utils");
42
42
  var import_manualProductDiscount = require("./utils/manualProductDiscount");
43
+ var import_bundleDiscountHydration = require("./utils/bundleDiscountHydration");
43
44
  var import_Discount = require("../Discount");
44
45
  var import_Rules = require("../Rules");
45
46
  var import_types2 = require("../Rules/types");
@@ -64,6 +65,20 @@ function isManualProductDiscountProduct(product) {
64
65
  var _a, _b;
65
66
  return ((_a = product == null ? void 0 : product.metadata) == null ? void 0 : _a.is_manual_discount) === true || ((_b = product == null ? void 0 : product.metadata) == null ? void 0 : _b.is_manual_discount) === 1 || ((product == null ? void 0 : product.discount_list) || []).some((item) => (item == null ? void 0 : item.type) === "product");
66
67
  }
68
+ var ORDER_PAYMENT_STATUS_PAID = "paid";
69
+ var ORDER_PAYMENT_STATUS_PENDING = "payment_pending";
70
+ function isVoucherPaymentRecord(payment) {
71
+ return (payment == null ? void 0 : payment.voucher_id) !== void 0 && Number(payment.voucher_id) !== 0;
72
+ }
73
+ function hasSyncedOrderPaymentRecord(payment) {
74
+ return (payment == null ? void 0 : payment.order_payment_id) !== void 0 && payment.order_payment_id !== null;
75
+ }
76
+ function isPendingVoucherPaymentRecord(payment) {
77
+ return isVoucherPaymentRecord(payment) && (payment == null ? void 0 : payment.status) === ORDER_PAYMENT_STATUS_PENDING;
78
+ }
79
+ function isPaidPaymentRecord(payment) {
80
+ return (payment == null ? void 0 : payment.status) === ORDER_PAYMENT_STATUS_PAID;
81
+ }
67
82
  var OrderModule = class extends import_BaseModule.BaseModule {
68
83
  constructor(name, version) {
69
84
  super(name || "order", version);
@@ -91,7 +106,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
91
106
  collectHydratedEditDiscounts(products) {
92
107
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
93
108
  const discountMap = /* @__PURE__ */ new Map();
94
- for (const product of products || []) {
109
+ for (const product of (0, import_bundleDiscountHydration.expandHydratedProductsForDiscountCollection)(products)) {
95
110
  for (const item of (product == null ? void 0 : product.discount_list) || []) {
96
111
  const type = (item == null ? void 0 : item.type) ?? (item == null ? void 0 : item.tag);
97
112
  if (!type || type === "product")
@@ -114,6 +129,21 @@ var OrderModule = class extends import_BaseModule.BaseModule {
114
129
  discount: item == null ? void 0 : item.discount,
115
130
  _num: Number(((_e = item == null ? void 0 : item.metadata) == null ? void 0 : _e.num) ?? (product == null ? void 0 : product.num) ?? (product == null ? void 0 : product.product_quantity) ?? 1) || 1
116
131
  };
132
+ const existed = discountMap.get(key);
133
+ if (existed) {
134
+ existed.amount = Number(existed.amount || 0) + amount;
135
+ existed.used_par_value = String(Number(existed.used_par_value || 0) + amount);
136
+ existed.balance = String(Number(existed.balance || 0) + amount);
137
+ existed.applicableProductDetails = [
138
+ ...existed.applicableProductDetails || [],
139
+ detail
140
+ ];
141
+ existed.appliedProductDetails = [
142
+ ...existed.appliedProductDetails || [],
143
+ detail
144
+ ];
145
+ continue;
146
+ }
117
147
  discountMap.set(key, {
118
148
  id: key,
119
149
  product_name: "",
@@ -236,6 +266,21 @@ var OrderModule = class extends import_BaseModule.BaseModule {
236
266
  product.metadata.main_product_selling_price = newMainSellingPrice;
237
267
  product.metadata.price_schema_version = 2;
238
268
  }
269
+ if (Array.isArray(product.product_bundle)) {
270
+ product.product_bundle = product.product_bundle.map((bundle) => {
271
+ const hasBundleDiscount = Array.isArray(bundle == null ? void 0 : bundle.discount_list) && bundle.discount_list.length > 0;
272
+ if (hasBundleDiscount)
273
+ return bundle;
274
+ const restoredPrice = (bundle == null ? void 0 : bundle.original_price) ?? (bundle == null ? void 0 : bundle.product_price) ?? (bundle == null ? void 0 : bundle.price);
275
+ if (restoredPrice === void 0 || restoredPrice === null || restoredPrice === "")
276
+ return bundle;
277
+ return {
278
+ ...bundle,
279
+ price: restoredPrice,
280
+ bundle_selling_price: restoredPrice
281
+ };
282
+ });
283
+ }
239
284
  product.selling_price = (0, import_utils.composeLinePrice)({
240
285
  mainPrice: newMainSellingPrice,
241
286
  bundle: product.product_bundle
@@ -434,7 +479,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
434
479
  });
435
480
  }
436
481
  async scanCode(code, customerId) {
437
- var _a, _b, _c, _d, _e;
482
+ var _a, _b, _c, _d;
438
483
  const resultDiscountWithReason = await ((_a = this.store.discount) == null ? void 0 : _a.batchSearch(code, { customerId })) || { discountList: [], unavailableReasonKey: null };
439
484
  const { discountList: resultDiscountList, unavailableReasonKey } = resultDiscountWithReason;
440
485
  const rulesModule = this.store.rules;
@@ -470,17 +515,17 @@ var OrderModule = class extends import_BaseModule.BaseModule {
470
515
  };
471
516
  }
472
517
  const tempOrder = this.store.tempOrder;
473
- const holders = ((_b = tempOrder == null ? void 0 : tempOrder.holder) == null ? void 0 : _b.form_record_id) ? [{ form_record_id: tempOrder.holder.form_record_id }] : [];
518
+ const holders = ((_b = tempOrder == null ? void 0 : tempOrder.holder) == null ? void 0 : _b.form_record) || [];
474
519
  const { isAvailable, discountList: newDiscountList, unavailableReason } = rulesModule.isDiscountListAvailable({
475
520
  productList: (tempOrder == null ? void 0 : tempOrder.products) || [],
476
521
  oldDiscountList: this.getDiscountList(),
477
522
  newDiscountList: withScanList,
478
523
  orderTotalAmount: Number(((_c = this.store.summary) == null ? void 0 : _c.total_amount) || 0),
479
524
  holders,
480
- isFormSubject: !!((_d = tempOrder == null ? void 0 : tempOrder.holder) == null ? void 0 : _d.type) && tempOrder.holder.type === "form"
525
+ isFormSubject: (product) => (0, import_utils.resolveIsFormSubject)(tempOrder, product)
481
526
  }) || { isAvailable: false, discountList: this.getDiscountList() };
482
527
  if (isAvailable && newDiscountList) {
483
- (_e = this.store.discount) == null ? void 0 : _e.setDiscountList(newDiscountList);
528
+ (_d = this.store.discount) == null ? void 0 : _d.setDiscountList(newDiscountList);
484
529
  this.applyDiscount();
485
530
  }
486
531
  return {
@@ -492,24 +537,25 @@ var OrderModule = class extends import_BaseModule.BaseModule {
492
537
  };
493
538
  }
494
539
  applyDiscount() {
495
- var _a, _b, _c, _d, _e;
540
+ var _a, _b, _c, _d;
496
541
  const tempOrder = this.store.tempOrder;
497
542
  if (!tempOrder)
498
543
  return;
499
544
  delete tempOrder.discount_list;
500
- if (this.shouldSkipDiscountCalculation(tempOrder))
545
+ const discountList = ((_a = this.store.discount) == null ? void 0 : _a.getDiscountList()) || [];
546
+ const shouldSkipDiscountCalculation = this.shouldSkipDiscountCalculation(tempOrder);
547
+ if (shouldSkipDiscountCalculation)
501
548
  return;
502
549
  const rulesModule = this.store.rules;
503
550
  if (!rulesModule)
504
551
  return;
505
- const discountList = ((_a = this.store.discount) == null ? void 0 : _a.getDiscountList()) || [];
506
- const holders = ((_b = tempOrder.holder) == null ? void 0 : _b.form_record_id) ? [{ form_record_id: tempOrder.holder.form_record_id }] : [];
552
+ const holders = ((_b = tempOrder.holder) == null ? void 0 : _b.form_record) || [];
507
553
  const result = rulesModule.calcDiscount({
508
554
  productList: tempOrder.products,
509
555
  discountList,
510
556
  holders,
511
- isFormSubject: !!((_c = tempOrder.holder) == null ? void 0 : _c.type) && tempOrder.holder.type === "form",
512
- orderTotalAmount: Number(((_d = this.store.summary) == null ? void 0 : _d.total_amount) || 0)
557
+ isFormSubject: (product) => (0, import_utils.resolveIsFormSubject)(tempOrder, product),
558
+ orderTotalAmount: Number(((_c = this.store.summary) == null ? void 0 : _c.total_amount) || 0)
513
559
  });
514
560
  if (result == null ? void 0 : result.productList) {
515
561
  const previousProductsByUid = (0, import_utils.indexOrderProductsByUid)(tempOrder.products);
@@ -538,7 +584,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
538
584
  result.productList || tempOrder.products,
539
585
  result.discountList
540
586
  );
541
- (_e = this.store.discount) == null ? void 0 : _e.setDiscountList(result.discountList);
587
+ (_d = this.store.discount) == null ? void 0 : _d.setDiscountList(result.discountList);
542
588
  }
543
589
  this.hasActiveCustomerBoundDiscount = (tempOrder.products || []).some(
544
590
  (product) => this.productHasCustomerBoundDiscount(product)
@@ -816,6 +862,8 @@ var OrderModule = class extends import_BaseModule.BaseModule {
816
862
  return (0, import_utils.mapPaymentItemsToOrderPayments)(payments, {
817
863
  includeVoided: false
818
864
  }).reduce((sum, payment) => {
865
+ if (!isPaidPaymentRecord(payment))
866
+ return sum;
819
867
  return sum.plus(this.calculatePaymentEffectiveAmount(payment));
820
868
  }, new import_decimal.default(0));
821
869
  }
@@ -886,11 +934,11 @@ var OrderModule = class extends import_BaseModule.BaseModule {
886
934
  }
887
935
  };
888
936
  tempOrder.deposit_amount = snapshot.depositAmount;
889
- tempOrder.is_deposit = snapshot.isDeposit;
937
+ tempOrder.is_deposit = tempOrder.is_deposit === 0 && new import_decimal.default(tempOrder.deposit_amount || 0).gt(0) ? 0 : snapshot.isDeposit;
890
938
  return nextSummary;
891
939
  }
892
940
  isDepositOrder(tempOrder, depositAmount) {
893
- return tempOrder.is_deposit === 1 || depositAmount.gt(0);
941
+ return tempOrder.is_deposit === 1 && depositAmount.gt(0);
894
942
  }
895
943
  getOrderExpectedAmount(summary) {
896
944
  return new import_decimal.default(summary.expect_amount || summary.total_amount || 0);
@@ -933,7 +981,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
933
981
  calculateDepositPaidAmount(payments) {
934
982
  return (payments || []).reduce((sum, payment) => {
935
983
  const paymentRecord = payment;
936
- if ((paymentRecord == null ? void 0 : paymentRecord.status) !== "paid")
984
+ if (!isPaidPaymentRecord(paymentRecord))
937
985
  return sum;
938
986
  const isDepositPayment = paymentRecord.type === "deposit" || paymentRecord.order_payment_type === "deposit";
939
987
  if (!isDepositPayment)
@@ -1084,7 +1132,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
1084
1132
  return (payments || []).reduce(
1085
1133
  (summary, payment) => {
1086
1134
  const paymentRecord = payment;
1087
- if ((paymentRecord == null ? void 0 : paymentRecord.status) !== "paid")
1135
+ if (!isPaidPaymentRecord(paymentRecord))
1088
1136
  return summary;
1089
1137
  return {
1090
1138
  customerPaidAmount: summary.customerPaidAmount.plus(paymentRecord.amount || 0),
@@ -1510,6 +1558,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
1510
1558
  }
1511
1559
  restoreOrder() {
1512
1560
  var _a, _b;
1561
+ this.logInfo("restoreOrder start", {});
1513
1562
  const freshTempOrder = this.createDefaultTempOrderInstance();
1514
1563
  this.ensureExternalSaleNumber(freshTempOrder);
1515
1564
  this.store.tempOrder = freshTempOrder;
@@ -1588,7 +1637,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
1588
1637
  orderPaidAmount: paidPaymentSummary.gapPaidAmount
1589
1638
  });
1590
1639
  return {
1591
- isDeposit: tempOrder.is_deposit === 1 || depositAmount !== "0.00",
1640
+ isDeposit: tempOrder.is_deposit === 1,
1592
1641
  depositAmount,
1593
1642
  expectAmount: summary.expect_amount || "0.00",
1594
1643
  totalAmount: summary.total_amount || "0.00",
@@ -1873,8 +1922,9 @@ var OrderModule = class extends import_BaseModule.BaseModule {
1873
1922
  hasDeposit: true
1874
1923
  } : void 0
1875
1924
  };
1925
+ const isExistingDepositDisabled = tempOrder.is_deposit === 0 && new import_decimal.default(tempOrder.deposit_amount || 0).gt(0);
1876
1926
  tempOrder.deposit_amount = depositAmount;
1877
- tempOrder.is_deposit = hasDepositAmount ? 1 : 0;
1927
+ tempOrder.is_deposit = hasDepositAmount && !isExistingDepositDisabled ? 1 : 0;
1878
1928
  return this.applyLastOrderDepositSnapshot(tempOrder, nextSummary);
1879
1929
  }
1880
1930
  async recalculateSummary(options) {
@@ -2219,6 +2269,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
2219
2269
  if (String(previousCustomerId ?? "") !== String(nextCustomerId ?? "")) {
2220
2270
  (0, import_utils.clearTempOrderHolderAssignments)(tempOrder);
2221
2271
  this.clearCustomerBoundDiscounts(tempOrder);
2272
+ this.applyDiscount();
2222
2273
  }
2223
2274
  this.persistTempOrder();
2224
2275
  this.saveDraftInBackground();
@@ -2376,38 +2427,128 @@ var OrderModule = class extends import_BaseModule.BaseModule {
2376
2427
  this.persistTempOrder();
2377
2428
  return tempOrder.payments;
2378
2429
  }
2430
+ confirmPendingVoucherPaymentsInList(payments) {
2431
+ return (payments || []).map((payment) => {
2432
+ if (!isPendingVoucherPaymentRecord(payment))
2433
+ return payment;
2434
+ return {
2435
+ ...payment,
2436
+ status: ORDER_PAYMENT_STATUS_PAID
2437
+ };
2438
+ });
2439
+ }
2440
+ discardPendingVoucherPaymentsFromList(payments) {
2441
+ return (payments || []).filter((payment) => !isPendingVoucherPaymentRecord(payment));
2442
+ }
2443
+ prepareVoucherPaymentsForSubmit(payments, confirmPendingVoucherPayments = true) {
2444
+ const mappedPayments = (0, import_utils.mapPaymentItemsToOrderPayments)(payments || []);
2445
+ return confirmPendingVoucherPayments ? this.confirmPendingVoucherPaymentsInList(mappedPayments) : this.discardPendingVoucherPaymentsFromList(mappedPayments);
2446
+ }
2447
+ applyPaymentLifecycleChange(tempOrder, options) {
2448
+ this.updateTempOrderPaymentStatus(tempOrder);
2449
+ if ((options == null ? void 0 : options.persist) !== false) {
2450
+ this.persistTempOrder();
2451
+ }
2452
+ if ((options == null ? void 0 : options.recalculateSummary) === false)
2453
+ return;
2454
+ const logContext = (options == null ? void 0 : options.logContext) || "payment lifecycle change";
2455
+ void this.recalculateSummary({ createIfMissing: true }).catch((error) => {
2456
+ this.logError(`recalculate summary after ${logContext} failed`, {
2457
+ error: error instanceof Error ? error.message : String(error)
2458
+ });
2459
+ });
2460
+ }
2461
+ confirmPendingVoucherPayments(options) {
2462
+ const tempOrder = this.ensureTempOrder();
2463
+ const payments = tempOrder.payments || [];
2464
+ const hasPendingVoucher = payments.some((payment) => isPendingVoucherPaymentRecord(payment));
2465
+ if (!hasPendingVoucher)
2466
+ return payments;
2467
+ tempOrder.payments = this.confirmPendingVoucherPaymentsInList(payments);
2468
+ this.applyPaymentLifecycleChange(tempOrder, {
2469
+ ...options,
2470
+ logContext: "confirmPendingVoucherPayments"
2471
+ });
2472
+ return tempOrder.payments;
2473
+ }
2474
+ discardPendingVoucherPayments(options) {
2475
+ const tempOrder = this.ensureTempOrder();
2476
+ const payments = tempOrder.payments || [];
2477
+ const nextPayments = this.discardPendingVoucherPaymentsFromList(payments);
2478
+ if (nextPayments.length === payments.length)
2479
+ return payments;
2480
+ tempOrder.payments = nextPayments;
2481
+ this.applyPaymentLifecycleChange(tempOrder, {
2482
+ ...options,
2483
+ logContext: "discardPendingVoucherPayments"
2484
+ });
2485
+ return tempOrder.payments;
2486
+ }
2379
2487
  /**
2380
2488
  * 覆盖当前订单中的 wallet pass 支付项。
2381
2489
  *
2382
2490
  * 判断口径与 PaymentModal 保持一致:voucher_id 存在且不为 0 的支付项
2383
2491
  * 视为 wallet pass;其它支付项保持不变。
2384
2492
  */
2385
- updateVoucherOrderPayments(payments) {
2493
+ updateVoucherOrderPayments(payments, options) {
2386
2494
  const tempOrder = this.ensureTempOrder();
2387
- const isVoucherPayment = (payment) => {
2388
- return payment.voucher_id !== void 0 && Number(payment.voucher_id) !== 0;
2495
+ const isOrderPaymentType = (value) => {
2496
+ return value === "normal" || value === "deposit";
2389
2497
  };
2390
- const hasSyncedOrderPayment = (payment) => {
2391
- return payment.order_payment_id !== void 0 && payment.order_payment_id !== null;
2498
+ const voucherOrderPaymentType = this.resolveNextOrderPaymentType(tempOrder);
2499
+ const withVoucherPaymentType = (payment) => {
2500
+ const paymentRecord = payment;
2501
+ if (!isVoucherPaymentRecord(paymentRecord))
2502
+ return payment;
2503
+ const explicitPaymentType = isOrderPaymentType(paymentRecord.order_payment_type) ? paymentRecord.order_payment_type : isOrderPaymentType(paymentRecord.type) ? paymentRecord.type : void 0;
2504
+ const nextPaymentType = explicitPaymentType || voucherOrderPaymentType;
2505
+ const shouldPreserveCustomPaymentType = paymentRecord.custom_payment_type === void 0 && paymentRecord.type !== void 0 && !isOrderPaymentType(paymentRecord.type);
2506
+ return {
2507
+ ...paymentRecord,
2508
+ ...shouldPreserveCustomPaymentType ? { custom_payment_type: paymentRecord.type } : {},
2509
+ type: nextPaymentType,
2510
+ order_payment_type: nextPaymentType
2511
+ };
2512
+ };
2513
+ const normalizeVoucherPaymentStatus = (payment) => {
2514
+ const paymentRecord = payment;
2515
+ if (!isVoucherPaymentRecord(paymentRecord))
2516
+ return payment;
2517
+ if (hasSyncedOrderPaymentRecord(paymentRecord) && (paymentRecord.status === void 0 || paymentRecord.status === "active")) {
2518
+ return {
2519
+ ...paymentRecord,
2520
+ status: ORDER_PAYMENT_STATUS_PAID
2521
+ };
2522
+ }
2523
+ if (paymentRecord.status !== void 0 && paymentRecord.status !== "active") {
2524
+ return payment;
2525
+ }
2526
+ return {
2527
+ ...paymentRecord,
2528
+ status: (options == null ? void 0 : options.status) || ORDER_PAYMENT_STATUS_PENDING
2529
+ };
2392
2530
  };
2393
2531
  const mappedVouchers = (0, import_utils.mapPaymentItemsToOrderPayments)(
2394
2532
  payments.map(
2395
- (payment) => this.normalizePaymentSource(payment, { defaultPaid: true })
2533
+ (payment) => this.normalizePaymentSource(
2534
+ normalizeVoucherPaymentStatus(withVoucherPaymentType(payment)),
2535
+ { defaultPaid: false }
2536
+ )
2396
2537
  )
2397
2538
  ).filter((payment) => {
2398
- return isVoucherPayment(payment);
2539
+ return isVoucherPaymentRecord(payment);
2399
2540
  });
2400
2541
  const nonVoucherPayments = (tempOrder.payments || []).filter((payment) => {
2401
- return !isVoucherPayment(payment);
2542
+ return !isVoucherPaymentRecord(payment);
2402
2543
  });
2403
2544
  const syncedVoucherPayments = (tempOrder.payments || []).filter((payment) => {
2404
- return isVoucherPayment(payment) && hasSyncedOrderPayment(payment);
2545
+ return isVoucherPaymentRecord(payment) && hasSyncedOrderPaymentRecord(payment);
2405
2546
  });
2406
2547
  const syncedVoucherPaymentIds = new Set(
2407
2548
  syncedVoucherPayments.map((payment) => String(payment.order_payment_id))
2408
2549
  );
2409
2550
  const replaceableVouchers = mappedVouchers.filter((payment) => {
2410
- if (!hasSyncedOrderPayment(payment))
2551
+ if (!hasSyncedOrderPaymentRecord(payment))
2411
2552
  return true;
2412
2553
  return !syncedVoucherPaymentIds.has(String(payment.order_payment_id));
2413
2554
  });
@@ -2634,7 +2775,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
2634
2775
  */
2635
2776
  async addProductToOrder(product, booking) {
2636
2777
  const tempOrder = this.ensureTempOrder();
2637
- debugger;
2638
2778
  if (booking) {
2639
2779
  const productRecord = product;
2640
2780
  const splitCount = (0, import_utils3.getSafeProductNum)(
@@ -2657,6 +2797,10 @@ var OrderModule = class extends import_BaseModule.BaseModule {
2657
2797
  }
2658
2798
  await this.appendProductLineToTempOrder(tempOrder, product, booking);
2659
2799
  await this.finalizeProductOrderMutation(tempOrder);
2800
+ this.logInfo("addProductToOrder success", {
2801
+ product_id: product.product_id,
2802
+ with_booking: !!booking
2803
+ });
2660
2804
  return tempOrder.products;
2661
2805
  }
2662
2806
  /**
@@ -2698,6 +2842,9 @@ var OrderModule = class extends import_BaseModule.BaseModule {
2698
2842
  await this.appendProductLineToTempOrder(tempOrder, product, booking);
2699
2843
  }
2700
2844
  await this.finalizeProductOrderMutation(tempOrder, options);
2845
+ this.logInfo("addProductsToOrder success", {
2846
+ itemsCount: items.length
2847
+ });
2701
2848
  return tempOrder.products;
2702
2849
  }
2703
2850
  hasGoodPassDiscount(product) {
@@ -2920,6 +3067,9 @@ var OrderModule = class extends import_BaseModule.BaseModule {
2920
3067
  this.sanitizeTempOrderProducts(tempOrder);
2921
3068
  await this.recalculateSummary({ createIfMissing: true });
2922
3069
  this.persistTempOrder();
3070
+ this.logInfo("updateOrderProduct success", {
3071
+ params
3072
+ });
2923
3073
  return tempOrder.products;
2924
3074
  }
2925
3075
  async updateOrderProducts(paramsList) {
@@ -2931,6 +3081,9 @@ var OrderModule = class extends import_BaseModule.BaseModule {
2931
3081
  this.applyOrderProductUpdateToTempOrder(tempOrder, params);
2932
3082
  });
2933
3083
  await this.finalizeAfterProductsMutation(tempOrder);
3084
+ this.logInfo("updateOrderProduct success", {
3085
+ paramsList
3086
+ });
2934
3087
  return tempOrder.products;
2935
3088
  }
2936
3089
  async updateOrderProductQuantity(params) {
@@ -2995,6 +3148,9 @@ var OrderModule = class extends import_BaseModule.BaseModule {
2995
3148
  this.sanitizeTempOrderProducts(tempOrder);
2996
3149
  await this.recalculateSummary({ createIfMissing: true });
2997
3150
  this.persistTempOrder();
3151
+ this.logInfo("updateOrderProductQuantity success", {
3152
+ params
3153
+ });
2998
3154
  return tempOrder.products;
2999
3155
  }
3000
3156
  updateOrderBooking(params) {
@@ -3096,6 +3252,9 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3096
3252
  });
3097
3253
  }
3098
3254
  await this.finalizeAfterProductsMutation(tempOrder);
3255
+ this.logInfo("removeProductsFromOrder success", {
3256
+ identities
3257
+ });
3099
3258
  return tempOrder.products;
3100
3259
  }
3101
3260
  async removeProductFromOrder(identity) {
@@ -3121,6 +3280,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3121
3280
  this.sanitizeTempOrderProducts(tempOrder);
3122
3281
  await this.recalculateSummary({ createIfMissing: true });
3123
3282
  this.persistTempOrder();
3283
+ this.logInfo("clearOrderCartLines success", {});
3124
3284
  return tempOrder;
3125
3285
  }
3126
3286
  // ─── TempOrder: 提交 ───
@@ -3181,19 +3341,35 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3181
3341
  async runSubmitTempOrder(params) {
3182
3342
  var _a, _b, _c, _d, _e;
3183
3343
  const tempOrder = this.ensureTempOrder();
3344
+ const shouldConfirmPendingVoucherPayments = (params == null ? void 0 : params.confirmPendingVoucherPayments) !== false;
3345
+ const hasPaymentOverride = (params == null ? void 0 : params.payments) !== void 0;
3346
+ const preparedPaymentOverride = hasPaymentOverride ? this.prepareVoucherPaymentsForSubmit(
3347
+ (params == null ? void 0 : params.payments) || [],
3348
+ shouldConfirmPendingVoucherPayments
3349
+ ) : void 0;
3350
+ if (!hasPaymentOverride) {
3351
+ if (shouldConfirmPendingVoucherPayments) {
3352
+ this.confirmPendingVoucherPayments({
3353
+ persist: false,
3354
+ recalculateSummary: false
3355
+ });
3356
+ } else {
3357
+ this.discardPendingVoucherPayments({
3358
+ persist: false,
3359
+ recalculateSummary: false
3360
+ });
3361
+ }
3362
+ }
3184
3363
  this.persistTempOrder();
3185
3364
  const latestSummary = await this.recalculateSummary({ createIfMissing: true }) || this.store.summary || (0, import_utils.createEmptySummary)();
3186
3365
  const effectiveCacheId = (params == null ? void 0 : params.cacheId) ?? this.cacheId;
3187
- const hasPaymentOverride = (params == null ? void 0 : params.payments) !== void 0;
3188
3366
  const hasPaymentStatusOverride = (params == null ? void 0 : params.paymentStatus) !== void 0;
3189
3367
  const hasSmallTicketDataFlagOverride = (params == null ? void 0 : params.smallTicketDataFlag) !== void 0;
3190
3368
  const enhancePayload = hasPaymentOverride || hasPaymentStatusOverride || hasSmallTicketDataFlagOverride || (params == null ? void 0 : params.enhancePayload) ? (payload2, ctx) => {
3191
3369
  const nextPayload = {
3192
3370
  ...payload2,
3193
3371
  ...hasPaymentOverride ? {
3194
- payments: (0, import_utils.mapPaymentItemsToOrderPayments)(
3195
- (params == null ? void 0 : params.payments) || []
3196
- )
3372
+ payments: preparedPaymentOverride || []
3197
3373
  } : {},
3198
3374
  ...hasPaymentStatusOverride ? { payment_status: params == null ? void 0 : params.paymentStatus } : {},
3199
3375
  ...hasSmallTicketDataFlagOverride ? { small_ticket_data_flag: params == null ? void 0 : params.smallTicketDataFlag } : {}
@@ -3213,6 +3389,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3213
3389
  request_unique_idempotency_token: params == null ? void 0 : params.request_unique_idempotency_token,
3214
3390
  enhance: enhancePayload
3215
3391
  });
3392
+ console.log("[Order.runSubmitTempOrder.payload]", payload);
3216
3393
  if (params == null ? void 0 : params.syncMode) {
3217
3394
  payload.sync_mode = true;
3218
3395
  }
@@ -3346,9 +3523,12 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3346
3523
  const mappedPayments = (0, import_utils.mapPaymentItemsToOrderPayments)(
3347
3524
  params.payments || []
3348
3525
  );
3349
- const completion = this.getPaymentCompletion(mappedPayments);
3350
- const paymentStatus = completion.isOrderFullyPaid ? params.paymentStatus || "paid" : "partially_paid";
3351
- tempOrder.payments = mappedPayments;
3526
+ const shouldConfirmPendingVoucherPayments = params.confirmPendingVoucherPayments ?? params.submitWhenPaid === true;
3527
+ const effectivePayments = shouldConfirmPendingVoucherPayments ? this.confirmPendingVoucherPaymentsInList(mappedPayments) : params.confirmPendingVoucherPayments === false ? this.discardPendingVoucherPaymentsFromList(mappedPayments) : mappedPayments;
3528
+ const completion = this.getPaymentCompletion(effectivePayments);
3529
+ const paidPaymentTotal = this.calculatePaymentTotal(effectivePayments);
3530
+ const paymentStatus = completion.isOrderFullyPaid ? params.paymentStatus || "paid" : paidPaymentTotal.gt(0) ? "partially_paid" : "payment_processing";
3531
+ tempOrder.payments = effectivePayments;
3352
3532
  tempOrder.payment_status = paymentStatus;
3353
3533
  this.persistTempOrder();
3354
3534
  await this.saveDraft();
@@ -3361,14 +3541,15 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3361
3541
  this.store.syncState = "submitting";
3362
3542
  const paymentSyncIdempotencyToken = this.buildPaymentSyncIdempotencyToken(
3363
3543
  tempOrder,
3364
- mappedPayments
3544
+ effectivePayments
3365
3545
  );
3366
3546
  const submitResult = await this.submitTempOrder({
3367
- payments: mappedPayments,
3547
+ payments: effectivePayments,
3368
3548
  paymentStatus,
3369
3549
  smallTicketDataFlag: params.smallTicketDataFlag,
3370
3550
  businessCode: params.businessCode,
3371
3551
  channel: params.channel,
3552
+ confirmPendingVoucherPayments: true,
3372
3553
  enhancePayload: (payload) => {
3373
3554
  const nextPayload = {
3374
3555
  ...payload,
@@ -3711,6 +3892,10 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3711
3892
  const currentDiscounts = typeof ((_a = this.store.discount) == null ? void 0 : _a.getDiscountList) === "function" ? this.store.discount.getDiscountList() || [] : [];
3712
3893
  const shouldSkipEmptyDiscountSync = hydratedEditDiscounts.length === 0 && currentDiscounts.length === 0;
3713
3894
  if (!shouldSkipEmptyDiscountSync) {
3895
+ OrderModule.populateSavedAmounts(
3896
+ nextTempOrder.products,
3897
+ hydratedEditDiscounts
3898
+ );
3714
3899
  await ((_b = this.store.discount) == null ? void 0 : _b.setOriginalDiscountList(hydratedEditDiscounts));
3715
3900
  await ((_c = this.store.discount) == null ? void 0 : _c.setDiscountList(hydratedEditDiscounts));
3716
3901
  }
@@ -3915,7 +4100,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3915
4100
  return normalized;
3916
4101
  }
3917
4102
  normalizeHydratedOrderProduct(product) {
3918
- const row = { ...product || {} };
4103
+ let row = { ...product || {} };
3919
4104
  if (row.num === void 0 && row.product_quantity !== void 0) {
3920
4105
  row.num = row.product_quantity;
3921
4106
  }
@@ -3936,6 +4121,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3936
4121
  if (row.booking_uid && !row.metadata.booking_uid) {
3937
4122
  row.metadata.booking_uid = row.booking_uid;
3938
4123
  }
4124
+ row = (0, import_bundleDiscountHydration.restoreHydratedBundleDiscounts)(row, import_utils.normalizeOrderProductDiscountList);
3939
4125
  const existingIdentity = row.metadata.unique_identification_number || row.unique_identification_number;
3940
4126
  if (!existingIdentity) {
3941
4127
  row.metadata.unique_identification_number = (0, import_utils.createUuidV4)();
@@ -3955,7 +4141,8 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3955
4141
  };
3956
4142
  if (result.metadata)
3957
4143
  delete result.metadata.price_schema_version;
3958
- return this.sanitizeOrderProductForTempOrder(result);
4144
+ const restored = (0, import_bundleDiscountHydration.restoreHydratedBundleDiscounts)(result, import_utils.normalizeOrderProductDiscountList);
4145
+ return this.sanitizeOrderProductForTempOrder(restored);
3959
4146
  }
3960
4147
  getLastOrderInfo() {
3961
4148
  var _a;
@@ -3964,6 +4151,8 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3964
4151
  async getOrderInfo(order_id) {
3965
4152
  const res = await this.request.get(`/order/sales/${order_id}`, {
3966
4153
  with: ["products", "bookings"]
4154
+ }, {
4155
+ osServer: true
3967
4156
  });
3968
4157
  return res;
3969
4158
  }