@managesome/eposmatic-toolkit 0.4.8 → 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 (336) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +448 -0
  2. package/esm/constants/enums/communications.js +1 -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 +286 -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 +244 -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 +426 -0
  11. package/esm/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +372 -0
  12. package/esm/helpers/materialized-reports/materialized-delivery-compile-helpers.js +214 -0
  13. package/esm/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +318 -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 +111 -0
  20. package/esm/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +88 -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 +476 -0
  25. package/esm/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +201 -0
  26. package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +7 -0
  27. package/esm/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.js +189 -0
  28. package/esm/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.js +157 -0
  29. package/esm/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.js +588 -0
  30. package/esm/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.js +175 -0
  31. package/esm/helpers/materialized-reports/tenant-cash-in-out-formatter.js +156 -0
  32. package/esm/helpers/materialized-reports/tenant-delivery-formatter.js +230 -0
  33. package/esm/helpers/materialized-reports/tenant-discount-formatter.js +427 -0
  34. package/esm/helpers/materialized-reports/tenant-hourly-comparison-formatter.js +237 -0
  35. package/esm/helpers/materialized-reports/tenant-payments-comparison-formatter.js +279 -0
  36. package/esm/helpers/materialized-reports/tenant-prep-time-formatter.js +228 -0
  37. package/esm/helpers/materialized-reports/tenant-sales-comparison-formatter.js +343 -0
  38. package/esm/helpers/reporting-contract/index.js +2 -0
  39. package/esm/helpers/reporting-contract/overview-contract.js +122 -0
  40. package/esm/helpers/reporting-contract/report-contract.js +228 -0
  41. package/esm/helpers/rider-earnings/index.js +1 -0
  42. package/esm/helpers/rider-earnings/rider-earnings.js +59 -0
  43. package/esm/helpers/rider-reports/index.js +2 -0
  44. package/esm/helpers/rider-reports/rider-daily-report.js +90 -0
  45. package/esm/helpers/rider-reports/rider-report.js +67 -0
  46. package/esm/schemas/models/deliveries.js +75 -0
  47. package/esm/schemas/models/index.js +15 -0
  48. package/esm/schemas/models/materialized_rider_daily_reports.js +39 -0
  49. package/esm/schemas/models/materialized_tenant_cash_entries.js +95 -0
  50. package/esm/schemas/models/materialized_tenant_cash_reports.js +77 -0
  51. package/esm/schemas/models/materialized_tenant_delivery_entries.js +63 -0
  52. package/esm/schemas/models/materialized_tenant_delivery_reports.js +63 -0
  53. package/esm/schemas/models/materialized_tenant_discount_entries.js +156 -0
  54. package/esm/schemas/models/materialized_tenant_discount_reports.js +333 -0
  55. package/esm/schemas/models/materialized_tenant_hourly_comparison_entries.js +87 -0
  56. package/esm/schemas/models/materialized_tenant_hourly_comparison_reports.js +99 -0
  57. package/esm/schemas/models/materialized_tenant_payments_comparison_entries.js +71 -0
  58. package/esm/schemas/models/materialized_tenant_payments_comparison_reports.js +76 -0
  59. package/esm/schemas/models/materialized_tenant_prep_time_entries.js +130 -0
  60. package/esm/schemas/models/materialized_tenant_prep_time_reports.js +109 -0
  61. package/esm/schemas/models/materialized_tenant_sales_comparison_entries.js +130 -0
  62. package/esm/schemas/models/materialized_tenant_sales_comparison_reports.js +154 -0
  63. package/esm/schemas/types/index.js +15 -0
  64. package/esm/schemas/types/materialized_rider_daily_reports.js +1 -0
  65. package/esm/schemas/types/materialized_tenant_cash_entries.js +1 -0
  66. package/esm/schemas/types/materialized_tenant_cash_reports.js +1 -0
  67. package/esm/schemas/types/materialized_tenant_delivery_entries.js +1 -0
  68. package/esm/schemas/types/materialized_tenant_delivery_reports.js +1 -0
  69. package/esm/schemas/types/materialized_tenant_discount_entries.js +1 -0
  70. package/esm/schemas/types/materialized_tenant_discount_reports.js +2 -0
  71. package/esm/schemas/types/materialized_tenant_hourly_comparison_entries.js +1 -0
  72. package/esm/schemas/types/materialized_tenant_hourly_comparison_reports.js +1 -0
  73. package/esm/schemas/types/materialized_tenant_payments_comparison_entries.js +1 -0
  74. package/esm/schemas/types/materialized_tenant_payments_comparison_reports.js +11 -0
  75. package/esm/schemas/types/materialized_tenant_prep_time_entries.js +1 -0
  76. package/esm/schemas/types/materialized_tenant_prep_time_reports.js +1 -0
  77. package/esm/schemas/types/materialized_tenant_sales_comparison_entries.js +1 -0
  78. package/esm/schemas/types/materialized_tenant_sales_comparison_reports.js +14 -0
  79. package/lib/constants/MATERIALIZED_REPORTS.d.ts +339 -2
  80. package/lib/constants/MATERIALIZED_REPORTS.js +450 -1
  81. package/lib/constants/enums/communications.d.ts +1 -0
  82. package/lib/constants/enums/communications.js +2 -1
  83. package/lib/helpers/index.d.ts +3 -0
  84. package/lib/helpers/index.js +3 -0
  85. package/lib/helpers/materialized-reports/index.d.ts +32 -0
  86. package/lib/helpers/materialized-reports/index.js +32 -0
  87. package/lib/helpers/materialized-reports/materialized-cash-accumulator-helpers.d.ts +18 -0
  88. package/lib/helpers/materialized-reports/materialized-cash-accumulator-helpers.js +118 -0
  89. package/lib/helpers/materialized-reports/materialized-cash-classification-helpers.d.ts +90 -0
  90. package/lib/helpers/materialized-reports/materialized-cash-classification-helpers.js +295 -0
  91. package/lib/helpers/materialized-reports/materialized-cash-close-day-helpers.d.ts +102 -0
  92. package/lib/helpers/materialized-reports/materialized-cash-close-day-helpers.js +409 -0
  93. package/lib/helpers/materialized-reports/materialized-cash-compile-helpers.d.ts +72 -0
  94. package/lib/helpers/materialized-reports/materialized-cash-compile-helpers.js +251 -0
  95. package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.d.ts +7 -0
  96. package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +173 -0
  97. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.d.ts +84 -0
  98. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.js +438 -0
  99. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.d.ts +97 -0
  100. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +385 -0
  101. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.d.ts +40 -0
  102. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.js +218 -0
  103. package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.d.ts +52 -0
  104. package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +324 -0
  105. package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.d.ts +50 -0
  106. package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.js +222 -0
  107. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.d.ts +182 -0
  108. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.js +615 -0
  109. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.d.ts +47 -0
  110. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.js +193 -0
  111. package/lib/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.d.ts +64 -0
  112. package/lib/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.js +163 -0
  113. package/lib/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.d.ts +61 -0
  114. package/lib/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.js +207 -0
  115. package/lib/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.d.ts +23 -0
  116. package/lib/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.js +116 -0
  117. package/lib/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.d.ts +25 -0
  118. package/lib/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +98 -0
  119. package/lib/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.d.ts +79 -0
  120. package/lib/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.js +407 -0
  121. package/lib/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.d.ts +35 -0
  122. package/lib/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.js +173 -0
  123. package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.d.ts +66 -0
  124. package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +224 -0
  125. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.d.ts +128 -0
  126. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +490 -0
  127. package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.d.ts +46 -0
  128. package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +208 -0
  129. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.d.ts +8 -0
  130. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +8 -1
  131. package/lib/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.d.ts +48 -0
  132. package/lib/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.js +204 -0
  133. package/lib/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.d.ts +22 -0
  134. package/lib/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.js +161 -0
  135. package/lib/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.d.ts +163 -0
  136. package/lib/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.js +603 -0
  137. package/lib/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.d.ts +68 -0
  138. package/lib/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.js +182 -0
  139. package/lib/helpers/materialized-reports/tenant-cash-in-out-formatter.d.ts +98 -0
  140. package/lib/helpers/materialized-reports/tenant-cash-in-out-formatter.js +159 -0
  141. package/lib/helpers/materialized-reports/tenant-delivery-formatter.d.ts +133 -0
  142. package/lib/helpers/materialized-reports/tenant-delivery-formatter.js +233 -0
  143. package/lib/helpers/materialized-reports/tenant-discount-formatter.d.ts +114 -0
  144. package/lib/helpers/materialized-reports/tenant-discount-formatter.js +431 -0
  145. package/lib/helpers/materialized-reports/tenant-hourly-comparison-formatter.d.ts +60 -0
  146. package/lib/helpers/materialized-reports/tenant-hourly-comparison-formatter.js +243 -0
  147. package/lib/helpers/materialized-reports/tenant-payments-comparison-formatter.d.ts +75 -0
  148. package/lib/helpers/materialized-reports/tenant-payments-comparison-formatter.js +286 -0
  149. package/lib/helpers/materialized-reports/tenant-prep-time-formatter.d.ts +97 -0
  150. package/lib/helpers/materialized-reports/tenant-prep-time-formatter.js +232 -0
  151. package/lib/helpers/materialized-reports/tenant-sales-comparison-formatter.d.ts +105 -0
  152. package/lib/helpers/materialized-reports/tenant-sales-comparison-formatter.js +349 -0
  153. package/lib/helpers/reporting-contract/index.d.ts +2 -0
  154. package/lib/helpers/reporting-contract/index.js +18 -0
  155. package/lib/helpers/reporting-contract/overview-contract.d.ts +55 -0
  156. package/lib/helpers/reporting-contract/overview-contract.js +126 -0
  157. package/lib/helpers/reporting-contract/report-contract.d.ts +159 -0
  158. package/lib/helpers/reporting-contract/report-contract.js +239 -0
  159. package/lib/helpers/rider-earnings/index.d.ts +1 -0
  160. package/lib/helpers/rider-earnings/index.js +17 -0
  161. package/lib/helpers/rider-earnings/rider-earnings.d.ts +58 -0
  162. package/lib/helpers/rider-earnings/rider-earnings.js +64 -0
  163. package/lib/helpers/rider-reports/index.d.ts +2 -0
  164. package/lib/helpers/rider-reports/index.js +18 -0
  165. package/lib/helpers/rider-reports/rider-daily-report.d.ts +26 -0
  166. package/lib/helpers/rider-reports/rider-daily-report.js +93 -0
  167. package/lib/helpers/rider-reports/rider-report.d.ts +49 -0
  168. package/lib/helpers/rider-reports/rider-report.js +70 -0
  169. package/lib/helpers/types/materialized-reports-apis.d.ts +297 -1
  170. package/lib/schemas/models/deliveries.d.ts +54 -0
  171. package/lib/schemas/models/deliveries.js +75 -0
  172. package/lib/schemas/models/expense_details.d.ts +3 -3
  173. package/lib/schemas/models/index.d.ts +15 -0
  174. package/lib/schemas/models/index.js +15 -0
  175. package/lib/schemas/models/materialized_rider_daily_reports.d.ts +27 -0
  176. package/lib/schemas/models/materialized_rider_daily_reports.js +42 -0
  177. package/lib/schemas/models/materialized_tenant_cash_entries.d.ts +181 -0
  178. package/lib/schemas/models/materialized_tenant_cash_entries.js +98 -0
  179. package/lib/schemas/models/materialized_tenant_cash_reports.d.ts +111 -0
  180. package/lib/schemas/models/materialized_tenant_cash_reports.js +80 -0
  181. package/lib/schemas/models/materialized_tenant_delivery_entries.d.ts +98 -0
  182. package/lib/schemas/models/materialized_tenant_delivery_entries.js +66 -0
  183. package/lib/schemas/models/materialized_tenant_delivery_reports.d.ts +98 -0
  184. package/lib/schemas/models/materialized_tenant_delivery_reports.js +66 -0
  185. package/lib/schemas/models/materialized_tenant_discount_entries.d.ts +199 -0
  186. package/lib/schemas/models/materialized_tenant_discount_entries.js +159 -0
  187. package/lib/schemas/models/materialized_tenant_discount_reports.d.ts +216 -0
  188. package/lib/schemas/models/materialized_tenant_discount_reports.js +336 -0
  189. package/lib/schemas/models/materialized_tenant_hourly_comparison_entries.d.ts +95 -0
  190. package/lib/schemas/models/materialized_tenant_hourly_comparison_entries.js +82 -0
  191. package/lib/schemas/models/materialized_tenant_hourly_comparison_reports.d.ts +105 -0
  192. package/lib/schemas/models/materialized_tenant_hourly_comparison_reports.js +91 -0
  193. package/lib/schemas/models/materialized_tenant_payments_comparison_entries.d.ts +91 -0
  194. package/lib/schemas/models/materialized_tenant_payments_comparison_entries.js +70 -0
  195. package/lib/schemas/models/materialized_tenant_payments_comparison_reports.d.ts +98 -0
  196. package/lib/schemas/models/materialized_tenant_payments_comparison_reports.js +71 -0
  197. package/lib/schemas/models/materialized_tenant_prep_time_entries.d.ts +181 -0
  198. package/lib/schemas/models/materialized_tenant_prep_time_entries.js +133 -0
  199. package/lib/schemas/models/materialized_tenant_prep_time_reports.d.ts +118 -0
  200. package/lib/schemas/models/materialized_tenant_prep_time_reports.js +112 -0
  201. package/lib/schemas/models/materialized_tenant_sales_comparison_entries.d.ts +139 -0
  202. package/lib/schemas/models/materialized_tenant_sales_comparison_entries.js +120 -0
  203. package/lib/schemas/models/materialized_tenant_sales_comparison_reports.d.ts +167 -0
  204. package/lib/schemas/models/materialized_tenant_sales_comparison_reports.js +137 -0
  205. package/lib/schemas/models/tenant_purchase_order_vendor_returns.d.ts +3 -3
  206. package/lib/schemas/models/warehouse_internal_movements.d.ts +6 -6
  207. package/lib/schemas/models/warehouse_purchase_order_receipts.d.ts +3 -3
  208. package/lib/schemas/models/warehouse_purchase_order_vendor_returns.d.ts +3 -3
  209. package/lib/schemas/models/warehouse_sale_deliveries.d.ts +6 -6
  210. package/lib/schemas/models/warehouse_sale_invoices.d.ts +3 -3
  211. package/lib/schemas/types/deliveries.d.ts +40 -1
  212. package/lib/schemas/types/index.d.ts +15 -0
  213. package/lib/schemas/types/index.js +15 -0
  214. package/lib/schemas/types/materialized_rider_daily_reports.d.ts +37 -0
  215. package/lib/schemas/types/materialized_rider_daily_reports.js +2 -0
  216. package/lib/schemas/types/materialized_tenant_cash_entries.d.ts +87 -0
  217. package/lib/schemas/types/materialized_tenant_cash_entries.js +2 -0
  218. package/lib/schemas/types/materialized_tenant_cash_reports.d.ts +144 -0
  219. package/lib/schemas/types/materialized_tenant_cash_reports.js +2 -0
  220. package/lib/schemas/types/materialized_tenant_delivery_entries.d.ts +98 -0
  221. package/lib/schemas/types/materialized_tenant_delivery_entries.js +2 -0
  222. package/lib/schemas/types/materialized_tenant_delivery_reports.d.ts +179 -0
  223. package/lib/schemas/types/materialized_tenant_delivery_reports.js +2 -0
  224. package/lib/schemas/types/materialized_tenant_discount_entries.d.ts +81 -0
  225. package/lib/schemas/types/materialized_tenant_discount_entries.js +2 -0
  226. package/lib/schemas/types/materialized_tenant_discount_reports.d.ts +209 -0
  227. package/lib/schemas/types/materialized_tenant_discount_reports.js +3 -0
  228. package/lib/schemas/types/materialized_tenant_hourly_comparison_entries.d.ts +33 -0
  229. package/lib/schemas/types/materialized_tenant_hourly_comparison_entries.js +2 -0
  230. package/lib/schemas/types/materialized_tenant_hourly_comparison_reports.d.ts +40 -0
  231. package/lib/schemas/types/materialized_tenant_hourly_comparison_reports.js +2 -0
  232. package/lib/schemas/types/materialized_tenant_payments_comparison_entries.d.ts +48 -0
  233. package/lib/schemas/types/materialized_tenant_payments_comparison_entries.js +2 -0
  234. package/lib/schemas/types/materialized_tenant_payments_comparison_reports.d.ts +51 -0
  235. package/lib/schemas/types/materialized_tenant_payments_comparison_reports.js +14 -0
  236. package/lib/schemas/types/materialized_tenant_prep_time_entries.d.ts +88 -0
  237. package/lib/schemas/types/materialized_tenant_prep_time_entries.js +2 -0
  238. package/lib/schemas/types/materialized_tenant_prep_time_reports.d.ts +132 -0
  239. package/lib/schemas/types/materialized_tenant_prep_time_reports.js +2 -0
  240. package/lib/schemas/types/materialized_tenant_sales_comparison_entries.d.ts +43 -0
  241. package/lib/schemas/types/materialized_tenant_sales_comparison_entries.js +2 -0
  242. package/lib/schemas/types/materialized_tenant_sales_comparison_reports.d.ts +74 -0
  243. package/lib/schemas/types/materialized_tenant_sales_comparison_reports.js +17 -0
  244. package/lib/tests/materialized-reports/analytics-growth-helpers.test.d.ts +1 -0
  245. package/lib/tests/materialized-reports/analytics-overview-helpers.test.d.ts +1 -0
  246. package/lib/tests/materialized-reports/analytics-performance-helpers.test.d.ts +1 -0
  247. package/lib/tests/materialized-reports/fixtures.d.ts +15 -1
  248. package/lib/tests/materialized-reports/fixtures.js +1 -0
  249. package/lib/tests/materialized-reports/materialized-cash-compile.test.d.ts +1 -0
  250. package/lib/tests/materialized-reports/materialized-cash-compile.test.js +196 -0
  251. package/lib/tests/materialized-reports/materialized-cash-fold.test.d.ts +1 -0
  252. package/lib/tests/materialized-reports/materialized-cash-fold.test.js +116 -0
  253. package/lib/tests/materialized-reports/materialized-cash-formatter.test.d.ts +1 -0
  254. package/lib/tests/materialized-reports/materialized-cash-formatter.test.js +152 -0
  255. package/lib/tests/materialized-reports/materialized-cash-plan.test.d.ts +1 -0
  256. package/lib/tests/materialized-reports/materialized-cash-plan.test.js +168 -0
  257. package/lib/tests/materialized-reports/materialized-cash-reconciliation.test.d.ts +1 -0
  258. package/lib/tests/materialized-reports/materialized-cash-reconciliation.test.js +267 -0
  259. package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.d.ts +1 -0
  260. package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.js +147 -0
  261. package/lib/tests/materialized-reports/materialized-delivery-compile.test.d.ts +1 -0
  262. package/lib/tests/materialized-reports/materialized-delivery-compile.test.js +241 -0
  263. package/lib/tests/materialized-reports/materialized-delivery-fold.test.d.ts +1 -0
  264. package/lib/tests/materialized-reports/materialized-delivery-fold.test.js +109 -0
  265. package/lib/tests/materialized-reports/materialized-delivery-formatter.test.d.ts +1 -0
  266. package/lib/tests/materialized-reports/materialized-delivery-formatter.test.js +141 -0
  267. package/lib/tests/materialized-reports/materialized-delivery-plan.test.d.ts +1 -0
  268. package/lib/tests/materialized-reports/materialized-delivery-plan.test.js +198 -0
  269. package/lib/tests/materialized-reports/materialized-discount-aggregated-formatter.test.d.ts +1 -0
  270. package/lib/tests/materialized-reports/materialized-discount-aggregated-formatter.test.js +173 -0
  271. package/lib/tests/materialized-reports/materialized-discount-classification.test.d.ts +1 -0
  272. package/lib/tests/materialized-reports/materialized-discount-classification.test.js +339 -0
  273. package/lib/tests/materialized-reports/materialized-discount-close-day.test.d.ts +1 -0
  274. package/lib/tests/materialized-reports/materialized-discount-close-day.test.js +506 -0
  275. package/lib/tests/materialized-reports/materialized-discount-compile.test.d.ts +1 -0
  276. package/lib/tests/materialized-reports/materialized-discount-compile.test.js +161 -0
  277. package/lib/tests/materialized-reports/materialized-discount-counter-rows.test.d.ts +1 -0
  278. package/lib/tests/materialized-reports/materialized-discount-counter-rows.test.js +107 -0
  279. package/lib/tests/materialized-reports/materialized-discount-entries-accumulation.test.d.ts +1 -0
  280. package/lib/tests/materialized-reports/materialized-discount-entries-accumulation.test.js +125 -0
  281. package/lib/tests/materialized-reports/materialized-hourly-comparison-compile.test.d.ts +1 -0
  282. package/lib/tests/materialized-reports/materialized-hourly-comparison-compile.test.js +146 -0
  283. package/lib/tests/materialized-reports/materialized-hourly-comparison-formatter.test.d.ts +1 -0
  284. package/lib/tests/materialized-reports/materialized-hourly-comparison-formatter.test.js +127 -0
  285. package/lib/tests/materialized-reports/materialized-hourly-comparison-plan.test.d.ts +1 -0
  286. package/lib/tests/materialized-reports/materialized-hourly-comparison-plan.test.js +138 -0
  287. package/lib/tests/materialized-reports/materialized-hourly-comparison-selection.test.d.ts +1 -0
  288. package/lib/tests/materialized-reports/materialized-hourly-comparison-selection.test.js +90 -0
  289. package/lib/tests/materialized-reports/materialized-payments-comparison-compile.test.d.ts +1 -0
  290. package/lib/tests/materialized-reports/materialized-payments-comparison-compile.test.js +164 -0
  291. package/lib/tests/materialized-reports/materialized-payments-comparison-formatter.test.d.ts +1 -0
  292. package/lib/tests/materialized-reports/materialized-payments-comparison-formatter.test.js +117 -0
  293. package/lib/tests/materialized-reports/materialized-payments-comparison-plan.test.d.ts +1 -0
  294. package/lib/tests/materialized-reports/materialized-payments-comparison-plan.test.js +149 -0
  295. package/lib/tests/materialized-reports/materialized-prep-time-classification.test.d.ts +1 -0
  296. package/lib/tests/materialized-reports/materialized-prep-time-classification.test.js +273 -0
  297. package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.d.ts +1 -0
  298. package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.js +270 -0
  299. package/lib/tests/materialized-reports/materialized-prep-time-compile.test.d.ts +1 -0
  300. package/lib/tests/materialized-reports/materialized-prep-time-compile.test.js +217 -0
  301. package/lib/tests/materialized-reports/materialized-prep-time-constants.test.d.ts +1 -0
  302. package/lib/tests/materialized-reports/materialized-prep-time-constants.test.js +84 -0
  303. package/lib/tests/materialized-reports/materialized-prep-time-plan.test.d.ts +1 -0
  304. package/lib/tests/materialized-reports/materialized-prep-time-plan.test.js +176 -0
  305. package/lib/tests/materialized-reports/materialized-sales-comparison-baseline.test.d.ts +1 -0
  306. package/lib/tests/materialized-reports/materialized-sales-comparison-baseline.test.js +126 -0
  307. package/lib/tests/materialized-reports/materialized-sales-comparison-compile.test.d.ts +1 -0
  308. package/lib/tests/materialized-reports/materialized-sales-comparison-compile.test.js +206 -0
  309. package/lib/tests/materialized-reports/materialized-sales-comparison-formatter.test.d.ts +1 -0
  310. package/lib/tests/materialized-reports/materialized-sales-comparison-formatter.test.js +179 -0
  311. package/lib/tests/materialized-reports/materialized-sales-comparison-parity.test.d.ts +1 -0
  312. package/lib/tests/materialized-reports/materialized-sales-comparison-parity.test.js +196 -0
  313. package/lib/tests/materialized-reports/materialized-sales-comparison-plan.test.d.ts +1 -0
  314. package/lib/tests/materialized-reports/materialized-sales-comparison-plan.test.js +241 -0
  315. package/lib/tests/materialized-reports/materialized-tenant-hourly-comparison-schema.test.d.ts +1 -0
  316. package/lib/tests/materialized-reports/materialized-tenant-hourly-comparison-schema.test.js +96 -0
  317. package/lib/tests/materialized-reports/materialized-tenant-payments-comparison-schema.test.d.ts +1 -0
  318. package/lib/tests/materialized-reports/materialized-tenant-payments-comparison-schema.test.js +112 -0
  319. package/lib/tests/materialized-reports/materialized-tenant-sales-comparison-schema.test.d.ts +1 -0
  320. package/lib/tests/materialized-reports/materialized-tenant-sales-comparison-schema.test.js +142 -0
  321. package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +8 -1
  322. package/lib/tests/materialized-reports/sales-comparison-reference.d.ts +55 -0
  323. package/lib/tests/materialized-reports/sales-comparison-reference.js +86 -0
  324. package/lib/tests/materialized-reports/tenant-discount-formatter.test.d.ts +1 -0
  325. package/lib/tests/materialized-reports/tenant-discount-formatter.test.js +289 -0
  326. package/lib/tests/materialized-reports/tenant-prep-time-formatter.test.d.ts +1 -0
  327. package/lib/tests/materialized-reports/tenant-prep-time-formatter.test.js +354 -0
  328. package/lib/tests/reporting-contract.test.d.ts +1 -0
  329. package/lib/tests/reporting-contract.test.js +267 -0
  330. package/lib/tests/rider-daily-report.test.d.ts +1 -0
  331. package/lib/tests/rider-daily-report.test.js +86 -0
  332. package/lib/tests/rider-earnings.test.d.ts +1 -0
  333. package/lib/tests/rider-earnings.test.js +65 -0
  334. package/lib/tests/rider-report.test.d.ts +1 -0
  335. package/lib/tests/rider-report.test.js +98 -0
  336. package/package.json +10 -1
@@ -0,0 +1,427 @@
1
+ import { ALL_DAY_INDEX } from '../../constants/ENUMS';
2
+ import { MATERIALIZED_DISCOUNT_LEVEL_ENUM, MATERIALIZED_DISCOUNT_SLOT_ENUM, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM, MATERIALIZED_REPORT_KEY_FILTER_ENUM, MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_SCOPES_WITH_DAY_INDEX, } from '../../constants/MATERIALIZED_REPORTS';
3
+ import { MATERIALIZED_TENANT_DISCOUNT_REPORTS_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_discount_reports';
4
+ import { convertPriceFromCents } from '../utils/price-conversion';
5
+ import { parseMaterializedReportKey } from './key-helpers';
6
+ import { getMaterializedBusinessHourIndex } from './materialized-reports-read-helpers';
7
+ const emptySlot = () => ({ count: 0, amount: 0, resolved_amount: 0 });
8
+ const addSlot = (target, source) => {
9
+ if (source == null)
10
+ return;
11
+ target.count += source.count ?? 0;
12
+ target.amount += source.amount ?? 0;
13
+ target.resolved_amount += source.resolved_amount ?? 0;
14
+ };
15
+ /** Resolution of a slot's rate for display (`null` when no count/rate). */
16
+ function averageRate(slot) {
17
+ if (slot.count <= 0 || slot.amount <= 0)
18
+ return null;
19
+ return Math.round((slot.amount / slot.count) * 100) / 100;
20
+ }
21
+ const round2 = (value) => Math.round((value ?? 0) * 100) / 100;
22
+ const toDecimal = (cents) => convertPriceFromCents(cents ?? 0);
23
+ /** Human labels of the breakdown rows. */
24
+ const SUB_KIND_LABELS = {
25
+ [MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.COLLECTION]: 'Collection discount',
26
+ [MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.MANUAL]: 'Manual / quick discount',
27
+ [MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.PRICE_OVERRIDE]: 'Price override',
28
+ [MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM]: 'Item / catalog discount',
29
+ [MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER]: 'Offer',
30
+ [MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.GIFT_CARD]: 'Gift card',
31
+ [MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.WALLET_CASHBACK]: 'Wallet cashback',
32
+ [MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED]: 'Other / unattributed',
33
+ };
34
+ /** Does a parsed key match the requested filter sets? (no filter = match) */
35
+ function keyMatchesFilters(key, keyFilters, filters) {
36
+ const components = parseMaterializedReportKey(key);
37
+ if (components == null || components.length !== keyFilters.length)
38
+ return null;
39
+ if (filters == null)
40
+ return { matched: true, components };
41
+ const wanted = {};
42
+ if (filters.platforms != null && filters.platforms.length > 0) {
43
+ wanted[MATERIALIZED_REPORT_KEY_FILTER_ENUM.PLATFORM] = filters.platforms;
44
+ }
45
+ if (filters.order_statuses != null && filters.order_statuses.length > 0) {
46
+ wanted[MATERIALIZED_REPORT_KEY_FILTER_ENUM.ORDER_STATUS] = filters.order_statuses;
47
+ }
48
+ for (let index = 0; index < keyFilters.length; index += 1) {
49
+ const filter = keyFilters[index];
50
+ const allowed = wanted[filter];
51
+ if (allowed == null)
52
+ continue;
53
+ if (!allowed.includes(components[index]))
54
+ return { matched: false, components };
55
+ }
56
+ return { matched: true, components };
57
+ }
58
+ const isCancelledKey = (components, keyFilters) => {
59
+ const statusIndex = keyFilters.indexOf(MATERIALIZED_REPORT_KEY_FILTER_ENUM.ORDER_STATUS);
60
+ if (statusIndex === -1)
61
+ return false;
62
+ return components[statusIndex] === 'CANCELLED';
63
+ };
64
+ const emptyScopeAccumulator = () => ({
65
+ summary: {
66
+ total: 0,
67
+ orderLevel: 0,
68
+ itemLevel: 0,
69
+ discountCount: 0,
70
+ itemCount: 0,
71
+ unattributed: 0,
72
+ freeCount: 0,
73
+ freeValue: 0,
74
+ compCount: 0,
75
+ compAmount: 0,
76
+ },
77
+ orderSpecial: emptySlot(),
78
+ orderLoyalty: emptySlot(),
79
+ orderVoucher: emptySlot(),
80
+ rows: new Map(),
81
+ cancelled: { total: 0, count: 0 },
82
+ });
83
+ /** The row identity of a (sub-kind, slot, level) triple. */
84
+ const rowKey = (subKind, slot, level) => `${level}|${subKind}|${slot ?? '-'}`;
85
+ const rowFor = (accumulator, subKind, slot, level) => {
86
+ const key = rowKey(subKind, slot, level);
87
+ let row = accumulator.rows.get(key);
88
+ if (row == null) {
89
+ row = { subKind, slot, level, slotAcc: emptySlot() };
90
+ accumulator.rows.set(key, row);
91
+ }
92
+ return row.slotAcc;
93
+ };
94
+ /**
95
+ * Formats materialized discount documents into page-ready data — one entry per
96
+ * tenant (`separate_view_per_tenant`) or one combined entry. Returns `null` on
97
+ * unformattable input; an empty list yields a zero-filled page.
98
+ */
99
+ export function getFormattedTenantDiscountsReporting(reports, filters = null, separate_view_per_tenant = false) {
100
+ if (!Array.isArray(reports) || reports.some((report) => report == null))
101
+ return null;
102
+ if (filters != null && filters.day_indexes != null && Array.isArray(filters.day_indexes)) {
103
+ const invalid = filters.day_indexes.some((day) => !ALL_DAY_INDEX.includes(day));
104
+ if (invalid)
105
+ return null;
106
+ }
107
+ if (reports.length > 0) {
108
+ const signature = (reports[0].sale_type_key_filters ?? []).join(':');
109
+ const schemaVersion = reports[0].schema_version;
110
+ const seenScopes = new Set();
111
+ for (const report of reports) {
112
+ if ((report.sale_type_key_filters ?? []).join(':') !== signature)
113
+ return null;
114
+ if (report.schema_version !== schemaVersion)
115
+ return null;
116
+ // The read contract makes scope groups DISJOINT (the compiler never emits
117
+ // overlapping groups and the backend unions them) — so a repeated
118
+ // (scope_context, scope_field) WITHIN A TENANT would double-count every shared
119
+ // document. Reject rather than silently inflating the totals.
120
+ //
121
+ // The TENANT is part of the identity. Without it, two tenants of one brand that
122
+ // share a scope field — every brand-mode selection, since each store has its own
123
+ // document for the same date — looked like a duplicate and the whole formatter
124
+ // returned NULL, blanking the report for the exact multi-tenant read the page
125
+ // makes in production.
126
+ const identity = `${report.tenant_id}|${report.scope_context}|${report.scope_field}`;
127
+ if (seenScopes.has(identity))
128
+ return null;
129
+ seenScopes.add(identity);
130
+ }
131
+ }
132
+ const byTenant = new Map();
133
+ for (const report of reports) {
134
+ const tenant = String(report.tenant_id ?? '');
135
+ const list = byTenant.get(tenant) ?? [];
136
+ list.push(report);
137
+ byTenant.set(tenant, list);
138
+ }
139
+ if (byTenant.size === 0) {
140
+ return [[buildPageData([], emptyScopeAccumulator(), null, null)], null];
141
+ }
142
+ const pages = [];
143
+ const groups = separate_view_per_tenant ? [...byTenant.values()] : [[...byTenant.values()].flat()];
144
+ for (const group of groups) {
145
+ if (group.length === 0)
146
+ continue;
147
+ const keyFilters = group[0].sale_type_key_filters ?? [];
148
+ // Date/hour scopes feed the weekday + hourly views; period scopes feed the
149
+ // summary. A brand-level read returns BOTH, so accumulate per scope class.
150
+ const periodAccumulator = emptyScopeAccumulator();
151
+ const weekdayMap = new Map();
152
+ const hourMap = new Map();
153
+ let sawDateScoped = false;
154
+ let sawPeriodScoped = false;
155
+ for (const report of group) {
156
+ const scoped = MATERIALIZED_REPORT_SCOPES_WITH_DAY_INDEX.includes(report.scope_context);
157
+ if (scoped)
158
+ sawDateScoped = true;
159
+ else
160
+ sawPeriodScoped = true;
161
+ // The weekday filter applies only to date/hour documents.
162
+ if (filters?.day_indexes != null && filters.day_indexes.length > 0) {
163
+ if (!scoped)
164
+ continue;
165
+ const dayIndex = report.day_index ?? null;
166
+ if (dayIndex == null || !filters.day_indexes.includes(dayIndex))
167
+ continue;
168
+ }
169
+ for (const [key, counters] of Object.entries(report.discount_counters ?? {})) {
170
+ const parsed = keyMatchesFilters(key, keyFilters, filters);
171
+ if (parsed == null)
172
+ continue;
173
+ if (!parsed.matched)
174
+ continue;
175
+ // Cancelled keys split out — they never feed the summary.
176
+ if (isCancelledKey(parsed.components, keyFilters)) {
177
+ periodAccumulator.cancelled.total += counters.amount ?? 0;
178
+ periodAccumulator.cancelled.count += counters.count ?? 0;
179
+ continue;
180
+ }
181
+ accumulateCounters(periodAccumulator, counters);
182
+ if (scoped) {
183
+ const dayIndex = report.day_index ?? null;
184
+ if (dayIndex != null) {
185
+ const current = weekdayMap.get(dayIndex) ?? { total: 0, count: 0 };
186
+ current.total += counters.amount ?? 0;
187
+ current.count += counters.count ?? 0;
188
+ weekdayMap.set(dayIndex, current);
189
+ }
190
+ }
191
+ if (report.scope_context === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR) {
192
+ const parts = String(report.scope_field).split(' ');
193
+ const localDate = parts[0] ?? '';
194
+ const hour = Number.parseInt(parts[1] ?? '', 10);
195
+ if (localDate.length > 0 && Number.isInteger(hour) && hour >= 0 && hour <= 23) {
196
+ const current = hourMap.get(report.scope_field) ?? { total: 0, count: 0, localDate, hour };
197
+ current.total += counters.amount ?? 0;
198
+ current.count += counters.count ?? 0;
199
+ hourMap.set(report.scope_field, current);
200
+ }
201
+ }
202
+ }
203
+ }
204
+ // The summary sums every document: the read contract's scope groups are
205
+ // disjoint, so each order is represented exactly once. The weekday/hourly
206
+ // breakdowns can only come from DAY-scoped documents (period documents carry
207
+ // no `day_index` and no hour), so they are surfaced only when the selection
208
+ // actually contains them — a period-only read legitimately has neither.
209
+ const hasDayScoped = sawDateScoped;
210
+ const weekday = hasDayScoped
211
+ ? [...weekdayMap.entries()]
212
+ .sort((a, b) => ALL_DAY_INDEX.indexOf(a[0]) - ALL_DAY_INDEX.indexOf(b[0]))
213
+ .map(([dayIndex, value]) => ({
214
+ day_index: dayIndex,
215
+ total_discount: toDecimal(value.total),
216
+ discount_count: value.count,
217
+ }))
218
+ : null;
219
+ const hourly = hasDayScoped
220
+ ? [...hourMap.values()]
221
+ .sort((a, b) => a.localDate.localeCompare(b.localDate) ||
222
+ // Chronological business-hour order (06…23, 00…05) — never plain
223
+ // hour order, which would put the 00–05 tail before the evening.
224
+ (getMaterializedBusinessHourIndex(a.hour) ?? 0) - (getMaterializedBusinessHourIndex(b.hour) ?? 0))
225
+ .map((value) => ({
226
+ local_date: value.localDate,
227
+ hour: value.hour,
228
+ business_hour_index: getMaterializedBusinessHourIndex(value.hour) ?? value.hour,
229
+ total_discount: toDecimal(value.total),
230
+ discount_count: value.count,
231
+ }))
232
+ : null;
233
+ pages.push(buildPageData(group.map((report) => String(report.tenant_id)), periodAccumulator, weekday, hourly));
234
+ }
235
+ return [pages, null];
236
+ }
237
+ /** Folds one key's counters into a scope accumulator. */
238
+ function accumulateCounters(accumulator, counters) {
239
+ const { summary } = accumulator;
240
+ summary.total += counters.amount ?? 0;
241
+ summary.orderLevel += counters.order_level_amount ?? 0;
242
+ summary.itemLevel += counters.item_level_amount ?? 0;
243
+ summary.discountCount += counters.count ?? 0;
244
+ summary.itemCount += counters.item_discount_count ?? 0;
245
+ summary.unattributed += counters.unattributed_amount ?? 0;
246
+ summary.freeCount += counters.free_items?.count ?? 0;
247
+ summary.freeValue += counters.free_items?.value ?? 0;
248
+ summary.compCount += counters.complimentary?.payment_count ?? 0;
249
+ summary.compAmount += counters.complimentary?.amount ?? 0;
250
+ addSlot(accumulator.orderSpecial, counters.order_special);
251
+ addSlot(accumulator.orderLoyalty, counters.order_loyalty);
252
+ addSlot(accumulator.orderVoucher, counters.order_voucher);
253
+ // Order-level sub-kind rows
254
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.COLLECTION, MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER), counters.special_collection);
255
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.MANUAL, MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER), counters.special_manual);
256
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.PRICE_OVERRIDE, MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER), counters.special_price_override);
257
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM, MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER), counters.special_catalog_item);
258
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED, MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER), counters.special_other);
259
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER, MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER), counters.loyalty_offer);
260
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.GIFT_CARD, MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER), counters.loyalty_gift_card);
261
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.WALLET_CASHBACK, MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER), counters.loyalty_wallet);
262
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED, MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER), counters.loyalty_other);
263
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER, MATERIALIZED_DISCOUNT_SLOT_ENUM.VOUCHER, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER), counters.voucher_offer);
264
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED, MATERIALIZED_DISCOUNT_SLOT_ENUM.VOUCHER, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER), counters.voucher_other);
265
+ // Item-level slot totals
266
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.COLLECTION, MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM), {
267
+ count: counters.item_special?.collection_count ?? 0,
268
+ amount: counters.item_special?.collection_amount ?? 0,
269
+ resolved_amount: counters.item_special?.collection_resolved_amount ?? 0,
270
+ });
271
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.MANUAL, MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM), {
272
+ count: counters.item_special?.manual_count ?? 0,
273
+ amount: counters.item_special?.manual_amount ?? 0,
274
+ resolved_amount: counters.item_special?.manual_resolved_amount ?? 0,
275
+ });
276
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.PRICE_OVERRIDE, MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM), {
277
+ count: counters.item_special?.price_override_count ?? 0,
278
+ amount: counters.item_special?.price_override_amount ?? 0,
279
+ resolved_amount: counters.item_special?.price_override_resolved_amount ?? 0,
280
+ });
281
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM, MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM), {
282
+ count: counters.item_special?.catalog_item_count ?? 0,
283
+ amount: counters.item_special?.catalog_item_amount ?? 0,
284
+ resolved_amount: counters.item_special?.catalog_item_resolved_amount ?? 0,
285
+ });
286
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED, MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM), {
287
+ count: counters.item_special?.other_count ?? 0,
288
+ amount: counters.item_special?.other_amount ?? 0,
289
+ resolved_amount: counters.item_special?.other_resolved_amount ?? 0,
290
+ });
291
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER, MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM), {
292
+ count: counters.item_loyalty?.offer_count ?? 0,
293
+ amount: counters.item_loyalty?.offer_amount ?? 0,
294
+ resolved_amount: counters.item_loyalty?.offer_resolved_amount ?? 0,
295
+ });
296
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.GIFT_CARD, MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM), {
297
+ count: counters.item_loyalty?.gift_card_count ?? 0,
298
+ amount: counters.item_loyalty?.gift_card_amount ?? 0,
299
+ resolved_amount: counters.item_loyalty?.gift_card_resolved_amount ?? 0,
300
+ });
301
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.WALLET_CASHBACK, MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM), {
302
+ count: counters.item_loyalty?.wallet_count ?? 0,
303
+ amount: counters.item_loyalty?.wallet_amount ?? 0,
304
+ resolved_amount: counters.item_loyalty?.wallet_resolved_amount ?? 0,
305
+ });
306
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED, MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM), {
307
+ count: counters.item_loyalty?.other_count ?? 0,
308
+ amount: counters.item_loyalty?.other_amount ?? 0,
309
+ resolved_amount: counters.item_loyalty?.other_resolved_amount ?? 0,
310
+ });
311
+ addSlot(rowFor(accumulator, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER, MATERIALIZED_DISCOUNT_SLOT_ENUM.VOUCHER, MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM), {
312
+ count: counters.item_voucher?.offer_count ?? 0,
313
+ amount: counters.item_voucher?.offer_amount ?? 0,
314
+ resolved_amount: counters.item_voucher?.offer_resolved_amount ?? 0,
315
+ });
316
+ }
317
+ /** Builds one page from an accumulator. */
318
+ function buildPageData(tenantIds, accumulator, weekday, hourly) {
319
+ const totalCents = accumulator.summary.total;
320
+ const sections = [...accumulator.rows.values()]
321
+ .filter((row) => row.slotAcc.count > 0 || row.slotAcc.resolved_amount !== 0)
322
+ .map((row) => ({
323
+ sub_kind: row.subKind,
324
+ slot: row.slot,
325
+ level: row.level,
326
+ label: SUB_KIND_LABELS[row.subKind],
327
+ count: row.slotAcc.count,
328
+ amount: toDecimal(row.slotAcc.resolved_amount),
329
+ // The average RAW rate of the row's occurrences (a percent average for
330
+ // percentage-heavy rows) — display context only, NEVER money.
331
+ rate: averageRate(row.slotAcc),
332
+ share_percent: totalCents === 0 ? 0 : round2((row.slotAcc.resolved_amount / totalCents) * 100),
333
+ }))
334
+ .sort((a, b) => b.amount - a.amount);
335
+ return {
336
+ tenant_ids: [...new Set(tenantIds)],
337
+ summary: {
338
+ total_discount: toDecimal(accumulator.summary.total),
339
+ order_level_discount: toDecimal(accumulator.summary.orderLevel),
340
+ item_level_discount: toDecimal(accumulator.summary.itemLevel),
341
+ discount_count: accumulator.summary.discountCount,
342
+ item_discount_count: accumulator.summary.itemCount,
343
+ unattributed_discount: toDecimal(accumulator.summary.unattributed),
344
+ free_item_count: accumulator.summary.freeCount,
345
+ free_item_value: toDecimal(accumulator.summary.freeValue),
346
+ complimentary_count: accumulator.summary.compCount,
347
+ complimentary_amount: toDecimal(accumulator.summary.compAmount),
348
+ },
349
+ order_slots: [
350
+ {
351
+ slot: MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL,
352
+ count: accumulator.orderSpecial.count,
353
+ amount: toDecimal(accumulator.orderSpecial.resolved_amount),
354
+ rate: averageRate(accumulator.orderSpecial),
355
+ },
356
+ {
357
+ slot: MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY,
358
+ count: accumulator.orderLoyalty.count,
359
+ amount: toDecimal(accumulator.orderLoyalty.resolved_amount),
360
+ rate: averageRate(accumulator.orderLoyalty),
361
+ },
362
+ {
363
+ slot: MATERIALIZED_DISCOUNT_SLOT_ENUM.VOUCHER,
364
+ count: accumulator.orderVoucher.count,
365
+ amount: toDecimal(accumulator.orderVoucher.resolved_amount),
366
+ rate: averageRate(accumulator.orderVoucher),
367
+ },
368
+ ],
369
+ sections,
370
+ cancelled: accumulator.cancelled.count > 0
371
+ ? { total_discount: toDecimal(accumulator.cancelled.total), discount_count: accumulator.cancelled.count }
372
+ : null,
373
+ weekday,
374
+ hourly,
375
+ };
376
+ }
377
+ /* ==================== Aggregated (pipeline) variant ==================== */
378
+ /**
379
+ * Frontend formatter for the AGGREGATED discounts read
380
+ * (`POST /reports/discounts` returns `IMaterializedTenantDiscountAggregatedReport[]`
381
+ * - one record per tenant, folded inside MongoDB).
382
+ *
383
+ * The aggregated record carries the SAME `discount_counters` map as a stored
384
+ * document, so this delegates to the stored-document formatter: one code path,
385
+ * no duplicated shaping. The envelope fields it reads are supplied here.
386
+ *
387
+ * `scope_field` carries the TENANT. The formatter's duplicate-identity guard
388
+ * (`scope_context|scope_field`) exists to catch the same stored scope document
389
+ * being fed twice; for folded records the meaningful identity IS the tenant, so
390
+ * one record per tenant keeps that guard useful instead of tripping on every
391
+ * record sharing an absent scope.
392
+ *
393
+ * LIMITATION - no `weekday` / `hourly` breakdowns. Those group on a stored
394
+ * document's `day_index` / `scope_field` hour, and a folded record spans many
395
+ * scope documents, so the fold has no single value to group on. A weekday or
396
+ * day-index filter is therefore REFUSED (`null`, the unformattable convention)
397
+ * rather than silently ignored; `weekday` and `hourly` come back `null`. The
398
+ * summary, order slots and sections are exact.
399
+ */
400
+ export function getFormattedTenantDiscountsAggregatedReporting(reports, filters = null, separate_view_per_tenant = false) {
401
+ if (!Array.isArray(reports) || reports.some((report) => report == null)) {
402
+ return null;
403
+ }
404
+ // A folded record has no single day_index, so a weekday filter cannot be
405
+ // honoured - refuse rather than quietly return every weekday.
406
+ if (filters != null && Array.isArray(filters.day_indexes) && filters.day_indexes.length > 0) {
407
+ return null;
408
+ }
409
+ const asStored = reports.map((report) => ({
410
+ _id: `${report.tenant_id}|aggregated`,
411
+ tenant_id: report.tenant_id,
412
+ brand_id: report.brand_id,
413
+ // The fold spans scopes; LOCAL_MONTH is the report's widest scope and is
414
+ // only used for the (skipped) hour parse.
415
+ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH,
416
+ scope_field: report.tenant_id,
417
+ day_index: null,
418
+ sale_type_key_filters: report.sale_type_key_filters,
419
+ discount_counters: report.discount_counters,
420
+ schema_version: MATERIALIZED_TENANT_DISCOUNT_REPORTS_SCHEMA_VERSION,
421
+ source_watermark: report.max_updated_at ?? new Date(0),
422
+ computed_at: report.max_updated_at ?? new Date(0),
423
+ createdAt: report.max_updated_at ?? new Date(0),
424
+ updatedAt: report.max_updated_at ?? new Date(0),
425
+ }));
426
+ return getFormattedTenantDiscountsReporting(asStored, filters, separate_view_per_tenant);
427
+ }
@@ -0,0 +1,237 @@
1
+ import { MATERIALIZED_REPORT_KEY_FILTER_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
2
+ import { buildSalesComparisonMetrics, formatSalesComparisonSeries, } from './tenant-sales-comparison-formatter';
3
+ import { addSalesComparisonCounters, createEmptySalesComparisonCounters, } from './materialized-sales-comparison-accumulator-helpers';
4
+ import { parseMaterializedReportKey } from './key-helpers';
5
+ /** `'YYYY-MM-DD HH'` → hour, or null. */
6
+ const hourOfScopeField = (scopeField) => {
7
+ if (typeof scopeField !== 'string')
8
+ return null;
9
+ const match = /^\d{4}-\d{2}-\d{2} ([01]\d|2[0-3])$/.exec(scopeField);
10
+ return match == null ? null : Number(match[1]);
11
+ };
12
+ /**
13
+ * Accepts BOTH read shapes: an aggregated profile record (`hour_counters`) and
14
+ * a STORED hour document (`sales_counters` + `scope_field`) — the same dual
15
+ * input every document-shaped materialized formatter accepts, so a stored
16
+ * document and a read record go down one code path.
17
+ */
18
+ const normalizeHourlyDocument = (doc) => {
19
+ if (doc == null)
20
+ return null;
21
+ const keyFilters = (doc.sale_type_key_filters ?? []);
22
+ if ('hour_counters' in doc && doc.hour_counters != null) {
23
+ return {
24
+ tenant_id: String(doc.tenant_id),
25
+ brand_id: String(doc.brand_id ?? ''),
26
+ sale_type_key_filters: keyFilters,
27
+ hour_counters: doc.hour_counters,
28
+ matched_documents: Number(doc.matched_documents ?? 1) || 1,
29
+ max_updated_at: doc.max_updated_at == null ? null : new Date(doc.max_updated_at),
30
+ };
31
+ }
32
+ const stored = doc;
33
+ if (stored.sales_counters == null)
34
+ return null;
35
+ const hour = hourOfScopeField(stored.scope_field);
36
+ if (hour == null)
37
+ return null;
38
+ return {
39
+ tenant_id: String(stored.tenant_id),
40
+ brand_id: String(stored.brand_id ?? ''),
41
+ sale_type_key_filters: keyFilters,
42
+ hour_counters: { [String(hour)]: stored.sales_counters },
43
+ matched_documents: 1,
44
+ max_updated_at: stored.updatedAt == null ? null : new Date(stored.updatedAt),
45
+ };
46
+ };
47
+ /** `'HH:00–HH+1:00'` (end wraps at midnight). */
48
+ export const getHourlyComparisonHourLabel = (hour) => {
49
+ const pad = (value) => String(value).padStart(2, '0');
50
+ return `${pad(hour)}:00–${pad((hour + 1) % 24)}:00`;
51
+ };
52
+ /** Business-hour index: 06 → 0, …, 23 → 17, 00 → 18, …, 05 → 23. */
53
+ export const getHourlyComparisonBusinessIndex = (hour) => (hour + 18) % 24;
54
+ const isCancelled = (status) => status === 'CANCELLED';
55
+ const includesCancelled = (filters) => Array.isArray(filters.order_statuses) && filters.order_statuses.includes('CANCELLED');
56
+ const parseKey = (key, keyFilters) => {
57
+ const components = parseMaterializedReportKey(key);
58
+ if (components == null || components.length !== keyFilters.length)
59
+ return null;
60
+ let orderStatus = null;
61
+ let platform = null;
62
+ for (let index = 0; index < keyFilters.length; index += 1) {
63
+ if (keyFilters[index] === MATERIALIZED_REPORT_KEY_FILTER_ENUM.ORDER_STATUS) {
64
+ orderStatus = components[index];
65
+ }
66
+ else if (keyFilters[index] === MATERIALIZED_REPORT_KEY_FILTER_ENUM.PLATFORM) {
67
+ platform = components[index];
68
+ }
69
+ }
70
+ return { order_status: orderStatus, platform };
71
+ };
72
+ const keyMatchesFilters = (parsed, filters) => {
73
+ if (filters.platforms != null &&
74
+ filters.platforms.length > 0 &&
75
+ (parsed.platform == null || !filters.platforms.includes(parsed.platform))) {
76
+ return false;
77
+ }
78
+ if (filters.order_statuses != null &&
79
+ filters.order_statuses.length > 0 &&
80
+ (parsed.order_status == null || !filters.order_statuses.includes(parsed.order_status))) {
81
+ return false;
82
+ }
83
+ return true;
84
+ };
85
+ const emptyFold = () => ({
86
+ byHour: new Map(),
87
+ cancelledByHour: new Map(),
88
+ keyFilters: [],
89
+ matchedDocuments: 0,
90
+ maxUpdatedAt: null,
91
+ });
92
+ const foldDocs = (docs, filters, includeCancelledInTotals) => {
93
+ const fold = emptyFold();
94
+ for (const doc of docs ?? []) {
95
+ if (doc == null)
96
+ continue;
97
+ if (fold.keyFilters.length === 0)
98
+ fold.keyFilters = doc.sale_type_key_filters ?? [];
99
+ fold.matchedDocuments += Number(doc.matched_documents ?? 1) || 0;
100
+ const updatedAt = doc.max_updated_at == null ? null : new Date(doc.max_updated_at);
101
+ if (updatedAt != null &&
102
+ !Number.isNaN(updatedAt.getTime()) &&
103
+ (fold.maxUpdatedAt == null || updatedAt.getTime() > fold.maxUpdatedAt.getTime())) {
104
+ fold.maxUpdatedAt = updatedAt;
105
+ }
106
+ for (const [hourKey, countersByKey] of Object.entries(doc.hour_counters ?? {})) {
107
+ const hour = Number(hourKey);
108
+ if (!Number.isInteger(hour) || hour < 0 || hour > 23)
109
+ continue;
110
+ for (const [key, counters] of Object.entries(countersByKey ?? {})) {
111
+ const parsed = parseKey(key, fold.keyFilters);
112
+ if (parsed == null || !keyMatchesFilters(parsed, filters))
113
+ continue;
114
+ if (isCancelled(parsed.order_status) && !includeCancelledInTotals) {
115
+ const cancelled = fold.cancelledByHour.get(hour) ?? createEmptySalesComparisonCounters();
116
+ addSalesComparisonCounters(cancelled, counters);
117
+ fold.cancelledByHour.set(hour, cancelled);
118
+ continue;
119
+ }
120
+ const byKey = fold.byHour.get(hour) ?? new Map();
121
+ const existing = byKey.get(key) ?? createEmptySalesComparisonCounters();
122
+ addSalesComparisonCounters(existing, counters);
123
+ byKey.set(key, existing);
124
+ fold.byHour.set(hour, byKey);
125
+ }
126
+ }
127
+ }
128
+ return fold;
129
+ };
130
+ /** Sums every non-cancelled hour into one totals block. */
131
+ const totalOf = (fold) => {
132
+ const total = createEmptySalesComparisonCounters();
133
+ for (const byKey of fold.byHour.values()) {
134
+ for (const counters of byKey.values())
135
+ addSalesComparisonCounters(total, counters);
136
+ }
137
+ return total;
138
+ };
139
+ /** Sums the cancelled hour blocks. */
140
+ const cancelledTotalOf = (fold) => {
141
+ const total = createEmptySalesComparisonCounters();
142
+ for (const counters of fold.cancelledByHour.values())
143
+ addSalesComparisonCounters(total, counters);
144
+ return total;
145
+ };
146
+ const buildPage = (group, filters) => {
147
+ const includeCancelled = includesCancelled(filters);
148
+ const currentFold = foldDocs(group.currentDocs, filters, includeCancelled);
149
+ const comparisonFold = group.comparisonDocs == null ? null : foldDocs(group.comparisonDocs, filters, includeCancelled);
150
+ const hours = [];
151
+ for (let businessIndex = 0; businessIndex < 24; businessIndex += 1) {
152
+ // business order 06…23, 00…05
153
+ const hour = (businessIndex + 6) % 24;
154
+ const current = formatSalesComparisonSeries(sumHour(currentFold.byHour.get(hour)));
155
+ const comparison = comparisonFold == null ? null : formatSalesComparisonSeries(sumHour(comparisonFold.byHour.get(hour)));
156
+ hours.push({
157
+ hour,
158
+ label: getHourlyComparisonHourLabel(hour),
159
+ business_index: businessIndex,
160
+ current,
161
+ comparison,
162
+ metrics: buildSalesComparisonMetrics(current, comparison),
163
+ });
164
+ }
165
+ const current = formatSalesComparisonSeries(totalOf(currentFold));
166
+ const comparison = comparisonFold == null ? null : formatSalesComparisonSeries(totalOf(comparisonFold));
167
+ return {
168
+ tenant_ids: [...group.tenantIds].sort(),
169
+ filters: {
170
+ platforms: filters.platforms != null && filters.platforms.length > 0 ? [...filters.platforms] : null,
171
+ order_statuses: filters.order_statuses != null && filters.order_statuses.length > 0 ? [...filters.order_statuses] : null,
172
+ },
173
+ current,
174
+ comparison,
175
+ metrics: buildSalesComparisonMetrics(current, comparison),
176
+ cancelled: formatSalesComparisonSeries(cancelledTotalOf(currentFold)),
177
+ hours,
178
+ hours_with_data: hours.filter((entry) => entry.current.counters.order_count > 0).length,
179
+ matched_documents: currentFold.matchedDocuments,
180
+ max_updated_at: currentFold.maxUpdatedAt,
181
+ };
182
+ };
183
+ const sumHour = (byKey) => {
184
+ const total = createEmptySalesComparisonCounters();
185
+ if (byKey != null)
186
+ for (const counters of byKey.values())
187
+ addSalesComparisonCounters(total, counters);
188
+ return total;
189
+ };
190
+ /**
191
+ * Page-ready Hourly Comparison data.
192
+ *
193
+ * `comparisonDocs` is `null` for `baseline: NONE`; an empty array is a valid
194
+ * baseline window with no sales (a zero profile).
195
+ */
196
+ export function getFormattedTenantHourlyComparisonReporting(currentDocs, comparisonDocs, filters = null, separate_view_per_tenant = false) {
197
+ if (!Array.isArray(currentDocs) || currentDocs.length === 0)
198
+ return null;
199
+ if (currentDocs.some((doc) => doc == null))
200
+ return null;
201
+ if (comparisonDocs != null && !Array.isArray(comparisonDocs))
202
+ return null;
203
+ if (filters != null) {
204
+ if (filters.platforms != null && !Array.isArray(filters.platforms))
205
+ return null;
206
+ if (filters.order_statuses != null && !Array.isArray(filters.order_statuses))
207
+ return null;
208
+ }
209
+ const resolvedFilters = filters ?? {};
210
+ const normalizedCurrent = currentDocs.map(normalizeHourlyDocument);
211
+ if (normalizedCurrent.some((doc) => doc == null))
212
+ return null;
213
+ const normalizedComparison = comparisonDocs == null ? null : comparisonDocs.map(normalizeHourlyDocument);
214
+ if (normalizedComparison != null && normalizedComparison.some((doc) => doc == null))
215
+ return null;
216
+ const usableCurrent = normalizedCurrent;
217
+ if (usableCurrent.length === 0)
218
+ return null;
219
+ const signature = (usableCurrent[0].sale_type_key_filters ?? []).join(':');
220
+ for (const doc of [...usableCurrent, ...(normalizedComparison ?? [])]) {
221
+ if ((doc.sale_type_key_filters ?? []).join(':') !== signature)
222
+ return null;
223
+ }
224
+ const allTenantIds = [...new Set(usableCurrent.map((doc) => String(doc.tenant_id)))].sort();
225
+ const scopes = separate_view_per_tenant ? allTenantIds.map((tenantId) => [tenantId]) : [allTenantIds];
226
+ const pages = [];
227
+ for (const scope of scopes) {
228
+ const currentForScope = usableCurrent.filter((doc) => scope.includes(String(doc.tenant_id)));
229
+ if (currentForScope.length === 0)
230
+ continue;
231
+ const comparisonForScope = normalizedComparison == null
232
+ ? null
233
+ : normalizedComparison.filter((doc) => scope.includes(String(doc.tenant_id)));
234
+ pages.push(buildPage({ tenantIds: scope, currentDocs: currentForScope, comparisonDocs: comparisonForScope }, resolvedFilters));
235
+ }
236
+ return pages.length === 0 ? null : [pages, null];
237
+ }