@pisell/pisellos 2.1.24 → 2.1.26

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_Payment = require("../../modules/Payment");
40
40
  var import_types = require("./types");
41
41
  var import_types2 = require("../../modules/Payment/types");
42
42
  var import_utils = require("./utils");
43
+ var import_utils2 = require("../../modules/Cart/utils");
43
44
  __reExport(Checkout_exports, require("./types"), module.exports);
44
45
  var CheckoutImpl = class extends import_BaseModule.BaseModule {
45
46
  constructor(name, version) {
@@ -221,7 +222,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
221
222
  return [...productList, ...relationProducts];
222
223
  }
223
224
  async initWalletData(params) {
224
- var _a, _b, _c, _d;
225
+ var _a, _b, _c, _d, _e, _f, _g;
225
226
  this.logInfo("initWalletData called", {
226
227
  params
227
228
  });
@@ -236,11 +237,16 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
236
237
  customer_id: ((_c = this.store.currentCustomer) == null ? void 0 : _c.customer_id) ? Number(this.store.currentCustomer.customer_id) : void 0,
237
238
  amountInfo: {
238
239
  totalAmount: amountInfo.totalAmount,
239
- subTotal: amountInfo.subTotal
240
+ subTotal: amountInfo.subTotal,
241
+ depositAmount: (_d = this.store.currentOrder) == null ? void 0 : _d.deposit_amount,
242
+ isDeposit: (_e = this.store.currentOrder) == null ? void 0 : _e.is_deposit
240
243
  },
241
244
  products: this.getProductListByOrder(),
242
245
  ...params
243
246
  };
247
+ if (this.store.isOrderSynced) {
248
+ walletBusinessData.payment_order_id = (_f = this.store.currentOrder) == null ? void 0 : _f.order_id;
249
+ }
244
250
  this.logInfo("开始拉取:initializeWalletDataFromBusinessAsync", {
245
251
  walletBusinessData
246
252
  });
@@ -257,7 +263,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
257
263
  currentOrder: this.store.currentOrder
258
264
  });
259
265
  await this.core.effects.emit(import_types.CheckoutHooks.OnWalletDataInitialized, {
260
- orderUuid: (_d = this.store.currentOrder) == null ? void 0 : _d.uuid,
266
+ orderUuid: (_g = this.store.currentOrder) == null ? void 0 : _g.uuid,
261
267
  customerId: walletBusinessData.customer_id,
262
268
  walletBusinessData: {
263
269
  customer_id: walletBusinessData.customer_id,
@@ -267,6 +273,57 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
267
273
  timestamp: Date.now()
268
274
  });
269
275
  }
276
+ checkIsNeedDepositAsync(bookings, relationProducts) {
277
+ const summaryDeposit = {
278
+ total: 0,
279
+ protocols: [],
280
+ hasDeposit: false
281
+ };
282
+ bookings == null ? void 0 : bookings.forEach((item) => {
283
+ const total = item.product.calculated_selling_price;
284
+ const deposit = (0, import_utils2.getProductDeposit)({
285
+ cartItem: {
286
+ total
287
+ },
288
+ product: item.product,
289
+ bundle: item.bundle,
290
+ options: item.options,
291
+ num: item.num
292
+ });
293
+ item.deposit = deposit;
294
+ if ((deposit == null ? void 0 : deposit.total) && Number(deposit.total) > 0) {
295
+ summaryDeposit.protocols = summaryDeposit.protocols.concat(
296
+ deposit.protocols
297
+ );
298
+ summaryDeposit.total = new import_decimal.default(summaryDeposit.total).plus(deposit.total).toNumber();
299
+ summaryDeposit.hasDeposit = true;
300
+ }
301
+ });
302
+ relationProducts == null ? void 0 : relationProducts.forEach((item) => {
303
+ const deposit = (0, import_utils2.getProductDeposit)({
304
+ cartItem: {
305
+ total: item.price * item.num
306
+ },
307
+ product: item,
308
+ bundle: item.product_bundle,
309
+ options: item.product_option_item,
310
+ num: item.num
311
+ });
312
+ item.deposit = deposit;
313
+ if ((deposit == null ? void 0 : deposit.total) && Number(deposit.total) > 0) {
314
+ summaryDeposit.total = new import_decimal.default(summaryDeposit.total).plus(deposit.total).toNumber();
315
+ item.discount_list.forEach((discount) => {
316
+ summaryDeposit.total = new import_decimal.default(summaryDeposit.total).minus(discount.amount || 0).toNumber();
317
+ });
318
+ summaryDeposit.protocols = summaryDeposit.protocols.concat(
319
+ deposit.protocols
320
+ );
321
+ summaryDeposit.hasDeposit = true;
322
+ }
323
+ });
324
+ summaryDeposit.total = new import_decimal.default(summaryDeposit.total).toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toNumber();
325
+ return summaryDeposit;
326
+ }
270
327
  /**
271
328
  * 创建本地订单 (前端模拟下单流程)
272
329
  *
@@ -316,11 +373,15 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
316
373
  this.store.currentCustomer = void 0;
317
374
  console.log("[Checkout] 未提供客户信息");
318
375
  }
376
+ const isNeedDeposit = this.checkIsNeedDepositAsync(
377
+ params.orderData.bookings,
378
+ params.orderData.relation_products
379
+ );
319
380
  const paymentOrder = await this.payment.createPaymentOrderAsync({
320
381
  order_id: localOrderId,
321
382
  total_amount: amountInfo.totalAmount,
322
- is_deposit: params.orderData.is_deposit || 0,
323
- deposit_amount: amountInfo.depositAmount || "0.00",
383
+ is_deposit: isNeedDeposit.hasDeposit ? 1 : 0,
384
+ deposit_amount: isNeedDeposit.total ? isNeedDeposit.total.toString() : "0.00",
324
385
  order_info: {
325
386
  original_order_data: params.orderData,
326
387
  cart_summary: params.cartSummary,
@@ -339,7 +400,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
339
400
  order: paymentOrder,
340
401
  timestamp: Date.now()
341
402
  });
342
- await this.updateStateAmountToRemaining();
403
+ await this.updateStateAmountToRemaining(false);
343
404
  this.logInfo("本地订单创建成功:", {
344
405
  localOrderId,
345
406
  uuid: paymentOrder.uuid,
@@ -363,6 +424,141 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
363
424
  throw error;
364
425
  }
365
426
  }
427
+ /**
428
+ * 更新本地订单(已同步后端的订单)并设置为当前订单
429
+ *
430
+ * 通过传入真实的 orderId,对已缓存的订单数据进行更新,
431
+ * 会覆盖 order_info 与金额等字段,并重新计算待付金额,最后设置为 currentOrder。
432
+ */
433
+ async updateLocalOrderAsync(params) {
434
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
435
+ this.logInfo("updateLocalOrderAsync called", {
436
+ orderId: params.orderId,
437
+ orderDataType: (_a = params.orderData) == null ? void 0 : _a.type,
438
+ bookingsCount: ((_c = (_b = params.orderData) == null ? void 0 : _b.bookings) == null ? void 0 : _c.length) || 0,
439
+ cartSummaryCount: ((_d = params.cartSummary) == null ? void 0 : _d.length) || 0,
440
+ totalInfoKeys: params.totalInfo ? Object.keys(params.totalInfo) : []
441
+ });
442
+ const validation = (0, import_utils.validateLocalOrderData)(params.orderData);
443
+ if (!validation.valid) {
444
+ throw (0, import_utils.createCheckoutError)(
445
+ import_types.CheckoutErrorType.ValidationFailed,
446
+ `订单数据验证失败: ${validation.errors.join(", ")}`
447
+ );
448
+ }
449
+ const amountInfo = (0, import_utils.extractAmountFromCartSummary)(params.cartSummary);
450
+ params.orderData.created_at = (0, import_utils.formatDateTime)(/* @__PURE__ */ new Date());
451
+ params.orderData.platform = "pos";
452
+ params.orderData.surcharge_fee = (_f = (_e = params.totalInfo) == null ? void 0 : _e.total) == null ? void 0 : _f.otherAmount;
453
+ params.orderData.surcharges = (_h = (_g = params.totalInfo) == null ? void 0 : _g.total) == null ? void 0 : _h.surcharge;
454
+ params.orderData.shop_discount = (_j = (_i = params.totalInfo) == null ? void 0 : _i.total) == null ? void 0 : _j.shopDiscount;
455
+ params.orderData.tax_fee = (_l = (_k = params.totalInfo) == null ? void 0 : _k.total) == null ? void 0 : _l.tax;
456
+ this.store.localOrderData = params.orderData;
457
+ this.store.cartSummary = params.cartSummary;
458
+ const customerInfo = {
459
+ customer_id: params.orderData.customer_id,
460
+ customer_name: params.orderData.customer_name
461
+ };
462
+ if (customerInfo.customer_id || customerInfo.customer_name) {
463
+ this.store.currentCustomer = customerInfo;
464
+ } else {
465
+ this.store.currentCustomer = void 0;
466
+ }
467
+ const allOrders = await this.payment.getOrderListAsync();
468
+ const existingOrder = allOrders.find(
469
+ (o) => String(o.id) === String(params.orderId) || String(o.order_id) === String(params.orderId)
470
+ );
471
+ if (existingOrder) {
472
+ const totalAmount = new import_decimal.default(amountInfo.totalAmount || "0");
473
+ const activePayments = (existingOrder.payment || []).filter(
474
+ (p) => p.status !== "voided"
475
+ );
476
+ const paidAmount = activePayments.reduce((sum, p) => {
477
+ const amt = new import_decimal.default(p.amount || "0");
478
+ const rounding = new import_decimal.default(p.rounding_amount || "0").abs();
479
+ return sum.plus(amt).plus(rounding);
480
+ }, new import_decimal.default(0));
481
+ const remaining = import_decimal.default.max(0, totalAmount.minus(paidAmount)).toFixed(
482
+ 2
483
+ );
484
+ const isNeedDeposit2 = this.checkIsNeedDepositAsync(
485
+ params.orderData.bookings,
486
+ params.orderData.relation_products
487
+ );
488
+ await this.payment.updateOrderAsync(existingOrder.uuid, {
489
+ total_amount: amountInfo.totalAmount,
490
+ is_deposit: isNeedDeposit2.hasDeposit ? 1 : 0,
491
+ deposit_amount: isNeedDeposit2.total ? isNeedDeposit2.total.toString() : "0.00",
492
+ expect_amount: remaining,
493
+ order_info: {
494
+ original_order_data: params.orderData,
495
+ cart_summary: params.cartSummary,
496
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
497
+ platform: params.orderData.platform,
498
+ type: params.orderData.type,
499
+ schedule_date: params.orderData.schedule_date,
500
+ shop_note: params.orderData.shop_note,
501
+ amount_breakdown: amountInfo
502
+ }
503
+ });
504
+ const updated = await this.payment.getPaymentOrderByUuidAsync(
505
+ existingOrder.uuid
506
+ );
507
+ if (!updated)
508
+ throw (0, import_utils.createCheckoutError)(
509
+ import_types.CheckoutErrorType.UnknownError,
510
+ "订单更新失败"
511
+ );
512
+ this.store.currentOrder = updated;
513
+ await this.updateStateAmountToRemaining(false);
514
+ this.logInfo("本地订单更新成功(保留支付项):", {
515
+ orderId: params.orderId,
516
+ uuid: updated.uuid,
517
+ payments: ((_m = updated.payment) == null ? void 0 : _m.length) || 0,
518
+ totalAmount: updated.total_amount,
519
+ expectAmount: updated.expect_amount
520
+ });
521
+ await this.core.effects.emit(import_types.CheckoutHooks.OnOrderCreated, {
522
+ order: updated,
523
+ timestamp: Date.now()
524
+ });
525
+ const syncedAmount = activePayments.reduce((sum, p) => {
526
+ const amt = new import_decimal.default(p.amount || "0");
527
+ const rounding = new import_decimal.default(p.rounding_amount || "0").abs();
528
+ return sum.plus(amt).plus(rounding);
529
+ }, new import_decimal.default(0));
530
+ const remainingExpectAmount = import_decimal.default.max(0, totalAmount.minus(syncedAmount)).toFixed(
531
+ 2
532
+ );
533
+ this.initWalletData({
534
+ order_wait_pay_amount: Number(remainingExpectAmount)
535
+ });
536
+ return updated;
537
+ }
538
+ const isNeedDeposit = this.checkIsNeedDepositAsync(
539
+ params.orderData.bookings,
540
+ params.orderData.relation_products
541
+ );
542
+ const created = await this.payment.createPaymentOrderAsync({
543
+ order_id: String(params.orderId),
544
+ total_amount: amountInfo.totalAmount,
545
+ is_deposit: isNeedDeposit.hasDeposit ? 1 : 0,
546
+ deposit_amount: isNeedDeposit.total ? isNeedDeposit.total.toString() : "0.00",
547
+ order_info: {
548
+ original_order_data: params.orderData,
549
+ cart_summary: params.cartSummary,
550
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
551
+ platform: params.orderData.platform,
552
+ type: params.orderData.type,
553
+ schedule_date: params.orderData.schedule_date,
554
+ shop_note: params.orderData.shop_note,
555
+ amount_breakdown: amountInfo
556
+ }
557
+ });
558
+ this.store.currentOrder = created;
559
+ this.initWalletData();
560
+ return created;
561
+ }
366
562
  /**
367
563
  * 完成结账
368
564
  */
@@ -504,7 +700,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
504
700
  newOrderId: updatedOrder.id
505
701
  });
506
702
  this.store.isOrderSynced = true;
507
- await this.updateStateAmountToRemaining();
703
+ await this.updateStateAmountToRemaining(false);
508
704
  }
509
705
  return updatedOrder;
510
706
  } catch (error) {
@@ -687,7 +883,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
687
883
  }
688
884
  }
689
885
  }
690
- await this.core.effects.emit(import_types.CheckoutHooks.OnPaymentItemAdded, {
886
+ this.core.effects.emit(import_types.CheckoutHooks.OnPaymentItemAdded, {
691
887
  orderUuid: this.store.currentOrder.uuid,
692
888
  paymentMethodCode: paymentItem.code,
693
889
  paymentMethodName: paymentItem.name,
@@ -762,7 +958,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
762
958
  }
763
959
  this.store.currentOrder = updatedOrder;
764
960
  }
765
- await this.updateStateAmountToRemaining();
961
+ await this.updateStateAmountToRemaining(false);
766
962
  await this.core.effects.emit(import_types.CheckoutHooks.OnPaymentStarted, {
767
963
  orderUuid: this.store.currentOrder.uuid,
768
964
  paymentMethodCode: paymentItem.code,
@@ -794,10 +990,14 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
794
990
  "当前没有活跃订单,无法更新代金券支付项"
795
991
  );
796
992
  }
993
+ const paymentItems = await this.payment.getPaymentItemsAsync(
994
+ this.store.currentOrder.uuid
995
+ );
996
+ const allPaymentItemsSynced = paymentItems.every((item) => item.isSynced);
797
997
  const remainingAmount = await this.calculateRemainingAmountAsync();
798
998
  const remainingValue = new import_decimal.default(remainingAmount);
799
999
  const isOrderSynced = this.store.isOrderSynced;
800
- if (remainingValue.lte(0) && isOrderSynced && voucherPaymentItems.length === 0) {
1000
+ if (remainingValue.lte(0) && isOrderSynced && voucherPaymentItems.length === 0 && allPaymentItemsSynced) {
801
1001
  this.logInfo("订单已同步且支付完成,跳过清空代金券操作避免重复同步:", {
802
1002
  orderUuid: this.store.currentOrder.uuid,
803
1003
  orderId: this.store.currentOrder.order_id,
@@ -852,7 +1052,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
852
1052
  }
853
1053
  this.store.currentOrder = updatedOrder;
854
1054
  }
855
- await this.updateStateAmountToRemaining();
1055
+ await this.updateStateAmountToRemaining(false);
856
1056
  await this.core.effects.emit(import_types.CheckoutHooks.OnPaymentStarted, {
857
1057
  orderUuid: this.store.currentOrder.uuid,
858
1058
  paymentMethodCode: "VOUCHER_BATCH",
@@ -894,13 +1094,14 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
894
1094
  console.log("[Checkout] 定金状态无变化,跳过更新");
895
1095
  return;
896
1096
  }
1097
+ let deposit_amount = newDepositValue === 1 ? this.store.currentOrder.deposit_amount : "0.00";
1098
+ if (!deposit_amount || Number(deposit_amount) === 0) {
1099
+ deposit_amount = this.store.currentOrder.expect_amount;
1100
+ }
897
1101
  const updateParams = {
898
- is_deposit: newDepositValue
1102
+ is_deposit: newDepositValue,
1103
+ deposit_amount
899
1104
  };
900
- if (isDeposit === 0 && this.store.currentOrder.deposit_amount !== "0.00") {
901
- updateParams.deposit_amount = "0.00";
902
- this.logInfo("订单从定金改为全款,清空定金金额");
903
- }
904
1105
  await this.payment.updateOrderAsync(
905
1106
  this.store.currentOrder.uuid,
906
1107
  updateParams
@@ -912,6 +1113,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
912
1113
  if (this.store.localOrderData) {
913
1114
  this.store.localOrderData.is_deposit = newDepositValue;
914
1115
  }
1116
+ this.updateStateAmountToRemaining(false);
915
1117
  await this.core.effects.emit(import_types.CheckoutHooks.OnOrderCreated, {
916
1118
  order: this.store.currentOrder,
917
1119
  timestamp: Date.now()
@@ -960,13 +1162,6 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
960
1162
  "未找到当前订单,无法设置定金金额"
961
1163
  );
962
1164
  }
963
- const totalAmount = new import_decimal.default(this.store.currentOrder.total_amount || "0");
964
- if (depositValue.gt(totalAmount)) {
965
- throw (0, import_utils.createCheckoutError)(
966
- import_types.CheckoutErrorType.ValidationFailed,
967
- `定金金额 ${depositAmount} 不能超过订单总额 ${totalAmount.toFixed(2)}`
968
- );
969
- }
970
1165
  const formattedDepositAmount = depositValue.toFixed(2);
971
1166
  const oldDepositAmount = this.store.currentOrder.deposit_amount || "0.00";
972
1167
  if (formattedDepositAmount === oldDepositAmount) {
@@ -981,7 +1176,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
981
1176
  orderId: this.store.currentOrder.order_id,
982
1177
  oldDepositAmount,
983
1178
  newDepositAmount: formattedDepositAmount,
984
- totalAmount: totalAmount.toFixed(2)
1179
+ totalAmount: this.store.currentOrder.total_amount
985
1180
  });
986
1181
  const updateParams = {
987
1182
  deposit_amount: formattedDepositAmount
@@ -1009,6 +1204,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1009
1204
  this.store.localOrderData.is_deposit = updateParams.is_deposit;
1010
1205
  }
1011
1206
  }
1207
+ this.updateStateAmountToRemaining(false);
1012
1208
  await this.core.effects.emit(import_types.CheckoutHooks.OnOrderCreated, {
1013
1209
  order: this.store.currentOrder,
1014
1210
  timestamp: Date.now()
@@ -1036,7 +1232,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1036
1232
  * 用于强制同步订单到后端,特别适用于纯代金券支付完成的订单
1037
1233
  */
1038
1234
  async manualSyncOrderAsync() {
1039
- var _a, _b, _c, _d;
1235
+ var _a, _b, _c, _d, _e;
1040
1236
  this.logInfo("manualSyncOrderAsync called", {
1041
1237
  hasCurrentOrder: !!this.store.currentOrder,
1042
1238
  currentOrderId: (_a = this.store.currentOrder) == null ? void 0 : _a.order_id,
@@ -1081,14 +1277,19 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1081
1277
  "[Checkout] 严重警告:返回的订单ID与存储的订单ID不一致!"
1082
1278
  );
1083
1279
  }
1084
- return syncResult;
1280
+ let newRes = {
1281
+ ...syncResult,
1282
+ is_deposit: ((_d = this.store.currentOrder) == null ? void 0 : _d.is_deposit) || 0
1283
+ };
1284
+ await this.updateStateAmountToRemaining(false);
1285
+ return newRes;
1085
1286
  } catch (error) {
1086
1287
  this.logError("手动同步订单失败:", error);
1087
1288
  const errorMessage = error instanceof Error ? error.message : "同步失败";
1088
1289
  return {
1089
1290
  success: false,
1090
1291
  message: `订单同步失败: ${errorMessage}`,
1091
- orderUuid: (_d = this.store.currentOrder) == null ? void 0 : _d.uuid
1292
+ orderUuid: (_e = this.store.currentOrder) == null ? void 0 : _e.uuid
1092
1293
  };
1093
1294
  }
1094
1295
  }
@@ -1599,9 +1800,13 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1599
1800
  this.logWarning("calculateRemainingAmountAsync: 没有当前订单");
1600
1801
  return "0.00";
1601
1802
  }
1602
- const totalAmount = new import_decimal.default(
1603
- this.store.currentOrder.total_amount || "0"
1803
+ let totalAmount = new import_decimal.default(this.store.currentOrder.total_amount || "0");
1804
+ const currentOrder = await this.payment.getPaymentOrderByUuidAsync(
1805
+ this.store.currentOrder.uuid
1604
1806
  );
1807
+ if ((currentOrder == null ? void 0 : currentOrder.is_deposit) && (currentOrder == null ? void 0 : currentOrder.deposit_amount) && Number(currentOrder == null ? void 0 : currentOrder.deposit_amount) > 0) {
1808
+ totalAmount = new import_decimal.default(currentOrder == null ? void 0 : currentOrder.deposit_amount);
1809
+ }
1605
1810
  const paidAmountStr = await this.calculatePaidAmountAsync();
1606
1811
  const paidAmount = new import_decimal.default(paidAmountStr);
1607
1812
  const remainingAmount = totalAmount.sub(paidAmount);
@@ -1640,8 +1845,33 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1640
1845
  /**
1641
1846
  * 更新 stateAmount 为当前剩余未支付金额
1642
1847
  */
1643
- async updateStateAmountToRemaining() {
1848
+ async updateStateAmountToRemaining(checkOrder = true) {
1849
+ var _a, _b, _c, _d;
1644
1850
  try {
1851
+ if (((_a = this.store.currentOrder) == null ? void 0 : _a.is_deposit) === 1) {
1852
+ const paymentItems = await this.payment.getPaymentItemsAsync(
1853
+ (_b = this.store.currentOrder) == null ? void 0 : _b.uuid
1854
+ );
1855
+ const depositPaidAmount = paymentItems.filter(
1856
+ (item) => item.order_payment_type === "deposit" && item.status !== "voided"
1857
+ ).reduce((sum, item) => {
1858
+ if (item.voucher_id && !item.isSynced) {
1859
+ return sum;
1860
+ }
1861
+ const amount = new import_decimal.default(item.amount || "0");
1862
+ const roundingAmount = new import_decimal.default(item.rounding_amount || "0");
1863
+ return sum.add(amount.add(roundingAmount.abs()));
1864
+ }, new import_decimal.default(0));
1865
+ if (depositPaidAmount.gte((_c = this.store.currentOrder) == null ? void 0 : _c.deposit_amount)) {
1866
+ this.store.currentOrder = {
1867
+ ...this.store.currentOrder,
1868
+ is_deposit: 0
1869
+ };
1870
+ await this.payment.updateOrderAsync((_d = this.store.currentOrder) == null ? void 0 : _d.uuid, {
1871
+ is_deposit: 0
1872
+ });
1873
+ }
1874
+ }
1645
1875
  const remainingAmount = await this.calculateRemainingAmountAsync();
1646
1876
  const currentStateAmount = this.store.stateAmount;
1647
1877
  if (remainingAmount !== currentStateAmount) {
@@ -1662,7 +1892,11 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1662
1892
  });
1663
1893
  }
1664
1894
  await this.updateBalanceDueAmount();
1665
- await this.checkOrderPaymentCompletion();
1895
+ if (checkOrder) {
1896
+ await this.checkOrderPaymentCompletion();
1897
+ } else {
1898
+ this.logInfo("外部传入无需 checkOrder,不执行订单支付完成检测和同步");
1899
+ }
1666
1900
  } catch (error) {
1667
1901
  this.logError("更新 stateAmount 为剩余金额失败:", error);
1668
1902
  }
@@ -1748,7 +1982,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1748
1982
  * @returns 包含订单ID、UUID和完整后端响应的对象
1749
1983
  */
1750
1984
  async syncOrderToBackendWithReturn(isManual = false, customPaymentItems) {
1751
- var _a, _b, _c, _d, _e, _f;
1985
+ var _a, _b, _c, _d, _e, _f, _g, _h;
1752
1986
  if (!this.store.localOrderData || !this.store.currentOrder) {
1753
1987
  throw new Error("缺少必要的订单数据,无法同步到后端");
1754
1988
  }
@@ -1776,42 +2010,23 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1776
2010
  });
1777
2011
  let finalDepositAmount;
1778
2012
  const manualDepositAmount = ((_a = this.store.currentOrder) == null ? void 0 : _a.deposit_amount) || "0.00";
1779
- const depositPaymentItems = processedPaymentItems.filter(
1780
- (item) => item.order_payment_type === "deposit" && item.status !== "voided"
1781
- );
1782
- const calculatedDepositAmount = depositPaymentItems.reduce((sum, item) => {
1783
- const amount = new import_decimal.default(item.amount || "0");
1784
- const roundingAmount = new import_decimal.default(item.rounding_amount || "0");
1785
- const effectiveAmount = amount.add(roundingAmount.abs());
1786
- return sum.add(effectiveAmount);
1787
- }, new import_decimal.default(0)).toFixed(2);
1788
2013
  const manualDepositValue = new import_decimal.default(manualDepositAmount);
1789
- const calculatedDepositValue = new import_decimal.default(calculatedDepositAmount);
1790
2014
  if (manualDepositValue.gt(0)) {
1791
2015
  finalDepositAmount = manualDepositAmount;
1792
2016
  this.logInfo("使用手动设置的定金金额", {
1793
2017
  manualDepositAmount,
1794
- calculatedDepositAmount,
1795
2018
  reason: "用户通过setDepositAmountAsync手动设置了定金金额"
1796
2019
  });
1797
- } else if (calculatedDepositValue.gt(0)) {
1798
- finalDepositAmount = calculatedDepositAmount;
1799
- this.logInfo("使用从支付项计算的定金金额", {
1800
- manualDepositAmount,
1801
- calculatedDepositAmount,
1802
- reason: "未手动设置定金金额,从定金类型支付项计算得出"
1803
- });
1804
2020
  } else {
1805
2021
  finalDepositAmount = "0.00";
1806
2022
  this.logInfo("定金金额为0", {
1807
2023
  manualDepositAmount,
1808
- calculatedDepositAmount,
1809
2024
  reason: "手动设置和计算值均为0"
1810
2025
  });
1811
2026
  }
1812
2027
  this.logInfo("定金金额确定结果", {
1813
- depositPaymentItemsCount: depositPaymentItems.length,
1814
- depositPaymentItems: depositPaymentItems.map((item) => ({
2028
+ depositPaymentItemsCount: processedPaymentItems.length,
2029
+ depositPaymentItems: processedPaymentItems.map((item) => ({
1815
2030
  uuid: item.uuid,
1816
2031
  code: item.code,
1817
2032
  amount: item.amount,
@@ -1820,7 +2035,6 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1820
2035
  status: item.status
1821
2036
  })),
1822
2037
  manualDepositAmount,
1823
- calculatedDepositAmount,
1824
2038
  finalDepositAmount,
1825
2039
  isDeposit: ((_b = this.store.currentOrder) == null ? void 0 : _b.is_deposit) || 0
1826
2040
  });
@@ -1843,11 +2057,30 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1843
2057
  currency_code: this.otherParams.currency_code,
1844
2058
  currency_symbol: this.otherParams.currency_symbol,
1845
2059
  currency_format: this.otherParams.currency_format,
1846
- is_deposit: ((_d = this.store.currentOrder) == null ? void 0 : _d.is_deposit) || 0,
2060
+ is_deposit: (((_d = this.store.currentOrder) == null ? void 0 : _d.is_deposit) || Number(finalDepositAmount) > 0 ? 1 : 0) || 0,
1847
2061
  deposit_amount: finalDepositAmount,
1848
2062
  // 使用最终确定的定金金额(手动设置优先)
1849
2063
  product_tax_fee: this.store.localOrderData.tax_fee,
1850
- note: this.store.localOrderData.shop_note
2064
+ note: this.store.localOrderData.shop_note,
2065
+ bookings: (_e = this.store.localOrderData.bookings) == null ? void 0 : _e.map((item) => {
2066
+ return {
2067
+ ...item,
2068
+ product: {
2069
+ ...item.product,
2070
+ custom_deposit_data: void 0
2071
+ },
2072
+ deposit: void 0
2073
+ };
2074
+ }),
2075
+ relation_products: (_f = this.store.localOrderData.relation_products) == null ? void 0 : _f.map(
2076
+ (item) => {
2077
+ return {
2078
+ ...item,
2079
+ custom_deposit_data: void 0,
2080
+ deposit: void 0
2081
+ };
2082
+ }
2083
+ )
1851
2084
  };
1852
2085
  if (isUpdateOperation) {
1853
2086
  if ((0, import_utils.isVirtualOrderId)(currentOrderId)) {
@@ -1898,7 +2131,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1898
2131
  orderUuid: this.store.currentOrder.uuid,
1899
2132
  operation: isUpdateOperation ? "update" : "create",
1900
2133
  isManual,
1901
- orderId: submitSuccess ? ((_e = checkoutResponse == null ? void 0 : checkoutResponse.data) == null ? void 0 : _e.order_id) || (checkoutResponse == null ? void 0 : checkoutResponse.order_id) : void 0,
2134
+ orderId: submitSuccess ? ((_g = checkoutResponse == null ? void 0 : checkoutResponse.data) == null ? void 0 : _g.order_id) || (checkoutResponse == null ? void 0 : checkoutResponse.order_id) : void 0,
1902
2135
  error: submitError,
1903
2136
  duration: Date.now() - startTime,
1904
2137
  timestamp: Date.now()
@@ -1924,7 +2157,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1924
2157
  if (isUpdateOperation) {
1925
2158
  realOrderId = currentOrderId;
1926
2159
  } else {
1927
- let extractedOrderId = (_f = checkoutResponse == null ? void 0 : checkoutResponse.data) == null ? void 0 : _f.order_id;
2160
+ let extractedOrderId = (_h = checkoutResponse == null ? void 0 : checkoutResponse.data) == null ? void 0 : _h.order_id;
1928
2161
  if (!extractedOrderId) {
1929
2162
  extractedOrderId = checkoutResponse == null ? void 0 : checkoutResponse.order_id;
1930
2163
  }
@@ -1979,6 +2212,18 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1979
2212
  this.logInfo("错误恢复:手动设置订单ID:", realOrderId);
1980
2213
  }
1981
2214
  }
2215
+ try {
2216
+ const syncedPaymentUuids = processedPaymentItems.filter((item) => item.status !== "voided").map((item) => item.uuid).filter(Boolean);
2217
+ for (const paymentUuid of syncedPaymentUuids) {
2218
+ await this.payment.updatePaymentAsync(
2219
+ this.store.currentOrder.uuid,
2220
+ paymentUuid,
2221
+ { isSynced: true, syncError: void 0 }
2222
+ );
2223
+ }
2224
+ } catch (e) {
2225
+ this.logWarning("标记支付项已同步失败(不阻塞主流程)", { error: e });
2226
+ }
1982
2227
  this.store.isOrderSynced = true;
1983
2228
  await this.core.effects.emit(import_types.CheckoutHooks.OnOrderSynced, {
1984
2229
  orderUuid: this.store.currentOrder.uuid,
@@ -91,6 +91,19 @@ export interface CreateLocalOrderParams {
91
91
  autoPayment?: boolean;
92
92
  totalInfo: any;
93
93
  }
94
+ /**
95
+ * 本地订单更新参数
96
+ */
97
+ export interface UpdateLocalOrderParams {
98
+ /** 要更新的后端订单ID(已同步过后端的订单) */
99
+ orderId: string;
100
+ /** 更新后的订单数据(结构与创建时一致,允许部分字段变化) */
101
+ orderData: LocalOrderData;
102
+ /** 更新后的购物车小计数据 */
103
+ cartSummary: CartSummaryItem[];
104
+ /** total 信息(用于拆解金额) */
105
+ totalInfo: any;
106
+ }
94
107
  /**
95
108
  * 本地订单数据结构 (基于 appointmentDemo.json)
96
109
  */
@@ -373,6 +386,10 @@ export interface CheckoutModuleAPI extends Module {
373
386
  * 创建本地订单 (前端模拟下单流程)
374
387
  */
375
388
  createLocalOrderAsync(params: CreateLocalOrderParams): Promise<PaymentOrder>;
389
+ /**
390
+ * 更新本地订单(基于已同步后的真实 orderId)并设置为当前订单
391
+ */
392
+ updateLocalOrderAsync(params: UpdateLocalOrderParams): Promise<PaymentOrder>;
376
393
  /**
377
394
  * 完成结账
378
395
  */
@@ -42,6 +42,11 @@ export declare class ShopDiscountImpl extends BaseModule implements Module {
42
42
  private getDiscountList;
43
43
  private getCustomerWallet;
44
44
  private bestDiscount;
45
- private loadPrepareConfig;
45
+ loadPrepareConfig(params: {
46
+ customerId: number;
47
+ action?: 'create' | 'update';
48
+ with_good_pass?: 0 | 1;
49
+ with_discount_card?: 0 | 1;
50
+ }): Promise<void>;
46
51
  }
47
52
  export default ShopDiscountImpl;
@@ -324,6 +324,7 @@ var ShopDiscountImpl = class extends import_BaseModule.BaseModule {
324
324
  }
325
325
  editModeDiscountList.push({
326
326
  ...discount,
327
+ name: discount.name || discount.discount.title.auto,
327
328
  isEditMode: true,
328
329
  limited_relation_product_data: {},
329
330
  savedAmount: discount.amount * ((product == null ? void 0 : product.num) || 1),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@pisell/pisellos",
4
- "version": "2.1.24",
4
+ "version": "2.1.26",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",