@managesome/eposmatic-toolkit 0.4.9 → 0.4.10

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 (333) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +448 -0
  2. package/esm/helpers/index.js +3 -0
  3. package/esm/helpers/materialized-reports/index.js +32 -0
  4. package/esm/helpers/materialized-reports/materialized-cash-accumulator-helpers.js +114 -0
  5. package/esm/helpers/materialized-reports/materialized-cash-classification-helpers.js +286 -0
  6. package/esm/helpers/materialized-reports/materialized-cash-close-day-helpers.js +396 -0
  7. package/esm/helpers/materialized-reports/materialized-cash-compile-helpers.js +244 -0
  8. package/esm/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +170 -0
  9. package/esm/helpers/materialized-reports/materialized-delivery-classification-helpers.js +426 -0
  10. package/esm/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +372 -0
  11. package/esm/helpers/materialized-reports/materialized-delivery-compile-helpers.js +214 -0
  12. package/esm/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +318 -0
  13. package/esm/helpers/materialized-reports/materialized-discount-classification-helpers.js +213 -0
  14. package/esm/helpers/materialized-reports/materialized-discount-close-day-helpers.js +604 -0
  15. package/esm/helpers/materialized-reports/materialized-discount-compile-helpers.js +186 -0
  16. package/esm/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.js +155 -0
  17. package/esm/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.js +199 -0
  18. package/esm/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.js +111 -0
  19. package/esm/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +88 -0
  20. package/esm/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.js +394 -0
  21. package/esm/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.js +167 -0
  22. package/esm/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +215 -0
  23. package/esm/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +476 -0
  24. package/esm/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +201 -0
  25. package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +7 -0
  26. package/esm/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.js +189 -0
  27. package/esm/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.js +157 -0
  28. package/esm/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.js +588 -0
  29. package/esm/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.js +175 -0
  30. package/esm/helpers/materialized-reports/tenant-cash-in-out-formatter.js +156 -0
  31. package/esm/helpers/materialized-reports/tenant-delivery-formatter.js +230 -0
  32. package/esm/helpers/materialized-reports/tenant-discount-formatter.js +427 -0
  33. package/esm/helpers/materialized-reports/tenant-hourly-comparison-formatter.js +237 -0
  34. package/esm/helpers/materialized-reports/tenant-payments-comparison-formatter.js +279 -0
  35. package/esm/helpers/materialized-reports/tenant-prep-time-formatter.js +228 -0
  36. package/esm/helpers/materialized-reports/tenant-sales-comparison-formatter.js +343 -0
  37. package/esm/helpers/reporting-contract/index.js +2 -0
  38. package/esm/helpers/reporting-contract/overview-contract.js +122 -0
  39. package/esm/helpers/reporting-contract/report-contract.js +228 -0
  40. package/esm/helpers/rider-earnings/index.js +1 -0
  41. package/esm/helpers/rider-earnings/rider-earnings.js +59 -0
  42. package/esm/helpers/rider-reports/index.js +2 -0
  43. package/esm/helpers/rider-reports/rider-daily-report.js +90 -0
  44. package/esm/helpers/rider-reports/rider-report.js +67 -0
  45. package/esm/schemas/models/deliveries.js +75 -0
  46. package/esm/schemas/models/index.js +15 -0
  47. package/esm/schemas/models/materialized_rider_daily_reports.js +39 -0
  48. package/esm/schemas/models/materialized_tenant_cash_entries.js +95 -0
  49. package/esm/schemas/models/materialized_tenant_cash_reports.js +77 -0
  50. package/esm/schemas/models/materialized_tenant_delivery_entries.js +63 -0
  51. package/esm/schemas/models/materialized_tenant_delivery_reports.js +63 -0
  52. package/esm/schemas/models/materialized_tenant_discount_entries.js +156 -0
  53. package/esm/schemas/models/materialized_tenant_discount_reports.js +333 -0
  54. package/esm/schemas/models/materialized_tenant_hourly_comparison_entries.js +87 -0
  55. package/esm/schemas/models/materialized_tenant_hourly_comparison_reports.js +99 -0
  56. package/esm/schemas/models/materialized_tenant_payments_comparison_entries.js +71 -0
  57. package/esm/schemas/models/materialized_tenant_payments_comparison_reports.js +76 -0
  58. package/esm/schemas/models/materialized_tenant_prep_time_entries.js +130 -0
  59. package/esm/schemas/models/materialized_tenant_prep_time_reports.js +109 -0
  60. package/esm/schemas/models/materialized_tenant_sales_comparison_entries.js +130 -0
  61. package/esm/schemas/models/materialized_tenant_sales_comparison_reports.js +154 -0
  62. package/esm/schemas/types/index.js +15 -0
  63. package/esm/schemas/types/materialized_rider_daily_reports.js +1 -0
  64. package/esm/schemas/types/materialized_tenant_cash_entries.js +1 -0
  65. package/esm/schemas/types/materialized_tenant_cash_reports.js +1 -0
  66. package/esm/schemas/types/materialized_tenant_delivery_entries.js +1 -0
  67. package/esm/schemas/types/materialized_tenant_delivery_reports.js +1 -0
  68. package/esm/schemas/types/materialized_tenant_discount_entries.js +1 -0
  69. package/esm/schemas/types/materialized_tenant_discount_reports.js +2 -0
  70. package/esm/schemas/types/materialized_tenant_hourly_comparison_entries.js +1 -0
  71. package/esm/schemas/types/materialized_tenant_hourly_comparison_reports.js +1 -0
  72. package/esm/schemas/types/materialized_tenant_payments_comparison_entries.js +1 -0
  73. package/esm/schemas/types/materialized_tenant_payments_comparison_reports.js +11 -0
  74. package/esm/schemas/types/materialized_tenant_prep_time_entries.js +1 -0
  75. package/esm/schemas/types/materialized_tenant_prep_time_reports.js +1 -0
  76. package/esm/schemas/types/materialized_tenant_sales_comparison_entries.js +1 -0
  77. package/esm/schemas/types/materialized_tenant_sales_comparison_reports.js +14 -0
  78. package/lib/constants/MATERIALIZED_REPORTS.d.ts +339 -2
  79. package/lib/constants/MATERIALIZED_REPORTS.js +450 -1
  80. package/lib/helpers/index.d.ts +3 -0
  81. package/lib/helpers/index.js +3 -0
  82. package/lib/helpers/materialized-reports/index.d.ts +32 -0
  83. package/lib/helpers/materialized-reports/index.js +32 -0
  84. package/lib/helpers/materialized-reports/materialized-cash-accumulator-helpers.d.ts +18 -0
  85. package/lib/helpers/materialized-reports/materialized-cash-accumulator-helpers.js +118 -0
  86. package/lib/helpers/materialized-reports/materialized-cash-classification-helpers.d.ts +90 -0
  87. package/lib/helpers/materialized-reports/materialized-cash-classification-helpers.js +295 -0
  88. package/lib/helpers/materialized-reports/materialized-cash-close-day-helpers.d.ts +102 -0
  89. package/lib/helpers/materialized-reports/materialized-cash-close-day-helpers.js +409 -0
  90. package/lib/helpers/materialized-reports/materialized-cash-compile-helpers.d.ts +72 -0
  91. package/lib/helpers/materialized-reports/materialized-cash-compile-helpers.js +251 -0
  92. package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.d.ts +7 -0
  93. package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +173 -0
  94. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.d.ts +84 -0
  95. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.js +438 -0
  96. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.d.ts +97 -0
  97. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +385 -0
  98. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.d.ts +40 -0
  99. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.js +218 -0
  100. package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.d.ts +52 -0
  101. package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +324 -0
  102. package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.d.ts +50 -0
  103. package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.js +222 -0
  104. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.d.ts +182 -0
  105. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.js +615 -0
  106. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.d.ts +47 -0
  107. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.js +193 -0
  108. package/lib/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.d.ts +64 -0
  109. package/lib/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.js +163 -0
  110. package/lib/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.d.ts +61 -0
  111. package/lib/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.js +207 -0
  112. package/lib/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.d.ts +23 -0
  113. package/lib/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.js +116 -0
  114. package/lib/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.d.ts +25 -0
  115. package/lib/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +98 -0
  116. package/lib/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.d.ts +79 -0
  117. package/lib/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.js +407 -0
  118. package/lib/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.d.ts +35 -0
  119. package/lib/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.js +173 -0
  120. package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.d.ts +66 -0
  121. package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +224 -0
  122. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.d.ts +128 -0
  123. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +490 -0
  124. package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.d.ts +46 -0
  125. package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +208 -0
  126. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.d.ts +8 -0
  127. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +8 -1
  128. package/lib/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.d.ts +48 -0
  129. package/lib/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.js +204 -0
  130. package/lib/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.d.ts +22 -0
  131. package/lib/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.js +161 -0
  132. package/lib/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.d.ts +163 -0
  133. package/lib/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.js +603 -0
  134. package/lib/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.d.ts +68 -0
  135. package/lib/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.js +182 -0
  136. package/lib/helpers/materialized-reports/tenant-cash-in-out-formatter.d.ts +98 -0
  137. package/lib/helpers/materialized-reports/tenant-cash-in-out-formatter.js +159 -0
  138. package/lib/helpers/materialized-reports/tenant-delivery-formatter.d.ts +133 -0
  139. package/lib/helpers/materialized-reports/tenant-delivery-formatter.js +233 -0
  140. package/lib/helpers/materialized-reports/tenant-discount-formatter.d.ts +114 -0
  141. package/lib/helpers/materialized-reports/tenant-discount-formatter.js +431 -0
  142. package/lib/helpers/materialized-reports/tenant-hourly-comparison-formatter.d.ts +60 -0
  143. package/lib/helpers/materialized-reports/tenant-hourly-comparison-formatter.js +243 -0
  144. package/lib/helpers/materialized-reports/tenant-payments-comparison-formatter.d.ts +75 -0
  145. package/lib/helpers/materialized-reports/tenant-payments-comparison-formatter.js +286 -0
  146. package/lib/helpers/materialized-reports/tenant-prep-time-formatter.d.ts +97 -0
  147. package/lib/helpers/materialized-reports/tenant-prep-time-formatter.js +232 -0
  148. package/lib/helpers/materialized-reports/tenant-sales-comparison-formatter.d.ts +105 -0
  149. package/lib/helpers/materialized-reports/tenant-sales-comparison-formatter.js +349 -0
  150. package/lib/helpers/reporting-contract/index.d.ts +2 -0
  151. package/lib/helpers/reporting-contract/index.js +18 -0
  152. package/lib/helpers/reporting-contract/overview-contract.d.ts +55 -0
  153. package/lib/helpers/reporting-contract/overview-contract.js +126 -0
  154. package/lib/helpers/reporting-contract/report-contract.d.ts +159 -0
  155. package/lib/helpers/reporting-contract/report-contract.js +239 -0
  156. package/lib/helpers/rider-earnings/index.d.ts +1 -0
  157. package/lib/helpers/rider-earnings/index.js +17 -0
  158. package/lib/helpers/rider-earnings/rider-earnings.d.ts +58 -0
  159. package/lib/helpers/rider-earnings/rider-earnings.js +64 -0
  160. package/lib/helpers/rider-reports/index.d.ts +2 -0
  161. package/lib/helpers/rider-reports/index.js +18 -0
  162. package/lib/helpers/rider-reports/rider-daily-report.d.ts +26 -0
  163. package/lib/helpers/rider-reports/rider-daily-report.js +93 -0
  164. package/lib/helpers/rider-reports/rider-report.d.ts +49 -0
  165. package/lib/helpers/rider-reports/rider-report.js +70 -0
  166. package/lib/helpers/types/materialized-reports-apis.d.ts +297 -1
  167. package/lib/schemas/models/deliveries.d.ts +54 -0
  168. package/lib/schemas/models/deliveries.js +75 -0
  169. package/lib/schemas/models/expense_details.d.ts +3 -3
  170. package/lib/schemas/models/index.d.ts +15 -0
  171. package/lib/schemas/models/index.js +15 -0
  172. package/lib/schemas/models/materialized_rider_daily_reports.d.ts +27 -0
  173. package/lib/schemas/models/materialized_rider_daily_reports.js +42 -0
  174. package/lib/schemas/models/materialized_tenant_cash_entries.d.ts +181 -0
  175. package/lib/schemas/models/materialized_tenant_cash_entries.js +98 -0
  176. package/lib/schemas/models/materialized_tenant_cash_reports.d.ts +111 -0
  177. package/lib/schemas/models/materialized_tenant_cash_reports.js +80 -0
  178. package/lib/schemas/models/materialized_tenant_delivery_entries.d.ts +98 -0
  179. package/lib/schemas/models/materialized_tenant_delivery_entries.js +66 -0
  180. package/lib/schemas/models/materialized_tenant_delivery_reports.d.ts +98 -0
  181. package/lib/schemas/models/materialized_tenant_delivery_reports.js +66 -0
  182. package/lib/schemas/models/materialized_tenant_discount_entries.d.ts +199 -0
  183. package/lib/schemas/models/materialized_tenant_discount_entries.js +159 -0
  184. package/lib/schemas/models/materialized_tenant_discount_reports.d.ts +216 -0
  185. package/lib/schemas/models/materialized_tenant_discount_reports.js +336 -0
  186. package/lib/schemas/models/materialized_tenant_hourly_comparison_entries.d.ts +95 -0
  187. package/lib/schemas/models/materialized_tenant_hourly_comparison_entries.js +82 -0
  188. package/lib/schemas/models/materialized_tenant_hourly_comparison_reports.d.ts +105 -0
  189. package/lib/schemas/models/materialized_tenant_hourly_comparison_reports.js +91 -0
  190. package/lib/schemas/models/materialized_tenant_payments_comparison_entries.d.ts +91 -0
  191. package/lib/schemas/models/materialized_tenant_payments_comparison_entries.js +70 -0
  192. package/lib/schemas/models/materialized_tenant_payments_comparison_reports.d.ts +98 -0
  193. package/lib/schemas/models/materialized_tenant_payments_comparison_reports.js +71 -0
  194. package/lib/schemas/models/materialized_tenant_prep_time_entries.d.ts +181 -0
  195. package/lib/schemas/models/materialized_tenant_prep_time_entries.js +133 -0
  196. package/lib/schemas/models/materialized_tenant_prep_time_reports.d.ts +118 -0
  197. package/lib/schemas/models/materialized_tenant_prep_time_reports.js +112 -0
  198. package/lib/schemas/models/materialized_tenant_sales_comparison_entries.d.ts +139 -0
  199. package/lib/schemas/models/materialized_tenant_sales_comparison_entries.js +120 -0
  200. package/lib/schemas/models/materialized_tenant_sales_comparison_reports.d.ts +167 -0
  201. package/lib/schemas/models/materialized_tenant_sales_comparison_reports.js +137 -0
  202. package/lib/schemas/models/tenant_purchase_order_vendor_returns.d.ts +3 -3
  203. package/lib/schemas/models/warehouse_internal_movements.d.ts +6 -6
  204. package/lib/schemas/models/warehouse_purchase_order_receipts.d.ts +3 -3
  205. package/lib/schemas/models/warehouse_purchase_order_vendor_returns.d.ts +3 -3
  206. package/lib/schemas/models/warehouse_sale_deliveries.d.ts +6 -6
  207. package/lib/schemas/models/warehouse_sale_invoices.d.ts +3 -3
  208. package/lib/schemas/types/deliveries.d.ts +40 -1
  209. package/lib/schemas/types/index.d.ts +15 -0
  210. package/lib/schemas/types/index.js +15 -0
  211. package/lib/schemas/types/materialized_rider_daily_reports.d.ts +37 -0
  212. package/lib/schemas/types/materialized_rider_daily_reports.js +2 -0
  213. package/lib/schemas/types/materialized_tenant_cash_entries.d.ts +87 -0
  214. package/lib/schemas/types/materialized_tenant_cash_entries.js +2 -0
  215. package/lib/schemas/types/materialized_tenant_cash_reports.d.ts +144 -0
  216. package/lib/schemas/types/materialized_tenant_cash_reports.js +2 -0
  217. package/lib/schemas/types/materialized_tenant_delivery_entries.d.ts +98 -0
  218. package/lib/schemas/types/materialized_tenant_delivery_entries.js +2 -0
  219. package/lib/schemas/types/materialized_tenant_delivery_reports.d.ts +179 -0
  220. package/lib/schemas/types/materialized_tenant_delivery_reports.js +2 -0
  221. package/lib/schemas/types/materialized_tenant_discount_entries.d.ts +81 -0
  222. package/lib/schemas/types/materialized_tenant_discount_entries.js +2 -0
  223. package/lib/schemas/types/materialized_tenant_discount_reports.d.ts +209 -0
  224. package/lib/schemas/types/materialized_tenant_discount_reports.js +3 -0
  225. package/lib/schemas/types/materialized_tenant_hourly_comparison_entries.d.ts +33 -0
  226. package/lib/schemas/types/materialized_tenant_hourly_comparison_entries.js +2 -0
  227. package/lib/schemas/types/materialized_tenant_hourly_comparison_reports.d.ts +40 -0
  228. package/lib/schemas/types/materialized_tenant_hourly_comparison_reports.js +2 -0
  229. package/lib/schemas/types/materialized_tenant_payments_comparison_entries.d.ts +48 -0
  230. package/lib/schemas/types/materialized_tenant_payments_comparison_entries.js +2 -0
  231. package/lib/schemas/types/materialized_tenant_payments_comparison_reports.d.ts +51 -0
  232. package/lib/schemas/types/materialized_tenant_payments_comparison_reports.js +14 -0
  233. package/lib/schemas/types/materialized_tenant_prep_time_entries.d.ts +88 -0
  234. package/lib/schemas/types/materialized_tenant_prep_time_entries.js +2 -0
  235. package/lib/schemas/types/materialized_tenant_prep_time_reports.d.ts +132 -0
  236. package/lib/schemas/types/materialized_tenant_prep_time_reports.js +2 -0
  237. package/lib/schemas/types/materialized_tenant_sales_comparison_entries.d.ts +43 -0
  238. package/lib/schemas/types/materialized_tenant_sales_comparison_entries.js +2 -0
  239. package/lib/schemas/types/materialized_tenant_sales_comparison_reports.d.ts +74 -0
  240. package/lib/schemas/types/materialized_tenant_sales_comparison_reports.js +17 -0
  241. package/lib/tests/materialized-reports/analytics-growth-helpers.test.d.ts +1 -0
  242. package/lib/tests/materialized-reports/analytics-overview-helpers.test.d.ts +1 -0
  243. package/lib/tests/materialized-reports/analytics-performance-helpers.test.d.ts +1 -0
  244. package/lib/tests/materialized-reports/fixtures.d.ts +15 -1
  245. package/lib/tests/materialized-reports/fixtures.js +1 -0
  246. package/lib/tests/materialized-reports/materialized-cash-compile.test.d.ts +1 -0
  247. package/lib/tests/materialized-reports/materialized-cash-compile.test.js +196 -0
  248. package/lib/tests/materialized-reports/materialized-cash-fold.test.d.ts +1 -0
  249. package/lib/tests/materialized-reports/materialized-cash-fold.test.js +116 -0
  250. package/lib/tests/materialized-reports/materialized-cash-formatter.test.d.ts +1 -0
  251. package/lib/tests/materialized-reports/materialized-cash-formatter.test.js +152 -0
  252. package/lib/tests/materialized-reports/materialized-cash-plan.test.d.ts +1 -0
  253. package/lib/tests/materialized-reports/materialized-cash-plan.test.js +168 -0
  254. package/lib/tests/materialized-reports/materialized-cash-reconciliation.test.d.ts +1 -0
  255. package/lib/tests/materialized-reports/materialized-cash-reconciliation.test.js +267 -0
  256. package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.d.ts +1 -0
  257. package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.js +147 -0
  258. package/lib/tests/materialized-reports/materialized-delivery-compile.test.d.ts +1 -0
  259. package/lib/tests/materialized-reports/materialized-delivery-compile.test.js +241 -0
  260. package/lib/tests/materialized-reports/materialized-delivery-fold.test.d.ts +1 -0
  261. package/lib/tests/materialized-reports/materialized-delivery-fold.test.js +109 -0
  262. package/lib/tests/materialized-reports/materialized-delivery-formatter.test.d.ts +1 -0
  263. package/lib/tests/materialized-reports/materialized-delivery-formatter.test.js +141 -0
  264. package/lib/tests/materialized-reports/materialized-delivery-plan.test.d.ts +1 -0
  265. package/lib/tests/materialized-reports/materialized-delivery-plan.test.js +198 -0
  266. package/lib/tests/materialized-reports/materialized-discount-aggregated-formatter.test.d.ts +1 -0
  267. package/lib/tests/materialized-reports/materialized-discount-aggregated-formatter.test.js +173 -0
  268. package/lib/tests/materialized-reports/materialized-discount-classification.test.d.ts +1 -0
  269. package/lib/tests/materialized-reports/materialized-discount-classification.test.js +339 -0
  270. package/lib/tests/materialized-reports/materialized-discount-close-day.test.d.ts +1 -0
  271. package/lib/tests/materialized-reports/materialized-discount-close-day.test.js +506 -0
  272. package/lib/tests/materialized-reports/materialized-discount-compile.test.d.ts +1 -0
  273. package/lib/tests/materialized-reports/materialized-discount-compile.test.js +161 -0
  274. package/lib/tests/materialized-reports/materialized-discount-counter-rows.test.d.ts +1 -0
  275. package/lib/tests/materialized-reports/materialized-discount-counter-rows.test.js +107 -0
  276. package/lib/tests/materialized-reports/materialized-discount-entries-accumulation.test.d.ts +1 -0
  277. package/lib/tests/materialized-reports/materialized-discount-entries-accumulation.test.js +125 -0
  278. package/lib/tests/materialized-reports/materialized-hourly-comparison-compile.test.d.ts +1 -0
  279. package/lib/tests/materialized-reports/materialized-hourly-comparison-compile.test.js +146 -0
  280. package/lib/tests/materialized-reports/materialized-hourly-comparison-formatter.test.d.ts +1 -0
  281. package/lib/tests/materialized-reports/materialized-hourly-comparison-formatter.test.js +127 -0
  282. package/lib/tests/materialized-reports/materialized-hourly-comparison-plan.test.d.ts +1 -0
  283. package/lib/tests/materialized-reports/materialized-hourly-comparison-plan.test.js +138 -0
  284. package/lib/tests/materialized-reports/materialized-hourly-comparison-selection.test.d.ts +1 -0
  285. package/lib/tests/materialized-reports/materialized-hourly-comparison-selection.test.js +90 -0
  286. package/lib/tests/materialized-reports/materialized-payments-comparison-compile.test.d.ts +1 -0
  287. package/lib/tests/materialized-reports/materialized-payments-comparison-compile.test.js +164 -0
  288. package/lib/tests/materialized-reports/materialized-payments-comparison-formatter.test.d.ts +1 -0
  289. package/lib/tests/materialized-reports/materialized-payments-comparison-formatter.test.js +117 -0
  290. package/lib/tests/materialized-reports/materialized-payments-comparison-plan.test.d.ts +1 -0
  291. package/lib/tests/materialized-reports/materialized-payments-comparison-plan.test.js +149 -0
  292. package/lib/tests/materialized-reports/materialized-prep-time-classification.test.d.ts +1 -0
  293. package/lib/tests/materialized-reports/materialized-prep-time-classification.test.js +273 -0
  294. package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.d.ts +1 -0
  295. package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.js +270 -0
  296. package/lib/tests/materialized-reports/materialized-prep-time-compile.test.d.ts +1 -0
  297. package/lib/tests/materialized-reports/materialized-prep-time-compile.test.js +217 -0
  298. package/lib/tests/materialized-reports/materialized-prep-time-constants.test.d.ts +1 -0
  299. package/lib/tests/materialized-reports/materialized-prep-time-constants.test.js +84 -0
  300. package/lib/tests/materialized-reports/materialized-prep-time-plan.test.d.ts +1 -0
  301. package/lib/tests/materialized-reports/materialized-prep-time-plan.test.js +176 -0
  302. package/lib/tests/materialized-reports/materialized-sales-comparison-baseline.test.d.ts +1 -0
  303. package/lib/tests/materialized-reports/materialized-sales-comparison-baseline.test.js +126 -0
  304. package/lib/tests/materialized-reports/materialized-sales-comparison-compile.test.d.ts +1 -0
  305. package/lib/tests/materialized-reports/materialized-sales-comparison-compile.test.js +206 -0
  306. package/lib/tests/materialized-reports/materialized-sales-comparison-formatter.test.d.ts +1 -0
  307. package/lib/tests/materialized-reports/materialized-sales-comparison-formatter.test.js +179 -0
  308. package/lib/tests/materialized-reports/materialized-sales-comparison-parity.test.d.ts +1 -0
  309. package/lib/tests/materialized-reports/materialized-sales-comparison-parity.test.js +196 -0
  310. package/lib/tests/materialized-reports/materialized-sales-comparison-plan.test.d.ts +1 -0
  311. package/lib/tests/materialized-reports/materialized-sales-comparison-plan.test.js +241 -0
  312. package/lib/tests/materialized-reports/materialized-tenant-hourly-comparison-schema.test.d.ts +1 -0
  313. package/lib/tests/materialized-reports/materialized-tenant-hourly-comparison-schema.test.js +96 -0
  314. package/lib/tests/materialized-reports/materialized-tenant-payments-comparison-schema.test.d.ts +1 -0
  315. package/lib/tests/materialized-reports/materialized-tenant-payments-comparison-schema.test.js +112 -0
  316. package/lib/tests/materialized-reports/materialized-tenant-sales-comparison-schema.test.d.ts +1 -0
  317. package/lib/tests/materialized-reports/materialized-tenant-sales-comparison-schema.test.js +142 -0
  318. package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +8 -1
  319. package/lib/tests/materialized-reports/sales-comparison-reference.d.ts +55 -0
  320. package/lib/tests/materialized-reports/sales-comparison-reference.js +86 -0
  321. package/lib/tests/materialized-reports/tenant-discount-formatter.test.d.ts +1 -0
  322. package/lib/tests/materialized-reports/tenant-discount-formatter.test.js +289 -0
  323. package/lib/tests/materialized-reports/tenant-prep-time-formatter.test.d.ts +1 -0
  324. package/lib/tests/materialized-reports/tenant-prep-time-formatter.test.js +354 -0
  325. package/lib/tests/reporting-contract.test.d.ts +1 -0
  326. package/lib/tests/reporting-contract.test.js +267 -0
  327. package/lib/tests/rider-daily-report.test.d.ts +1 -0
  328. package/lib/tests/rider-daily-report.test.js +86 -0
  329. package/lib/tests/rider-earnings.test.d.ts +1 -0
  330. package/lib/tests/rider-earnings.test.js +65 -0
  331. package/lib/tests/rider-report.test.d.ts +1 -0
  332. package/lib/tests/rider-report.test.js +98 -0
  333. package/package.json +10 -1
@@ -0,0 +1,318 @@
1
+ import { MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM, MATERIALIZED_DISCOUNT_LEVEL_ENUM, MATERIALIZED_DISCOUNT_SLOT_ENUM, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
2
+ import { createEmptyDiscountOrderCounters, createEmptyDiscountSlotCounter, MATERIALIZED_DISCOUNT_SLOTS, mergeDiscountSlotCounter, readDiscountSlot, resolveRowMoney, } from './materialized-discount-classification-helpers';
3
+ const isComplimentaryPayment = (payment) => payment?.payment_type === 'COMPLIMENTARY';
4
+ const nonNegative = (value) => (Number.isFinite(value) && value > 0 ? value : 0);
5
+ /** The line label of a punch hour (null when the hour is unknown). */
6
+ export const toDiscountHourLabel = (localDate, hour) => hour == null || hour < 0 || hour > 23 ? null : `${localDate} ${String(hour).padStart(2, '0')}`;
7
+ /** Base row fields shared by every discount entry. */
8
+ function entryBase(order, context, kind) {
9
+ return {
10
+ kind,
11
+ order_id: String(order._id),
12
+ order_number: order.order_number ?? null,
13
+ item_id: null,
14
+ item_name: null,
15
+ payment_id: null,
16
+ rate: null,
17
+ is_percentage: null,
18
+ item_value: null,
19
+ discount_id: null,
20
+ name: null,
21
+ reference_number: null,
22
+ reason: null,
23
+ staff_id: order.staff_details?.staff_id ?? null,
24
+ staff_name: order.staff_details?.staff_name ?? null,
25
+ order_taker_id: order.order_taker_details?.staff_id ?? null,
26
+ order_taker_name: order.order_taker_details?.staff_name ?? null,
27
+ order_status: String(order.order_status ?? ''),
28
+ platform: String(order.platform ?? ''),
29
+ created_at: new Date(order.createdAt ?? context.local_date),
30
+ local_hour: context.local_hour_label,
31
+ };
32
+ }
33
+ /** Adds one ORDER-LEVEL slot reading into its counters + rows. */
34
+ function accumulateOrderSlot(counters, entries, order, context, reading, resolved) {
35
+ const raw = nonNegative(reading.rate);
36
+ const money = nonNegative(resolved);
37
+ const slotCounter = reading.slot === MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL
38
+ ? counters.order_special
39
+ : reading.slot === MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY
40
+ ? counters.order_loyalty
41
+ : counters.order_voucher;
42
+ slotCounter.count += 1;
43
+ slotCounter.amount += raw;
44
+ slotCounter.resolved_amount += money;
45
+ const subCounter = orderSubKindCounter(counters, reading.slot, reading.sub_kind);
46
+ subCounter.count += 1;
47
+ subCounter.amount += raw;
48
+ subCounter.resolved_amount += money;
49
+ entries.push({
50
+ ...entryBase(order, context, MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT),
51
+ slot: reading.slot,
52
+ sub_kind: reading.sub_kind,
53
+ level: MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER,
54
+ entry_key: `order:${reading.slot.toLowerCase()}`,
55
+ amount: money,
56
+ rate: raw,
57
+ is_percentage: reading.is_percentage,
58
+ discount_id: reading.discount_id,
59
+ name: reading.name,
60
+ reference_number: reading.reference_number,
61
+ reason: reading.reason,
62
+ });
63
+ }
64
+ /** Maps an order-level sub-kind to its dedicated counter. */
65
+ function orderSubKindCounter(counters, slot, subKind) {
66
+ if (slot === MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL) {
67
+ switch (subKind) {
68
+ case MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.COLLECTION:
69
+ return counters.special_collection;
70
+ case MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.MANUAL:
71
+ return counters.special_manual;
72
+ case MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.PRICE_OVERRIDE:
73
+ return counters.special_price_override;
74
+ case MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM:
75
+ return counters.special_catalog_item;
76
+ default:
77
+ return counters.special_other;
78
+ }
79
+ }
80
+ if (slot === MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY) {
81
+ switch (subKind) {
82
+ case MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER:
83
+ return counters.loyalty_offer;
84
+ case MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.GIFT_CARD:
85
+ return counters.loyalty_gift_card;
86
+ case MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.WALLET_CASHBACK:
87
+ return counters.loyalty_wallet;
88
+ default:
89
+ return counters.loyalty_other;
90
+ }
91
+ }
92
+ return subKind === MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER ? counters.voucher_offer : counters.voucher_other;
93
+ }
94
+ /** Adds one ITEM-LEVEL slot reading into its counters + rows. */
95
+ function accumulateItemSlot(counters, entries, order, context, itemId, itemName, itemValue, reading, resolved, itemTotal) {
96
+ const raw = nonNegative(reading.rate);
97
+ const money = nonNegative(resolved);
98
+ const isFree = reading.is_percentage && reading.rate >= 100;
99
+ if (reading.slot === MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL) {
100
+ const counter = counters.item_special;
101
+ counter.count += 1;
102
+ counter.amount += raw;
103
+ counter.resolved_amount += money;
104
+ switch (reading.sub_kind) {
105
+ case MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.COLLECTION:
106
+ counter.collection_count += 1;
107
+ counter.collection_amount += raw;
108
+ counter.collection_resolved_amount += money;
109
+ break;
110
+ case MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.MANUAL:
111
+ counter.manual_count += 1;
112
+ counter.manual_amount += raw;
113
+ counter.manual_resolved_amount += money;
114
+ break;
115
+ case MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.PRICE_OVERRIDE:
116
+ counter.price_override_count += 1;
117
+ counter.price_override_amount += raw;
118
+ counter.price_override_resolved_amount += money;
119
+ break;
120
+ case MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM:
121
+ counter.catalog_item_count += 1;
122
+ counter.catalog_item_amount += raw;
123
+ counter.catalog_item_resolved_amount += money;
124
+ break;
125
+ default:
126
+ counter.other_count += 1;
127
+ counter.other_amount += raw;
128
+ counter.other_resolved_amount += money;
129
+ break;
130
+ }
131
+ }
132
+ else if (reading.slot === MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY) {
133
+ const counter = counters.item_loyalty;
134
+ counter.count += 1;
135
+ counter.amount += raw;
136
+ counter.resolved_amount += money;
137
+ switch (reading.sub_kind) {
138
+ case MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER:
139
+ counter.offer_count += 1;
140
+ counter.offer_amount += raw;
141
+ counter.offer_resolved_amount += money;
142
+ break;
143
+ case MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.GIFT_CARD:
144
+ counter.gift_card_count += 1;
145
+ counter.gift_card_amount += raw;
146
+ counter.gift_card_resolved_amount += money;
147
+ break;
148
+ case MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.WALLET_CASHBACK:
149
+ counter.wallet_count += 1;
150
+ counter.wallet_amount += raw;
151
+ counter.wallet_resolved_amount += money;
152
+ break;
153
+ default:
154
+ counter.other_count += 1;
155
+ counter.other_amount += raw;
156
+ counter.other_resolved_amount += money;
157
+ break;
158
+ }
159
+ }
160
+ else {
161
+ const counter = counters.item_voucher;
162
+ counter.count += 1;
163
+ counter.amount += raw;
164
+ counter.resolved_amount += money;
165
+ if (reading.reference_number != null)
166
+ counter.ref_count += 1;
167
+ if (reading.sub_kind === MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER) {
168
+ counter.offer_count += 1;
169
+ counter.offer_amount += raw;
170
+ counter.offer_resolved_amount += money;
171
+ }
172
+ }
173
+ if (isFree) {
174
+ counters.free_items.count += 1;
175
+ counters.free_items.amount += raw;
176
+ counters.free_items.resolved_amount += money;
177
+ counters.free_items.value += nonNegative(itemValue);
178
+ }
179
+ entries.push({
180
+ ...entryBase(order, context, isFree ? MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.FREE_ITEM : MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT),
181
+ slot: reading.slot,
182
+ sub_kind: reading.sub_kind,
183
+ level: MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
184
+ entry_key: `item:${itemId}:${reading.slot.toLowerCase()}`,
185
+ amount: money,
186
+ rate: raw,
187
+ is_percentage: reading.is_percentage,
188
+ item_id: itemId,
189
+ item_name: itemName,
190
+ item_value: isFree ? nonNegative(itemValue) : null,
191
+ discount_id: reading.discount_id,
192
+ name: reading.name,
193
+ reference_number: reading.reference_number,
194
+ reason: reading.reason,
195
+ });
196
+ }
197
+ /**
198
+ * Folds ONE order into counters + rows. Returns zeroed counters and no rows for
199
+ * an order with no discounts (the caller decides whether to keep the key).
200
+ */
201
+ export function accumulateOrderDiscounts(order, context) {
202
+ const counters = createEmptyDiscountOrderCounters();
203
+ const entries = [];
204
+ if (order == null)
205
+ return { counters, entries, has_discount: false };
206
+ const orderDetails = order.order_discount_details ?? null;
207
+ const applied = nonNegative(Number(order.total_discount_applied ?? 0));
208
+ const storedOrderTotal = nonNegative(orderDetails?.total_discount != null ? Number(orderDetails.total_discount) : 0);
209
+ // ---- order level -------------------------------------------------------
210
+ const orderMoney = resolveRowMoney(orderDetails, storedOrderTotal);
211
+ // Slot PRESENCE = a positive rate OR an id. Zero-rate slots that carry an id
212
+ // do occur live, and they are real (zero-money) facts the drill-down shows —
213
+ // but the COUNTERS only add money, so an empty slot can never inflate a count
214
+ // or an amount.
215
+ // A slot COUNTS only when it applied money. Live data holds zero-money slots
216
+ // that still carry an id (a discount that was attempted then cleared — the
217
+ // writer leaves `*_discount: 0` with the id behind), and counting those would
218
+ // inflate every occurrence counter while adding nothing to the money; it would
219
+ // also break the "resolved slots explain the row total" invariant. They are
220
+ // therefore excluded here (the row itself is not a discount row).
221
+ const orderReadings = MATERIALIZED_DISCOUNT_SLOTS.map((slot) => readDiscountSlot(orderDetails, slot))
222
+ .filter((reading) => reading != null && reading.rate >= 0)
223
+ .map((reading) => ({ reading, money: nonNegative(orderMoney.perSlot.get(reading.slot) ?? 0) }))
224
+ .filter((entry) => entry.money > 0);
225
+ for (const { reading, money } of orderReadings) {
226
+ accumulateOrderSlot(counters, entries, order, context, reading, money);
227
+ }
228
+ if (storedOrderTotal > 0) {
229
+ counters.order_level_amount += storedOrderTotal;
230
+ // Money the slots cannot explain (no slots at all, several active slots, or
231
+ // a slot value the row total contradicts) is a tripwire: counted, never
232
+ // silently dropped.
233
+ counters.unattributed_amount += nonNegative(orderMoney.unallocated);
234
+ }
235
+ // ---- item level --------------------------------------------------------
236
+ let itemLevelTotal = 0;
237
+ for (const category of order.categories ?? []) {
238
+ for (const item of category.items ?? []) {
239
+ const itemDetails = item.item_discount_details ?? null;
240
+ const itemTotal = nonNegative(Number(itemDetails?.total_discount ?? 0));
241
+ if (itemTotal <= 0)
242
+ continue;
243
+ itemLevelTotal += itemTotal;
244
+ counters.item_discount_count += 1;
245
+ // The item's own value for a FREE_ITEM row: the net (post-discount) amount
246
+ // when the writer computed item metadata, else the list price. Never used
247
+ // for any counter total — display context only.
248
+ // Prefer the net (post-discount) amount, then the gross, then the list
249
+ // price — a fully discounted item has net 0, so a plain ?? chain would
250
+ // report its value as 0.
251
+ const netValue = nonNegative(Number(item._net_amount ?? 0));
252
+ const itemValue = netValue > 0 ? netValue : nonNegative(Number(item._gross_amount ?? item.price ?? 0));
253
+ const itemMoney = resolveRowMoney(itemDetails, itemTotal);
254
+ const itemReadings = MATERIALIZED_DISCOUNT_SLOTS.map((slot) => readDiscountSlot(itemDetails, slot))
255
+ .filter((reading) => reading != null && reading.rate >= 0)
256
+ .map((reading) => ({ reading, money: nonNegative(itemMoney.perSlot.get(reading.slot) ?? 0) }))
257
+ .filter((entry) => entry.money > 0);
258
+ if (itemReadings.length === 0) {
259
+ // Item money with no usable slot: keep it in the item total (so the
260
+ // headline reconciles) and record one unattributed row for the
261
+ // drill-down to show.
262
+ entries.push({
263
+ ...entryBase(order, context, MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT),
264
+ slot: null,
265
+ sub_kind: MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED,
266
+ level: MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
267
+ entry_key: `item:${String(item._id)}:unattributed`,
268
+ amount: itemTotal,
269
+ item_id: String(item._id),
270
+ item_name: item.name ?? null,
271
+ });
272
+ continue;
273
+ }
274
+ for (const { reading, money } of itemReadings) {
275
+ accumulateItemSlot(counters, entries, order, context, String(item._id), item.name ?? null, itemValue, reading, money, itemTotal);
276
+ }
277
+ // Multi-slot / fixed-slot leftovers stay visible on the order-level
278
+ // tripwire so anchors E/H keep holding.
279
+ counters.unattributed_amount += nonNegative(itemMoney.unallocated);
280
+ }
281
+ }
282
+ counters.item_level_amount += itemLevelTotal;
283
+ // ---- headline + comp ---------------------------------------------------
284
+ counters.amount += applied;
285
+ let compAmount = 0;
286
+ for (const payment of order.payments ?? []) {
287
+ if (!isComplimentaryPayment(payment))
288
+ continue;
289
+ counters.complimentary.payment_count += 1;
290
+ if (payment.note)
291
+ counters.complimentary.note_count += 1;
292
+ entries.push({
293
+ ...entryBase(order, context, MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY),
294
+ slot: null,
295
+ sub_kind: MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED,
296
+ level: MATERIALIZED_DISCOUNT_LEVEL_ENUM.PAYMENT,
297
+ entry_key: `payment:${String(payment._id)}`,
298
+ amount: 0,
299
+ payment_id: payment._id != null ? String(payment._id) : null,
300
+ });
301
+ }
302
+ if (counters.complimentary.payment_count > 0) {
303
+ compAmount = nonNegative(Number(order.amount_earned ?? 0));
304
+ counters.complimentary.amount += compAmount;
305
+ }
306
+ // ONE count per order with any discount or comp payment — incremented exactly
307
+ // once, after every source has been folded (a double increment here would
308
+ // silently inflate the "orders with a discount" KPI).
309
+ const hasDiscount = counters.order_level_amount > 0 || counters.item_level_amount > 0 || counters.complimentary.payment_count > 0;
310
+ if (hasDiscount)
311
+ counters.count = 1;
312
+ return { counters, entries, has_discount: hasDiscount };
313
+ }
314
+ /** Folds a slot counter map into a target (used by tests + the key fold). */
315
+ export function mergeDiscountSlotCounters(target, source) {
316
+ mergeDiscountSlotCounter(target, source);
317
+ }
318
+ export { createEmptyDiscountSlotCounter };
@@ -0,0 +1,213 @@
1
+ import { WALLET_CASHBACK_LOYALTY_DISCOUNT_ID } from '../../constants/COMMON';
2
+ import { MATERIALIZED_DISCOUNT_MANUAL_PREFIX, MATERIALIZED_DISCOUNT_PRICE_OVERRIDE_PREFIX, MATERIALIZED_DISCOUNT_SLOT_ENUM, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
3
+ /**
4
+ * The discounts-report CLASSIFIER (M1) — pure functions that turn one order's
5
+ * discount slots into the counter contract of
6
+ * `materialized_tenant_discount_reports`.
7
+ *
8
+ * ## The two rules that define correctness
9
+ *
10
+ * 1. **Never sum raw slots as money.** A slot holds the ORIGINAL value: for a
11
+ * percentage discount it is the PERCENT (`special_discount: 25`), while the
12
+ * money actually applied sits in the row's `total_discount`. So every
13
+ * classified slot yields BOTH `amount` (raw — the only value comparable to
14
+ * the sales overview's `discount_by_type`) and `resolved_amount` (money).
15
+ * 2. **Classify by the ID SHAPE.** The write path stores no kind discriminator;
16
+ * the id is it: a 24-hex ObjectId is a `discounts` collection document,
17
+ * `MANUAL_SINGLE_` is a price override, `MANUAL_` is a quick/custom/
18
+ * payment-type discount, and the loyalty/voucher slots reference offers, gift
19
+ * cards or the wallet sentinel.
20
+ *
21
+ * Everything here is pure and total: no throws, no clock, no I/O. Invalid input
22
+ * degrades to `UNATTRIBUTED` / 0 so a report can never be broken by one odd
23
+ * order.
24
+ */
25
+ const HEX24 = /^[0-9a-f]{24}$/i;
26
+ /** `'SPECIAL_DISCOUNT'` — the catalog item-discount sentinel (pos-web). */
27
+ export const MATERIALIZED_DISCOUNT_CATALOG_ITEM_ID = 'SPECIAL_DISCOUNT';
28
+ export const MATERIALIZED_DISCOUNT_SLOTS = [
29
+ MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL,
30
+ MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY,
31
+ MATERIALIZED_DISCOUNT_SLOT_ENUM.VOUCHER,
32
+ ];
33
+ /** Field-name prefix of a slot in `IDiscountDetails` (`special` → `special_discount`). */
34
+ const slotFieldKey = (slot) => slot.toLowerCase();
35
+ /** Reads ONE slot of an `IDiscountDetails` — null when the slot is empty. */
36
+ export function readDiscountSlot(details, slot) {
37
+ if (details == null)
38
+ return null;
39
+ const key = slotFieldKey(slot);
40
+ const rate = Number(details[`${key}_discount`] ?? 0);
41
+ const idValue = details[`${key}_discount_id`];
42
+ const discount_id = typeof idValue === 'string' && idValue.length > 0 ? idValue : null;
43
+ // A slot counts as present when it carries money/percent OR an id. Zero-value
44
+ // slots with ids DO occur live (e.g. `is_loyalty_discount_percentage: true`
45
+ // with only `total_discount`) — they are read but contribute 0.
46
+ if (rate === 0 && discount_id == null)
47
+ return null;
48
+ return {
49
+ slot,
50
+ sub_kind: classifyDiscountSubKind(slot, discount_id),
51
+ rate: Number.isFinite(rate) ? rate : 0,
52
+ is_percentage: details[`is_${key}_discount_percentage`] === true,
53
+ discount_id,
54
+ name: details[`${key}_discount_name`] ?? null,
55
+ reference_number: details[`${key}_reference_number`] ?? null,
56
+ reason: details[`${key}_discount_reason`] ?? null,
57
+ };
58
+ }
59
+ /**
60
+ * Classifies a slot's sub-kind from its id shape (and, for the special slot,
61
+ * the id's namespace).
62
+ */
63
+ export function classifyDiscountSubKind(slot, discountId) {
64
+ if (discountId == null)
65
+ return MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED;
66
+ if (slot === MATERIALIZED_DISCOUNT_SLOT_ENUM.VOUCHER) {
67
+ return discountId === WALLET_CASHBACK_LOYALTY_DISCOUNT_ID
68
+ ? MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.WALLET_CASHBACK
69
+ : MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER;
70
+ }
71
+ if (slot === MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY) {
72
+ if (discountId === WALLET_CASHBACK_LOYALTY_DISCOUNT_ID) {
73
+ return MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.WALLET_CASHBACK;
74
+ }
75
+ // Offers and gift cards both use ObjectIds; the reference number is what
76
+ // tells them apart (`buildGiftCardDiscountUpdate` writes the card code).
77
+ return HEX24.test(discountId)
78
+ ? MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER
79
+ : MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.GIFT_CARD;
80
+ }
81
+ // SPECIAL slot — the namespace prefix is the discriminator.
82
+ if (discountId === MATERIALIZED_DISCOUNT_CATALOG_ITEM_ID) {
83
+ return MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM;
84
+ }
85
+ if (discountId.startsWith(MATERIALIZED_DISCOUNT_PRICE_OVERRIDE_PREFIX)) {
86
+ return MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.PRICE_OVERRIDE;
87
+ }
88
+ if (discountId.startsWith(MATERIALIZED_DISCOUNT_MANUAL_PREFIX)) {
89
+ return MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.MANUAL;
90
+ }
91
+ if (HEX24.test(discountId))
92
+ return MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.COLLECTION;
93
+ return MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM;
94
+ }
95
+ export function resolveRowMoney(details, rowTotal) {
96
+ const perSlot = new Map();
97
+ const total = Number.isFinite(rowTotal) ? rowTotal : 0;
98
+ if (details == null || total <= 0) {
99
+ return { perSlot, row_total: Math.max(0, total), allocated: 0, unallocated: Math.max(0, total) };
100
+ }
101
+ const readings = MATERIALIZED_DISCOUNT_SLOTS.map((slot) => readDiscountSlot(details, slot)).filter((reading) => reading != null && reading.rate > 0);
102
+ if (readings.length === 0) {
103
+ return { perSlot, row_total: total, allocated: 0, unallocated: total };
104
+ }
105
+ if (readings.length === 1) {
106
+ // Single active slot: percent rows resolve to the row total, fixed rows to
107
+ // their own value (clamped to the row total so a stale slot cannot
108
+ // over-report money the row never applied).
109
+ const [only] = readings;
110
+ const resolved = only.is_percentage ? total : Math.min(only.rate, total);
111
+ perSlot.set(only.slot, resolved);
112
+ return { perSlot, row_total: total, allocated: resolved, unallocated: Math.max(0, total - resolved) };
113
+ }
114
+ // Several active slots (allocated offers): every slot is a FIXED amount when
115
+ // more than one is active in practice, so assign each its own value and let
116
+ // the remainder be unattributed rather than guessing a split.
117
+ let allocated = 0;
118
+ for (const reading of readings) {
119
+ if (reading.is_percentage)
120
+ continue; // a % mixed with other slots is unsplittable
121
+ const value = Math.max(0, reading.rate);
122
+ perSlot.set(reading.slot, value);
123
+ allocated += value;
124
+ }
125
+ return {
126
+ perSlot,
127
+ row_total: total,
128
+ allocated,
129
+ unallocated: Math.max(0, total - allocated),
130
+ };
131
+ }
132
+ /** Empty slot counter. */
133
+ export const createEmptyDiscountSlotCounter = () => ({
134
+ count: 0,
135
+ amount: 0,
136
+ resolved_amount: 0,
137
+ });
138
+ const createEmptyItemSpecial = () => ({
139
+ ...createEmptyDiscountSlotCounter(),
140
+ collection_count: 0,
141
+ collection_amount: 0,
142
+ collection_resolved_amount: 0,
143
+ manual_count: 0,
144
+ manual_amount: 0,
145
+ manual_resolved_amount: 0,
146
+ price_override_count: 0,
147
+ price_override_amount: 0,
148
+ price_override_resolved_amount: 0,
149
+ catalog_item_count: 0,
150
+ catalog_item_amount: 0,
151
+ catalog_item_resolved_amount: 0,
152
+ other_count: 0,
153
+ other_amount: 0,
154
+ other_resolved_amount: 0,
155
+ });
156
+ const createEmptyItemLoyalty = () => ({
157
+ ...createEmptyDiscountSlotCounter(),
158
+ offer_count: 0,
159
+ offer_amount: 0,
160
+ offer_resolved_amount: 0,
161
+ gift_card_count: 0,
162
+ gift_card_amount: 0,
163
+ gift_card_resolved_amount: 0,
164
+ wallet_count: 0,
165
+ wallet_amount: 0,
166
+ wallet_resolved_amount: 0,
167
+ other_count: 0,
168
+ other_amount: 0,
169
+ other_resolved_amount: 0,
170
+ });
171
+ const createEmptyItemVoucher = () => ({
172
+ ...createEmptyDiscountSlotCounter(),
173
+ ref_count: 0,
174
+ offer_count: 0,
175
+ offer_amount: 0,
176
+ offer_resolved_amount: 0,
177
+ });
178
+ /** Empty counter block for one key — every field present and zeroed. */
179
+ export const createEmptyDiscountOrderCounters = () => ({
180
+ count: 0,
181
+ amount: 0,
182
+ order_level_amount: 0,
183
+ item_level_amount: 0,
184
+ item_discount_count: 0,
185
+ unattributed_amount: 0,
186
+ order_special: createEmptyDiscountSlotCounter(),
187
+ order_loyalty: createEmptyDiscountSlotCounter(),
188
+ order_voucher: createEmptyDiscountSlotCounter(),
189
+ special_collection: createEmptyDiscountSlotCounter(),
190
+ special_manual: createEmptyDiscountSlotCounter(),
191
+ special_price_override: createEmptyDiscountSlotCounter(),
192
+ special_catalog_item: createEmptyDiscountSlotCounter(),
193
+ special_other: createEmptyDiscountSlotCounter(),
194
+ loyalty_offer: createEmptyDiscountSlotCounter(),
195
+ loyalty_gift_card: createEmptyDiscountSlotCounter(),
196
+ loyalty_wallet: createEmptyDiscountSlotCounter(),
197
+ loyalty_other: createEmptyDiscountSlotCounter(),
198
+ voucher_offer: createEmptyDiscountSlotCounter(),
199
+ voucher_other: createEmptyDiscountSlotCounter(),
200
+ item_special: createEmptyItemSpecial(),
201
+ item_loyalty: createEmptyItemLoyalty(),
202
+ item_voucher: createEmptyItemVoucher(),
203
+ free_items: { count: 0, amount: 0, resolved_amount: 0, value: 0 },
204
+ complimentary: { payment_count: 0, amount: 0, note_count: 0 },
205
+ });
206
+ /** Adds `source` counts/amounts into `target` (used by the key fold + tests). */
207
+ export function mergeDiscountSlotCounter(target, source) {
208
+ if (source == null)
209
+ return;
210
+ target.count += source.count ?? 0;
211
+ target.amount += source.amount ?? 0;
212
+ target.resolved_amount += source.resolved_amount ?? 0;
213
+ }