@pisell/pisellos 2.3.40 → 2.3.42

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 (230) hide show
  1. package/dist/core/index.d.ts +2 -0
  2. package/dist/core/index.js +7 -0
  3. package/dist/model/strategy/adapter/promotion/adapter.d.ts +4 -0
  4. package/dist/model/strategy/adapter/promotion/adapter.js +23 -0
  5. package/dist/model/strategy/adapter/promotion/evaluator.d.ts +17 -0
  6. package/dist/model/strategy/adapter/promotion/evaluator.js +279 -6
  7. package/dist/model/strategy/adapter/promotion/examples.d.ts +86 -0
  8. package/dist/model/strategy/adapter/promotion/examples.js +99 -0
  9. package/dist/model/strategy/adapter/promotion/index.d.ts +1 -1
  10. package/dist/model/strategy/adapter/promotion/index.js +9 -0
  11. package/dist/model/strategy/adapter/promotion/type.d.ts +90 -2
  12. package/dist/model/strategy/adapter/promotion/type.js +45 -0
  13. package/dist/modules/BookingContext/utils/buildCacheItemFromOrderLine.js +5 -3
  14. package/dist/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.js +6 -3
  15. package/dist/modules/Discount/index.d.ts +0 -1
  16. package/dist/modules/Discount/index.js +11 -40
  17. package/dist/modules/Discount/types.d.ts +0 -1
  18. package/dist/modules/OpenData/index.d.ts +2 -0
  19. package/dist/modules/OpenData/index.js +10 -2
  20. package/dist/modules/Order/index.d.ts +29 -12
  21. package/dist/modules/Order/index.js +751 -510
  22. package/dist/modules/Order/payment-utils.d.ts +26 -0
  23. package/dist/modules/Order/payment-utils.js +225 -0
  24. package/dist/modules/Order/types.d.ts +33 -10
  25. package/dist/modules/Order/utils/orderCollectionIdentity.js +7 -2
  26. package/dist/modules/Order/utils.d.ts +0 -10
  27. package/dist/modules/Order/utils.js +13 -41
  28. package/dist/modules/Payment/index.d.ts +2 -0
  29. package/dist/modules/Payment/index.js +79 -50
  30. package/dist/modules/Payment/types.d.ts +99 -28
  31. package/dist/modules/Payment/types.js +20 -3
  32. package/dist/modules/Payment/walletpass.d.ts +25 -1
  33. package/dist/modules/Payment/walletpass.js +707 -157
  34. package/dist/modules/Product/types.d.ts +0 -1
  35. package/dist/modules/ProductList/index.js +14 -33
  36. package/dist/modules/Quotation/index.d.ts +1 -1
  37. package/dist/modules/Quotation/index.js +2 -2
  38. package/dist/modules/Rules/index.d.ts +1 -0
  39. package/dist/modules/Rules/index.js +89 -96
  40. package/dist/modules/SalesSummary/index.js +13 -12
  41. package/dist/modules/ScanOrderLogger/index.d.ts +0 -2
  42. package/dist/modules/ScanOrderLogger/index.js +2 -15
  43. package/dist/modules/ScanOrderLogger/providers/feishu.js +27 -45
  44. package/dist/modules/ScanOrderLogger/types.d.ts +0 -1
  45. package/dist/modules/Schedule/index.d.ts +1 -3
  46. package/dist/modules/Schedule/index.js +16 -21
  47. package/dist/modules/Summary/utils.js +13 -38
  48. package/dist/modules/index.d.ts +0 -2
  49. package/dist/modules/index.js +1 -3
  50. package/dist/plugins/window.d.ts +0 -1
  51. package/dist/server/index.d.ts +16 -14
  52. package/dist/server/index.js +1925 -1143
  53. package/dist/server/modules/order/index.d.ts +54 -4
  54. package/dist/server/modules/order/index.js +1600 -700
  55. package/dist/server/modules/order/types.d.ts +11 -3
  56. package/dist/server/modules/order/types.js +1 -1
  57. package/dist/solution/BaseSales/index.d.ts +109 -20
  58. package/dist/solution/BaseSales/index.js +1800 -480
  59. package/dist/solution/BaseSales/types.d.ts +52 -0
  60. package/dist/solution/BaseSales/types.js +2 -1
  61. package/dist/solution/BaseSales/utils/cartPromotion.d.ts +3 -1
  62. package/dist/solution/BaseSales/utils/cartPromotion.js +90 -31
  63. package/dist/solution/BaseSales/utils/parseSalesResponse.d.ts +6 -1
  64. package/dist/solution/BookingByStep/index.d.ts +2 -17
  65. package/dist/solution/BookingByStep/index.js +215 -294
  66. package/dist/solution/BookingByStep/types.d.ts +1 -2
  67. package/dist/solution/BookingByStep/types.js +1 -3
  68. package/dist/solution/BookingByStep/utils/capacity.js +1 -17
  69. package/dist/solution/BookingByStep/utils/timeslots.d.ts +1 -3
  70. package/dist/solution/BookingByStep/utils/timeslots.js +12 -19
  71. package/dist/solution/BookingTicket/index.d.ts +8 -4
  72. package/dist/solution/BookingTicket/index.js +134 -51
  73. package/dist/solution/BookingTicket/utils/addProductDecision.js +6 -0
  74. package/dist/solution/BookingTicket/utils/scan/applyGlobalScan.d.ts +8 -0
  75. package/dist/solution/BookingTicket/utils/scan/applyGlobalScan.js +172 -82
  76. package/dist/solution/BookingTicket/utils/scan/handleScan.d.ts +1 -3
  77. package/dist/solution/BookingTicket/utils/scan/handleScan.js +2 -10
  78. package/dist/solution/BookingTicket/utils/weighingProductSku.d.ts +5 -0
  79. package/dist/solution/BookingTicket/utils/weighingProductSku.js +45 -0
  80. package/dist/solution/Sales/index.d.ts +1 -0
  81. package/dist/solution/Sales/index.js +10 -2
  82. package/dist/solution/ScanOrder/index.d.ts +14 -26
  83. package/dist/solution/ScanOrder/index.js +756 -1148
  84. package/dist/solution/ScanOrder/types.d.ts +1 -21
  85. package/dist/solution/ScanOrder/utils.d.ts +0 -8
  86. package/dist/solution/ScanOrder/utils.js +25 -66
  87. package/dist/solution/ShopDiscount/index.d.ts +0 -1
  88. package/dist/solution/ShopDiscount/index.js +87 -125
  89. package/dist/solution/VenueBooking/index.d.ts +11 -39
  90. package/dist/solution/VenueBooking/index.js +860 -1176
  91. package/dist/solution/VenueBooking/types.d.ts +0 -3
  92. package/dist/solution/VenueBooking/utils/resource.js +0 -1
  93. package/dist/solution/VenueBooking/utils/slotMerge.d.ts +4 -4
  94. package/dist/solution/VenueBooking/utils/slotMerge.js +5 -10
  95. package/dist/solution/index.d.ts +0 -1
  96. package/dist/solution/index.js +1 -2
  97. package/dist/types/index.d.ts +1 -1
  98. package/dist/utils/payment-number.d.ts +9 -0
  99. package/dist/utils/payment-number.js +69 -0
  100. package/lib/core/index.d.ts +2 -0
  101. package/lib/core/index.js +4 -0
  102. package/lib/model/strategy/adapter/promotion/adapter.d.ts +4 -0
  103. package/lib/model/strategy/adapter/promotion/adapter.js +20 -0
  104. package/lib/model/strategy/adapter/promotion/evaluator.d.ts +17 -0
  105. package/lib/model/strategy/adapter/promotion/evaluator.js +235 -0
  106. package/lib/model/strategy/adapter/promotion/examples.d.ts +86 -0
  107. package/lib/model/strategy/adapter/promotion/examples.js +91 -0
  108. package/lib/model/strategy/adapter/promotion/index.d.ts +1 -1
  109. package/lib/model/strategy/adapter/promotion/index.js +2 -0
  110. package/lib/model/strategy/adapter/promotion/type.d.ts +90 -2
  111. package/lib/model/strategy/adapter/promotion/type.js +2 -0
  112. package/lib/modules/BookingContext/utils/buildCacheItemFromOrderLine.js +2 -1
  113. package/lib/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.js +2 -1
  114. package/lib/modules/Discount/index.d.ts +0 -1
  115. package/lib/modules/Discount/index.js +0 -9
  116. package/lib/modules/Discount/types.d.ts +0 -1
  117. package/lib/modules/OpenData/index.d.ts +2 -0
  118. package/lib/modules/OpenData/index.js +6 -1
  119. package/lib/modules/Order/index.d.ts +29 -12
  120. package/lib/modules/Order/index.js +197 -87
  121. package/lib/modules/Order/payment-utils.d.ts +26 -0
  122. package/lib/modules/Order/payment-utils.js +224 -0
  123. package/lib/modules/Order/types.d.ts +33 -10
  124. package/lib/modules/Order/utils/orderCollectionIdentity.js +3 -0
  125. package/lib/modules/Order/utils.d.ts +0 -10
  126. package/lib/modules/Order/utils.js +16 -40
  127. package/lib/modules/Payment/index.d.ts +2 -0
  128. package/lib/modules/Payment/index.js +34 -13
  129. package/lib/modules/Payment/types.d.ts +99 -28
  130. package/lib/modules/Payment/types.js +4 -3
  131. package/lib/modules/Payment/walletpass.d.ts +25 -1
  132. package/lib/modules/Payment/walletpass.js +470 -21
  133. package/lib/modules/Product/types.d.ts +0 -1
  134. package/lib/modules/ProductList/index.js +31 -40
  135. package/lib/modules/Quotation/index.d.ts +1 -1
  136. package/lib/modules/Quotation/index.js +2 -2
  137. package/lib/modules/Rules/index.d.ts +1 -0
  138. package/lib/modules/Rules/index.js +47 -40
  139. package/lib/modules/SalesSummary/index.js +7 -6
  140. package/lib/modules/ScanOrderLogger/index.d.ts +0 -2
  141. package/lib/modules/ScanOrderLogger/index.js +1 -13
  142. package/lib/modules/ScanOrderLogger/providers/feishu.js +6 -15
  143. package/lib/modules/ScanOrderLogger/types.d.ts +0 -1
  144. package/lib/modules/Schedule/index.d.ts +1 -3
  145. package/lib/modules/Schedule/index.js +8 -10
  146. package/lib/modules/Summary/utils.js +1 -21
  147. package/lib/modules/index.d.ts +0 -2
  148. package/lib/modules/index.js +1 -5
  149. package/lib/plugins/window.d.ts +0 -1
  150. package/lib/server/index.d.ts +16 -14
  151. package/lib/server/index.js +675 -83
  152. package/lib/server/modules/order/index.d.ts +54 -4
  153. package/lib/server/modules/order/index.js +679 -66
  154. package/lib/server/modules/order/types.d.ts +11 -3
  155. package/lib/solution/BaseSales/index.d.ts +109 -20
  156. package/lib/solution/BaseSales/index.js +883 -70
  157. package/lib/solution/BaseSales/types.d.ts +52 -0
  158. package/lib/solution/BaseSales/types.js +2 -1
  159. package/lib/solution/BaseSales/utils/cartPromotion.d.ts +3 -1
  160. package/lib/solution/BaseSales/utils/cartPromotion.js +63 -12
  161. package/lib/solution/BaseSales/utils/parseSalesResponse.d.ts +6 -1
  162. package/lib/solution/BookingByStep/index.d.ts +2 -17
  163. package/lib/solution/BookingByStep/index.js +18 -60
  164. package/lib/solution/BookingByStep/types.d.ts +1 -2
  165. package/lib/solution/BookingByStep/types.js +0 -5
  166. package/lib/solution/BookingByStep/utils/capacity.js +1 -20
  167. package/lib/solution/BookingByStep/utils/timeslots.d.ts +1 -3
  168. package/lib/solution/BookingByStep/utils/timeslots.js +11 -19
  169. package/lib/solution/BookingTicket/index.d.ts +8 -4
  170. package/lib/solution/BookingTicket/index.js +64 -19
  171. package/lib/solution/BookingTicket/utils/addProductDecision.js +4 -0
  172. package/lib/solution/BookingTicket/utils/scan/applyGlobalScan.d.ts +8 -0
  173. package/lib/solution/BookingTicket/utils/scan/applyGlobalScan.js +36 -4
  174. package/lib/solution/BookingTicket/utils/scan/handleScan.d.ts +1 -3
  175. package/lib/solution/BookingTicket/utils/scan/handleScan.js +2 -2
  176. package/lib/solution/BookingTicket/utils/weighingProductSku.d.ts +5 -0
  177. package/lib/solution/BookingTicket/utils/weighingProductSku.js +63 -0
  178. package/lib/solution/Sales/index.d.ts +1 -0
  179. package/lib/solution/Sales/index.js +5 -3
  180. package/lib/solution/ScanOrder/index.d.ts +14 -26
  181. package/lib/solution/ScanOrder/index.js +190 -449
  182. package/lib/solution/ScanOrder/types.d.ts +1 -21
  183. package/lib/solution/ScanOrder/utils.d.ts +0 -8
  184. package/lib/solution/ScanOrder/utils.js +0 -31
  185. package/lib/solution/ShopDiscount/index.d.ts +0 -1
  186. package/lib/solution/ShopDiscount/index.js +0 -14
  187. package/lib/solution/VenueBooking/index.d.ts +11 -39
  188. package/lib/solution/VenueBooking/index.js +123 -328
  189. package/lib/solution/VenueBooking/types.d.ts +0 -3
  190. package/lib/solution/VenueBooking/utils/resource.js +0 -1
  191. package/lib/solution/VenueBooking/utils/slotMerge.d.ts +4 -4
  192. package/lib/solution/VenueBooking/utils/slotMerge.js +4 -6
  193. package/lib/solution/index.d.ts +0 -1
  194. package/lib/solution/index.js +1 -3
  195. package/lib/types/index.d.ts +1 -1
  196. package/lib/utils/payment-number.d.ts +9 -0
  197. package/lib/utils/payment-number.js +64 -0
  198. package/package.json +1 -1
  199. package/dist/modules/Holder/index.d.ts +0 -48
  200. package/dist/modules/Holder/index.js +0 -640
  201. package/dist/modules/Holder/types.d.ts +0 -123
  202. package/dist/modules/Holder/types.js +0 -1
  203. package/dist/modules/Holder/utils.d.ts +0 -13
  204. package/dist/modules/Holder/utils.js +0 -34
  205. package/dist/modules/ResourcePlanner/index.d.ts +0 -24
  206. package/dist/modules/ResourcePlanner/index.js +0 -181
  207. package/dist/modules/ResourcePlanner/planner.d.ts +0 -14
  208. package/dist/modules/ResourcePlanner/planner.js +0 -1069
  209. package/dist/modules/ResourcePlanner/types.d.ts +0 -227
  210. package/dist/modules/ResourcePlanner/types.js +0 -1
  211. package/dist/solution/UnifiedBookingSales/index.d.ts +0 -183
  212. package/dist/solution/UnifiedBookingSales/index.js +0 -1871
  213. package/dist/solution/UnifiedBookingSales/types.d.ts +0 -143
  214. package/dist/solution/UnifiedBookingSales/types.js +0 -11
  215. package/lib/modules/Holder/index.d.ts +0 -48
  216. package/lib/modules/Holder/index.js +0 -402
  217. package/lib/modules/Holder/types.d.ts +0 -123
  218. package/lib/modules/Holder/types.js +0 -17
  219. package/lib/modules/Holder/utils.d.ts +0 -13
  220. package/lib/modules/Holder/utils.js +0 -71
  221. package/lib/modules/ResourcePlanner/index.d.ts +0 -24
  222. package/lib/modules/ResourcePlanner/index.js +0 -148
  223. package/lib/modules/ResourcePlanner/planner.d.ts +0 -14
  224. package/lib/modules/ResourcePlanner/planner.js +0 -933
  225. package/lib/modules/ResourcePlanner/types.d.ts +0 -227
  226. package/lib/modules/ResourcePlanner/types.js +0 -17
  227. package/lib/solution/UnifiedBookingSales/index.d.ts +0 -183
  228. package/lib/solution/UnifiedBookingSales/index.js +0 -1076
  229. package/lib/solution/UnifiedBookingSales/types.d.ts +0 -143
  230. package/lib/solution/UnifiedBookingSales/types.js +0 -33
@@ -34,6 +34,8 @@ __export(server_exports, {
34
34
  });
35
35
  module.exports = __toCommonJS(server_exports);
36
36
  var import_dayjs = __toESM(require("dayjs"));
37
+ var import_decimal = __toESM(require("decimal.js"));
38
+ var import_lodash_es = require("lodash-es");
37
39
  var import_products = require("./modules/products");
38
40
  var import_menu = require("./modules/menu");
39
41
  var import_quotation = require("./modules/quotation");
@@ -50,6 +52,8 @@ var import_types3 = require("./modules/order/types");
50
52
  var import_filterOrders = require("./modules/order/utils/filterOrders");
51
53
  var import_filterBookings = require("./modules/order/utils/filterBookings");
52
54
  var import_small_ticket = require("./utils/small-ticket");
55
+ var import_BaseSales = require("../solution/BaseSales");
56
+ var import_payment_utils = require("../modules/Order/payment-utils");
53
57
  __reExport(server_exports, require("./modules"), module.exports);
54
58
  var PRODUCT_TITLE_SNAPSHOT_KEYS = [
55
59
  "en",
@@ -78,10 +82,6 @@ var Server = class {
78
82
  this.productQuerySubscribers = /* @__PURE__ */ new Map();
79
83
  /** subscriberId → 智能定价变价时间点定时器句柄(与 subscriber 分离存储,便于 clear) */
80
84
  this.productQueryScheduleTimers = /* @__PURE__ */ new Map();
81
- /** 餐牌筛选为空时才触发的菜单/日程缓存自愈,避免每次商品查询都请求远端。 */
82
- this.menuScheduleRefreshInFlight = null;
83
- this.lastMenuScheduleRefreshAt = 0;
84
- this.MENU_SCHEDULE_REFRESH_COOLDOWN_MS = 30 * 1e3;
85
85
  // ---- 订单 / 预约列表查询订阅者 ----
86
86
  this.orderQuerySubscribers = /* @__PURE__ */ new Map();
87
87
  this.bookingQuerySubscribers = /* @__PURE__ */ new Map();
@@ -91,6 +91,7 @@ var Server = class {
91
91
  this.BOOKING_REMOTE_CACHE_MAX_ENTRIES = 80;
92
92
  this.salesSearchSubscribers = /* @__PURE__ */ new Map();
93
93
  this.deviceTaskActionsRegistered = false;
94
+ this.localPaymentUpdateQueues = /* @__PURE__ */ new Map();
94
95
  this.floorPlanQuerySubscribers = /* @__PURE__ */ new Map();
95
96
  // 模块注册表 - 定义所有可用的模块配置
96
97
  this.moduleRegistry = {
@@ -214,11 +215,7 @@ var Server = class {
214
215
  */
215
216
  this.handleProductQuery = async ({ url, method, data, config }) => {
216
217
  console.log("[Server] handleProductQuery:", url, method, data, config);
217
- const { menu_list_ids, customer_id, ids, extension_type, strategy_context } = data;
218
- const { schedule_date, schedule_datetime } = this.resolveProductQueryScheduleContext({
219
- schedule_date: data.schedule_date,
220
- schedule_datetime: data.schedule_datetime
221
- });
218
+ const { menu_list_ids, schedule_datetime, schedule_date, customer_id, ids, extension_type, strategy_context } = data;
222
219
  const { callback, subscriberId } = config || {};
223
220
  this.logInfo("handleProductQuery: 开始处理商品查询请求", {
224
221
  menu_list_ids,
@@ -583,6 +580,128 @@ var Server = class {
583
580
  title: "handleOrderSalesCheckout"
584
581
  });
585
582
  };
583
+ this.handleOrderSalesDraft = async ({ data }) => {
584
+ return this.handleOrderDraftSubmit({
585
+ data,
586
+ title: "handleOrderSalesDraft"
587
+ });
588
+ };
589
+ this.handleGetLocalPayment = async ({ data }) => {
590
+ const saleId = data == null ? void 0 : data.sale_id;
591
+ const paymentNumber = data == null ? void 0 : data.payment_number;
592
+ const operationId = this.createLocalPaymentOperationId("get");
593
+ const requestContext = {
594
+ operation_id: operationId,
595
+ sale_id: saleId ?? null,
596
+ payment_number: paymentNumber ?? null
597
+ };
598
+ this.logInfo("handleGetLocalPayment: 请求进入", requestContext);
599
+ if (saleId === void 0 || saleId === null || saleId === "" || paymentNumber === void 0 || paymentNumber === null || paymentNumber === "") {
600
+ this.logWarning("handleGetLocalPayment: 参数校验失败", {
601
+ ...requestContext,
602
+ missing_fields: [
603
+ ...saleId === void 0 || saleId === null || saleId === "" ? ["sale_id"] : [],
604
+ ...paymentNumber === void 0 || paymentNumber === null || paymentNumber === "" ? ["payment_number"] : []
605
+ ]
606
+ });
607
+ return {
608
+ code: 400,
609
+ status: false,
610
+ message: "sale_id 和 payment_number 必填",
611
+ data: null
612
+ };
613
+ }
614
+ if (!this.order) {
615
+ this.logError("handleGetLocalPayment: Order 模块未注册", requestContext);
616
+ return { code: 500, status: false, message: "Order 模块未注册", data: null };
617
+ }
618
+ const startedAt = Date.now();
619
+ try {
620
+ this.logInfo("handleGetLocalPayment: 开始查询本地支付项", requestContext);
621
+ const found = await this.order.getLocalPayment({
622
+ saleId,
623
+ paymentNumber,
624
+ operationId
625
+ });
626
+ if (!found) {
627
+ this.logWarning("handleGetLocalPayment: 本地支付项不存在", {
628
+ ...requestContext,
629
+ duration_ms: Date.now() - startedAt
630
+ });
631
+ return { code: 404, status: false, message: "本地支付项不存在", data: null };
632
+ }
633
+ this.logInfo("handleGetLocalPayment: 查询命中", {
634
+ ...requestContext,
635
+ duration_ms: Date.now() - startedAt,
636
+ matched_by: found.matchedBy,
637
+ payment_index: found.paymentIndex,
638
+ ...this.buildLocalPaymentOrderLogContext(found.order),
639
+ payment: found.payment
640
+ });
641
+ return { code: 200, status: true, message: "", data: found.payment };
642
+ } catch (error) {
643
+ this.logError("handleGetLocalPayment: 查询异常", {
644
+ ...requestContext,
645
+ duration_ms: Date.now() - startedAt,
646
+ error: error instanceof Error ? error.message : String(error),
647
+ error_detail: this.normalizeUnknownError(error)
648
+ });
649
+ throw error;
650
+ }
651
+ };
652
+ this.handleUpdateLocalPayment = async ({ data }) => {
653
+ const saleId = data == null ? void 0 : data.sale_id;
654
+ const paymentNumber = data == null ? void 0 : data.payment_number;
655
+ const number = data == null ? void 0 : data.number;
656
+ const status = data == null ? void 0 : data.status;
657
+ const serviceCharge = data == null ? void 0 : data.service_charge;
658
+ const cardReaderSurcharge = data == null ? void 0 : data.card_reader_surcharge;
659
+ const hasSurchargeInput = serviceCharge !== void 0 || cardReaderSurcharge !== void 0;
660
+ const invalidSurchargeFields = this.getInvalidLocalPaymentSurchargeFields(
661
+ serviceCharge,
662
+ cardReaderSurcharge
663
+ );
664
+ const operationId = this.createLocalPaymentOperationId("update");
665
+ const requestContext = {
666
+ operation_id: operationId,
667
+ sale_id: saleId ?? null,
668
+ payment_number: paymentNumber ?? null,
669
+ transaction_number: number ?? null,
670
+ requested_status: status ?? null,
671
+ service_charge: serviceCharge ?? null,
672
+ card_reader_surcharge: cardReaderSurcharge ?? null
673
+ };
674
+ this.logInfo("handleUpdateLocalPayment: 请求进入", requestContext);
675
+ if (saleId === void 0 || saleId === null || saleId === "" || paymentNumber === void 0 || paymentNumber === null || paymentNumber === "" || number === void 0 || number === null || number === "" || status !== "success" && status !== "fail" || invalidSurchargeFields.length > 0) {
676
+ this.logWarning("handleUpdateLocalPayment: 参数校验失败", {
677
+ ...requestContext,
678
+ missing_or_invalid_fields: [
679
+ ...saleId === void 0 || saleId === null || saleId === "" ? ["sale_id"] : [],
680
+ ...paymentNumber === void 0 || paymentNumber === null || paymentNumber === "" ? ["payment_number"] : [],
681
+ ...number === void 0 || number === null || number === "" ? ["number"] : [],
682
+ ...status !== "success" && status !== "fail" ? ["status"] : [],
683
+ ...invalidSurchargeFields
684
+ ]
685
+ });
686
+ return {
687
+ code: 400,
688
+ status: false,
689
+ message: invalidSurchargeFields.length > 0 ? `手续费参数非法: ${invalidSurchargeFields.join(", ")}` : "sale_id、payment_number、number 必填,status 仅支持 success/fail",
690
+ data: null
691
+ };
692
+ }
693
+ const queueKey = `${String(saleId)}:${String(paymentNumber)}`;
694
+ return this.runLocalPaymentUpdateInQueue(queueKey, () => this.processLocalPaymentUpdate({
695
+ operationId,
696
+ saleId,
697
+ paymentNumber,
698
+ number: String(number),
699
+ status,
700
+ serviceCharge,
701
+ cardReaderSurcharge,
702
+ hasSurchargeInput
703
+ }), requestContext);
704
+ };
586
705
  this.handleOrderCheckout = async ({ data }) => {
587
706
  return this.handleOrderCheckoutSubmit({
588
707
  backendPath: "/order/checkout",
@@ -1364,7 +1483,7 @@ var Server = class {
1364
1483
  const { backendPath, title } = params;
1365
1484
  const normalizedData = await this.normalizeCheckoutSubmitData(params.data, title);
1366
1485
  const checkoutData = await this.ensureCheckoutOrderNumbers(normalizedData, title);
1367
- const remoteCheckoutData = this.omitCheckoutSyncMode(checkoutData);
1486
+ const remoteCheckoutData = this.buildRemoteCheckoutData(checkoutData, title);
1368
1487
  if (!((_b = (_a = this.app) == null ? void 0 : _a.request) == null ? void 0 : _b.post)) {
1369
1488
  throw new Error("app.request 不可用");
1370
1489
  }
@@ -1380,20 +1499,20 @@ var Server = class {
1380
1499
  errorResponse: response,
1381
1500
  response,
1382
1501
  normalizedResponse: this.normalizeCheckoutResponse(response),
1383
- checkoutData: remoteCheckoutData
1502
+ checkoutData
1384
1503
  };
1385
1504
  }
1386
1505
  const fresh = this.extractOrderDataFromCheckoutResponse(response);
1387
1506
  const externalSaleNumber = this.getCheckoutExternalSaleNumber(
1388
- remoteCheckoutData,
1507
+ checkoutData,
1389
1508
  {
1390
1509
  external_sale_number: params.externalSaleNumber,
1391
- data: remoteCheckoutData
1510
+ data: checkoutData
1392
1511
  }
1393
1512
  );
1394
1513
  const syncedOrder = await this.persistSyncedCheckoutOrder({
1395
1514
  backendPath,
1396
- checkoutData: remoteCheckoutData,
1515
+ checkoutData,
1397
1516
  externalSaleNumber,
1398
1517
  fresh,
1399
1518
  title,
@@ -1404,11 +1523,11 @@ var Server = class {
1404
1523
  checkoutResponseOrder: fresh
1405
1524
  }) : void 0;
1406
1525
  if (printableSyncedOrder && this.shouldPrintSyncedOrder({
1407
- checkoutData: remoteCheckoutData,
1526
+ checkoutData,
1408
1527
  syncedOrder: printableSyncedOrder
1409
1528
  })) {
1410
1529
  await this.dispatchPrintOtherReceiptTask({
1411
- checkoutData: remoteCheckoutData,
1530
+ checkoutData,
1412
1531
  syncedOrder: printableSyncedOrder,
1413
1532
  response,
1414
1533
  deviceId: params.deviceId
@@ -1418,7 +1537,7 @@ var Server = class {
1418
1537
  rejected: false,
1419
1538
  response,
1420
1539
  normalizedResponse: this.normalizeCheckoutResponse(response),
1421
- checkoutData: remoteCheckoutData,
1540
+ checkoutData,
1422
1541
  syncedOrder,
1423
1542
  fresh
1424
1543
  };
@@ -1431,6 +1550,54 @@ var Server = class {
1431
1550
  delete next.syncMode;
1432
1551
  return next;
1433
1552
  }
1553
+ buildRemoteCheckoutData(data, title) {
1554
+ const next = (0, import_lodash_es.cloneDeep)(this.omitCheckoutSyncMode(data));
1555
+ if (!next || typeof next !== "object")
1556
+ return next;
1557
+ const record = next;
1558
+ if (!Array.isArray(record.payments))
1559
+ return next;
1560
+ const originalPayments = record.payments;
1561
+ const filteredPayments = originalPayments.filter((payment) => !(0, import_payment_utils.isPendingOrderPayment)(payment));
1562
+ const removedPayments = originalPayments.filter((payment) => (0, import_payment_utils.isPendingOrderPayment)(payment));
1563
+ record.payments = filteredPayments;
1564
+ this.logInfo(`${title}: checkout 云端支付项已过滤`, {
1565
+ external_sale_number: record.external_sale_number,
1566
+ original_payment_count: originalPayments.length,
1567
+ remote_payment_count: filteredPayments.length,
1568
+ filtered_pending_count: removedPayments.length,
1569
+ filtered_payment_identities: removedPayments.map((payment) => (0, import_payment_utils.getOrderPaymentIdentityKeys)(payment))
1570
+ });
1571
+ return next;
1572
+ }
1573
+ mergeCheckoutSyncPayments(checkoutData, fresh, title) {
1574
+ const localPayments = Array.isArray(checkoutData == null ? void 0 : checkoutData.payments) ? checkoutData.payments : [];
1575
+ const freshRecord = fresh;
1576
+ if (!Array.isArray(freshRecord.payments)) {
1577
+ return localPayments.length > 0 ? { ...freshRecord, payments: (0, import_lodash_es.cloneDeep)(localPayments) } : freshRecord;
1578
+ }
1579
+ const freshPayments = freshRecord.payments;
1580
+ const mergedPayments = (0, import_payment_utils.mergeOrderPaymentListsByIdentity)(
1581
+ localPayments,
1582
+ freshPayments,
1583
+ { preserveUnmatchedExistingWhen: import_payment_utils.isIdentifiedPendingOrderPayment }
1584
+ );
1585
+ const remoteIdentityKeys = new Set(
1586
+ freshPayments.flatMap((payment) => (0, import_payment_utils.getOrderPaymentIdentityKeys)(payment))
1587
+ );
1588
+ const preservedPending = localPayments.filter((payment) => (0, import_payment_utils.isIdentifiedPendingOrderPayment)(payment) && !(0, import_payment_utils.getOrderPaymentIdentityKeys)(payment).some((key) => remoteIdentityKeys.has(key)));
1589
+ if (preservedPending.length > 0) {
1590
+ this.logInfo(`${title}: checkout 响应合并保留本地 pending 支付项`, {
1591
+ external_sale_number: checkoutData == null ? void 0 : checkoutData.external_sale_number,
1592
+ preserved_pending_count: preservedPending.length,
1593
+ preserved_payment_identities: preservedPending.map((payment) => (0, import_payment_utils.getOrderPaymentIdentityKeys)(payment))
1594
+ });
1595
+ }
1596
+ return {
1597
+ ...freshRecord,
1598
+ payments: mergedPayments
1599
+ };
1600
+ }
1434
1601
  async persistSyncedCheckoutOrder(params) {
1435
1602
  const {
1436
1603
  backendPath,
@@ -1445,15 +1612,17 @@ var Server = class {
1445
1612
  if (this.order && fresh && (shouldMergeRemoteOrder || persistCheckoutDataWithResponse)) {
1446
1613
  syncedOrder = persistCheckoutDataWithResponse ? {
1447
1614
  ...checkoutData,
1448
- ...fresh,
1615
+ ...this.mergeCheckoutSyncPayments(checkoutData, fresh, title),
1449
1616
  external_sale_number: fresh.external_sale_number ?? externalSaleNumber,
1450
- need_sync: 0
1617
+ need_sync: 0,
1618
+ is_draft_order: 0
1451
1619
  } : {
1452
- ...fresh,
1620
+ ...this.mergeCheckoutSyncPayments(checkoutData, fresh, title),
1453
1621
  external_sale_number: fresh.external_sale_number ?? externalSaleNumber,
1454
- need_sync: 0
1622
+ need_sync: 0,
1623
+ is_draft_order: 0
1455
1624
  };
1456
- await this.order.upsertOrdersFromRemote([syncedOrder]);
1625
+ await this.order.upsertOrdersFromRemote([syncedOrder], "checkoutSync");
1457
1626
  this.logInfo(`${title}: sync_sales 完整订单已同步到本地`, {
1458
1627
  order_id: fresh.order_id,
1459
1628
  external_sale_number: syncedOrder.external_sale_number,
@@ -1471,7 +1640,8 @@ var Server = class {
1471
1640
  ...checkoutData,
1472
1641
  ...patch,
1473
1642
  external_sale_number: externalSaleNumber,
1474
- need_sync: 0
1643
+ need_sync: 0,
1644
+ is_draft_order: 0
1475
1645
  };
1476
1646
  this.logInfo(`${title}: sync_sales 已按 external_sale_number 标记本地订单`, {
1477
1647
  order_id: (syncedOrder == null ? void 0 : syncedOrder.order_id) ?? null,
@@ -1851,6 +2021,11 @@ var Server = class {
1851
2021
  path: "/shop/order/sales/checkout",
1852
2022
  handler: this.handleOrderSalesCheckout.bind(this)
1853
2023
  },
2024
+ {
2025
+ method: "post",
2026
+ path: "/shop/order/sales/draft",
2027
+ handler: this.handleOrderSalesDraft.bind(this)
2028
+ },
1854
2029
  {
1855
2030
  method: "post",
1856
2031
  path: "/order/checkout",
@@ -1861,6 +2036,16 @@ var Server = class {
1861
2036
  path: "/shop/local/print-order",
1862
2037
  handler: this.handleLocalPrintOrder.bind(this)
1863
2038
  },
2039
+ {
2040
+ method: "post",
2041
+ path: "/shop/local/getLocalPayment",
2042
+ handler: this.handleGetLocalPayment.bind(this)
2043
+ },
2044
+ {
2045
+ method: "post",
2046
+ path: "/shop/local/updateLocalPayment",
2047
+ handler: this.handleUpdateLocalPayment.bind(this)
2048
+ },
1864
2049
  {
1865
2050
  method: "post",
1866
2051
  path: "/shop/machinecode/batch",
@@ -2092,43 +2277,6 @@ var Server = class {
2092
2277
  this.scheduleSubscriberTimePointReloads(subscriberId, timePoints);
2093
2278
  }
2094
2279
  }
2095
- /**
2096
- * ProductList callers such as standalone retail pages do not always have a selected schedule.
2097
- * In that case query against the current local time so menu availability can still be evaluated.
2098
- */
2099
- resolveProductQueryScheduleContext(params) {
2100
- const now = (0, import_dayjs.default)();
2101
- const schedule_datetime = typeof params.schedule_datetime === "string" && params.schedule_datetime.trim() ? params.schedule_datetime : now.format("YYYY-MM-DD HH:mm:ss");
2102
- const schedule_date = typeof params.schedule_date === "string" && params.schedule_date.trim() ? params.schedule_date : (0, import_dayjs.default)(schedule_datetime).isValid() ? (0, import_dayjs.default)(schedule_datetime).format("YYYY-MM-DD") : now.format("YYYY-MM-DD");
2103
- return { schedule_date, schedule_datetime };
2104
- }
2105
- /**
2106
- * OpenData 的关联餐牌可能已更新,而 server_menu/server_schedule 仍命中本地持久化缓存。
2107
- * 仅在当前筛选没有任何生效餐牌时刷新一次,并由调用方重新计算筛选结果。
2108
- */
2109
- async refreshMenuScheduleCacheAfterEmptyResult() {
2110
- if (!this.menu || !this.schedule)
2111
- return false;
2112
- const now = Date.now();
2113
- if (now - this.lastMenuScheduleRefreshAt < this.MENU_SCHEDULE_REFRESH_COOLDOWN_MS) {
2114
- return false;
2115
- }
2116
- if (!this.menuScheduleRefreshInFlight) {
2117
- this.lastMenuScheduleRefreshAt = now;
2118
- this.menuScheduleRefreshInFlight = Promise.all([
2119
- this.menu.loadMenuList(),
2120
- this.schedule.loadAllSchedule()
2121
- ]).then(() => void 0).catch((error) => {
2122
- this.logWarning("刷新菜单/日程缓存失败,继续使用现有缓存", {
2123
- error: error instanceof Error ? error.message : String(error)
2124
- });
2125
- }).finally(() => {
2126
- this.menuScheduleRefreshInFlight = null;
2127
- });
2128
- }
2129
- await this.menuScheduleRefreshInFlight;
2130
- return true;
2131
- }
2132
2280
  /**
2133
2281
  * 执行 sales 搜索并登记可见订单号。
2134
2282
  * 搜索参数完全透传后端,OS 不做过滤、筛选、排序。
@@ -2739,6 +2887,405 @@ var Server = class {
2739
2887
  };
2740
2888
  }
2741
2889
  }
2890
+ createLocalPaymentOperationId(action) {
2891
+ return `local_payment_${action}_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
2892
+ }
2893
+ buildLocalPaymentOrderLogContext(order) {
2894
+ if (!order)
2895
+ return {};
2896
+ return {
2897
+ order_id: order.order_id ?? null,
2898
+ external_sale_number: order.external_sale_number ?? null,
2899
+ order_number: order.order_number ?? null,
2900
+ shop_order_number: order.shop_order_number ?? null,
2901
+ shop_full_order_number: order.shop_full_order_number ?? null,
2902
+ order_status: order.status ?? null,
2903
+ payment_status: order.payment_status ?? null,
2904
+ is_draft_order: Number(order.is_draft_order || 0),
2905
+ need_sync: Number(order.need_sync || 0),
2906
+ platform: order.platform ?? null,
2907
+ business_code: order.business_code ?? null,
2908
+ order_sales_channel: order.order_sales_channel ?? null,
2909
+ sales_channel: order.sales_channel ?? null,
2910
+ order_type: order.type ?? null,
2911
+ payments_count: Array.isArray(order.payments) ? order.payments.length : 0
2912
+ };
2913
+ }
2914
+ isValidLocalPaymentDecimal(value) {
2915
+ if (typeof value !== "string" && typeof value !== "number")
2916
+ return false;
2917
+ if (typeof value === "string" && value.trim() === "")
2918
+ return false;
2919
+ try {
2920
+ return new import_decimal.default(value).isFinite();
2921
+ } catch {
2922
+ return false;
2923
+ }
2924
+ }
2925
+ getInvalidLocalPaymentSurchargeFields(serviceCharge, cardReaderSurcharge) {
2926
+ const invalidFields = [];
2927
+ if (serviceCharge !== void 0) {
2928
+ if (serviceCharge === null || typeof serviceCharge !== "object" || Array.isArray(serviceCharge)) {
2929
+ invalidFields.push("service_charge");
2930
+ } else {
2931
+ const input = serviceCharge;
2932
+ if (input.fixed !== void 0 && !this.isValidLocalPaymentDecimal(input.fixed)) {
2933
+ invalidFields.push("service_charge.fixed");
2934
+ }
2935
+ if (input.percentage !== void 0 && !this.isValidLocalPaymentDecimal(input.percentage)) {
2936
+ invalidFields.push("service_charge.percentage");
2937
+ }
2938
+ }
2939
+ }
2940
+ if (cardReaderSurcharge !== void 0 && !this.isValidLocalPaymentDecimal(cardReaderSurcharge)) {
2941
+ invalidFields.push("card_reader_surcharge");
2942
+ }
2943
+ return invalidFields;
2944
+ }
2945
+ buildLocalPaymentSurchargeUpdate(params) {
2946
+ var _a, _b, _c;
2947
+ const amount = new import_decimal.default(((_a = params.serviceCharge) == null ? void 0 : _a.fixed) || 0).plus(params.cardReaderSurcharge || 0).toFixed(2);
2948
+ const serviceFee = new import_decimal.default(params.paymentAmount || 0).times(((_b = params.serviceCharge) == null ? void 0 : _b.percentage) || 0).plus(amount).toDecimalPlaces(2).toFixed(2);
2949
+ return {
2950
+ service_charge: {
2951
+ amount,
2952
+ ...((_c = params.serviceCharge) == null ? void 0 : _c.percentage) !== void 0 ? { percentage: params.serviceCharge.percentage } : {}
2953
+ },
2954
+ service_fee: serviceFee
2955
+ };
2956
+ }
2957
+ async runLocalPaymentUpdateInQueue(key, task, context) {
2958
+ var _a;
2959
+ const queuedAt = Date.now();
2960
+ const hasPreviousTask = this.localPaymentUpdateQueues.has(key);
2961
+ const previous = this.localPaymentUpdateQueues.get(key) || Promise.resolve();
2962
+ this.logInfo("handleUpdateLocalPayment: 串行队列入队", {
2963
+ ...context,
2964
+ queue_key: key,
2965
+ waiting_for_previous: hasPreviousTask,
2966
+ active_queue_count: this.localPaymentUpdateQueues.size
2967
+ });
2968
+ const current = previous.catch(() => void 0).then(async () => {
2969
+ this.logInfo("handleUpdateLocalPayment: 串行队列开始执行", {
2970
+ ...context,
2971
+ queue_key: key,
2972
+ queue_wait_ms: Date.now() - queuedAt
2973
+ });
2974
+ return task();
2975
+ });
2976
+ const tail = current.then(() => void 0, () => void 0);
2977
+ this.localPaymentUpdateQueues.set(key, tail);
2978
+ try {
2979
+ const result = await current;
2980
+ this.logInfo("handleUpdateLocalPayment: 串行队列执行完成", {
2981
+ ...context,
2982
+ queue_key: key,
2983
+ total_duration_ms: Date.now() - queuedAt,
2984
+ result_code: result == null ? void 0 : result.code,
2985
+ result_status: result == null ? void 0 : result.status,
2986
+ payment: (_a = result == null ? void 0 : result.data) == null ? void 0 : _a.payment
2987
+ });
2988
+ return result;
2989
+ } catch (error) {
2990
+ this.logError("handleUpdateLocalPayment: 串行队列执行失败", {
2991
+ ...context,
2992
+ queue_key: key,
2993
+ total_duration_ms: Date.now() - queuedAt,
2994
+ error: error instanceof Error ? error.message : String(error),
2995
+ error_detail: this.normalizeUnknownError(error)
2996
+ });
2997
+ throw error;
2998
+ } finally {
2999
+ let released = false;
3000
+ if (this.localPaymentUpdateQueues.get(key) === tail) {
3001
+ this.localPaymentUpdateQueues.delete(key);
3002
+ released = true;
3003
+ }
3004
+ this.logInfo("handleUpdateLocalPayment: 串行队列释放", {
3005
+ ...context,
3006
+ queue_key: key,
3007
+ released,
3008
+ active_queue_count: this.localPaymentUpdateQueues.size
3009
+ });
3010
+ }
3011
+ }
3012
+ async processLocalPaymentUpdate(params) {
3013
+ var _a, _b, _c;
3014
+ const requestContext = {
3015
+ operation_id: params.operationId,
3016
+ sale_id: params.saleId,
3017
+ payment_number: params.paymentNumber,
3018
+ transaction_number: params.number,
3019
+ requested_status: params.status,
3020
+ service_charge: params.serviceCharge ?? null,
3021
+ card_reader_surcharge: params.cardReaderSurcharge ?? null
3022
+ };
3023
+ this.logInfo("processLocalPaymentUpdate: 开始处理", requestContext);
3024
+ if (!this.order) {
3025
+ this.logError("processLocalPaymentUpdate: Order 模块未注册", requestContext);
3026
+ return { code: 500, status: false, message: "Order 模块未注册", data: null };
3027
+ }
3028
+ this.logInfo("processLocalPaymentUpdate: 开始查询本地支付项", requestContext);
3029
+ const found = await this.order.getLocalPayment({
3030
+ saleId: params.saleId,
3031
+ paymentNumber: params.paymentNumber,
3032
+ operationId: params.operationId
3033
+ });
3034
+ if (!found) {
3035
+ this.logWarning("processLocalPaymentUpdate: 本地支付项不存在", requestContext);
3036
+ return { code: 404, status: false, message: "本地支付项不存在", data: null };
3037
+ }
3038
+ const surchargeUpdate = params.status === "success" && params.hasSurchargeInput ? this.buildLocalPaymentSurchargeUpdate({
3039
+ paymentAmount: (_a = found.payment) == null ? void 0 : _a.amount,
3040
+ serviceCharge: params.serviceCharge,
3041
+ cardReaderSurcharge: params.cardReaderSurcharge
3042
+ }) : void 0;
3043
+ const incomingPaymentUpdate = params.status === "success" ? {
3044
+ status: "paid",
3045
+ ...surchargeUpdate || {},
3046
+ metadata: {
3047
+ unique_payment_number: params.number,
3048
+ transactions: [{ number: params.number, status: "success" }]
3049
+ }
3050
+ } : {
3051
+ metadata: {
3052
+ transactions: [{ number: params.number, status: "fail" }]
3053
+ }
3054
+ };
3055
+ const transactions = Array.isArray((_c = (_b = found.payment) == null ? void 0 : _b.metadata) == null ? void 0 : _c.transactions) ? found.payment.metadata.transactions : [];
3056
+ const existingTransaction = transactions.find((item) => String((item == null ? void 0 : item.number) || "") === params.number);
3057
+ const hasRecordedSuccess = transactions.some((item) => (item == null ? void 0 : item.status) === "success");
3058
+ const orderIsDraft = Number(found.order.is_draft_order || 0) === 1;
3059
+ const foundContext = {
3060
+ ...requestContext,
3061
+ matched_by: found.matchedBy,
3062
+ payment_index: found.paymentIndex,
3063
+ ...this.buildLocalPaymentOrderLogContext(found.order),
3064
+ payment: found.payment,
3065
+ incoming_payment_update: incomingPaymentUpdate,
3066
+ existing_transaction: existingTransaction || null,
3067
+ has_recorded_success: hasRecordedSuccess
3068
+ };
3069
+ this.logInfo("processLocalPaymentUpdate: 查询命中", foundContext);
3070
+ if (params.status === "fail" && (found.payment.status === "paid" || hasRecordedSuccess)) {
3071
+ this.logInfo("processLocalPaymentUpdate: 幂等返回", {
3072
+ ...foundContext,
3073
+ idempotent_reason: "ignore_failure_after_success"
3074
+ });
3075
+ return {
3076
+ code: 200,
3077
+ status: true,
3078
+ message: "",
3079
+ data: { payment: found.payment, order: found.order, idempotent: true }
3080
+ };
3081
+ }
3082
+ const alreadyRecordedFailure = params.status === "fail" && (existingTransaction == null ? void 0 : existingTransaction.status) === "fail";
3083
+ if (alreadyRecordedFailure) {
3084
+ this.logInfo("processLocalPaymentUpdate: 幂等返回", {
3085
+ ...foundContext,
3086
+ idempotent_reason: "failure_already_recorded"
3087
+ });
3088
+ return {
3089
+ code: 200,
3090
+ status: true,
3091
+ message: "",
3092
+ data: { payment: found.payment, order: found.order, idempotent: true }
3093
+ };
3094
+ }
3095
+ if (params.status === "success" && (existingTransaction == null ? void 0 : existingTransaction.status) === "success" && found.payment.status === "paid" && !orderIsDraft) {
3096
+ this.logInfo("processLocalPaymentUpdate: 幂等返回", {
3097
+ ...foundContext,
3098
+ idempotent_reason: "success_already_committed"
3099
+ });
3100
+ return {
3101
+ code: 200,
3102
+ status: true,
3103
+ message: "",
3104
+ data: { payment: found.payment, order: found.order, idempotent: true }
3105
+ };
3106
+ }
3107
+ if (params.status === "success") {
3108
+ this.logInfo("processLocalPaymentUpdate: 进入 success 恢复分支", {
3109
+ ...foundContext,
3110
+ force_submit_if_paid: found.payment.status === "paid" && orderIsDraft
3111
+ });
3112
+ return this.processSuccessfulLocalPayment({
3113
+ ...params,
3114
+ order: found.order,
3115
+ payment: found.payment,
3116
+ matchBy: found.matchedBy === "payment_number" ? "payment_number" : "compat",
3117
+ forceSubmitIfPaid: found.payment.status === "paid" && orderIsDraft,
3118
+ paymentUpdate: incomingPaymentUpdate
3119
+ });
3120
+ }
3121
+ this.logInfo("processLocalPaymentUpdate: 进入 fail 本地更新分支", {
3122
+ ...foundContext,
3123
+ incoming_payment_update: incomingPaymentUpdate
3124
+ });
3125
+ let updated;
3126
+ try {
3127
+ updated = await this.order.updateLocalPayment({
3128
+ saleId: params.saleId,
3129
+ paymentNumber: params.paymentNumber,
3130
+ operationId: params.operationId,
3131
+ updates: incomingPaymentUpdate
3132
+ });
3133
+ } catch (error) {
3134
+ this.logError("processLocalPaymentUpdate: fail 本地更新失败", {
3135
+ ...foundContext,
3136
+ incoming_payment_update: incomingPaymentUpdate,
3137
+ error: error instanceof Error ? error.message : String(error),
3138
+ error_detail: this.normalizeUnknownError(error)
3139
+ });
3140
+ throw error;
3141
+ }
3142
+ if (!updated) {
3143
+ this.logWarning("processLocalPaymentUpdate: fail 本地更新未命中", foundContext);
3144
+ return { code: 404, status: false, message: "本地支付项不存在", data: null };
3145
+ }
3146
+ this.logInfo("processLocalPaymentUpdate: fail 本地更新完成", {
3147
+ ...requestContext,
3148
+ ...this.buildLocalPaymentOrderLogContext(updated.order),
3149
+ previous_payment: updated.previousPayment,
3150
+ payment: updated.payment
3151
+ });
3152
+ return {
3153
+ code: 200,
3154
+ status: true,
3155
+ message: "",
3156
+ data: {
3157
+ local_only: true,
3158
+ payment: updated.payment,
3159
+ order: updated.order
3160
+ }
3161
+ };
3162
+ }
3163
+ async processSuccessfulLocalPayment(params) {
3164
+ var _a, _b, _c, _d, _e;
3165
+ const moduleName = `local_payment_recovery_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
3166
+ const baseSales = new import_BaseSales.BaseSalesImpl(moduleName);
3167
+ const startedAt = Date.now();
3168
+ const recoveryContext = {
3169
+ operation_id: params.operationId,
3170
+ sale_id: params.saleId,
3171
+ payment_number: params.paymentNumber,
3172
+ transaction_number: params.number,
3173
+ requested_status: "success",
3174
+ match_by: params.matchBy,
3175
+ force_submit_if_paid: params.forceSubmitIfPaid,
3176
+ incoming_payment_update: params.paymentUpdate,
3177
+ base_sales_module_name: moduleName,
3178
+ ...this.buildLocalPaymentOrderLogContext(params.order),
3179
+ payment: params.payment
3180
+ };
3181
+ this.logInfo("processSuccessfulLocalPayment: 创建临时 BaseSales", recoveryContext);
3182
+ try {
3183
+ const order = this.sanitizeLocalRecoveryOrder(params.order);
3184
+ this.logInfo("processSuccessfulLocalPayment: 本地订单清洗完成", {
3185
+ ...recoveryContext,
3186
+ sanitized_order_id: order.order_id ?? null,
3187
+ removed_synthetic_order_id: params.order.order_id != null && order.order_id == null
3188
+ });
3189
+ await baseSales.initialize(this.core, {
3190
+ otherParams: {
3191
+ platform: order.platform,
3192
+ businessCode: order.business_code,
3193
+ business_code: order.business_code,
3194
+ channel: order.order_sales_channel || order.sales_channel,
3195
+ type: order.type,
3196
+ enableTempOrderPersist: false
3197
+ }
3198
+ });
3199
+ this.logInfo("processSuccessfulLocalPayment: BaseSales 初始化完成", recoveryContext);
3200
+ await baseSales.replaceTempOrder(order);
3201
+ this.logInfo("processSuccessfulLocalPayment: 本地订单 tempOrder 恢复完成", recoveryContext);
3202
+ this.logInfo("processSuccessfulLocalPayment: 开始更新支付项并提交", {
3203
+ ...recoveryContext,
3204
+ incoming_payment_update: params.paymentUpdate
3205
+ });
3206
+ const result = await baseSales.updateOrderPayment(
3207
+ params.paymentNumber,
3208
+ params.paymentUpdate,
3209
+ {
3210
+ submitWhenPaid: true,
3211
+ matchBy: params.matchBy,
3212
+ forceSubmitIfPaid: params.forceSubmitIfPaid,
3213
+ applyUpdatesWhenPaid: params.forceSubmitIfPaid,
3214
+ smallTicketDataFlag: 1
3215
+ }
3216
+ );
3217
+ this.logInfo("processSuccessfulLocalPayment: 支付恢复完成", {
3218
+ ...recoveryContext,
3219
+ duration_ms: Date.now() - startedAt,
3220
+ payment: result.payment,
3221
+ payments_count: ((_a = result.payments) == null ? void 0 : _a.length) || 0,
3222
+ summary: result.summary,
3223
+ sync_result: result.syncResult ? {
3224
+ is_fully_paid: result.syncResult.isFullyPaid,
3225
+ is_order_fully_paid: result.syncResult.isOrderFullyPaid,
3226
+ is_deposit_fully_paid: result.syncResult.isDepositFullyPaid,
3227
+ payment_stage: result.syncResult.paymentStage,
3228
+ deposit_amount: result.syncResult.depositAmount,
3229
+ order_expected_amount: result.syncResult.orderExpectedAmount,
3230
+ submit_result_code: (_b = result.syncResult.submitResult) == null ? void 0 : _b.code,
3231
+ submit_result_status: (_c = result.syncResult.submitResult) == null ? void 0 : _c.status
3232
+ } : null,
3233
+ draft_result: result.draftResult ? {
3234
+ code: (_d = result.draftResult) == null ? void 0 : _d.code,
3235
+ status: (_e = result.draftResult) == null ? void 0 : _e.status
3236
+ } : null,
3237
+ draft_error: result.draftError instanceof Error ? result.draftError.message : result.draftError
3238
+ });
3239
+ return {
3240
+ code: 200,
3241
+ status: true,
3242
+ message: "",
3243
+ data: {
3244
+ payment: result.payment,
3245
+ payments: result.payments,
3246
+ summary: result.summary,
3247
+ sync_result: result.syncResult
3248
+ }
3249
+ };
3250
+ } catch (error) {
3251
+ const message = error instanceof Error ? error.message : String(error);
3252
+ this.logError("processSuccessfulLocalPayment: 支付恢复失败", {
3253
+ ...recoveryContext,
3254
+ duration_ms: Date.now() - startedAt,
3255
+ error: message,
3256
+ error_detail: this.normalizeUnknownError(error)
3257
+ });
3258
+ return { code: 500, status: false, message, data: null };
3259
+ } finally {
3260
+ try {
3261
+ await baseSales.destroy();
3262
+ this.logInfo("processSuccessfulLocalPayment: 临时 BaseSales 销毁完成", {
3263
+ ...recoveryContext,
3264
+ duration_ms: Date.now() - startedAt
3265
+ });
3266
+ } catch (error) {
3267
+ this.logError("processSuccessfulLocalPayment: 临时 BaseSales 销毁失败", {
3268
+ ...recoveryContext,
3269
+ duration_ms: Date.now() - startedAt,
3270
+ error: error instanceof Error ? error.message : String(error),
3271
+ error_detail: this.normalizeUnknownError(error)
3272
+ });
3273
+ }
3274
+ }
3275
+ }
3276
+ sanitizeLocalRecoveryOrder(order) {
3277
+ const next = {
3278
+ ...order,
3279
+ country_calling_code: String(order.country_calling_code ?? ""),
3280
+ phone: String(order.phone ?? ""),
3281
+ email: String(order.email ?? "")
3282
+ };
3283
+ const externalSaleNumber = next.external_sale_number;
3284
+ if (Number(next.is_draft_order || 0) === 1 && next.order_id !== void 0 && externalSaleNumber !== void 0 && String(next.order_id) === String(externalSaleNumber)) {
3285
+ delete next.order_id;
3286
+ }
3287
+ return next;
3288
+ }
2742
3289
  getDeviceTaskPlugin() {
2743
3290
  var _a, _b;
2744
3291
  return ((_b = (_a = this.app) == null ? void 0 : _a.getPlugin) == null ? void 0 : _b.call(_a, "deviceTask")) || null;
@@ -3232,22 +3779,10 @@ var Server = class {
3232
3779
  data: (normalized == null ? void 0 : normalized.data) ?? null
3233
3780
  };
3234
3781
  }
3235
- const response = this.withSyncedOrderIdInCheckoutResponse(
3782
+ return this.withSyncedOrderIdInCheckoutResponse(
3236
3783
  syncResult.normalizedResponse,
3237
3784
  syncResult.syncedOrder
3238
3785
  );
3239
- const syncedOrder = syncResult.syncedOrder;
3240
- if (syncedOrder && this.shouldBuildSmallTicketData(syncResult.checkoutData) && this.isSalesOrderFullyPaid(syncedOrder)) {
3241
- await this.dispatchLocalReceiptPrint({
3242
- checkoutData: syncResult.checkoutData,
3243
- order: syncedOrder,
3244
- response: syncResult.normalizedResponse,
3245
- requireFullyPaid: true,
3246
- // print_all 的 type=0 是收据打印;不要沿用同步成功后的券/手环打印 type=99。
3247
- isManualPrint: true
3248
- });
3249
- }
3250
- return response;
3251
3786
  } catch (error) {
3252
3787
  const backendError = this.extractBackendErrorResponse(error);
3253
3788
  if (backendError) {
@@ -3273,6 +3808,59 @@ var Server = class {
3273
3808
  };
3274
3809
  }
3275
3810
  }
3811
+ async handleOrderDraftSubmit(params) {
3812
+ var _a, _b, _c;
3813
+ const { data, title } = params;
3814
+ this.logInfo(`${title}: 开始处理`, {
3815
+ order_id: data == null ? void 0 : data.order_id,
3816
+ external_sale_number: data == null ? void 0 : data.external_sale_number,
3817
+ order_number: data == null ? void 0 : data.order_number
3818
+ });
3819
+ const normalizedData = await this.normalizeCheckoutSubmitData(data, title);
3820
+ const createdAt = normalizedData.created_at || normalizedData.create_date || (0, import_dayjs.default)().format("YYYY-MM-DD HH:mm:ss");
3821
+ const draftOrder = {
3822
+ ...normalizedData,
3823
+ external_sale_number: normalizedData.external_sale_number || `LOCAL_DRAFT_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`,
3824
+ created_at: createdAt,
3825
+ schedule_date: normalizedData.schedule_date || createdAt,
3826
+ need_sync: 0,
3827
+ is_draft_order: 1
3828
+ };
3829
+ if (!this.order || typeof this.order.upsertLocalDraftOrders !== "function") {
3830
+ this.logError(`${title}: Order 模块不支持本地草稿写入`);
3831
+ return {
3832
+ code: 500,
3833
+ status: false,
3834
+ message: "Order 模块不支持本地草稿写入",
3835
+ data: null
3836
+ };
3837
+ }
3838
+ try {
3839
+ await this.order.upsertLocalDraftOrders([draftOrder]);
3840
+ this.logInfo(`${title}: 草稿订单已写入本地`, {
3841
+ order_id: draftOrder.order_id,
3842
+ external_sale_number: draftOrder.external_sale_number
3843
+ });
3844
+ return {
3845
+ code: 200,
3846
+ status: true,
3847
+ message: "",
3848
+ data: {
3849
+ ...draftOrder,
3850
+ amount_gap: (_a = draftOrder == null ? void 0 : draftOrder.summary) == null ? void 0 : _a.amount_gap,
3851
+ expect_amount: (_b = draftOrder == null ? void 0 : draftOrder.summary) == null ? void 0 : _b.expect_amount,
3852
+ total_amount: (_c = draftOrder == null ? void 0 : draftOrder.summary) == null ? void 0 : _c.total_amount,
3853
+ payment_status: draftOrder == null ? void 0 : draftOrder.payment_status
3854
+ }
3855
+ };
3856
+ } catch (error) {
3857
+ const errorMessage = error instanceof Error ? error.message : String(error);
3858
+ this.logError(`${title}: 写入本地草稿订单失败`, {
3859
+ error: errorMessage
3860
+ });
3861
+ return { code: 500, status: false, message: errorMessage, data: null };
3862
+ }
3863
+ }
3276
3864
  async handlePendingSyncCheckoutOrder(params) {
3277
3865
  var _a, _b, _c, _d;
3278
3866
  const { backendPath, data, title, reason } = params;
@@ -3348,7 +3936,8 @@ var Server = class {
3348
3936
  return null;
3349
3937
  const pendingOrder = {
3350
3938
  ...data,
3351
- need_sync: 1
3939
+ need_sync: 1,
3940
+ is_draft_order: 0
3352
3941
  };
3353
3942
  const productMap = await this.buildSmallTicketProductMap(pendingOrder);
3354
3943
  const orderWithProductTitles = this.withPendingSyncProductTitles(
@@ -3401,7 +3990,7 @@ var Server = class {
3401
3990
  return 0;
3402
3991
  return payments.reduce((sum, payment) => {
3403
3992
  const status = String((payment == null ? void 0 : payment.status) || "").toLowerCase();
3404
- if (status === "voided" || status === "failed" || status === "cancelled")
3993
+ if (status !== "paid")
3405
3994
  return sum;
3406
3995
  return sum + this.toAmountNumber((payment == null ? void 0 : payment.amount) ?? (payment == null ? void 0 : payment.origin_amount));
3407
3996
  }, 0);
@@ -3968,9 +4557,11 @@ var Server = class {
3968
4557
  return value === void 0 ? void 0 : String(value);
3969
4558
  }
3970
4559
  buildCheckoutSyncSuccessPatch(fresh) {
4560
+ const patch = {
4561
+ is_draft_order: 0
4562
+ };
3971
4563
  if (!fresh)
3972
- return {};
3973
- const patch = {};
4564
+ return patch;
3974
4565
  if (fresh.order_id !== void 0 && fresh.order_id !== null)
3975
4566
  patch.order_id = fresh.order_id;
3976
4567
  if (fresh.order_number !== void 0 && fresh.order_number !== null) {
@@ -4251,6 +4842,10 @@ var Server = class {
4251
4842
  const rawList = this.order.getOrders();
4252
4843
  this.logInfo("computeOrderQueryResult: 本地订单数量", { rawCount: rawList.length });
4253
4844
  const result = (0, import_filterOrders.filterOrders)(rawList, data);
4845
+ result.list.forEach((order) => {
4846
+ var _a;
4847
+ order.payments = (_a = order.payments) == null ? void 0 : _a.filter((payment) => !(0, import_payment_utils.isPendingOrderPayment)(payment));
4848
+ });
4254
4849
  this.logInfo("computeOrderQueryResult: 过滤结果", {
4255
4850
  rawCount: rawList.length,
4256
4851
  filteredCount: result.count,
@@ -4477,16 +5072,13 @@ var Server = class {
4477
5072
  let activeMenuList = [];
4478
5073
  if (menu_list_ids && Array.isArray(menu_list_ids) && menu_list_ids.length > 0) {
4479
5074
  const tMenu = performance.now();
4480
- const getActiveMenus = () => this.menu.getMenuByIds(menu_list_ids).filter((menu) => {
5075
+ const menuList = this.menu.getMenuByIds(menu_list_ids);
5076
+ activeMenuList = menuList.filter((menu) => {
4481
5077
  var _a;
4482
5078
  return ((_a = this.schedule) == null ? void 0 : _a.getDateIsInSchedule(schedule_datetime, menu.schedule)) || false;
4483
5079
  });
4484
- activeMenuList = getActiveMenus();
4485
- if (!activeMenuList.length && await this.refreshMenuScheduleCacheAfterEmptyResult()) {
4486
- activeMenuList = getActiveMenus();
4487
- }
4488
5080
  (0, import_product.perfMark)("computeQuery.filterActiveMenu", performance.now() - tMenu, {
4489
- totalMenu: menu_list_ids.length,
5081
+ totalMenu: menuList.length,
4490
5082
  activeMenu: activeMenuList.length
4491
5083
  });
4492
5084
  }