@pisell/pisellos 2.2.181 → 2.2.183

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 (173) hide show
  1. package/dist/modules/BookingContext/index.d.ts +37 -0
  2. package/dist/modules/BookingContext/index.js +436 -0
  3. package/dist/modules/BookingContext/types.d.ts +102 -0
  4. package/dist/modules/BookingContext/types.js +51 -0
  5. package/dist/modules/BookingContext/utils/buildCacheItemFromOrderLine.d.ts +16 -0
  6. package/dist/modules/BookingContext/utils/buildCacheItemFromOrderLine.js +276 -0
  7. package/dist/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.d.ts +8 -0
  8. package/dist/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.js +137 -0
  9. package/dist/modules/BookingContext/utils/cacheItemToBookingInput.d.ts +75 -0
  10. package/dist/modules/BookingContext/utils/cacheItemToBookingInput.js +215 -0
  11. package/dist/modules/BookingContext/utils/flexible.d.ts +28 -0
  12. package/dist/modules/BookingContext/utils/flexible.js +56 -0
  13. package/dist/modules/BookingContext/utils/formatResourceList.d.ts +8 -0
  14. package/dist/modules/BookingContext/utils/formatResourceList.js +38 -0
  15. package/dist/modules/BookingContext/utils/index.d.ts +11 -0
  16. package/dist/modules/BookingContext/utils/index.js +11 -0
  17. package/dist/modules/BookingContext/utils/noResource.d.ts +13 -0
  18. package/dist/modules/BookingContext/utils/noResource.js +77 -0
  19. package/dist/modules/BookingContext/utils/productExtend.d.ts +72 -0
  20. package/dist/modules/BookingContext/utils/productExtend.js +339 -0
  21. package/dist/modules/BookingContext/utils/resourceErrors.d.ts +41 -0
  22. package/dist/modules/BookingContext/utils/resourceErrors.js +74 -0
  23. package/dist/modules/BookingContext/utils/resourceSelection.d.ts +7 -0
  24. package/dist/modules/BookingContext/utils/resourceSelection.js +24 -0
  25. package/dist/modules/BookingContext/utils/resources.d.ts +80 -0
  26. package/dist/modules/BookingContext/utils/resources.js +486 -0
  27. package/dist/modules/BookingContext/utils/serviceTimes.d.ts +55 -0
  28. package/dist/modules/BookingContext/utils/serviceTimes.js +151 -0
  29. package/dist/modules/BookingContext/utils/timeSlices.d.ts +42 -0
  30. package/dist/modules/BookingContext/utils/timeSlices.js +100 -0
  31. package/dist/modules/Customer/index.d.ts +6 -0
  32. package/dist/modules/Customer/index.js +129 -83
  33. package/dist/modules/Customer/types.d.ts +2 -0
  34. package/dist/modules/Discount/index.js +4 -3
  35. package/dist/modules/Discount/types.d.ts +1 -0
  36. package/dist/modules/Order/index.d.ts +100 -9
  37. package/dist/modules/Order/index.js +1447 -499
  38. package/dist/modules/Order/types.d.ts +201 -18
  39. package/dist/modules/Order/types.js +31 -0
  40. package/dist/modules/Order/utils/manualProductDiscount.d.ts +106 -0
  41. package/dist/modules/Order/utils/manualProductDiscount.js +210 -0
  42. package/dist/modules/Order/utils.d.ts +83 -1
  43. package/dist/modules/Order/utils.js +405 -61
  44. package/dist/modules/Product/index.d.ts +1 -1
  45. package/dist/modules/Quotation/index.d.ts +0 -1
  46. package/dist/modules/Quotation/index.js +23 -21
  47. package/dist/modules/Rules/index.d.ts +4 -0
  48. package/dist/modules/Rules/index.js +43 -39
  49. package/dist/modules/Rules/types.d.ts +1 -0
  50. package/dist/modules/SalesSummary/index.js +4 -2
  51. package/dist/modules/SalesSummary/utils.js +21 -7
  52. package/dist/modules/Schedule/index.js +6 -2
  53. package/dist/modules/index.d.ts +1 -0
  54. package/dist/modules/index.js +2 -1
  55. package/dist/server/index.js +87 -29
  56. package/dist/server/modules/order/index.d.ts +23 -0
  57. package/dist/server/modules/order/index.js +123 -46
  58. package/dist/server/modules/order/utils/filterOrders.js +20 -6
  59. package/dist/server/modules/products/index.d.ts +1 -1
  60. package/dist/server/modules/products/index.js +130 -113
  61. package/dist/server/modules/schedule/index.js +13 -6
  62. package/dist/solution/BaseSales/index.d.ts +89 -7
  63. package/dist/solution/BaseSales/index.js +1504 -380
  64. package/dist/solution/BaseSales/types.d.ts +67 -1
  65. package/dist/solution/BaseSales/types.js +3 -1
  66. package/dist/solution/BaseSales/utils/cartPromotion.d.ts +275 -0
  67. package/dist/solution/BaseSales/utils/cartPromotion.js +1258 -0
  68. package/dist/solution/BaseSales/utils/quotationPrice.d.ts +7 -0
  69. package/dist/solution/BaseSales/utils/quotationPrice.js +237 -0
  70. package/dist/solution/BaseSales/utils/transformBaseProductToOrderProduct.js +68 -16
  71. package/dist/solution/BaseSales/utils.js +46 -8
  72. package/dist/solution/BookingByStep/index.d.ts +1 -1
  73. package/dist/solution/BookingTicket/index.d.ts +151 -1
  74. package/dist/solution/BookingTicket/index.js +848 -184
  75. package/dist/solution/BookingTicket/types.d.ts +2 -0
  76. package/dist/solution/BookingTicket/types.js +3 -0
  77. package/dist/solution/BookingTicket/utils/addProductDecision.d.ts +113 -0
  78. package/dist/solution/BookingTicket/utils/addProductDecision.js +346 -0
  79. package/dist/solution/BookingTicket/utils/cartView.js +4 -2
  80. package/dist/solution/ScanOrder/index.d.ts +9 -3
  81. package/dist/solution/ScanOrder/index.js +231 -128
  82. package/dist/solution/ScanOrder/utils.js +46 -8
  83. package/dist/solution/VenueBooking/index.d.ts +5 -2
  84. package/dist/solution/VenueBooking/index.js +103 -55
  85. package/dist/solution/VenueBooking/utils/slotMerge.d.ts +4 -0
  86. package/dist/solution/VenueBooking/utils/slotMerge.js +10 -0
  87. package/lib/modules/BookingContext/index.d.ts +37 -0
  88. package/lib/modules/BookingContext/index.js +297 -0
  89. package/lib/modules/BookingContext/types.d.ts +102 -0
  90. package/lib/modules/BookingContext/types.js +34 -0
  91. package/lib/modules/BookingContext/utils/buildCacheItemFromOrderLine.d.ts +16 -0
  92. package/lib/modules/BookingContext/utils/buildCacheItemFromOrderLine.js +271 -0
  93. package/lib/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.d.ts +8 -0
  94. package/lib/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.js +141 -0
  95. package/lib/modules/BookingContext/utils/cacheItemToBookingInput.d.ts +75 -0
  96. package/lib/modules/BookingContext/utils/cacheItemToBookingInput.js +198 -0
  97. package/lib/modules/BookingContext/utils/flexible.d.ts +28 -0
  98. package/lib/modules/BookingContext/utils/flexible.js +80 -0
  99. package/lib/modules/BookingContext/utils/formatResourceList.d.ts +8 -0
  100. package/lib/modules/BookingContext/utils/formatResourceList.js +50 -0
  101. package/lib/modules/BookingContext/utils/index.d.ts +11 -0
  102. package/lib/modules/BookingContext/utils/index.js +43 -0
  103. package/lib/modules/BookingContext/utils/noResource.d.ts +13 -0
  104. package/lib/modules/BookingContext/utils/noResource.js +90 -0
  105. package/lib/modules/BookingContext/utils/productExtend.d.ts +72 -0
  106. package/lib/modules/BookingContext/utils/productExtend.js +283 -0
  107. package/lib/modules/BookingContext/utils/resourceErrors.d.ts +41 -0
  108. package/lib/modules/BookingContext/utils/resourceErrors.js +80 -0
  109. package/lib/modules/BookingContext/utils/resourceSelection.d.ts +7 -0
  110. package/lib/modules/BookingContext/utils/resourceSelection.js +46 -0
  111. package/lib/modules/BookingContext/utils/resources.d.ts +80 -0
  112. package/lib/modules/BookingContext/utils/resources.js +377 -0
  113. package/lib/modules/BookingContext/utils/serviceTimes.d.ts +55 -0
  114. package/lib/modules/BookingContext/utils/serviceTimes.js +162 -0
  115. package/lib/modules/BookingContext/utils/timeSlices.d.ts +42 -0
  116. package/lib/modules/BookingContext/utils/timeSlices.js +124 -0
  117. package/lib/modules/Customer/index.d.ts +6 -0
  118. package/lib/modules/Customer/index.js +26 -11
  119. package/lib/modules/Customer/types.d.ts +2 -0
  120. package/lib/modules/Discount/index.js +5 -1
  121. package/lib/modules/Discount/types.d.ts +1 -0
  122. package/lib/modules/Order/index.d.ts +100 -9
  123. package/lib/modules/Order/index.js +751 -160
  124. package/lib/modules/Order/types.d.ts +201 -18
  125. package/lib/modules/Order/types.js +1 -0
  126. package/lib/modules/Order/utils/manualProductDiscount.d.ts +106 -0
  127. package/lib/modules/Order/utils/manualProductDiscount.js +206 -0
  128. package/lib/modules/Order/utils.d.ts +83 -1
  129. package/lib/modules/Order/utils.js +340 -22
  130. package/lib/modules/Product/index.d.ts +1 -1
  131. package/lib/modules/Quotation/index.d.ts +0 -1
  132. package/lib/modules/Quotation/index.js +18 -15
  133. package/lib/modules/Rules/index.d.ts +4 -0
  134. package/lib/modules/Rules/index.js +26 -27
  135. package/lib/modules/Rules/types.d.ts +1 -0
  136. package/lib/modules/SalesSummary/index.js +4 -2
  137. package/lib/modules/SalesSummary/utils.js +21 -7
  138. package/lib/modules/Schedule/index.js +3 -1
  139. package/lib/modules/index.d.ts +1 -0
  140. package/lib/modules/index.js +3 -1
  141. package/lib/server/index.js +41 -4
  142. package/lib/server/modules/order/index.d.ts +23 -0
  143. package/lib/server/modules/order/index.js +46 -14
  144. package/lib/server/modules/order/utils/filterOrders.js +12 -4
  145. package/lib/server/modules/products/index.d.ts +1 -1
  146. package/lib/server/modules/products/index.js +30 -20
  147. package/lib/server/modules/schedule/index.js +2 -1
  148. package/lib/solution/BaseSales/index.d.ts +89 -7
  149. package/lib/solution/BaseSales/index.js +743 -23
  150. package/lib/solution/BaseSales/types.d.ts +67 -1
  151. package/lib/solution/BaseSales/types.js +3 -1
  152. package/lib/solution/BaseSales/utils/cartPromotion.d.ts +275 -0
  153. package/lib/solution/BaseSales/utils/cartPromotion.js +836 -0
  154. package/lib/solution/BaseSales/utils/quotationPrice.d.ts +7 -0
  155. package/lib/solution/BaseSales/utils/quotationPrice.js +277 -0
  156. package/lib/solution/BaseSales/utils/transformBaseProductToOrderProduct.js +71 -12
  157. package/lib/solution/BaseSales/utils.js +46 -6
  158. package/lib/solution/BookingByStep/index.d.ts +1 -1
  159. package/lib/solution/BookingTicket/index.d.ts +151 -1
  160. package/lib/solution/BookingTicket/index.js +389 -9
  161. package/lib/solution/BookingTicket/types.d.ts +2 -0
  162. package/lib/solution/BookingTicket/types.js +6 -0
  163. package/lib/solution/BookingTicket/utils/addProductDecision.d.ts +113 -0
  164. package/lib/solution/BookingTicket/utils/addProductDecision.js +318 -0
  165. package/lib/solution/BookingTicket/utils/cartView.js +4 -2
  166. package/lib/solution/ScanOrder/index.d.ts +9 -3
  167. package/lib/solution/ScanOrder/index.js +147 -66
  168. package/lib/solution/ScanOrder/utils.js +46 -6
  169. package/lib/solution/VenueBooking/index.d.ts +5 -2
  170. package/lib/solution/VenueBooking/index.js +50 -14
  171. package/lib/solution/VenueBooking/utils/slotMerge.d.ts +4 -0
  172. package/lib/solution/VenueBooking/utils/slotMerge.js +10 -0
  173. package/package.json +1 -1
@@ -34,13 +34,16 @@ __export(BaseSales_exports, {
34
34
  });
35
35
  module.exports = __toCommonJS(BaseSales_exports);
36
36
  var import_BaseModule = require("../../modules/BaseModule");
37
+ var import_types = require("./types");
37
38
  var import_Order = require("../../modules/Order");
38
39
  var import_decimal = __toESM(require("decimal.js"));
39
- var import_types = require("../BookingByStep/types");
40
+ var import_types2 = require("../BookingByStep/types");
40
41
  var import_utils = require("../../modules/Order/utils");
41
42
  var import_dayjs = __toESM(require("dayjs"));
42
43
  __reExport(BaseSales_exports, require("./types"), module.exports);
44
+ var import_Quotation = require("../../modules/Quotation");
43
45
  var import_transformBaseProductToOrderProduct = require("./utils/transformBaseProductToOrderProduct");
46
+ var import_quotationPrice = require("./utils/quotationPrice");
44
47
  var BaseSalesImpl = class extends import_BaseModule.BaseModule {
45
48
  constructor(name, version) {
46
49
  super(name, version);
@@ -48,20 +51,21 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
48
51
  this.defaultVersion = "1.0.0";
49
52
  this.isSolution = true;
50
53
  this.initializeOptions = {};
51
- // LoggerManager 实例
52
54
  this.store = {
53
55
  order: void 0
54
56
  };
55
57
  this.otherParams = {};
56
58
  // 当前缓存中已加载的最新 SalesDetail;只读 getter 走它对外暴露详情数据。
57
59
  this.currentSalesDetail = null;
60
+ this.discountConfigCacheKey = null;
61
+ this.hasManualDiscountSelection = false;
58
62
  }
59
63
  /**
60
64
  * 子类可覆盖此方法以追加/收敛要注册的子模块。
61
- * 默认包含通用销售模块:products / order / salesSummary / schedule / payment。
65
+ * 默认包含通用销售模块:products / order / salesSummary / schedule / payment / quotation
62
66
  */
63
67
  getRegisteredModuleNames() {
64
- return ["products", "order", "salesSummary", "schedule", "payment"];
68
+ return ["products", "order", "salesSummary", "schedule", "payment", "quotation"];
65
69
  }
66
70
  /**
67
71
  * 记录信息日志
@@ -105,13 +109,182 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
105
109
  get payment() {
106
110
  return this.store.payment;
107
111
  }
112
+ getCurrentOrderCustomerId() {
113
+ var _a, _b;
114
+ const tempOrder = (_b = (_a = this.store.order) == null ? void 0 : _a.getTempOrder) == null ? void 0 : _b.call(_a);
115
+ const customerId = tempOrder == null ? void 0 : tempOrder.customer_id;
116
+ if (customerId === null || customerId === void 0)
117
+ return void 0;
118
+ return customerId;
119
+ }
120
+ applyQuotationScheduleResolver(quotation) {
121
+ const scheduleModule = this.store.schedule;
122
+ if (!scheduleModule)
123
+ return;
124
+ quotation.setScheduleResolver((id) => {
125
+ var _a;
126
+ const schedules = ((_a = scheduleModule.getScheduleListByIds) == null ? void 0 : _a.call(scheduleModule, [id])) || [];
127
+ return schedules[0];
128
+ });
129
+ }
130
+ async loadQuotationForPriceQuery(params) {
131
+ var _a;
132
+ if (!params.quotation)
133
+ return;
134
+ this.applyQuotationScheduleResolver(params.quotation);
135
+ await params.quotation.loadQuotations({
136
+ channel: params.channel || ((_a = this.otherParams) == null ? void 0 : _a.channel),
137
+ customer_id: params.customer_id
138
+ });
139
+ }
140
+ getPriceQueryProductId(product) {
141
+ const productId = Number((product == null ? void 0 : product.product_id) ?? (product == null ? void 0 : product.id));
142
+ if (!Number.isFinite(productId))
143
+ return null;
144
+ return productId;
145
+ }
146
+ getPriceQuerySchedule(params) {
147
+ const booking = params.booking || {};
148
+ if (typeof booking.start_date === "string" && booking.start_date.trim()) {
149
+ const scheduleDate = booking.start_date.trim();
150
+ const startTime = typeof booking.start_time === "string" && booking.start_time.trim() ? booking.start_time.trim() : "00:00:00";
151
+ const datetime = (0, import_dayjs.default)(`${scheduleDate} ${startTime}`);
152
+ return {
153
+ schedule_date: scheduleDate,
154
+ schedule_datetime: datetime.isValid() ? datetime.format("YYYY-MM-DD HH:mm:ss") : `${scheduleDate} ${startTime}`
155
+ };
156
+ }
157
+ if (typeof params.datetime === "string" && params.datetime.trim()) {
158
+ const datetime = (0, import_dayjs.default)(params.datetime.trim());
159
+ if (datetime.isValid()) {
160
+ return {
161
+ schedule_date: datetime.format("YYYY-MM-DD"),
162
+ schedule_datetime: datetime.format("YYYY-MM-DD HH:mm:ss")
163
+ };
164
+ }
165
+ }
166
+ const now = (0, import_dayjs.default)();
167
+ return {
168
+ schedule_date: now.format("YYYY-MM-DD"),
169
+ schedule_datetime: now.format("YYYY-MM-DD HH:mm:ss")
170
+ };
171
+ }
172
+ async loadProductForPriceQuery(params, customerId) {
173
+ var _a, _b;
174
+ const product = params.product || {};
175
+ const productId = this.getPriceQueryProductId(product);
176
+ if (productId === null || !this.request)
177
+ return null;
178
+ const schedule = this.getPriceQuerySchedule(params);
179
+ try {
180
+ const response = await this.request.post("/product/query", {
181
+ ids: [productId],
182
+ open_quotation: 1,
183
+ open_bundle: 1,
184
+ status: "published",
185
+ num: 1,
186
+ skip: 1,
187
+ customer_id: customerId,
188
+ schedule_date: schedule.schedule_date,
189
+ schedule_datetime: schedule.schedule_datetime,
190
+ application_code: params.channel || ((_a = this.otherParams) == null ? void 0 : _a.channel)
191
+ }, {
192
+ osServer: true,
193
+ customToast: () => {
194
+ }
195
+ });
196
+ const list = ((_b = response == null ? void 0 : response.data) == null ? void 0 : _b.list) || (response == null ? void 0 : response.list) || [];
197
+ if (!Array.isArray(list))
198
+ return null;
199
+ return list.find((item) => Number((item == null ? void 0 : item.id) ?? (item == null ? void 0 : item.product_id)) === productId) || null;
200
+ } catch (error) {
201
+ this.logWarning("loadProductForPriceQuery: 商品重查失败,使用入参 fallback", {
202
+ productId,
203
+ error: error instanceof Error ? error.message : String(error)
204
+ });
205
+ return null;
206
+ }
207
+ }
208
+ getAuthoritativeBundleItems(product) {
209
+ if (Array.isArray(product == null ? void 0 : product.product_bundle))
210
+ return product.product_bundle;
211
+ const groups = Array.isArray(product == null ? void 0 : product.bundle_group) ? product.bundle_group : Array.isArray(product == null ? void 0 : product.bundleGroup) ? product.bundleGroup : [];
212
+ return groups.flatMap((group) => {
213
+ const items = Array.isArray(group == null ? void 0 : group.bundle_item) ? group.bundle_item : Array.isArray(group == null ? void 0 : group.bundleItem) ? group.bundleItem : [];
214
+ return items.map((item) => ({
215
+ ...item,
216
+ group_id: (item == null ? void 0 : item.group_id) ?? (group == null ? void 0 : group.id),
217
+ bundle_group_id: (item == null ? void 0 : item.bundle_group_id) ?? (item == null ? void 0 : item.group_id) ?? (group == null ? void 0 : group.id)
218
+ }));
219
+ });
220
+ }
221
+ findAuthoritativeBundleItem(bundle, candidates) {
222
+ const bundleId = bundle.bundle_id ?? bundle.id;
223
+ if (bundleId != null) {
224
+ const matchedById = candidates.find((item) => String((item == null ? void 0 : item.bundle_id) ?? (item == null ? void 0 : item.id)) === String(bundleId));
225
+ if (matchedById)
226
+ return matchedById;
227
+ }
228
+ const productId = bundle.bundle_product_id ?? bundle._bundle_product_id;
229
+ const groupId = bundle.bundle_group_id ?? bundle.group_id;
230
+ return candidates.find((item) => {
231
+ const itemProductId = (item == null ? void 0 : item.bundle_product_id) ?? (item == null ? void 0 : item._bundle_product_id);
232
+ const itemGroupId = (item == null ? void 0 : item.bundle_group_id) ?? (item == null ? void 0 : item.group_id);
233
+ if (productId == null || String(itemProductId) !== String(productId))
234
+ return false;
235
+ if (groupId == null)
236
+ return true;
237
+ return String(itemGroupId) === String(groupId);
238
+ }) || null;
239
+ }
240
+ getAuthoritativeBundleUnitPrice(bundle) {
241
+ return bundle.price ?? bundle.bundle_selling_price ?? bundle.custom_price ?? bundle.product_price ?? bundle.base_price;
242
+ }
243
+ mergeProductForPriceQuery(product, authoritativeProduct) {
244
+ if (!authoritativeProduct)
245
+ return product;
246
+ const selectedBundles = Array.isArray(product.product_bundle) ? product.product_bundle : [];
247
+ const authoritativeBundles = this.getAuthoritativeBundleItems(authoritativeProduct);
248
+ const productBundle = selectedBundles.map((bundle) => {
249
+ const authoritativeBundle = this.findAuthoritativeBundleItem(bundle, authoritativeBundles);
250
+ const unitPrice = authoritativeBundle ? this.getAuthoritativeBundleUnitPrice(authoritativeBundle) : void 0;
251
+ if (unitPrice === void 0 || unitPrice === null || unitPrice === "")
252
+ return bundle;
253
+ return {
254
+ ...bundle,
255
+ price: unitPrice,
256
+ bundle_selling_price: unitPrice,
257
+ base_price: (authoritativeBundle == null ? void 0 : authoritativeBundle.base_price) ?? bundle.base_price,
258
+ product_price: (authoritativeBundle == null ? void 0 : authoritativeBundle.product_price) ?? bundle.product_price
259
+ };
260
+ });
261
+ return {
262
+ ...product,
263
+ ...authoritativeProduct,
264
+ id: product.id ?? authoritativeProduct.id,
265
+ product_id: product.product_id ?? authoritativeProduct.product_id ?? authoritativeProduct.id,
266
+ product_variant_id: product.product_variant_id ?? authoritativeProduct.product_variant_id ?? 0,
267
+ num: product.num ?? product.quantity ?? authoritativeProduct.num,
268
+ quantity: product.quantity ?? product.num ?? authoritativeProduct.quantity,
269
+ product_option_item: product.product_option_item ?? authoritativeProduct.product_option_item,
270
+ product_bundle: productBundle,
271
+ metadata: product.metadata ?? authoritativeProduct.metadata
272
+ };
273
+ }
274
+ getSubmitOrderSalesChannel() {
275
+ var _a;
276
+ return (_a = this.otherParams) == null ? void 0 : _a.channel;
277
+ }
108
278
  /**
109
279
  * 工厂入口:根据子模块名实例化对应模块。
110
280
  * 默认使用 BookingByStep 公共 `createModule`。子类若需引入额外类型(如 customer),
111
281
  * 可覆盖此方法并把未识别的 name 委托给 `super.createSubModule(name)`。
112
282
  */
113
283
  createSubModule(moduleName) {
114
- return (0, import_types.createModule)(moduleName, this.name);
284
+ if (moduleName === "quotation") {
285
+ return new import_Quotation.QuotationModule(`${this.name}_quotation`);
286
+ }
287
+ return (0, import_types2.createModule)(moduleName, this.name);
115
288
  }
116
289
  getSubModuleHooks(moduleName) {
117
290
  var _a, _b;
@@ -140,19 +313,64 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
140
313
  return {};
141
314
  }
142
315
  }
316
+ getAppData(key) {
317
+ var _a, _b, _c, _d, _e, _f;
318
+ const getData = (_b = (_a = this.appPlugin).getData) == null ? void 0 : _b.call(_a);
319
+ if (typeof getData === "function")
320
+ return getData(key);
321
+ const app = this.appPlugin.getApp();
322
+ const coreData = (_f = (_d = (_c = app == null ? void 0 : app.models) == null ? void 0 : _c.getStore) == null ? void 0 : (_e = _d.call(_c)).getDataByModel) == null ? void 0 : _f.call(_e, "core", "core");
323
+ return coreData == null ? void 0 : coreData[key];
324
+ }
325
+ syncAppDataToTempOrder(tempOrder) {
326
+ const isPriceIncludeTax = this.getAppData("is_price_include_tax");
327
+ const taxCountryCode = this.getAppData("tax_country_code");
328
+ const currencyCode = this.getAppData("shop_currency_code");
329
+ const currencySymbol = this.getAppData("shop_symbol");
330
+ let isChanged = false;
331
+ if (isPriceIncludeTax !== void 0) {
332
+ const nextIsPriceIncludeTax = Number(isPriceIncludeTax);
333
+ if ((nextIsPriceIncludeTax === 0 || nextIsPriceIncludeTax === 1) && tempOrder.is_price_include_tax !== nextIsPriceIncludeTax) {
334
+ tempOrder.is_price_include_tax = nextIsPriceIncludeTax;
335
+ isChanged = true;
336
+ }
337
+ }
338
+ if (taxCountryCode !== void 0) {
339
+ const nextTaxCountryCode = String(taxCountryCode || "");
340
+ if (tempOrder.tax_country_code !== nextTaxCountryCode) {
341
+ tempOrder.tax_country_code = nextTaxCountryCode;
342
+ isChanged = true;
343
+ }
344
+ }
345
+ if (currencyCode !== void 0) {
346
+ const nextCurrencyCode = String(currencyCode || "");
347
+ if (tempOrder.currency_code !== nextCurrencyCode) {
348
+ tempOrder.currency_code = nextCurrencyCode;
349
+ isChanged = true;
350
+ }
351
+ }
352
+ if (currencySymbol !== void 0) {
353
+ const nextCurrencySymbol = String(currencySymbol || "");
354
+ if (tempOrder.currency_symbol !== nextCurrencySymbol) {
355
+ tempOrder.currency_symbol = nextCurrencySymbol;
356
+ isChanged = true;
357
+ }
358
+ }
359
+ return isChanged;
360
+ }
143
361
  async initialize(core, options = {}) {
144
- var _a;
362
+ var _a, _b;
145
363
  this.core = core;
146
364
  this.initializeOptions = options || {};
147
365
  this.otherParams = options.otherParams || {};
148
366
  this.cacheId = (_a = this.otherParams) == null ? void 0 : _a.cacheId;
149
367
  this.window = core.getPlugin("window");
150
368
  this.request = core.getPlugin("request");
151
- const appPlugin = core.getPlugin("app");
152
- if (!appPlugin) {
369
+ this.appPlugin = core.getPlugin("app");
370
+ if (!this.appPlugin) {
153
371
  throw new Error("Checkout 解决方案需要 app 插件支持");
154
372
  }
155
- const app = appPlugin.getApp();
373
+ const app = this.appPlugin.getApp();
156
374
  this.logger = app.logger;
157
375
  const targetCacheData = this.readSessionCacheData();
158
376
  const moduleNames = this.getRegisteredModuleNames();
@@ -178,6 +396,11 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
178
396
  if (this.store.schedule) {
179
397
  await this.store.schedule.loadAllSchedule();
180
398
  }
399
+ if (this.store.order && typeof ((_b = this.otherParams) == null ? void 0 : _b.enableTempOrderPersist) === "boolean") {
400
+ this.store.order.setEnableTempOrderPersist(
401
+ this.otherParams.enableTempOrderPersist
402
+ );
403
+ }
181
404
  this.core.effects.emit(`${this.name}:onInited`, {});
182
405
  }
183
406
  async destroy() {
@@ -207,6 +430,7 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
207
430
  throw new Error("order 模块未初始化");
208
431
  const params = typeof orderIdOrParams === "object" ? orderIdOrParams : { orderId: orderIdOrParams };
209
432
  const externalSaleNumber = params.externalSaleNumber || params.external_sale_number;
433
+ debugger;
210
434
  if (!params.forceRemote) {
211
435
  const localRecord = params.orderId !== void 0 ? await this.store.order.getLocalOrderByOrderId(params.orderId) : externalSaleNumber ? await this.store.order.getLocalOrderByExternalSaleNumber(externalSaleNumber) : params.orderNumber ? await this.store.order.getLocalOrderByOrderNumber(params.orderNumber) : null;
212
436
  if (localRecord) {
@@ -286,6 +510,13 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
286
510
  const result = ((_a = this.store.order) == null ? void 0 : _a.getTempOrder()) || null;
287
511
  return result;
288
512
  }
513
+ async replaceTempOrder(tempOrder) {
514
+ if (!this.store.order)
515
+ throw new Error("order 模块未初始化");
516
+ const nextTempOrder = await this.store.order.replaceTempOrder(tempOrder);
517
+ await this.effectsEmit("onTempOrderReplaced", { tempOrder: nextTempOrder });
518
+ return nextTempOrder;
519
+ }
289
520
  getOrderIdentity() {
290
521
  if (!this.store.order)
291
522
  return null;
@@ -311,16 +542,21 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
311
542
  return "";
312
543
  return this.store.order.getTempOrderNote();
313
544
  }
314
- updateTempOrderNote(note) {
545
+ updateTempOrderNote(note, sync = false) {
315
546
  try {
316
547
  if (!this.store.order)
317
548
  throw new Error("order 模块未初始化");
318
- const result = this.store.order.updateTempOrderNote(note);
549
+ const result = this.store.order.updateTempOrderNote(note, sync);
319
550
  return result;
320
551
  } catch (error) {
321
552
  throw error;
322
553
  }
323
554
  }
555
+ updateRemoteOrderNote(orderId, note) {
556
+ if (!this.store.order)
557
+ throw new Error("order 模块未初始化");
558
+ this.store.order.syncTempOrderNoteToRemote(orderId, note);
559
+ }
324
560
  updateTempOrderShopDiscount(amount) {
325
561
  if (!this.store.order)
326
562
  throw new Error("order 模块未初始化");
@@ -331,6 +567,18 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
331
567
  throw new Error("order 模块未初始化");
332
568
  return this.store.order.updateTempOrderContactsInfo(contactsInfo);
333
569
  }
570
+ /** @deprecated OrderModule 不再负责 tempOrder localStorage 持久化。 */
571
+ setEnableTempOrderPersist(enabled) {
572
+ if (!this.store.order)
573
+ throw new Error("order 模块未初始化");
574
+ this.store.order.setEnableTempOrderPersist(enabled);
575
+ }
576
+ /** 是否允许 saveDraft 写入 IndexedDB 草稿。 */
577
+ isTempOrderPersistEnabled() {
578
+ if (!this.store.order)
579
+ return false;
580
+ return this.store.order.isTempOrderPersistEnabled();
581
+ }
334
582
  ensureTempOrder() {
335
583
  if (!this.store.order)
336
584
  throw new Error("order 模块未初始化");
@@ -342,6 +590,10 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
342
590
  if (!this.store.order)
343
591
  throw new Error("order 模块未初始化");
344
592
  const tempOrder = await this.store.order.addNewOrder();
593
+ if (this.syncAppDataToTempOrder(tempOrder) && this.isTempOrderPersistEnabled()) {
594
+ this.store.order.persistTempOrder();
595
+ await this.store.order.saveDraft();
596
+ }
345
597
  return tempOrder;
346
598
  } catch (error) {
347
599
  throw error;
@@ -360,6 +612,43 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
360
612
  throw new Error("scanOrder 解决方案需要 order 模块支持");
361
613
  }
362
614
  const tempOrder = this.store.order.restoreOrder();
615
+ this.currentSalesDetail = null;
616
+ this.discountConfigCacheKey = null;
617
+ this.hasManualDiscountSelection = false;
618
+ await this.effectsEmit("onTempOrderReplaced", { tempOrder });
619
+ if (this.syncAppDataToTempOrder(tempOrder) && this.isTempOrderPersistEnabled()) {
620
+ this.store.order.persistTempOrder();
621
+ await this.store.order.saveDraft();
622
+ }
623
+ return tempOrder;
624
+ } catch (error) {
625
+ throw error;
626
+ }
627
+ }
628
+ /**
629
+ * 仅清空 tempOrder 购物车行(products / bookings / discount_list)。
630
+ * 同步清空内存中的 salesDetail 商品/预约视图,避免编辑态仍读到旧 bookings。
631
+ */
632
+ async clearOrderCartLines() {
633
+ try {
634
+ if (!this.store.order)
635
+ throw new Error("order 模块未初始化");
636
+ const tempOrder = await this.store.order.clearOrderCartLines();
637
+ if (this.currentSalesDetail) {
638
+ this.currentSalesDetail = {
639
+ ...this.currentSalesDetail,
640
+ products: [],
641
+ bookings: [],
642
+ rootBooking: null,
643
+ bookingsByProductUid: {},
644
+ discount_list: []
645
+ };
646
+ }
647
+ await this.effectsEmit("onTempOrderReplaced", { tempOrder });
648
+ if (this.syncAppDataToTempOrder(tempOrder) && this.isTempOrderPersistEnabled()) {
649
+ this.store.order.persistTempOrder();
650
+ await this.store.order.saveDraft();
651
+ }
363
652
  return tempOrder;
364
653
  } catch (error) {
365
654
  throw error;
@@ -381,6 +670,246 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
381
670
  throw error;
382
671
  }
383
672
  }
673
+ getHistoricalProductDiscountList(products) {
674
+ const historyDiscountList = [];
675
+ products.forEach((item) => {
676
+ if (!this.isPersistedOrderProduct(item))
677
+ return;
678
+ (item.discount_list || []).forEach((discount) => {
679
+ var _a, _b, _c, _d, _e, _f;
680
+ const discountId = ((_a = discount.discount) == null ? void 0 : _a.resource_id) || discount.id;
681
+ if (!discountId || !["good_pass", "discount_card"].includes(discount.type))
682
+ return;
683
+ const quantity = item.quantity || item.num || item.product_quantity || 1;
684
+ const savedAmount = new import_decimal.default(discount.amount || 0).times(quantity).plus(((_b = discount == null ? void 0 : discount.metadata) == null ? void 0 : _b.product_discount_difference) || 0);
685
+ const index = historyDiscountList.findIndex((n) => n.id === discountId);
686
+ if (index !== -1) {
687
+ historyDiscountList[index] = {
688
+ ...historyDiscountList[index],
689
+ amount: new import_decimal.default(historyDiscountList[index].amount || 0).plus(discount.amount || 0).toNumber(),
690
+ savedAmount: savedAmount.plus(historyDiscountList[index].savedAmount || 0).toNumber()
691
+ };
692
+ return;
693
+ }
694
+ historyDiscountList.push({
695
+ ...discount,
696
+ id: discountId,
697
+ tag: discount.tag || discount.type,
698
+ name: discount.name || ((_d = (_c = discount.discount) == null ? void 0 : _c.title) == null ? void 0 : _d.auto),
699
+ format_title: ((_e = discount.discount) == null ? void 0 : _e.title) || discount.format_title,
700
+ isEditMode: true,
701
+ limited_relation_product_data: {},
702
+ savedAmount: savedAmount.toNumber(),
703
+ isAvailable: true,
704
+ isDisabled: true,
705
+ isSelected: true,
706
+ product_id: ((_f = discount.discount) == null ? void 0 : _f.product_id) || discount.product_id
707
+ });
708
+ });
709
+ });
710
+ return historyDiscountList;
711
+ }
712
+ isPersistedOrderProduct(product) {
713
+ const orderDetailId = product.order_detail_id ?? product.orderDetailId;
714
+ if (orderDetailId !== void 0 && orderDetailId !== null && orderDetailId !== "") {
715
+ return true;
716
+ }
717
+ const bookingId = product.booking_id ?? product.bookingId;
718
+ return bookingId !== void 0 && bookingId !== null && bookingId !== "";
719
+ }
720
+ mergeHistoricalDiscountList(discountList, products) {
721
+ const historyDiscountList = this.getHistoricalProductDiscountList(products);
722
+ const historyIds = new Set(historyDiscountList.map((discount) => discount.id));
723
+ return [
724
+ ...historyDiscountList,
725
+ ...(discountList || []).filter((discount) => !historyIds.has(discount.id))
726
+ ];
727
+ }
728
+ mergeCurrentDiscountSelectionState(discountList, currentDiscountList) {
729
+ if (!currentDiscountList.length)
730
+ return discountList;
731
+ const currentDiscountMap = new Map(
732
+ currentDiscountList.map((discount) => [discount.id, discount])
733
+ );
734
+ return discountList.map((discount) => {
735
+ const currentDiscount = currentDiscountMap.get(discount.id);
736
+ if (!currentDiscount)
737
+ return discount;
738
+ if (currentDiscount.isManualSelect) {
739
+ return {
740
+ ...discount,
741
+ isSelected: false,
742
+ isManualSelect: true
743
+ };
744
+ }
745
+ if (currentDiscount.isSelected) {
746
+ return {
747
+ ...discount,
748
+ isSelected: true,
749
+ isManualSelect: false
750
+ };
751
+ }
752
+ return discount;
753
+ });
754
+ }
755
+ async loadDiscountConfig(params) {
756
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
757
+ if (!this.store.order)
758
+ throw new Error("order 模块未初始化");
759
+ const tempOrder = this.store.order.ensureTempOrder();
760
+ const orderStore = this.store.order.store || {};
761
+ const discountModule = orderStore.discount;
762
+ const rulesModule = orderStore.rules;
763
+ const orderId = tempOrder.order_id || ((_b = (_a = this.getOrderIdentity) == null ? void 0 : _a.call(this)) == null ? void 0 : _b.orderId);
764
+ const customerId = Number(
765
+ (params == null ? void 0 : params.customerId) || tempOrder.customer_id || ((_c = tempOrder.customer) == null ? void 0 : _c.id) || ((_e = (_d = tempOrder._extend) == null ? void 0 : _d.customerSnapshot) == null ? void 0 : _e.id) || ((_g = (_f = tempOrder._extend) == null ? void 0 : _f.customerSnapshot) == null ? void 0 : _g.customer_id) || 0
766
+ );
767
+ const currentDiscountList = ((_h = discountModule == null ? void 0 : discountModule.getDiscountList) == null ? void 0 : _h.call(discountModule)) || [];
768
+ const originalDiscountList = ((_i = discountModule == null ? void 0 : discountModule.getOriginalDiscountList) == null ? void 0 : _i.call(discountModule)) || [];
769
+ const hasManualDiscountSelection = this.hasManualDiscountSelection || currentDiscountList.some((discount) => discount.isManualSelect);
770
+ const discountAction = (params == null ? void 0 : params.action) || (orderId ? "edit" : "create");
771
+ const discountConfigCacheKey = [
772
+ customerId,
773
+ discountAction,
774
+ Number(orderId) || 0
775
+ ].join(":");
776
+ let preparedDiscountList = [];
777
+ if (customerId && customerId !== 1) {
778
+ if (this.discountConfigCacheKey === discountConfigCacheKey) {
779
+ preparedDiscountList = originalDiscountList;
780
+ } else {
781
+ preparedDiscountList = await this.store.order.loadDiscountConfig({
782
+ customerId,
783
+ action: discountAction,
784
+ orderId: Number(orderId) || void 0,
785
+ apply: false
786
+ });
787
+ this.discountConfigCacheKey = discountConfigCacheKey;
788
+ await ((_j = discountModule == null ? void 0 : discountModule.setOriginalDiscountList) == null ? void 0 : _j.call(discountModule, preparedDiscountList));
789
+ }
790
+ }
791
+ if (hasManualDiscountSelection && currentDiscountList.length) {
792
+ return {
793
+ productList: tempOrder.products || [],
794
+ discountList: currentDiscountList
795
+ };
796
+ }
797
+ let nextDiscountList = this.mergeHistoricalDiscountList(
798
+ preparedDiscountList || ((_k = discountModule == null ? void 0 : discountModule.getDiscountList) == null ? void 0 : _k.call(discountModule)) || [],
799
+ tempOrder.products || []
800
+ );
801
+ nextDiscountList = this.mergeCurrentDiscountSelectionState(
802
+ nextDiscountList,
803
+ currentDiscountList
804
+ );
805
+ await (discountModule == null ? void 0 : discountModule.setDiscountList(nextDiscountList));
806
+ if (rulesModule) {
807
+ const holders = ((_l = tempOrder.holder) == null ? void 0 : _l.form_record_id) ? [{ form_record_id: tempOrder.holder.form_record_id }] : [];
808
+ const result = rulesModule.calcDiscount({
809
+ productList: tempOrder.products,
810
+ discountList: nextDiscountList,
811
+ holders,
812
+ isFormSubject: !!((_m = tempOrder.holder) == null ? void 0 : _m.type) && tempOrder.holder.type === "form",
813
+ orderTotalAmount: Number(((_n = orderStore.summary) == null ? void 0 : _n.total_amount) || 0)
814
+ }) || { productList: tempOrder.products, discountList: nextDiscountList };
815
+ if (result.productList) {
816
+ tempOrder.products = result.productList;
817
+ }
818
+ for (const product of tempOrder.products || []) {
819
+ const productUid = (_o = product.metadata) == null ? void 0 : _o.unique_identification_number;
820
+ const runtimeOrigin = productUid ? (_r = (_q = (_p = tempOrder._extend) == null ? void 0 : _p.productsByUid) == null ? void 0 : _q[productUid]) == null ? void 0 : _r.origin : void 0;
821
+ if ((runtimeOrigin == null ? void 0 : runtimeOrigin.isManualDiscount) || ((_s = product.metadata) == null ? void 0 : _s.is_manual_discount))
822
+ continue;
823
+ const totalPerUnitDiscount = (product.discount_list || []).reduce(
824
+ (sum, pd) => sum + Number(pd.amount || 0),
825
+ 0
826
+ );
827
+ const optionSum = (0, import_utils.sumOptionUnitPrice)(product.product_option_item);
828
+ const sourcePrice = ((_t = product.metadata) == null ? void 0 : _t.source_product_price) ?? (((_u = product.metadata) == null ? void 0 : _u.main_product_original_price) != null ? new import_decimal.default(Number(product.metadata.main_product_original_price) || 0).minus(optionSum).toFixed(2) : product.original_price ?? "0");
829
+ const newSourceSellingPrice = new import_decimal.default(Number(sourcePrice) || 0).minus(totalPerUnitDiscount).toDecimalPlaces(2).toString();
830
+ const newMainSellingPrice = new import_decimal.default(Number(newSourceSellingPrice) || 0).plus(optionSum).toDecimalPlaces(2).toFixed(2);
831
+ if (product.metadata) {
832
+ product.metadata.main_product_selling_price = newMainSellingPrice;
833
+ product.metadata.price_schema_version = 2;
834
+ }
835
+ product.selling_price = (0, import_utils.composeLinePrice)({
836
+ mainPrice: newMainSellingPrice,
837
+ bundle: product.product_bundle
838
+ });
839
+ }
840
+ if (result.discountList) {
841
+ nextDiscountList = this.mergeHistoricalDiscountList(
842
+ result.discountList,
843
+ tempOrder.products || []
844
+ );
845
+ import_Order.OrderModule.populateSavedAmounts(tempOrder.products, nextDiscountList);
846
+ await (discountModule == null ? void 0 : discountModule.setDiscountList(nextDiscountList));
847
+ tempOrder.discount_list = nextDiscountList.filter((discount) => discount.isSelected);
848
+ }
849
+ }
850
+ const summary = await this.store.order.recalculateSummary({ createIfMissing: true });
851
+ this.store.order.persistTempOrder();
852
+ await this.effectsEmit("onDiscountApplied", {
853
+ productList: tempOrder.products || [],
854
+ discountList: nextDiscountList,
855
+ selectedDiscountList: tempOrder.discount_list || [],
856
+ tempOrder
857
+ });
858
+ return {
859
+ productList: tempOrder.products || [],
860
+ discountList: nextDiscountList
861
+ };
862
+ }
863
+ async bestDiscount(cb) {
864
+ var _a, _b, _c, _d, _e;
865
+ if (!this.store.order)
866
+ throw new Error("order 模块未初始化");
867
+ const tempOrder = this.store.order.ensureTempOrder();
868
+ const orderStore = this.store.order.store || {};
869
+ const discountModule = orderStore.discount;
870
+ const rulesModule = orderStore.rules;
871
+ const originalDiscountList = ((_a = discountModule == null ? void 0 : discountModule.getOriginalDiscountList) == null ? void 0 : _a.call(discountModule)) || this.getDiscountList();
872
+ const currentDiscountList = ((_b = discountModule == null ? void 0 : discountModule.getDiscountList) == null ? void 0 : _b.call(discountModule)) || [];
873
+ let nextDiscountList = this.mergeHistoricalDiscountList(
874
+ originalDiscountList || [],
875
+ tempOrder.products || []
876
+ );
877
+ let result = {
878
+ productList: tempOrder.products || [],
879
+ discountList: nextDiscountList
880
+ };
881
+ await (discountModule == null ? void 0 : discountModule.setDiscountList(nextDiscountList));
882
+ if (rulesModule) {
883
+ const holders = ((_c = tempOrder.holder) == null ? void 0 : _c.form_record_id) ? [{ form_record_id: tempOrder.holder.form_record_id }] : [];
884
+ result = rulesModule.calcDiscount({
885
+ productList: tempOrder.products,
886
+ discountList: nextDiscountList,
887
+ holders,
888
+ isFormSubject: !!((_d = tempOrder.holder) == null ? void 0 : _d.type) && tempOrder.holder.type === "form",
889
+ orderTotalAmount: Number(((_e = orderStore.summary) == null ? void 0 : _e.total_amount) || 0)
890
+ }) || result;
891
+ if (result.productList) {
892
+ tempOrder.products = result.productList;
893
+ }
894
+ if (result.discountList) {
895
+ nextDiscountList = this.mergeHistoricalDiscountList(
896
+ result.discountList,
897
+ tempOrder.products || []
898
+ );
899
+ import_Order.OrderModule.populateSavedAmounts(tempOrder.products, nextDiscountList);
900
+ await (discountModule == null ? void 0 : discountModule.setDiscountList(nextDiscountList));
901
+ tempOrder.discount_list = nextDiscountList.filter((discount) => discount.isSelected);
902
+ result = {
903
+ productList: tempOrder.products,
904
+ discountList: nextDiscountList
905
+ };
906
+ }
907
+ }
908
+ await this.store.order.recalculateSummary({ createIfMissing: true });
909
+ this.store.order.persistTempOrder();
910
+ cb == null ? void 0 : cb(result);
911
+ return result;
912
+ }
384
913
  getDiscountList() {
385
914
  if (!this.store.order)
386
915
  return [];
@@ -418,6 +947,7 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
418
947
  isManualSelect: !params.isSelected
419
948
  } : d
420
949
  );
950
+ this.hasManualDiscountSelection = true;
421
951
  const tempOrder = this.store.order.ensureTempOrder();
422
952
  const orderStore = this.store.order.store || {};
423
953
  const discountModule = orderStore.discount;
@@ -450,6 +980,7 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
450
980
  for (const d of nextDiscountList) {
451
981
  if (d.isSelected && !beforeSelectedIds.has(d.id)) {
452
982
  d.isSelected = false;
983
+ d.isManualSelect = true;
453
984
  }
454
985
  }
455
986
  }
@@ -490,6 +1021,40 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
490
1021
  tempOrder.discount_list = (nextDiscountList || []).filter((d) => d.isSelected);
491
1022
  await this.store.order.recalculateSummary({ createIfMissing: true });
492
1023
  this.store.order.persistTempOrder();
1024
+ this.effectsEmit("onDiscountApplied", {
1025
+ productList: tempOrder.products,
1026
+ discountList: nextDiscountList,
1027
+ selectedDiscountList: tempOrder.discount_list,
1028
+ tempOrder
1029
+ });
1030
+ } catch (error) {
1031
+ throw error;
1032
+ }
1033
+ }
1034
+ async applyDiscountCalculationResult(result) {
1035
+ try {
1036
+ if (!this.store.order)
1037
+ throw new Error("order 模块未初始化");
1038
+ if (!result)
1039
+ return;
1040
+ const tempOrder = this.store.order.ensureTempOrder();
1041
+ const orderStore = this.store.order.store || {};
1042
+ const discountModule = orderStore.discount;
1043
+ if (result.productList) {
1044
+ tempOrder.products = result.productList;
1045
+ }
1046
+ if (result.discountList) {
1047
+ import_Order.OrderModule.populateSavedAmounts(
1048
+ tempOrder.products,
1049
+ result.discountList
1050
+ );
1051
+ await (discountModule == null ? void 0 : discountModule.setDiscountList(result.discountList));
1052
+ tempOrder.discount_list = result.discountList.filter(
1053
+ (discount) => discount.isSelected
1054
+ );
1055
+ }
1056
+ await this.store.order.recalculateSummary({ createIfMissing: true });
1057
+ this.store.order.persistTempOrder();
493
1058
  } catch (error) {
494
1059
  throw error;
495
1060
  }
@@ -504,7 +1069,7 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
504
1069
  * payments 会交给 OrderModule 统一映射为 Sales 协议支付项。
505
1070
  */
506
1071
  async submitSalesOrder(params) {
507
- var _a, _b, _c, _d;
1072
+ var _a, _b, _c;
508
1073
  if (!this.store.order) {
509
1074
  throw new Error("BaseSales 解决方案需要 order 模块支持");
510
1075
  }
@@ -513,19 +1078,57 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
513
1078
  cacheId: this.cacheId,
514
1079
  platform: (_a = this.otherParams) == null ? void 0 : _a.platform,
515
1080
  businessCode: (_b = this.otherParams) == null ? void 0 : _b.businessCode,
516
- channel: (_c = this.otherParams) == null ? void 0 : _c.channel,
517
- type: (_d = this.otherParams) == null ? void 0 : _d.type,
1081
+ channel: this.getSubmitOrderSalesChannel(),
1082
+ type: (_c = this.otherParams) == null ? void 0 : _c.type,
518
1083
  ...(params == null ? void 0 : params.payments) !== void 0 ? { payments: params.payments } : {},
519
1084
  ...(params == null ? void 0 : params.paymentStatus) !== void 0 ? { paymentStatus: params.paymentStatus } : {},
520
1085
  ...(params == null ? void 0 : params.smallTicketDataFlag) !== void 0 ? { smallTicketDataFlag: params.smallTicketDataFlag } : {},
521
1086
  ...(params == null ? void 0 : params.enhancePayload) !== void 0 ? { enhancePayload: params.enhancePayload } : {}
522
1087
  };
1088
+ console.log(submitParams, "submitParams123");
523
1089
  return this.store.order.submitTempOrder(submitParams);
524
1090
  }
525
1091
  async syncPaymentsToOrder(params) {
526
1092
  if (!this.store.order)
527
1093
  throw new Error("order 模块未初始化");
528
- return this.store.order.syncPaymentsToOrder(params);
1094
+ const channel = params.channel ?? this.getSubmitOrderSalesChannel();
1095
+ const syncParams = {
1096
+ ...params,
1097
+ ...channel !== void 0 ? { channel } : {}
1098
+ };
1099
+ const syncState = this.store.order.getOrderSyncState();
1100
+ if (params.submitWhenPaid && syncState === "submitting") {
1101
+ return this.store.order.syncPaymentsToOrder(syncParams);
1102
+ }
1103
+ const payments = params.payments || [];
1104
+ await this.effectsEmit(import_types.BaseSalesHookNames.onPaymentSyncStart, {
1105
+ payments,
1106
+ params: syncParams,
1107
+ amountSnapshot: this.store.order.getOrderAmountSnapshot(),
1108
+ orderSnapshot: this.store.order.getOrderSnapshot()
1109
+ });
1110
+ try {
1111
+ const syncResult = await this.store.order.syncPaymentsToOrder(syncParams);
1112
+ await this.effectsEmit(import_types.BaseSalesHookNames.onPaymentSyncEnd, {
1113
+ ok: true,
1114
+ syncResult,
1115
+ payments: this.store.order.getOrderPayments(),
1116
+ params: syncParams,
1117
+ amountSnapshot: this.store.order.getOrderAmountSnapshot(),
1118
+ orderSnapshot: this.store.order.getOrderSnapshot()
1119
+ });
1120
+ return syncResult;
1121
+ } catch (error) {
1122
+ await this.effectsEmit(import_types.BaseSalesHookNames.onPaymentSyncEnd, {
1123
+ ok: false,
1124
+ error,
1125
+ payments: this.store.order.getOrderPayments(),
1126
+ params: syncParams,
1127
+ amountSnapshot: this.store.order.getOrderAmountSnapshot(),
1128
+ orderSnapshot: this.store.order.getOrderSnapshot()
1129
+ });
1130
+ throw error;
1131
+ }
529
1132
  }
530
1133
  /** 读取当前 BaseSales 订单上下文中的 Sales 协议支付项。 */
531
1134
  getOrderPayments() {
@@ -543,7 +1146,31 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
543
1146
  addOrderPayment(payment) {
544
1147
  if (!this.store.order)
545
1148
  throw new Error("order 模块未初始化");
546
- return this.store.order.addOrderPayment(payment);
1149
+ const paymentRecord = payment;
1150
+ const metadata = paymentRecord.metadata && typeof paymentRecord.metadata === "object" ? { ...paymentRecord.metadata } : {};
1151
+ if (!metadata.unique_identification_number) {
1152
+ metadata.unique_identification_number = (0, import_utils.createUuidV4)();
1153
+ }
1154
+ const payments = this.store.order.addOrderPayment({
1155
+ ...paymentRecord,
1156
+ metadata
1157
+ });
1158
+ const amountSnapshot = this.store.order.getOrderAmountSnapshot();
1159
+ const syncState = this.store.order.getOrderSyncState();
1160
+ if (amountSnapshot && syncState !== "submitting") {
1161
+ const paymentStatus = Number(amountSnapshot.amountGap || 0) <= 0 ? "paid" : "partially_paid";
1162
+ void this.syncPaymentsToOrder({
1163
+ payments,
1164
+ paymentStatus,
1165
+ submitWhenPaid: true,
1166
+ smallTicketDataFlag: 1
1167
+ }).catch((error) => {
1168
+ this.logError("auto sync payments failed", {
1169
+ error: error instanceof Error ? error.message : String(error)
1170
+ });
1171
+ });
1172
+ }
1173
+ return payments;
547
1174
  }
548
1175
  /** 更新当前订单中的指定支付项。 */
549
1176
  updateOrderPayment(paymentIdentity, updates) {
@@ -564,11 +1191,12 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
564
1191
  return this.store.order.updateVoucherOrderPayments(payments);
565
1192
  }
566
1193
  getWalletProductList() {
567
- var _a, _b;
1194
+ var _a, _b, _c;
568
1195
  const tempOrder = (_a = this.store.order) == null ? void 0 : _a.getTempOrder();
569
1196
  if (!((_b = tempOrder == null ? void 0 : tempOrder.products) == null ? void 0 : _b.length))
570
1197
  return [];
571
- return tempOrder.products.map((product) => {
1198
+ const products = (_c = tempOrder == null ? void 0 : tempOrder.products) == null ? void 0 : _c.filter((n) => n.product_id);
1199
+ return products.map((product) => {
572
1200
  const metadata = product.metadata || {};
573
1201
  return {
574
1202
  product_id: product.product_id,
@@ -578,6 +1206,7 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
578
1206
  is_charge_tax: product.is_charge_tax ?? 0,
579
1207
  tax_fee: product.tax_fee,
580
1208
  selling_price: Number(product.selling_price || 0),
1209
+ discount_list: product.discount_list || [],
581
1210
  holder_id: product.holder_id ?? metadata.holder_id,
582
1211
  original_price: product.original_price,
583
1212
  main_product_original_price: metadata.main_product_original_price ?? product.original_price,
@@ -591,8 +1220,10 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
591
1220
  const bundleMetadata = bundle.metadata || {};
592
1221
  return {
593
1222
  is_price_include_tax: tempOrder.is_price_include_tax,
594
- bundle_id: bundle.bundle_id,
595
- bundle_product_id: bundle.bundle_product_id,
1223
+ // 套餐子项在 tempOrder 上常为 id(如 1718),prepare/deduction 协议要求 bundle_id
1224
+ bundle_id: bundle.bundle_id ?? bundle.id,
1225
+ bundle_product_id: bundle.bundle_product_id ?? bundle._bundle_product_id,
1226
+ bundle_group_id: bundle.bundle_group_id ?? bundle.group_id,
596
1227
  bundle_variant_id: bundle.bundle_variant_id,
597
1228
  price_type: bundle.price_type,
598
1229
  price_type_ext: bundle.price_type_ext,
@@ -731,6 +1362,24 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
731
1362
  transformBaseProductToOrderProduct(params) {
732
1363
  return (0, import_transformBaseProductToOrderProduct.transformBaseProductToOrderProduct)(params);
733
1364
  }
1365
+ async calculateProductBookingPrice(params) {
1366
+ const quotation = this.store.quotation;
1367
+ const customerId = params.customer_id ?? this.getCurrentOrderCustomerId();
1368
+ const authoritativeProduct = await this.loadProductForPriceQuery(params, customerId);
1369
+ const product = this.mergeProductForPriceQuery(params.product, authoritativeProduct);
1370
+ await this.loadQuotationForPriceQuery({
1371
+ quotation,
1372
+ customer_id: customerId,
1373
+ channel: params.channel
1374
+ });
1375
+ return (0, import_quotationPrice.calculateBaseSalesProductBookingPrice)({
1376
+ ...params,
1377
+ product,
1378
+ fallback_price: authoritativeProduct ? void 0 : params.fallback_price,
1379
+ customer_id: customerId,
1380
+ quotation
1381
+ });
1382
+ }
734
1383
  async addProductToOrder(product, booking) {
735
1384
  try {
736
1385
  if (!this.store.order)
@@ -741,16 +1390,35 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
741
1390
  throw error;
742
1391
  }
743
1392
  }
744
- async updateProductInOrder(params) {
1393
+ async updateOrderProduct(params) {
745
1394
  try {
746
1395
  if (!this.store.order)
747
1396
  throw new Error("order 模块未初始化");
748
- const products = await this.store.order.updateProductInOrder(params);
1397
+ const products = await this.store.order.updateOrderProduct(params);
749
1398
  return products;
750
1399
  } catch (error) {
751
1400
  throw error;
752
1401
  }
753
1402
  }
1403
+ async updateOrderProductQuantity(params) {
1404
+ try {
1405
+ if (!this.store.order)
1406
+ throw new Error("order 模块未初始化");
1407
+ const products = await this.store.order.updateOrderProductQuantity(params);
1408
+ return products;
1409
+ } catch (error) {
1410
+ throw error;
1411
+ }
1412
+ }
1413
+ updateOrderBooking(params) {
1414
+ try {
1415
+ if (!this.store.order)
1416
+ throw new Error("order 模块未初始化");
1417
+ return this.store.order.updateOrderBooking(params);
1418
+ } catch (error) {
1419
+ throw error;
1420
+ }
1421
+ }
754
1422
  /**
755
1423
  * 设置单行商品备注(与整单 `updateTempOrderNote` 区分)。
756
1424
  * 多行同 SKU 时必须传入与删除/更新一致的 unique_identification_number。
@@ -770,7 +1438,7 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
770
1438
  }
771
1439
  if (identity.product_bundle !== void 0)
772
1440
  params.product_bundle = identity.product_bundle;
773
- const products = await this.updateProductInOrder(params);
1441
+ const products = await this.updateOrderProduct(params);
774
1442
  return products;
775
1443
  } catch (error) {
776
1444
  throw error;
@@ -786,6 +1454,58 @@ var BaseSalesImpl = class extends import_BaseModule.BaseModule {
786
1454
  throw error;
787
1455
  }
788
1456
  }
1457
+ // ─── 促销/赠品 透传(迁移自 booking usePromotion) ────────────────
1458
+ /**
1459
+ * 注入促销评估器到底层 OrderModule。
1460
+ *
1461
+ * @example
1462
+ * solution.setPromotionEvaluator(appHelper.utils.promotionEvaluator);
1463
+ */
1464
+ setPromotionEvaluator(evaluator) {
1465
+ if (!this.store.order)
1466
+ throw new Error("order 模块未初始化");
1467
+ this.store.order.setPromotionEvaluator(evaluator);
1468
+ }
1469
+ /**
1470
+ * 注入赠品选择 resolver。UI 层(SalesSdk)通过 bridge 提供。
1471
+ *
1472
+ * @example
1473
+ * solution.setGiftSelectResolver(async (ctx) => bridge.request(ctx));
1474
+ */
1475
+ setGiftSelectResolver(resolver) {
1476
+ if (!this.store.order)
1477
+ throw new Error("order 模块未初始化");
1478
+ this.store.order.setGiftSelectResolver(resolver);
1479
+ }
1480
+ /**
1481
+ * 为商品目录追加促销标签,供 SalesSdkProductProvider 等商品列表入口复用。
1482
+ *
1483
+ * @example
1484
+ * const products = solution.appendPromotionTags(catalogProducts);
1485
+ */
1486
+ appendPromotionTags(products) {
1487
+ if (!this.store.order)
1488
+ throw new Error("order 模块未初始化");
1489
+ return this.store.order.appendPromotionTags(products);
1490
+ }
1491
+ /**
1492
+ * 主动触发一次促销应用(一般写路径会自动触发,少数场景如客户切换后可手动调)。
1493
+ */
1494
+ async applyPromotion() {
1495
+ if (!this.store.order)
1496
+ throw new Error("order 模块未初始化");
1497
+ await this.store.order.applyPromotion();
1498
+ }
1499
+ /** 最近一次促销计算输出的未满足提示。 */
1500
+ getUnfulfilledPromotions() {
1501
+ var _a;
1502
+ return ((_a = this.store.order) == null ? void 0 : _a.getUnfulfilledPromotions()) || [];
1503
+ }
1504
+ /** 最近一次促销计算输出的赠品操作 diff。 */
1505
+ getLastGiftActions() {
1506
+ var _a;
1507
+ return ((_a = this.store.order) == null ? void 0 : _a.getLastGiftActions()) || null;
1508
+ }
789
1509
  // 获取商品列表
790
1510
  // TODO 需要跟 webpos 内的对齐一下
791
1511
  async getProductList() {