@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,396 @@
1
+ import mongoose from 'mongoose';
2
+ import { CASH_KEY_FILTERS, MATERIALIZED_CASH_REPORT_SCOPES, MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_TYPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
3
+ import { buildMaterializedTenantReportIteration } from './materialized-tenant-reports-iterations-helpers';
4
+ import { getMaterializedPeriodScopeFields } from './scope-helpers';
5
+ import { freshCashCounters } from './materialized-cash-classification-helpers';
6
+ import { mergeMaterializedCashCounters } from './materialized-cash-accumulator-helpers';
7
+ /**
8
+ * M3 close-day helpers.
9
+ *
10
+ * ## Period counters are RE-FOLDED, never patched
11
+ *
12
+ * The discounted ledgers keep periods current by subtracting a day delta and adding
13
+ * the fresh one. That CANNOT work here, for the same reason it cannot work in the prep
14
+ * time report: cash has a NON-ADDITIVE counter. `staff_count` counts DISTINCT staff,
15
+ * and there is no way to subtract a distinct count - removing the day where a given
16
+ * staff member first appeared would leave a count that no longer corresponds to any
17
+ * set of days. So every touched period is rebuilt from its whole day set.
18
+ *
19
+ * ## The watermark comes from the FOLDED DAYS
20
+ *
21
+ * A folded document that inherited the STORED document watermark would look
22
+ * unchanged to `needsMaterializedCashReportWrite`, and the write would be skipped
23
+ * forever - freezing the period at its first closed day. That bug shipped once in the
24
+ * prep time report and is designed out here from the start.
25
+ */
26
+ /** Period scopes this report materializes (everything but the day scope). */
27
+ export const MATERIALIZED_CASH_PERIOD_SCOPES = MATERIALIZED_CASH_REPORT_SCOPES.filter((scope) => String(scope) !== MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE);
28
+ /** True when the scope is the plain day scope. */
29
+ export const isMaterializedCashDayScope = (scopeContext) => String(scopeContext) === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE;
30
+ const cashScopeKey = (doc) => String(doc.scope_context) + '|' + String(doc.scope_field);
31
+ /**
32
+ * Merges TWO period entry sections, keyed on `entry_key`.
33
+ *
34
+ * Rows are deduplicated by identity rather than concatenated: a source row belongs to
35
+ * exactly one day, so the same `entry_key` appearing twice means the fold saw it
36
+ * twice, and money must never be counted twice.
37
+ */
38
+ export function mergeMaterializedCashEntrySections(target, source) {
39
+ const rowsByKey = new Map();
40
+ const seenByKey = new Map();
41
+ const absorb = (input) => {
42
+ if (input == null)
43
+ return;
44
+ for (const [key, section] of Object.entries(input)) {
45
+ const rows = rowsByKey.get(key) ?? [];
46
+ const seen = seenByKey.get(key) ?? new Set();
47
+ for (const row of section?.rows ?? []) {
48
+ const identity = String(row.entry_key ?? row.cash_in_out_id ?? '');
49
+ if (identity !== '') {
50
+ if (seen.has(identity))
51
+ continue;
52
+ seen.add(identity);
53
+ }
54
+ rows.push(row);
55
+ }
56
+ rowsByKey.set(key, rows);
57
+ seenByKey.set(key, seen);
58
+ }
59
+ };
60
+ absorb(target);
61
+ absorb(source);
62
+ const out = {};
63
+ for (const [key, rows] of rowsByKey) {
64
+ rows.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime() ||
65
+ (String(a.cash_in_out_id) < String(b.cash_in_out_id) ? 1 : -1));
66
+ out[key] = { rows };
67
+ }
68
+ return out;
69
+ }
70
+ /** True when the stored document already carries this exact content. */
71
+ export function needsMaterializedCashReportWrite(existing, next) {
72
+ if (existing == null)
73
+ return true;
74
+ const stored = new Date(existing.source_watermark ?? 0).getTime();
75
+ const incoming = new Date(next.source_watermark ?? 0).getTime();
76
+ if (Number.isNaN(stored) || Number.isNaN(incoming))
77
+ return true;
78
+ return incoming > stored;
79
+ }
80
+ /** True when the stored entry document already carries these exact rows. */
81
+ export function needsMaterializedCashEntriesWrite(existing, next) {
82
+ if (existing == null)
83
+ return true;
84
+ const stored = new Date(existing.source_watermark ?? 0).getTime();
85
+ const incoming = new Date(next.source_watermark ?? 0).getTime();
86
+ if (Number.isNaN(stored) || Number.isNaN(incoming))
87
+ return true;
88
+ return incoming > stored;
89
+ }
90
+ /** Max watermark across the day documents that feed a fold. */
91
+ function watermarkOfDays(dayDocs, fallback) {
92
+ let latest = fallback.getTime();
93
+ for (const doc of dayDocs) {
94
+ const time = new Date(doc?.source_watermark ?? 0).getTime();
95
+ if (!Number.isNaN(time) && time > latest)
96
+ latest = time;
97
+ }
98
+ return new Date(latest);
99
+ }
100
+ /** Folds the counters of every day document that belongs to one period scope. */
101
+ export function foldMaterializedCashPeriodCounters(input) {
102
+ const { periodScope, periodScopeField, dayDocuments } = input;
103
+ const folded = {};
104
+ for (const dayDocument of dayDocuments ?? []) {
105
+ if (!isMaterializedCashDayScope(String(dayDocument?.scope_context)))
106
+ continue;
107
+ const fields = getMaterializedPeriodScopeFields(String(dayDocument.scope_field));
108
+ if (fields == null)
109
+ continue;
110
+ if (fields[periodScope] !== periodScopeField)
111
+ continue;
112
+ const counters = dayDocument.cash_counters ?? {};
113
+ for (const [key, block] of Object.entries(counters)) {
114
+ folded[key] = mergeMaterializedCashCounters(folded[key] ?? freshCashCounters(), block);
115
+ }
116
+ }
117
+ return folded;
118
+ }
119
+ /** Folds the statement rows of every day document that belongs to one period scope. */
120
+ export function foldMaterializedCashPeriodRows(input) {
121
+ const { periodScope, periodScopeField, dayDocuments } = input;
122
+ let merged = {};
123
+ for (const dayDocument of dayDocuments ?? []) {
124
+ if (!isMaterializedCashDayScope(String(dayDocument?.scope_context)))
125
+ continue;
126
+ const fields = getMaterializedPeriodScopeFields(String(dayDocument.scope_field));
127
+ if (fields == null)
128
+ continue;
129
+ if (fields[periodScope] !== periodScopeField)
130
+ continue;
131
+ merged = mergeMaterializedCashEntrySections(merged, dayDocument.cash_entries ?? null);
132
+ }
133
+ return merged;
134
+ }
135
+ /**
136
+ * Builds the close-day plan for ONE tenant-day.
137
+ *
138
+ * The DAY scope is written from the compile. Every PERIOD scope the day touches is
139
+ * RE-FOLDED from its whole day set - and the touched set is the union of the closed
140
+ * day's periods AND every removed day's periods, because emptying a day has to clear
141
+ * its contribution from the periods it used to be part of.
142
+ *
143
+ * Returns null on malformed input.
144
+ */
145
+ export function buildTenantCashCloseDayPlan(input) {
146
+ if (input == null)
147
+ return null;
148
+ const { tenantId, reports, entries, reason } = input;
149
+ if (typeof tenantId !== 'string' || tenantId.length === 0)
150
+ return null;
151
+ if (typeof reason !== 'string' || reason.length === 0)
152
+ return null;
153
+ const computedAt = input.computedAt ?? new Date();
154
+ const compiledReports = (reports ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
155
+ const compiledEntries = (entries ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
156
+ const storedReports = (input.existing ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
157
+ const storedEntries = (input.existingEntries ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
158
+ const compiledDateDoc = compiledReports.find((doc) => isMaterializedCashDayScope(String(doc.scope_context)));
159
+ const localDate = input.localDate ?? (compiledDateDoc == null ? null : String(compiledDateDoc.scope_field));
160
+ if (localDate == null || localDate === '')
161
+ return null;
162
+ const writes = [];
163
+ const entryWrites = [];
164
+ const staleDocuments = [];
165
+ const staleEntryDocuments = [];
166
+ const skippedDocuments = [];
167
+ const skippedEntryDocuments = [];
168
+ const storedReportsByScope = new Map();
169
+ for (const doc of storedReports)
170
+ storedReportsByScope.set(cashScopeKey(doc), doc);
171
+ const storedEntriesByScope = new Map();
172
+ for (const doc of storedEntries)
173
+ storedEntriesByScope.set(cashScopeKey(doc), doc);
174
+ const addWrite = (report) => {
175
+ const persisted = report.iteration == null ? { ...report, iteration: 1 } : report;
176
+ const iteration = buildMaterializedTenantReportIteration(persisted, {
177
+ reportType: MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_REPORTS,
178
+ reason,
179
+ computedAt,
180
+ });
181
+ if (iteration == null)
182
+ return false;
183
+ writes.push({ report: persisted, iteration: iteration });
184
+ return true;
185
+ };
186
+ const addEntryWrite = (report) => {
187
+ const persisted = report.iteration == null ? { ...report, iteration: 1 } : report;
188
+ const iteration = buildMaterializedTenantReportIteration(persisted, {
189
+ reportType: MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES,
190
+ reason,
191
+ computedAt,
192
+ });
193
+ if (iteration == null)
194
+ return false;
195
+ entryWrites.push({ report: persisted, iteration: iteration });
196
+ return true;
197
+ };
198
+ // ---- the DAY scope, straight from the compile ----
199
+ if (compiledDateDoc != null) {
200
+ const scopeKey = cashScopeKey(compiledDateDoc);
201
+ const existing = storedReportsByScope.get(scopeKey);
202
+ const next = {
203
+ ...compiledDateDoc,
204
+ _id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
205
+ iteration: Number(existing?.iteration ?? 0) + 1,
206
+ };
207
+ if (needsMaterializedCashReportWrite(existing, next))
208
+ addWrite(next);
209
+ else
210
+ skippedDocuments.push(existing);
211
+ }
212
+ const compiledDateEntries = compiledEntries.find((doc) => isMaterializedCashDayScope(String(doc.scope_context)));
213
+ if (compiledDateEntries != null) {
214
+ const scopeKey = cashScopeKey(compiledDateEntries);
215
+ const existing = storedEntriesByScope.get(scopeKey);
216
+ const next = {
217
+ ...compiledDateEntries,
218
+ _id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
219
+ iteration: Number(existing?.iteration ?? 0) + 1,
220
+ };
221
+ if (needsMaterializedCashEntriesWrite(existing, next)) {
222
+ addEntryWrite(next);
223
+ }
224
+ else {
225
+ skippedEntryDocuments.push(existing);
226
+ }
227
+ }
228
+ // ---- DAY deletion: a stored day doc that no longer compiled ----
229
+ const compiledDateKeys = new Set(compiledReports
230
+ .filter((doc) => isMaterializedCashDayScope(String(doc.scope_context)))
231
+ .map((doc) => cashScopeKey(doc)));
232
+ const removedDates = [];
233
+ for (const doc of storedReports) {
234
+ if (!isMaterializedCashDayScope(String(doc.scope_context)))
235
+ continue;
236
+ if (String(doc.scope_field) !== localDate)
237
+ continue;
238
+ if (compiledDateKeys.has(cashScopeKey(doc)))
239
+ continue;
240
+ staleDocuments.push(doc);
241
+ removedDates.push(String(doc.scope_field));
242
+ }
243
+ const compiledDateEntryKeys = new Set(compiledEntries
244
+ .filter((doc) => isMaterializedCashDayScope(String(doc.scope_context)))
245
+ .map((doc) => cashScopeKey(doc)));
246
+ for (const doc of storedEntries) {
247
+ if (!isMaterializedCashDayScope(String(doc.scope_context)))
248
+ continue;
249
+ if (String(doc.scope_field) !== localDate)
250
+ continue;
251
+ if (compiledDateEntryKeys.has(cashScopeKey(doc)))
252
+ continue;
253
+ staleEntryDocuments.push(doc);
254
+ }
255
+ // ---- PERIOD re-fold ----
256
+ const nextDayDocs = storedReports.filter((doc) => {
257
+ if (!isMaterializedCashDayScope(String(doc.scope_context)))
258
+ return false;
259
+ if (String(doc.scope_field) === localDate)
260
+ return false;
261
+ return true;
262
+ });
263
+ const willHaveDay = compiledDateDoc != null;
264
+ if (willHaveDay) {
265
+ nextDayDocs.push(writes.find((w) => isMaterializedCashDayScope(String(w.report.scope_context)))?.report ??
266
+ compiledDateDoc);
267
+ }
268
+ const nextDayEntryDocs = storedEntries.filter((doc) => {
269
+ if (!isMaterializedCashDayScope(String(doc.scope_context)))
270
+ return false;
271
+ return String(doc.scope_field) !== localDate;
272
+ });
273
+ const compiledDateEntryDoc = entryWrites.find((w) => isMaterializedCashDayScope(String(w.report.scope_context)))?.report;
274
+ if (compiledDateEntryDoc != null)
275
+ nextDayEntryDocs.push(compiledDateEntryDoc);
276
+ const touchedPeriods = new Map();
277
+ const noteDay = (day) => {
278
+ const fields = getMaterializedPeriodScopeFields(day);
279
+ if (fields == null)
280
+ return;
281
+ for (const scope of MATERIALIZED_CASH_PERIOD_SCOPES) {
282
+ const field = fields[String(scope)];
283
+ if (field == null || field === '')
284
+ continue;
285
+ touchedPeriods.set(String(scope) + '|' + field, { scope: String(scope), field });
286
+ }
287
+ };
288
+ noteDay(localDate);
289
+ for (const day of removedDates)
290
+ noteDay(day);
291
+ const periodFieldsOfDay = (day) => getMaterializedPeriodScopeFields(day);
292
+ for (const { scope, field } of touchedPeriods.values()) {
293
+ const folded = foldMaterializedCashPeriodCounters({
294
+ periodScope: scope,
295
+ periodScopeField: field,
296
+ dayDocuments: nextDayDocs,
297
+ });
298
+ const existing = storedReportsByScope.get(scope + '|' + field);
299
+ const keys = Object.keys(folded).sort();
300
+ if (keys.length === 0) {
301
+ if (existing != null)
302
+ staleDocuments.push(existing);
303
+ continue;
304
+ }
305
+ const counters = {};
306
+ const counterRows = [];
307
+ for (const key of keys) {
308
+ counters[key] = folded[key];
309
+ counterRows.push({ key, counters: folded[key] });
310
+ }
311
+ const report = {
312
+ _id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
313
+ tenant_id: tenantId,
314
+ brand_id: existing?.brand_id ?? compiledDateDoc?.brand_id ?? '',
315
+ scope_context: scope,
316
+ scope_field: field,
317
+ day_index: null,
318
+ sale_type_key_filters: existing?.sale_type_key_filters ?? [...CASH_KEY_FILTERS],
319
+ cash_counters: counters,
320
+ cash_counter_rows: counterRows,
321
+ iteration: existing?.iteration,
322
+ schema_version: existing?.schema_version ?? 1,
323
+ // FROM THE FOLDED DAYS - never the stored watermark, or the write would be
324
+ // skipped forever and the period would freeze at its first closed day.
325
+ source_watermark: watermarkOfDays(nextDayDocs, existing?.source_watermark ?? computedAt),
326
+ computed_at: computedAt,
327
+ createdAt: existing?.createdAt ?? computedAt,
328
+ updatedAt: computedAt,
329
+ };
330
+ const next = { ...report, iteration: Number(existing?.iteration ?? 0) + 1 };
331
+ if (needsMaterializedCashReportWrite(existing, next)) {
332
+ addWrite(next);
333
+ }
334
+ else {
335
+ skippedDocuments.push(existing);
336
+ }
337
+ }
338
+ for (const { scope, field } of touchedPeriods.values()) {
339
+ const sections = foldMaterializedCashPeriodRows({
340
+ periodScope: scope,
341
+ periodScopeField: field,
342
+ dayDocuments: nextDayEntryDocs,
343
+ });
344
+ const existing = storedEntriesByScope.get(scope + '|' + field);
345
+ const keys = Object.keys(sections);
346
+ if (keys.length === 0) {
347
+ if (existing != null)
348
+ staleEntryDocuments.push(existing);
349
+ continue;
350
+ }
351
+ let watermark = existing?.source_watermark ?? null;
352
+ for (const dayDoc of nextDayEntryDocs) {
353
+ const fields = periodFieldsOfDay(String(dayDoc.scope_field));
354
+ if (fields == null)
355
+ continue;
356
+ if (fields[scope] !== field)
357
+ continue;
358
+ const time = new Date(dayDoc.source_watermark ?? 0).getTime();
359
+ if (Number.isNaN(time))
360
+ continue;
361
+ if (watermark == null || time > new Date(watermark).getTime())
362
+ watermark = new Date(time);
363
+ }
364
+ const report = {
365
+ _id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
366
+ tenant_id: tenantId,
367
+ brand_id: existing?.brand_id ?? compiledDateEntries?.brand_id ?? '',
368
+ scope_context: scope,
369
+ scope_field: field,
370
+ day_index: null,
371
+ sale_type_key_filters: existing?.sale_type_key_filters ?? [...CASH_KEY_FILTERS],
372
+ cash_entries: sections,
373
+ iteration: existing?.iteration,
374
+ schema_version: existing?.schema_version ?? 1,
375
+ source_watermark: watermark ?? computedAt,
376
+ computed_at: computedAt,
377
+ createdAt: existing?.createdAt ?? computedAt,
378
+ updatedAt: computedAt,
379
+ };
380
+ const next = { ...report, iteration: Number(existing?.iteration ?? 0) + 1 };
381
+ if (needsMaterializedCashEntriesWrite(existing, next)) {
382
+ addEntryWrite(next);
383
+ }
384
+ else {
385
+ skippedEntryDocuments.push(existing);
386
+ }
387
+ }
388
+ return {
389
+ writes,
390
+ entryWrites,
391
+ staleDocuments,
392
+ staleEntryDocuments,
393
+ skippedDocuments,
394
+ skippedEntryDocuments,
395
+ };
396
+ }
@@ -0,0 +1,245 @@
1
+ import { ALL_DAY_INDEX } from '../../constants/ENUMS';
2
+ import { CASH_IN_OUT_REF_ENUM, CASH_IN_OUT_TYPE_ENUM } from '../../constants/enums/cash';
3
+ import { CASH_KEY_FILTERS, MATERIALIZED_REPORT_SCOPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
4
+ import { MATERIALIZED_TENANT_CASH_ENTRIES_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_cash_entries';
5
+ import { MATERIALIZED_TENANT_CASH_REPORTS_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_cash_reports';
6
+ import { addCashMovement, classifyCashMovement, addCashSession, finalizeCashAccumulator, freshCashAccumulator, } from './materialized-cash-classification-helpers';
7
+ /**
8
+ * M3 cash - key grammar and the day compile.
9
+ *
10
+ * ## The key, and the ONE place this spec needed a decision
11
+ *
12
+ * The key is `<DRAWER_ID>:<REF_TYPE>` (decision (b3)). But a counter block holds BOTH
13
+ * `movements` and `sessions`, and a SESSION document has no `ref_type` at all - so the
14
+ * concept doc does not say which key a session belongs to. Resolved here as:
15
+ *
16
+ * session counters attach to the `<drawer_id>:DRAWER` key.
17
+ *
18
+ * That is the one key it can belong to without inventing a fake ref type: `DRAWER` is
19
+ * a real ledger ref type, and the rows carrying it ARE the session lifecycle (the
20
+ * opening float, every reopen, the count-out). So "drawer lifecycle movements + the
21
+ * session facts behind them" is exactly what that key means, and no read-side special
22
+ * casing or reserved token is needed. Flagged to the owner as a spec gap filled.
23
+ *
24
+ * ## The day compile
25
+ *
26
+ * Cash is NOT order-derived: the scope comes from the movement rows and the session
27
+ * documents directly. One key produces one counter block, and the block is stored
28
+ * TWICE - as a key-addressed map and as an array of rows - so the read is the
29
+ * canonical unwind/group/direct-sum pattern.
30
+ */
31
+ /** The separator between the drawer id and the ref type inside a key. */
32
+ export const MATERIALIZED_CASH_KEY_SEPARATOR = ':';
33
+ /**
34
+ * Builds a key. The separator cannot appear in either component: a drawer id or ref
35
+ * type containing it would make the key unparseable, so such a row is SKIPPED by the
36
+ * caller rather than silently mis-keyed.
37
+ */
38
+ export function buildMaterializedCashKey(drawerId, refType) {
39
+ const drawer = drawerId == null ? '' : String(drawerId).trim();
40
+ const ref = refType == null ? '' : String(refType).trim();
41
+ if (drawer === '' || ref === '')
42
+ return null;
43
+ if (drawer.includes(MATERIALIZED_CASH_KEY_SEPARATOR) || ref.includes(MATERIALIZED_CASH_KEY_SEPARATOR)) {
44
+ return null;
45
+ }
46
+ return drawer + MATERIALIZED_CASH_KEY_SEPARATOR + ref;
47
+ }
48
+ /** Splits a key back into its parts, or null when it is malformed. */
49
+ export function parseMaterializedCashKey(key) {
50
+ if (key == null)
51
+ return null;
52
+ const parts = String(key).split(MATERIALIZED_CASH_KEY_SEPARATOR);
53
+ if (parts.length !== 2)
54
+ return null;
55
+ const [drawerId, refType] = parts;
56
+ if (drawerId === '' || refType === '')
57
+ return null;
58
+ return { drawerId, refType };
59
+ }
60
+ /** The key that carries a drawer session counters. */
61
+ export function cashSessionKeyForDrawer(drawerId) {
62
+ return buildMaterializedCashKey(drawerId, CASH_IN_OUT_REF_ENUM.DRAWER);
63
+ }
64
+ const watermarkOf = (row, fallback) => {
65
+ const raw = row?.updatedAt ?? row?.createdAt;
66
+ const time = raw == null ? NaN : new Date(raw).getTime();
67
+ return Number.isNaN(time) ? fallback : new Date(time);
68
+ };
69
+ /**
70
+ * Compiles one business day into the DATE report document plus the DATE entries
71
+ * document. Returns null when the day has NEITHER a movement NOR a session - an
72
+ * empted day writes nothing, and the close-day planner deletes what is left over.
73
+ */
74
+ export function compileTenantCashReports(input) {
75
+ if (input == null)
76
+ return null;
77
+ const { tenantId, brandId, localDate } = input;
78
+ if (typeof tenantId !== 'string' ||
79
+ tenantId === '' ||
80
+ typeof brandId !== 'string' ||
81
+ brandId === '' ||
82
+ typeof localDate !== 'string' ||
83
+ localDate === '') {
84
+ return null;
85
+ }
86
+ const movements = Array.isArray(input.movements) ? input.movements : [];
87
+ const sessions = Array.isArray(input.sessions) ? input.sessions : [];
88
+ const computedAt = input.computedAt ?? new Date();
89
+ // Foreign tenants must never leak into this tenant's documents.
90
+ const ownMovements = movements.filter((row) => String(row?.tenant_id ?? tenantId) === tenantId);
91
+ const ownSessions = sessions.filter((row) => String(row?.tenant_id ?? tenantId) === tenantId);
92
+ if (ownMovements.length === 0 && ownSessions.length === 0)
93
+ return null;
94
+ const accumulators = new Map();
95
+ const entriesByKey = new Map();
96
+ const rowWatermarks = [];
97
+ const accumulatorFor = (key) => {
98
+ const existing = accumulators.get(key);
99
+ if (existing != null)
100
+ return existing;
101
+ const created = freshCashAccumulator();
102
+ accumulators.set(key, created);
103
+ return created;
104
+ };
105
+ for (const movement of ownMovements) {
106
+ const key = buildMaterializedCashKey(movement?.drawer_id, movement?.ref_type);
107
+ if (key == null) {
108
+ // An unparseable drawer id or ref type: SKIPPED, never mis-keyed.
109
+ continue;
110
+ }
111
+ addCashMovement(accumulatorFor(key), movement);
112
+ rowWatermarks.push(watermarkOf(movement, computedAt));
113
+ const drawerId = String(movement.drawer_id);
114
+ const type = String(movement.type) === CASH_IN_OUT_TYPE_ENUM.CASH_IN
115
+ ? CASH_IN_OUT_TYPE_ENUM.CASH_IN
116
+ : CASH_IN_OUT_TYPE_ENUM.CASH_OUT;
117
+ const amount = Math.abs(Number(movement.amount) || 0);
118
+ const isIn = type === CASH_IN_OUT_TYPE_ENUM.CASH_IN;
119
+ const created = new Date(movement?.createdAt ?? computedAt);
120
+ const sourceId = String(movement?.cash_in_out_id ?? movement?._id ?? '');
121
+ const entry = {
122
+ cash_in_out_id: sourceId,
123
+ entry_key: localDate + '|' + sourceId,
124
+ drawer_id: drawerId,
125
+ drawer_name: input.drawerNames?.[drawerId] ?? null,
126
+ day_id: String(movement?.day_id ?? ''),
127
+ shift_id: movement?.shift_id == null ? null : String(movement.shift_id),
128
+ shift_number: movement?.shift_number == null ? null : Number(movement.shift_number) || 0,
129
+ type,
130
+ ref_type: String(movement?.ref_type ?? CASH_IN_OUT_REF_ENUM.NONE),
131
+ ref_id: movement?.ref_id == null ? null : String(movement.ref_id),
132
+ amount,
133
+ signed_amount: isIn ? amount : -amount,
134
+ drawer_balance_before: Number(movement?.drawer_balance_before) || 0,
135
+ drawer_balance_after: Number(movement?.drawer_balance_after) || 0,
136
+ staff_id: movement?.staff_id == null ? null : String(movement.staff_id),
137
+ staff_name: movement?.staff_name == null ? null : String(movement.staff_name),
138
+ note: movement?.note == null ? null : String(movement.note),
139
+ ...classifyForEntry(movement),
140
+ created_at: created,
141
+ local_hour: localHourOf(movement),
142
+ };
143
+ const bucket = entriesByKey.get(key);
144
+ if (bucket == null)
145
+ entriesByKey.set(key, [entry]);
146
+ else
147
+ bucket.push(entry);
148
+ }
149
+ for (const session of ownSessions) {
150
+ const key = cashSessionKeyForDrawer(session?.drawer_id);
151
+ if (key == null) {
152
+ // Same posture as the movements: a row we cannot key is skipped.
153
+ continue;
154
+ }
155
+ addCashSession(accumulatorFor(key), session);
156
+ rowWatermarks.push(watermarkOf(session, computedAt));
157
+ }
158
+ if (accumulators.size === 0)
159
+ return null;
160
+ const cashCounters = {};
161
+ const cashCounterRows = [];
162
+ for (const key of [...accumulators.keys()].sort()) {
163
+ const counters = finalizeCashAccumulator(accumulators.get(key));
164
+ cashCounters[key] = counters;
165
+ cashCounterRows.push({ key, counters });
166
+ }
167
+ // Rows are sorted newest-first WITHIN each key. Merging the keys into one global
168
+ // newest-first statement is the READER's job: the page cursor is
169
+ // {time, cash_in_out_id}, which implies a global order across the keys in scope.
170
+ const cashEntries = {};
171
+ for (const key of [...entriesByKey.keys()].sort()) {
172
+ const rows = entriesByKey.get(key);
173
+ rows.sort((a, b) => b.created_at.getTime() - a.created_at.getTime() || (a.cash_in_out_id < b.cash_in_out_id ? 1 : -1));
174
+ cashEntries[key] = { rows };
175
+ }
176
+ const watermark = rowWatermarks.reduce((latest, current) => (current.getTime() > latest.getTime() ? current : latest), computedAt);
177
+ const reports = [
178
+ {
179
+ _id: undefined,
180
+ tenant_id: tenantId,
181
+ brand_id: brandId,
182
+ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
183
+ scope_field: localDate,
184
+ day_index: dayIndexOf(localDate),
185
+ sale_type_key_filters: [...CASH_KEY_FILTERS],
186
+ cash_counters: cashCounters,
187
+ cash_counter_rows: cashCounterRows,
188
+ schema_version: MATERIALIZED_TENANT_CASH_REPORTS_SCHEMA_VERSION,
189
+ source_watermark: watermark,
190
+ computed_at: computedAt,
191
+ createdAt: computedAt,
192
+ updatedAt: computedAt,
193
+ },
194
+ ];
195
+ const entries = [
196
+ {
197
+ _id: undefined,
198
+ tenant_id: tenantId,
199
+ brand_id: brandId,
200
+ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
201
+ scope_field: localDate,
202
+ day_index: dayIndexOf(localDate),
203
+ sale_type_key_filters: [...CASH_KEY_FILTERS],
204
+ cash_entries: Object.keys(cashEntries).length === 0 ? null : cashEntries,
205
+ schema_version: MATERIALIZED_TENANT_CASH_ENTRIES_SCHEMA_VERSION,
206
+ source_watermark: watermark,
207
+ computed_at: computedAt,
208
+ createdAt: computedAt,
209
+ updatedAt: computedAt,
210
+ },
211
+ ];
212
+ return { reports, entries };
213
+ }
214
+ /** The four lifecycle flags, computed ONCE here so the read never re-derives them. */
215
+ function classifyForEntry(row) {
216
+ const p = classifyCashMovement(row);
217
+ return {
218
+ is_reopen: p.is_reopen,
219
+ is_opening_float: p.is_opening_float,
220
+ is_count_out: p.is_count_out,
221
+ is_system_adjustment: p.is_system_adjustment,
222
+ };
223
+ }
224
+ /** `YYYY-MM-DD HH` from the local time fields, or null. */
225
+ function localHourOf(row) {
226
+ const explicit = row?.localHour;
227
+ if (typeof explicit === 'number' && Number.isFinite(explicit)) {
228
+ const day = String(row?.localDate ?? '');
229
+ return day === '' ? null : day + ' ' + String(explicit).padStart(2, '0');
230
+ }
231
+ const raw = row?.createdAt;
232
+ if (raw == null)
233
+ return null;
234
+ const date = new Date(raw);
235
+ if (Number.isNaN(date.getTime()))
236
+ return null;
237
+ return date.toISOString().slice(0, 13).replace('T', ' ');
238
+ }
239
+ /** Weekday denormalization for a date document. */
240
+ function dayIndexOf(localDate) {
241
+ const date = new Date(localDate + 'T00:00:00.000Z');
242
+ if (Number.isNaN(date.getTime()))
243
+ return null;
244
+ return ALL_DAY_INDEX[date.getUTCDay()] ?? null;
245
+ }