@managesome/eposmatic-toolkit 0.4.9 → 0.4.11

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 (351) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +455 -0
  2. package/esm/constants/enums/delivery.js +8 -0
  3. package/esm/helpers/index.js +3 -0
  4. package/esm/helpers/materialized-reports/index.js +32 -0
  5. package/esm/helpers/materialized-reports/materialized-cash-accumulator-helpers.js +114 -0
  6. package/esm/helpers/materialized-reports/materialized-cash-classification-helpers.js +248 -0
  7. package/esm/helpers/materialized-reports/materialized-cash-close-day-helpers.js +396 -0
  8. package/esm/helpers/materialized-reports/materialized-cash-compile-helpers.js +245 -0
  9. package/esm/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +170 -0
  10. package/esm/helpers/materialized-reports/materialized-delivery-classification-helpers.js +430 -0
  11. package/esm/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +373 -0
  12. package/esm/helpers/materialized-reports/materialized-delivery-compile-helpers.js +216 -0
  13. package/esm/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +319 -0
  14. package/esm/helpers/materialized-reports/materialized-discount-classification-helpers.js +213 -0
  15. package/esm/helpers/materialized-reports/materialized-discount-close-day-helpers.js +604 -0
  16. package/esm/helpers/materialized-reports/materialized-discount-compile-helpers.js +186 -0
  17. package/esm/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.js +155 -0
  18. package/esm/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.js +199 -0
  19. package/esm/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.js +115 -0
  20. package/esm/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +89 -0
  21. package/esm/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.js +394 -0
  22. package/esm/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.js +167 -0
  23. package/esm/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +215 -0
  24. package/esm/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +477 -0
  25. package/esm/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +201 -0
  26. package/esm/helpers/materialized-reports/materialized-reports-scope-group-helpers.js +9 -6
  27. package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +11 -4
  28. package/esm/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.js +189 -0
  29. package/esm/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.js +156 -0
  30. package/esm/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.js +588 -0
  31. package/esm/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.js +175 -0
  32. package/esm/helpers/materialized-reports/tenant-cash-in-out-formatter.js +157 -0
  33. package/esm/helpers/materialized-reports/tenant-delivery-formatter.js +230 -0
  34. package/esm/helpers/materialized-reports/tenant-discount-formatter.js +427 -0
  35. package/esm/helpers/materialized-reports/tenant-hourly-comparison-formatter.js +238 -0
  36. package/esm/helpers/materialized-reports/tenant-payments-comparison-formatter.js +282 -0
  37. package/esm/helpers/materialized-reports/tenant-prep-time-formatter.js +228 -0
  38. package/esm/helpers/materialized-reports/tenant-sales-comparison-formatter.js +346 -0
  39. package/esm/helpers/reporting-contract/index.js +2 -0
  40. package/esm/helpers/reporting-contract/overview-contract.js +123 -0
  41. package/esm/helpers/reporting-contract/report-contract.js +228 -0
  42. package/esm/helpers/rider-earnings/index.js +1 -0
  43. package/esm/helpers/rider-earnings/rider-earnings.js +53 -0
  44. package/esm/helpers/rider-reports/index.js +2 -0
  45. package/esm/helpers/rider-reports/rider-daily-report.js +90 -0
  46. package/esm/helpers/rider-reports/rider-report.js +67 -0
  47. package/esm/helpers/store/common-order-actions.js +3 -2
  48. package/esm/schemas/models/deliveries.js +82 -1
  49. package/esm/schemas/models/index.js +15 -0
  50. package/esm/schemas/models/materialized_credit_bills.js +1 -1
  51. package/esm/schemas/models/materialized_rider_daily_reports.js +39 -0
  52. package/esm/schemas/models/materialized_tenant_cash_entries.js +95 -0
  53. package/esm/schemas/models/materialized_tenant_cash_reports.js +77 -0
  54. package/esm/schemas/models/materialized_tenant_delivery_entries.js +63 -0
  55. package/esm/schemas/models/materialized_tenant_delivery_reports.js +63 -0
  56. package/esm/schemas/models/materialized_tenant_discount_entries.js +156 -0
  57. package/esm/schemas/models/materialized_tenant_discount_reports.js +333 -0
  58. package/esm/schemas/models/materialized_tenant_hourly_comparison_entries.js +87 -0
  59. package/esm/schemas/models/materialized_tenant_hourly_comparison_reports.js +99 -0
  60. package/esm/schemas/models/materialized_tenant_payments_comparison_entries.js +71 -0
  61. package/esm/schemas/models/materialized_tenant_payments_comparison_reports.js +76 -0
  62. package/esm/schemas/models/materialized_tenant_prep_time_entries.js +130 -0
  63. package/esm/schemas/models/materialized_tenant_prep_time_reports.js +109 -0
  64. package/esm/schemas/models/materialized_tenant_sales_comparison_entries.js +130 -0
  65. package/esm/schemas/models/materialized_tenant_sales_comparison_reports.js +154 -0
  66. package/esm/schemas/models/materialized_tenant_sales_overview_reports.js +1 -1
  67. package/esm/schemas/types/index.js +15 -0
  68. package/esm/schemas/types/materialized_rider_daily_reports.js +1 -0
  69. package/esm/schemas/types/materialized_tenant_cash_entries.js +1 -0
  70. package/esm/schemas/types/materialized_tenant_cash_reports.js +1 -0
  71. package/esm/schemas/types/materialized_tenant_delivery_entries.js +1 -0
  72. package/esm/schemas/types/materialized_tenant_delivery_reports.js +1 -0
  73. package/esm/schemas/types/materialized_tenant_discount_entries.js +1 -0
  74. package/esm/schemas/types/materialized_tenant_discount_reports.js +2 -0
  75. package/esm/schemas/types/materialized_tenant_hourly_comparison_entries.js +1 -0
  76. package/esm/schemas/types/materialized_tenant_hourly_comparison_reports.js +1 -0
  77. package/esm/schemas/types/materialized_tenant_payments_comparison_entries.js +1 -0
  78. package/esm/schemas/types/materialized_tenant_payments_comparison_reports.js +11 -0
  79. package/esm/schemas/types/materialized_tenant_prep_time_entries.js +1 -0
  80. package/esm/schemas/types/materialized_tenant_prep_time_reports.js +1 -0
  81. package/esm/schemas/types/materialized_tenant_sales_comparison_entries.js +1 -0
  82. package/esm/schemas/types/materialized_tenant_sales_comparison_reports.js +14 -0
  83. package/lib/constants/MATERIALIZED_REPORTS.d.ts +345 -2
  84. package/lib/constants/MATERIALIZED_REPORTS.js +457 -1
  85. package/lib/constants/enums/delivery.d.ts +7 -0
  86. package/lib/constants/enums/delivery.js +9 -1
  87. package/lib/helpers/index.d.ts +3 -0
  88. package/lib/helpers/index.js +3 -0
  89. package/lib/helpers/materialized-reports/index.d.ts +32 -0
  90. package/lib/helpers/materialized-reports/index.js +32 -0
  91. package/lib/helpers/materialized-reports/materialized-cash-accumulator-helpers.d.ts +18 -0
  92. package/lib/helpers/materialized-reports/materialized-cash-accumulator-helpers.js +118 -0
  93. package/lib/helpers/materialized-reports/materialized-cash-classification-helpers.d.ts +142 -0
  94. package/lib/helpers/materialized-reports/materialized-cash-classification-helpers.js +257 -0
  95. package/lib/helpers/materialized-reports/materialized-cash-close-day-helpers.d.ts +103 -0
  96. package/lib/helpers/materialized-reports/materialized-cash-close-day-helpers.js +409 -0
  97. package/lib/helpers/materialized-reports/materialized-cash-compile-helpers.d.ts +73 -0
  98. package/lib/helpers/materialized-reports/materialized-cash-compile-helpers.js +252 -0
  99. package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.d.ts +7 -0
  100. package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +173 -0
  101. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.d.ts +113 -0
  102. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.js +442 -0
  103. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.d.ts +98 -0
  104. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +386 -0
  105. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.d.ts +41 -0
  106. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.js +220 -0
  107. package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.d.ts +52 -0
  108. package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +325 -0
  109. package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.d.ts +50 -0
  110. package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.js +222 -0
  111. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.d.ts +182 -0
  112. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.js +615 -0
  113. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.d.ts +47 -0
  114. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.js +193 -0
  115. package/lib/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.d.ts +64 -0
  116. package/lib/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.js +163 -0
  117. package/lib/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.d.ts +61 -0
  118. package/lib/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.js +207 -0
  119. package/lib/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.d.ts +23 -0
  120. package/lib/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.js +120 -0
  121. package/lib/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.d.ts +25 -0
  122. package/lib/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +99 -0
  123. package/lib/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.d.ts +79 -0
  124. package/lib/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.js +407 -0
  125. package/lib/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.d.ts +35 -0
  126. package/lib/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.js +173 -0
  127. package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.d.ts +98 -0
  128. package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +224 -0
  129. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.d.ts +130 -0
  130. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +491 -0
  131. package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.d.ts +47 -0
  132. package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +208 -0
  133. package/lib/helpers/materialized-reports/materialized-reports-scope-group-helpers.d.ts +6 -3
  134. package/lib/helpers/materialized-reports/materialized-reports-scope-group-helpers.js +8 -5
  135. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.d.ts +8 -0
  136. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +10 -3
  137. package/lib/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.d.ts +48 -0
  138. package/lib/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.js +204 -0
  139. package/lib/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.d.ts +22 -0
  140. package/lib/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.js +160 -0
  141. package/lib/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.d.ts +163 -0
  142. package/lib/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.js +603 -0
  143. package/lib/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.d.ts +68 -0
  144. package/lib/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.js +182 -0
  145. package/lib/helpers/materialized-reports/tenant-cash-in-out-formatter.d.ts +98 -0
  146. package/lib/helpers/materialized-reports/tenant-cash-in-out-formatter.js +160 -0
  147. package/lib/helpers/materialized-reports/tenant-delivery-formatter.d.ts +133 -0
  148. package/lib/helpers/materialized-reports/tenant-delivery-formatter.js +233 -0
  149. package/lib/helpers/materialized-reports/tenant-discount-formatter.d.ts +114 -0
  150. package/lib/helpers/materialized-reports/tenant-discount-formatter.js +431 -0
  151. package/lib/helpers/materialized-reports/tenant-hourly-comparison-formatter.d.ts +60 -0
  152. package/lib/helpers/materialized-reports/tenant-hourly-comparison-formatter.js +244 -0
  153. package/lib/helpers/materialized-reports/tenant-payments-comparison-formatter.d.ts +75 -0
  154. package/lib/helpers/materialized-reports/tenant-payments-comparison-formatter.js +289 -0
  155. package/lib/helpers/materialized-reports/tenant-prep-time-formatter.d.ts +98 -0
  156. package/lib/helpers/materialized-reports/tenant-prep-time-formatter.js +232 -0
  157. package/lib/helpers/materialized-reports/tenant-sales-comparison-formatter.d.ts +105 -0
  158. package/lib/helpers/materialized-reports/tenant-sales-comparison-formatter.js +352 -0
  159. package/lib/helpers/reporting-contract/index.d.ts +2 -0
  160. package/lib/helpers/reporting-contract/index.js +18 -0
  161. package/lib/helpers/reporting-contract/overview-contract.d.ts +55 -0
  162. package/lib/helpers/reporting-contract/overview-contract.js +127 -0
  163. package/lib/helpers/reporting-contract/report-contract.d.ts +159 -0
  164. package/lib/helpers/reporting-contract/report-contract.js +239 -0
  165. package/lib/helpers/rider-earnings/index.d.ts +1 -0
  166. package/lib/helpers/rider-earnings/index.js +17 -0
  167. package/lib/helpers/rider-earnings/rider-earnings.d.ts +51 -0
  168. package/lib/helpers/rider-earnings/rider-earnings.js +57 -0
  169. package/lib/helpers/rider-reports/index.d.ts +2 -0
  170. package/lib/helpers/rider-reports/index.js +18 -0
  171. package/lib/helpers/rider-reports/rider-daily-report.d.ts +28 -0
  172. package/lib/helpers/rider-reports/rider-daily-report.js +93 -0
  173. package/lib/helpers/rider-reports/rider-report.d.ts +49 -0
  174. package/lib/helpers/rider-reports/rider-report.js +70 -0
  175. package/lib/helpers/store/common-order-actions.js +3 -2
  176. package/lib/helpers/types/materialized-reports-apis.d.ts +300 -1
  177. package/lib/schemas/models/deliveries.d.ts +58 -1
  178. package/lib/schemas/models/deliveries.js +81 -0
  179. package/lib/schemas/models/expense_details.d.ts +3 -3
  180. package/lib/schemas/models/index.d.ts +15 -0
  181. package/lib/schemas/models/index.js +15 -0
  182. package/lib/schemas/models/materialized_credit_bills.d.ts +3 -3
  183. package/lib/schemas/models/materialized_credit_bills.js +1 -1
  184. package/lib/schemas/models/materialized_rider_daily_reports.d.ts +27 -0
  185. package/lib/schemas/models/materialized_rider_daily_reports.js +42 -0
  186. package/lib/schemas/models/materialized_tenant_cash_entries.d.ts +181 -0
  187. package/lib/schemas/models/materialized_tenant_cash_entries.js +98 -0
  188. package/lib/schemas/models/materialized_tenant_cash_reports.d.ts +111 -0
  189. package/lib/schemas/models/materialized_tenant_cash_reports.js +80 -0
  190. package/lib/schemas/models/materialized_tenant_delivery_entries.d.ts +98 -0
  191. package/lib/schemas/models/materialized_tenant_delivery_entries.js +66 -0
  192. package/lib/schemas/models/materialized_tenant_delivery_reports.d.ts +98 -0
  193. package/lib/schemas/models/materialized_tenant_delivery_reports.js +66 -0
  194. package/lib/schemas/models/materialized_tenant_discount_entries.d.ts +199 -0
  195. package/lib/schemas/models/materialized_tenant_discount_entries.js +159 -0
  196. package/lib/schemas/models/materialized_tenant_discount_reports.d.ts +216 -0
  197. package/lib/schemas/models/materialized_tenant_discount_reports.js +336 -0
  198. package/lib/schemas/models/materialized_tenant_hourly_comparison_entries.d.ts +95 -0
  199. package/lib/schemas/models/materialized_tenant_hourly_comparison_entries.js +82 -0
  200. package/lib/schemas/models/materialized_tenant_hourly_comparison_reports.d.ts +105 -0
  201. package/lib/schemas/models/materialized_tenant_hourly_comparison_reports.js +91 -0
  202. package/lib/schemas/models/materialized_tenant_payments_comparison_entries.d.ts +91 -0
  203. package/lib/schemas/models/materialized_tenant_payments_comparison_entries.js +70 -0
  204. package/lib/schemas/models/materialized_tenant_payments_comparison_reports.d.ts +98 -0
  205. package/lib/schemas/models/materialized_tenant_payments_comparison_reports.js +71 -0
  206. package/lib/schemas/models/materialized_tenant_prep_time_entries.d.ts +181 -0
  207. package/lib/schemas/models/materialized_tenant_prep_time_entries.js +133 -0
  208. package/lib/schemas/models/materialized_tenant_prep_time_reports.d.ts +118 -0
  209. package/lib/schemas/models/materialized_tenant_prep_time_reports.js +112 -0
  210. package/lib/schemas/models/materialized_tenant_sales_comparison_entries.d.ts +139 -0
  211. package/lib/schemas/models/materialized_tenant_sales_comparison_entries.js +120 -0
  212. package/lib/schemas/models/materialized_tenant_sales_comparison_reports.d.ts +167 -0
  213. package/lib/schemas/models/materialized_tenant_sales_comparison_reports.js +137 -0
  214. package/lib/schemas/models/materialized_tenant_sales_overview_reports.d.ts +3 -3
  215. package/lib/schemas/models/materialized_tenant_sales_overview_reports.js +1 -1
  216. package/lib/schemas/models/tenant_purchase_order_vendor_returns.d.ts +3 -3
  217. package/lib/schemas/models/warehouse_internal_movements.d.ts +6 -6
  218. package/lib/schemas/models/warehouse_purchase_order_receipts.d.ts +3 -3
  219. package/lib/schemas/models/warehouse_purchase_order_vendor_returns.d.ts +3 -3
  220. package/lib/schemas/models/warehouse_sale_deliveries.d.ts +6 -6
  221. package/lib/schemas/models/warehouse_sale_invoices.d.ts +3 -3
  222. package/lib/schemas/types/deliveries.d.ts +40 -1
  223. package/lib/schemas/types/index.d.ts +15 -0
  224. package/lib/schemas/types/index.js +15 -0
  225. package/lib/schemas/types/materialized_rider_daily_reports.d.ts +37 -0
  226. package/lib/schemas/types/materialized_rider_daily_reports.js +2 -0
  227. package/lib/schemas/types/materialized_tenant_cash_entries.d.ts +87 -0
  228. package/lib/schemas/types/materialized_tenant_cash_entries.js +2 -0
  229. package/lib/schemas/types/materialized_tenant_cash_reports.d.ts +144 -0
  230. package/lib/schemas/types/materialized_tenant_cash_reports.js +2 -0
  231. package/lib/schemas/types/materialized_tenant_delivery_entries.d.ts +98 -0
  232. package/lib/schemas/types/materialized_tenant_delivery_entries.js +2 -0
  233. package/lib/schemas/types/materialized_tenant_delivery_reports.d.ts +179 -0
  234. package/lib/schemas/types/materialized_tenant_delivery_reports.js +2 -0
  235. package/lib/schemas/types/materialized_tenant_discount_entries.d.ts +81 -0
  236. package/lib/schemas/types/materialized_tenant_discount_entries.js +2 -0
  237. package/lib/schemas/types/materialized_tenant_discount_reports.d.ts +209 -0
  238. package/lib/schemas/types/materialized_tenant_discount_reports.js +3 -0
  239. package/lib/schemas/types/materialized_tenant_hourly_comparison_entries.d.ts +33 -0
  240. package/lib/schemas/types/materialized_tenant_hourly_comparison_entries.js +2 -0
  241. package/lib/schemas/types/materialized_tenant_hourly_comparison_reports.d.ts +40 -0
  242. package/lib/schemas/types/materialized_tenant_hourly_comparison_reports.js +2 -0
  243. package/lib/schemas/types/materialized_tenant_payments_comparison_entries.d.ts +48 -0
  244. package/lib/schemas/types/materialized_tenant_payments_comparison_entries.js +2 -0
  245. package/lib/schemas/types/materialized_tenant_payments_comparison_reports.d.ts +51 -0
  246. package/lib/schemas/types/materialized_tenant_payments_comparison_reports.js +14 -0
  247. package/lib/schemas/types/materialized_tenant_prep_time_entries.d.ts +88 -0
  248. package/lib/schemas/types/materialized_tenant_prep_time_entries.js +2 -0
  249. package/lib/schemas/types/materialized_tenant_prep_time_reports.d.ts +132 -0
  250. package/lib/schemas/types/materialized_tenant_prep_time_reports.js +2 -0
  251. package/lib/schemas/types/materialized_tenant_sales_comparison_entries.d.ts +43 -0
  252. package/lib/schemas/types/materialized_tenant_sales_comparison_entries.js +2 -0
  253. package/lib/schemas/types/materialized_tenant_sales_comparison_reports.d.ts +74 -0
  254. package/lib/schemas/types/materialized_tenant_sales_comparison_reports.js +17 -0
  255. package/lib/schemas/types/materialized_tenant_sales_overview_reports.d.ts +7 -0
  256. package/lib/tests/deliveries-schema.test.d.ts +1 -0
  257. package/lib/tests/deliveries-schema.test.js +70 -0
  258. package/lib/tests/materialized-reports/analytics-growth-helpers.test.d.ts +1 -0
  259. package/lib/tests/materialized-reports/analytics-overview-helpers.test.d.ts +1 -0
  260. package/lib/tests/materialized-reports/analytics-performance-helpers.test.d.ts +1 -0
  261. package/lib/tests/materialized-reports/fixtures.d.ts +15 -1
  262. package/lib/tests/materialized-reports/fixtures.js +1 -0
  263. package/lib/tests/materialized-reports/materialized-cash-compile.test.d.ts +1 -0
  264. package/lib/tests/materialized-reports/materialized-cash-compile.test.js +197 -0
  265. package/lib/tests/materialized-reports/materialized-cash-fold.test.d.ts +1 -0
  266. package/lib/tests/materialized-reports/materialized-cash-fold.test.js +116 -0
  267. package/lib/tests/materialized-reports/materialized-cash-formatter.test.d.ts +1 -0
  268. package/lib/tests/materialized-reports/materialized-cash-formatter.test.js +153 -0
  269. package/lib/tests/materialized-reports/materialized-cash-plan.test.d.ts +1 -0
  270. package/lib/tests/materialized-reports/materialized-cash-plan.test.js +168 -0
  271. package/lib/tests/materialized-reports/materialized-cash-reconciliation.test.d.ts +1 -0
  272. package/lib/tests/materialized-reports/materialized-cash-reconciliation.test.js +267 -0
  273. package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.d.ts +1 -0
  274. package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.js +147 -0
  275. package/lib/tests/materialized-reports/materialized-delivery-compile.test.d.ts +1 -0
  276. package/lib/tests/materialized-reports/materialized-delivery-compile.test.js +242 -0
  277. package/lib/tests/materialized-reports/materialized-delivery-fold.test.d.ts +1 -0
  278. package/lib/tests/materialized-reports/materialized-delivery-fold.test.js +109 -0
  279. package/lib/tests/materialized-reports/materialized-delivery-formatter.test.d.ts +1 -0
  280. package/lib/tests/materialized-reports/materialized-delivery-formatter.test.js +142 -0
  281. package/lib/tests/materialized-reports/materialized-delivery-plan.test.d.ts +1 -0
  282. package/lib/tests/materialized-reports/materialized-delivery-plan.test.js +198 -0
  283. package/lib/tests/materialized-reports/materialized-discount-aggregated-formatter.test.d.ts +1 -0
  284. package/lib/tests/materialized-reports/materialized-discount-aggregated-formatter.test.js +173 -0
  285. package/lib/tests/materialized-reports/materialized-discount-classification.test.d.ts +1 -0
  286. package/lib/tests/materialized-reports/materialized-discount-classification.test.js +339 -0
  287. package/lib/tests/materialized-reports/materialized-discount-close-day.test.d.ts +1 -0
  288. package/lib/tests/materialized-reports/materialized-discount-close-day.test.js +506 -0
  289. package/lib/tests/materialized-reports/materialized-discount-compile.test.d.ts +1 -0
  290. package/lib/tests/materialized-reports/materialized-discount-compile.test.js +161 -0
  291. package/lib/tests/materialized-reports/materialized-discount-counter-rows.test.d.ts +1 -0
  292. package/lib/tests/materialized-reports/materialized-discount-counter-rows.test.js +107 -0
  293. package/lib/tests/materialized-reports/materialized-discount-entries-accumulation.test.d.ts +1 -0
  294. package/lib/tests/materialized-reports/materialized-discount-entries-accumulation.test.js +125 -0
  295. package/lib/tests/materialized-reports/materialized-hourly-comparison-compile.test.d.ts +1 -0
  296. package/lib/tests/materialized-reports/materialized-hourly-comparison-compile.test.js +146 -0
  297. package/lib/tests/materialized-reports/materialized-hourly-comparison-formatter.test.d.ts +1 -0
  298. package/lib/tests/materialized-reports/materialized-hourly-comparison-formatter.test.js +127 -0
  299. package/lib/tests/materialized-reports/materialized-hourly-comparison-plan.test.d.ts +1 -0
  300. package/lib/tests/materialized-reports/materialized-hourly-comparison-plan.test.js +138 -0
  301. package/lib/tests/materialized-reports/materialized-hourly-comparison-selection.test.d.ts +1 -0
  302. package/lib/tests/materialized-reports/materialized-hourly-comparison-selection.test.js +90 -0
  303. package/lib/tests/materialized-reports/materialized-payments-comparison-compile.test.d.ts +1 -0
  304. package/lib/tests/materialized-reports/materialized-payments-comparison-compile.test.js +164 -0
  305. package/lib/tests/materialized-reports/materialized-payments-comparison-formatter.test.d.ts +1 -0
  306. package/lib/tests/materialized-reports/materialized-payments-comparison-formatter.test.js +117 -0
  307. package/lib/tests/materialized-reports/materialized-payments-comparison-plan.test.d.ts +1 -0
  308. package/lib/tests/materialized-reports/materialized-payments-comparison-plan.test.js +149 -0
  309. package/lib/tests/materialized-reports/materialized-prep-time-classification.test.d.ts +1 -0
  310. package/lib/tests/materialized-reports/materialized-prep-time-classification.test.js +273 -0
  311. package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.d.ts +1 -0
  312. package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.js +271 -0
  313. package/lib/tests/materialized-reports/materialized-prep-time-compile.test.d.ts +1 -0
  314. package/lib/tests/materialized-reports/materialized-prep-time-compile.test.js +217 -0
  315. package/lib/tests/materialized-reports/materialized-prep-time-constants.test.d.ts +1 -0
  316. package/lib/tests/materialized-reports/materialized-prep-time-constants.test.js +84 -0
  317. package/lib/tests/materialized-reports/materialized-prep-time-plan.test.d.ts +1 -0
  318. package/lib/tests/materialized-reports/materialized-prep-time-plan.test.js +176 -0
  319. package/lib/tests/materialized-reports/materialized-reports-scope-group-helpers.test.js +13 -13
  320. package/lib/tests/materialized-reports/materialized-sales-comparison-baseline.test.d.ts +1 -0
  321. package/lib/tests/materialized-reports/materialized-sales-comparison-baseline.test.js +126 -0
  322. package/lib/tests/materialized-reports/materialized-sales-comparison-compile.test.d.ts +1 -0
  323. package/lib/tests/materialized-reports/materialized-sales-comparison-compile.test.js +207 -0
  324. package/lib/tests/materialized-reports/materialized-sales-comparison-formatter.test.d.ts +1 -0
  325. package/lib/tests/materialized-reports/materialized-sales-comparison-formatter.test.js +179 -0
  326. package/lib/tests/materialized-reports/materialized-sales-comparison-parity.test.d.ts +1 -0
  327. package/lib/tests/materialized-reports/materialized-sales-comparison-parity.test.js +196 -0
  328. package/lib/tests/materialized-reports/materialized-sales-comparison-plan.test.d.ts +1 -0
  329. package/lib/tests/materialized-reports/materialized-sales-comparison-plan.test.js +241 -0
  330. package/lib/tests/materialized-reports/materialized-tenant-hourly-comparison-schema.test.d.ts +1 -0
  331. package/lib/tests/materialized-reports/materialized-tenant-hourly-comparison-schema.test.js +96 -0
  332. package/lib/tests/materialized-reports/materialized-tenant-payments-comparison-schema.test.d.ts +1 -0
  333. package/lib/tests/materialized-reports/materialized-tenant-payments-comparison-schema.test.js +112 -0
  334. package/lib/tests/materialized-reports/materialized-tenant-sales-comparison-schema.test.d.ts +1 -0
  335. package/lib/tests/materialized-reports/materialized-tenant-sales-comparison-schema.test.js +142 -0
  336. package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +8 -1
  337. package/lib/tests/materialized-reports/sales-comparison-reference.d.ts +55 -0
  338. package/lib/tests/materialized-reports/sales-comparison-reference.js +86 -0
  339. package/lib/tests/materialized-reports/tenant-discount-formatter.test.d.ts +1 -0
  340. package/lib/tests/materialized-reports/tenant-discount-formatter.test.js +289 -0
  341. package/lib/tests/materialized-reports/tenant-prep-time-formatter.test.d.ts +1 -0
  342. package/lib/tests/materialized-reports/tenant-prep-time-formatter.test.js +354 -0
  343. package/lib/tests/reporting-contract.test.d.ts +1 -0
  344. package/lib/tests/reporting-contract.test.js +267 -0
  345. package/lib/tests/rider-daily-report.test.d.ts +1 -0
  346. package/lib/tests/rider-daily-report.test.js +86 -0
  347. package/lib/tests/rider-earnings.test.d.ts +1 -0
  348. package/lib/tests/rider-earnings.test.js +65 -0
  349. package/lib/tests/rider-report.test.d.ts +1 -0
  350. package/lib/tests/rider-report.test.js +98 -0
  351. package/package.json +10 -1
@@ -0,0 +1,615 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.subtractMaterializedDiscountEntrySections = exports.mergeMaterializedDiscountEntrySections = void 0;
4
+ exports.getMaterializedDiscountScopeRefsForDay = getMaterializedDiscountScopeRefsForDay;
5
+ exports.needsMaterializedDiscountReportWrite = needsMaterializedDiscountReportWrite;
6
+ exports.updateMaterializedDiscountReport = updateMaterializedDiscountReport;
7
+ exports.updateMaterializedDiscountEntries = updateMaterializedDiscountEntries;
8
+ exports.needsMaterializedDiscountEntriesWrite = needsMaterializedDiscountEntriesWrite;
9
+ exports.buildTenantDiscountCloseDayPlan = buildTenantDiscountCloseDayPlan;
10
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
11
+ const materialized_tenant_reports_iterations_helpers_1 = require("./materialized-tenant-reports-iterations-helpers");
12
+ const scope_helpers_1 = require("./scope-helpers");
13
+ /** Day-scoped documents (replaced wholesale for the compiled day). */
14
+ const DISCOUNT_DAY_SCOPES = [
15
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR,
16
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
17
+ ];
18
+ /** Period documents (accumulate day deltas; corrected by subtraction). */
19
+ // DERIVED from the report's scope list, like prep / cash / delivery. Hard-coding this list
20
+ // meant a scope added to MATERIALIZED_DISCOUNT_REPORT_SCOPES could be compiled and read but
21
+ // never accumulated here - and an accumulating scope missing from this list is invisible
22
+ // until a period document disagrees with its days.
23
+ const DISCOUNT_ACCUMULATED_SCOPES = MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_REPORT_SCOPES.filter((scope) => !DISCOUNT_DAY_SCOPES.includes(scope));
24
+ /** True for the accumulating (period) scopes. */
25
+ const isAccumulatedScope = (scope) => DISCOUNT_ACCUMULATED_SCOPES.includes(scope);
26
+ const scopeKey = (doc) => `${doc.scope_context}|${doc.scope_field}`;
27
+ /** Every stored scope one local day's plan needs (date + 3 periods + 24 hours). */
28
+ function getMaterializedDiscountScopeRefsForDay(localDate) {
29
+ const periodFields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(localDate);
30
+ if (periodFields == null)
31
+ return null;
32
+ const refs = [
33
+ { scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE, scope_field: localDate },
34
+ ];
35
+ for (const scope of DISCOUNT_ACCUMULATED_SCOPES) {
36
+ refs.push({ scope_context: scope, scope_field: periodFields[scope] });
37
+ }
38
+ for (let hour = 0; hour < 24; hour += 1) {
39
+ const scopeField = (0, scope_helpers_1.getMaterializedHourScopeField)(localDate, hour);
40
+ if (scopeField == null)
41
+ return null;
42
+ refs.push({ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR, scope_field: scopeField });
43
+ }
44
+ return refs;
45
+ }
46
+ /**
47
+ * Watermark guard: does the stored document need a structural write?
48
+ * `false` (skip) when the incoming `source_watermark` is not newer than the
49
+ * stored one and the schema version + key grammar match.
50
+ */
51
+ function needsMaterializedDiscountReportWrite(existing, incoming) {
52
+ if (existing == null || incoming == null)
53
+ return true;
54
+ if (existing.schema_version !== incoming.schema_version)
55
+ return true;
56
+ if ((existing.sale_type_key_filters ?? []).join(':') !== (incoming.sale_type_key_filters ?? []).join(':')) {
57
+ return true;
58
+ }
59
+ const existingTime = new Date(existing.source_watermark ?? 0).getTime();
60
+ const incomingTime = new Date(incoming.source_watermark ?? 0).getTime();
61
+ if (Number.isNaN(existingTime) || Number.isNaN(incomingTime))
62
+ return true;
63
+ return incomingTime > existingTime;
64
+ }
65
+ /** Adds or subtracts every numeric counter field, recursively (`sign` = ±1). */
66
+ function applyCounters(target, delta, sign) {
67
+ if (target == null || delta == null)
68
+ return;
69
+ for (const [key, value] of Object.entries(delta)) {
70
+ if (typeof value === 'number') {
71
+ const current = target[key];
72
+ target[key] = (typeof current === 'number' ? current : 0) + sign * value;
73
+ continue;
74
+ }
75
+ if (value != null && typeof value === 'object' && !Array.isArray(value)) {
76
+ const nested = (target[key] ?? (target[key] = {}));
77
+ applyCounters(nested, value, sign);
78
+ }
79
+ }
80
+ }
81
+ /** True when a counter block is all zeros (pruned so an emptied day leaves no key). */
82
+ function isZeroCounters(counters) {
83
+ if (counters == null)
84
+ return true;
85
+ for (const value of Object.values(counters)) {
86
+ if (typeof value === 'number') {
87
+ if (value !== 0)
88
+ return false;
89
+ continue;
90
+ }
91
+ if (value != null && typeof value === 'object' && !isZeroCounters(value)) {
92
+ return false;
93
+ }
94
+ }
95
+ return true;
96
+ }
97
+ /**
98
+ * Applies a counter update to a stored document: optionally REPLACE the counter
99
+ * map wholesale, else subtract the given deltas and add the compiled ones. Keys
100
+ * that end up all-zero are pruned so a corrected day leaves no ghost entry.
101
+ * Returns `null` on a schema / key-grammar conflict (never throws).
102
+ */
103
+ function updateMaterializedDiscountReport(existing, updates, options = {}) {
104
+ if (existing == null || updates == null)
105
+ return null;
106
+ if ((existing.sale_type_key_filters ?? []).join(':') !==
107
+ ((updates.replacement ?? updates.additions?.[0])?.sale_type_key_filters ??
108
+ existing.sale_type_key_filters ??
109
+ []).join(':')) {
110
+ return null;
111
+ }
112
+ if (updates.replacement != null &&
113
+ (updates.replacement.schema_version !== existing.schema_version ||
114
+ updates.replacement.scope_context !== existing.scope_context ||
115
+ updates.replacement.scope_field !== existing.scope_field)) {
116
+ return null;
117
+ }
118
+ const computedAt = options.computedAt ?? updates.computedAt ?? new Date();
119
+ const counters = updates.replacement != null
120
+ ? JSON.parse(JSON.stringify(updates.replacement.discount_counters ?? {}))
121
+ : JSON.parse(JSON.stringify(existing.discount_counters ?? {}));
122
+ if (updates.replacement == null) {
123
+ for (const delta of updates.subtract ?? []) {
124
+ if (delta.schema_version !== existing.schema_version)
125
+ return null;
126
+ applyCounters(counters, delta.discount_counters, -1);
127
+ }
128
+ for (const delta of updates.additions ?? []) {
129
+ if (delta.schema_version !== existing.schema_version)
130
+ return null;
131
+ const merged = counters;
132
+ for (const [key, value] of Object.entries(delta.discount_counters ?? {})) {
133
+ const bucket = (merged[key] ?? (merged[key] = {}));
134
+ applyCounters(bucket, value, 1);
135
+ }
136
+ }
137
+ }
138
+ // Prune keys the corrections emptied (an all-zero bucket must not survive).
139
+ for (const key of Object.keys(counters)) {
140
+ if (isZeroCounters(counters[key]))
141
+ delete counters[key];
142
+ }
143
+ const watermarkCandidates = [
144
+ existing.source_watermark,
145
+ updates.sourceWatermark ?? null,
146
+ updates.replacement?.source_watermark ?? null,
147
+ ...(updates.additions ?? []).map((doc) => doc.source_watermark),
148
+ ].filter((value) => value != null);
149
+ const sourceWatermark = watermarkCandidates.reduce((max, value) => (new Date(value).getTime() > new Date(max).getTime() ? value : max), existing.source_watermark);
150
+ const finalCounters = Object.keys(counters).length === 0
151
+ ? null
152
+ : counters;
153
+ return {
154
+ ...existing,
155
+ discount_counters: finalCounters,
156
+ // The read-path rows are REBUILT from the folded map rather than mutated in
157
+ // step with it — one source of truth, so the map and the rows can never
158
+ // disagree (and a correction that empties a key drops it from both).
159
+ discount_counter_rows: finalCounters == null ? null : Object.entries(finalCounters).map(([key, value]) => ({ key, counters: value })),
160
+ source_watermark: sourceWatermark,
161
+ computed_at: computedAt,
162
+ updatedAt: computedAt,
163
+ iteration: (existing.iteration ?? 1) + 1,
164
+ };
165
+ }
166
+ /** Row identity inside a period document: an order can hold many rows. */
167
+ const discountEntryRowIdentity = (row) => String(row.order_id) + '|' + String(row.entry_key);
168
+ /**
169
+ * Merges a freshly compiled day of rows into a PERIOD document's stored rows.
170
+ *
171
+ * Idempotent by construction: the merge key is `(order_id, entry_key)`, which is
172
+ * globally unique per row - `entry_key` is the row's stable identity INSIDE its
173
+ * order (`item:<itemId>:<slot>` / `order:<slot>` / `payment:<paymentId>`), and an
174
+ * order belongs to exactly one day. So re-closing the SAME day REPLACES its own
175
+ * rows (fresh wins) instead of duplicating them, while a new day ADDS its rows.
176
+ *
177
+ * Without this, a period document held only the last closed day's rows - the
178
+ * measured symptom was a `localMonth` entry document carrying 9 rows where the
179
+ * equivalent credit-ledger month document carried the whole month (20).
180
+ */
181
+ const mergeMaterializedDiscountEntrySections = (stored, fresh) => {
182
+ const out = {};
183
+ const keys = new Set([...Object.keys(stored ?? {}), ...Object.keys(fresh ?? {})]);
184
+ for (const key of keys) {
185
+ const byIdentity = new Map();
186
+ for (const row of stored?.[key]?.rows ?? []) {
187
+ byIdentity.set(discountEntryRowIdentity(row), row);
188
+ }
189
+ // FRESH wins: a re-close of the same day overwrites its own rows.
190
+ for (const row of fresh?.[key]?.rows ?? []) {
191
+ byIdentity.set(discountEntryRowIdentity(row), row);
192
+ }
193
+ if (byIdentity.size > 0)
194
+ out[key] = { rows: [...byIdentity.values()] };
195
+ }
196
+ return out;
197
+ };
198
+ exports.mergeMaterializedDiscountEntrySections = mergeMaterializedDiscountEntrySections;
199
+ /**
200
+ * Removes the given rows from a stored document's sections - the MIRROR of
201
+ * `mergeMaterializedDiscountEntrySections`.
202
+ *
203
+ * Deliberately keyed on the SAME `discountEntryRowIdentity`, so the merge and the
204
+ * subtraction cannot drift: whatever the merge treats as one row, this removes.
205
+ * A section left with no rows disappears, matching the merge's own output shape.
206
+ */
207
+ const subtractMaterializedDiscountEntrySections = (stored, removed) => {
208
+ const out = {};
209
+ for (const key of Object.keys(stored ?? {})) {
210
+ const removedIdentities = new Set((removed?.[key]?.rows ?? []).map(discountEntryRowIdentity));
211
+ const rows = (stored?.[key]?.rows ?? []).filter((row) => !removedIdentities.has(discountEntryRowIdentity(row)));
212
+ if (rows.length > 0)
213
+ out[key] = { rows };
214
+ }
215
+ return out;
216
+ };
217
+ exports.subtractMaterializedDiscountEntrySections = subtractMaterializedDiscountEntrySections;
218
+ /**
219
+ * Updates a stored ENTRY document.
220
+ *
221
+ * - DAY scopes (`localHour` / `localDate`) are REPLACED wholesale: the day's rows
222
+ * are the whole truth for that scope.
223
+ * - PERIOD scopes (`localWeek` / `localWeekOfYear` / `localMonth`) MERGE, because
224
+ * the compile produces only ONE day of rows: replacing would leave the period
225
+ * showing just its final day.
226
+ *
227
+ * Returns null when the documents disagree on schema/scope (a data issue the
228
+ * caller must surface).
229
+ */
230
+ function updateMaterializedDiscountEntries(existing, compiled, options = {}) {
231
+ if (existing == null || compiled == null)
232
+ return null;
233
+ if (existing.schema_version !== compiled.schema_version ||
234
+ existing.scope_context !== compiled.scope_context ||
235
+ existing.scope_field !== compiled.scope_field) {
236
+ return null;
237
+ }
238
+ const computedAt = options.computedAt ?? new Date();
239
+ const existingTime = new Date(existing.source_watermark ?? 0).getTime();
240
+ const compiledTime = new Date(compiled.source_watermark ?? 0).getTime();
241
+ return {
242
+ ...existing,
243
+ discount_entries: options.mergePeriodRows === true
244
+ ? (0, exports.mergeMaterializedDiscountEntrySections)(existing.discount_entries, compiled.discount_entries)
245
+ : compiled.discount_entries,
246
+ source_watermark: compiledTime > existingTime ? compiled.source_watermark : existing.source_watermark,
247
+ computed_at: computedAt,
248
+ updatedAt: computedAt,
249
+ iteration: (existing.iteration ?? 1) + 1,
250
+ };
251
+ }
252
+ /** True when a stored entry document equals the compiled one's rows + watermark. */
253
+ function needsMaterializedDiscountEntriesWrite(existing, incoming) {
254
+ if (existing == null || incoming == null)
255
+ return true;
256
+ if (existing.schema_version !== incoming.schema_version)
257
+ return true;
258
+ if ((existing.sale_type_key_filters ?? []).join(':') !== (incoming.sale_type_key_filters ?? []).join(':')) {
259
+ return true;
260
+ }
261
+ const existingTime = new Date(existing.source_watermark ?? 0).getTime();
262
+ const incomingTime = new Date(incoming.source_watermark ?? 0).getTime();
263
+ if (Number.isNaN(existingTime) || Number.isNaN(incomingTime))
264
+ return true;
265
+ return incomingTime > existingTime;
266
+ }
267
+ /**
268
+ * Builds the close-day write plan for ONE tenant-day. Returns `null` (never
269
+ * throws) on inconsistent input — compiled docs of another tenant, duplicate
270
+ * scope documents, a malformed date, duplicate STORED rows for one scope (a
271
+ * missing unique index), or a schema/grammar conflict.
272
+ */
273
+ function buildTenantDiscountCloseDayPlan(input) {
274
+ if (input == null)
275
+ return null;
276
+ const { tenantId, reports, entries, reason } = input;
277
+ if (typeof tenantId !== 'string' || tenantId.length === 0)
278
+ return null;
279
+ if (typeof reason !== 'string' || reason.length === 0)
280
+ return null;
281
+ if (!Array.isArray(reports) || !Array.isArray(entries))
282
+ return null;
283
+ if (reports.some((doc) => doc == null || doc.tenant_id !== tenantId))
284
+ return null;
285
+ const computedAt = input.computedAt ?? new Date();
286
+ const force = input.force === true;
287
+ const dateDoc = reports.find((doc) => doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE) ?? null;
288
+ // An emptied day has no compiled documents, so the caller supplies the date.
289
+ const localDate = dateDoc?.scope_field ?? input.localDate ?? null;
290
+ if (localDate == null || getMaterializedDiscountScopeRefsForDay(localDate) == null)
291
+ return null;
292
+ if (dateDoc != null && dateDoc.tenant_id !== tenantId)
293
+ return null;
294
+ const compiledByScope = new Map();
295
+ for (const doc of reports) {
296
+ const key = scopeKey(doc);
297
+ if (compiledByScope.has(key))
298
+ return null; // duplicate — data issue
299
+ compiledByScope.set(key, doc);
300
+ }
301
+ const existingByScope = new Map();
302
+ for (const doc of input.existing ?? []) {
303
+ if (doc == null || doc.tenant_id !== tenantId)
304
+ continue;
305
+ const key = scopeKey(doc);
306
+ const prior = existingByScope.get(key);
307
+ if (prior == null) {
308
+ existingByScope.set(key, doc);
309
+ continue;
310
+ }
311
+ if (String(prior._id) !== String(doc._id))
312
+ return null; // two rows for one scope
313
+ }
314
+ const existingEntriesByScope = new Map();
315
+ /**
316
+ * Rebuilds a PERIOD entry document's rows from the DAY entry documents still on hand.
317
+ *
318
+ * Discount period entries ACCUMULATE each day's rows, so a period document that was never
319
+ * persisted - a partial failure between the counter writes and the entry writes - would
320
+ * otherwise be rebuilt from the CLOSING DAY ALONE, silently dropping every sibling day's
321
+ * rows from the period drill-down even though those days' documents are right there. The
322
+ * counters already repair this case explicitly; without this, the entries did not.
323
+ *
324
+ * Only called when the period document is MISSING, so the normal path keeps its
325
+ * accumulate-forward behaviour and pays nothing for this.
326
+ */
327
+ const refoldPeriodEntrySections = (scopeContext, scopeField) => {
328
+ let folded = {};
329
+ for (const doc of existingEntriesByScope.values()) {
330
+ if (doc.scope_context !== MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE)
331
+ continue;
332
+ const refs = getMaterializedDiscountScopeRefsForDay(String(doc.scope_field));
333
+ if (refs == null)
334
+ continue;
335
+ const belongs = refs.some((ref) => ref.scope_context === scopeContext && ref.scope_field === scopeField);
336
+ if (!belongs)
337
+ continue;
338
+ folded = (0, exports.mergeMaterializedDiscountEntrySections)(folded, doc.discount_entries);
339
+ }
340
+ return folded;
341
+ };
342
+ for (const doc of input.existingEntries ?? []) {
343
+ if (doc == null || doc.tenant_id !== tenantId)
344
+ continue;
345
+ const key = scopeKey(doc);
346
+ const prior = existingEntriesByScope.get(key);
347
+ if (prior == null) {
348
+ existingEntriesByScope.set(key, doc);
349
+ continue;
350
+ }
351
+ if (String(prior._id) !== String(doc._id))
352
+ return null;
353
+ }
354
+ const writes = [];
355
+ const entryWrites = [];
356
+ const staleDocuments = [];
357
+ const staleEntryDocuments = [];
358
+ const skippedDocuments = [];
359
+ const skippedEntryDocuments = [];
360
+ const createdPeriodDocuments = [];
361
+ const addWrite = (report) => {
362
+ const persisted = report.iteration == null ? { ...report, iteration: 1 } : report;
363
+ const iteration = (0, materialized_tenant_reports_iterations_helpers_1.buildMaterializedTenantReportIteration)(persisted, {
364
+ reportType: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_REPORTS,
365
+ reason,
366
+ computedAt,
367
+ });
368
+ if (iteration == null)
369
+ return null;
370
+ writes.push({ report: persisted, iteration });
371
+ return persisted;
372
+ };
373
+ const addEntryWrite = (report) => {
374
+ const persisted = report.iteration == null ? { ...report, iteration: 1 } : report;
375
+ const iteration = (0, materialized_tenant_reports_iterations_helpers_1.buildMaterializedTenantReportIteration)(persisted, {
376
+ reportType: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES,
377
+ reason,
378
+ computedAt,
379
+ });
380
+ if (iteration == null)
381
+ return null;
382
+ entryWrites.push({ report: persisted, iteration });
383
+ return persisted;
384
+ };
385
+ const storedDateDoc = existingByScope.get(`${MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE}|${localDate}`) ?? null;
386
+ const storedDateDocInCompiled = compiledByScope.get(`${MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE}|${localDate}`) ?? null;
387
+ // The rows this day CURRENTLY contributes, read from its own day-scoped entry documents.
388
+ // A period entry document MERGES day rows in, and a merge can only ADD or OVERWRITE - it
389
+ // cannot remove. So any close that replaces this day must subtract these rows from every
390
+ // period document first, or rows the correction removed linger there as phantoms while the
391
+ // DAY document correctly shrinks. Two callers need this: the emptied-day path (subtract
392
+ // everything) and the normal path (subtract, then merge the fresh rows back).
393
+ const storedDayEntrySections = {};
394
+ const isThisDayEntryDoc = (doc) => doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE
395
+ ? doc.scope_field === localDate
396
+ : doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR &&
397
+ typeof doc.scope_field === 'string' &&
398
+ doc.scope_field.startsWith(`${localDate} `);
399
+ for (const doc of existingEntriesByScope.values()) {
400
+ if (!isThisDayEntryDoc(doc))
401
+ continue;
402
+ for (const [key, section] of Object.entries(doc.discount_entries ?? {})) {
403
+ storedDayEntrySections[key] = {
404
+ rows: [...(storedDayEntrySections[key]?.rows ?? []), ...(section?.rows ?? [])],
405
+ };
406
+ }
407
+ }
408
+ if (reports.length === 0) {
409
+ // The day has NO discounts anymore (orders deleted / moved): subtract the
410
+ // stored day from every period document and drop the day's documents.
411
+ if (storedDateDoc != null) {
412
+ for (const doc of existingByScope.values()) {
413
+ if (!isAccumulatedScope(doc.scope_context))
414
+ continue;
415
+ const updated = updateMaterializedDiscountReport(doc, { subtract: [storedDateDoc] }, { computedAt });
416
+ if (updated == null)
417
+ return null;
418
+ if ((updated.iteration ?? 1) === (doc.iteration ?? 1))
419
+ continue;
420
+ if (addWrite(updated) == null)
421
+ return null;
422
+ }
423
+ for (const doc of existingByScope.values()) {
424
+ if (DISCOUNT_DAY_SCOPES.includes(doc.scope_context))
425
+ staleDocuments.push(doc);
426
+ }
427
+ // ENTRY documents. A PERIOD entry document ACCUMULATES every day's rows
428
+ // (`updateMaterializedDiscountEntries` merges them in), so DELETING it discards
429
+ // every OTHER day it holds - those rows exist nowhere else. The emptied day must
430
+ // be SUBTRACTED from each period document, and only the day's OWN documents
431
+ // dropped. Deleting the period document would also leave the period drill-down
432
+ // empty until some later close recreated it, holding that one day.
433
+ for (const doc of existingEntriesByScope.values()) {
434
+ if (isThisDayEntryDoc(doc))
435
+ staleEntryDocuments.push(doc);
436
+ }
437
+ for (const doc of existingEntriesByScope.values()) {
438
+ // Another day's document is not this close's business - never touch it.
439
+ if (DISCOUNT_DAY_SCOPES.includes(doc.scope_context))
440
+ continue;
441
+ const remaining = (0, exports.subtractMaterializedDiscountEntrySections)(doc.discount_entries, storedDayEntrySections);
442
+ const rowsBefore = Object.values(doc.discount_entries ?? {}).reduce((sum, section) => sum + (section?.rows ?? []).length, 0);
443
+ const rowsAfter = Object.values(remaining).reduce((sum, section) => sum + (section?.rows ?? []).length, 0);
444
+ // Nothing of this day was in that period: leave it untouched.
445
+ if (rowsAfter === rowsBefore)
446
+ continue;
447
+ // No rows left anywhere in the period: the document itself goes.
448
+ if (rowsAfter === 0) {
449
+ staleEntryDocuments.push(doc);
450
+ continue;
451
+ }
452
+ if (addEntryWrite({
453
+ ...doc,
454
+ discount_entries: remaining,
455
+ computed_at: computedAt,
456
+ updatedAt: computedAt,
457
+ iteration: (doc.iteration ?? 1) + 1,
458
+ }) == null) {
459
+ return null;
460
+ }
461
+ }
462
+ }
463
+ return {
464
+ writes,
465
+ entryWrites,
466
+ staleDocuments,
467
+ staleEntryDocuments,
468
+ skippedDocuments,
469
+ skippedEntryDocuments,
470
+ createdPeriodDocuments,
471
+ };
472
+ }
473
+ const dayChanged = force || needsMaterializedDiscountReportWrite(storedDateDoc, storedDateDocInCompiled);
474
+ if (!dayChanged) {
475
+ for (const compiled of reports) {
476
+ const existing = existingByScope.get(scopeKey(compiled)) ?? null;
477
+ if (existing != null)
478
+ skippedDocuments.push(existing);
479
+ // Partial-failure repair: the day is up to date but this doc was never persisted.
480
+ else if (addWrite(compiled) == null)
481
+ return null;
482
+ }
483
+ }
484
+ else {
485
+ for (const compiled of reports) {
486
+ const existing = existingByScope.get(scopeKey(compiled)) ?? null;
487
+ if (DISCOUNT_DAY_SCOPES.includes(compiled.scope_context)) {
488
+ if (existing == null) {
489
+ if (addWrite(compiled) == null)
490
+ return null;
491
+ }
492
+ else {
493
+ const updated = updateMaterializedDiscountReport(existing, { replacement: compiled }, { computedAt });
494
+ if (updated == null || addWrite(updated) == null)
495
+ return null;
496
+ }
497
+ continue;
498
+ }
499
+ if (!isAccumulatedScope(compiled.scope_context))
500
+ return null; // unknown scope
501
+ if (existing == null) {
502
+ if (addWrite(compiled) == null)
503
+ return null;
504
+ createdPeriodDocuments.push({ scope_context: compiled.scope_context, scope_field: compiled.scope_field });
505
+ continue;
506
+ }
507
+ const updated = updateMaterializedDiscountReport(existing, { subtract: storedDateDoc != null ? [storedDateDoc] : [], additions: [compiled] }, { computedAt });
508
+ if (updated == null || addWrite(updated) == null)
509
+ return null;
510
+ }
511
+ // Stored hour documents of this day with no activity left.
512
+ for (const doc of existingByScope.values()) {
513
+ if (doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR &&
514
+ typeof doc.scope_field === 'string' &&
515
+ doc.scope_field.startsWith(`${localDate} `) &&
516
+ !compiledByScope.has(scopeKey(doc))) {
517
+ staleDocuments.push(doc);
518
+ }
519
+ }
520
+ }
521
+ // ---- entry documents ------------------------------------------------------
522
+ // DAY scopes are replaced wholesale (the day is the whole truth); PERIOD scopes
523
+ // MERGE the day's rows in - see updateMaterializedDiscountEntries. ----------
524
+ const compiledEntriesByScope = new Map();
525
+ for (const doc of entries) {
526
+ if (doc == null || doc.tenant_id !== tenantId)
527
+ return null;
528
+ const key = scopeKey(doc);
529
+ if (compiledEntriesByScope.has(key))
530
+ return null; // duplicate — data issue
531
+ compiledEntriesByScope.set(key, doc);
532
+ }
533
+ for (const compiled of compiledEntriesByScope.values()) {
534
+ const existing = existingEntriesByScope.get(scopeKey(compiled)) ?? null;
535
+ if (!dayChanged && existing != null && !needsMaterializedDiscountEntriesWrite(existing, compiled)) {
536
+ skippedEntryDocuments.push(existing);
537
+ continue;
538
+ }
539
+ if (existing == null) {
540
+ const missingIsPeriod = !DISCOUNT_DAY_SCOPES.includes(compiled.scope_context);
541
+ if (!missingIsPeriod) {
542
+ if (addEntryWrite(compiled) == null)
543
+ return null;
544
+ continue;
545
+ }
546
+ // Never persisted: rebuild from the sibling days, minus this day's stored rows (a
547
+ // correction may have REMOVED rows), then merge this day's fresh rows on top.
548
+ const folded = (0, exports.subtractMaterializedDiscountEntrySections)(refoldPeriodEntrySections(compiled.scope_context, String(compiled.scope_field)), storedDayEntrySections);
549
+ const rebuilt = (0, exports.mergeMaterializedDiscountEntrySections)(folded, compiled.discount_entries);
550
+ if (addEntryWrite({ ...compiled, discount_entries: rebuilt }) == null)
551
+ return null;
552
+ continue;
553
+ }
554
+ // A PERIOD document accumulates every day of the period; a DAY document is
555
+ // the day itself. Replacing a period wholesale left it holding only the
556
+ // final closed day (measured).
557
+ const isPeriodEntry = !DISCOUNT_DAY_SCOPES.includes(compiled.scope_context);
558
+ // PERIOD documents: remove this day's STORED rows FIRST, then merge the fresh ones.
559
+ // Merging alone cannot remove, so a correction that dropped a row left it behind in
560
+ // the period while the day document shrank - a phantom row in the period drill-down.
561
+ // DAY documents are replaced wholesale and need no subtraction.
562
+ const updated = updateMaterializedDiscountEntries(isPeriodEntry
563
+ ? {
564
+ ...existing,
565
+ discount_entries: (0, exports.subtractMaterializedDiscountEntrySections)(existing.discount_entries, storedDayEntrySections),
566
+ }
567
+ : existing, compiled, { computedAt, mergePeriodRows: isPeriodEntry });
568
+ if (updated == null || addEntryWrite(updated) == null)
569
+ return null;
570
+ }
571
+ // Stored entry documents for this day's scopes that the compile no longer has.
572
+ for (const doc of existingEntriesByScope.values()) {
573
+ if (compiledEntriesByScope.has(scopeKey(doc)))
574
+ continue;
575
+ const isDayScoped = doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE
576
+ ? doc.scope_field === localDate
577
+ : doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR &&
578
+ typeof doc.scope_field === 'string' &&
579
+ doc.scope_field.startsWith(`${localDate} `);
580
+ if (isDayScoped) {
581
+ staleEntryDocuments.push(doc);
582
+ continue;
583
+ }
584
+ // A DAY-scoped document belonging to ANOTHER day is simply not this close's
585
+ // business. Without this guard it fell through to the period branch below, where
586
+ // the compile had (correctly) not emitted it, so it was marked STALE and DELETED -
587
+ // meaning every close wiped every OTHER day's entry rows. Measured in the demo data
588
+ // before the fix: counters covered 19 discount days while only ONE day of entry rows
589
+ // survived, so the drill-down was empty for 18 days that the summary said had
590
+ // discounts. The period branch must only ever see PERIOD scopes.
591
+ if (doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE ||
592
+ doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR) {
593
+ continue;
594
+ }
595
+ // A PERIOD entry document must drop this day's rows: rows never accumulate,
596
+ // so a period doc carries the whole period's rows. When the day lost its
597
+ // rows the period doc is rebuilt from the compile's period entry sections
598
+ // (which the compile already produces per period scope).
599
+ const periodCompiled = compiledEntriesByScope.get(scopeKey(doc));
600
+ if (periodCompiled == null) {
601
+ // No rows left anywhere in this period scope for the compiled day — the
602
+ // caller's fetch includes it only when the compile emitted it, so drop it.
603
+ staleEntryDocuments.push(doc);
604
+ }
605
+ }
606
+ return {
607
+ writes,
608
+ entryWrites,
609
+ staleDocuments,
610
+ staleEntryDocuments,
611
+ skippedDocuments,
612
+ skippedEntryDocuments,
613
+ createdPeriodDocuments,
614
+ };
615
+ }
@@ -0,0 +1,47 @@
1
+ import { MATERIALIZED_REPORT_SCOPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
2
+ import { IOrder } from '../../schemas/types/orders';
3
+ import { IMaterializedTenantDiscountEntries } from '../../schemas/types/materialized_tenant_discount_entries';
4
+ import { IMaterializedTenantDiscountReport } from '../../schemas/types/materialized_tenant_discount_reports';
5
+ /**
6
+ * Discounts-report (M1) compile helpers — turn one tenant-day's orders into the
7
+ * documents the close writes:
8
+ *
9
+ * - `materialized_tenant_discount_reports` — COUNTERS per key per scope,
10
+ * - `materialized_tenant_discount_entries` — the drill-down ROWS per key per
11
+ * scope (month-capped, like the credit ledger).
12
+ *
13
+ * Both come from ONE pass of the accumulator, so a document's counters and its
14
+ * rows can never disagree.
15
+ *
16
+ * Scope fan-out: an order contributes to its punch HOUR, the DATE, the WEEK,
17
+ * the ISO WEEK-OF-YEAR and the MONTH — exactly `MATERIALIZED_DISCOUNT_REPORT_SCOPES`.
18
+ * Quarter / semester / year are deliberately NOT materialized (the report's
19
+ * calendar never asks for them).
20
+ *
21
+ * Pure and total: `null` on malformed input, never a throw.
22
+ */
23
+ export interface ICompileTenantDiscountReportsInput {
24
+ tenantId: string;
25
+ brandId: string;
26
+ /** The compiled business day (tenant-local `'YYYY-MM-DD'`). */
27
+ localDate: string;
28
+ /** Orders already staged for this day (punched or settled that day). */
29
+ orders: IOrder[];
30
+ /** Tenant GMT offset — used only when an order lacks enriched local fields. */
31
+ gmtOffset?: number;
32
+ computedAt: Date;
33
+ }
34
+ export interface ICompileTenantDiscountReportsResult {
35
+ reports: IMaterializedTenantDiscountReport[];
36
+ entries: IMaterializedTenantDiscountEntries[];
37
+ /** Orders that contributed to at least one document. */
38
+ contributingOrders: number;
39
+ }
40
+ /**
41
+ * Compiles one tenant-day into the discount report + entries documents.
42
+ * Returns `null` on malformed input (bad date / tenant / brand); returns
43
+ * EMPTY arrays when the day has no discounts at all.
44
+ */
45
+ export declare function compileTenantDiscountReports(input: ICompileTenantDiscountReportsInput | null | undefined): ICompileTenantDiscountReportsResult | null;
46
+ /** Scopes this report materializes — exported for the close plan + tests. */
47
+ export declare const MATERIALIZED_DISCOUNT_COMPILED_SCOPES: MATERIALIZED_REPORT_SCOPE_ENUM[];