@managesome/eposmatic-toolkit 0.4.9 → 0.4.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (333) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +448 -0
  2. package/esm/helpers/index.js +3 -0
  3. package/esm/helpers/materialized-reports/index.js +32 -0
  4. package/esm/helpers/materialized-reports/materialized-cash-accumulator-helpers.js +114 -0
  5. package/esm/helpers/materialized-reports/materialized-cash-classification-helpers.js +286 -0
  6. package/esm/helpers/materialized-reports/materialized-cash-close-day-helpers.js +396 -0
  7. package/esm/helpers/materialized-reports/materialized-cash-compile-helpers.js +244 -0
  8. package/esm/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +170 -0
  9. package/esm/helpers/materialized-reports/materialized-delivery-classification-helpers.js +426 -0
  10. package/esm/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +372 -0
  11. package/esm/helpers/materialized-reports/materialized-delivery-compile-helpers.js +214 -0
  12. package/esm/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +318 -0
  13. package/esm/helpers/materialized-reports/materialized-discount-classification-helpers.js +213 -0
  14. package/esm/helpers/materialized-reports/materialized-discount-close-day-helpers.js +604 -0
  15. package/esm/helpers/materialized-reports/materialized-discount-compile-helpers.js +186 -0
  16. package/esm/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.js +155 -0
  17. package/esm/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.js +199 -0
  18. package/esm/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.js +111 -0
  19. package/esm/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +88 -0
  20. package/esm/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.js +394 -0
  21. package/esm/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.js +167 -0
  22. package/esm/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +215 -0
  23. package/esm/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +476 -0
  24. package/esm/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +201 -0
  25. package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +7 -0
  26. package/esm/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.js +189 -0
  27. package/esm/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.js +157 -0
  28. package/esm/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.js +588 -0
  29. package/esm/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.js +175 -0
  30. package/esm/helpers/materialized-reports/tenant-cash-in-out-formatter.js +156 -0
  31. package/esm/helpers/materialized-reports/tenant-delivery-formatter.js +230 -0
  32. package/esm/helpers/materialized-reports/tenant-discount-formatter.js +427 -0
  33. package/esm/helpers/materialized-reports/tenant-hourly-comparison-formatter.js +237 -0
  34. package/esm/helpers/materialized-reports/tenant-payments-comparison-formatter.js +279 -0
  35. package/esm/helpers/materialized-reports/tenant-prep-time-formatter.js +228 -0
  36. package/esm/helpers/materialized-reports/tenant-sales-comparison-formatter.js +343 -0
  37. package/esm/helpers/reporting-contract/index.js +2 -0
  38. package/esm/helpers/reporting-contract/overview-contract.js +122 -0
  39. package/esm/helpers/reporting-contract/report-contract.js +228 -0
  40. package/esm/helpers/rider-earnings/index.js +1 -0
  41. package/esm/helpers/rider-earnings/rider-earnings.js +59 -0
  42. package/esm/helpers/rider-reports/index.js +2 -0
  43. package/esm/helpers/rider-reports/rider-daily-report.js +90 -0
  44. package/esm/helpers/rider-reports/rider-report.js +67 -0
  45. package/esm/schemas/models/deliveries.js +75 -0
  46. package/esm/schemas/models/index.js +15 -0
  47. package/esm/schemas/models/materialized_rider_daily_reports.js +39 -0
  48. package/esm/schemas/models/materialized_tenant_cash_entries.js +95 -0
  49. package/esm/schemas/models/materialized_tenant_cash_reports.js +77 -0
  50. package/esm/schemas/models/materialized_tenant_delivery_entries.js +63 -0
  51. package/esm/schemas/models/materialized_tenant_delivery_reports.js +63 -0
  52. package/esm/schemas/models/materialized_tenant_discount_entries.js +156 -0
  53. package/esm/schemas/models/materialized_tenant_discount_reports.js +333 -0
  54. package/esm/schemas/models/materialized_tenant_hourly_comparison_entries.js +87 -0
  55. package/esm/schemas/models/materialized_tenant_hourly_comparison_reports.js +99 -0
  56. package/esm/schemas/models/materialized_tenant_payments_comparison_entries.js +71 -0
  57. package/esm/schemas/models/materialized_tenant_payments_comparison_reports.js +76 -0
  58. package/esm/schemas/models/materialized_tenant_prep_time_entries.js +130 -0
  59. package/esm/schemas/models/materialized_tenant_prep_time_reports.js +109 -0
  60. package/esm/schemas/models/materialized_tenant_sales_comparison_entries.js +130 -0
  61. package/esm/schemas/models/materialized_tenant_sales_comparison_reports.js +154 -0
  62. package/esm/schemas/types/index.js +15 -0
  63. package/esm/schemas/types/materialized_rider_daily_reports.js +1 -0
  64. package/esm/schemas/types/materialized_tenant_cash_entries.js +1 -0
  65. package/esm/schemas/types/materialized_tenant_cash_reports.js +1 -0
  66. package/esm/schemas/types/materialized_tenant_delivery_entries.js +1 -0
  67. package/esm/schemas/types/materialized_tenant_delivery_reports.js +1 -0
  68. package/esm/schemas/types/materialized_tenant_discount_entries.js +1 -0
  69. package/esm/schemas/types/materialized_tenant_discount_reports.js +2 -0
  70. package/esm/schemas/types/materialized_tenant_hourly_comparison_entries.js +1 -0
  71. package/esm/schemas/types/materialized_tenant_hourly_comparison_reports.js +1 -0
  72. package/esm/schemas/types/materialized_tenant_payments_comparison_entries.js +1 -0
  73. package/esm/schemas/types/materialized_tenant_payments_comparison_reports.js +11 -0
  74. package/esm/schemas/types/materialized_tenant_prep_time_entries.js +1 -0
  75. package/esm/schemas/types/materialized_tenant_prep_time_reports.js +1 -0
  76. package/esm/schemas/types/materialized_tenant_sales_comparison_entries.js +1 -0
  77. package/esm/schemas/types/materialized_tenant_sales_comparison_reports.js +14 -0
  78. package/lib/constants/MATERIALIZED_REPORTS.d.ts +339 -2
  79. package/lib/constants/MATERIALIZED_REPORTS.js +450 -1
  80. package/lib/helpers/index.d.ts +3 -0
  81. package/lib/helpers/index.js +3 -0
  82. package/lib/helpers/materialized-reports/index.d.ts +32 -0
  83. package/lib/helpers/materialized-reports/index.js +32 -0
  84. package/lib/helpers/materialized-reports/materialized-cash-accumulator-helpers.d.ts +18 -0
  85. package/lib/helpers/materialized-reports/materialized-cash-accumulator-helpers.js +118 -0
  86. package/lib/helpers/materialized-reports/materialized-cash-classification-helpers.d.ts +90 -0
  87. package/lib/helpers/materialized-reports/materialized-cash-classification-helpers.js +295 -0
  88. package/lib/helpers/materialized-reports/materialized-cash-close-day-helpers.d.ts +102 -0
  89. package/lib/helpers/materialized-reports/materialized-cash-close-day-helpers.js +409 -0
  90. package/lib/helpers/materialized-reports/materialized-cash-compile-helpers.d.ts +72 -0
  91. package/lib/helpers/materialized-reports/materialized-cash-compile-helpers.js +251 -0
  92. package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.d.ts +7 -0
  93. package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +173 -0
  94. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.d.ts +84 -0
  95. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.js +438 -0
  96. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.d.ts +97 -0
  97. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +385 -0
  98. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.d.ts +40 -0
  99. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.js +218 -0
  100. package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.d.ts +52 -0
  101. package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +324 -0
  102. package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.d.ts +50 -0
  103. package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.js +222 -0
  104. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.d.ts +182 -0
  105. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.js +615 -0
  106. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.d.ts +47 -0
  107. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.js +193 -0
  108. package/lib/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.d.ts +64 -0
  109. package/lib/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.js +163 -0
  110. package/lib/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.d.ts +61 -0
  111. package/lib/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.js +207 -0
  112. package/lib/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.d.ts +23 -0
  113. package/lib/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.js +116 -0
  114. package/lib/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.d.ts +25 -0
  115. package/lib/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +98 -0
  116. package/lib/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.d.ts +79 -0
  117. package/lib/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.js +407 -0
  118. package/lib/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.d.ts +35 -0
  119. package/lib/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.js +173 -0
  120. package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.d.ts +66 -0
  121. package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +224 -0
  122. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.d.ts +128 -0
  123. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +490 -0
  124. package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.d.ts +46 -0
  125. package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +208 -0
  126. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.d.ts +8 -0
  127. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +8 -1
  128. package/lib/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.d.ts +48 -0
  129. package/lib/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.js +204 -0
  130. package/lib/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.d.ts +22 -0
  131. package/lib/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.js +161 -0
  132. package/lib/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.d.ts +163 -0
  133. package/lib/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.js +603 -0
  134. package/lib/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.d.ts +68 -0
  135. package/lib/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.js +182 -0
  136. package/lib/helpers/materialized-reports/tenant-cash-in-out-formatter.d.ts +98 -0
  137. package/lib/helpers/materialized-reports/tenant-cash-in-out-formatter.js +159 -0
  138. package/lib/helpers/materialized-reports/tenant-delivery-formatter.d.ts +133 -0
  139. package/lib/helpers/materialized-reports/tenant-delivery-formatter.js +233 -0
  140. package/lib/helpers/materialized-reports/tenant-discount-formatter.d.ts +114 -0
  141. package/lib/helpers/materialized-reports/tenant-discount-formatter.js +431 -0
  142. package/lib/helpers/materialized-reports/tenant-hourly-comparison-formatter.d.ts +60 -0
  143. package/lib/helpers/materialized-reports/tenant-hourly-comparison-formatter.js +243 -0
  144. package/lib/helpers/materialized-reports/tenant-payments-comparison-formatter.d.ts +75 -0
  145. package/lib/helpers/materialized-reports/tenant-payments-comparison-formatter.js +286 -0
  146. package/lib/helpers/materialized-reports/tenant-prep-time-formatter.d.ts +97 -0
  147. package/lib/helpers/materialized-reports/tenant-prep-time-formatter.js +232 -0
  148. package/lib/helpers/materialized-reports/tenant-sales-comparison-formatter.d.ts +105 -0
  149. package/lib/helpers/materialized-reports/tenant-sales-comparison-formatter.js +349 -0
  150. package/lib/helpers/reporting-contract/index.d.ts +2 -0
  151. package/lib/helpers/reporting-contract/index.js +18 -0
  152. package/lib/helpers/reporting-contract/overview-contract.d.ts +55 -0
  153. package/lib/helpers/reporting-contract/overview-contract.js +126 -0
  154. package/lib/helpers/reporting-contract/report-contract.d.ts +159 -0
  155. package/lib/helpers/reporting-contract/report-contract.js +239 -0
  156. package/lib/helpers/rider-earnings/index.d.ts +1 -0
  157. package/lib/helpers/rider-earnings/index.js +17 -0
  158. package/lib/helpers/rider-earnings/rider-earnings.d.ts +58 -0
  159. package/lib/helpers/rider-earnings/rider-earnings.js +64 -0
  160. package/lib/helpers/rider-reports/index.d.ts +2 -0
  161. package/lib/helpers/rider-reports/index.js +18 -0
  162. package/lib/helpers/rider-reports/rider-daily-report.d.ts +26 -0
  163. package/lib/helpers/rider-reports/rider-daily-report.js +93 -0
  164. package/lib/helpers/rider-reports/rider-report.d.ts +49 -0
  165. package/lib/helpers/rider-reports/rider-report.js +70 -0
  166. package/lib/helpers/types/materialized-reports-apis.d.ts +297 -1
  167. package/lib/schemas/models/deliveries.d.ts +54 -0
  168. package/lib/schemas/models/deliveries.js +75 -0
  169. package/lib/schemas/models/expense_details.d.ts +3 -3
  170. package/lib/schemas/models/index.d.ts +15 -0
  171. package/lib/schemas/models/index.js +15 -0
  172. package/lib/schemas/models/materialized_rider_daily_reports.d.ts +27 -0
  173. package/lib/schemas/models/materialized_rider_daily_reports.js +42 -0
  174. package/lib/schemas/models/materialized_tenant_cash_entries.d.ts +181 -0
  175. package/lib/schemas/models/materialized_tenant_cash_entries.js +98 -0
  176. package/lib/schemas/models/materialized_tenant_cash_reports.d.ts +111 -0
  177. package/lib/schemas/models/materialized_tenant_cash_reports.js +80 -0
  178. package/lib/schemas/models/materialized_tenant_delivery_entries.d.ts +98 -0
  179. package/lib/schemas/models/materialized_tenant_delivery_entries.js +66 -0
  180. package/lib/schemas/models/materialized_tenant_delivery_reports.d.ts +98 -0
  181. package/lib/schemas/models/materialized_tenant_delivery_reports.js +66 -0
  182. package/lib/schemas/models/materialized_tenant_discount_entries.d.ts +199 -0
  183. package/lib/schemas/models/materialized_tenant_discount_entries.js +159 -0
  184. package/lib/schemas/models/materialized_tenant_discount_reports.d.ts +216 -0
  185. package/lib/schemas/models/materialized_tenant_discount_reports.js +336 -0
  186. package/lib/schemas/models/materialized_tenant_hourly_comparison_entries.d.ts +95 -0
  187. package/lib/schemas/models/materialized_tenant_hourly_comparison_entries.js +82 -0
  188. package/lib/schemas/models/materialized_tenant_hourly_comparison_reports.d.ts +105 -0
  189. package/lib/schemas/models/materialized_tenant_hourly_comparison_reports.js +91 -0
  190. package/lib/schemas/models/materialized_tenant_payments_comparison_entries.d.ts +91 -0
  191. package/lib/schemas/models/materialized_tenant_payments_comparison_entries.js +70 -0
  192. package/lib/schemas/models/materialized_tenant_payments_comparison_reports.d.ts +98 -0
  193. package/lib/schemas/models/materialized_tenant_payments_comparison_reports.js +71 -0
  194. package/lib/schemas/models/materialized_tenant_prep_time_entries.d.ts +181 -0
  195. package/lib/schemas/models/materialized_tenant_prep_time_entries.js +133 -0
  196. package/lib/schemas/models/materialized_tenant_prep_time_reports.d.ts +118 -0
  197. package/lib/schemas/models/materialized_tenant_prep_time_reports.js +112 -0
  198. package/lib/schemas/models/materialized_tenant_sales_comparison_entries.d.ts +139 -0
  199. package/lib/schemas/models/materialized_tenant_sales_comparison_entries.js +120 -0
  200. package/lib/schemas/models/materialized_tenant_sales_comparison_reports.d.ts +167 -0
  201. package/lib/schemas/models/materialized_tenant_sales_comparison_reports.js +137 -0
  202. package/lib/schemas/models/tenant_purchase_order_vendor_returns.d.ts +3 -3
  203. package/lib/schemas/models/warehouse_internal_movements.d.ts +6 -6
  204. package/lib/schemas/models/warehouse_purchase_order_receipts.d.ts +3 -3
  205. package/lib/schemas/models/warehouse_purchase_order_vendor_returns.d.ts +3 -3
  206. package/lib/schemas/models/warehouse_sale_deliveries.d.ts +6 -6
  207. package/lib/schemas/models/warehouse_sale_invoices.d.ts +3 -3
  208. package/lib/schemas/types/deliveries.d.ts +40 -1
  209. package/lib/schemas/types/index.d.ts +15 -0
  210. package/lib/schemas/types/index.js +15 -0
  211. package/lib/schemas/types/materialized_rider_daily_reports.d.ts +37 -0
  212. package/lib/schemas/types/materialized_rider_daily_reports.js +2 -0
  213. package/lib/schemas/types/materialized_tenant_cash_entries.d.ts +87 -0
  214. package/lib/schemas/types/materialized_tenant_cash_entries.js +2 -0
  215. package/lib/schemas/types/materialized_tenant_cash_reports.d.ts +144 -0
  216. package/lib/schemas/types/materialized_tenant_cash_reports.js +2 -0
  217. package/lib/schemas/types/materialized_tenant_delivery_entries.d.ts +98 -0
  218. package/lib/schemas/types/materialized_tenant_delivery_entries.js +2 -0
  219. package/lib/schemas/types/materialized_tenant_delivery_reports.d.ts +179 -0
  220. package/lib/schemas/types/materialized_tenant_delivery_reports.js +2 -0
  221. package/lib/schemas/types/materialized_tenant_discount_entries.d.ts +81 -0
  222. package/lib/schemas/types/materialized_tenant_discount_entries.js +2 -0
  223. package/lib/schemas/types/materialized_tenant_discount_reports.d.ts +209 -0
  224. package/lib/schemas/types/materialized_tenant_discount_reports.js +3 -0
  225. package/lib/schemas/types/materialized_tenant_hourly_comparison_entries.d.ts +33 -0
  226. package/lib/schemas/types/materialized_tenant_hourly_comparison_entries.js +2 -0
  227. package/lib/schemas/types/materialized_tenant_hourly_comparison_reports.d.ts +40 -0
  228. package/lib/schemas/types/materialized_tenant_hourly_comparison_reports.js +2 -0
  229. package/lib/schemas/types/materialized_tenant_payments_comparison_entries.d.ts +48 -0
  230. package/lib/schemas/types/materialized_tenant_payments_comparison_entries.js +2 -0
  231. package/lib/schemas/types/materialized_tenant_payments_comparison_reports.d.ts +51 -0
  232. package/lib/schemas/types/materialized_tenant_payments_comparison_reports.js +14 -0
  233. package/lib/schemas/types/materialized_tenant_prep_time_entries.d.ts +88 -0
  234. package/lib/schemas/types/materialized_tenant_prep_time_entries.js +2 -0
  235. package/lib/schemas/types/materialized_tenant_prep_time_reports.d.ts +132 -0
  236. package/lib/schemas/types/materialized_tenant_prep_time_reports.js +2 -0
  237. package/lib/schemas/types/materialized_tenant_sales_comparison_entries.d.ts +43 -0
  238. package/lib/schemas/types/materialized_tenant_sales_comparison_entries.js +2 -0
  239. package/lib/schemas/types/materialized_tenant_sales_comparison_reports.d.ts +74 -0
  240. package/lib/schemas/types/materialized_tenant_sales_comparison_reports.js +17 -0
  241. package/lib/tests/materialized-reports/analytics-growth-helpers.test.d.ts +1 -0
  242. package/lib/tests/materialized-reports/analytics-overview-helpers.test.d.ts +1 -0
  243. package/lib/tests/materialized-reports/analytics-performance-helpers.test.d.ts +1 -0
  244. package/lib/tests/materialized-reports/fixtures.d.ts +15 -1
  245. package/lib/tests/materialized-reports/fixtures.js +1 -0
  246. package/lib/tests/materialized-reports/materialized-cash-compile.test.d.ts +1 -0
  247. package/lib/tests/materialized-reports/materialized-cash-compile.test.js +196 -0
  248. package/lib/tests/materialized-reports/materialized-cash-fold.test.d.ts +1 -0
  249. package/lib/tests/materialized-reports/materialized-cash-fold.test.js +116 -0
  250. package/lib/tests/materialized-reports/materialized-cash-formatter.test.d.ts +1 -0
  251. package/lib/tests/materialized-reports/materialized-cash-formatter.test.js +152 -0
  252. package/lib/tests/materialized-reports/materialized-cash-plan.test.d.ts +1 -0
  253. package/lib/tests/materialized-reports/materialized-cash-plan.test.js +168 -0
  254. package/lib/tests/materialized-reports/materialized-cash-reconciliation.test.d.ts +1 -0
  255. package/lib/tests/materialized-reports/materialized-cash-reconciliation.test.js +267 -0
  256. package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.d.ts +1 -0
  257. package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.js +147 -0
  258. package/lib/tests/materialized-reports/materialized-delivery-compile.test.d.ts +1 -0
  259. package/lib/tests/materialized-reports/materialized-delivery-compile.test.js +241 -0
  260. package/lib/tests/materialized-reports/materialized-delivery-fold.test.d.ts +1 -0
  261. package/lib/tests/materialized-reports/materialized-delivery-fold.test.js +109 -0
  262. package/lib/tests/materialized-reports/materialized-delivery-formatter.test.d.ts +1 -0
  263. package/lib/tests/materialized-reports/materialized-delivery-formatter.test.js +141 -0
  264. package/lib/tests/materialized-reports/materialized-delivery-plan.test.d.ts +1 -0
  265. package/lib/tests/materialized-reports/materialized-delivery-plan.test.js +198 -0
  266. package/lib/tests/materialized-reports/materialized-discount-aggregated-formatter.test.d.ts +1 -0
  267. package/lib/tests/materialized-reports/materialized-discount-aggregated-formatter.test.js +173 -0
  268. package/lib/tests/materialized-reports/materialized-discount-classification.test.d.ts +1 -0
  269. package/lib/tests/materialized-reports/materialized-discount-classification.test.js +339 -0
  270. package/lib/tests/materialized-reports/materialized-discount-close-day.test.d.ts +1 -0
  271. package/lib/tests/materialized-reports/materialized-discount-close-day.test.js +506 -0
  272. package/lib/tests/materialized-reports/materialized-discount-compile.test.d.ts +1 -0
  273. package/lib/tests/materialized-reports/materialized-discount-compile.test.js +161 -0
  274. package/lib/tests/materialized-reports/materialized-discount-counter-rows.test.d.ts +1 -0
  275. package/lib/tests/materialized-reports/materialized-discount-counter-rows.test.js +107 -0
  276. package/lib/tests/materialized-reports/materialized-discount-entries-accumulation.test.d.ts +1 -0
  277. package/lib/tests/materialized-reports/materialized-discount-entries-accumulation.test.js +125 -0
  278. package/lib/tests/materialized-reports/materialized-hourly-comparison-compile.test.d.ts +1 -0
  279. package/lib/tests/materialized-reports/materialized-hourly-comparison-compile.test.js +146 -0
  280. package/lib/tests/materialized-reports/materialized-hourly-comparison-formatter.test.d.ts +1 -0
  281. package/lib/tests/materialized-reports/materialized-hourly-comparison-formatter.test.js +127 -0
  282. package/lib/tests/materialized-reports/materialized-hourly-comparison-plan.test.d.ts +1 -0
  283. package/lib/tests/materialized-reports/materialized-hourly-comparison-plan.test.js +138 -0
  284. package/lib/tests/materialized-reports/materialized-hourly-comparison-selection.test.d.ts +1 -0
  285. package/lib/tests/materialized-reports/materialized-hourly-comparison-selection.test.js +90 -0
  286. package/lib/tests/materialized-reports/materialized-payments-comparison-compile.test.d.ts +1 -0
  287. package/lib/tests/materialized-reports/materialized-payments-comparison-compile.test.js +164 -0
  288. package/lib/tests/materialized-reports/materialized-payments-comparison-formatter.test.d.ts +1 -0
  289. package/lib/tests/materialized-reports/materialized-payments-comparison-formatter.test.js +117 -0
  290. package/lib/tests/materialized-reports/materialized-payments-comparison-plan.test.d.ts +1 -0
  291. package/lib/tests/materialized-reports/materialized-payments-comparison-plan.test.js +149 -0
  292. package/lib/tests/materialized-reports/materialized-prep-time-classification.test.d.ts +1 -0
  293. package/lib/tests/materialized-reports/materialized-prep-time-classification.test.js +273 -0
  294. package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.d.ts +1 -0
  295. package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.js +270 -0
  296. package/lib/tests/materialized-reports/materialized-prep-time-compile.test.d.ts +1 -0
  297. package/lib/tests/materialized-reports/materialized-prep-time-compile.test.js +217 -0
  298. package/lib/tests/materialized-reports/materialized-prep-time-constants.test.d.ts +1 -0
  299. package/lib/tests/materialized-reports/materialized-prep-time-constants.test.js +84 -0
  300. package/lib/tests/materialized-reports/materialized-prep-time-plan.test.d.ts +1 -0
  301. package/lib/tests/materialized-reports/materialized-prep-time-plan.test.js +176 -0
  302. package/lib/tests/materialized-reports/materialized-sales-comparison-baseline.test.d.ts +1 -0
  303. package/lib/tests/materialized-reports/materialized-sales-comparison-baseline.test.js +126 -0
  304. package/lib/tests/materialized-reports/materialized-sales-comparison-compile.test.d.ts +1 -0
  305. package/lib/tests/materialized-reports/materialized-sales-comparison-compile.test.js +206 -0
  306. package/lib/tests/materialized-reports/materialized-sales-comparison-formatter.test.d.ts +1 -0
  307. package/lib/tests/materialized-reports/materialized-sales-comparison-formatter.test.js +179 -0
  308. package/lib/tests/materialized-reports/materialized-sales-comparison-parity.test.d.ts +1 -0
  309. package/lib/tests/materialized-reports/materialized-sales-comparison-parity.test.js +196 -0
  310. package/lib/tests/materialized-reports/materialized-sales-comparison-plan.test.d.ts +1 -0
  311. package/lib/tests/materialized-reports/materialized-sales-comparison-plan.test.js +241 -0
  312. package/lib/tests/materialized-reports/materialized-tenant-hourly-comparison-schema.test.d.ts +1 -0
  313. package/lib/tests/materialized-reports/materialized-tenant-hourly-comparison-schema.test.js +96 -0
  314. package/lib/tests/materialized-reports/materialized-tenant-payments-comparison-schema.test.d.ts +1 -0
  315. package/lib/tests/materialized-reports/materialized-tenant-payments-comparison-schema.test.js +112 -0
  316. package/lib/tests/materialized-reports/materialized-tenant-sales-comparison-schema.test.d.ts +1 -0
  317. package/lib/tests/materialized-reports/materialized-tenant-sales-comparison-schema.test.js +142 -0
  318. package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +8 -1
  319. package/lib/tests/materialized-reports/sales-comparison-reference.d.ts +55 -0
  320. package/lib/tests/materialized-reports/sales-comparison-reference.js +86 -0
  321. package/lib/tests/materialized-reports/tenant-discount-formatter.test.d.ts +1 -0
  322. package/lib/tests/materialized-reports/tenant-discount-formatter.test.js +289 -0
  323. package/lib/tests/materialized-reports/tenant-prep-time-formatter.test.d.ts +1 -0
  324. package/lib/tests/materialized-reports/tenant-prep-time-formatter.test.js +354 -0
  325. package/lib/tests/reporting-contract.test.d.ts +1 -0
  326. package/lib/tests/reporting-contract.test.js +267 -0
  327. package/lib/tests/rider-daily-report.test.d.ts +1 -0
  328. package/lib/tests/rider-daily-report.test.js +86 -0
  329. package/lib/tests/rider-earnings.test.d.ts +1 -0
  330. package/lib/tests/rider-earnings.test.js +65 -0
  331. package/lib/tests/rider-report.test.d.ts +1 -0
  332. package/lib/tests/rider-report.test.js +98 -0
  333. package/package.json +10 -1
@@ -0,0 +1,603 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.isMaterializedSalesComparisonDayScope = exports.MATERIALIZED_SALES_COMPARISON_PERIOD_SCOPES = void 0;
7
+ exports.getMaterializedSalesComparisonScopeRefsForDay = getMaterializedSalesComparisonScopeRefsForDay;
8
+ exports.getMaterializedSalesComparisonPeriodDayKeys = getMaterializedSalesComparisonPeriodDayKeys;
9
+ exports.foldMaterializedSalesComparisonPeriodCounters = foldMaterializedSalesComparisonPeriodCounters;
10
+ exports.mergeMaterializedSalesComparisonEntrySections = mergeMaterializedSalesComparisonEntrySections;
11
+ exports.foldMaterializedSalesComparisonPeriodEntries = foldMaterializedSalesComparisonPeriodEntries;
12
+ exports.needsMaterializedSalesComparisonReportWrite = needsMaterializedSalesComparisonReportWrite;
13
+ exports.needsMaterializedSalesComparisonEntriesWrite = needsMaterializedSalesComparisonEntriesWrite;
14
+ exports.buildTenantSalesComparisonCloseDayPlan = buildTenantSalesComparisonCloseDayPlan;
15
+ const mongoose_1 = __importDefault(require("mongoose"));
16
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
17
+ const materialized_tenant_reports_iterations_helpers_1 = require("./materialized-tenant-reports-iterations-helpers");
18
+ const scope_helpers_1 = require("./scope-helpers");
19
+ const materialized_sales_comparison_accumulator_helpers_1 = require("./materialized-sales-comparison-accumulator-helpers");
20
+ /**
21
+ * Sales Comparison close-day helpers.
22
+ *
23
+ * ## Period counters are RE-FOLDED, never patched
24
+ *
25
+ * The discount report accumulates period deltas forward and subtracts a day
26
+ * delta on re-close. That design produced defects 4–6 in the handoff (emptied
27
+ * days, corrections that REMOVE rows, and a missing period document rebuilt
28
+ * from one day). This report mirrors CASH instead: every touched period is
29
+ * rebuilt from its whole day set, so a correction cannot leave residue and a
30
+ * missing period document is rebuilt from the days still on hand.
31
+ *
32
+ * ## The watermark comes from the FOLDED DAYS
33
+ *
34
+ * A folded document that inherited the STORED document watermark would look
35
+ * unchanged to the write guard, so the write would be skipped forever and the
36
+ * period would freeze at its first closed day. This bug shipped once in the
37
+ * prep time report; it is designed out here from the start.
38
+ *
39
+ * ## Close-day fetch contract — UNDER-FETCHING IS SILENT
40
+ *
41
+ * `existing` / `existingEntries` must contain:
42
+ * 1. the closing day's own range document (and the period documents it rolls
43
+ * into), and
44
+ * 2. **every stored `localDate` document inside those periods** — the sibling
45
+ * days are the fold's input. `getMaterializedSalesComparisonPeriodDayKeys`
46
+ * returns exactly the day keys a caller must fetch (`scope_field: { $in }`).
47
+ *
48
+ * Passing only (1) still handles emptied days and corrections on the closing
49
+ * day, but a period document is then rebuilt from the closing day alone and
50
+ * every sibling day silently disappears. That is why (2) is part of the
51
+ * contract, exactly like the discount entries contract.
52
+ *
53
+ * Helpers never throw — invalid input returns `null`.
54
+ */
55
+ /** Period scopes this report materializes (everything except the day scope). */
56
+ exports.MATERIALIZED_SALES_COMPARISON_PERIOD_SCOPES = MATERIALIZED_REPORTS_1.MATERIALIZED_SALES_COMPARISON_REPORT_SCOPES.filter((scope) => scope !== MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE);
57
+ /** True when the scope is the plain day scope. */
58
+ const isMaterializedSalesComparisonDayScope = (scopeContext) => String(scopeContext) === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE;
59
+ exports.isMaterializedSalesComparisonDayScope = isMaterializedSalesComparisonDayScope;
60
+ const scopeKey = (doc) => String(doc.scope_context) + '|' + String(doc.scope_field);
61
+ /**
62
+ * The stored scope references one settled day's plan needs: its `localDate`
63
+ * document plus the five period documents it rolls into. NO hour scope — this
64
+ * report has none.
65
+ */
66
+ function getMaterializedSalesComparisonScopeRefsForDay(localDate) {
67
+ const periodFields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(localDate);
68
+ if (periodFields == null)
69
+ return null;
70
+ const refs = [
71
+ {
72
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
73
+ scope_field: localDate,
74
+ },
75
+ ];
76
+ for (const scope of exports.MATERIALIZED_SALES_COMPARISON_PERIOD_SCOPES) {
77
+ const field = periodFields[scope];
78
+ if (field == null || field === '')
79
+ return null;
80
+ refs.push({ scope_context: scope, scope_field: field });
81
+ }
82
+ return refs;
83
+ }
84
+ /* ------------------------------------------------------------------------ */
85
+ /* Period day-key ranges */
86
+ /* ------------------------------------------------------------------------ */
87
+ const KEY_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
88
+ const keyToUtcDate = (key) => {
89
+ if (typeof key !== 'string' || !KEY_PATTERN.test(key))
90
+ return null;
91
+ const [year, month, day] = key.split('-').map(Number);
92
+ const date = new Date(Date.UTC(year, month - 1, day));
93
+ if (date.getUTCFullYear() !== year || date.getUTCMonth() !== month - 1 || date.getUTCDate() !== day) {
94
+ return null;
95
+ }
96
+ return date;
97
+ };
98
+ const utcDateToKey = (date) => date.toISOString().slice(0, 10);
99
+ const addUtcDays = (date, amount) => new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate() + amount));
100
+ /** Inclusive [start, end] day keys of one period scope value. */
101
+ function periodDayRange(periodScope, periodField) {
102
+ switch (periodScope) {
103
+ case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK: {
104
+ const startDate = keyToUtcDate(periodField);
105
+ if (startDate == null)
106
+ return null;
107
+ return { start: periodField, end: utcDateToKey(addUtcDays(startDate, 6)) };
108
+ }
109
+ case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH: {
110
+ const match = /^(\d{4})-(\d{2})$/.exec(periodField);
111
+ if (match == null)
112
+ return null;
113
+ const year = Number(match[1]);
114
+ const month = Number(match[2]);
115
+ if (month < 1 || month > 12)
116
+ return null;
117
+ const first = new Date(Date.UTC(year, month - 1, 1));
118
+ const last = new Date(Date.UTC(year, month, 0));
119
+ return { start: utcDateToKey(first), end: utcDateToKey(last) };
120
+ }
121
+ case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_QUARTER: {
122
+ const match = /^(\d{4})-Q([1-4])$/.exec(periodField);
123
+ if (match == null)
124
+ return null;
125
+ const year = Number(match[1]);
126
+ const quarter = Number(match[2]);
127
+ const first = new Date(Date.UTC(year, (quarter - 1) * 3, 1));
128
+ const last = new Date(Date.UTC(year, quarter * 3, 0));
129
+ return { start: utcDateToKey(first), end: utcDateToKey(last) };
130
+ }
131
+ case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_SEMESTER: {
132
+ const match = /^(\d{4})-S([12])$/.exec(periodField);
133
+ if (match == null)
134
+ return null;
135
+ const year = Number(match[1]);
136
+ const semester = Number(match[2]);
137
+ const first = new Date(Date.UTC(year, semester === 1 ? 0 : 6, 1));
138
+ const last = new Date(Date.UTC(year, semester === 1 ? 6 : 12, 0));
139
+ return { start: utcDateToKey(first), end: utcDateToKey(last) };
140
+ }
141
+ case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_YEAR: {
142
+ const match = /^(\d{4})$/.exec(periodField);
143
+ if (match == null)
144
+ return null;
145
+ const year = Number(match[1]);
146
+ return { start: `${year}-01-01`, end: `${year}-12-31` };
147
+ }
148
+ default:
149
+ return null;
150
+ }
151
+ }
152
+ /**
153
+ * Every `localDate` scope value a caller must fetch to re-fold the periods one
154
+ * settled day touches — the union of the day's week/month/quarter/semester/year
155
+ * ranges (≤ 366 keys). Sorted, deduped.
156
+ */
157
+ function getMaterializedSalesComparisonPeriodDayKeys(localDate) {
158
+ const periodFields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(localDate);
159
+ if (periodFields == null)
160
+ return null;
161
+ const keys = new Set();
162
+ for (const scope of exports.MATERIALIZED_SALES_COMPARISON_PERIOD_SCOPES) {
163
+ const field = periodFields[scope];
164
+ if (field == null || field === '')
165
+ return null;
166
+ const range = periodDayRange(scope, field);
167
+ if (range == null)
168
+ return null;
169
+ const start = keyToUtcDate(range.start);
170
+ const end = keyToUtcDate(range.end);
171
+ if (start == null || end == null)
172
+ return null;
173
+ for (let cursor = start; cursor.getTime() <= end.getTime(); cursor = addUtcDays(cursor, 1)) {
174
+ keys.add(utcDateToKey(cursor));
175
+ }
176
+ }
177
+ return [...keys].sort();
178
+ }
179
+ /* ------------------------------------------------------------------------ */
180
+ /* Folds */
181
+ /* ------------------------------------------------------------------------ */
182
+ /** Highest `source_watermark` across the day documents a fold actually used. */
183
+ function watermarkOfFoldedDays(dayDocuments, fallback) {
184
+ let latest = fallback.getTime();
185
+ for (const doc of dayDocuments ?? []) {
186
+ const time = new Date(doc?.source_watermark ?? 0).getTime();
187
+ if (!Number.isNaN(time) && time > latest)
188
+ latest = time;
189
+ }
190
+ return new Date(latest);
191
+ }
192
+ /**
193
+ * Folds the counters of every DAY document that belongs to one period scope.
194
+ * Pure; the caller supplies the whole day set.
195
+ */
196
+ function foldMaterializedSalesComparisonPeriodCounters(input) {
197
+ const folded = {};
198
+ for (const dayDocument of input?.dayDocuments ?? []) {
199
+ if (!(0, exports.isMaterializedSalesComparisonDayScope)(String(dayDocument?.scope_context)))
200
+ continue;
201
+ const fields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(String(dayDocument.scope_field));
202
+ if (fields == null)
203
+ continue;
204
+ if (fields[String(input.periodScope)] !== input.periodScopeField)
205
+ continue;
206
+ for (const [key, block] of Object.entries(dayDocument.sales_counters ?? {})) {
207
+ const target = folded[key] ?? (0, materialized_sales_comparison_accumulator_helpers_1.createEmptySalesComparisonCounters)();
208
+ (0, materialized_sales_comparison_accumulator_helpers_1.addSalesComparisonCounters)(target, block);
209
+ folded[key] = target;
210
+ }
211
+ }
212
+ return folded;
213
+ }
214
+ /** Newest-first row order, then stable id order. */
215
+ function compareRowsDescending(a, b) {
216
+ const byTime = new Date(b.time).getTime() - new Date(a.time).getTime();
217
+ if (byTime !== 0)
218
+ return byTime;
219
+ // Same instant: `order_id` ASC, matching the page cursor.
220
+ return a.order_id < b.order_id ? -1 : a.order_id > b.order_id ? 1 : 0;
221
+ }
222
+ /**
223
+ * Merges two entry sections, de-duplicating rows by `order_id`.
224
+ *
225
+ * A row belongs to exactly one settled day, so the same order id twice means
226
+ * the fold saw it twice; money and rows must never double-count.
227
+ */
228
+ function mergeMaterializedSalesComparisonEntrySections(target, source) {
229
+ const rowsByKey = new Map();
230
+ const seenByKey = new Map();
231
+ const absorb = (input) => {
232
+ if (input == null)
233
+ return;
234
+ for (const [key, section] of Object.entries(input)) {
235
+ const rows = rowsByKey.get(key) ?? [];
236
+ const seen = seenByKey.get(key) ?? new Set();
237
+ for (const row of section?.rows ?? []) {
238
+ const identity = String(row.order_id ?? '');
239
+ if (identity !== '') {
240
+ if (seen.has(identity))
241
+ continue;
242
+ seen.add(identity);
243
+ }
244
+ rows.push(row);
245
+ }
246
+ rowsByKey.set(key, rows);
247
+ seenByKey.set(key, seen);
248
+ }
249
+ };
250
+ absorb(target);
251
+ absorb(source);
252
+ const out = {};
253
+ for (const [key, rows] of rowsByKey) {
254
+ rows.sort(compareRowsDescending);
255
+ out[key] = { rows };
256
+ }
257
+ return out;
258
+ }
259
+ /**
260
+ * Folds the itemized rows of every DAY entry document that belongs to one
261
+ * period scope, de-duplicated by `order_id`.
262
+ */
263
+ function foldMaterializedSalesComparisonPeriodEntries(input) {
264
+ let merged = {};
265
+ for (const dayDocument of input?.dayDocuments ?? []) {
266
+ if (!(0, exports.isMaterializedSalesComparisonDayScope)(String(dayDocument?.scope_context)))
267
+ continue;
268
+ const fields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(String(dayDocument.scope_field));
269
+ if (fields == null)
270
+ continue;
271
+ if (fields[String(input.periodScope)] !== input.periodScopeField)
272
+ continue;
273
+ merged = mergeMaterializedSalesComparisonEntrySections(merged, dayDocument.sales_entries ?? null);
274
+ }
275
+ return merged;
276
+ }
277
+ /* ------------------------------------------------------------------------ */
278
+ /* Write guards */
279
+ /* ------------------------------------------------------------------------ */
280
+ /**
281
+ * Canonical JSON of a counter/row tree (object keys sorted) so a content
282
+ * comparison cannot be fooled by insertion order — e.g. a document reloaded
283
+ * from Mongo with its map keys in a different order.
284
+ */
285
+ function stableJson(value) {
286
+ const canonicalize = (input) => {
287
+ if (Array.isArray(input))
288
+ return input.map(canonicalize);
289
+ if (input != null && typeof input === 'object') {
290
+ const out = {};
291
+ for (const key of Object.keys(input).sort()) {
292
+ out[key] = canonicalize(input[key]);
293
+ }
294
+ return out;
295
+ }
296
+ return input;
297
+ };
298
+ return JSON.stringify(canonicalize(value ?? null));
299
+ }
300
+ /**
301
+ * Write guard: does the stored document need a structural write?
302
+ *
303
+ * - `force` bypasses the guard (repairs / schema rollouts);
304
+ * - a schema-version or key-grammar change forces the write;
305
+ * - a NEWER watermark forces the write;
306
+ * - an OLDER watermark is a stale replay and is skipped;
307
+ * - an EQUAL watermark still writes when the CONTENT differs. This is the
308
+ * correction case: removing an order can leave the day's max `updatedAt`
309
+ * unchanged, and a watermark-only guard would then leave the period at its
310
+ * pre-correction figures forever.
311
+ */
312
+ function needsMaterializedSalesComparisonReportWrite(existing, incoming, force = false) {
313
+ if (existing == null || incoming == null)
314
+ return true;
315
+ if (force)
316
+ return true;
317
+ if (Number(existing.schema_version) !== Number(incoming.schema_version) ||
318
+ Number.isNaN(Number(incoming.schema_version))) {
319
+ return true;
320
+ }
321
+ if ((existing.sale_type_key_filters ?? []).join(':') !== (incoming.sale_type_key_filters ?? []).join(':')) {
322
+ return true;
323
+ }
324
+ const stored = new Date(existing.source_watermark ?? 0).getTime();
325
+ const next = new Date(incoming.source_watermark ?? 0).getTime();
326
+ if (Number.isNaN(stored) || Number.isNaN(next))
327
+ return true;
328
+ if (next > stored)
329
+ return true;
330
+ if (next < stored)
331
+ return false;
332
+ return stableJson(existing.sales_counters) !== stableJson(incoming.sales_counters);
333
+ }
334
+ /** True when the stored entry document already carries these exact rows. */
335
+ function needsMaterializedSalesComparisonEntriesWrite(existing, incoming, force = false) {
336
+ if (existing == null || incoming == null)
337
+ return true;
338
+ if (force)
339
+ return true;
340
+ if (Number(existing.schema_version) !== Number(incoming.schema_version) ||
341
+ Number.isNaN(Number(incoming.schema_version))) {
342
+ return true;
343
+ }
344
+ if ((existing.sale_type_key_filters ?? []).join(':') !== (incoming.sale_type_key_filters ?? []).join(':')) {
345
+ return true;
346
+ }
347
+ const stored = new Date(existing.source_watermark ?? 0).getTime();
348
+ const next = new Date(incoming.source_watermark ?? 0).getTime();
349
+ if (Number.isNaN(stored) || Number.isNaN(next))
350
+ return true;
351
+ if (next > stored)
352
+ return true;
353
+ if (next < stored)
354
+ return false;
355
+ return stableJson(existing.sales_entries) !== stableJson(incoming.sales_entries);
356
+ }
357
+ /**
358
+ * Builds the close-day plan for ONE settled tenant-day.
359
+ *
360
+ * The DAY scope is written from the compile. Every touched PERIOD scope is
361
+ * re-folded from its whole day set (stored siblings + the freshly compiled
362
+ * day). Returns `null` on malformed input.
363
+ */
364
+ function buildTenantSalesComparisonCloseDayPlan(input) {
365
+ if (input == null)
366
+ return null;
367
+ const { tenantId, reason } = input;
368
+ if (typeof tenantId !== 'string' || tenantId.length === 0)
369
+ return null;
370
+ if (typeof reason !== 'string' || reason.length === 0)
371
+ return null;
372
+ const computedAt = input.computedAt ?? new Date();
373
+ if (Number.isNaN(computedAt.getTime()))
374
+ return null;
375
+ const compiledReports = (input.reports ?? []).filter((doc) => doc != null && String(doc.tenant_id) === tenantId);
376
+ const compiledEntries = (input.entries ?? []).filter((doc) => doc != null && String(doc.tenant_id) === tenantId);
377
+ const storedReports = (input.existing ?? []).filter((doc) => doc != null && String(doc.tenant_id) === tenantId);
378
+ const storedEntries = (input.existingEntries ?? []).filter((doc) => doc != null && String(doc.tenant_id) === tenantId);
379
+ const compiledDateDoc = compiledReports.find((doc) => (0, exports.isMaterializedSalesComparisonDayScope)(String(doc.scope_context)));
380
+ const compiledDateEntries = compiledEntries.find((doc) => (0, exports.isMaterializedSalesComparisonDayScope)(String(doc.scope_context)));
381
+ const localDate = input.localDate ?? compiledDateDoc?.scope_field ?? null;
382
+ if (localDate == null || localDate === '' || getMaterializedSalesComparisonScopeRefsForDay(localDate) == null) {
383
+ return null;
384
+ }
385
+ const writes = [];
386
+ const entryWrites = [];
387
+ const staleDocuments = [];
388
+ const staleEntryDocuments = [];
389
+ const skippedDocuments = [];
390
+ const skippedEntryDocuments = [];
391
+ const createdPeriodDocuments = [];
392
+ const storedReportsByScope = new Map();
393
+ for (const doc of storedReports)
394
+ storedReportsByScope.set(scopeKey(doc), doc);
395
+ const storedEntriesByScope = new Map();
396
+ for (const doc of storedEntries)
397
+ storedEntriesByScope.set(scopeKey(doc), doc);
398
+ const addWrite = (report) => {
399
+ const persisted = report.iteration == null ? { ...report, iteration: 1 } : report;
400
+ const iteration = (0, materialized_tenant_reports_iterations_helpers_1.buildMaterializedTenantReportIteration)(persisted, {
401
+ reportType: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_COMPARISON_REPORTS,
402
+ reason,
403
+ computedAt,
404
+ });
405
+ if (iteration == null)
406
+ return false;
407
+ writes.push({ report: persisted, iteration: iteration });
408
+ return true;
409
+ };
410
+ const addEntryWrite = (report) => {
411
+ const persisted = report.iteration == null ? { ...report, iteration: 1 } : report;
412
+ const iteration = (0, materialized_tenant_reports_iterations_helpers_1.buildMaterializedTenantReportIteration)(persisted, {
413
+ reportType: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_COMPARISON_ENTRIES,
414
+ reason,
415
+ computedAt,
416
+ });
417
+ if (iteration == null)
418
+ return false;
419
+ entryWrites.push({ report: persisted, iteration: iteration });
420
+ return true;
421
+ };
422
+ const storedDateDoc = storedReportsByScope.get(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE + '|' + localDate);
423
+ const storedDateEntries = storedEntriesByScope.get(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE + '|' + localDate);
424
+ // ---- the DAY scope, straight from the compile ----
425
+ let effectiveDayDoc = null;
426
+ if (compiledDateDoc != null) {
427
+ const next = {
428
+ ...compiledDateDoc,
429
+ _id: storedDateDoc?._id ?? compiledDateDoc._id ?? new mongoose_1.default.Types.ObjectId().toHexString(),
430
+ iteration: Number(storedDateDoc?.iteration ?? 0) + 1,
431
+ };
432
+ if (needsMaterializedSalesComparisonReportWrite(storedDateDoc, next, input.force === true)) {
433
+ if (addWrite(next))
434
+ effectiveDayDoc = next;
435
+ }
436
+ else if (storedDateDoc != null) {
437
+ skippedDocuments.push(storedDateDoc);
438
+ effectiveDayDoc = storedDateDoc;
439
+ }
440
+ else {
441
+ effectiveDayDoc = next;
442
+ }
443
+ }
444
+ else if (storedDateDoc != null) {
445
+ // A day that no longer has settled orders is retired; the period re-fold
446
+ // below removes its contribution.
447
+ staleDocuments.push(storedDateDoc);
448
+ }
449
+ let effectiveDayEntries = null;
450
+ if (compiledDateEntries != null) {
451
+ const next = {
452
+ ...compiledDateEntries,
453
+ _id: storedDateEntries?._id ?? compiledDateEntries._id ?? new mongoose_1.default.Types.ObjectId().toHexString(),
454
+ iteration: Number(storedDateEntries?.iteration ?? 0) + 1,
455
+ };
456
+ if (needsMaterializedSalesComparisonEntriesWrite(storedDateEntries, next, input.force === true)) {
457
+ if (addEntryWrite(next))
458
+ effectiveDayEntries = next;
459
+ }
460
+ else if (storedDateEntries != null) {
461
+ skippedEntryDocuments.push(storedDateEntries);
462
+ effectiveDayEntries = storedDateEntries;
463
+ }
464
+ else {
465
+ effectiveDayEntries = next;
466
+ }
467
+ }
468
+ else if (storedDateEntries != null) {
469
+ staleEntryDocuments.push(storedDateEntries);
470
+ }
471
+ // ---- the fold's day set: every OTHER stored day + this compiled day ----
472
+ const nextDayDocs = storedReports.filter((doc) => (0, exports.isMaterializedSalesComparisonDayScope)(String(doc.scope_context)) && String(doc.scope_field) !== localDate);
473
+ if (effectiveDayDoc != null)
474
+ nextDayDocs.push(effectiveDayDoc);
475
+ const nextDayEntryDocs = storedEntries.filter((doc) => (0, exports.isMaterializedSalesComparisonDayScope)(String(doc.scope_context)) && String(doc.scope_field) !== localDate);
476
+ if (effectiveDayEntries != null)
477
+ nextDayEntryDocs.push(effectiveDayEntries);
478
+ // ---- touched periods (the closing day's; one day cannot touch others) ----
479
+ const periodFields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(localDate);
480
+ if (periodFields == null)
481
+ return null;
482
+ const touchedPeriods = [];
483
+ for (const scope of exports.MATERIALIZED_SALES_COMPARISON_PERIOD_SCOPES) {
484
+ const field = periodFields[scope];
485
+ if (field == null || field === '')
486
+ return null;
487
+ touchedPeriods.push({ scope_context: scope, scope_field: field });
488
+ }
489
+ for (const period of touchedPeriods) {
490
+ const key = scopeKey(period);
491
+ const existing = storedReportsByScope.get(key);
492
+ const folded = foldMaterializedSalesComparisonPeriodCounters({
493
+ periodScope: period.scope_context,
494
+ periodScopeField: period.scope_field,
495
+ dayDocuments: nextDayDocs,
496
+ });
497
+ const keys = Object.keys(folded).sort();
498
+ if (keys.length === 0) {
499
+ if (existing != null && !staleDocuments.includes(existing)) {
500
+ staleDocuments.push(existing);
501
+ }
502
+ continue;
503
+ }
504
+ const salesCounters = {};
505
+ const counterRows = [];
506
+ for (const counterKey of keys) {
507
+ salesCounters[counterKey] = folded[counterKey];
508
+ counterRows.push({ key: counterKey, counters: folded[counterKey] });
509
+ }
510
+ const contributingDays = nextDayDocs.filter((doc) => {
511
+ const fields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(String(doc.scope_field));
512
+ return fields != null && fields[period.scope_context] === period.scope_field;
513
+ });
514
+ const report = {
515
+ _id: existing?._id ?? new mongoose_1.default.Types.ObjectId().toHexString(),
516
+ tenant_id: tenantId,
517
+ brand_id: existing?.brand_id ?? effectiveDayDoc?.brand_id ?? compiledDateDoc?.brand_id ?? '',
518
+ scope_context: period.scope_context,
519
+ scope_field: period.scope_field,
520
+ day_index: null,
521
+ sale_type_key_filters: existing?.sale_type_key_filters ?? [...MATERIALIZED_REPORTS_1.SALES_COMPARISON_KEY_FILTERS],
522
+ sales_counters: salesCounters,
523
+ sales_counter_rows: counterRows,
524
+ iteration: existing?.iteration,
525
+ schema_version: existing?.schema_version ?? MATERIALIZED_REPORTS_1.MATERIALIZED_TENANT_SALES_COMPARISON_REPORTS_SCHEMA_VERSION,
526
+ // FROM THE FOLDED DAYS - never the stored watermark, or the write would
527
+ // be skipped forever and the period would freeze at its first closed day.
528
+ source_watermark: watermarkOfFoldedDays(contributingDays, existing?.source_watermark ?? computedAt),
529
+ computed_at: computedAt,
530
+ createdAt: existing?.createdAt ?? computedAt,
531
+ updatedAt: computedAt,
532
+ };
533
+ const next = {
534
+ ...report,
535
+ iteration: Number(existing?.iteration ?? 0) + 1,
536
+ };
537
+ if (needsMaterializedSalesComparisonReportWrite(existing, next, input.force === true)) {
538
+ if (addWrite(next) && existing == null) {
539
+ createdPeriodDocuments.push({
540
+ scope_context: period.scope_context,
541
+ scope_field: period.scope_field,
542
+ });
543
+ }
544
+ }
545
+ else if (existing != null) {
546
+ skippedDocuments.push(existing);
547
+ }
548
+ }
549
+ for (const period of touchedPeriods) {
550
+ const key = scopeKey(period);
551
+ const existing = storedEntriesByScope.get(key);
552
+ const sections = foldMaterializedSalesComparisonPeriodEntries({
553
+ periodScope: period.scope_context,
554
+ periodScopeField: period.scope_field,
555
+ dayDocuments: nextDayEntryDocs,
556
+ });
557
+ const keys = Object.keys(sections);
558
+ if (keys.length === 0) {
559
+ if (existing != null)
560
+ staleEntryDocuments.push(existing);
561
+ continue;
562
+ }
563
+ const contributingDays = nextDayEntryDocs.filter((doc) => {
564
+ const fields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(String(doc.scope_field));
565
+ return fields != null && fields[period.scope_context] === period.scope_field;
566
+ });
567
+ const report = {
568
+ _id: existing?._id ?? new mongoose_1.default.Types.ObjectId().toHexString(),
569
+ tenant_id: tenantId,
570
+ brand_id: existing?.brand_id ?? effectiveDayEntries?.brand_id ?? compiledDateEntries?.brand_id ?? '',
571
+ scope_context: period.scope_context,
572
+ scope_field: period.scope_field,
573
+ day_index: null,
574
+ sale_type_key_filters: existing?.sale_type_key_filters ?? [...MATERIALIZED_REPORTS_1.SALES_COMPARISON_KEY_FILTERS],
575
+ sales_entries: sections,
576
+ iteration: existing?.iteration,
577
+ schema_version: existing?.schema_version ?? MATERIALIZED_REPORTS_1.MATERIALIZED_TENANT_SALES_COMPARISON_ENTRIES_SCHEMA_VERSION,
578
+ source_watermark: watermarkOfFoldedDays(contributingDays, existing?.source_watermark ?? computedAt),
579
+ computed_at: computedAt,
580
+ createdAt: existing?.createdAt ?? computedAt,
581
+ updatedAt: computedAt,
582
+ };
583
+ const next = {
584
+ ...report,
585
+ iteration: Number(existing?.iteration ?? 0) + 1,
586
+ };
587
+ if (needsMaterializedSalesComparisonEntriesWrite(existing, next, input.force === true)) {
588
+ addEntryWrite(next);
589
+ }
590
+ else if (existing != null) {
591
+ skippedEntryDocuments.push(existing);
592
+ }
593
+ }
594
+ return {
595
+ writes,
596
+ entryWrites,
597
+ staleDocuments,
598
+ staleEntryDocuments,
599
+ skippedDocuments,
600
+ skippedEntryDocuments,
601
+ createdPeriodDocuments,
602
+ };
603
+ }
@@ -0,0 +1,68 @@
1
+ import { MATERIALIZED_REPORT_SCOPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
2
+ import { IOrder } from '../../schemas/types/orders';
3
+ import { IMaterializedTenantSalesComparisonEntries } from '../../schemas/types/materialized_tenant_sales_comparison_entries';
4
+ import { IMaterializedTenantSalesComparisonReport } from '../../schemas/types/materialized_tenant_sales_comparison_reports';
5
+ /**
6
+ * Sales Comparison compile — one tenant-day of orders into the day's
7
+ * `materialized_tenant_sales_comparison_reports` document and its
8
+ * `materialized_tenant_sales_comparison_entries` row document.
9
+ *
10
+ * Mirrors the CASH compile, not the discount one: this report RE-FOLDS its
11
+ * period documents from day documents in the close-day plan, so the compile
12
+ * only ever produces the `localDate` pair. (The discount report accumulates
13
+ * period deltas and therefore compiles periods itself; that difference is the
14
+ * defect class behind discount defects 4–6 and must not be copied here.)
15
+ *
16
+ * ## Anchor — the SETTLED day, never the punch day
17
+ *
18
+ * An order belongs to the tenant-local calendar date of `order_settled_at`
19
+ * (concept §3). Orders whose settled day is not the day being compiled are
20
+ * skipped (the caller stages the right day), and orders that were never
21
+ * settled contribute nothing.
22
+ *
23
+ * ## Two rules this function exists to enforce
24
+ *
25
+ * 1. **The comparison is never stored.** These documents hold ONE period's
26
+ * truth; `current` and `comparison` are ordinary reads subtracted later.
27
+ * 2. **Counters and rows come from ONE pass**, so a document's map and its
28
+ * read-path row array can never disagree, and the entries can never list a
29
+ * different order set than the counters.
30
+ *
31
+ * Pure and total: `null` on malformed input, never a throw.
32
+ */
33
+ export interface ICompileTenantSalesComparisonReportsInput {
34
+ tenantId: string;
35
+ brandId: string;
36
+ /** The compiled settled day, tenant-local `'YYYY-MM-DD'`. */
37
+ localDate: string;
38
+ /** Orders staged for this day (the close's fetch window). */
39
+ orders: IOrder[];
40
+ /**
41
+ * Tenant GST offset in hours (`+5`) used to derive the SETTLED local date.
42
+ * Defaults to 0 (UTC) only for synthetic callers; a real close MUST pass it.
43
+ */
44
+ gmtOffset?: number;
45
+ computedAt: Date;
46
+ }
47
+ export interface ICompileTenantSalesComparisonReportsResult {
48
+ reports: IMaterializedTenantSalesComparisonReport[];
49
+ entries: IMaterializedTenantSalesComparisonEntries[];
50
+ /** Orders that produced a counter row. */
51
+ contributingOrders: number;
52
+ /** Orders skipped because they were never settled. */
53
+ skippedUnsettledOrders: number;
54
+ /** Orders skipped because their settled day is not `localDate`. */
55
+ skippedForeignOrders: number;
56
+ /** Orders skipped because their tenant/status/platform could not be keyed. */
57
+ skippedUnkeyableOrders: number;
58
+ }
59
+ /**
60
+ * Compiles one settled tenant-day.
61
+ *
62
+ * Returns EMPTY arrays (never null) when the day has no settled orders: an
63
+ * emptied day must produce no day document, and the close-day plan deletes the
64
+ * stored one.
65
+ */
66
+ export declare function compileTenantSalesComparisonReports(input: ICompileTenantSalesComparisonReportsInput | null | undefined): ICompileTenantSalesComparisonReportsResult | null;
67
+ /** The scopes this report materializes — exported for the close plan + tests. */
68
+ export declare const MATERIALIZED_SALES_COMPARISON_COMPILED_SCOPES: MATERIALIZED_REPORT_SCOPE_ENUM[];