@pisell/pisellos 2.3.7 → 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.
@@ -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: "",
@@ -832,6 +862,8 @@ var OrderModule = class extends import_BaseModule.BaseModule {
832
862
  return (0, import_utils.mapPaymentItemsToOrderPayments)(payments, {
833
863
  includeVoided: false
834
864
  }).reduce((sum, payment) => {
865
+ if (!isPaidPaymentRecord(payment))
866
+ return sum;
835
867
  return sum.plus(this.calculatePaymentEffectiveAmount(payment));
836
868
  }, new import_decimal.default(0));
837
869
  }
@@ -949,7 +981,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
949
981
  calculateDepositPaidAmount(payments) {
950
982
  return (payments || []).reduce((sum, payment) => {
951
983
  const paymentRecord = payment;
952
- if ((paymentRecord == null ? void 0 : paymentRecord.status) !== "paid")
984
+ if (!isPaidPaymentRecord(paymentRecord))
953
985
  return sum;
954
986
  const isDepositPayment = paymentRecord.type === "deposit" || paymentRecord.order_payment_type === "deposit";
955
987
  if (!isDepositPayment)
@@ -1100,7 +1132,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
1100
1132
  return (payments || []).reduce(
1101
1133
  (summary, payment) => {
1102
1134
  const paymentRecord = payment;
1103
- if ((paymentRecord == null ? void 0 : paymentRecord.status) !== "paid")
1135
+ if (!isPaidPaymentRecord(paymentRecord))
1104
1136
  return summary;
1105
1137
  return {
1106
1138
  customerPaidAmount: summary.customerPaidAmount.plus(paymentRecord.amount || 0),
@@ -1526,6 +1558,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
1526
1558
  }
1527
1559
  restoreOrder() {
1528
1560
  var _a, _b;
1561
+ this.logInfo("restoreOrder start", {});
1529
1562
  const freshTempOrder = this.createDefaultTempOrderInstance();
1530
1563
  this.ensureExternalSaleNumber(freshTempOrder);
1531
1564
  this.store.tempOrder = freshTempOrder;
@@ -2394,27 +2427,78 @@ var OrderModule = class extends import_BaseModule.BaseModule {
2394
2427
  this.persistTempOrder();
2395
2428
  return tempOrder.payments;
2396
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
+ }
2397
2487
  /**
2398
2488
  * 覆盖当前订单中的 wallet pass 支付项。
2399
2489
  *
2400
2490
  * 判断口径与 PaymentModal 保持一致:voucher_id 存在且不为 0 的支付项
2401
2491
  * 视为 wallet pass;其它支付项保持不变。
2402
2492
  */
2403
- updateVoucherOrderPayments(payments) {
2493
+ updateVoucherOrderPayments(payments, options) {
2404
2494
  const tempOrder = this.ensureTempOrder();
2405
- const isVoucherPayment = (payment) => {
2406
- return payment.voucher_id !== void 0 && Number(payment.voucher_id) !== 0;
2407
- };
2408
- const hasSyncedOrderPayment = (payment) => {
2409
- return payment.order_payment_id !== void 0 && payment.order_payment_id !== null;
2410
- };
2411
2495
  const isOrderPaymentType = (value) => {
2412
2496
  return value === "normal" || value === "deposit";
2413
2497
  };
2414
2498
  const voucherOrderPaymentType = this.resolveNextOrderPaymentType(tempOrder);
2415
2499
  const withVoucherPaymentType = (payment) => {
2416
2500
  const paymentRecord = payment;
2417
- if (!isVoucherPayment(paymentRecord))
2501
+ if (!isVoucherPaymentRecord(paymentRecord))
2418
2502
  return payment;
2419
2503
  const explicitPaymentType = isOrderPaymentType(paymentRecord.order_payment_type) ? paymentRecord.order_payment_type : isOrderPaymentType(paymentRecord.type) ? paymentRecord.type : void 0;
2420
2504
  const nextPaymentType = explicitPaymentType || voucherOrderPaymentType;
@@ -2426,24 +2510,45 @@ var OrderModule = class extends import_BaseModule.BaseModule {
2426
2510
  order_payment_type: nextPaymentType
2427
2511
  };
2428
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
+ };
2530
+ };
2429
2531
  const mappedVouchers = (0, import_utils.mapPaymentItemsToOrderPayments)(
2430
2532
  payments.map(
2431
- (payment) => this.normalizePaymentSource(withVoucherPaymentType(payment), { defaultPaid: true })
2533
+ (payment) => this.normalizePaymentSource(
2534
+ normalizeVoucherPaymentStatus(withVoucherPaymentType(payment)),
2535
+ { defaultPaid: false }
2536
+ )
2432
2537
  )
2433
2538
  ).filter((payment) => {
2434
- return isVoucherPayment(payment);
2539
+ return isVoucherPaymentRecord(payment);
2435
2540
  });
2436
2541
  const nonVoucherPayments = (tempOrder.payments || []).filter((payment) => {
2437
- return !isVoucherPayment(payment);
2542
+ return !isVoucherPaymentRecord(payment);
2438
2543
  });
2439
2544
  const syncedVoucherPayments = (tempOrder.payments || []).filter((payment) => {
2440
- return isVoucherPayment(payment) && hasSyncedOrderPayment(payment);
2545
+ return isVoucherPaymentRecord(payment) && hasSyncedOrderPaymentRecord(payment);
2441
2546
  });
2442
2547
  const syncedVoucherPaymentIds = new Set(
2443
2548
  syncedVoucherPayments.map((payment) => String(payment.order_payment_id))
2444
2549
  );
2445
2550
  const replaceableVouchers = mappedVouchers.filter((payment) => {
2446
- if (!hasSyncedOrderPayment(payment))
2551
+ if (!hasSyncedOrderPaymentRecord(payment))
2447
2552
  return true;
2448
2553
  return !syncedVoucherPaymentIds.has(String(payment.order_payment_id));
2449
2554
  });
@@ -2692,6 +2797,10 @@ var OrderModule = class extends import_BaseModule.BaseModule {
2692
2797
  }
2693
2798
  await this.appendProductLineToTempOrder(tempOrder, product, booking);
2694
2799
  await this.finalizeProductOrderMutation(tempOrder);
2800
+ this.logInfo("addProductToOrder success", {
2801
+ product_id: product.product_id,
2802
+ with_booking: !!booking
2803
+ });
2695
2804
  return tempOrder.products;
2696
2805
  }
2697
2806
  /**
@@ -2733,6 +2842,9 @@ var OrderModule = class extends import_BaseModule.BaseModule {
2733
2842
  await this.appendProductLineToTempOrder(tempOrder, product, booking);
2734
2843
  }
2735
2844
  await this.finalizeProductOrderMutation(tempOrder, options);
2845
+ this.logInfo("addProductsToOrder success", {
2846
+ itemsCount: items.length
2847
+ });
2736
2848
  return tempOrder.products;
2737
2849
  }
2738
2850
  hasGoodPassDiscount(product) {
@@ -2955,6 +3067,9 @@ var OrderModule = class extends import_BaseModule.BaseModule {
2955
3067
  this.sanitizeTempOrderProducts(tempOrder);
2956
3068
  await this.recalculateSummary({ createIfMissing: true });
2957
3069
  this.persistTempOrder();
3070
+ this.logInfo("updateOrderProduct success", {
3071
+ params
3072
+ });
2958
3073
  return tempOrder.products;
2959
3074
  }
2960
3075
  async updateOrderProducts(paramsList) {
@@ -2966,6 +3081,9 @@ var OrderModule = class extends import_BaseModule.BaseModule {
2966
3081
  this.applyOrderProductUpdateToTempOrder(tempOrder, params);
2967
3082
  });
2968
3083
  await this.finalizeAfterProductsMutation(tempOrder);
3084
+ this.logInfo("updateOrderProduct success", {
3085
+ paramsList
3086
+ });
2969
3087
  return tempOrder.products;
2970
3088
  }
2971
3089
  async updateOrderProductQuantity(params) {
@@ -3030,6 +3148,9 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3030
3148
  this.sanitizeTempOrderProducts(tempOrder);
3031
3149
  await this.recalculateSummary({ createIfMissing: true });
3032
3150
  this.persistTempOrder();
3151
+ this.logInfo("updateOrderProductQuantity success", {
3152
+ params
3153
+ });
3033
3154
  return tempOrder.products;
3034
3155
  }
3035
3156
  updateOrderBooking(params) {
@@ -3131,6 +3252,9 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3131
3252
  });
3132
3253
  }
3133
3254
  await this.finalizeAfterProductsMutation(tempOrder);
3255
+ this.logInfo("removeProductsFromOrder success", {
3256
+ identities
3257
+ });
3134
3258
  return tempOrder.products;
3135
3259
  }
3136
3260
  async removeProductFromOrder(identity) {
@@ -3156,6 +3280,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3156
3280
  this.sanitizeTempOrderProducts(tempOrder);
3157
3281
  await this.recalculateSummary({ createIfMissing: true });
3158
3282
  this.persistTempOrder();
3283
+ this.logInfo("clearOrderCartLines success", {});
3159
3284
  return tempOrder;
3160
3285
  }
3161
3286
  // ─── TempOrder: 提交 ───
@@ -3216,19 +3341,35 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3216
3341
  async runSubmitTempOrder(params) {
3217
3342
  var _a, _b, _c, _d, _e;
3218
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
+ }
3219
3363
  this.persistTempOrder();
3220
3364
  const latestSummary = await this.recalculateSummary({ createIfMissing: true }) || this.store.summary || (0, import_utils.createEmptySummary)();
3221
3365
  const effectiveCacheId = (params == null ? void 0 : params.cacheId) ?? this.cacheId;
3222
- const hasPaymentOverride = (params == null ? void 0 : params.payments) !== void 0;
3223
3366
  const hasPaymentStatusOverride = (params == null ? void 0 : params.paymentStatus) !== void 0;
3224
3367
  const hasSmallTicketDataFlagOverride = (params == null ? void 0 : params.smallTicketDataFlag) !== void 0;
3225
3368
  const enhancePayload = hasPaymentOverride || hasPaymentStatusOverride || hasSmallTicketDataFlagOverride || (params == null ? void 0 : params.enhancePayload) ? (payload2, ctx) => {
3226
3369
  const nextPayload = {
3227
3370
  ...payload2,
3228
3371
  ...hasPaymentOverride ? {
3229
- payments: (0, import_utils.mapPaymentItemsToOrderPayments)(
3230
- (params == null ? void 0 : params.payments) || []
3231
- )
3372
+ payments: preparedPaymentOverride || []
3232
3373
  } : {},
3233
3374
  ...hasPaymentStatusOverride ? { payment_status: params == null ? void 0 : params.paymentStatus } : {},
3234
3375
  ...hasSmallTicketDataFlagOverride ? { small_ticket_data_flag: params == null ? void 0 : params.smallTicketDataFlag } : {}
@@ -3248,6 +3389,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3248
3389
  request_unique_idempotency_token: params == null ? void 0 : params.request_unique_idempotency_token,
3249
3390
  enhance: enhancePayload
3250
3391
  });
3392
+ console.log("[Order.runSubmitTempOrder.payload]", payload);
3251
3393
  if (params == null ? void 0 : params.syncMode) {
3252
3394
  payload.sync_mode = true;
3253
3395
  }
@@ -3381,9 +3523,12 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3381
3523
  const mappedPayments = (0, import_utils.mapPaymentItemsToOrderPayments)(
3382
3524
  params.payments || []
3383
3525
  );
3384
- const completion = this.getPaymentCompletion(mappedPayments);
3385
- const paymentStatus = completion.isOrderFullyPaid ? params.paymentStatus || "paid" : "partially_paid";
3386
- 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;
3387
3532
  tempOrder.payment_status = paymentStatus;
3388
3533
  this.persistTempOrder();
3389
3534
  await this.saveDraft();
@@ -3396,14 +3541,15 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3396
3541
  this.store.syncState = "submitting";
3397
3542
  const paymentSyncIdempotencyToken = this.buildPaymentSyncIdempotencyToken(
3398
3543
  tempOrder,
3399
- mappedPayments
3544
+ effectivePayments
3400
3545
  );
3401
3546
  const submitResult = await this.submitTempOrder({
3402
- payments: mappedPayments,
3547
+ payments: effectivePayments,
3403
3548
  paymentStatus,
3404
3549
  smallTicketDataFlag: params.smallTicketDataFlag,
3405
3550
  businessCode: params.businessCode,
3406
3551
  channel: params.channel,
3552
+ confirmPendingVoucherPayments: true,
3407
3553
  enhancePayload: (payload) => {
3408
3554
  const nextPayload = {
3409
3555
  ...payload,
@@ -3746,6 +3892,10 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3746
3892
  const currentDiscounts = typeof ((_a = this.store.discount) == null ? void 0 : _a.getDiscountList) === "function" ? this.store.discount.getDiscountList() || [] : [];
3747
3893
  const shouldSkipEmptyDiscountSync = hydratedEditDiscounts.length === 0 && currentDiscounts.length === 0;
3748
3894
  if (!shouldSkipEmptyDiscountSync) {
3895
+ OrderModule.populateSavedAmounts(
3896
+ nextTempOrder.products,
3897
+ hydratedEditDiscounts
3898
+ );
3749
3899
  await ((_b = this.store.discount) == null ? void 0 : _b.setOriginalDiscountList(hydratedEditDiscounts));
3750
3900
  await ((_c = this.store.discount) == null ? void 0 : _c.setDiscountList(hydratedEditDiscounts));
3751
3901
  }
@@ -3950,7 +4100,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3950
4100
  return normalized;
3951
4101
  }
3952
4102
  normalizeHydratedOrderProduct(product) {
3953
- const row = { ...product || {} };
4103
+ let row = { ...product || {} };
3954
4104
  if (row.num === void 0 && row.product_quantity !== void 0) {
3955
4105
  row.num = row.product_quantity;
3956
4106
  }
@@ -3971,6 +4121,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3971
4121
  if (row.booking_uid && !row.metadata.booking_uid) {
3972
4122
  row.metadata.booking_uid = row.booking_uid;
3973
4123
  }
4124
+ row = (0, import_bundleDiscountHydration.restoreHydratedBundleDiscounts)(row, import_utils.normalizeOrderProductDiscountList);
3974
4125
  const existingIdentity = row.metadata.unique_identification_number || row.unique_identification_number;
3975
4126
  if (!existingIdentity) {
3976
4127
  row.metadata.unique_identification_number = (0, import_utils.createUuidV4)();
@@ -3990,7 +4141,8 @@ var OrderModule = class extends import_BaseModule.BaseModule {
3990
4141
  };
3991
4142
  if (result.metadata)
3992
4143
  delete result.metadata.price_schema_version;
3993
- return this.sanitizeOrderProductForTempOrder(result);
4144
+ const restored = (0, import_bundleDiscountHydration.restoreHydratedBundleDiscounts)(result, import_utils.normalizeOrderProductDiscountList);
4145
+ return this.sanitizeOrderProductForTempOrder(restored);
3994
4146
  }
3995
4147
  getLastOrderInfo() {
3996
4148
  var _a;
@@ -491,7 +491,7 @@ export interface OrderPaymentData {
491
491
  voucher_id?: string | number;
492
492
  origin_amount?: string;
493
493
  service_fee?: string;
494
- status?: 'active' | 'voided' | string;
494
+ status?: 'active' | 'paid' | 'voided' | 'payment_pending' | string;
495
495
  /** 支付时间 (格式: YYYY-MM-DD HH:mm:ss) */
496
496
  payment_time?: string;
497
497
  wallet_pass_usage_unit?: unknown;
@@ -537,6 +537,7 @@ export interface SyncPaymentsToOrderParams {
537
537
  smallTicketDataFlag?: number;
538
538
  businessCode?: string;
539
539
  channel?: string;
540
+ confirmPendingVoucherPayments?: boolean;
540
541
  }
541
542
  export interface SyncPaymentsToOrderResult<T = any> {
542
543
  isFullyPaid: boolean;
@@ -696,6 +697,7 @@ export interface OrderModuleAPI {
696
697
  payments?: OrderPaymentSource[];
697
698
  paymentStatus?: SubmitSalesOrderParams['query']['payment_status'];
698
699
  smallTicketDataFlag?: number;
700
+ confirmPendingVoucherPayments?: boolean;
699
701
  enhancePayload?: SubmitPayloadEnhancer;
700
702
  }) => Promise<T>;
701
703
  submitTempOrderAsync: <T = any>(params?: {
@@ -707,6 +709,7 @@ export interface OrderModuleAPI {
707
709
  payments?: OrderPaymentSource[];
708
710
  paymentStatus?: SubmitSalesOrderParams['query']['payment_status'];
709
711
  smallTicketDataFlag?: number;
712
+ confirmPendingVoucherPayments?: boolean;
710
713
  enhancePayload?: SubmitPayloadEnhancer;
711
714
  }) => Promise<T>;
712
715
  getOrderPayments: () => OrderPaymentData[];
@@ -714,7 +717,17 @@ export interface OrderModuleAPI {
714
717
  addOrderPayment: (payment: OrderPaymentSource) => OrderPaymentData[];
715
718
  updateOrderPayment: (paymentIdentity: string | number, updates: Partial<OrderPaymentData> & Record<string, any>) => OrderPaymentData[];
716
719
  deleteOrderPayment: (paymentIdentity: string | number) => OrderPaymentData[];
717
- updateVoucherOrderPayments: (payments: OrderPaymentSource[]) => OrderPaymentData[];
720
+ updateVoucherOrderPayments: (payments: OrderPaymentSource[], options?: {
721
+ status?: 'paid' | 'payment_pending';
722
+ }) => OrderPaymentData[];
723
+ confirmPendingVoucherPayments: (options?: {
724
+ persist?: boolean;
725
+ recalculateSummary?: boolean;
726
+ }) => OrderPaymentData[];
727
+ discardPendingVoucherPayments: (options?: {
728
+ persist?: boolean;
729
+ recalculateSummary?: boolean;
730
+ }) => OrderPaymentData[];
718
731
  sendCustomerPayLink: <T = any>(params: SendCustomerPayLinkParams) => Promise<T>;
719
732
  saveDraft: () => Promise<void>;
720
733
  hydrateTempOrderFromRecord: (record: Record<string, any>, options?: {
@@ -0,0 +1,5 @@
1
+ import type { OrderProductDiscountItem } from '../types';
2
+ type NormalizeDiscountList = (discountList?: unknown[] | null) => OrderProductDiscountItem[];
3
+ export declare function restoreHydratedBundleDiscounts<T extends Record<string, any>>(product: T, normalizeDiscountList: NormalizeDiscountList): T;
4
+ export declare function expandHydratedProductsForDiscountCollection(products: any[] | undefined | null): any[];
5
+ export {};
@@ -0,0 +1,139 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/modules/Order/utils/bundleDiscountHydration.ts
20
+ var bundleDiscountHydration_exports = {};
21
+ __export(bundleDiscountHydration_exports, {
22
+ expandHydratedProductsForDiscountCollection: () => expandHydratedProductsForDiscountCollection,
23
+ restoreHydratedBundleDiscounts: () => restoreHydratedBundleDiscounts
24
+ });
25
+ module.exports = __toCommonJS(bundleDiscountHydration_exports);
26
+ var BUNDLE_MAP_ID_KEY = "custom_product_bundle_map_id";
27
+ function getBundleMapId(bundle) {
28
+ var _a;
29
+ const metadataMapId = (_a = bundle == null ? void 0 : bundle.metadata) == null ? void 0 : _a[BUNDLE_MAP_ID_KEY];
30
+ if (metadataMapId !== void 0 && metadataMapId !== null && metadataMapId !== "") {
31
+ return String(metadataMapId);
32
+ }
33
+ if ((bundle == null ? void 0 : bundle._id) !== void 0 && bundle._id !== null && bundle._id !== "") {
34
+ return String(bundle._id);
35
+ }
36
+ return null;
37
+ }
38
+ function getDiscountBundleMapId(discount) {
39
+ var _a;
40
+ const mapId = (_a = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a[BUNDLE_MAP_ID_KEY];
41
+ if (mapId === void 0 || mapId === null || mapId === "")
42
+ return null;
43
+ return String(mapId);
44
+ }
45
+ function hasSameBundleDiscount(discountList, discount) {
46
+ var _a;
47
+ const targetOrderDiscountId = discount.order_discount_id;
48
+ if (targetOrderDiscountId !== void 0 && targetOrderDiscountId !== null) {
49
+ return discountList.some((item) => (item == null ? void 0 : item.order_discount_id) === targetOrderDiscountId);
50
+ }
51
+ const targetResourceId = ((_a = discount == null ? void 0 : discount.discount) == null ? void 0 : _a.resource_id) ?? (discount == null ? void 0 : discount.resource_id) ?? (discount == null ? void 0 : discount.discount_id);
52
+ const targetMapId = getDiscountBundleMapId(discount);
53
+ return discountList.some((item) => {
54
+ var _a2;
55
+ const resourceId = ((_a2 = item == null ? void 0 : item.discount) == null ? void 0 : _a2.resource_id) ?? (item == null ? void 0 : item.resource_id) ?? (item == null ? void 0 : item.discount_id);
56
+ return resourceId === targetResourceId && String((item == null ? void 0 : item.amount) ?? "") === String((discount == null ? void 0 : discount.amount) ?? "") && getDiscountBundleMapId(item) === targetMapId;
57
+ });
58
+ }
59
+ function restoreHydratedBundleDiscounts(product, normalizeDiscountList) {
60
+ if (!Array.isArray(product.product_bundle) || !Array.isArray(product.discount_list)) {
61
+ return product;
62
+ }
63
+ const bundleByMapId = /* @__PURE__ */ new Map();
64
+ product.product_bundle.forEach((bundle) => {
65
+ if (!bundle || typeof bundle !== "object")
66
+ return;
67
+ const mapId = getBundleMapId(bundle);
68
+ if (mapId)
69
+ bundleByMapId.set(mapId, bundle);
70
+ });
71
+ if (!bundleByMapId.size)
72
+ return product;
73
+ const mainDiscountList = [];
74
+ const bundleDiscounts = [];
75
+ product.discount_list.forEach((discount) => {
76
+ const mapId = getDiscountBundleMapId(discount);
77
+ if (mapId && bundleByMapId.has(mapId)) {
78
+ bundleDiscounts.push({ mapId, discount });
79
+ } else {
80
+ mainDiscountList.push(discount);
81
+ }
82
+ });
83
+ if (!bundleDiscounts.length)
84
+ return product;
85
+ return {
86
+ ...product,
87
+ discount_list: normalizeDiscountList(mainDiscountList),
88
+ product_bundle: product.product_bundle.map((bundle) => {
89
+ if (!bundle || typeof bundle !== "object")
90
+ return bundle;
91
+ const mapId = getBundleMapId(bundle);
92
+ if (!mapId)
93
+ return bundle;
94
+ const matchedDiscounts = bundleDiscounts.filter((item) => item.mapId === mapId).map((item) => item.discount);
95
+ if (!matchedDiscounts.length)
96
+ return bundle;
97
+ const currentDiscountList = Array.isArray(bundle.discount_list) ? [...bundle.discount_list] : [];
98
+ for (const discount of matchedDiscounts) {
99
+ if (!hasSameBundleDiscount(currentDiscountList, discount)) {
100
+ currentDiscountList.push(discount);
101
+ }
102
+ }
103
+ return {
104
+ ...bundle,
105
+ discount_list: normalizeDiscountList(currentDiscountList),
106
+ metadata: {
107
+ ...bundle.metadata || {},
108
+ [BUNDLE_MAP_ID_KEY]: mapId
109
+ }
110
+ };
111
+ })
112
+ };
113
+ }
114
+ function expandHydratedProductsForDiscountCollection(products) {
115
+ const expanded = [];
116
+ for (const product of products || []) {
117
+ expanded.push(product);
118
+ for (const bundle of (product == null ? void 0 : product.product_bundle) || []) {
119
+ if (!Array.isArray(bundle == null ? void 0 : bundle.discount_list) || bundle.discount_list.length === 0)
120
+ continue;
121
+ expanded.push({
122
+ ...product,
123
+ product_id: bundle.bundle_product_id ?? bundle._bundle_product_id ?? bundle.product_id ?? (product == null ? void 0 : product.product_id),
124
+ num: bundle.num ?? bundle.quantity ?? (product == null ? void 0 : product.num),
125
+ product_quantity: bundle.quantity ?? bundle.num ?? (product == null ? void 0 : product.product_quantity),
126
+ original_price: bundle.original_price ?? bundle.product_price ?? bundle.price,
127
+ selling_price: bundle.bundle_selling_price ?? bundle.price,
128
+ is_charge_tax: bundle.is_charge_tax ?? (product == null ? void 0 : product.is_charge_tax),
129
+ discount_list: bundle.discount_list
130
+ });
131
+ }
132
+ }
133
+ return expanded;
134
+ }
135
+ // Annotate the CommonJS export names for ESM import in node:
136
+ 0 && (module.exports = {
137
+ expandHydratedProductsForDiscountCollection,
138
+ restoreHydratedBundleDiscounts
139
+ });