@pisell/pisellos 2.2.195 → 2.2.196

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.
@@ -150,6 +150,8 @@ export declare class OrderModule extends BaseModule implements Module, OrderModu
150
150
  private formatSummaryMetadataMoney;
151
151
  private syncSummaryProductMetadata;
152
152
  private calculateSummaryAmountGap;
153
+ private getSummaryRefundAmount;
154
+ private calculatePaymentTargetAmount;
153
155
  private getPaymentTargetAmount;
154
156
  ensureTempOrder(): OrderTempOrder;
155
157
  restoreOrder(): OrderTempOrder;
@@ -993,27 +993,48 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
993
993
  }
994
994
  }, {
995
995
  key: "updateTempOrderPaymentStatus",
996
- value: function updateTempOrderPaymentStatus(tempOrder) {
997
- if (this.shouldKeepPaymentStatusAfterAmountRecalc(tempOrder.payment_status)) {
996
+ value: function updateTempOrderPaymentStatus(tempOrder, options) {
997
+ var summary = (options === null || options === void 0 ? void 0 : options.summary) || this.store.summary || createEmptySummary();
998
+ var paymentTotal = this.calculatePaymentTotal(tempOrder.payments || []);
999
+ var refundAmount = this.getSummaryRefundAmount(summary);
1000
+ var netPaymentTotal = Decimal.max(paymentTotal.minus(refundAmount), 0);
1001
+ var targetAmount = this.calculatePaymentTargetAmount(tempOrder, summary);
1002
+ if (this.shouldKeepPaymentStatusAfterAmountRecalc(tempOrder.payment_status, {
1003
+ netPaymentTotal: netPaymentTotal,
1004
+ paymentTotal: paymentTotal,
1005
+ refundAmount: refundAmount,
1006
+ targetAmount: targetAmount,
1007
+ previousSummary: options === null || options === void 0 ? void 0 : options.previousSummary,
1008
+ tempOrder: tempOrder
1009
+ })) {
998
1010
  return;
999
1011
  }
1000
- var paymentTotal = this.calculatePaymentTotal(tempOrder.payments || []);
1001
- var targetAmount = this.getPaymentTargetAmount();
1002
1012
  if (targetAmount.lte(0)) {
1003
1013
  tempOrder.payment_status = 'paid';
1004
1014
  return;
1005
1015
  }
1006
- if (paymentTotal.lte(0)) {
1016
+ if (netPaymentTotal.lte(0)) {
1007
1017
  tempOrder.payment_status = 'payment_processing';
1008
1018
  return;
1009
1019
  }
1010
- tempOrder.payment_status = paymentTotal.gte(targetAmount) ? 'paid' : 'partially_paid';
1020
+ tempOrder.payment_status = netPaymentTotal.gte(targetAmount) ? 'paid' : 'partially_paid';
1011
1021
  }
1012
1022
  }, {
1013
1023
  key: "shouldKeepPaymentStatusAfterAmountRecalc",
1014
- value: function shouldKeepPaymentStatusAfterAmountRecalc(paymentStatus) {
1024
+ value: function shouldKeepPaymentStatusAfterAmountRecalc(paymentStatus, params) {
1015
1025
  var normalizedStatus = String(paymentStatus || '').trim().toLowerCase();
1016
- return normalizedStatus === 'refunded' || normalizedStatus === 'partially_refunded';
1026
+ if (normalizedStatus !== 'refunded' && normalizedStatus !== 'partially_refunded') {
1027
+ return false;
1028
+ }
1029
+ if (params.refundAmount.lte(0)) return false;
1030
+ if (params.previousSummary) {
1031
+ var previousTargetAmount = this.calculatePaymentTargetAmount(params.tempOrder, params.previousSummary);
1032
+ if (!previousTargetAmount.eq(params.targetAmount)) return false;
1033
+ }
1034
+ if (normalizedStatus === 'refunded') {
1035
+ return params.refundAmount.gte(params.paymentTotal);
1036
+ }
1037
+ return params.netPaymentTotal.gte(params.targetAmount);
1017
1038
  }
1018
1039
  }, {
1019
1040
  key: "resolveWalletPassUseValue",
@@ -1174,20 +1195,29 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1174
1195
  var tempOrder = params.tempOrder,
1175
1196
  summary = params.summary,
1176
1197
  orderPaidAmount = params.orderPaidAmount;
1198
+ var refundAmount = this.getSummaryRefundAmount(summary);
1199
+ var netOrderPaidAmount = Decimal.max(orderPaidAmount.minus(refundAmount), 0);
1200
+ var targetAmount = this.calculatePaymentTargetAmount(tempOrder, summary);
1201
+ return targetAmount.minus(netOrderPaidAmount).toFixed(2);
1202
+ }
1203
+ }, {
1204
+ key: "getSummaryRefundAmount",
1205
+ value: function getSummaryRefundAmount(summary) {
1206
+ return Decimal.max(new Decimal((summary === null || summary === void 0 ? void 0 : summary.total_refund_amount) || 0), 0);
1207
+ }
1208
+ }, {
1209
+ key: "calculatePaymentTargetAmount",
1210
+ value: function calculatePaymentTargetAmount(tempOrder, summary) {
1177
1211
  var depositAmount = summary.deposit_amount || tempOrder.deposit_amount || '0.00';
1178
1212
  var isDeposit = tempOrder.is_deposit === 1 || new Decimal(depositAmount || 0).gt(0);
1179
- var targetAmount = isDeposit ? new Decimal(depositAmount || 0) : new Decimal(summary.total_amount || summary.expect_amount || 0);
1180
- return targetAmount.minus(orderPaidAmount).toFixed(2);
1213
+ return isDeposit ? new Decimal(depositAmount || 0) : new Decimal(summary.total_amount || summary.expect_amount || 0);
1181
1214
  }
1182
1215
  }, {
1183
1216
  key: "getPaymentTargetAmount",
1184
1217
  value: function getPaymentTargetAmount() {
1185
1218
  var tempOrder = this.ensureTempOrder();
1186
1219
  var summary = this.store.summary || createEmptySummary();
1187
- if (tempOrder.is_deposit === 1) {
1188
- return new Decimal(tempOrder.deposit_amount || summary.deposit_amount || 0);
1189
- }
1190
- return new Decimal(summary.total_amount || summary.expect_amount || 0);
1220
+ return this.calculatePaymentTargetAmount(tempOrder, summary);
1191
1221
  }
1192
1222
  }, {
1193
1223
  key: "ensureTempOrder",
@@ -1311,9 +1341,12 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1311
1341
  if (!tempOrder) return null;
1312
1342
  var summary = this.store.summary || createEmptySummary();
1313
1343
  var depositAmount = tempOrder.deposit_amount || summary.deposit_amount || '0.00';
1314
- var targetAmount = tempOrder.is_deposit === 1 ? new Decimal(depositAmount || 0) : new Decimal(summary.total_amount || summary.expect_amount || 0);
1315
- var paymentTotal = this.calculatePaymentTotal(tempOrder.payments || []);
1316
- var amountGap = targetAmount.minus(paymentTotal).toFixed(2);
1344
+ var paidPaymentSummary = this.calculatePaidPaymentSummary(tempOrder.payments || []);
1345
+ var amountGap = this.calculateSummaryAmountGap({
1346
+ tempOrder: tempOrder,
1347
+ summary: summary,
1348
+ orderPaidAmount: paidPaymentSummary.gapPaidAmount
1349
+ });
1317
1350
  return {
1318
1351
  isDeposit: tempOrder.is_deposit === 1 || depositAmount !== '0.00',
1319
1352
  depositAmount: depositAmount,
@@ -1515,7 +1548,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1515
1548
  key: "recalculateSummary",
1516
1549
  value: function () {
1517
1550
  var _recalculateSummary = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9(options) {
1518
- var tempOrder, salesSummary, existedSummary, paidPaymentSummary, emptySummary, salesSummaryResult, summary;
1551
+ var tempOrder, salesSummary, existedSummary, totalRefundAmount, paidPaymentSummary, emptyBaseSummary, emptySummary, salesSummaryResult, summaryForGap, summary;
1519
1552
  return _regeneratorRuntime().wrap(function _callee9$(_context11) {
1520
1553
  while (1) switch (_context11.prev = _context11.next) {
1521
1554
  case 0:
@@ -1529,43 +1562,52 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1529
1562
  this.sanitizeTempOrderProducts(tempOrder);
1530
1563
  salesSummary = this.getSalesSummary();
1531
1564
  existedSummary = this.store.summary || createEmptySummary();
1565
+ totalRefundAmount = existedSummary.total_refund_amount || '0.00';
1532
1566
  paidPaymentSummary = this.calculatePaidPaymentSummary(tempOrder.payments || []);
1533
1567
  if (salesSummary) {
1534
- _context11.next = 13;
1568
+ _context11.next = 15;
1535
1569
  break;
1536
1570
  }
1537
- emptySummary = _objectSpread(_objectSpread({}, createEmptySummary()), {}, {
1538
- total_refund_amount: existedSummary.total_refund_amount || '0.00',
1571
+ emptyBaseSummary = _objectSpread(_objectSpread({}, createEmptySummary()), {}, {
1572
+ total_refund_amount: totalRefundAmount
1573
+ });
1574
+ emptySummary = _objectSpread(_objectSpread({}, emptyBaseSummary), {}, {
1539
1575
  customer_paid_amount: paidPaymentSummary.customerPaidAmount.toFixed(2),
1540
1576
  order_paid_amount: paidPaymentSummary.orderPaidAmount.toFixed(2),
1541
1577
  amount_gap: this.calculateSummaryAmountGap({
1542
1578
  tempOrder: tempOrder,
1543
- summary: createEmptySummary(),
1579
+ summary: emptyBaseSummary,
1544
1580
  orderPaidAmount: paidPaymentSummary.gapPaidAmount
1545
1581
  }),
1546
1582
  pay_service_charge_amount: paidPaymentSummary.payServiceChargeAmount.toFixed(2)
1547
1583
  });
1548
1584
  tempOrder.surcharge_fee = emptySummary.surcharge_fee;
1549
- this.updateTempOrderPaymentStatus(tempOrder);
1550
1585
  this.store.summary = emptySummary;
1586
+ this.updateTempOrderPaymentStatus(tempOrder, {
1587
+ previousSummary: existedSummary,
1588
+ summary: emptySummary
1589
+ });
1551
1590
  return _context11.abrupt("return", this.store.summary);
1552
- case 13:
1553
- _context11.next = 15;
1591
+ case 15:
1592
+ _context11.next = 17;
1554
1593
  return salesSummary.getSummary({
1555
1594
  products: tempOrder.products,
1556
1595
  isPriceIncludeTax: tempOrder.is_price_include_tax,
1557
1596
  shopDiscount: tempOrder.shop_discount
1558
1597
  });
1559
- case 15:
1598
+ case 17:
1560
1599
  salesSummaryResult = _context11.sent;
1561
1600
  this.syncSummaryProductMetadata(tempOrder.products || []);
1601
+ summaryForGap = _objectSpread(_objectSpread({}, salesSummaryResult), {}, {
1602
+ total_refund_amount: totalRefundAmount
1603
+ });
1562
1604
  summary = _objectSpread(_objectSpread({}, salesSummaryResult), {}, {
1563
- total_refund_amount: existedSummary.total_refund_amount || '0.00',
1605
+ total_refund_amount: totalRefundAmount,
1564
1606
  customer_paid_amount: paidPaymentSummary.customerPaidAmount.toFixed(2),
1565
1607
  order_paid_amount: paidPaymentSummary.orderPaidAmount.toFixed(2),
1566
1608
  amount_gap: this.calculateSummaryAmountGap({
1567
1609
  tempOrder: tempOrder,
1568
- summary: salesSummaryResult,
1610
+ summary: summaryForGap,
1569
1611
  orderPaidAmount: paidPaymentSummary.gapPaidAmount
1570
1612
  }),
1571
1613
  pay_service_charge_amount: paidPaymentSummary.payServiceChargeAmount.toFixed(2)
@@ -1581,9 +1623,12 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1581
1623
  tempOrder.surcharges = summary.surcharges || [];
1582
1624
  tempOrder.deposit_amount = summary.deposit_amount || '0.00';
1583
1625
  tempOrder.is_deposit = summary.deposit_amount !== '0.00' ? 1 : 0;
1584
- this.updateTempOrderPaymentStatus(tempOrder);
1626
+ this.updateTempOrderPaymentStatus(tempOrder, {
1627
+ previousSummary: existedSummary,
1628
+ summary: summary
1629
+ });
1585
1630
  return _context11.abrupt("return", this.store.summary);
1586
- case 27:
1631
+ case 30:
1587
1632
  case "end":
1588
1633
  return _context11.stop();
1589
1634
  }
@@ -150,6 +150,8 @@ export declare class OrderModule extends BaseModule implements Module, OrderModu
150
150
  private formatSummaryMetadataMoney;
151
151
  private syncSummaryProductMetadata;
152
152
  private calculateSummaryAmountGap;
153
+ private getSummaryRefundAmount;
154
+ private calculatePaymentTargetAmount;
153
155
  private getPaymentTargetAmount;
154
156
  ensureTempOrder(): OrderTempOrder;
155
157
  restoreOrder(): OrderTempOrder;
@@ -644,25 +644,51 @@ var OrderModule = class extends import_BaseModule.BaseModule {
644
644
  }
645
645
  return normalized;
646
646
  }
647
- updateTempOrderPaymentStatus(tempOrder) {
648
- if (this.shouldKeepPaymentStatusAfterAmountRecalc(tempOrder.payment_status)) {
647
+ updateTempOrderPaymentStatus(tempOrder, options) {
648
+ const summary = (options == null ? void 0 : options.summary) || this.store.summary || (0, import_utils.createEmptySummary)();
649
+ const paymentTotal = this.calculatePaymentTotal(tempOrder.payments || []);
650
+ const refundAmount = this.getSummaryRefundAmount(summary);
651
+ const netPaymentTotal = import_decimal.default.max(paymentTotal.minus(refundAmount), 0);
652
+ const targetAmount = this.calculatePaymentTargetAmount(tempOrder, summary);
653
+ if (this.shouldKeepPaymentStatusAfterAmountRecalc(tempOrder.payment_status, {
654
+ netPaymentTotal,
655
+ paymentTotal,
656
+ refundAmount,
657
+ targetAmount,
658
+ previousSummary: options == null ? void 0 : options.previousSummary,
659
+ tempOrder
660
+ })) {
649
661
  return;
650
662
  }
651
- const paymentTotal = this.calculatePaymentTotal(tempOrder.payments || []);
652
- const targetAmount = this.getPaymentTargetAmount();
653
663
  if (targetAmount.lte(0)) {
654
664
  tempOrder.payment_status = "paid";
655
665
  return;
656
666
  }
657
- if (paymentTotal.lte(0)) {
667
+ if (netPaymentTotal.lte(0)) {
658
668
  tempOrder.payment_status = "payment_processing";
659
669
  return;
660
670
  }
661
- tempOrder.payment_status = paymentTotal.gte(targetAmount) ? "paid" : "partially_paid";
671
+ tempOrder.payment_status = netPaymentTotal.gte(targetAmount) ? "paid" : "partially_paid";
662
672
  }
663
- shouldKeepPaymentStatusAfterAmountRecalc(paymentStatus) {
673
+ shouldKeepPaymentStatusAfterAmountRecalc(paymentStatus, params) {
664
674
  const normalizedStatus = String(paymentStatus || "").trim().toLowerCase();
665
- return normalizedStatus === "refunded" || normalizedStatus === "partially_refunded";
675
+ if (normalizedStatus !== "refunded" && normalizedStatus !== "partially_refunded") {
676
+ return false;
677
+ }
678
+ if (params.refundAmount.lte(0))
679
+ return false;
680
+ if (params.previousSummary) {
681
+ const previousTargetAmount = this.calculatePaymentTargetAmount(
682
+ params.tempOrder,
683
+ params.previousSummary
684
+ );
685
+ if (!previousTargetAmount.eq(params.targetAmount))
686
+ return false;
687
+ }
688
+ if (normalizedStatus === "refunded") {
689
+ return params.refundAmount.gte(params.paymentTotal);
690
+ }
691
+ return params.netPaymentTotal.gte(params.targetAmount);
666
692
  }
667
693
  resolveWalletPassUseValue(payment, amount) {
668
694
  const usageUnit = payment.wallet_pass_usage_unit && typeof payment.wallet_pass_usage_unit === "object" ? payment.wallet_pass_usage_unit : null;
@@ -797,20 +823,23 @@ var OrderModule = class extends import_BaseModule.BaseModule {
797
823
  }
798
824
  calculateSummaryAmountGap(params) {
799
825
  const { tempOrder, summary, orderPaidAmount } = params;
826
+ const refundAmount = this.getSummaryRefundAmount(summary);
827
+ const netOrderPaidAmount = import_decimal.default.max(orderPaidAmount.minus(refundAmount), 0);
828
+ const targetAmount = this.calculatePaymentTargetAmount(tempOrder, summary);
829
+ return targetAmount.minus(netOrderPaidAmount).toFixed(2);
830
+ }
831
+ getSummaryRefundAmount(summary) {
832
+ return import_decimal.default.max(new import_decimal.default((summary == null ? void 0 : summary.total_refund_amount) || 0), 0);
833
+ }
834
+ calculatePaymentTargetAmount(tempOrder, summary) {
800
835
  const depositAmount = summary.deposit_amount || tempOrder.deposit_amount || "0.00";
801
836
  const isDeposit = tempOrder.is_deposit === 1 || new import_decimal.default(depositAmount || 0).gt(0);
802
- const targetAmount = isDeposit ? new import_decimal.default(depositAmount || 0) : new import_decimal.default(summary.total_amount || summary.expect_amount || 0);
803
- return targetAmount.minus(orderPaidAmount).toFixed(2);
837
+ return isDeposit ? new import_decimal.default(depositAmount || 0) : new import_decimal.default(summary.total_amount || summary.expect_amount || 0);
804
838
  }
805
839
  getPaymentTargetAmount() {
806
840
  const tempOrder = this.ensureTempOrder();
807
841
  const summary = this.store.summary || (0, import_utils.createEmptySummary)();
808
- if (tempOrder.is_deposit === 1) {
809
- return new import_decimal.default(
810
- tempOrder.deposit_amount || summary.deposit_amount || 0
811
- );
812
- }
813
- return new import_decimal.default(summary.total_amount || summary.expect_amount || 0);
842
+ return this.calculatePaymentTargetAmount(tempOrder, summary);
814
843
  }
815
844
  ensureTempOrder() {
816
845
  if (this.store.tempOrder) {
@@ -896,9 +925,12 @@ var OrderModule = class extends import_BaseModule.BaseModule {
896
925
  return null;
897
926
  const summary = this.store.summary || (0, import_utils.createEmptySummary)();
898
927
  const depositAmount = tempOrder.deposit_amount || summary.deposit_amount || "0.00";
899
- const targetAmount = tempOrder.is_deposit === 1 ? new import_decimal.default(depositAmount || 0) : new import_decimal.default(summary.total_amount || summary.expect_amount || 0);
900
- const paymentTotal = this.calculatePaymentTotal(tempOrder.payments || []);
901
- const amountGap = targetAmount.minus(paymentTotal).toFixed(2);
928
+ const paidPaymentSummary = this.calculatePaidPaymentSummary(tempOrder.payments || []);
929
+ const amountGap = this.calculateSummaryAmountGap({
930
+ tempOrder,
931
+ summary,
932
+ orderPaidAmount: paidPaymentSummary.gapPaidAmount
933
+ });
902
934
  return {
903
935
  isDeposit: tempOrder.is_deposit === 1 || depositAmount !== "0.00",
904
936
  depositAmount,
@@ -1062,25 +1094,32 @@ var OrderModule = class extends import_BaseModule.BaseModule {
1062
1094
  this.sanitizeTempOrderProducts(tempOrder);
1063
1095
  const salesSummary = this.getSalesSummary();
1064
1096
  const existedSummary = this.store.summary || (0, import_utils.createEmptySummary)();
1097
+ const totalRefundAmount = existedSummary.total_refund_amount || "0.00";
1065
1098
  const paidPaymentSummary = this.calculatePaidPaymentSummary(
1066
1099
  tempOrder.payments || []
1067
1100
  );
1068
1101
  if (!salesSummary) {
1069
- const emptySummary = {
1102
+ const emptyBaseSummary = {
1070
1103
  ...(0, import_utils.createEmptySummary)(),
1071
- total_refund_amount: existedSummary.total_refund_amount || "0.00",
1104
+ total_refund_amount: totalRefundAmount
1105
+ };
1106
+ const emptySummary = {
1107
+ ...emptyBaseSummary,
1072
1108
  customer_paid_amount: paidPaymentSummary.customerPaidAmount.toFixed(2),
1073
1109
  order_paid_amount: paidPaymentSummary.orderPaidAmount.toFixed(2),
1074
1110
  amount_gap: this.calculateSummaryAmountGap({
1075
1111
  tempOrder,
1076
- summary: (0, import_utils.createEmptySummary)(),
1112
+ summary: emptyBaseSummary,
1077
1113
  orderPaidAmount: paidPaymentSummary.gapPaidAmount
1078
1114
  }),
1079
1115
  pay_service_charge_amount: paidPaymentSummary.payServiceChargeAmount.toFixed(2)
1080
1116
  };
1081
1117
  tempOrder.surcharge_fee = emptySummary.surcharge_fee;
1082
- this.updateTempOrderPaymentStatus(tempOrder);
1083
1118
  this.store.summary = emptySummary;
1119
+ this.updateTempOrderPaymentStatus(tempOrder, {
1120
+ previousSummary: existedSummary,
1121
+ summary: emptySummary
1122
+ });
1084
1123
  return this.store.summary;
1085
1124
  }
1086
1125
  const salesSummaryResult = await salesSummary.getSummary({
@@ -1089,14 +1128,18 @@ var OrderModule = class extends import_BaseModule.BaseModule {
1089
1128
  shopDiscount: tempOrder.shop_discount
1090
1129
  });
1091
1130
  this.syncSummaryProductMetadata(tempOrder.products || []);
1131
+ const summaryForGap = {
1132
+ ...salesSummaryResult,
1133
+ total_refund_amount: totalRefundAmount
1134
+ };
1092
1135
  const summary = {
1093
1136
  ...salesSummaryResult,
1094
- total_refund_amount: existedSummary.total_refund_amount || "0.00",
1137
+ total_refund_amount: totalRefundAmount,
1095
1138
  customer_paid_amount: paidPaymentSummary.customerPaidAmount.toFixed(2),
1096
1139
  order_paid_amount: paidPaymentSummary.orderPaidAmount.toFixed(2),
1097
1140
  amount_gap: this.calculateSummaryAmountGap({
1098
1141
  tempOrder,
1099
- summary: salesSummaryResult,
1142
+ summary: summaryForGap,
1100
1143
  orderPaidAmount: paidPaymentSummary.gapPaidAmount
1101
1144
  }),
1102
1145
  pay_service_charge_amount: paidPaymentSummary.payServiceChargeAmount.toFixed(2)
@@ -1112,7 +1155,10 @@ var OrderModule = class extends import_BaseModule.BaseModule {
1112
1155
  tempOrder.surcharges = summary.surcharges || [];
1113
1156
  tempOrder.deposit_amount = summary.deposit_amount || "0.00";
1114
1157
  tempOrder.is_deposit = summary.deposit_amount !== "0.00" ? 1 : 0;
1115
- this.updateTempOrderPaymentStatus(tempOrder);
1158
+ this.updateTempOrderPaymentStatus(tempOrder, {
1159
+ previousSummary: existedSummary,
1160
+ summary
1161
+ });
1116
1162
  return this.store.summary;
1117
1163
  }
1118
1164
  async getOrderSummary() {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@pisell/pisellos",
4
- "version": "2.2.195",
4
+ "version": "2.2.196",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",