@pisell/pisellos 2.2.197 → 2.2.199

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 (45) hide show
  1. package/dist/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.js +7 -3
  2. package/dist/modules/Cart/utils/cartProduct.js +11 -1
  3. package/dist/modules/Cart/utils/changePrice.js +11 -11
  4. package/dist/modules/Order/index.d.ts +16 -0
  5. package/dist/modules/Order/index.js +1232 -568
  6. package/dist/modules/Order/types.d.ts +1 -0
  7. package/dist/modules/Order/utils.d.ts +33 -2
  8. package/dist/modules/Order/utils.js +112 -55
  9. package/dist/modules/Payment/walletpass.js +14 -7
  10. package/dist/modules/SalesSummary/utils.js +394 -149
  11. package/dist/server/index.d.ts +3 -0
  12. package/dist/server/index.js +294 -190
  13. package/dist/server/modules/order/index.d.ts +8 -0
  14. package/dist/server/modules/order/index.js +536 -317
  15. package/dist/server/modules/order/types.d.ts +2 -0
  16. package/dist/solution/BaseSales/index.js +2 -1
  17. package/dist/solution/BaseSales/types.d.ts +1 -0
  18. package/dist/solution/BaseSales/utils/transformBaseProductToOrderProduct.js +19 -4
  19. package/dist/solution/BookingByStep/index.d.ts +1 -1
  20. package/dist/solution/ScanOrder/types.d.ts +1 -0
  21. package/dist/solution/ScanOrder/utils.js +26 -9
  22. package/dist/solution/VenueBooking/index.js +2 -1
  23. package/lib/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.js +4 -1
  24. package/lib/modules/Cart/utils/cartProduct.js +11 -1
  25. package/lib/modules/Cart/utils/changePrice.js +12 -20
  26. package/lib/modules/Order/index.d.ts +16 -0
  27. package/lib/modules/Order/index.js +525 -18
  28. package/lib/modules/Order/types.d.ts +1 -0
  29. package/lib/modules/Order/utils.d.ts +33 -2
  30. package/lib/modules/Order/utils.js +44 -9
  31. package/lib/modules/Payment/walletpass.js +7 -3
  32. package/lib/modules/SalesSummary/utils.js +256 -46
  33. package/lib/server/index.d.ts +3 -0
  34. package/lib/server/index.js +82 -3
  35. package/lib/server/modules/order/index.d.ts +8 -0
  36. package/lib/server/modules/order/index.js +106 -4
  37. package/lib/server/modules/order/types.d.ts +2 -0
  38. package/lib/solution/BaseSales/index.js +2 -1
  39. package/lib/solution/BaseSales/types.d.ts +1 -0
  40. package/lib/solution/BaseSales/utils/transformBaseProductToOrderProduct.js +15 -1
  41. package/lib/solution/BookingByStep/index.d.ts +1 -1
  42. package/lib/solution/ScanOrder/types.d.ts +1 -0
  43. package/lib/solution/ScanOrder/utils.js +28 -10
  44. package/lib/solution/VenueBooking/index.js +2 -1
  45. package/package.json +1 -1
@@ -1,19 +1,23 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
3
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
5
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
6
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
1
7
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
2
8
  function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
9
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
3
10
  function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
4
11
  function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
5
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
12
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
6
13
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
7
14
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
8
15
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
9
16
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
10
17
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
11
- function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
12
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
13
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
14
18
  import Decimal from 'decimal.js';
15
19
  import { getSurcharge, getSurchargeAmount } from "../Summary/utils";
16
- import { getProductSkuOptions } from "../Order/utils";
20
+ import { getBundleSellingMagnitude, getBundleSignedUnit, getProductSkuOptions, isMarkdownBundle } from "../Order/utils";
17
21
  function toDecimal(value) {
18
22
  return new Decimal(value || 0);
19
23
  }
@@ -53,10 +57,12 @@ function getBundleUnitPrice(product) {
53
57
  var useOriginal = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
54
58
  var bundleItems = product.product_bundle || [];
55
59
  return bundleItems.reduce(function (sum, item) {
56
- var _ref, _item$original_price, _item$bundle_selling_;
57
60
  var quantity = getSafeNum(item === null || item === void 0 ? void 0 : item.num);
58
- var price = useOriginal ? toDecimal((_ref = (_item$original_price = item === null || item === void 0 ? void 0 : item.original_price) !== null && _item$original_price !== void 0 ? _item$original_price : item === null || item === void 0 ? void 0 : item.product_price) !== null && _ref !== void 0 ? _ref : item === null || item === void 0 ? void 0 : item.price) : toDecimal((_item$bundle_selling_ = item === null || item === void 0 ? void 0 : item.bundle_selling_price) !== null && _item$bundle_selling_ !== void 0 ? _item$bundle_selling_ : item === null || item === void 0 ? void 0 : item.price);
59
- return sum.plus(price.times(quantity));
61
+ // markdown(减价)按带符号净额取减;markup/原价维持相加。
62
+ var signedUnit = getBundleSignedUnit(item, {
63
+ useOriginal: useOriginal
64
+ });
65
+ return sum.plus(signedUnit.times(quantity));
60
66
  }, new Decimal(0));
61
67
  }
62
68
 
@@ -89,6 +95,7 @@ function buildSurchargeServiceItems(products) {
89
95
  var quantity = getSafeNum(product.num);
90
96
  var unitTotal = getUnitPaymentTotal(product);
91
97
  var options = getProductSkuOptions(product);
98
+ var persistedSurchargeFee = getPersistedMainSurchargeFee(product);
92
99
  return {
93
100
  id: product.product_id,
94
101
  num: quantity,
@@ -109,19 +116,20 @@ function buildSurchargeServiceItems(products) {
109
116
  option: options,
110
117
  options: options,
111
118
  relation_details: [],
112
- metadata: product.metadata || {},
119
+ metadata: _objectSpread(_objectSpread({}, product.metadata || {}), {}, {
120
+ __persisted_surcharge_fee: persistedSurchargeFee === null || persistedSurchargeFee === void 0 ? void 0 : persistedSurchargeFee.toFixed(2)
121
+ }),
113
122
  start_date: ((_product$metadata5 = product.metadata) === null || _product$metadata5 === void 0 ? void 0 : _product$metadata5.start_date) || '',
114
123
  start_time: ((_product$metadata6 = product.metadata) === null || _product$metadata6 === void 0 ? void 0 : _product$metadata6.start_time) || ''
115
124
  };
116
125
  });
117
126
  }
118
127
  function getRelationSurchargeIds(item) {
119
- var _item$relation_surcha, _item$metadata;
120
- var ids = (_item$relation_surcha = item === null || item === void 0 ? void 0 : item.relation_surcharge_ids) !== null && _item$relation_surcha !== void 0 ? _item$relation_surcha : item === null || item === void 0 || (_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.relation_surcharge_ids;
121
- if (!Array.isArray(ids)) return [];
122
- return ids.filter(function (id) {
128
+ var _item$metadata;
129
+ var ids = [].concat(_toConsumableArray(Array.isArray(item === null || item === void 0 ? void 0 : item.relation_surcharge_ids) ? item.relation_surcharge_ids : []), _toConsumableArray(Array.isArray(item === null || item === void 0 || (_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.relation_surcharge_ids) ? item.metadata.relation_surcharge_ids : []));
130
+ return _toConsumableArray(new Set(ids.filter(function (id) {
123
131
  return id !== undefined && id !== null && id !== '';
124
- });
132
+ })));
125
133
  }
126
134
  function getSurchargeConfigId(config) {
127
135
  var _config$id;
@@ -145,31 +153,67 @@ function getSurchargeConfigById(surchargeList) {
145
153
  return map;
146
154
  }
147
155
  function getMainSurchargeUnitAmount(product) {
148
- var _ref2, _ref3, _record$metadata$main, _record$metadata, _record$metadata2;
156
+ var _ref, _ref2, _record$metadata$main, _record$metadata, _record$metadata2;
149
157
  var record = product;
150
- return toDecimal((_ref2 = (_ref3 = (_record$metadata$main = (_record$metadata = record.metadata) === null || _record$metadata === void 0 ? void 0 : _record$metadata.main_product_attached_bundle_selling_price) !== null && _record$metadata$main !== void 0 ? _record$metadata$main : (_record$metadata2 = record.metadata) === null || _record$metadata2 === void 0 ? void 0 : _record$metadata2.main_product_selling_price) !== null && _ref3 !== void 0 ? _ref3 : record.selling_price) !== null && _ref2 !== void 0 ? _ref2 : 0);
158
+ return toDecimal((_ref = (_ref2 = (_record$metadata$main = (_record$metadata = record.metadata) === null || _record$metadata === void 0 ? void 0 : _record$metadata.main_product_attached_bundle_selling_price) !== null && _record$metadata$main !== void 0 ? _record$metadata$main : (_record$metadata2 = record.metadata) === null || _record$metadata2 === void 0 ? void 0 : _record$metadata2.main_product_selling_price) !== null && _ref2 !== void 0 ? _ref2 : record.selling_price) !== null && _ref !== void 0 ? _ref : 0);
151
159
  }
152
160
  function getBundleSurchargeUnitAmount(bundle) {
153
- var _ref4, _ref5, _bundle$bundle_sellin;
154
- return toDecimal((_ref4 = (_ref5 = (_bundle$bundle_sellin = bundle === null || bundle === void 0 ? void 0 : bundle.bundle_selling_price) !== null && _bundle$bundle_sellin !== void 0 ? _bundle$bundle_sellin : bundle === null || bundle === void 0 ? void 0 : bundle.bundle_sum_price) !== null && _ref5 !== void 0 ? _ref5 : bundle === null || bundle === void 0 ? void 0 : bundle.price) !== null && _ref4 !== void 0 ? _ref4 : 0);
161
+ var _ref3, _ref4, _bundle$bundle_sellin;
162
+ return toDecimal((_ref3 = (_ref4 = (_bundle$bundle_sellin = bundle === null || bundle === void 0 ? void 0 : bundle.bundle_selling_price) !== null && _bundle$bundle_sellin !== void 0 ? _bundle$bundle_sellin : bundle === null || bundle === void 0 ? void 0 : bundle.bundle_sum_price) !== null && _ref4 !== void 0 ? _ref4 : bundle === null || bundle === void 0 ? void 0 : bundle.price) !== null && _ref3 !== void 0 ? _ref3 : 0);
155
163
  }
156
164
  function getBundleProductId(bundle) {
157
- var _ref6, _bundle$bundle_produc;
158
- var id = (_ref6 = (_bundle$bundle_produc = bundle === null || bundle === void 0 ? void 0 : bundle.bundle_product_id) !== null && _bundle$bundle_produc !== void 0 ? _bundle$bundle_produc : bundle === null || bundle === void 0 ? void 0 : bundle._bundle_product_id) !== null && _ref6 !== void 0 ? _ref6 : bundle === null || bundle === void 0 ? void 0 : bundle.product_id;
165
+ var _ref5, _bundle$bundle_produc;
166
+ var id = (_ref5 = (_bundle$bundle_produc = bundle === null || bundle === void 0 ? void 0 : bundle.bundle_product_id) !== null && _bundle$bundle_produc !== void 0 ? _bundle$bundle_produc : bundle === null || bundle === void 0 ? void 0 : bundle._bundle_product_id) !== null && _ref5 !== void 0 ? _ref5 : bundle === null || bundle === void 0 ? void 0 : bundle.product_id;
159
167
  if (id === undefined || id === null || id === '') return null;
160
168
  var parsed = Number(id);
161
169
  if (!Number.isFinite(parsed)) return null;
162
170
  return parsed;
163
171
  }
172
+ function getPersistedMainSurchargeFee(product) {
173
+ var _ref6, _product$surcharge_fe, _product$metadata7;
174
+ if ((product === null || product === void 0 ? void 0 : product.order_detail_id) === undefined || (product === null || product === void 0 ? void 0 : product.order_detail_id) === null) return null;
175
+ var value = (_ref6 = (_product$surcharge_fe = product === null || product === void 0 ? void 0 : product.surcharge_fee) !== null && _product$surcharge_fe !== void 0 ? _product$surcharge_fe : product === null || product === void 0 ? void 0 : product.main_product_attached_bundle_surcharge_fee) !== null && _ref6 !== void 0 ? _ref6 : product === null || product === void 0 || (_product$metadata7 = product.metadata) === null || _product$metadata7 === void 0 ? void 0 : _product$metadata7.main_product_attached_bundle_surcharge_fee;
176
+ if (value === undefined || value === null || value === '') return null;
177
+ return toDecimal(value);
178
+ }
179
+ function getPersistedSourceSurchargeFee(sourceItem) {
180
+ var _sourceItem$metadata;
181
+ var value = sourceItem === null || sourceItem === void 0 || (_sourceItem$metadata = sourceItem.metadata) === null || _sourceItem$metadata === void 0 ? void 0 : _sourceItem$metadata.__persisted_surcharge_fee;
182
+ if (value === undefined || value === null || value === '') return null;
183
+ return toDecimal(value);
184
+ }
185
+ function getPersistedSurchargeRemainder(item, sourceItem) {
186
+ var _ref7, _sourceItem$metadata$, _sourceItem$metadata2, _item$metadata2;
187
+ var value = (_ref7 = (_sourceItem$metadata$ = sourceItem === null || sourceItem === void 0 || (_sourceItem$metadata2 = sourceItem.metadata) === null || _sourceItem$metadata2 === void 0 ? void 0 : _sourceItem$metadata2.surcharge_rounding_remainder) !== null && _sourceItem$metadata$ !== void 0 ? _sourceItem$metadata$ : item === null || item === void 0 ? void 0 : item.surcharge_rounding_remainder) !== null && _ref7 !== void 0 ? _ref7 : item === null || item === void 0 || (_item$metadata2 = item.metadata) === null || _item$metadata2 === void 0 ? void 0 : _item$metadata2.surcharge_rounding_remainder;
188
+ if (value === undefined || value === null || value === '') return null;
189
+ return toDecimal(value);
190
+ }
191
+ function getPersistedQuantity(item, fallbackQuantity) {
192
+ var _item$product_quantit;
193
+ var isPersistedLine = (item === null || item === void 0 ? void 0 : item.order_detail_id) !== undefined && (item === null || item === void 0 ? void 0 : item.order_detail_id) !== null;
194
+ if (!isPersistedLine) return new Decimal(0);
195
+ var value = (_item$product_quantit = item === null || item === void 0 ? void 0 : item.product_quantity) !== null && _item$product_quantit !== void 0 ? _item$product_quantit : item === null || item === void 0 ? void 0 : item.quantity;
196
+ var parsed = new Decimal(value || 0);
197
+ if (parsed.lte(0)) return Decimal.min(fallbackQuantity, 1);
198
+ return Decimal.min(fallbackQuantity, parsed);
199
+ }
200
+ function getPersistedBundleSurchargeFee(bundle, product) {
201
+ var _bundle$surcharge_fee, _bundle$metadata;
202
+ if ((bundle === null || bundle === void 0 ? void 0 : bundle.order_detail_id) === undefined && (product === null || product === void 0 ? void 0 : product.order_detail_id) === undefined) return null;
203
+ if ((bundle === null || bundle === void 0 ? void 0 : bundle.order_detail_id) === null && (product === null || product === void 0 ? void 0 : product.order_detail_id) === null) return null;
204
+ var value = (_bundle$surcharge_fee = bundle === null || bundle === void 0 ? void 0 : bundle.surcharge_fee) !== null && _bundle$surcharge_fee !== void 0 ? _bundle$surcharge_fee : bundle === null || bundle === void 0 || (_bundle$metadata = bundle.metadata) === null || _bundle$metadata === void 0 ? void 0 : _bundle$metadata.surcharge_fee;
205
+ if (value === undefined || value === null || value === '') return null;
206
+ return toDecimal(value);
207
+ }
164
208
  function createSurchargeAllocationNodes(products, sourceItems) {
165
209
  var nodes = [];
166
210
  for (var productIndex = 0; productIndex < products.length; productIndex++) {
167
- var _product$product_id;
211
+ var _product$product_id, _getPersistedSourceSu;
168
212
  var product = products[productIndex];
169
213
  var sourceItem = sourceItems[productIndex] || {};
170
214
  var productQuantity = new Decimal(getSafeNum(product.num));
171
215
  for (var bundleIndex = 0; bundleIndex < (product.product_bundle || []).length; bundleIndex++) {
172
- var _sourceItem$bundle, _bundle$num;
216
+ var _sourceItem$bundle, _bundle$num, _bundle$num2;
173
217
  var bundle = product.product_bundle[bundleIndex];
174
218
  if (!isBundleOriginalPrice(bundle)) continue;
175
219
  var sourceBundle = (sourceItem === null || sourceItem === void 0 || (_sourceItem$bundle = sourceItem.bundle) === null || _sourceItem$bundle === void 0 ? void 0 : _sourceItem$bundle[bundleIndex]) || {};
@@ -180,7 +224,10 @@ function createSurchargeAllocationNodes(products, sourceItems) {
180
224
  productId: getBundleProductId(bundle),
181
225
  unitAmount: getBundleSurchargeUnitAmount(bundle),
182
226
  quantity: new Decimal(getSafeNum((_bundle$num = bundle === null || bundle === void 0 ? void 0 : bundle.num) !== null && _bundle$num !== void 0 ? _bundle$num : bundle === null || bundle === void 0 ? void 0 : bundle.quantity)).times(productQuantity),
183
- relationSurchargeIds: getRelationSurchargeIds(sourceBundle)
227
+ persistedQuantity: getPersistedQuantity(bundle, new Decimal(getSafeNum((_bundle$num2 = bundle === null || bundle === void 0 ? void 0 : bundle.num) !== null && _bundle$num2 !== void 0 ? _bundle$num2 : bundle === null || bundle === void 0 ? void 0 : bundle.quantity)).times(productQuantity)),
228
+ relationSurchargeIds: getRelationSurchargeIds(sourceBundle).length ? getRelationSurchargeIds(sourceBundle) : getRelationSurchargeIds(bundle),
229
+ persistedUnitSurchargeFee: getPersistedBundleSurchargeFee(bundle, product),
230
+ persistedSurchargeRemainder: getPersistedSurchargeRemainder(bundle, sourceBundle)
184
231
  });
185
232
  }
186
233
  nodes.push({
@@ -190,7 +237,10 @@ function createSurchargeAllocationNodes(products, sourceItems) {
190
237
  productId: (_product$product_id = product.product_id) !== null && _product$product_id !== void 0 ? _product$product_id : null,
191
238
  unitAmount: getMainSurchargeUnitAmount(product),
192
239
  quantity: productQuantity,
193
- relationSurchargeIds: getRelationSurchargeIds(sourceItem)
240
+ persistedQuantity: getPersistedQuantity(product, productQuantity),
241
+ relationSurchargeIds: getRelationSurchargeIds(sourceItem).length ? getRelationSurchargeIds(sourceItem) : getRelationSurchargeIds(product),
242
+ persistedUnitSurchargeFee: (_getPersistedSourceSu = getPersistedSourceSurchargeFee(sourceItem)) !== null && _getPersistedSourceSu !== void 0 ? _getPersistedSourceSu : getPersistedMainSurchargeFee(product),
243
+ persistedSurchargeRemainder: getPersistedSurchargeRemainder(product, sourceItem)
194
244
  });
195
245
  }
196
246
  return nodes;
@@ -222,19 +272,20 @@ function resetProductSurchargeFields(products) {
222
272
  }
223
273
  }
224
274
  }
225
- function buildSurchargeQuantityTotals(nodes) {
226
- var totals = new Map();
275
+ function getPersistedSurchargeIds(nodes) {
276
+ var ids = new Set();
227
277
  var _iterator3 = _createForOfIteratorHelper(nodes),
228
278
  _step3;
229
279
  try {
230
280
  for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
231
281
  var node = _step3.value;
282
+ if (!node.persistedUnitSurchargeFee || node.persistedUnitSurchargeFee.lte(0)) continue;
232
283
  var _iterator4 = _createForOfIteratorHelper(node.relationSurchargeIds),
233
284
  _step4;
234
285
  try {
235
286
  for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
236
287
  var surchargeId = _step4.value;
237
- totals.set(surchargeId, (totals.get(surchargeId) || new Decimal(0)).plus(node.quantity));
288
+ ids.add(surchargeId);
238
289
  }
239
290
  } catch (err) {
240
291
  _iterator4.e(err);
@@ -247,6 +298,33 @@ function buildSurchargeQuantityTotals(nodes) {
247
298
  } finally {
248
299
  _iterator3.f();
249
300
  }
301
+ return ids;
302
+ }
303
+ function buildSurchargeQuantityTotals(nodes) {
304
+ var totals = new Map();
305
+ var _iterator5 = _createForOfIteratorHelper(nodes),
306
+ _step5;
307
+ try {
308
+ for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
309
+ var node = _step5.value;
310
+ var _iterator6 = _createForOfIteratorHelper(node.relationSurchargeIds),
311
+ _step6;
312
+ try {
313
+ for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
314
+ var surchargeId = _step6.value;
315
+ totals.set(surchargeId, (totals.get(surchargeId) || new Decimal(0)).plus(node.quantity));
316
+ }
317
+ } catch (err) {
318
+ _iterator6.e(err);
319
+ } finally {
320
+ _iterator6.f();
321
+ }
322
+ }
323
+ } catch (err) {
324
+ _iterator5.e(err);
325
+ } finally {
326
+ _iterator5.f();
327
+ }
250
328
  return totals;
251
329
  }
252
330
  function getFallbackAllProductSurchargeIds(surchargeList) {
@@ -275,6 +353,24 @@ function getBackendSurchargeState(stateMap, surchargeId, config) {
275
353
  }
276
354
  return state;
277
355
  }
356
+ function buildSurchargeResultItem(config, surchargeId, amount, index) {
357
+ var name = (config === null || config === void 0 ? void 0 : config.name) || (config === null || config === void 0 ? void 0 : config.label) || {};
358
+ return {
359
+ key: "custom_surcharge_".concat(index),
360
+ label: name,
361
+ name: name,
362
+ value: amount.toNumber(),
363
+ surcharge_id: surchargeId,
364
+ description: config === null || config === void 0 ? void 0 : config.description,
365
+ type: 'default',
366
+ fixed: config === null || config === void 0 ? void 0 : config.fixed,
367
+ amount: amount.toNumber(),
368
+ percentage: config === null || config === void 0 ? void 0 : config.percentage,
369
+ metadata: {
370
+ open_product: config === null || config === void 0 ? void 0 : config.open_product
371
+ }
372
+ };
373
+ }
278
374
  function applySurchargeNodeResult(node, params) {
279
375
  var unitSurchargeFee = params.unitSurchargeFee,
280
376
  roundingRemainder = params.roundingRemainder,
@@ -302,45 +398,77 @@ function calculateBackendProductSurcharges(params) {
302
398
  var configById = getSurchargeConfigById(surchargeList);
303
399
  var nodes = createSurchargeAllocationNodes(products, sourceItems);
304
400
  var fallbackAllProductSurchargeIds = getFallbackAllProductSurchargeIds(surchargeList);
305
- var _iterator5 = _createForOfIteratorHelper(nodes),
306
- _step5;
401
+ var _iterator7 = _createForOfIteratorHelper(nodes),
402
+ _step7;
307
403
  try {
308
- for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
309
- var node = _step5.value;
404
+ for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
405
+ var node = _step7.value;
310
406
  if (node.relationSurchargeIds.length === 0) {
311
407
  node.relationSurchargeIds = fallbackAllProductSurchargeIds;
312
408
  }
313
409
  }
314
410
  } catch (err) {
315
- _iterator5.e(err);
411
+ _iterator7.e(err);
316
412
  } finally {
317
- _iterator5.f();
413
+ _iterator7.f();
318
414
  }
415
+ var persistedSurchargeIds = getPersistedSurchargeIds(nodes);
319
416
  var quantityTotals = buildSurchargeQuantityTotals(nodes);
320
417
  var stateMap = new Map();
321
- var _iterator6 = _createForOfIteratorHelper(nodes),
322
- _step6;
418
+ var _iterator8 = _createForOfIteratorHelper(nodes),
419
+ _step8;
323
420
  try {
324
- for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
325
- var _node = _step6.value;
421
+ for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
422
+ var _node = _step8.value;
326
423
  var unitSurchargeFee = new Decimal(0);
327
424
  var roundingRemainder = new Decimal(0);
328
425
  var appliedSurchargeIds = [];
329
- var _iterator7 = _createForOfIteratorHelper(_node.relationSurchargeIds),
330
- _step7;
426
+ var persistedRemainderApplied = false;
427
+ var _iterator10 = _createForOfIteratorHelper(_node.relationSurchargeIds),
428
+ _step10;
331
429
  try {
332
- for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
333
- var surchargeId = _step7.value;
430
+ for (_iterator10.s(); !(_step10 = _iterator10.n()).done;) {
431
+ var surchargeId = _step10.value;
334
432
  var config = configById.get(surchargeId);
335
433
  var totalQuantity = quantityTotals.get(surchargeId) || new Decimal(0);
336
- if (!config || totalQuantity.lte(0)) continue;
434
+ if (totalQuantity.lte(0)) continue;
435
+ var persistedUnitSurchargeFee = _node.persistedUnitSurchargeFee;
436
+ if (persistedUnitSurchargeFee && persistedUnitSurchargeFee.gt(0)) {
437
+ var persistedQuantity = Decimal.min(_node.persistedQuantity, _node.quantity);
438
+ var deltaQuantity = Decimal.max(_node.quantity.minus(persistedQuantity), 0);
439
+ var _state = getBackendSurchargeState(stateMap, surchargeId, config || {
440
+ surcharge_id: surchargeId
441
+ });
442
+ var persistedSurchargeRemainder = !persistedRemainderApplied && _node.persistedSurchargeRemainder ? _node.persistedSurchargeRemainder : new Decimal(0);
443
+ _state.amount = bcAdd(bcAdd(_state.amount, bcMul(persistedUnitSurchargeFee, persistedQuantity, 2), 2), persistedSurchargeRemainder, 2);
444
+ var _fixed = new Decimal((config === null || config === void 0 ? void 0 : config.fixed) || 0);
445
+ if (_fixed.gt(0)) _state.fixedIncluded = true;
446
+ var nodeTotalSurcharge = bcAdd(bcMul(persistedUnitSurchargeFee, persistedQuantity, 2), persistedSurchargeRemainder, 2);
447
+ if (config && deltaQuantity.gt(0)) {
448
+ var _percentage = new Decimal(config.percentage || 0);
449
+ var deltaUnitPercentageFee = Decimal.max(0, bcMul(_node.unitAmount, _percentage, 2));
450
+ var deltaSurcharge = bcMul(deltaQuantity, deltaUnitPercentageFee, 2);
451
+ _state.amount = bcAdd(_state.amount, deltaSurcharge, 2);
452
+ nodeTotalSurcharge = bcAdd(nodeTotalSurcharge, deltaSurcharge, 2);
453
+ }
454
+ if (_node.quantity.gt(0)) {
455
+ unitSurchargeFee = bcAdd(unitSurchargeFee, bcDiv(nodeTotalSurcharge, _node.quantity, 2), 2);
456
+ }
457
+ if (!persistedSurchargeRemainder.eq(0)) {
458
+ roundingRemainder = bcAdd(roundingRemainder, persistedSurchargeRemainder, 2);
459
+ persistedRemainderApplied = true;
460
+ }
461
+ appliedSurchargeIds.push(surchargeId);
462
+ continue;
463
+ }
464
+ if (!config) continue;
337
465
  var percentage = new Decimal(config.percentage || 0);
338
466
  var fixed = new Decimal(config.fixed || 0);
339
467
  var unitPercentageFee = Decimal.max(0, bcMul(_node.unitAmount, percentage, 2));
340
468
  var percentageTotalFee = bcMul(_node.quantity, unitPercentageFee, 2);
341
469
  var unitFixedFee = new Decimal(0);
342
470
  var fixedRemainder = new Decimal(0);
343
- if (fixed.gt(0)) {
471
+ if (fixed.gt(0) && !persistedSurchargeIds.has(surchargeId)) {
344
472
  unitFixedFee = bcDiv(fixed, totalQuantity, 2);
345
473
  var distributedFixed = bcMul(unitFixedFee, totalQuantity, 2);
346
474
  fixedRemainder = bcSub(fixed, distributedFixed, 2);
@@ -353,7 +481,7 @@ function calculateBackendProductSurcharges(params) {
353
481
  var offsetBaseFee = Decimal.max(0, bcSub(precisePercentage, unitPercentageFee, 6));
354
482
  state.totalOffsetFee = bcAdd(state.totalOffsetFee, bcMul(offsetBaseFee, _node.quantity, 6), 6);
355
483
  state.calculatedQuantity = bcAdd(state.calculatedQuantity, _node.quantity, 6);
356
- if (!state.fixedIncluded && fixed.gt(0)) {
484
+ if (!state.fixedIncluded && fixed.gt(0) && !persistedSurchargeIds.has(surchargeId)) {
357
485
  state.amount = bcAdd(state.amount, fixed, 2);
358
486
  state.fixedIncluded = true;
359
487
  }
@@ -371,9 +499,9 @@ function calculateBackendProductSurcharges(params) {
371
499
  appliedSurchargeIds.push(surchargeId);
372
500
  }
373
501
  } catch (err) {
374
- _iterator7.e(err);
502
+ _iterator10.e(err);
375
503
  } finally {
376
- _iterator7.f();
504
+ _iterator10.f();
377
505
  }
378
506
  applySurchargeNodeResult(_node, {
379
507
  unitSurchargeFee: unitSurchargeFee,
@@ -382,33 +510,39 @@ function calculateBackendProductSurcharges(params) {
382
510
  });
383
511
  }
384
512
  } catch (err) {
385
- _iterator6.e(err);
513
+ _iterator8.e(err);
386
514
  } finally {
387
- _iterator6.f();
515
+ _iterator8.f();
388
516
  }
389
- return (surchargeList || []).reduce(function (result, config, index) {
517
+ var result = (surchargeList || []).reduce(function (items, config, index) {
390
518
  var surchargeId = getSurchargeConfigId(config);
391
- if (surchargeId === undefined) return result;
519
+ if (surchargeId === undefined) return items;
392
520
  var state = stateMap.get(surchargeId);
393
- if (!state || state.amount.lte(0)) return result;
394
- var name = config.name || config.label || {};
395
- result.push({
396
- key: "custom_surcharge_".concat(index),
397
- label: name,
398
- name: name,
399
- value: state.amount.toNumber(),
400
- surcharge_id: surchargeId,
401
- description: config.description,
402
- type: 'default',
403
- fixed: config.fixed,
404
- amount: state.amount.toNumber(),
405
- percentage: config.percentage,
406
- metadata: {
407
- open_product: config.open_product
408
- }
409
- });
410
- return result;
521
+ if (!state || state.amount.lte(0)) return items;
522
+ items.push(buildSurchargeResultItem(config, surchargeId, state.amount, index));
523
+ return items;
411
524
  }, []);
525
+ var includedIds = new Set(result.map(function (item) {
526
+ return item === null || item === void 0 ? void 0 : item.surcharge_id;
527
+ }).filter(function (id) {
528
+ return id !== undefined;
529
+ }));
530
+ var _iterator9 = _createForOfIteratorHelper(stateMap.entries()),
531
+ _step9;
532
+ try {
533
+ for (_iterator9.s(); !(_step9 = _iterator9.n()).done;) {
534
+ var _step9$value = _slicedToArray(_step9.value, 2),
535
+ _surchargeId = _step9$value[0],
536
+ _state2 = _step9$value[1];
537
+ if (includedIds.has(_surchargeId) || _state2.amount.lte(0)) continue;
538
+ result.push(buildSurchargeResultItem(_state2.config, _surchargeId, _state2.amount, result.length));
539
+ }
540
+ } catch (err) {
541
+ _iterator9.e(err);
542
+ } finally {
543
+ _iterator9.f();
544
+ }
545
+ return result;
412
546
  }
413
547
  function getProductDepositData(product) {
414
548
  var _product$_origin, _product$_origin2;
@@ -444,11 +578,11 @@ function calculateProductsDeposit(products) {
444
578
  var hasDeposit = false;
445
579
  var policyIds = new Set();
446
580
  var policyDataMap = new Map();
447
- var _iterator8 = _createForOfIteratorHelper(products),
448
- _step8;
581
+ var _iterator11 = _createForOfIteratorHelper(products),
582
+ _step11;
449
583
  try {
450
- for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
451
- var product = _step8.value;
584
+ for (_iterator11.s(); !(_step11 = _iterator11.n()).done;) {
585
+ var product = _step11.value;
452
586
  var depositData = getProductDepositData(product);
453
587
  if (!depositData || depositData.has_deposit != 1) continue;
454
588
  var num = getSafeNum(product.num);
@@ -461,45 +595,45 @@ function calculateProductsDeposit(products) {
461
595
  var itemDeposit = fixed.plus(percentage.times(productTotal));
462
596
  totalDeposit = totalDeposit.plus(itemDeposit);
463
597
  hasDeposit = true;
464
- var _iterator10 = _createForOfIteratorHelper(collectDepositPolicyIds(depositData)),
465
- _step10;
598
+ var _iterator13 = _createForOfIteratorHelper(collectDepositPolicyIds(depositData)),
599
+ _step13;
466
600
  try {
467
- for (_iterator10.s(); !(_step10 = _iterator10.n()).done;) {
468
- var id = _step10.value;
601
+ for (_iterator13.s(); !(_step13 = _iterator13.n()).done;) {
602
+ var id = _step13.value;
469
603
  policyIds.add(id);
470
604
  }
471
605
  } catch (err) {
472
- _iterator10.e(err);
606
+ _iterator13.e(err);
473
607
  } finally {
474
- _iterator10.f();
608
+ _iterator13.f();
475
609
  }
476
- var _iterator11 = _createForOfIteratorHelper(collectDepositPolicyData(depositData)),
477
- _step11;
610
+ var _iterator14 = _createForOfIteratorHelper(collectDepositPolicyData(depositData)),
611
+ _step14;
478
612
  try {
479
- for (_iterator11.s(); !(_step11 = _iterator11.n()).done;) {
480
- var policy = _step11.value;
613
+ for (_iterator14.s(); !(_step14 = _iterator14.n()).done;) {
614
+ var policy = _step14.value;
481
615
  if (!policyDataMap.has(policy.id)) {
482
616
  policyDataMap.set(policy.id, policy);
483
617
  }
484
618
  policyIds.add(policy.id);
485
619
  }
486
620
  } catch (err) {
487
- _iterator11.e(err);
621
+ _iterator14.e(err);
488
622
  } finally {
489
- _iterator11.f();
623
+ _iterator14.f();
490
624
  }
491
625
  }
492
626
  } catch (err) {
493
- _iterator8.e(err);
627
+ _iterator11.e(err);
494
628
  } finally {
495
- _iterator8.f();
629
+ _iterator11.f();
496
630
  }
497
631
  if (!hasDeposit) return undefined;
498
- var _iterator9 = _createForOfIteratorHelper(policyIds),
499
- _step9;
632
+ var _iterator12 = _createForOfIteratorHelper(policyIds),
633
+ _step12;
500
634
  try {
501
- for (_iterator9.s(); !(_step9 = _iterator9.n()).done;) {
502
- var _id = _step9.value;
635
+ for (_iterator12.s(); !(_step12 = _iterator12.n()).done;) {
636
+ var _id = _step12.value;
503
637
  if (!policyDataMap.has(_id)) {
504
638
  policyDataMap.set(_id, {
505
639
  id: _id,
@@ -508,9 +642,9 @@ function calculateProductsDeposit(products) {
508
642
  }
509
643
  }
510
644
  } catch (err) {
511
- _iterator9.e(err);
645
+ _iterator12.e(err);
512
646
  } finally {
513
- _iterator9.f();
647
+ _iterator12.f();
514
648
  }
515
649
  return {
516
650
  total: totalDeposit.toFixed(2),
@@ -551,40 +685,56 @@ function calculateSingleItemTax(params) {
551
685
  }
552
686
 
553
687
  /**
554
- * 获取主商品折扣后总价(含 option、含主商品折扣、含加价/减价 bundle)。
688
+ * 主商品 + 非原价(加价/减价)子商品的"折扣后"售价合计(含 option、含主商品折扣,未减整单折扣)。
555
689
  *
556
- * 新语义 v2 下:
557
- * - `metadata.main_product_selling_price` 已是"含 option、含折扣"的主价,直接读即可;
558
- * - 无需再 `minus(mainDiscountAmount)` `plus(getOptionUnitPrice)`;
559
- * - 仅需叠加 `markup` / `markdown` 类 bundle(原价 bundle 已由 `calculateProductsTax` 独立处理)。
690
+ * 对齐后端 metadata.main_product_attached_bundle_selling_price:
691
+ * - `metadata.main_product_selling_price` 已是"含 option、含折扣"的主价;
692
+ * - 叠加 `markup` / `markdown` 类 bundle(原价 bundle 由 `calculateProductsTax` 独立处理)。
560
693
  */
561
- function getMainProductPaymentTotal(product) {
562
- var _ref7, _product$metadata$mai, _product$metadata7, _product$metadata8;
563
- var mainSelling = (_ref7 = (_product$metadata$mai = (_product$metadata7 = product.metadata) === null || _product$metadata7 === void 0 ? void 0 : _product$metadata7.main_product_selling_price) !== null && _product$metadata$mai !== void 0 ? _product$metadata$mai : (_product$metadata8 = product.metadata) === null || _product$metadata8 === void 0 ? void 0 : _product$metadata8.main_product_original_price) !== null && _ref7 !== void 0 ? _ref7 : 0;
694
+ function getMainAttachedBundleSellingTotal(product) {
695
+ var _ref8, _product$metadata$mai, _product$metadata8, _product$metadata9;
696
+ var mainSelling = (_ref8 = (_product$metadata$mai = (_product$metadata8 = product.metadata) === null || _product$metadata8 === void 0 ? void 0 : _product$metadata8.main_product_selling_price) !== null && _product$metadata$mai !== void 0 ? _product$metadata$mai : (_product$metadata9 = product.metadata) === null || _product$metadata9 === void 0 ? void 0 : _product$metadata9.main_product_original_price) !== null && _ref8 !== void 0 ? _ref8 : 0;
564
697
  var total = toDecimal(mainSelling);
565
698
  var bundleItems = product.product_bundle || [];
566
- var _iterator12 = _createForOfIteratorHelper(bundleItems),
567
- _step12;
699
+ var _iterator15 = _createForOfIteratorHelper(bundleItems),
700
+ _step15;
568
701
  try {
569
- for (_iterator12.s(); !(_step12 = _iterator12.n()).done;) {
570
- var bundleItem = _step12.value;
702
+ for (_iterator15.s(); !(_step15 = _iterator15.n()).done;) {
703
+ var bundleItem = _step15.value;
571
704
  if (isBundleMarkupOrDiscount(bundleItem)) {
572
- var _ref8, _bundleItem$bundle_se, _bundleItem$num;
573
- var unit = toDecimal((_ref8 = (_bundleItem$bundle_se = bundleItem.bundle_selling_price) !== null && _bundleItem$bundle_se !== void 0 ? _bundleItem$bundle_se : bundleItem.price) !== null && _ref8 !== void 0 ? _ref8 : 0);
705
+ var _bundleItem$num;
706
+ // markdown(减价)按带符号净额取减;markup 维持相加。
707
+ var unit = getBundleSignedUnit(bundleItem);
574
708
  var qty = getSafeNum((_bundleItem$num = bundleItem.num) !== null && _bundleItem$num !== void 0 ? _bundleItem$num : bundleItem.quantity);
575
709
  total = total.plus(unit.times(qty));
576
710
  }
577
711
  }
578
712
  } catch (err) {
579
- _iterator12.e(err);
713
+ _iterator15.e(err);
580
714
  } finally {
581
- _iterator12.f();
715
+ _iterator15.f();
582
716
  }
583
717
  return Decimal.max(total, 0);
584
718
  }
719
+
720
+ /**
721
+ * 获取主商品税费基数(含 option、含主商品折扣、含加价/减价 bundle、已减整单折扣均摊)。
722
+ *
723
+ * 对齐后端 handleMainProductTaxFee:税基使用 main_product_attached_bundle_payment_price。
724
+ * - 优先读取均摊层写入的 payment 价;
725
+ * - 兜底退化为 selling 合计(无整单折扣时两者相等)。
726
+ */
727
+ function getMainProductPaymentTotal(product) {
728
+ var _product$metadata10;
729
+ var attachedPayment = (_product$metadata10 = product.metadata) === null || _product$metadata10 === void 0 ? void 0 : _product$metadata10.main_product_attached_bundle_payment_price;
730
+ if (attachedPayment !== undefined && attachedPayment !== null && attachedPayment !== '') {
731
+ return Decimal.max(toDecimal(attachedPayment), 0);
732
+ }
733
+ return getMainAttachedBundleSellingTotal(product);
734
+ }
585
735
  function getExplicitTaxRemainder(item) {
586
- var _item$tax_fee_roundin, _item$metadata2;
587
- var value = (_item$tax_fee_roundin = item === null || item === void 0 ? void 0 : item.tax_fee_rounding_remainder) !== null && _item$tax_fee_roundin !== void 0 ? _item$tax_fee_roundin : item === null || item === void 0 || (_item$metadata2 = item.metadata) === null || _item$metadata2 === void 0 ? void 0 : _item$metadata2.tax_fee_rounding_remainder;
736
+ var _item$tax_fee_roundin, _item$metadata3;
737
+ var value = (_item$tax_fee_roundin = item === null || item === void 0 ? void 0 : item.tax_fee_rounding_remainder) !== null && _item$tax_fee_roundin !== void 0 ? _item$tax_fee_roundin : item === null || item === void 0 || (_item$metadata3 = item.metadata) === null || _item$metadata3 === void 0 ? void 0 : _item$metadata3.tax_fee_rounding_remainder;
588
738
  if (value === undefined || value === null || value === '') return null;
589
739
  return new Decimal(value || 0);
590
740
  }
@@ -593,6 +743,13 @@ function getPersistedTaxRemainder(item, parentProduct) {
593
743
  if (!isPersistedLine) return null;
594
744
  return getExplicitTaxRemainder(item);
595
745
  }
746
+ function getPersistedMainTaxFee(product) {
747
+ var _ref9, _product$metadata$mai2, _product$metadata11;
748
+ if ((product === null || product === void 0 ? void 0 : product.order_detail_id) === undefined || (product === null || product === void 0 ? void 0 : product.order_detail_id) === null) return null;
749
+ var value = (_ref9 = (_product$metadata$mai2 = product === null || product === void 0 || (_product$metadata11 = product.metadata) === null || _product$metadata11 === void 0 ? void 0 : _product$metadata11.main_product_attached_bundle_tax_fee) !== null && _product$metadata$mai2 !== void 0 ? _product$metadata$mai2 : product === null || product === void 0 ? void 0 : product.main_product_attached_bundle_tax_fee) !== null && _ref9 !== void 0 ? _ref9 : product === null || product === void 0 ? void 0 : product.tax_fee;
750
+ if (value === undefined || value === null || value === '') return null;
751
+ return toDecimal(value);
752
+ }
596
753
  function writeTaxRemainder(item, roundingRemainder) {
597
754
  item.tax_fee_rounding_remainder = roundingRemainder.toNumber();
598
755
  if (item.metadata && _typeof(item.metadata) === 'object') {
@@ -601,35 +758,41 @@ function writeTaxRemainder(item, roundingRemainder) {
601
758
  }
602
759
  function resetTaxRemainderFields(products) {
603
760
  for (var _i2 = 0, _arr2 = products; _i2 < _arr2.length; _i2++) {
604
- var _productRemainder$toN;
605
761
  var product = _arr2[_i2];
606
762
  var productRemainder = getPersistedTaxRemainder(product);
607
- product.tax_fee_rounding_remainder = (_productRemainder$toN = productRemainder === null || productRemainder === void 0 ? void 0 : productRemainder.toNumber()) !== null && _productRemainder$toN !== void 0 ? _productRemainder$toN : 0;
763
+ if (productRemainder !== null) {
764
+ product.tax_fee_rounding_remainder = productRemainder.toNumber();
765
+ } else {
766
+ delete product.tax_fee_rounding_remainder;
767
+ }
608
768
  product.original_tax_fee_rounding_remainder = 0;
609
- var _iterator13 = _createForOfIteratorHelper(product.product_bundle || []),
610
- _step13;
769
+ var _iterator16 = _createForOfIteratorHelper(product.product_bundle || []),
770
+ _step16;
611
771
  try {
612
- for (_iterator13.s(); !(_step13 = _iterator13.n()).done;) {
613
- var _bundleRemainder$toFi, _bundleRemainder$toNu;
614
- var bundle = _step13.value;
772
+ for (_iterator16.s(); !(_step16 = _iterator16.n()).done;) {
773
+ var bundle = _step16.value;
615
774
  var bundleRemainder = getPersistedTaxRemainder(bundle, product);
616
- bundle.metadata = _objectSpread(_objectSpread({}, bundle.metadata || {}), {}, {
617
- tax_fee_rounding_remainder: (_bundleRemainder$toFi = bundleRemainder === null || bundleRemainder === void 0 ? void 0 : bundleRemainder.toFixed(2)) !== null && _bundleRemainder$toFi !== void 0 ? _bundleRemainder$toFi : '0.00'
618
- });
619
- bundle.tax_fee_rounding_remainder = (_bundleRemainder$toNu = bundleRemainder === null || bundleRemainder === void 0 ? void 0 : bundleRemainder.toNumber()) !== null && _bundleRemainder$toNu !== void 0 ? _bundleRemainder$toNu : 0;
775
+ bundle.metadata = _objectSpread({}, bundle.metadata || {});
776
+ if (bundleRemainder !== null) {
777
+ bundle.metadata.tax_fee_rounding_remainder = bundleRemainder.toFixed(2);
778
+ bundle.tax_fee_rounding_remainder = bundleRemainder.toNumber();
779
+ } else {
780
+ delete bundle.metadata.tax_fee_rounding_remainder;
781
+ delete bundle.tax_fee_rounding_remainder;
782
+ }
620
783
  bundle.original_tax_fee_rounding_remainder = 0;
621
784
  }
622
785
  } catch (err) {
623
- _iterator13.e(err);
786
+ _iterator16.e(err);
624
787
  } finally {
625
- _iterator13.f();
788
+ _iterator16.f();
626
789
  }
627
790
  }
628
791
  }
629
792
  function getBundlePaymentPrice(bundle, product) {
630
- var _ref9, _ref10, _bundle$bundle_sellin2, _metadata$average_dis, _metadata;
793
+ var _ref10, _ref11, _bundle$bundle_sellin2, _metadata$average_dis, _metadata;
631
794
  if ((bundle === null || bundle === void 0 ? void 0 : bundle.bundle_payment_price) !== undefined) return toDecimal(bundle.bundle_payment_price);
632
- var basePrice = toDecimal((_ref9 = (_ref10 = (_bundle$bundle_sellin2 = bundle === null || bundle === void 0 ? void 0 : bundle.bundle_selling_price) !== null && _bundle$bundle_sellin2 !== void 0 ? _bundle$bundle_sellin2 : bundle === null || bundle === void 0 ? void 0 : bundle.bundle_sum_price) !== null && _ref10 !== void 0 ? _ref10 : bundle === null || bundle === void 0 ? void 0 : bundle.price) !== null && _ref9 !== void 0 ? _ref9 : 0);
795
+ var basePrice = toDecimal((_ref10 = (_ref11 = (_bundle$bundle_sellin2 = bundle === null || bundle === void 0 ? void 0 : bundle.bundle_selling_price) !== null && _bundle$bundle_sellin2 !== void 0 ? _bundle$bundle_sellin2 : bundle === null || bundle === void 0 ? void 0 : bundle.bundle_sum_price) !== null && _ref11 !== void 0 ? _ref11 : bundle === null || bundle === void 0 ? void 0 : bundle.price) !== null && _ref10 !== void 0 ? _ref10 : 0);
633
796
  if (!isBundleOriginalPrice(bundle)) return basePrice;
634
797
  var averageDiscountRate = toDecimal((_metadata$average_dis = (_metadata = product.metadata) === null || _metadata === void 0 ? void 0 : _metadata.average_discount_amount_rate) !== null && _metadata$average_dis !== void 0 ? _metadata$average_dis : 1);
635
798
  return toAmountFormat(bcMul(basePrice, averageDiscountRate, 6));
@@ -661,33 +824,33 @@ function calculateProductsTax(products, taxRate, isPriceIncludeTax) {
661
824
  var totalTax = new Decimal(0);
662
825
  var totalOriginTax = new Decimal(0);
663
826
  resetTaxRemainderFields(products);
664
- var _iterator14 = _createForOfIteratorHelper(products),
665
- _step14;
827
+ var _iterator17 = _createForOfIteratorHelper(products),
828
+ _step17;
666
829
  try {
667
- for (_iterator14.s(); !(_step14 = _iterator14.n()).done;) {
830
+ for (_iterator17.s(); !(_step17 = _iterator17.n()).done;) {
668
831
  var _product$is_charge_ta2, _product$is_charge_ta3;
669
- var product = _step14.value;
832
+ var product = _step17.value;
670
833
  var quantity = new Decimal(getSafeNum(product.num));
671
834
  var productUnitTax = new Decimal(0);
672
835
  var productUnitOriginTax = new Decimal(0);
673
836
  var bundleItems = product.product_bundle || [];
674
- var _iterator15 = _createForOfIteratorHelper(bundleItems),
675
- _step15;
837
+ var _iterator18 = _createForOfIteratorHelper(bundleItems),
838
+ _step18;
676
839
  try {
677
- for (_iterator15.s(); !(_step15 = _iterator15.n()).done;) {
678
- var _bundleItem$num2, _ref11, _ref12, _bundleItem$original_, _ref13, _bundleItem$is_charge, _ref14, _bundleItem$is_charge2;
679
- var bundleItem = _step15.value;
840
+ for (_iterator18.s(); !(_step18 = _iterator18.n()).done;) {
841
+ var _bundleItem$num2, _ref12, _ref13, _bundleItem$original_, _ref14, _bundleItem$is_charge, _ref15, _bundleItem$is_charge2;
842
+ var bundleItem = _step18.value;
680
843
  if (!isBundleOriginalPrice(bundleItem)) continue;
681
844
  var bundleQuantity = new Decimal(getSafeNum((_bundleItem$num2 = bundleItem.num) !== null && _bundleItem$num2 !== void 0 ? _bundleItem$num2 : bundleItem.quantity));
682
845
  var totalBundleQuantity = bundleQuantity.times(quantity);
683
846
  var bundlePrice = getBundlePaymentPrice(bundleItem, product);
684
- var bundleOriginalPrice = toDecimal((_ref11 = (_ref12 = (_bundleItem$original_ = bundleItem.original_price) !== null && _bundleItem$original_ !== void 0 ? _bundleItem$original_ : bundleItem.product_price) !== null && _ref12 !== void 0 ? _ref12 : bundleItem.price) !== null && _ref11 !== void 0 ? _ref11 : 0);
847
+ var bundleOriginalPrice = toDecimal((_ref12 = (_ref13 = (_bundleItem$original_ = bundleItem.original_price) !== null && _bundleItem$original_ !== void 0 ? _bundleItem$original_ : bundleItem.product_price) !== null && _ref13 !== void 0 ? _ref13 : bundleItem.price) !== null && _ref12 !== void 0 ? _ref12 : 0);
685
848
  var bundleSurchargeFee = toDecimal(bundleItem.surcharge_fee);
686
849
  var bundleTaxPrecise = calculateSingleItemTax({
687
850
  price: bundlePrice.plus(bundleSurchargeFee),
688
851
  taxRate: rate,
689
852
  isPriceIncludeTax: isPriceIncludeTax,
690
- isChargeTax: (_ref13 = (_bundleItem$is_charge = bundleItem.is_charge_tax) !== null && _bundleItem$is_charge !== void 0 ? _bundleItem$is_charge : product.is_charge_tax) !== null && _ref13 !== void 0 ? _ref13 : 0
853
+ isChargeTax: (_ref14 = (_bundleItem$is_charge = bundleItem.is_charge_tax) !== null && _bundleItem$is_charge !== void 0 ? _bundleItem$is_charge : product.is_charge_tax) !== null && _ref14 !== void 0 ? _ref14 : 0
691
854
  });
692
855
  var bundleTaxRounded = toBackendTaxFee(bundleTaxPrecise);
693
856
  bundleItem.tax_fee = bundleTaxRounded.toNumber();
@@ -704,7 +867,7 @@ function calculateProductsTax(products, taxRate, isPriceIncludeTax) {
704
867
  price: bundleOriginalPrice.plus(bundleSurchargeFee),
705
868
  taxRate: rate,
706
869
  isPriceIncludeTax: isPriceIncludeTax,
707
- isChargeTax: (_ref14 = (_bundleItem$is_charge2 = bundleItem.is_charge_tax) !== null && _bundleItem$is_charge2 !== void 0 ? _bundleItem$is_charge2 : product.is_charge_tax) !== null && _ref14 !== void 0 ? _ref14 : 0
870
+ isChargeTax: (_ref15 = (_bundleItem$is_charge2 = bundleItem.is_charge_tax) !== null && _bundleItem$is_charge2 !== void 0 ? _bundleItem$is_charge2 : product.is_charge_tax) !== null && _ref15 !== void 0 ? _ref15 : 0
708
871
  });
709
872
  var bundleOrigTaxRounded = toAmountFormat(bundleOrigTaxPrecise);
710
873
  bundleItem.original_tax_fee = bundleOrigTaxRounded.toNumber();
@@ -714,9 +877,9 @@ function calculateProductsTax(products, taxRate, isPriceIncludeTax) {
714
877
  totalOriginTax = totalOriginTax.plus(bundleOrigTaxRounded.times(totalBundleQuantity));
715
878
  }
716
879
  } catch (err) {
717
- _iterator15.e(err);
880
+ _iterator18.e(err);
718
881
  } finally {
719
- _iterator15.f();
882
+ _iterator18.f();
720
883
  }
721
884
  var mainTotal = getMainProductPaymentTotal(product);
722
885
  var surchargeFee = toDecimal(product.surcharge_fee);
@@ -728,16 +891,22 @@ function calculateProductsTax(products, taxRate, isPriceIncludeTax) {
728
891
  isChargeTax: (_product$is_charge_ta2 = product.is_charge_tax) !== null && _product$is_charge_ta2 !== void 0 ? _product$is_charge_ta2 : 0
729
892
  });
730
893
  var mainTaxRounded = toBackendTaxFee(mainTaxPrecise);
731
- product.main_product_attached_bundle_tax_fee = mainTaxRounded.toFixed(2);
732
894
  var persistedMainRemainder = getPersistedTaxRemainder(product);
733
- var mainRemainder = persistedMainRemainder !== null && persistedMainRemainder !== void 0 ? persistedMainRemainder : applyBackendTaxRemainder({
895
+ var persistedMainTaxFee = getPersistedMainTaxFee(product);
896
+ var persistedQuantity = getPersistedQuantity(product, quantity);
897
+ var deltaQuantity = Decimal.max(quantity.minus(persistedQuantity), 0);
898
+ var shouldUsePersistedMainTax = persistedMainTaxFee && persistedMainTaxFee.gt(0) && persistedQuantity.gte(quantity);
899
+ var shouldSplitPersistedMainTax = persistedMainTaxFee && persistedMainTaxFee.gt(0) && persistedQuantity.gt(0) && deltaQuantity.gt(0);
900
+ var mainRemainder = shouldUsePersistedMainTax || shouldSplitPersistedMainTax ? new Decimal(0) : persistedMainRemainder !== null && persistedMainRemainder !== void 0 ? persistedMainRemainder : applyBackendTaxRemainder({
734
901
  target: product,
735
902
  rawTax: mainTaxPrecise,
736
903
  formattedTax: mainTaxRounded,
737
904
  quantity: quantity,
738
905
  state: taxRemainderState
739
906
  });
740
- if (persistedMainRemainder) writeTaxRemainder(product, persistedMainRemainder);
907
+ if (persistedMainRemainder && !shouldSplitPersistedMainTax) {
908
+ writeTaxRemainder(product, persistedMainRemainder);
909
+ }
741
910
  var originalTotal = getUnitOriginalTotal(product);
742
911
  var originalTaxableBase = originalTotal.lte(0) ? surchargeFee : originalTotal.plus(surchargeFee);
743
912
  var originalTaxPrecise = calculateSingleItemTax({
@@ -747,23 +916,90 @@ function calculateProductsTax(products, taxRate, isPriceIncludeTax) {
747
916
  isChargeTax: (_product$is_charge_ta3 = product.is_charge_tax) !== null && _product$is_charge_ta3 !== void 0 ? _product$is_charge_ta3 : 0
748
917
  });
749
918
  var originalTaxRounded = toAmountFormat(originalTaxPrecise);
750
- productUnitTax = productUnitTax.plus(mainTaxRounded);
919
+ var effectiveMainUnitTax = shouldUsePersistedMainTax ? persistedMainTaxFee : shouldSplitPersistedMainTax ? persistedMainTaxFee.times(persistedQuantity).plus(mainTaxRounded.times(deltaQuantity)).div(quantity).toDecimalPlaces(2, Decimal.ROUND_HALF_UP) : mainTaxRounded;
920
+ productUnitTax = productUnitTax.plus(effectiveMainUnitTax);
751
921
  productUnitOriginTax = productUnitOriginTax.plus(originalTaxRounded);
922
+ product.main_product_attached_bundle_tax_fee = effectiveMainUnitTax.toDecimalPlaces(2, Decimal.ROUND_HALF_UP).toFixed(2);
752
923
  product.tax_fee = productUnitTax.toDecimalPlaces(2, Decimal.ROUND_HALF_UP).toFixed(2);
753
924
  product.original_tax_fee = productUnitOriginTax.toDecimalPlaces(2, Decimal.ROUND_HALF_UP).toNumber();
754
- totalTax = totalTax.plus(mainTaxRounded.times(quantity)).plus(mainRemainder);
925
+ totalTax = totalTax.plus(shouldUsePersistedMainTax ? persistedMainTaxFee.times(quantity).plus(persistedMainRemainder || 0) : shouldSplitPersistedMainTax ? persistedMainTaxFee.times(persistedQuantity).plus(mainTaxRounded.times(deltaQuantity)) : mainTaxRounded.times(quantity).plus(mainRemainder));
755
926
  totalOriginTax = totalOriginTax.plus(originalTaxRounded.times(quantity));
927
+ if (product.tax_fee_rounding_remainder === undefined) {
928
+ product.tax_fee_rounding_remainder = 0;
929
+ }
756
930
  }
757
931
  } catch (err) {
758
- _iterator14.e(err);
932
+ _iterator17.e(err);
759
933
  } finally {
760
- _iterator14.f();
934
+ _iterator17.f();
761
935
  }
762
936
  return {
763
937
  tax: totalTax.toDecimalPlaces(2, Decimal.ROUND_HALF_UP),
764
938
  originTax: totalOriginTax.toDecimalPlaces(2, Decimal.ROUND_HALF_UP)
765
939
  };
766
940
  }
941
+
942
+ /**
943
+ * 整单折扣(shop_discount)按各行 selling 合计占比均摊到 payment 维度。
944
+ *
945
+ * 文档口径:
946
+ * - payment_price = selling_price − 整单折扣均摊;rate = payment/selling(全行同一 rate);
947
+ * - 附加费不受整单折扣影响(基数仍用 selling,本函数写 main_product_attached_bundle_selling_price);
948
+ * - 税费受整单折扣影响(基数用 payment,本函数写 main_product_attached_bundle_payment_price 与
949
+ * 原价子商品 bundle_payment_price)。
950
+ *
951
+ * 与后端一致:bundle_payment_price = bundle_selling_price × rate,
952
+ * main_product_attached_bundle_payment_price = main_product_attached_bundle_selling_price × rate。
953
+ */
954
+ function allocateShopDiscountToPayment(products, productAmount, shopDiscountAmount) {
955
+ var effectiveDiscount = productAmount.lte(0) ? new Decimal(0) : Decimal.max(Decimal.min(productAmount, shopDiscountAmount), 0);
956
+ var allocatedDiscount = new Decimal(0);
957
+ products.forEach(function (product, index) {
958
+ var quantity = new Decimal(getSafeNum(product.num));
959
+ var sellingUnit = getUnitPaymentTotal(product);
960
+ var lineSellingTotal = sellingUnit.times(quantity);
961
+ var lineDiscount = effectiveDiscount.lte(0) ? new Decimal(0) : index === products.length - 1 ? Decimal.max(effectiveDiscount.minus(allocatedDiscount), 0) : toAmountFormat(lineSellingTotal.div(productAmount).times(effectiveDiscount));
962
+ allocatedDiscount = allocatedDiscount.plus(lineDiscount);
963
+ var unitDiscount = quantity.lte(0) ? new Decimal(0) : lineDiscount.div(quantity);
964
+ var paymentUnit = Decimal.max(sellingUnit.minus(unitDiscount), 0);
965
+ var rate = sellingUnit.lte(0) ? new Decimal(1) : toBcScale(paymentUnit.div(sellingUnit), 6);
966
+ var mainSellingTotal = getMainAttachedBundleSellingTotal(product);
967
+ var mainPaymentTotal = toAmountFormat(bcMul(mainSellingTotal, rate, 6));
968
+ product.metadata = _objectSpread(_objectSpread({}, product.metadata || {}), {}, {
969
+ average_discount_amount_rate: rate.toString(),
970
+ main_product_attached_bundle_selling_price: mainSellingTotal.toFixed(2),
971
+ main_product_attached_bundle_payment_price: mainPaymentTotal.toFixed(2)
972
+ });
973
+ var _iterator19 = _createForOfIteratorHelper(product.product_bundle || []),
974
+ _step19;
975
+ try {
976
+ for (_iterator19.s(); !(_step19 = _iterator19.n()).done;) {
977
+ var bundle = _step19.value;
978
+ // 原价子商品:bundle_payment_price = bundle_selling_price × rate
979
+ // markdown 子商品:以净额幅度 × rate(与后端一致,存正净额)
980
+ if (isBundleOriginalPrice(bundle)) {
981
+ var _ref16, _ref17, _bundle$bundle_sellin3;
982
+ var bundleSelling = toDecimal((_ref16 = (_ref17 = (_bundle$bundle_sellin3 = bundle.bundle_selling_price) !== null && _bundle$bundle_sellin3 !== void 0 ? _bundle$bundle_sellin3 : bundle.bundle_sum_price) !== null && _ref17 !== void 0 ? _ref17 : bundle.price) !== null && _ref16 !== void 0 ? _ref16 : 0);
983
+ bundle.bundle_payment_price = toAmountFormat(bcMul(bundleSelling, rate, 6)).toFixed(2);
984
+ continue;
985
+ }
986
+ if (isMarkdownBundle(bundle)) {
987
+ var _bundle$custom_price;
988
+ var magnitude = getBundleSellingMagnitude(bundle);
989
+ bundle.custom_price = (_bundle$custom_price = bundle.custom_price) !== null && _bundle$custom_price !== void 0 ? _bundle$custom_price : bundle.price;
990
+ bundle.price = magnitude.toFixed(2);
991
+ bundle.bundle_selling_price = magnitude.toFixed(2);
992
+ bundle.bundle_payment_price = toAmountFormat(bcMul(magnitude, rate, 6)).toFixed(2);
993
+ }
994
+ }
995
+ } catch (err) {
996
+ _iterator19.e(err);
997
+ } finally {
998
+ _iterator19.f();
999
+ }
1000
+ product.payment_price = paymentUnit.toFixed(2);
1001
+ });
1002
+ }
767
1003
  export function createEmptySalesSummary() {
768
1004
  return {
769
1005
  product_quantity: 0,
@@ -811,6 +1047,16 @@ export function calculateSalesSummary(params) {
811
1047
  var productAmount = summaryProducts.reduce(function (sum, item) {
812
1048
  return sum.plus(getUnitPaymentTotal(item).times(getSafeNum(item.num)));
813
1049
  }, new Decimal(0));
1050
+ var shopDiscountAmount = Decimal.max(new Decimal(Number(shopDiscount) || 0), 0);
1051
+ var hasPersistedSurchargeSnapshot = summaryProducts.some(function (product) {
1052
+ var _getPersistedMainSurc;
1053
+ return (_getPersistedMainSurc = getPersistedMainSurchargeFee(product)) === null || _getPersistedMainSurc === void 0 ? void 0 : _getPersistedMainSurc.gt(0);
1054
+ });
1055
+ // 整单折扣均摊:附加费基数用 selling(不受折扣),税费基数用 payment(受折扣)。
1056
+ // persisted 快照(已下单编辑)优先采信后端回传值,不重算均摊。
1057
+ if (!hasPersistedSurchargeSnapshot) {
1058
+ allocateShopDiscountToPayment(summaryProducts, productAmount, shopDiscountAmount);
1059
+ }
814
1060
  var surchargeServiceItems = buildSurchargeServiceItems(summaryProducts);
815
1061
  getSurcharge({
816
1062
  service: surchargeServiceItems,
@@ -844,9 +1090,8 @@ export function calculateSalesSummary(params) {
844
1090
  return sum.plus(toDecimal(item.tax_fee).times(getSafeNum(item.num)));
845
1091
  }, new Decimal(0));
846
1092
  }
847
- var shopDiscountAmount = Decimal.max(new Decimal(Number(shopDiscount) || 0), 0);
848
- var preTaxExpectAmount = Decimal.max(0, productAmount.plus(surchargeAmount).minus(shopDiscountAmount));
849
- var totalAmount = isPriceIncludeTax === 1 ? preTaxExpectAmount : preTaxExpectAmount.plus(productTaxFee);
1093
+ var orderAmountBeforeDiscount = productAmount.plus(surchargeAmount).plus(isPriceIncludeTax === 1 ? 0 : productTaxFee);
1094
+ var totalAmount = Decimal.max(0, orderAmountBeforeDiscount.minus(shopDiscountAmount));
850
1095
  var deposit = calculateProductsDeposit(summaryProducts);
851
1096
  return {
852
1097
  product_quantity: productQuantity,