@pisell/pisellos 0.0.568 → 0.10.1

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 (588) hide show
  1. package/dist/core/index.js +10 -6
  2. package/dist/index.d.ts +0 -1
  3. package/dist/index.js +0 -3
  4. package/dist/model/strategy/adapter/promotion/evaluator.js +3 -2
  5. package/dist/model/strategy/adapter/walletPass/evaluator.js +7 -5
  6. package/dist/model/strategy/adapter/walletPass/example.js +26 -3
  7. package/dist/model/strategy/adapter/walletPass/index.js +5 -5
  8. package/dist/model/strategy/adapter/walletPass/type.d.ts +0 -3
  9. package/dist/model/strategy/adapter/walletPass/utils.d.ts +0 -26
  10. package/dist/model/strategy/adapter/walletPass/utils.js +24 -96
  11. package/dist/model/strategy/strategy-example.js +19 -5
  12. package/dist/modules/BaseModule.d.ts +2 -2
  13. package/dist/modules/BaseModule.js +12 -14
  14. package/dist/modules/BookingContext/index.d.ts +48 -0
  15. package/dist/modules/BookingContext/index.js +566 -0
  16. package/dist/modules/BookingContext/types.d.ts +102 -0
  17. package/dist/modules/BookingContext/types.js +51 -0
  18. package/dist/modules/BookingContext/utils/buildCacheItemFromOrderLine.d.ts +16 -0
  19. package/dist/modules/BookingContext/utils/buildCacheItemFromOrderLine.js +302 -0
  20. package/dist/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.d.ts +8 -0
  21. package/dist/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.js +149 -0
  22. package/dist/modules/BookingContext/utils/cacheItemToBookingInput.d.ts +76 -0
  23. package/dist/modules/BookingContext/utils/cacheItemToBookingInput.js +249 -0
  24. package/dist/modules/BookingContext/utils/flexible.d.ts +28 -0
  25. package/dist/modules/BookingContext/utils/flexible.js +56 -0
  26. package/dist/modules/BookingContext/utils/formatResourceList.d.ts +8 -0
  27. package/dist/modules/BookingContext/utils/formatResourceList.js +38 -0
  28. package/dist/modules/BookingContext/utils/index.d.ts +11 -0
  29. package/dist/modules/BookingContext/utils/index.js +11 -0
  30. package/dist/modules/BookingContext/utils/noResource.d.ts +13 -0
  31. package/dist/modules/BookingContext/utils/noResource.js +77 -0
  32. package/dist/modules/BookingContext/utils/orderLineDisplay.d.ts +1 -0
  33. package/dist/modules/BookingContext/utils/orderLineDisplay.js +36 -0
  34. package/dist/modules/BookingContext/utils/productExtend.d.ts +72 -0
  35. package/dist/modules/BookingContext/utils/productExtend.js +339 -0
  36. package/dist/modules/BookingContext/utils/resourceErrors.d.ts +41 -0
  37. package/dist/modules/BookingContext/utils/resourceErrors.js +74 -0
  38. package/dist/modules/BookingContext/utils/resourceSelection.d.ts +7 -0
  39. package/dist/modules/BookingContext/utils/resourceSelection.js +24 -0
  40. package/dist/modules/BookingContext/utils/resources.d.ts +80 -0
  41. package/dist/modules/BookingContext/utils/resources.js +486 -0
  42. package/dist/modules/BookingContext/utils/serviceTimes.d.ts +55 -0
  43. package/dist/modules/BookingContext/utils/serviceTimes.js +151 -0
  44. package/dist/modules/BookingContext/utils/timeSlices.d.ts +42 -0
  45. package/dist/modules/BookingContext/utils/timeSlices.js +100 -0
  46. package/dist/modules/Cart/index.js +3 -0
  47. package/dist/modules/Cart/utils/cartProduct.js +27 -5
  48. package/dist/modules/Cart/utils/cartResource.js +0 -2
  49. package/dist/modules/Cart/utils/changePrice.js +11 -11
  50. package/dist/modules/Customer/index.d.ts +20 -0
  51. package/dist/modules/Customer/index.js +172 -83
  52. package/dist/modules/Customer/types.d.ts +2 -0
  53. package/dist/modules/Date/index.js +5 -1
  54. package/dist/modules/Discount/index.d.ts +2 -2
  55. package/dist/modules/Discount/index.js +43 -30
  56. package/dist/modules/Discount/types.d.ts +1 -1
  57. package/dist/modules/OpenData/index.d.ts +8 -0
  58. package/dist/modules/OpenData/index.js +38 -16
  59. package/dist/modules/Order/index.d.ts +449 -24
  60. package/dist/modules/Order/index.js +5460 -574
  61. package/dist/modules/Order/types.d.ts +693 -88
  62. package/dist/modules/Order/types.js +68 -1
  63. package/dist/modules/Order/utils/bundleDiscountHydration.d.ts +5 -0
  64. package/dist/modules/Order/utils/bundleDiscountHydration.js +144 -0
  65. package/dist/modules/Order/utils/manualProductDiscount.d.ts +106 -0
  66. package/dist/modules/Order/utils/manualProductDiscount.js +210 -0
  67. package/dist/modules/Order/utils/orderCollectionIdentity.d.ts +53 -0
  68. package/dist/modules/Order/utils/orderCollectionIdentity.js +405 -0
  69. package/dist/modules/Order/utils.d.ts +167 -18
  70. package/dist/modules/Order/utils.js +960 -165
  71. package/dist/modules/Payment/index.d.ts +10 -9
  72. package/dist/modules/Payment/index.js +573 -746
  73. package/dist/modules/Payment/types.d.ts +44 -24
  74. package/dist/modules/Payment/utils.d.ts +1 -1
  75. package/dist/modules/Payment/utils.js +4 -3
  76. package/dist/modules/Payment/walletpass.d.ts +0 -2
  77. package/dist/modules/Payment/walletpass.js +49 -33
  78. package/dist/modules/Product/types.d.ts +29 -7
  79. package/dist/modules/ProductList/index.d.ts +17 -13
  80. package/dist/modules/ProductList/index.js +182 -81
  81. package/dist/modules/ProductList/types.d.ts +16 -0
  82. package/dist/modules/Quotation/index.d.ts +22 -2
  83. package/dist/modules/Quotation/index.js +108 -42
  84. package/dist/modules/Quotation/types.d.ts +19 -0
  85. package/dist/modules/ResourcePlanner/index.d.ts +24 -0
  86. package/dist/modules/ResourcePlanner/index.js +181 -0
  87. package/dist/modules/ResourcePlanner/planner.d.ts +14 -0
  88. package/dist/modules/ResourcePlanner/planner.js +1069 -0
  89. package/dist/modules/ResourcePlanner/types.d.ts +227 -0
  90. package/dist/modules/ResourcePlanner/types.js +1 -0
  91. package/dist/modules/Rules/index.d.ts +8 -10
  92. package/dist/modules/Rules/index.js +324 -513
  93. package/dist/modules/Rules/types.d.ts +14 -3
  94. package/dist/modules/SalesSummary/index.d.ts +23 -26
  95. package/dist/modules/SalesSummary/index.js +152 -46
  96. package/dist/modules/SalesSummary/types.d.ts +9 -1
  97. package/dist/modules/SalesSummary/utils.d.ts +3 -22
  98. package/dist/modules/SalesSummary/utils.js +804 -165
  99. package/dist/modules/Schedule/getDateIsInSchedule.js +14 -12
  100. package/dist/modules/Schedule/index.d.ts +18 -5
  101. package/dist/modules/Schedule/index.js +79 -24
  102. package/dist/modules/Schedule/types.d.ts +2 -0
  103. package/dist/modules/Schedule/utils.js +1 -0
  104. package/dist/modules/Step/index.js +1 -0
  105. package/dist/modules/Summary/index.js +4 -3
  106. package/dist/modules/Summary/utils.js +22 -43
  107. package/dist/modules/SurchargeList/index.d.ts +18 -0
  108. package/dist/modules/SurchargeList/index.js +185 -0
  109. package/dist/modules/SurchargeList/types.d.ts +15 -0
  110. package/dist/modules/SurchargeList/types.js +1 -0
  111. package/dist/modules/index.d.ts +3 -0
  112. package/dist/modules/index.js +4 -1
  113. package/dist/plugins/app.d.ts +2 -0
  114. package/dist/plugins/request.d.ts +1 -0
  115. package/dist/plugins/request.js +4 -1
  116. package/dist/plugins/window.js +6 -2
  117. package/dist/server/index.d.ts +462 -8
  118. package/dist/server/index.js +6168 -680
  119. package/dist/server/modules/floor-plan/index.d.ts +39 -0
  120. package/dist/server/modules/floor-plan/index.js +595 -0
  121. package/dist/server/modules/floor-plan/types.d.ts +43 -0
  122. package/dist/server/modules/floor-plan/types.js +13 -0
  123. package/dist/server/modules/index.d.ts +7 -2
  124. package/dist/server/modules/index.js +6 -0
  125. package/dist/server/modules/menu/index.js +41 -32
  126. package/dist/server/modules/order/index.d.ts +207 -14
  127. package/dist/server/modules/order/index.js +2328 -379
  128. package/dist/server/modules/order/types.d.ts +76 -5
  129. package/dist/server/modules/order/types.js +14 -1
  130. package/dist/server/modules/order/utils/filterBookings.d.ts +7 -1
  131. package/dist/server/modules/order/utils/filterBookings.js +118 -11
  132. package/dist/server/modules/order/utils/filterOrders.js +242 -14
  133. package/dist/server/modules/payment/index.d.ts +28 -0
  134. package/dist/server/modules/payment/index.js +305 -0
  135. package/dist/server/modules/payment/types.d.ts +9 -0
  136. package/dist/server/modules/payment/types.js +1 -0
  137. package/dist/server/modules/products/index.d.ts +105 -14
  138. package/dist/server/modules/products/index.js +1416 -521
  139. package/dist/server/modules/products/types.d.ts +34 -1
  140. package/dist/server/modules/products/types.js +4 -0
  141. package/dist/server/modules/quotation/index.d.ts +39 -0
  142. package/dist/server/modules/quotation/index.js +298 -77
  143. package/dist/server/modules/resource/index.d.ts +2 -3
  144. package/dist/server/modules/resource/index.js +109 -86
  145. package/dist/server/modules/schedule/index.d.ts +6 -4
  146. package/dist/server/modules/schedule/index.js +145 -106
  147. package/dist/server/test.json +713 -0
  148. package/dist/server/types.d.ts +2 -0
  149. package/dist/server/utils/product.js +3 -2
  150. package/dist/server/utils/small-ticket.d.ts +71 -0
  151. package/dist/server/utils/small-ticket.js +963 -0
  152. package/dist/solution/BaseSales/index.d.ts +378 -0
  153. package/dist/solution/BaseSales/index.js +4009 -0
  154. package/dist/solution/BaseSales/types.d.ts +394 -0
  155. package/dist/solution/BaseSales/types.js +40 -0
  156. package/dist/solution/BaseSales/utils/cartPromotion.d.ts +287 -0
  157. package/dist/solution/BaseSales/utils/cartPromotion.js +1450 -0
  158. package/dist/solution/BaseSales/utils/parseSalesResponse.d.ts +144 -0
  159. package/dist/solution/BaseSales/utils/parseSalesResponse.js +157 -0
  160. package/dist/solution/BaseSales/utils/quotationPrice.d.ts +18 -0
  161. package/dist/solution/BaseSales/utils/quotationPrice.js +250 -0
  162. package/dist/solution/BaseSales/utils/transformBaseProductToOrderProduct.d.ts +63 -0
  163. package/dist/solution/BaseSales/utils/transformBaseProductToOrderProduct.js +218 -0
  164. package/dist/solution/BaseSales/utils.d.ts +40 -0
  165. package/dist/solution/BaseSales/utils.js +263 -0
  166. package/dist/solution/BookingByStep/index.d.ts +1 -1
  167. package/dist/solution/BookingByStep/index.js +60 -11
  168. package/dist/solution/BookingByStep/utils/capacity.js +11 -1
  169. package/dist/solution/BookingByStep/utils/resources.js +3 -1
  170. package/dist/solution/BookingByStep/utils/timeslots.js +43 -2
  171. package/dist/solution/BookingTicket/index.d.ts +393 -24
  172. package/dist/solution/BookingTicket/index.js +2508 -443
  173. package/dist/solution/BookingTicket/types.d.ts +191 -6
  174. package/dist/solution/BookingTicket/types.js +83 -10
  175. package/dist/solution/BookingTicket/utils/addProductDecision.d.ts +117 -0
  176. package/dist/solution/BookingTicket/utils/addProductDecision.js +346 -0
  177. package/dist/solution/BookingTicket/utils/addTimeAvailability.d.ts +35 -0
  178. package/dist/solution/BookingTicket/utils/addTimeAvailability.js +95 -0
  179. package/dist/solution/BookingTicket/utils/bookingStatus.d.ts +77 -0
  180. package/dist/solution/BookingTicket/utils/bookingStatus.js +136 -0
  181. package/dist/solution/BookingTicket/utils/cartView.d.ts +29 -0
  182. package/dist/solution/BookingTicket/utils/cartView.js +209 -0
  183. package/dist/solution/BookingTicket/utils/exampleData.d.ts +123 -0
  184. package/dist/solution/BookingTicket/utils/exampleData.js +183 -0
  185. package/dist/solution/BookingTicket/utils/orderCustomer.d.ts +4 -0
  186. package/dist/solution/BookingTicket/utils/orderCustomer.js +31 -0
  187. package/dist/solution/BookingTicket/utils/resolveBestAddTimePlan.d.ts +185 -0
  188. package/dist/solution/BookingTicket/utils/resolveBestAddTimePlan.js +438 -0
  189. package/dist/solution/BookingTicket/utils/scan/applyGlobalScan.d.ts +36 -0
  190. package/dist/solution/BookingTicket/utils/scan/applyGlobalScan.js +400 -0
  191. package/dist/solution/BookingTicket/utils/scan/cloudSearch.d.ts +4 -0
  192. package/dist/solution/BookingTicket/utils/scan/cloudSearch.js +42 -1
  193. package/dist/solution/BookingTicket/utils/scan/formatGlobalScan.d.ts +32 -0
  194. package/dist/solution/BookingTicket/utils/scan/formatGlobalScan.js +56 -0
  195. package/dist/solution/BookingTicket/utils/scan/handleScan.d.ts +3 -1
  196. package/dist/solution/BookingTicket/utils/scan/handleScan.js +42 -28
  197. package/dist/solution/BookingTicket/utils/scan/index.d.ts +5 -0
  198. package/dist/solution/BookingTicket/utils/scan/index.js +30 -14
  199. package/dist/solution/BookingTicket/utils/scan/scanProductValues.d.ts +21 -0
  200. package/dist/solution/BookingTicket/utils/scan/scanProductValues.js +150 -0
  201. package/dist/solution/BookingTicket/utils/sessionCatalogStale.d.ts +23 -0
  202. package/dist/solution/BookingTicket/utils/sessionCatalogStale.js +78 -0
  203. package/dist/solution/BuyTickets/index.js +12 -9
  204. package/dist/solution/Checkout/index.d.ts +1 -0
  205. package/dist/solution/Checkout/index.js +275 -209
  206. package/dist/solution/Checkout/utils/index.js +20 -8
  207. package/dist/solution/RegisterAndLogin/config.js +2 -10
  208. package/dist/solution/RegisterAndLogin/index.js +76 -30
  209. package/dist/solution/Sales/index.d.ts +50 -5
  210. package/dist/solution/Sales/index.js +484 -92
  211. package/dist/solution/Sales/types.d.ts +26 -2
  212. package/dist/solution/ScanOrder/index.d.ts +27 -21
  213. package/dist/solution/ScanOrder/index.js +519 -444
  214. package/dist/solution/ScanOrder/types.d.ts +33 -11
  215. package/dist/solution/ScanOrder/types.js +4 -5
  216. package/dist/solution/ScanOrder/utils.d.ts +4 -1
  217. package/dist/solution/ScanOrder/utils.js +109 -34
  218. package/dist/solution/ShopDiscount/index.js +18 -19
  219. package/dist/solution/ShopDiscount/types.d.ts +4 -5
  220. package/dist/solution/ShopDiscount/utils.d.ts +0 -9
  221. package/dist/solution/ShopDiscount/utils.js +0 -9
  222. package/dist/solution/UnifiedBookingSales/index.d.ts +183 -0
  223. package/dist/solution/UnifiedBookingSales/index.js +1871 -0
  224. package/dist/solution/UnifiedBookingSales/types.d.ts +143 -0
  225. package/dist/solution/UnifiedBookingSales/types.js +11 -0
  226. package/dist/solution/VenueBooking/index.d.ts +32 -51
  227. package/dist/solution/VenueBooking/index.js +741 -808
  228. package/dist/solution/VenueBooking/sales-section-4-annotated.json +0 -2
  229. package/dist/solution/VenueBooking/types.d.ts +1 -3
  230. package/dist/solution/VenueBooking/types.js +0 -1
  231. package/dist/solution/VenueBooking/utils/dateSummary.d.ts +2 -2
  232. package/dist/solution/VenueBooking/utils/dateSummary.js +25 -12
  233. package/dist/solution/VenueBooking/utils/slotMerge.d.ts +6 -0
  234. package/dist/solution/VenueBooking/utils/slotMerge.js +9 -3
  235. package/dist/solution/VenueBooking/utils/timeSlot.d.ts +0 -2
  236. package/dist/solution/VenueBooking/utils/timeSlot.js +4 -9
  237. package/dist/solution/index.d.ts +2 -0
  238. package/dist/solution/index.js +3 -1
  239. package/dist/types/index.d.ts +12 -2
  240. package/dist/utils/task.js +18 -15
  241. package/dist/utils/watch.js +1 -0
  242. package/lib/apis/picoding.js +0 -2
  243. package/lib/core/index.js +132 -130
  244. package/lib/effects/index.js +57 -46
  245. package/lib/index.d.ts +0 -1
  246. package/lib/index.js +50 -92
  247. package/lib/model/index.js +21 -14
  248. package/lib/model/strategy/adapter/dataVariant/adapter.js +50 -38
  249. package/lib/model/strategy/adapter/dataVariant/evaluator.js +236 -162
  250. package/lib/model/strategy/adapter/dataVariant/examples.js +237 -229
  251. package/lib/model/strategy/adapter/dataVariant/index.js +34 -39
  252. package/lib/model/strategy/adapter/dataVariant/type.js +28 -57
  253. package/lib/model/strategy/adapter/index.js +64 -100
  254. package/lib/model/strategy/adapter/itemRule/adapter.js +144 -156
  255. package/lib/model/strategy/adapter/itemRule/evaluator.js +48 -36
  256. package/lib/model/strategy/adapter/itemRule/examples.js +225 -295
  257. package/lib/model/strategy/adapter/itemRule/index.js +57 -83
  258. package/lib/model/strategy/adapter/itemRule/type.js +36 -97
  259. package/lib/model/strategy/adapter/promotion/adapter.js +63 -84
  260. package/lib/model/strategy/adapter/promotion/evaluator.js +233 -382
  261. package/lib/model/strategy/adapter/promotion/examples.js +159 -139
  262. package/lib/model/strategy/adapter/promotion/index.js +0 -1
  263. package/lib/model/strategy/adapter/promotion/type.js +35 -199
  264. package/lib/model/strategy/adapter/type.js +16 -4
  265. package/lib/model/strategy/adapter/walletPass/evaluator.js +152 -180
  266. package/lib/model/strategy/adapter/walletPass/example.js +190 -217
  267. package/lib/model/strategy/adapter/walletPass/index.js +51 -75
  268. package/lib/model/strategy/adapter/walletPass/locales.js +58 -36
  269. package/lib/model/strategy/adapter/walletPass/type.d.ts +0 -3
  270. package/lib/model/strategy/adapter/walletPass/type.js +16 -4
  271. package/lib/model/strategy/adapter/walletPass/utils.d.ts +0 -26
  272. package/lib/model/strategy/adapter/walletPass/utils.js +383 -676
  273. package/lib/model/strategy/engine.js +168 -270
  274. package/lib/model/strategy/index.js +34 -49
  275. package/lib/model/strategy/strategy-example.js +239 -243
  276. package/lib/model/strategy/type.js +40 -100
  277. package/lib/modules/Account/index.js +53 -39
  278. package/lib/modules/Account/types.js +33 -20
  279. package/lib/modules/AccountList/index.js +154 -116
  280. package/lib/modules/AccountList/types.js +31 -30
  281. package/lib/modules/AccountList/utils.js +30 -18
  282. package/lib/modules/BaseModule.d.ts +2 -2
  283. package/lib/modules/BaseModule.js +50 -29
  284. package/lib/modules/BookingContext/index.d.ts +48 -0
  285. package/lib/modules/BookingContext/index.js +368 -0
  286. package/lib/modules/BookingContext/types.d.ts +102 -0
  287. package/lib/modules/BookingContext/types.js +34 -0
  288. package/lib/modules/BookingContext/utils/buildCacheItemFromOrderLine.d.ts +16 -0
  289. package/lib/modules/BookingContext/utils/buildCacheItemFromOrderLine.js +298 -0
  290. package/lib/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.d.ts +8 -0
  291. package/lib/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.js +155 -0
  292. package/lib/modules/BookingContext/utils/cacheItemToBookingInput.d.ts +76 -0
  293. package/lib/modules/BookingContext/utils/cacheItemToBookingInput.js +222 -0
  294. package/lib/modules/BookingContext/utils/flexible.d.ts +28 -0
  295. package/lib/modules/BookingContext/utils/flexible.js +80 -0
  296. package/lib/modules/BookingContext/utils/formatResourceList.d.ts +8 -0
  297. package/lib/modules/BookingContext/utils/formatResourceList.js +50 -0
  298. package/lib/modules/BookingContext/utils/index.d.ts +11 -0
  299. package/lib/modules/BookingContext/utils/index.js +43 -0
  300. package/lib/modules/BookingContext/utils/noResource.d.ts +13 -0
  301. package/lib/modules/BookingContext/utils/noResource.js +90 -0
  302. package/lib/modules/BookingContext/utils/orderLineDisplay.d.ts +1 -0
  303. package/lib/modules/BookingContext/utils/orderLineDisplay.js +56 -0
  304. package/lib/modules/BookingContext/utils/productExtend.d.ts +72 -0
  305. package/lib/modules/BookingContext/utils/productExtend.js +283 -0
  306. package/lib/modules/BookingContext/utils/resourceErrors.d.ts +41 -0
  307. package/lib/modules/BookingContext/utils/resourceErrors.js +80 -0
  308. package/lib/modules/BookingContext/utils/resourceSelection.d.ts +7 -0
  309. package/lib/modules/BookingContext/utils/resourceSelection.js +46 -0
  310. package/lib/modules/BookingContext/utils/resources.d.ts +80 -0
  311. package/lib/modules/BookingContext/utils/resources.js +377 -0
  312. package/lib/modules/BookingContext/utils/serviceTimes.d.ts +55 -0
  313. package/lib/modules/BookingContext/utils/serviceTimes.js +162 -0
  314. package/lib/modules/BookingContext/utils/timeSlices.d.ts +42 -0
  315. package/lib/modules/BookingContext/utils/timeSlices.js +124 -0
  316. package/lib/modules/Cart/index.js +124 -170
  317. package/lib/modules/Cart/types.js +51 -46
  318. package/lib/modules/Cart/utils/cartAccount.js +39 -40
  319. package/lib/modules/Cart/utils/cartDate.js +61 -56
  320. package/lib/modules/Cart/utils/cartDiscount.js +33 -28
  321. package/lib/modules/Cart/utils/cartNote.js +32 -27
  322. package/lib/modules/Cart/utils/cartProduct.js +261 -309
  323. package/lib/modules/Cart/utils/cartRelationForms.js +35 -29
  324. package/lib/modules/Cart/utils/cartResource.js +78 -72
  325. package/lib/modules/Cart/utils/changePrice.js +59 -31
  326. package/lib/modules/Cart/utils/index.js +48 -106
  327. package/lib/modules/Customer/constants.js +34 -13
  328. package/lib/modules/Customer/index.d.ts +20 -0
  329. package/lib/modules/Customer/index.js +207 -217
  330. package/lib/modules/Customer/types.d.ts +2 -0
  331. package/lib/modules/Customer/types.js +35 -38
  332. package/lib/modules/Date/index.js +115 -116
  333. package/lib/modules/Date/types.js +28 -16
  334. package/lib/modules/Date/utils.js +123 -174
  335. package/lib/modules/Discount/index.d.ts +2 -2
  336. package/lib/modules/Discount/index.js +133 -119
  337. package/lib/modules/Discount/types.d.ts +1 -1
  338. package/lib/modules/Discount/types.js +31 -9
  339. package/lib/modules/Guests/index.js +56 -30
  340. package/lib/modules/Guests/types.js +33 -23
  341. package/lib/modules/Holder/index.js +189 -209
  342. package/lib/modules/Holder/types.js +16 -4
  343. package/lib/modules/Holder/utils.js +49 -20
  344. package/lib/modules/OpenData/index.d.ts +8 -0
  345. package/lib/modules/OpenData/index.js +88 -63
  346. package/lib/modules/OpenData/types.js +16 -4
  347. package/lib/modules/OpenData/utils.js +78 -44
  348. package/lib/modules/Order/index.d.ts +449 -24
  349. package/lib/modules/Order/index.js +4184 -514
  350. package/lib/modules/Order/types.d.ts +693 -88
  351. package/lib/modules/Order/types.js +35 -24
  352. package/lib/modules/Order/utils/bundleDiscountHydration.d.ts +5 -0
  353. package/lib/modules/Order/utils/bundleDiscountHydration.js +139 -0
  354. package/lib/modules/Order/utils/manualProductDiscount.d.ts +106 -0
  355. package/lib/modules/Order/utils/manualProductDiscount.js +206 -0
  356. package/lib/modules/Order/utils/orderCollectionIdentity.d.ts +53 -0
  357. package/lib/modules/Order/utils/orderCollectionIdentity.js +415 -0
  358. package/lib/modules/Order/utils.d.ts +167 -18
  359. package/lib/modules/Order/utils.js +1085 -414
  360. package/lib/modules/Payment/cash.js +33 -20
  361. package/lib/modules/Payment/cashRecommendationAlgorithm.js +76 -145
  362. package/lib/modules/Payment/eftpos.js +30 -16
  363. package/lib/modules/Payment/index.d.ts +10 -9
  364. package/lib/modules/Payment/index.js +402 -636
  365. package/lib/modules/Payment/mx51.js +0 -1
  366. package/lib/modules/Payment/types.d.ts +44 -24
  367. package/lib/modules/Payment/types.js +107 -216
  368. package/lib/modules/Payment/utils.d.ts +1 -1
  369. package/lib/modules/Payment/utils.js +50 -52
  370. package/lib/modules/Payment/walletpass.d.ts +0 -2
  371. package/lib/modules/Payment/walletpass.js +216 -221
  372. package/lib/modules/Product/index.js +46 -51
  373. package/lib/modules/Product/types.d.ts +29 -7
  374. package/lib/modules/Product/types.js +16 -4
  375. package/lib/modules/Product/utils.js +33 -32
  376. package/lib/modules/ProductList/index.d.ts +17 -13
  377. package/lib/modules/ProductList/index.js +161 -120
  378. package/lib/modules/ProductList/types.d.ts +16 -0
  379. package/lib/modules/ProductList/types.js +31 -9
  380. package/lib/modules/Quotation/index.d.ts +22 -2
  381. package/lib/modules/Quotation/index.js +154 -64
  382. package/lib/modules/Quotation/types.d.ts +19 -0
  383. package/lib/modules/Quotation/types.js +16 -4
  384. package/lib/modules/Resource/index.js +59 -27
  385. package/lib/modules/Resource/types.js +32 -22
  386. package/lib/modules/Resource/utils.js +38 -30
  387. package/lib/modules/ResourcePlanner/index.d.ts +24 -0
  388. package/lib/modules/ResourcePlanner/index.js +148 -0
  389. package/lib/modules/ResourcePlanner/planner.d.ts +14 -0
  390. package/lib/modules/ResourcePlanner/planner.js +933 -0
  391. package/lib/modules/ResourcePlanner/types.d.ts +227 -0
  392. package/lib/modules/ResourcePlanner/types.js +17 -0
  393. package/lib/modules/Rules/index.d.ts +8 -10
  394. package/lib/modules/Rules/index.js +960 -1440
  395. package/lib/modules/Rules/types.d.ts +14 -3
  396. package/lib/modules/Rules/types.js +40 -17
  397. package/lib/modules/SalesSummary/index.d.ts +23 -26
  398. package/lib/modules/SalesSummary/index.js +134 -61
  399. package/lib/modules/SalesSummary/types.d.ts +9 -1
  400. package/lib/modules/SalesSummary/types.js +16 -4
  401. package/lib/modules/SalesSummary/utils.d.ts +3 -22
  402. package/lib/modules/SalesSummary/utils.js +814 -233
  403. package/lib/modules/ScanOrderLogger/index.js +79 -59
  404. package/lib/modules/ScanOrderLogger/providers/feishu.js +78 -54
  405. package/lib/modules/ScanOrderLogger/providers/grafana.js +36 -13
  406. package/lib/modules/ScanOrderLogger/types.js +16 -4
  407. package/lib/modules/Schedule/getDateIsInSchedule.js +169 -338
  408. package/lib/modules/Schedule/index.d.ts +18 -5
  409. package/lib/modules/Schedule/index.js +153 -104
  410. package/lib/modules/Schedule/type.js +16 -4
  411. package/lib/modules/Schedule/types.d.ts +2 -0
  412. package/lib/modules/Schedule/types.js +16 -4
  413. package/lib/modules/Schedule/utils.js +291 -433
  414. package/lib/modules/Step/index.js +52 -40
  415. package/lib/modules/Step/tyeps.js +16 -4
  416. package/lib/modules/Summary/index.js +71 -66
  417. package/lib/modules/Summary/types.js +16 -4
  418. package/lib/modules/Summary/utils.js +418 -791
  419. package/lib/modules/SurchargeList/index.d.ts +18 -0
  420. package/lib/modules/SurchargeList/index.js +109 -0
  421. package/lib/modules/SurchargeList/types.d.ts +15 -0
  422. package/lib/modules/SurchargeList/types.js +17 -0
  423. package/lib/modules/index.d.ts +3 -0
  424. package/lib/modules/index.js +63 -212
  425. package/lib/plugins/app.d.ts +2 -0
  426. package/lib/plugins/app.js +16 -4
  427. package/lib/plugins/index.js +25 -36
  428. package/lib/plugins/request.d.ts +1 -0
  429. package/lib/plugins/request.js +126 -137
  430. package/lib/plugins/shopStore.js +16 -4
  431. package/lib/plugins/user.js +16 -4
  432. package/lib/plugins/window.js +179 -171
  433. package/lib/server/index.d.ts +462 -8
  434. package/lib/server/index.js +4520 -1162
  435. package/lib/server/modules/floor-plan/index.d.ts +39 -0
  436. package/lib/server/modules/floor-plan/index.js +327 -0
  437. package/lib/server/modules/floor-plan/types.d.ts +43 -0
  438. package/lib/server/modules/floor-plan/types.js +34 -0
  439. package/lib/server/modules/index.d.ts +7 -2
  440. package/lib/server/modules/index.js +67 -84
  441. package/lib/server/modules/menu/index.js +122 -146
  442. package/lib/server/modules/menu/types.js +31 -18
  443. package/lib/server/modules/order/index.d.ts +207 -14
  444. package/lib/server/modules/order/index.js +1543 -285
  445. package/lib/server/modules/order/types.d.ts +76 -5
  446. package/lib/server/modules/order/types.js +32 -107
  447. package/lib/server/modules/order/utils/filterBookings.d.ts +7 -1
  448. package/lib/server/modules/order/utils/filterBookings.js +259 -176
  449. package/lib/server/modules/order/utils/filterOrders.js +293 -94
  450. package/lib/server/modules/payment/index.d.ts +28 -0
  451. package/lib/server/modules/payment/index.js +192 -0
  452. package/lib/server/modules/payment/types.d.ts +9 -0
  453. package/lib/server/modules/payment/types.js +17 -0
  454. package/lib/server/modules/products/index.d.ts +105 -14
  455. package/lib/server/modules/products/index.js +850 -475
  456. package/lib/server/modules/products/types.d.ts +34 -1
  457. package/lib/server/modules/products/types.js +34 -41
  458. package/lib/server/modules/quotation/index.d.ts +39 -0
  459. package/lib/server/modules/quotation/index.js +254 -75
  460. package/lib/server/modules/quotation/types.js +31 -21
  461. package/lib/server/modules/resource/index.d.ts +2 -3
  462. package/lib/server/modules/resource/index.js +240 -243
  463. package/lib/server/modules/resource/types.js +33 -42
  464. package/lib/server/modules/schedule/index.d.ts +6 -4
  465. package/lib/server/modules/schedule/index.js +146 -147
  466. package/lib/server/modules/schedule/types.js +21 -14
  467. package/lib/server/modules/schedule/utils.js +163 -334
  468. package/lib/server/test.json +713 -0
  469. package/lib/server/types.d.ts +2 -0
  470. package/lib/server/types.js +16 -4
  471. package/lib/server/utils/index.js +23 -25
  472. package/lib/server/utils/product.js +140 -197
  473. package/lib/server/utils/schedule.js +41 -34
  474. package/lib/server/utils/small-ticket.d.ts +71 -0
  475. package/lib/server/utils/small-ticket.js +904 -0
  476. package/lib/server/utils/time.js +49 -40
  477. package/lib/solution/BaseSales/index.d.ts +378 -0
  478. package/lib/solution/BaseSales/index.js +2533 -0
  479. package/lib/solution/BaseSales/types.d.ts +394 -0
  480. package/lib/solution/BaseSales/types.js +43 -0
  481. package/lib/solution/BaseSales/utils/cartPromotion.d.ts +287 -0
  482. package/lib/solution/BaseSales/utils/cartPromotion.js +1004 -0
  483. package/lib/solution/BaseSales/utils/parseSalesResponse.d.ts +144 -0
  484. package/lib/solution/BaseSales/utils/parseSalesResponse.js +160 -0
  485. package/lib/solution/BaseSales/utils/quotationPrice.d.ts +18 -0
  486. package/lib/solution/BaseSales/utils/quotationPrice.js +289 -0
  487. package/lib/solution/BaseSales/utils/transformBaseProductToOrderProduct.d.ts +63 -0
  488. package/lib/solution/BaseSales/utils/transformBaseProductToOrderProduct.js +256 -0
  489. package/lib/solution/BaseSales/utils.d.ts +40 -0
  490. package/lib/solution/BaseSales/utils.js +244 -0
  491. package/lib/solution/BookingByStep/index.d.ts +1 -1
  492. package/lib/solution/BookingByStep/index.js +1340 -1530
  493. package/lib/solution/BookingByStep/types.js +99 -40
  494. package/lib/solution/BookingByStep/utils/capacity.js +160 -192
  495. package/lib/solution/BookingByStep/utils/products.js +52 -42
  496. package/lib/solution/BookingByStep/utils/resources.js +330 -527
  497. package/lib/solution/BookingByStep/utils/stock.js +44 -65
  498. package/lib/solution/BookingByStep/utils/timeslots.js +131 -129
  499. package/lib/solution/BookingTicket/index.d.ts +393 -24
  500. package/lib/solution/BookingTicket/index.js +1374 -184
  501. package/lib/solution/BookingTicket/types.d.ts +191 -6
  502. package/lib/solution/BookingTicket/types.js +79 -40
  503. package/lib/solution/BookingTicket/utils/addProductDecision.d.ts +117 -0
  504. package/lib/solution/BookingTicket/utils/addProductDecision.js +318 -0
  505. package/lib/solution/BookingTicket/utils/addTimeAvailability.d.ts +35 -0
  506. package/lib/solution/BookingTicket/utils/addTimeAvailability.js +125 -0
  507. package/lib/solution/BookingTicket/utils/bookingStatus.d.ts +77 -0
  508. package/lib/solution/BookingTicket/utils/bookingStatus.js +107 -0
  509. package/lib/solution/BookingTicket/utils/cartView.d.ts +29 -0
  510. package/lib/solution/BookingTicket/utils/cartView.js +184 -0
  511. package/lib/solution/BookingTicket/utils/exampleData.d.ts +123 -0
  512. package/lib/solution/BookingTicket/utils/exampleData.js +0 -0
  513. package/lib/solution/BookingTicket/utils/orderCustomer.d.ts +4 -0
  514. package/lib/solution/BookingTicket/utils/orderCustomer.js +58 -0
  515. package/lib/solution/BookingTicket/utils/resolveBestAddTimePlan.d.ts +185 -0
  516. package/lib/solution/BookingTicket/utils/resolveBestAddTimePlan.js +254 -0
  517. package/lib/solution/BookingTicket/utils/scan/applyGlobalScan.d.ts +36 -0
  518. package/lib/solution/BookingTicket/utils/scan/applyGlobalScan.js +169 -0
  519. package/lib/solution/BookingTicket/utils/scan/cloudSearch.d.ts +4 -0
  520. package/lib/solution/BookingTicket/utils/scan/cloudSearch.js +97 -64
  521. package/lib/solution/BookingTicket/utils/scan/formatGlobalScan.d.ts +32 -0
  522. package/lib/solution/BookingTicket/utils/scan/formatGlobalScan.js +51 -0
  523. package/lib/solution/BookingTicket/utils/scan/handleScan.d.ts +3 -1
  524. package/lib/solution/BookingTicket/utils/scan/handleScan.js +81 -82
  525. package/lib/solution/BookingTicket/utils/scan/index.d.ts +5 -0
  526. package/lib/solution/BookingTicket/utils/scan/index.js +109 -91
  527. package/lib/solution/BookingTicket/utils/scan/scanCache.js +43 -90
  528. package/lib/solution/BookingTicket/utils/scan/scanProductValues.d.ts +21 -0
  529. package/lib/solution/BookingTicket/utils/scan/scanProductValues.js +127 -0
  530. package/lib/solution/BookingTicket/utils/sessionCatalogStale.d.ts +23 -0
  531. package/lib/solution/BookingTicket/utils/sessionCatalogStale.js +85 -0
  532. package/lib/solution/BuyTickets/index.js +70 -67
  533. package/lib/solution/BuyTickets/types.js +38 -22
  534. package/lib/solution/Checkout/index.d.ts +1 -0
  535. package/lib/solution/Checkout/index.js +1169 -1463
  536. package/lib/solution/Checkout/types.js +61 -91
  537. package/lib/solution/Checkout/utils/index.js +156 -228
  538. package/lib/solution/PlaceOrder/index.js +0 -55
  539. package/lib/solution/RegisterAndLogin/config.js +462 -503
  540. package/lib/solution/RegisterAndLogin/index.js +630 -633
  541. package/lib/solution/RegisterAndLogin/types.js +76 -189
  542. package/lib/solution/RegisterAndLogin/utils.js +125 -183
  543. package/lib/solution/Sales/index.d.ts +50 -5
  544. package/lib/solution/Sales/index.js +498 -238
  545. package/lib/solution/Sales/types.d.ts +26 -2
  546. package/lib/solution/Sales/types.js +33 -27
  547. package/lib/solution/ScanOrder/index.d.ts +27 -21
  548. package/lib/solution/ScanOrder/index.js +936 -864
  549. package/lib/solution/ScanOrder/types.d.ts +33 -11
  550. package/lib/solution/ScanOrder/types.js +34 -34
  551. package/lib/solution/ScanOrder/utils.d.ts +4 -1
  552. package/lib/solution/ScanOrder/utils.js +422 -385
  553. package/lib/solution/ShopDiscount/index.js +233 -235
  554. package/lib/solution/ShopDiscount/types.d.ts +4 -5
  555. package/lib/solution/ShopDiscount/types.js +37 -15
  556. package/lib/solution/ShopDiscount/utils.d.ts +0 -9
  557. package/lib/solution/ShopDiscount/utils.js +172 -309
  558. package/lib/solution/UnifiedBookingSales/index.d.ts +183 -0
  559. package/lib/solution/UnifiedBookingSales/index.js +1076 -0
  560. package/lib/solution/UnifiedBookingSales/types.d.ts +143 -0
  561. package/lib/solution/UnifiedBookingSales/types.js +33 -0
  562. package/lib/solution/VenueBooking/index.d.ts +32 -51
  563. package/lib/solution/VenueBooking/index.js +1008 -984
  564. package/lib/solution/VenueBooking/sales-section-4-annotated.json +0 -2
  565. package/lib/solution/VenueBooking/types.d.ts +1 -3
  566. package/lib/solution/VenueBooking/types.js +39 -20
  567. package/lib/solution/VenueBooking/utils/dateSummary.d.ts +2 -2
  568. package/lib/solution/VenueBooking/utils/dateSummary.js +62 -25
  569. package/lib/solution/VenueBooking/utils/resource.js +54 -31
  570. package/lib/solution/VenueBooking/utils/slotMerge.d.ts +6 -0
  571. package/lib/solution/VenueBooking/utils/slotMerge.js +109 -90
  572. package/lib/solution/VenueBooking/utils/timeSlot.d.ts +0 -2
  573. package/lib/solution/VenueBooking/utils/timeSlot.js +133 -138
  574. package/lib/solution/VenueBooking/utils.js +67 -130
  575. package/lib/solution/index.d.ts +2 -0
  576. package/lib/solution/index.js +41 -102
  577. package/lib/store/createStore.js +29 -32
  578. package/lib/types/index.d.ts +12 -2
  579. package/lib/types/index.js +16 -4
  580. package/lib/utils/task.js +36 -36
  581. package/lib/utils/watch.js +47 -81
  582. package/package.json +2 -4
  583. package/dist/solution/Checkout/appointmentDemo.json +0 -1
  584. package/dist/solution/VenueBooking/utils/smartPricing.d.ts +0 -70
  585. package/dist/solution/VenueBooking/utils/smartPricing.js +0 -233
  586. package/lib/solution/Checkout/appointmentDemo.json +0 -1
  587. package/lib/solution/VenueBooking/utils/smartPricing.d.ts +0 -70
  588. package/lib/solution/VenueBooking/utils/smartPricing.js +0 -192
@@ -1,53 +1,113 @@
1
- "use strict";
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
2
28
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
29
+ // src/modules/SalesSummary/utils.ts
30
+ var utils_exports = {};
31
+ __export(utils_exports, {
32
+ calculateSalesSummary: () => calculateSalesSummary,
33
+ createEmptySalesSummary: () => createEmptySalesSummary
5
34
  });
6
- exports.calculateSalesSummary = calculateSalesSummary;
7
- exports.createEmptySalesSummary = createEmptySalesSummary;
8
- var _decimal = _interopRequireDefault(require("decimal.js"));
9
- var _utils = require("../Summary/utils");
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
35
+ module.exports = __toCommonJS(utils_exports);
36
+ var import_decimal = __toESM(require("decimal.js"));
37
+ var import_utils = require("../Summary/utils");
38
+ var import_utils2 = require("../Order/utils");
11
39
  function toDecimal(value) {
12
- return new _decimal.default(value || 0);
40
+ return new import_decimal.default(value || 0);
13
41
  }
14
42
  function toFixed2(value) {
15
- return new _decimal.default(value || 0).toFixed(2);
43
+ return new import_decimal.default(value || 0).toFixed(2);
44
+ }
45
+ function toAmountFormat(value) {
46
+ return new import_decimal.default(value || 0).toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP);
47
+ }
48
+ function toBackendTaxFee(value) {
49
+ return toBcScale(value, 2);
50
+ }
51
+ function toBcScale(value, scale) {
52
+ const factor = new import_decimal.default(10).pow(scale);
53
+ return new import_decimal.default(value || 0).times(factor).toDecimalPlaces(0, import_decimal.default.ROUND_DOWN).div(factor);
54
+ }
55
+ function bcAdd(left, right, scale) {
56
+ return toBcScale(new import_decimal.default(left || 0).plus(right || 0), scale);
57
+ }
58
+ function bcSub(left, right, scale) {
59
+ return toBcScale(new import_decimal.default(left || 0).minus(right || 0), scale);
60
+ }
61
+ function bcMul(left, right, scale) {
62
+ return toBcScale(new import_decimal.default(left || 0).times(right || 0), scale);
63
+ }
64
+ function bcDiv(left, right, scale) {
65
+ const divisor = new import_decimal.default(right || 0);
66
+ if (divisor.eq(0))
67
+ return new import_decimal.default(0);
68
+ return toBcScale(new import_decimal.default(left || 0).div(divisor), scale);
16
69
  }
17
70
  function getSafeNum(num) {
18
- if (!num || Number.isNaN(num)) return 1;
19
- if (num < 1) return 1;
71
+ if (!num || Number.isNaN(num))
72
+ return 1;
73
+ if (num < 1)
74
+ return 1;
20
75
  return Math.floor(num);
21
76
  }
22
77
  function getBundleUnitPrice(product, useOriginal = false) {
23
78
  const bundleItems = product.product_bundle || [];
24
79
  return bundleItems.reduce((sum, item) => {
25
- const quantity = getSafeNum(item?.num);
26
- const price = useOriginal ? toDecimal(item?.original_price ?? item?.product_price ?? item?.price) : toDecimal(item?.bundle_selling_price ?? item?.price);
27
- return sum.plus(price.times(quantity));
28
- }, new _decimal.default(0));
80
+ const quantity = getSafeNum(item == null ? void 0 : item.num);
81
+ const signedUnit = (0, import_utils2.getBundleSignedUnit)(item, { useOriginal });
82
+ return sum.plus(signedUnit.times(quantity));
83
+ }, new import_decimal.default(0));
29
84
  }
30
-
31
- // 单商品 composite 单价(随后再乘商品数量):
32
- // 新语义 v2 下 `metadata.main_product_selling_price` 已经是"含 option、含主商品折扣"的主价,
33
- // composite 单价 = main_product_selling_price + Σ(bundle_selling_price × num)。
34
- // 不再叠加 options(会重复计算),也不再用 variant/selling_price 兜底(选项已固化进 metadata)。
35
85
  function getUnitPaymentTotal(product) {
36
- const mainSelling = product.metadata?.main_product_selling_price;
37
- const mainOriginal = product.metadata?.main_product_original_price;
38
- const basePrice = mainSelling !== undefined ? toDecimal(mainSelling) : mainOriginal !== undefined ? toDecimal(mainOriginal) : toDecimal(product.selling_price);
86
+ var _a, _b;
87
+ const mainSelling = (_a = product.metadata) == null ? void 0 : _a.main_product_selling_price;
88
+ const mainOriginal = (_b = product.metadata) == null ? void 0 : _b.main_product_original_price;
89
+ const basePrice = mainSelling !== void 0 ? toDecimal(mainSelling) : mainOriginal !== void 0 ? toDecimal(mainOriginal) : toDecimal(product.selling_price);
39
90
  return basePrice.plus(getBundleUnitPrice(product));
40
91
  }
41
92
  function getUnitOriginalTotal(product) {
42
- const mainOriginal = product.metadata?.main_product_original_price;
43
- const mainSelling = product.metadata?.main_product_selling_price;
44
- const basePrice = mainOriginal !== undefined ? toDecimal(mainOriginal) : mainSelling !== undefined ? toDecimal(mainSelling) : toDecimal(product.original_price || product.selling_price);
93
+ var _a, _b;
94
+ const mainOriginal = (_a = product.metadata) == null ? void 0 : _a.main_product_original_price;
95
+ const mainSelling = (_b = product.metadata) == null ? void 0 : _b.main_product_selling_price;
96
+ const basePrice = mainOriginal !== void 0 ? toDecimal(mainOriginal) : mainSelling !== void 0 ? toDecimal(mainSelling) : toDecimal(product.original_price || product.selling_price);
45
97
  return basePrice.plus(getBundleUnitPrice(product, true));
46
98
  }
99
+ function isGeneratedVoucherProduct(product) {
100
+ const record = product;
101
+ const metadata = (record == null ? void 0 : record.metadata) || {};
102
+ return (record == null ? void 0 : record.extension_type) === "product_voucher" || metadata.parent_order_detail_id !== void 0 || metadata.gift_card_type !== void 0;
103
+ }
47
104
  function buildSurchargeServiceItems(products) {
48
- return products.map(product => {
105
+ return products.map((product) => {
106
+ var _a, _b;
49
107
  const quantity = getSafeNum(product.num);
50
108
  const unitTotal = getUnitPaymentTotal(product);
109
+ const options = (0, import_utils2.getProductSkuOptions)(product);
110
+ const persistedSurchargeFee = getPersistedMainSurchargeFee(product);
51
111
  return {
52
112
  id: product.product_id,
53
113
  num: quantity,
@@ -59,62 +119,441 @@ function buildSurchargeServiceItems(products) {
59
119
  product: {
60
120
  discount_list: product.discount_list || [],
61
121
  product_bundle: product.product_bundle || [],
62
- product_option_item: product.product_option_item || []
122
+ product_sku: product.product_sku || { option: options }
63
123
  }
64
124
  },
65
125
  bundle: product.product_bundle || [],
66
- option: product.product_option_item || [],
67
- options: product.product_option_item || [],
126
+ option: options,
127
+ options,
68
128
  relation_details: [],
69
- metadata: product.metadata || {},
70
- start_date: product.metadata?.start_date || '',
71
- start_time: product.metadata?.start_time || ''
129
+ metadata: {
130
+ ...product.metadata || {},
131
+ __persisted_surcharge_fee: persistedSurchargeFee == null ? void 0 : persistedSurchargeFee.toFixed(2)
132
+ },
133
+ start_date: ((_a = product.metadata) == null ? void 0 : _a.start_date) || "",
134
+ start_time: ((_b = product.metadata) == null ? void 0 : _b.start_time) || ""
72
135
  };
73
136
  });
74
137
  }
138
+ function getRelationSurchargeIds(item) {
139
+ var _a;
140
+ const ids = [
141
+ ...Array.isArray(item == null ? void 0 : item.relation_surcharge_ids) ? item.relation_surcharge_ids : [],
142
+ ...Array.isArray((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.relation_surcharge_ids) ? item.metadata.relation_surcharge_ids : []
143
+ ];
144
+ return [...new Set(ids.filter((id) => id !== void 0 && id !== null && id !== ""))];
145
+ }
146
+ function getSurchargeConfigId(config) {
147
+ return (config == null ? void 0 : config.id) ?? (config == null ? void 0 : config.surcharge_id);
148
+ }
149
+ function getSurchargeConfigById(surchargeList) {
150
+ const map = /* @__PURE__ */ new Map();
151
+ for (const config of surchargeList || []) {
152
+ const id = getSurchargeConfigId(config);
153
+ if (id !== void 0)
154
+ map.set(id, config);
155
+ }
156
+ return map;
157
+ }
158
+ function getMainSurchargeUnitAmount(product) {
159
+ var _a, _b;
160
+ const record = product;
161
+ return toDecimal(
162
+ ((_a = record.metadata) == null ? void 0 : _a.main_product_attached_bundle_selling_price) ?? ((_b = record.metadata) == null ? void 0 : _b.main_product_selling_price) ?? record.selling_price ?? 0
163
+ );
164
+ }
165
+ function getBundleSurchargeUnitAmount(bundle) {
166
+ return toDecimal((bundle == null ? void 0 : bundle.bundle_selling_price) ?? (bundle == null ? void 0 : bundle.bundle_sum_price) ?? (bundle == null ? void 0 : bundle.price) ?? 0);
167
+ }
168
+ function getBundleProductId(bundle) {
169
+ const id = (bundle == null ? void 0 : bundle.bundle_product_id) ?? (bundle == null ? void 0 : bundle._bundle_product_id) ?? (bundle == null ? void 0 : bundle.product_id);
170
+ if (id === void 0 || id === null || id === "")
171
+ return null;
172
+ const parsed = Number(id);
173
+ if (!Number.isFinite(parsed))
174
+ return null;
175
+ return parsed;
176
+ }
177
+ function getPersistedMainSurchargeFee(product) {
178
+ var _a;
179
+ if ((product == null ? void 0 : product.order_detail_id) === void 0 || (product == null ? void 0 : product.order_detail_id) === null)
180
+ return null;
181
+ const value = (product == null ? void 0 : product.surcharge_fee) ?? (product == null ? void 0 : product.main_product_attached_bundle_surcharge_fee) ?? ((_a = product == null ? void 0 : product.metadata) == null ? void 0 : _a.main_product_attached_bundle_surcharge_fee);
182
+ if (value === void 0 || value === null || value === "")
183
+ return null;
184
+ return toDecimal(value);
185
+ }
186
+ function getPersistedSourceSurchargeFee(sourceItem) {
187
+ var _a;
188
+ const value = (_a = sourceItem == null ? void 0 : sourceItem.metadata) == null ? void 0 : _a.__persisted_surcharge_fee;
189
+ if (value === void 0 || value === null || value === "")
190
+ return null;
191
+ return toDecimal(value);
192
+ }
193
+ function getPersistedSurchargeRemainder(item, sourceItem) {
194
+ var _a, _b;
195
+ const value = ((_a = sourceItem == null ? void 0 : sourceItem.metadata) == null ? void 0 : _a.surcharge_rounding_remainder) ?? (item == null ? void 0 : item.surcharge_rounding_remainder) ?? ((_b = item == null ? void 0 : item.metadata) == null ? void 0 : _b.surcharge_rounding_remainder);
196
+ if (value === void 0 || value === null || value === "")
197
+ return null;
198
+ return toDecimal(value);
199
+ }
200
+ function getPersistedQuantity(item, fallbackQuantity) {
201
+ const isPersistedLine = (item == null ? void 0 : item.order_detail_id) !== void 0 && (item == null ? void 0 : item.order_detail_id) !== null;
202
+ if (!isPersistedLine)
203
+ return new import_decimal.default(0);
204
+ const value = (item == null ? void 0 : item.product_quantity) ?? (item == null ? void 0 : item.quantity);
205
+ const parsed = new import_decimal.default(value || 0);
206
+ if (parsed.lte(0))
207
+ return import_decimal.default.min(fallbackQuantity, 1);
208
+ return import_decimal.default.min(fallbackQuantity, parsed);
209
+ }
210
+ function getPersistedBundleSurchargeFee(bundle, product) {
211
+ var _a;
212
+ if ((bundle == null ? void 0 : bundle.order_detail_id) === void 0 && (product == null ? void 0 : product.order_detail_id) === void 0)
213
+ return null;
214
+ if ((bundle == null ? void 0 : bundle.order_detail_id) === null && (product == null ? void 0 : product.order_detail_id) === null)
215
+ return null;
216
+ const value = (bundle == null ? void 0 : bundle.surcharge_fee) ?? ((_a = bundle == null ? void 0 : bundle.metadata) == null ? void 0 : _a.surcharge_fee);
217
+ if (value === void 0 || value === null || value === "")
218
+ return null;
219
+ return toDecimal(value);
220
+ }
221
+ function createSurchargeAllocationNodes(products, sourceItems) {
222
+ var _a;
223
+ const nodes = [];
224
+ for (let productIndex = 0; productIndex < products.length; productIndex++) {
225
+ const product = products[productIndex];
226
+ const sourceItem = sourceItems[productIndex] || {};
227
+ const productQuantity = new import_decimal.default(getSafeNum(product.num));
228
+ for (let bundleIndex = 0; bundleIndex < (product.product_bundle || []).length; bundleIndex++) {
229
+ const bundle = product.product_bundle[bundleIndex];
230
+ if (!isBundleOriginalPrice(bundle))
231
+ continue;
232
+ const sourceBundle = ((_a = sourceItem == null ? void 0 : sourceItem.bundle) == null ? void 0 : _a[bundleIndex]) || {};
233
+ nodes.push({
234
+ kind: "bundle",
235
+ target: bundle,
236
+ source: sourceBundle,
237
+ productId: getBundleProductId(bundle),
238
+ unitAmount: getBundleSurchargeUnitAmount(bundle),
239
+ quantity: new import_decimal.default(getSafeNum((bundle == null ? void 0 : bundle.num) ?? (bundle == null ? void 0 : bundle.quantity))).times(productQuantity),
240
+ persistedQuantity: getPersistedQuantity(bundle, new import_decimal.default(getSafeNum((bundle == null ? void 0 : bundle.num) ?? (bundle == null ? void 0 : bundle.quantity))).times(productQuantity)),
241
+ relationSurchargeIds: getRelationSurchargeIds(sourceBundle).length ? getRelationSurchargeIds(sourceBundle) : getRelationSurchargeIds(bundle),
242
+ persistedUnitSurchargeFee: getPersistedBundleSurchargeFee(bundle, product),
243
+ persistedSurchargeRemainder: getPersistedSurchargeRemainder(bundle, sourceBundle)
244
+ });
245
+ }
246
+ nodes.push({
247
+ kind: "main",
248
+ target: product,
249
+ source: sourceItem,
250
+ productId: product.product_id ?? null,
251
+ unitAmount: getMainSurchargeUnitAmount(product),
252
+ quantity: productQuantity,
253
+ persistedQuantity: getPersistedQuantity(product, productQuantity),
254
+ relationSurchargeIds: getRelationSurchargeIds(sourceItem).length ? getRelationSurchargeIds(sourceItem) : getRelationSurchargeIds(product),
255
+ persistedUnitSurchargeFee: getPersistedSourceSurchargeFee(sourceItem) ?? getPersistedMainSurchargeFee(product),
256
+ persistedSurchargeRemainder: getPersistedSurchargeRemainder(product, sourceItem)
257
+ });
258
+ }
259
+ return nodes;
260
+ }
261
+ function resetProductSurchargeFields(products) {
262
+ for (const product of products) {
263
+ product.surcharge_fee = 0;
264
+ product.main_product_attached_bundle_surcharge_fee = "0.00";
265
+ product.surcharge_rounding_remainder = 0;
266
+ product.relation_surcharge_ids = [];
267
+ for (const bundle of product.product_bundle || []) {
268
+ bundle.surcharge_fee = 0;
269
+ bundle.relation_surcharge_ids = [];
270
+ bundle.metadata = {
271
+ ...bundle.metadata || {},
272
+ surcharge_fee: "0.00",
273
+ surcharge_rounding_remainder: "0.00",
274
+ relation_surcharge_ids: []
275
+ };
276
+ }
277
+ }
278
+ }
279
+ function getPersistedSurchargeIds(nodes) {
280
+ const ids = /* @__PURE__ */ new Set();
281
+ for (const node of nodes) {
282
+ if (!node.persistedUnitSurchargeFee || node.persistedUnitSurchargeFee.lte(0))
283
+ continue;
284
+ for (const surchargeId of node.relationSurchargeIds) {
285
+ ids.add(surchargeId);
286
+ }
287
+ }
288
+ return ids;
289
+ }
290
+ function buildSurchargeQuantityTotals(nodes) {
291
+ const totals = /* @__PURE__ */ new Map();
292
+ for (const node of nodes) {
293
+ for (const surchargeId of node.relationSurchargeIds) {
294
+ totals.set(
295
+ surchargeId,
296
+ (totals.get(surchargeId) || new import_decimal.default(0)).plus(node.quantity)
297
+ );
298
+ }
299
+ }
300
+ return totals;
301
+ }
302
+ function getFallbackAllProductSurchargeIds(surchargeList) {
303
+ return (surchargeList || []).reduce((ids, config) => {
304
+ const id = getSurchargeConfigId(config);
305
+ if (id === void 0)
306
+ return ids;
307
+ if (Number(config.open_product ?? 0) === 0)
308
+ return ids;
309
+ if (Number(config.is_all ?? 0) !== 1)
310
+ return ids;
311
+ if (Number(config.open_schedule ?? 0) === 1 && config.schedule_type !== "all")
312
+ return ids;
313
+ ids.push(id);
314
+ return ids;
315
+ }, []);
316
+ }
317
+ function getBackendSurchargeState(stateMap, surchargeId, config) {
318
+ let state = stateMap.get(surchargeId);
319
+ if (!state) {
320
+ state = {
321
+ config,
322
+ amount: new import_decimal.default(0),
323
+ totalOffsetFee: new import_decimal.default(0),
324
+ calculatedQuantity: new import_decimal.default(0),
325
+ fixedIncluded: false,
326
+ fixedRemainderAssigned: false
327
+ };
328
+ stateMap.set(surchargeId, state);
329
+ }
330
+ return state;
331
+ }
332
+ function buildSurchargeResultItem(config, surchargeId, amount, index) {
333
+ const name = (config == null ? void 0 : config.name) || (config == null ? void 0 : config.label) || {};
334
+ return {
335
+ key: `custom_surcharge_${index}`,
336
+ label: name,
337
+ name,
338
+ value: amount.toNumber(),
339
+ surcharge_id: surchargeId,
340
+ description: config == null ? void 0 : config.description,
341
+ type: "default",
342
+ fixed: config == null ? void 0 : config.fixed,
343
+ amount: amount.toNumber(),
344
+ percentage: config == null ? void 0 : config.percentage,
345
+ metadata: {
346
+ open_product: config == null ? void 0 : config.open_product
347
+ }
348
+ };
349
+ }
350
+ function applySurchargeNodeResult(node, params) {
351
+ const { unitSurchargeFee, roundingRemainder, surchargeIds } = params;
352
+ if (node.kind === "main") {
353
+ node.target.surcharge_fee = unitSurchargeFee.toNumber();
354
+ node.target.main_product_attached_bundle_surcharge_fee = toFixed2(unitSurchargeFee);
355
+ node.target.surcharge_rounding_remainder = roundingRemainder.toNumber();
356
+ node.target.relation_surcharge_ids = surchargeIds;
357
+ return;
358
+ }
359
+ node.target.surcharge_fee = unitSurchargeFee.toNumber();
360
+ node.target.relation_surcharge_ids = surchargeIds;
361
+ node.target.metadata = {
362
+ ...node.target.metadata || {},
363
+ surcharge_fee: toFixed2(unitSurchargeFee),
364
+ surcharge_rounding_remainder: toFixed2(roundingRemainder),
365
+ relation_surcharge_ids: surchargeIds
366
+ };
367
+ }
368
+ function calculateBackendProductSurcharges(params) {
369
+ const { products, sourceItems, surchargeList } = params;
370
+ resetProductSurchargeFields(products);
371
+ const configById = getSurchargeConfigById(surchargeList);
372
+ const nodes = createSurchargeAllocationNodes(products, sourceItems);
373
+ const fallbackAllProductSurchargeIds = getFallbackAllProductSurchargeIds(surchargeList);
374
+ for (const node of nodes) {
375
+ if (node.relationSurchargeIds.length === 0) {
376
+ node.relationSurchargeIds = fallbackAllProductSurchargeIds;
377
+ }
378
+ }
379
+ const persistedSurchargeIds = getPersistedSurchargeIds(nodes);
380
+ const quantityTotals = buildSurchargeQuantityTotals(nodes);
381
+ const stateMap = /* @__PURE__ */ new Map();
382
+ for (const node of nodes) {
383
+ let unitSurchargeFee = new import_decimal.default(0);
384
+ let roundingRemainder = new import_decimal.default(0);
385
+ const appliedSurchargeIds = [];
386
+ let persistedRemainderApplied = false;
387
+ for (const surchargeId of node.relationSurchargeIds) {
388
+ const config = configById.get(surchargeId);
389
+ const totalQuantity = quantityTotals.get(surchargeId) || new import_decimal.default(0);
390
+ if (totalQuantity.lte(0))
391
+ continue;
392
+ const persistedUnitSurchargeFee = node.persistedUnitSurchargeFee;
393
+ if (persistedUnitSurchargeFee && persistedUnitSurchargeFee.gt(0)) {
394
+ const persistedQuantity = import_decimal.default.min(node.persistedQuantity, node.quantity);
395
+ const deltaQuantity = import_decimal.default.max(node.quantity.minus(persistedQuantity), 0);
396
+ const state2 = getBackendSurchargeState(
397
+ stateMap,
398
+ surchargeId,
399
+ config || { surcharge_id: surchargeId }
400
+ );
401
+ const persistedSurchargeRemainder = !persistedRemainderApplied && node.persistedSurchargeRemainder ? node.persistedSurchargeRemainder : new import_decimal.default(0);
402
+ state2.amount = bcAdd(
403
+ bcAdd(
404
+ state2.amount,
405
+ bcMul(persistedUnitSurchargeFee, persistedQuantity, 2),
406
+ 2
407
+ ),
408
+ persistedSurchargeRemainder,
409
+ 2
410
+ );
411
+ const fixed2 = new import_decimal.default((config == null ? void 0 : config.fixed) || 0);
412
+ if (fixed2.gt(0))
413
+ state2.fixedIncluded = true;
414
+ let nodeTotalSurcharge = bcAdd(
415
+ bcMul(persistedUnitSurchargeFee, persistedQuantity, 2),
416
+ persistedSurchargeRemainder,
417
+ 2
418
+ );
419
+ if (config && deltaQuantity.gt(0)) {
420
+ const percentage2 = new import_decimal.default(config.percentage || 0);
421
+ const deltaUnitPercentageFee = import_decimal.default.max(0, bcMul(node.unitAmount, percentage2, 2));
422
+ const deltaSurcharge = bcMul(deltaQuantity, deltaUnitPercentageFee, 2);
423
+ state2.amount = bcAdd(state2.amount, deltaSurcharge, 2);
424
+ nodeTotalSurcharge = bcAdd(nodeTotalSurcharge, deltaSurcharge, 2);
425
+ }
426
+ if (node.quantity.gt(0)) {
427
+ unitSurchargeFee = bcAdd(unitSurchargeFee, bcDiv(nodeTotalSurcharge, node.quantity, 2), 2);
428
+ }
429
+ if (!persistedSurchargeRemainder.eq(0)) {
430
+ roundingRemainder = bcAdd(roundingRemainder, persistedSurchargeRemainder, 2);
431
+ persistedRemainderApplied = true;
432
+ }
433
+ appliedSurchargeIds.push(surchargeId);
434
+ continue;
435
+ }
436
+ if (!config)
437
+ continue;
438
+ const percentage = new import_decimal.default(config.percentage || 0);
439
+ const fixed = new import_decimal.default(config.fixed || 0);
440
+ const unitPercentageFee = import_decimal.default.max(0, bcMul(node.unitAmount, percentage, 2));
441
+ const percentageTotalFee = bcMul(node.quantity, unitPercentageFee, 2);
442
+ let unitFixedFee = new import_decimal.default(0);
443
+ let fixedRemainder = new import_decimal.default(0);
444
+ if (fixed.gt(0) && !persistedSurchargeIds.has(surchargeId)) {
445
+ unitFixedFee = bcDiv(fixed, totalQuantity, 2);
446
+ const distributedFixed = bcMul(unitFixedFee, totalQuantity, 2);
447
+ fixedRemainder = bcSub(fixed, distributedFixed, 2);
448
+ }
449
+ const currentUnitSurcharge = import_decimal.default.max(
450
+ 0,
451
+ bcAdd(unitPercentageFee, unitFixedFee, 2)
452
+ );
453
+ if (currentUnitSurcharge.lte(0))
454
+ continue;
455
+ const state = getBackendSurchargeState(stateMap, surchargeId, config);
456
+ state.amount = bcAdd(state.amount, percentageTotalFee, 2);
457
+ const precisePercentage = import_decimal.default.max(0, bcMul(node.unitAmount, percentage, 6));
458
+ const offsetBaseFee = import_decimal.default.max(0, bcSub(precisePercentage, unitPercentageFee, 6));
459
+ state.totalOffsetFee = bcAdd(
460
+ state.totalOffsetFee,
461
+ bcMul(offsetBaseFee, node.quantity, 6),
462
+ 6
463
+ );
464
+ state.calculatedQuantity = bcAdd(state.calculatedQuantity, node.quantity, 6);
465
+ if (!state.fixedIncluded && fixed.gt(0) && !persistedSurchargeIds.has(surchargeId)) {
466
+ state.amount = bcAdd(state.amount, fixed, 2);
467
+ state.fixedIncluded = true;
468
+ }
469
+ if (!state.fixedRemainderAssigned && !fixedRemainder.eq(0)) {
470
+ state.fixedRemainderAssigned = true;
471
+ roundingRemainder = bcAdd(roundingRemainder, fixedRemainder, 2);
472
+ }
473
+ if (state.calculatedQuantity.gte(totalQuantity) && state.totalOffsetFee.gt(0)) {
474
+ const totalOffsetFee = toAmountFormat(state.totalOffsetFee);
475
+ state.amount = bcAdd(state.amount, totalOffsetFee, 2);
476
+ roundingRemainder = bcAdd(roundingRemainder, totalOffsetFee, 2);
477
+ state.totalOffsetFee = new import_decimal.default(0);
478
+ }
479
+ unitSurchargeFee = bcAdd(unitSurchargeFee, currentUnitSurcharge, 2);
480
+ appliedSurchargeIds.push(surchargeId);
481
+ }
482
+ applySurchargeNodeResult(node, {
483
+ unitSurchargeFee,
484
+ roundingRemainder,
485
+ surchargeIds: appliedSurchargeIds
486
+ });
487
+ }
488
+ const result = (surchargeList || []).reduce((items, config, index) => {
489
+ const surchargeId = getSurchargeConfigId(config);
490
+ if (surchargeId === void 0)
491
+ return items;
492
+ const state = stateMap.get(surchargeId);
493
+ if (!state || state.amount.lte(0))
494
+ return items;
495
+ items.push(buildSurchargeResultItem(config, surchargeId, state.amount, index));
496
+ return items;
497
+ }, []);
498
+ const includedIds = new Set(
499
+ result.map((item) => item == null ? void 0 : item.surcharge_id).filter((id) => id !== void 0)
500
+ );
501
+ for (const [surchargeId, state] of stateMap.entries()) {
502
+ if (includedIds.has(surchargeId) || state.amount.lte(0))
503
+ continue;
504
+ result.push(buildSurchargeResultItem(state.config, surchargeId, state.amount, result.length));
505
+ }
506
+ return result;
507
+ }
75
508
  function getProductDepositData(product) {
76
- const rootDepositData = product._origin?.custom_deposit_data;
77
- if (rootDepositData && typeof rootDepositData === 'object') return rootDepositData;
78
- const nestedDepositData = product._origin?.product?.custom_deposit_data;
79
- if (nestedDepositData && typeof nestedDepositData === 'object') return nestedDepositData;
80
- return undefined;
509
+ var _a, _b, _c;
510
+ const rootDepositData = (_a = product._origin) == null ? void 0 : _a.custom_deposit_data;
511
+ if (rootDepositData && typeof rootDepositData === "object")
512
+ return rootDepositData;
513
+ const nestedDepositData = (_c = (_b = product._origin) == null ? void 0 : _b.product) == null ? void 0 : _c.custom_deposit_data;
514
+ if (nestedDepositData && typeof nestedDepositData === "object")
515
+ return nestedDepositData;
516
+ return void 0;
81
517
  }
82
518
  function collectDepositPolicyIds(depositData) {
83
- const rawIds = [...(Array.isArray(depositData?.self_deposit_policy_ids) ? depositData.self_deposit_policy_ids : []), ...(Array.isArray(depositData?.deposit_policy_ids) ? depositData.deposit_policy_ids : [])];
519
+ const rawIds = [
520
+ ...Array.isArray(depositData == null ? void 0 : depositData.self_deposit_policy_ids) ? depositData.self_deposit_policy_ids : [],
521
+ ...Array.isArray(depositData == null ? void 0 : depositData.deposit_policy_ids) ? depositData.deposit_policy_ids : []
522
+ ];
84
523
  return rawIds.reduce((result, id) => {
85
524
  const normalizedId = Number(id);
86
- if (!Number.isFinite(normalizedId)) return result;
525
+ if (!Number.isFinite(normalizedId))
526
+ return result;
87
527
  result.push(Math.floor(normalizedId));
88
528
  return result;
89
529
  }, []);
90
530
  }
91
531
  function collectDepositPolicyData(depositData) {
92
- const rawPolicyData = Array.isArray(depositData?.deposit_policy_data) ? depositData.deposit_policy_data : [];
532
+ const rawPolicyData = Array.isArray(depositData == null ? void 0 : depositData.deposit_policy_data) ? depositData.deposit_policy_data : [];
93
533
  return rawPolicyData.reduce((result, policy) => {
94
- const normalizedId = Number(policy?.id);
95
- if (!Number.isFinite(normalizedId)) return result;
534
+ const normalizedId = Number(policy == null ? void 0 : policy.id);
535
+ if (!Number.isFinite(normalizedId))
536
+ return result;
96
537
  result.push({
97
538
  id: Math.floor(normalizedId),
98
- title: String(policy?.title || '')
539
+ title: String((policy == null ? void 0 : policy.title) || "")
99
540
  });
100
541
  return result;
101
542
  }, []);
102
543
  }
103
544
  function calculateProductsDeposit(products) {
104
- let totalDeposit = new _decimal.default(0);
545
+ let totalDeposit = new import_decimal.default(0);
105
546
  let hasDeposit = false;
106
- const policyIds = new Set();
107
- const policyDataMap = new Map();
547
+ const policyIds = /* @__PURE__ */ new Set();
548
+ const policyDataMap = /* @__PURE__ */ new Map();
108
549
  for (const product of products) {
109
550
  const depositData = getProductDepositData(product);
110
- if (!depositData || depositData.has_deposit != 1) continue;
551
+ if (!depositData || depositData.has_deposit != 1)
552
+ continue;
111
553
  const num = getSafeNum(product.num);
112
554
  const unitTotal = getUnitPaymentTotal(product);
113
555
  const productTotal = unitTotal.times(num);
114
- const {
115
- deposit_fixed,
116
- deposit_percentage
117
- } = depositData;
556
+ const { deposit_fixed, deposit_percentage } = depositData;
118
557
  const fixed = toDecimal(deposit_fixed);
119
558
  const percentage = toDecimal(deposit_percentage);
120
559
  const itemDeposit = fixed.plus(percentage.times(productTotal));
@@ -130,12 +569,13 @@ function calculateProductsDeposit(products) {
130
569
  policyIds.add(policy.id);
131
570
  }
132
571
  }
133
- if (!hasDeposit) return undefined;
572
+ if (!hasDeposit)
573
+ return void 0;
134
574
  for (const id of policyIds) {
135
575
  if (!policyDataMap.has(id)) {
136
576
  policyDataMap.set(id, {
137
577
  id,
138
- title: ''
578
+ title: ""
139
579
  });
140
580
  }
141
581
  }
@@ -147,192 +587,295 @@ function calculateProductsDeposit(products) {
147
587
  };
148
588
  }
149
589
  function isBundleOriginalPrice(item) {
150
- return item?.price_type === 'markup' && item?.price_type_ext === 'product_price';
590
+ return (item == null ? void 0 : item.price_type) === "markup" && (item == null ? void 0 : item.price_type_ext) === "product_price";
151
591
  }
152
592
  function isBundleMarkupOrDiscount(item) {
153
- const isMarkup = item?.price_type === 'markup' && (item?.price_type_ext === '' || !item?.price_type_ext);
154
- const isDiscount = item?.price_type === 'markdown' && (item?.price_type_ext === '' || !item?.price_type_ext);
593
+ const isMarkup = (item == null ? void 0 : item.price_type) === "markup" && ((item == null ? void 0 : item.price_type_ext) === "" || !(item == null ? void 0 : item.price_type_ext));
594
+ const isDiscount = (item == null ? void 0 : item.price_type) === "markdown" && ((item == null ? void 0 : item.price_type_ext) === "" || !(item == null ? void 0 : item.price_type_ext));
155
595
  return isMarkup || isDiscount;
156
596
  }
157
-
158
- /**
159
- * 计算单个商品税费(返回精确值,不四舍五入)。
160
- * 与 Summary/utils.ts 的 getProductItemTax 公式一致,但不依赖 bookingDetail。
161
- *
162
- * @param taxRate 百分比税率(如 5 表示 5%),函数内部会 /100
163
- */
164
597
  function calculateSingleItemTax(params) {
165
- const {
166
- price,
167
- taxRate,
168
- isPriceIncludeTax,
169
- isChargeTax
170
- } = params;
171
- if (price.lte(0)) return new _decimal.default(0);
172
- if (isChargeTax === 0 || !isChargeTax) return new _decimal.default(0);
598
+ const { price, taxRate, isPriceIncludeTax, isChargeTax } = params;
599
+ if (price.lte(0))
600
+ return new import_decimal.default(0);
601
+ if (isChargeTax === 0 || !isChargeTax)
602
+ return new import_decimal.default(0);
173
603
  const rate = taxRate.div(100);
174
604
  if (isPriceIncludeTax === 0) {
175
605
  return price.times(rate);
176
606
  }
177
- const divisor = new _decimal.default(1).plus(rate);
178
- if (divisor.lte(0)) return new _decimal.default(0);
607
+ const divisor = new import_decimal.default(1).plus(rate);
608
+ if (divisor.lte(0))
609
+ return new import_decimal.default(0);
179
610
  return price.dividedBy(divisor).times(rate);
180
611
  }
181
-
182
- /**
183
- * 获取主商品折扣后总价(含 option、含主商品折扣、含加价/减价 bundle)。
184
- *
185
- * 新语义 v2 下:
186
- * - `metadata.main_product_selling_price` 已是"含 option、含折扣"的主价,直接读即可;
187
- * - 无需再 `minus(mainDiscountAmount)` 或 `plus(getOptionUnitPrice)`;
188
- * - 仅需叠加 `markup` / `markdown` 类 bundle(原价 bundle 已由 `calculateProductsTax` 独立处理)。
189
- */
190
- function getMainProductPaymentTotal(product) {
191
- const mainSelling = product.metadata?.main_product_selling_price ?? product.metadata?.main_product_original_price ?? 0;
612
+ function getMainAttachedBundleSellingTotal(product) {
613
+ var _a, _b;
614
+ const mainSelling = ((_a = product.metadata) == null ? void 0 : _a.main_product_selling_price) ?? ((_b = product.metadata) == null ? void 0 : _b.main_product_original_price) ?? 0;
192
615
  let total = toDecimal(mainSelling);
193
616
  const bundleItems = product.product_bundle || [];
194
617
  for (const bundleItem of bundleItems) {
195
618
  if (isBundleMarkupOrDiscount(bundleItem)) {
196
- const unit = toDecimal(bundleItem.bundle_selling_price ?? bundleItem.price ?? 0);
619
+ const unit = (0, import_utils2.getBundleSignedUnit)(bundleItem);
197
620
  const qty = getSafeNum(bundleItem.num ?? bundleItem.quantity);
198
621
  total = total.plus(unit.times(qty));
199
622
  }
200
623
  }
201
- return _decimal.default.max(total, 0);
624
+ return import_decimal.default.max(total, 0);
202
625
  }
203
-
204
- /**
205
- * 遍历所有商品计算税费,回写 tax_fee 到每个商品及 bundle 原价子商品,处理舍入差值。
206
- * 参照 Summary/utils.ts 的 processItemsTax,适配 SalesSummaryProduct 结构。
207
- */
208
- function calculateProductsTax(products, taxRate, isPriceIncludeTax) {
209
- const rate = new _decimal.default(taxRate);
210
- let preciseTax = new _decimal.default(0);
211
- let roundedTax = new _decimal.default(0);
212
- let preciseOriginTax = new _decimal.default(0);
213
- let roundedOriginTax = new _decimal.default(0);
214
- let lastTaxableItem = null;
626
+ function getMainProductPaymentTotal(product) {
627
+ var _a;
628
+ const attachedPayment = (_a = product.metadata) == null ? void 0 : _a.main_product_attached_bundle_payment_price;
629
+ if (attachedPayment !== void 0 && attachedPayment !== null && attachedPayment !== "") {
630
+ return import_decimal.default.max(toDecimal(attachedPayment), 0);
631
+ }
632
+ return getMainAttachedBundleSellingTotal(product);
633
+ }
634
+ function getExplicitTaxRemainder(item) {
635
+ var _a;
636
+ const value = (item == null ? void 0 : item.tax_fee_rounding_remainder) ?? ((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.tax_fee_rounding_remainder);
637
+ if (value === void 0 || value === null || value === "")
638
+ return null;
639
+ return new import_decimal.default(value || 0);
640
+ }
641
+ function getPersistedTaxRemainder(item, parentProduct) {
642
+ const isPersistedLine = (item == null ? void 0 : item.order_detail_id) !== void 0 && (item == null ? void 0 : item.order_detail_id) !== null || (parentProduct == null ? void 0 : parentProduct.order_detail_id) !== void 0 && (parentProduct == null ? void 0 : parentProduct.order_detail_id) !== null;
643
+ if (!isPersistedLine)
644
+ return null;
645
+ return getExplicitTaxRemainder(item);
646
+ }
647
+ function getPersistedMainTaxFee(product) {
648
+ var _a;
649
+ if ((product == null ? void 0 : product.order_detail_id) === void 0 || (product == null ? void 0 : product.order_detail_id) === null)
650
+ return null;
651
+ const value = ((_a = product == null ? void 0 : product.metadata) == null ? void 0 : _a.main_product_attached_bundle_tax_fee) ?? (product == null ? void 0 : product.main_product_attached_bundle_tax_fee) ?? (product == null ? void 0 : product.tax_fee);
652
+ if (value === void 0 || value === null || value === "")
653
+ return null;
654
+ return toDecimal(value);
655
+ }
656
+ function writeTaxRemainder(item, roundingRemainder) {
657
+ item.tax_fee_rounding_remainder = roundingRemainder.toNumber();
658
+ if (item.metadata && typeof item.metadata === "object") {
659
+ item.metadata.tax_fee_rounding_remainder = roundingRemainder.toFixed(2);
660
+ }
661
+ }
662
+ function resetTaxRemainderFields(products) {
215
663
  for (const product of products) {
216
- const quantity = new _decimal.default(getSafeNum(product.num));
217
-
218
- // --- 主商品折后税费 ---
219
- const mainTotal = getMainProductPaymentTotal(product);
220
- const surchargeRoundingRemainder = toDecimal(product.surcharge_rounding_remainder);
221
- const surchargeFee = toDecimal(product.surcharge_fee).plus(quantity.gt(0) ? surchargeRoundingRemainder.div(quantity) : 0);
222
- const mainTaxableBase = mainTotal.lte(0) ? surchargeFee : mainTotal.plus(surchargeFee);
223
- const mainTaxPrecise = calculateSingleItemTax({
224
- price: mainTaxableBase,
225
- taxRate: rate,
226
- isPriceIncludeTax,
227
- isChargeTax: product.is_charge_tax ?? 0
228
- });
229
- const mainTaxRounded = mainTaxPrecise.toDecimalPlaces(2, _decimal.default.ROUND_HALF_UP);
230
- if (mainTaxPrecise.gt(0)) {
231
- lastTaxableItem = {
232
- type: 'main',
233
- item: product
664
+ const productRemainder = getPersistedTaxRemainder(product);
665
+ if (productRemainder !== null) {
666
+ product.tax_fee_rounding_remainder = productRemainder.toNumber();
667
+ } else {
668
+ delete product.tax_fee_rounding_remainder;
669
+ }
670
+ product.original_tax_fee_rounding_remainder = 0;
671
+ for (const bundle of product.product_bundle || []) {
672
+ const bundleRemainder = getPersistedTaxRemainder(bundle, product);
673
+ bundle.metadata = {
674
+ ...bundle.metadata || {}
234
675
  };
676
+ if (bundleRemainder !== null) {
677
+ bundle.metadata.tax_fee_rounding_remainder = bundleRemainder.toFixed(2);
678
+ bundle.tax_fee_rounding_remainder = bundleRemainder.toNumber();
679
+ } else {
680
+ delete bundle.metadata.tax_fee_rounding_remainder;
681
+ delete bundle.tax_fee_rounding_remainder;
682
+ }
683
+ bundle.original_tax_fee_rounding_remainder = 0;
235
684
  }
236
-
237
- // --- 主商品原价税费 ---
238
- const originalTotal = getUnitOriginalTotal(product);
239
- const originalTaxableBase = originalTotal.lte(0) ? surchargeFee : originalTotal.plus(surchargeFee);
240
- const originalTaxPrecise = calculateSingleItemTax({
241
- price: originalTaxableBase,
242
- taxRate: rate,
243
- isPriceIncludeTax,
244
- isChargeTax: product.is_charge_tax ?? 0
245
- });
246
-
247
- // --- bundle 原价子商品独立计税 ---
248
- let bundlePreciseTax = new _decimal.default(0);
249
- let bundleRoundedTax = new _decimal.default(0);
250
- let bundlePreciseOriginTax = new _decimal.default(0);
251
- let bundleRoundedOriginTax = new _decimal.default(0);
685
+ }
686
+ }
687
+ function getBundlePaymentPrice(bundle, product) {
688
+ var _a;
689
+ if ((bundle == null ? void 0 : bundle.bundle_payment_price) !== void 0)
690
+ return toDecimal(bundle.bundle_payment_price);
691
+ const basePrice = toDecimal((bundle == null ? void 0 : bundle.bundle_selling_price) ?? (bundle == null ? void 0 : bundle.bundle_sum_price) ?? (bundle == null ? void 0 : bundle.price) ?? 0);
692
+ if (!isBundleOriginalPrice(bundle))
693
+ return basePrice;
694
+ const averageDiscountRate = toDecimal(
695
+ ((_a = product.metadata) == null ? void 0 : _a.average_discount_amount_rate) ?? 1
696
+ );
697
+ return toAmountFormat(bcMul(basePrice, averageDiscountRate, 6));
698
+ }
699
+ function applyBackendTaxRemainder(params) {
700
+ const { target, rawTax, formattedTax, quantity, state } = params;
701
+ const taxFeeDifference = bcMul(
702
+ bcSub(rawTax, formattedTax, 6),
703
+ quantity,
704
+ 6
705
+ );
706
+ state.difference = bcAdd(state.difference, taxFeeDifference, 6);
707
+ if (state.difference.lt(5e-3))
708
+ return new import_decimal.default(0);
709
+ const roundingRemainder = toAmountFormat(state.difference);
710
+ writeTaxRemainder(target, roundingRemainder);
711
+ state.difference = bcSub(state.difference, roundingRemainder, 6);
712
+ return roundingRemainder;
713
+ }
714
+ function calculateProductsTax(products, taxRate, isPriceIncludeTax) {
715
+ const rate = new import_decimal.default(taxRate);
716
+ const taxRemainderState = { difference: new import_decimal.default(0) };
717
+ let totalTax = new import_decimal.default(0);
718
+ let totalOriginTax = new import_decimal.default(0);
719
+ resetTaxRemainderFields(products);
720
+ for (const product of products) {
721
+ const quantity = new import_decimal.default(getSafeNum(product.num));
722
+ let productUnitTax = new import_decimal.default(0);
723
+ let productUnitOriginTax = new import_decimal.default(0);
252
724
  const bundleItems = product.product_bundle || [];
253
725
  for (const bundleItem of bundleItems) {
254
- if (!isBundleOriginalPrice(bundleItem)) continue;
255
- const bundleQuantity = new _decimal.default(getSafeNum(bundleItem.num ?? bundleItem.quantity));
256
- const bundlePrice = toDecimal(bundleItem.bundle_selling_price ?? bundleItem.price ?? 0);
726
+ if (!isBundleOriginalPrice(bundleItem))
727
+ continue;
728
+ const bundleQuantity = new import_decimal.default(getSafeNum(bundleItem.num ?? bundleItem.quantity));
729
+ const totalBundleQuantity = bundleQuantity.times(quantity);
730
+ const bundlePrice = getBundlePaymentPrice(bundleItem, product);
257
731
  const bundleOriginalPrice = toDecimal(bundleItem.original_price ?? bundleItem.product_price ?? bundleItem.price ?? 0);
732
+ const bundleSurchargeFee = toDecimal(bundleItem.surcharge_fee);
258
733
  const bundleTaxPrecise = calculateSingleItemTax({
259
- price: bundlePrice,
734
+ price: bundlePrice.plus(bundleSurchargeFee),
260
735
  taxRate: rate,
261
736
  isPriceIncludeTax,
262
737
  isChargeTax: bundleItem.is_charge_tax ?? product.is_charge_tax ?? 0
263
738
  });
264
- const bundleTaxRounded = bundleTaxPrecise.toDecimalPlaces(2, _decimal.default.ROUND_HALF_UP);
739
+ const bundleTaxRounded = toBackendTaxFee(bundleTaxPrecise);
740
+ bundleItem.tax_fee = bundleTaxRounded.toNumber();
741
+ const persistedBundleRemainder = getPersistedTaxRemainder(bundleItem, product);
742
+ const bundleRemainder = persistedBundleRemainder ?? applyBackendTaxRemainder({
743
+ target: bundleItem,
744
+ rawTax: bundleTaxPrecise,
745
+ formattedTax: bundleTaxRounded,
746
+ quantity: totalBundleQuantity,
747
+ state: taxRemainderState
748
+ });
749
+ if (persistedBundleRemainder)
750
+ writeTaxRemainder(bundleItem, persistedBundleRemainder);
265
751
  const bundleOrigTaxPrecise = calculateSingleItemTax({
266
- price: bundleOriginalPrice,
752
+ price: bundleOriginalPrice.plus(bundleSurchargeFee),
267
753
  taxRate: rate,
268
754
  isPriceIncludeTax,
269
755
  isChargeTax: bundleItem.is_charge_tax ?? product.is_charge_tax ?? 0
270
756
  });
271
- const bundleOrigTaxRounded = bundleOrigTaxPrecise.toDecimalPlaces(2, _decimal.default.ROUND_HALF_UP);
272
- bundleItem.tax_fee = bundleTaxRounded.toNumber();
757
+ const bundleOrigTaxRounded = toAmountFormat(bundleOrigTaxPrecise);
273
758
  bundleItem.original_tax_fee = bundleOrigTaxRounded.toNumber();
274
- bundlePreciseTax = bundlePreciseTax.plus(bundleTaxPrecise.times(bundleQuantity));
275
- bundleRoundedTax = bundleRoundedTax.plus(bundleTaxRounded.times(bundleQuantity));
276
- bundlePreciseOriginTax = bundlePreciseOriginTax.plus(bundleOrigTaxPrecise.times(bundleQuantity));
277
- bundleRoundedOriginTax = bundleRoundedOriginTax.plus(bundleOrigTaxRounded.times(bundleQuantity));
278
- if (bundleTaxPrecise.gt(0)) {
279
- lastTaxableItem = {
280
- type: 'bundle',
281
- item: bundleItem
282
- };
283
- }
759
+ productUnitTax = productUnitTax.plus(bundleTaxRounded.times(bundleQuantity));
760
+ productUnitOriginTax = productUnitOriginTax.plus(bundleOrigTaxRounded.times(bundleQuantity));
761
+ totalTax = totalTax.plus(bundleTaxRounded.times(totalBundleQuantity)).plus(bundleRemainder);
762
+ totalOriginTax = totalOriginTax.plus(bundleOrigTaxRounded.times(totalBundleQuantity));
284
763
  }
285
-
286
- // --- 汇总到商品级 ---
287
- const itemTaxPrecise = mainTaxPrecise.plus(bundlePreciseTax);
288
- const itemTaxRounded = mainTaxRounded.plus(bundleRoundedTax);
289
- const itemOriginTaxPrecise = originalTaxPrecise.plus(bundlePreciseOriginTax);
290
- const itemOriginTaxRounded = originalTaxPrecise.toDecimalPlaces(2, _decimal.default.ROUND_HALF_UP).plus(bundleRoundedOriginTax);
291
- product.tax_fee = itemTaxRounded.toDecimalPlaces(2, _decimal.default.ROUND_HALF_UP).toFixed(2);
292
- product.original_tax_fee = itemOriginTaxRounded.toDecimalPlaces(2, _decimal.default.ROUND_HALF_UP).toNumber();
293
- preciseTax = preciseTax.plus(itemTaxPrecise.times(quantity));
294
- roundedTax = roundedTax.plus(itemTaxRounded.times(quantity));
295
- preciseOriginTax = preciseOriginTax.plus(itemOriginTaxPrecise.times(quantity));
296
- roundedOriginTax = roundedOriginTax.plus(itemOriginTaxRounded.times(quantity));
297
- }
298
-
299
- // --- 舍入差值处理 ---
300
- const expectedTax = preciseTax.toDecimalPlaces(2, _decimal.default.ROUND_HALF_UP);
301
- const expectedOriginTax = preciseOriginTax.toDecimalPlaces(2, _decimal.default.ROUND_HALF_UP);
302
- const taxRemainder = expectedTax.minus(roundedTax).toNumber();
303
- const originTaxRemainder = expectedOriginTax.minus(roundedOriginTax).toNumber();
304
- if (lastTaxableItem) {
305
- if (taxRemainder !== 0) {
306
- lastTaxableItem.item.tax_fee_rounding_remainder = taxRemainder;
764
+ const mainTotal = getMainProductPaymentTotal(product);
765
+ const surchargeFee = toDecimal(product.surcharge_fee);
766
+ const mainTaxableBase = mainTotal.lte(0) ? surchargeFee : mainTotal.plus(surchargeFee);
767
+ const mainTaxPrecise = calculateSingleItemTax({
768
+ price: mainTaxableBase,
769
+ taxRate: rate,
770
+ isPriceIncludeTax,
771
+ isChargeTax: product.is_charge_tax ?? 0
772
+ });
773
+ const mainTaxRounded = toBackendTaxFee(mainTaxPrecise);
774
+ const persistedMainRemainder = getPersistedTaxRemainder(product);
775
+ const persistedMainTaxFee = getPersistedMainTaxFee(product);
776
+ const persistedQuantity = getPersistedQuantity(product, quantity);
777
+ const deltaQuantity = import_decimal.default.max(quantity.minus(persistedQuantity), 0);
778
+ const shouldUsePersistedMainTax = persistedMainTaxFee && persistedMainTaxFee.gt(0) && persistedQuantity.gte(quantity);
779
+ const shouldSplitPersistedMainTax = persistedMainTaxFee && persistedMainTaxFee.gt(0) && persistedQuantity.gt(0) && deltaQuantity.gt(0);
780
+ const mainRemainder = shouldUsePersistedMainTax || shouldSplitPersistedMainTax ? new import_decimal.default(0) : persistedMainRemainder ?? applyBackendTaxRemainder({
781
+ target: product,
782
+ rawTax: mainTaxPrecise,
783
+ formattedTax: mainTaxRounded,
784
+ quantity,
785
+ state: taxRemainderState
786
+ });
787
+ if (persistedMainRemainder && !shouldSplitPersistedMainTax) {
788
+ writeTaxRemainder(product, persistedMainRemainder);
307
789
  }
308
- if (originTaxRemainder !== 0) {
309
- lastTaxableItem.item.original_tax_fee_rounding_remainder = originTaxRemainder;
790
+ const originalTotal = getUnitOriginalTotal(product);
791
+ const originalTaxableBase = originalTotal.lte(0) ? surchargeFee : originalTotal.plus(surchargeFee);
792
+ const originalTaxPrecise = calculateSingleItemTax({
793
+ price: originalTaxableBase,
794
+ taxRate: rate,
795
+ isPriceIncludeTax,
796
+ isChargeTax: product.is_charge_tax ?? 0
797
+ });
798
+ const originalTaxRounded = toAmountFormat(originalTaxPrecise);
799
+ const effectiveMainUnitTax = shouldUsePersistedMainTax ? persistedMainTaxFee : shouldSplitPersistedMainTax ? persistedMainTaxFee.times(persistedQuantity).plus(mainTaxRounded.times(deltaQuantity)).div(quantity).toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP) : mainTaxRounded;
800
+ productUnitTax = productUnitTax.plus(effectiveMainUnitTax);
801
+ productUnitOriginTax = productUnitOriginTax.plus(originalTaxRounded);
802
+ product.main_product_attached_bundle_tax_fee = effectiveMainUnitTax.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toFixed(2);
803
+ product.tax_fee = productUnitTax.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toFixed(2);
804
+ product.original_tax_fee = productUnitOriginTax.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toNumber();
805
+ totalTax = totalTax.plus(
806
+ shouldUsePersistedMainTax ? persistedMainTaxFee.times(quantity).plus(persistedMainRemainder || 0) : shouldSplitPersistedMainTax ? persistedMainTaxFee.times(persistedQuantity).plus(mainTaxRounded.times(deltaQuantity)) : mainTaxRounded.times(quantity).plus(mainRemainder)
807
+ );
808
+ totalOriginTax = totalOriginTax.plus(originalTaxRounded.times(quantity));
809
+ if (product.tax_fee_rounding_remainder === void 0) {
810
+ product.tax_fee_rounding_remainder = 0;
310
811
  }
311
812
  }
312
813
  return {
313
- tax: expectedTax,
314
- originTax: expectedOriginTax
814
+ tax: totalTax.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP),
815
+ originTax: totalOriginTax.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP)
315
816
  };
316
817
  }
818
+ function allocateShopDiscountToPayment(products, productAmount, shopDiscountAmount) {
819
+ const effectiveDiscount = productAmount.lte(0) ? new import_decimal.default(0) : import_decimal.default.max(import_decimal.default.min(productAmount, shopDiscountAmount), 0);
820
+ let allocatedDiscount = new import_decimal.default(0);
821
+ products.forEach((product, index) => {
822
+ const quantity = new import_decimal.default(getSafeNum(product.num));
823
+ const sellingUnit = getUnitPaymentTotal(product);
824
+ const lineSellingTotal = sellingUnit.times(quantity);
825
+ const lineDiscount = effectiveDiscount.lte(0) ? new import_decimal.default(0) : index === products.length - 1 ? import_decimal.default.max(effectiveDiscount.minus(allocatedDiscount), 0) : toAmountFormat(lineSellingTotal.div(productAmount).times(effectiveDiscount));
826
+ allocatedDiscount = allocatedDiscount.plus(lineDiscount);
827
+ const unitDiscount = quantity.lte(0) ? new import_decimal.default(0) : lineDiscount.div(quantity);
828
+ const paymentUnit = import_decimal.default.max(sellingUnit.minus(unitDiscount), 0);
829
+ const rate = sellingUnit.lte(0) ? new import_decimal.default(1) : toBcScale(paymentUnit.div(sellingUnit), 6);
830
+ const mainSellingTotal = getMainAttachedBundleSellingTotal(product);
831
+ const mainPaymentTotal = toAmountFormat(bcMul(mainSellingTotal, rate, 6));
832
+ product.metadata = {
833
+ ...product.metadata || {},
834
+ average_discount_amount_rate: rate.toString(),
835
+ main_product_attached_bundle_selling_price: mainSellingTotal.toFixed(2),
836
+ main_product_attached_bundle_payment_price: mainPaymentTotal.toFixed(2)
837
+ };
838
+ for (const bundle of product.product_bundle || []) {
839
+ if (isBundleOriginalPrice(bundle)) {
840
+ const bundleSelling = toDecimal(
841
+ bundle.bundle_selling_price ?? bundle.bundle_sum_price ?? bundle.price ?? 0
842
+ );
843
+ bundle.bundle_payment_price = toAmountFormat(bcMul(bundleSelling, rate, 6)).toFixed(2);
844
+ continue;
845
+ }
846
+ if ((0, import_utils2.isMarkdownBundle)(bundle)) {
847
+ const magnitude = (0, import_utils2.getBundleSellingMagnitude)(bundle);
848
+ bundle.custom_price = bundle.custom_price ?? bundle.price;
849
+ bundle.price = magnitude.toFixed(2);
850
+ bundle.bundle_selling_price = magnitude.toFixed(2);
851
+ bundle.bundle_payment_price = toAmountFormat(bcMul(magnitude, rate, 6)).toFixed(2);
852
+ }
853
+ }
854
+ product.payment_price = paymentUnit.toFixed(2);
855
+ });
856
+ }
317
857
  function createEmptySalesSummary() {
318
858
  return {
319
859
  product_quantity: 0,
320
- product_original_amount: '0.00',
321
- product_amount: '0.00',
322
- product_expect_amount: '0.00',
323
- product_tax_fee: '0.00',
324
- shipping_fee: '0.00',
325
- shipping_tax_fee: '0.00',
326
- tax_fee: '0.00',
327
- surcharge_fee: '0.00',
860
+ product_original_amount: "0.00",
861
+ product_amount: "0.00",
862
+ product_expect_amount: "0.00",
863
+ product_tax_fee: "0.00",
864
+ shipping_fee: "0.00",
865
+ shipping_tax_fee: "0.00",
866
+ tax_fee: "0.00",
867
+ surcharge_fee: "0.00",
328
868
  surcharges: [],
329
- discount_amount: '0.00',
330
- deposit_amount: '0.00',
331
- expect_amount: '0.00',
332
- total_amount: '0.00',
333
- amount_gap: '0.00',
334
- rounding_amount: '0.00',
335
- pay_service_charge_amount: '0.00'
869
+ discount_amount: "0.00",
870
+ deposit_amount: "0.00",
871
+ expect_amount: "0.00",
872
+ total_amount: "0.00",
873
+ total_refund_amount: "0.00",
874
+ customer_paid_amount: "0.00",
875
+ order_paid_amount: "0.00",
876
+ amount_gap: "0.00",
877
+ rounding_amount: "0.00",
878
+ pay_service_charge_amount: "0.00"
336
879
  };
337
880
  }
338
881
  function calculateSalesSummary(params) {
@@ -342,64 +885,102 @@ function calculateSalesSummary(params) {
342
885
  taxRate,
343
886
  surchargeList,
344
887
  scheduleById = {},
345
- isInScheduleByDate
346
- } = params;
347
- if (!products?.length) return createEmptySalesSummary();
348
- const productQuantity = products.reduce((sum, item) => sum + getSafeNum(item.num), 0);
349
- const productOriginalAmount = products.reduce((sum, item) => sum.plus(getUnitOriginalTotal(item).times(getSafeNum(item.num))), new _decimal.default(0));
350
- const productAmount = products.reduce((sum, item) => sum.plus(getUnitPaymentTotal(item).times(getSafeNum(item.num))), new _decimal.default(0));
351
- const surchargeServiceItems = buildSurchargeServiceItems(products);
352
- const surcharge = (0, _utils.getSurcharge)({
353
- service: surchargeServiceItems,
354
- addons: [],
355
- bookingDetail: null,
356
- bookingId: undefined
357
- }, {
358
- isEdit: false,
359
888
  isInScheduleByDate,
360
- surcharge_list: surchargeList,
361
- scheduleById
889
+ shopDiscount
890
+ } = params;
891
+ if (!(products == null ? void 0 : products.length))
892
+ return createEmptySalesSummary();
893
+ const summaryProducts = products.filter((product) => !isGeneratedVoucherProduct(product));
894
+ if (!summaryProducts.length)
895
+ return createEmptySalesSummary();
896
+ const productQuantity = summaryProducts.reduce(
897
+ (sum, item) => sum + getSafeNum(item.num),
898
+ 0
899
+ );
900
+ const productOriginalAmount = summaryProducts.reduce(
901
+ (sum, item) => sum.plus(getUnitOriginalTotal(item).times(getSafeNum(item.num))),
902
+ new import_decimal.default(0)
903
+ );
904
+ const productAmount = summaryProducts.reduce(
905
+ (sum, item) => sum.plus(getUnitPaymentTotal(item).times(getSafeNum(item.num))),
906
+ new import_decimal.default(0)
907
+ );
908
+ const shopDiscountAmount = import_decimal.default.max(new import_decimal.default(Number(shopDiscount) || 0), 0);
909
+ const hasPersistedSurchargeSnapshot = summaryProducts.some((product) => {
910
+ var _a;
911
+ return (_a = getPersistedMainSurchargeFee(product)) == null ? void 0 : _a.gt(0);
362
912
  });
363
- const surchargeAmount = new _decimal.default((0, _utils.getSurchargeAmount)({
364
- bookingDetail: null,
365
- bookingId: undefined
366
- }, surcharge, {
367
- isEdit: false
368
- }) || 0);
369
- for (let i = 0; i < products.length; i++) {
370
- products[i].surcharge_fee = surchargeServiceItems[i]?.surcharge_fee || 0;
371
- products[i].surcharge_rounding_remainder = surchargeServiceItems[i]?.surcharge_rounding_remainder || 0;
913
+ if (!hasPersistedSurchargeSnapshot) {
914
+ allocateShopDiscountToPayment(summaryProducts, productAmount, shopDiscountAmount);
372
915
  }
373
- const hasTaxRate = taxRate !== undefined && taxRate !== null && taxRate > 0;
916
+ const surchargeServiceItems = buildSurchargeServiceItems(summaryProducts);
917
+ (0, import_utils.getSurcharge)(
918
+ {
919
+ service: surchargeServiceItems,
920
+ addons: [],
921
+ bookingDetail: null,
922
+ bookingId: void 0
923
+ },
924
+ {
925
+ isEdit: false,
926
+ isInScheduleByDate,
927
+ surcharge_list: surchargeList,
928
+ scheduleById
929
+ }
930
+ );
931
+ const surcharge = calculateBackendProductSurcharges({
932
+ products: summaryProducts,
933
+ sourceItems: surchargeServiceItems,
934
+ surchargeList
935
+ });
936
+ const surchargeAmount = new import_decimal.default(
937
+ (0, import_utils.getSurchargeAmount)(
938
+ { bookingDetail: null, bookingId: void 0 },
939
+ surcharge,
940
+ { isEdit: false }
941
+ ) || 0
942
+ );
943
+ const hasTaxRate = taxRate !== void 0 && taxRate !== null && taxRate > 0;
374
944
  let productTaxFee;
375
945
  if (hasTaxRate) {
376
- const taxResult = calculateProductsTax(products, taxRate, isPriceIncludeTax);
946
+ const taxResult = calculateProductsTax(summaryProducts, taxRate, isPriceIncludeTax);
377
947
  productTaxFee = taxResult.tax;
378
948
  } else {
379
- productTaxFee = products.reduce((sum, item) => sum.plus(toDecimal(item.tax_fee).times(getSafeNum(item.num))), new _decimal.default(0));
949
+ productTaxFee = summaryProducts.reduce(
950
+ (sum, item) => sum.plus(toDecimal(item.tax_fee).times(getSafeNum(item.num))),
951
+ new import_decimal.default(0)
952
+ );
380
953
  }
381
- const expectAmount = productAmount.plus(surchargeAmount);
382
- const totalAmount = isPriceIncludeTax === 1 ? expectAmount : expectAmount.plus(productTaxFee);
383
- const deposit = calculateProductsDeposit(products);
954
+ const orderAmountBeforeDiscount = productAmount.plus(surchargeAmount).plus(isPriceIncludeTax === 1 ? 0 : productTaxFee);
955
+ const totalAmount = import_decimal.default.max(0, orderAmountBeforeDiscount.minus(shopDiscountAmount));
956
+ const deposit = calculateProductsDeposit(summaryProducts);
384
957
  return {
385
958
  product_quantity: productQuantity,
386
959
  product_original_amount: toFixed2(productOriginalAmount),
387
960
  product_amount: toFixed2(productAmount),
388
961
  product_expect_amount: toFixed2(productAmount),
389
962
  product_tax_fee: toFixed2(productTaxFee),
390
- shipping_fee: '0.00',
391
- shipping_tax_fee: '0.00',
963
+ shipping_fee: "0.00",
964
+ shipping_tax_fee: "0.00",
392
965
  tax_fee: toFixed2(productTaxFee),
393
966
  surcharge_fee: toFixed2(surchargeAmount),
394
967
  surcharges: surcharge,
395
- discount_amount: toFixed2(_decimal.default.max(productOriginalAmount.minus(productAmount), 0)),
396
- deposit_amount: deposit ? deposit.total : '0.00',
968
+ discount_amount: toFixed2(shopDiscountAmount),
969
+ deposit_amount: deposit ? deposit.total : "0.00",
397
970
  deposit,
398
- expect_amount: toFixed2(expectAmount),
971
+ expect_amount: toFixed2(totalAmount),
399
972
  total_amount: toFixed2(totalAmount),
400
- amount_gap: '0.00',
401
- rounding_amount: '0.00',
402
- pay_service_charge_amount: '0.00',
973
+ total_refund_amount: "0.00",
974
+ customer_paid_amount: "0.00",
975
+ order_paid_amount: "0.00",
976
+ amount_gap: "0.00",
977
+ rounding_amount: "0.00",
978
+ pay_service_charge_amount: "0.00",
403
979
  is_price_include_tax: isPriceIncludeTax
404
980
  };
405
- }
981
+ }
982
+ // Annotate the CommonJS export names for ESM import in node:
983
+ 0 && (module.exports = {
984
+ calculateSalesSummary,
985
+ createEmptySalesSummary
986
+ });