@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.
- package/esm/constants/MATERIALIZED_REPORTS.js +448 -0
- package/esm/helpers/index.js +3 -0
- package/esm/helpers/materialized-reports/index.js +32 -0
- package/esm/helpers/materialized-reports/materialized-cash-accumulator-helpers.js +114 -0
- package/esm/helpers/materialized-reports/materialized-cash-classification-helpers.js +286 -0
- package/esm/helpers/materialized-reports/materialized-cash-close-day-helpers.js +396 -0
- package/esm/helpers/materialized-reports/materialized-cash-compile-helpers.js +244 -0
- package/esm/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +170 -0
- package/esm/helpers/materialized-reports/materialized-delivery-classification-helpers.js +426 -0
- package/esm/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +372 -0
- package/esm/helpers/materialized-reports/materialized-delivery-compile-helpers.js +214 -0
- package/esm/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +318 -0
- package/esm/helpers/materialized-reports/materialized-discount-classification-helpers.js +213 -0
- package/esm/helpers/materialized-reports/materialized-discount-close-day-helpers.js +604 -0
- package/esm/helpers/materialized-reports/materialized-discount-compile-helpers.js +186 -0
- package/esm/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.js +155 -0
- package/esm/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.js +199 -0
- package/esm/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.js +111 -0
- package/esm/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +88 -0
- package/esm/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.js +394 -0
- package/esm/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.js +167 -0
- package/esm/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +215 -0
- package/esm/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +476 -0
- package/esm/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +201 -0
- package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +7 -0
- package/esm/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.js +189 -0
- package/esm/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.js +157 -0
- package/esm/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.js +588 -0
- package/esm/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.js +175 -0
- package/esm/helpers/materialized-reports/tenant-cash-in-out-formatter.js +156 -0
- package/esm/helpers/materialized-reports/tenant-delivery-formatter.js +230 -0
- package/esm/helpers/materialized-reports/tenant-discount-formatter.js +427 -0
- package/esm/helpers/materialized-reports/tenant-hourly-comparison-formatter.js +237 -0
- package/esm/helpers/materialized-reports/tenant-payments-comparison-formatter.js +279 -0
- package/esm/helpers/materialized-reports/tenant-prep-time-formatter.js +228 -0
- package/esm/helpers/materialized-reports/tenant-sales-comparison-formatter.js +343 -0
- package/esm/helpers/reporting-contract/index.js +2 -0
- package/esm/helpers/reporting-contract/overview-contract.js +122 -0
- package/esm/helpers/reporting-contract/report-contract.js +228 -0
- package/esm/helpers/rider-earnings/index.js +1 -0
- package/esm/helpers/rider-earnings/rider-earnings.js +59 -0
- package/esm/helpers/rider-reports/index.js +2 -0
- package/esm/helpers/rider-reports/rider-daily-report.js +90 -0
- package/esm/helpers/rider-reports/rider-report.js +67 -0
- package/esm/schemas/models/deliveries.js +75 -0
- package/esm/schemas/models/index.js +15 -0
- package/esm/schemas/models/materialized_rider_daily_reports.js +39 -0
- package/esm/schemas/models/materialized_tenant_cash_entries.js +95 -0
- package/esm/schemas/models/materialized_tenant_cash_reports.js +77 -0
- package/esm/schemas/models/materialized_tenant_delivery_entries.js +63 -0
- package/esm/schemas/models/materialized_tenant_delivery_reports.js +63 -0
- package/esm/schemas/models/materialized_tenant_discount_entries.js +156 -0
- package/esm/schemas/models/materialized_tenant_discount_reports.js +333 -0
- package/esm/schemas/models/materialized_tenant_hourly_comparison_entries.js +87 -0
- package/esm/schemas/models/materialized_tenant_hourly_comparison_reports.js +99 -0
- package/esm/schemas/models/materialized_tenant_payments_comparison_entries.js +71 -0
- package/esm/schemas/models/materialized_tenant_payments_comparison_reports.js +76 -0
- package/esm/schemas/models/materialized_tenant_prep_time_entries.js +130 -0
- package/esm/schemas/models/materialized_tenant_prep_time_reports.js +109 -0
- package/esm/schemas/models/materialized_tenant_sales_comparison_entries.js +130 -0
- package/esm/schemas/models/materialized_tenant_sales_comparison_reports.js +154 -0
- package/esm/schemas/types/index.js +15 -0
- package/esm/schemas/types/materialized_rider_daily_reports.js +1 -0
- package/esm/schemas/types/materialized_tenant_cash_entries.js +1 -0
- package/esm/schemas/types/materialized_tenant_cash_reports.js +1 -0
- package/esm/schemas/types/materialized_tenant_delivery_entries.js +1 -0
- package/esm/schemas/types/materialized_tenant_delivery_reports.js +1 -0
- package/esm/schemas/types/materialized_tenant_discount_entries.js +1 -0
- package/esm/schemas/types/materialized_tenant_discount_reports.js +2 -0
- package/esm/schemas/types/materialized_tenant_hourly_comparison_entries.js +1 -0
- package/esm/schemas/types/materialized_tenant_hourly_comparison_reports.js +1 -0
- package/esm/schemas/types/materialized_tenant_payments_comparison_entries.js +1 -0
- package/esm/schemas/types/materialized_tenant_payments_comparison_reports.js +11 -0
- package/esm/schemas/types/materialized_tenant_prep_time_entries.js +1 -0
- package/esm/schemas/types/materialized_tenant_prep_time_reports.js +1 -0
- package/esm/schemas/types/materialized_tenant_sales_comparison_entries.js +1 -0
- package/esm/schemas/types/materialized_tenant_sales_comparison_reports.js +14 -0
- package/lib/constants/MATERIALIZED_REPORTS.d.ts +339 -2
- package/lib/constants/MATERIALIZED_REPORTS.js +450 -1
- package/lib/helpers/index.d.ts +3 -0
- package/lib/helpers/index.js +3 -0
- package/lib/helpers/materialized-reports/index.d.ts +32 -0
- package/lib/helpers/materialized-reports/index.js +32 -0
- package/lib/helpers/materialized-reports/materialized-cash-accumulator-helpers.d.ts +18 -0
- package/lib/helpers/materialized-reports/materialized-cash-accumulator-helpers.js +118 -0
- package/lib/helpers/materialized-reports/materialized-cash-classification-helpers.d.ts +90 -0
- package/lib/helpers/materialized-reports/materialized-cash-classification-helpers.js +295 -0
- package/lib/helpers/materialized-reports/materialized-cash-close-day-helpers.d.ts +102 -0
- package/lib/helpers/materialized-reports/materialized-cash-close-day-helpers.js +409 -0
- package/lib/helpers/materialized-reports/materialized-cash-compile-helpers.d.ts +72 -0
- package/lib/helpers/materialized-reports/materialized-cash-compile-helpers.js +251 -0
- package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.d.ts +7 -0
- package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +173 -0
- package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.d.ts +84 -0
- package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.js +438 -0
- package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.d.ts +97 -0
- package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +385 -0
- package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.d.ts +40 -0
- package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.js +218 -0
- package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.d.ts +52 -0
- package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +324 -0
- package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.d.ts +50 -0
- package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.js +222 -0
- package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.d.ts +182 -0
- package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.js +615 -0
- package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.d.ts +47 -0
- package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.js +193 -0
- package/lib/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.d.ts +64 -0
- package/lib/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.js +163 -0
- package/lib/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.d.ts +61 -0
- package/lib/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.js +207 -0
- package/lib/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.d.ts +23 -0
- package/lib/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.js +116 -0
- package/lib/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.d.ts +25 -0
- package/lib/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +98 -0
- package/lib/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.d.ts +79 -0
- package/lib/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.js +407 -0
- package/lib/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.d.ts +35 -0
- package/lib/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.js +173 -0
- package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.d.ts +66 -0
- package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +224 -0
- package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.d.ts +128 -0
- package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +490 -0
- package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.d.ts +46 -0
- package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +208 -0
- package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.d.ts +8 -0
- package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +8 -1
- package/lib/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.d.ts +48 -0
- package/lib/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.js +204 -0
- package/lib/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.d.ts +22 -0
- package/lib/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.js +161 -0
- package/lib/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.d.ts +163 -0
- package/lib/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.js +603 -0
- package/lib/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.d.ts +68 -0
- package/lib/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.js +182 -0
- package/lib/helpers/materialized-reports/tenant-cash-in-out-formatter.d.ts +98 -0
- package/lib/helpers/materialized-reports/tenant-cash-in-out-formatter.js +159 -0
- package/lib/helpers/materialized-reports/tenant-delivery-formatter.d.ts +133 -0
- package/lib/helpers/materialized-reports/tenant-delivery-formatter.js +233 -0
- package/lib/helpers/materialized-reports/tenant-discount-formatter.d.ts +114 -0
- package/lib/helpers/materialized-reports/tenant-discount-formatter.js +431 -0
- package/lib/helpers/materialized-reports/tenant-hourly-comparison-formatter.d.ts +60 -0
- package/lib/helpers/materialized-reports/tenant-hourly-comparison-formatter.js +243 -0
- package/lib/helpers/materialized-reports/tenant-payments-comparison-formatter.d.ts +75 -0
- package/lib/helpers/materialized-reports/tenant-payments-comparison-formatter.js +286 -0
- package/lib/helpers/materialized-reports/tenant-prep-time-formatter.d.ts +97 -0
- package/lib/helpers/materialized-reports/tenant-prep-time-formatter.js +232 -0
- package/lib/helpers/materialized-reports/tenant-sales-comparison-formatter.d.ts +105 -0
- package/lib/helpers/materialized-reports/tenant-sales-comparison-formatter.js +349 -0
- package/lib/helpers/reporting-contract/index.d.ts +2 -0
- package/lib/helpers/reporting-contract/index.js +18 -0
- package/lib/helpers/reporting-contract/overview-contract.d.ts +55 -0
- package/lib/helpers/reporting-contract/overview-contract.js +126 -0
- package/lib/helpers/reporting-contract/report-contract.d.ts +159 -0
- package/lib/helpers/reporting-contract/report-contract.js +239 -0
- package/lib/helpers/rider-earnings/index.d.ts +1 -0
- package/lib/helpers/rider-earnings/index.js +17 -0
- package/lib/helpers/rider-earnings/rider-earnings.d.ts +58 -0
- package/lib/helpers/rider-earnings/rider-earnings.js +64 -0
- package/lib/helpers/rider-reports/index.d.ts +2 -0
- package/lib/helpers/rider-reports/index.js +18 -0
- package/lib/helpers/rider-reports/rider-daily-report.d.ts +26 -0
- package/lib/helpers/rider-reports/rider-daily-report.js +93 -0
- package/lib/helpers/rider-reports/rider-report.d.ts +49 -0
- package/lib/helpers/rider-reports/rider-report.js +70 -0
- package/lib/helpers/types/materialized-reports-apis.d.ts +297 -1
- package/lib/schemas/models/deliveries.d.ts +54 -0
- package/lib/schemas/models/deliveries.js +75 -0
- package/lib/schemas/models/expense_details.d.ts +3 -3
- package/lib/schemas/models/index.d.ts +15 -0
- package/lib/schemas/models/index.js +15 -0
- package/lib/schemas/models/materialized_rider_daily_reports.d.ts +27 -0
- package/lib/schemas/models/materialized_rider_daily_reports.js +42 -0
- package/lib/schemas/models/materialized_tenant_cash_entries.d.ts +181 -0
- package/lib/schemas/models/materialized_tenant_cash_entries.js +98 -0
- package/lib/schemas/models/materialized_tenant_cash_reports.d.ts +111 -0
- package/lib/schemas/models/materialized_tenant_cash_reports.js +80 -0
- package/lib/schemas/models/materialized_tenant_delivery_entries.d.ts +98 -0
- package/lib/schemas/models/materialized_tenant_delivery_entries.js +66 -0
- package/lib/schemas/models/materialized_tenant_delivery_reports.d.ts +98 -0
- package/lib/schemas/models/materialized_tenant_delivery_reports.js +66 -0
- package/lib/schemas/models/materialized_tenant_discount_entries.d.ts +199 -0
- package/lib/schemas/models/materialized_tenant_discount_entries.js +159 -0
- package/lib/schemas/models/materialized_tenant_discount_reports.d.ts +216 -0
- package/lib/schemas/models/materialized_tenant_discount_reports.js +336 -0
- package/lib/schemas/models/materialized_tenant_hourly_comparison_entries.d.ts +95 -0
- package/lib/schemas/models/materialized_tenant_hourly_comparison_entries.js +82 -0
- package/lib/schemas/models/materialized_tenant_hourly_comparison_reports.d.ts +105 -0
- package/lib/schemas/models/materialized_tenant_hourly_comparison_reports.js +91 -0
- package/lib/schemas/models/materialized_tenant_payments_comparison_entries.d.ts +91 -0
- package/lib/schemas/models/materialized_tenant_payments_comparison_entries.js +70 -0
- package/lib/schemas/models/materialized_tenant_payments_comparison_reports.d.ts +98 -0
- package/lib/schemas/models/materialized_tenant_payments_comparison_reports.js +71 -0
- package/lib/schemas/models/materialized_tenant_prep_time_entries.d.ts +181 -0
- package/lib/schemas/models/materialized_tenant_prep_time_entries.js +133 -0
- package/lib/schemas/models/materialized_tenant_prep_time_reports.d.ts +118 -0
- package/lib/schemas/models/materialized_tenant_prep_time_reports.js +112 -0
- package/lib/schemas/models/materialized_tenant_sales_comparison_entries.d.ts +139 -0
- package/lib/schemas/models/materialized_tenant_sales_comparison_entries.js +120 -0
- package/lib/schemas/models/materialized_tenant_sales_comparison_reports.d.ts +167 -0
- package/lib/schemas/models/materialized_tenant_sales_comparison_reports.js +137 -0
- package/lib/schemas/models/tenant_purchase_order_vendor_returns.d.ts +3 -3
- package/lib/schemas/models/warehouse_internal_movements.d.ts +6 -6
- package/lib/schemas/models/warehouse_purchase_order_receipts.d.ts +3 -3
- package/lib/schemas/models/warehouse_purchase_order_vendor_returns.d.ts +3 -3
- package/lib/schemas/models/warehouse_sale_deliveries.d.ts +6 -6
- package/lib/schemas/models/warehouse_sale_invoices.d.ts +3 -3
- package/lib/schemas/types/deliveries.d.ts +40 -1
- package/lib/schemas/types/index.d.ts +15 -0
- package/lib/schemas/types/index.js +15 -0
- package/lib/schemas/types/materialized_rider_daily_reports.d.ts +37 -0
- package/lib/schemas/types/materialized_rider_daily_reports.js +2 -0
- package/lib/schemas/types/materialized_tenant_cash_entries.d.ts +87 -0
- package/lib/schemas/types/materialized_tenant_cash_entries.js +2 -0
- package/lib/schemas/types/materialized_tenant_cash_reports.d.ts +144 -0
- package/lib/schemas/types/materialized_tenant_cash_reports.js +2 -0
- package/lib/schemas/types/materialized_tenant_delivery_entries.d.ts +98 -0
- package/lib/schemas/types/materialized_tenant_delivery_entries.js +2 -0
- package/lib/schemas/types/materialized_tenant_delivery_reports.d.ts +179 -0
- package/lib/schemas/types/materialized_tenant_delivery_reports.js +2 -0
- package/lib/schemas/types/materialized_tenant_discount_entries.d.ts +81 -0
- package/lib/schemas/types/materialized_tenant_discount_entries.js +2 -0
- package/lib/schemas/types/materialized_tenant_discount_reports.d.ts +209 -0
- package/lib/schemas/types/materialized_tenant_discount_reports.js +3 -0
- package/lib/schemas/types/materialized_tenant_hourly_comparison_entries.d.ts +33 -0
- package/lib/schemas/types/materialized_tenant_hourly_comparison_entries.js +2 -0
- package/lib/schemas/types/materialized_tenant_hourly_comparison_reports.d.ts +40 -0
- package/lib/schemas/types/materialized_tenant_hourly_comparison_reports.js +2 -0
- package/lib/schemas/types/materialized_tenant_payments_comparison_entries.d.ts +48 -0
- package/lib/schemas/types/materialized_tenant_payments_comparison_entries.js +2 -0
- package/lib/schemas/types/materialized_tenant_payments_comparison_reports.d.ts +51 -0
- package/lib/schemas/types/materialized_tenant_payments_comparison_reports.js +14 -0
- package/lib/schemas/types/materialized_tenant_prep_time_entries.d.ts +88 -0
- package/lib/schemas/types/materialized_tenant_prep_time_entries.js +2 -0
- package/lib/schemas/types/materialized_tenant_prep_time_reports.d.ts +132 -0
- package/lib/schemas/types/materialized_tenant_prep_time_reports.js +2 -0
- package/lib/schemas/types/materialized_tenant_sales_comparison_entries.d.ts +43 -0
- package/lib/schemas/types/materialized_tenant_sales_comparison_entries.js +2 -0
- package/lib/schemas/types/materialized_tenant_sales_comparison_reports.d.ts +74 -0
- package/lib/schemas/types/materialized_tenant_sales_comparison_reports.js +17 -0
- package/lib/tests/materialized-reports/analytics-growth-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/analytics-overview-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/analytics-performance-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/fixtures.d.ts +15 -1
- package/lib/tests/materialized-reports/fixtures.js +1 -0
- package/lib/tests/materialized-reports/materialized-cash-compile.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-cash-compile.test.js +196 -0
- package/lib/tests/materialized-reports/materialized-cash-fold.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-cash-fold.test.js +116 -0
- package/lib/tests/materialized-reports/materialized-cash-formatter.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-cash-formatter.test.js +152 -0
- package/lib/tests/materialized-reports/materialized-cash-plan.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-cash-plan.test.js +168 -0
- package/lib/tests/materialized-reports/materialized-cash-reconciliation.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-cash-reconciliation.test.js +267 -0
- package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.js +147 -0
- package/lib/tests/materialized-reports/materialized-delivery-compile.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-delivery-compile.test.js +241 -0
- package/lib/tests/materialized-reports/materialized-delivery-fold.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-delivery-fold.test.js +109 -0
- package/lib/tests/materialized-reports/materialized-delivery-formatter.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-delivery-formatter.test.js +141 -0
- package/lib/tests/materialized-reports/materialized-delivery-plan.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-delivery-plan.test.js +198 -0
- package/lib/tests/materialized-reports/materialized-discount-aggregated-formatter.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-discount-aggregated-formatter.test.js +173 -0
- package/lib/tests/materialized-reports/materialized-discount-classification.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-discount-classification.test.js +339 -0
- package/lib/tests/materialized-reports/materialized-discount-close-day.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-discount-close-day.test.js +506 -0
- package/lib/tests/materialized-reports/materialized-discount-compile.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-discount-compile.test.js +161 -0
- package/lib/tests/materialized-reports/materialized-discount-counter-rows.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-discount-counter-rows.test.js +107 -0
- package/lib/tests/materialized-reports/materialized-discount-entries-accumulation.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-discount-entries-accumulation.test.js +125 -0
- package/lib/tests/materialized-reports/materialized-hourly-comparison-compile.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-hourly-comparison-compile.test.js +146 -0
- package/lib/tests/materialized-reports/materialized-hourly-comparison-formatter.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-hourly-comparison-formatter.test.js +127 -0
- package/lib/tests/materialized-reports/materialized-hourly-comparison-plan.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-hourly-comparison-plan.test.js +138 -0
- package/lib/tests/materialized-reports/materialized-hourly-comparison-selection.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-hourly-comparison-selection.test.js +90 -0
- package/lib/tests/materialized-reports/materialized-payments-comparison-compile.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-payments-comparison-compile.test.js +164 -0
- package/lib/tests/materialized-reports/materialized-payments-comparison-formatter.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-payments-comparison-formatter.test.js +117 -0
- package/lib/tests/materialized-reports/materialized-payments-comparison-plan.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-payments-comparison-plan.test.js +149 -0
- package/lib/tests/materialized-reports/materialized-prep-time-classification.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-prep-time-classification.test.js +273 -0
- package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.js +270 -0
- package/lib/tests/materialized-reports/materialized-prep-time-compile.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-prep-time-compile.test.js +217 -0
- package/lib/tests/materialized-reports/materialized-prep-time-constants.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-prep-time-constants.test.js +84 -0
- package/lib/tests/materialized-reports/materialized-prep-time-plan.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-prep-time-plan.test.js +176 -0
- package/lib/tests/materialized-reports/materialized-sales-comparison-baseline.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-sales-comparison-baseline.test.js +126 -0
- package/lib/tests/materialized-reports/materialized-sales-comparison-compile.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-sales-comparison-compile.test.js +206 -0
- package/lib/tests/materialized-reports/materialized-sales-comparison-formatter.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-sales-comparison-formatter.test.js +179 -0
- package/lib/tests/materialized-reports/materialized-sales-comparison-parity.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-sales-comparison-parity.test.js +196 -0
- package/lib/tests/materialized-reports/materialized-sales-comparison-plan.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-sales-comparison-plan.test.js +241 -0
- package/lib/tests/materialized-reports/materialized-tenant-hourly-comparison-schema.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-tenant-hourly-comparison-schema.test.js +96 -0
- package/lib/tests/materialized-reports/materialized-tenant-payments-comparison-schema.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-tenant-payments-comparison-schema.test.js +112 -0
- package/lib/tests/materialized-reports/materialized-tenant-sales-comparison-schema.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-tenant-sales-comparison-schema.test.js +142 -0
- package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +8 -1
- package/lib/tests/materialized-reports/sales-comparison-reference.d.ts +55 -0
- package/lib/tests/materialized-reports/sales-comparison-reference.js +86 -0
- package/lib/tests/materialized-reports/tenant-discount-formatter.test.d.ts +1 -0
- package/lib/tests/materialized-reports/tenant-discount-formatter.test.js +289 -0
- package/lib/tests/materialized-reports/tenant-prep-time-formatter.test.d.ts +1 -0
- package/lib/tests/materialized-reports/tenant-prep-time-formatter.test.js +354 -0
- package/lib/tests/reporting-contract.test.d.ts +1 -0
- package/lib/tests/reporting-contract.test.js +267 -0
- package/lib/tests/rider-daily-report.test.d.ts +1 -0
- package/lib/tests/rider-daily-report.test.js +86 -0
- package/lib/tests/rider-earnings.test.d.ts +1 -0
- package/lib/tests/rider-earnings.test.js +65 -0
- package/lib/tests/rider-report.test.d.ts +1 -0
- package/lib/tests/rider-report.test.js +98 -0
- package/package.json +10 -1
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { CASH_IN_OUT_REF_ENUM, CASH_IN_OUT_TYPE_ENUM } from '../../constants/enums/cash';
|
|
2
|
+
/**
|
|
3
|
+
* M3 cash - lifecycle classifier + accumulators.
|
|
4
|
+
*
|
|
5
|
+
* ## The classification (decision (i3))
|
|
6
|
+
*
|
|
7
|
+
* Cash ledger rows carry NO explicit lifecycle flag, so provenance is derived from
|
|
8
|
+
* `ref_type` PLUS the note prefix written by the source, with the balance SHAPE as a
|
|
9
|
+
* secondary signal. The source notes are stable, source-verified strings:
|
|
10
|
+
*
|
|
11
|
+
* "Drawer opened - Day ..." -> the opening float
|
|
12
|
+
* "Drawer reopened - ... " -> a reopen re-baseline
|
|
13
|
+
* "Drawer closed - Day ..." -> the count-out
|
|
14
|
+
* "System adjustment - ..." -> an over/short sweep
|
|
15
|
+
*
|
|
16
|
+
* The classifier lives HERE and runs at COMPILE time; the entry rows store the
|
|
17
|
+
* resulting flags, so the read never re-derives them. When the durable
|
|
18
|
+
* `is_lifecycle` write-path stamp lands, only this function changes.
|
|
19
|
+
*
|
|
20
|
+
* ## The three nets, defined EXACTLY
|
|
21
|
+
*
|
|
22
|
+
* - `operator` - pure operator movement. Excludes the float, every reopen, the
|
|
23
|
+
* count-out AND the sweeps. This is the quantity the session
|
|
24
|
+
* document calls `net_movements`, so the two sources must AGREE.
|
|
25
|
+
* - `lifecycle` - float + reopen + count-out, broken out separately.
|
|
26
|
+
* - `system` - the over/short sweeps written at close.
|
|
27
|
+
*
|
|
28
|
+
* NOTE ON THE CONCEPT DOC: section 3.2.2 states the reconciliation as
|
|
29
|
+
* `ledger_net = operator_movement_net + reopen_amount + system_adjustment_net`.
|
|
30
|
+
* That is INCOMPLETE - it omits the float and the count-out, which are also ledger
|
|
31
|
+
* rows. Tracing the close routine shows why they matter: the count-out is written
|
|
32
|
+
* as `CASH_OUT amount = countedCash`, so a fully closed drawer-day nets to ZERO.
|
|
33
|
+
* The identity the harness must assert is therefore:
|
|
34
|
+
*
|
|
35
|
+
* ledger_net = operator.net + lifecycle.float_amount - lifecycle.countout_amount
|
|
36
|
+
* + lifecycle.reopen_amount + system.net
|
|
37
|
+
*
|
|
38
|
+
* which reduces to 0 for a closed day, while BOTH cross-collection checks
|
|
39
|
+
* (`operator.net == sessions.net_movements_amount` and
|
|
40
|
+
* `system.net == -sessions.variance_amount`) hold exactly.
|
|
41
|
+
*/
|
|
42
|
+
/** Note prefixes written by the cash drawer repository. */
|
|
43
|
+
export const CASH_OPENING_NOTE_PREFIX = 'Drawer opened';
|
|
44
|
+
export const CASH_REOPEN_NOTE_PREFIX = 'Drawer reopened';
|
|
45
|
+
export const CASH_COUNT_OUT_NOTE_PREFIX = 'Drawer closed';
|
|
46
|
+
export const CASH_SYSTEM_NOTE_PREFIX = 'System adjustment';
|
|
47
|
+
const noteOf = (row) => (typeof row?.note === 'string' ? row.note : '');
|
|
48
|
+
/**
|
|
49
|
+
* Classifies one ledger row.
|
|
50
|
+
*
|
|
51
|
+
* The note prefix is PRIMARY (it is what the source writes). The balance SHAPE is
|
|
52
|
+
* the fallback, because a lifecycle CASH_IN always opens from a zero balance while
|
|
53
|
+
* an operator cash-in never does - so a row whose note is missing or reworded is
|
|
54
|
+
* still classified correctly rather than silently counted as operator movement.
|
|
55
|
+
*/
|
|
56
|
+
export function classifyCashMovement(row) {
|
|
57
|
+
const refType = String(row?.ref_type ?? '');
|
|
58
|
+
const type = String(row?.type ?? '');
|
|
59
|
+
const note = noteOf(row);
|
|
60
|
+
const startsAtZero = Number(row?.drawer_balance_before ?? 0) === 0;
|
|
61
|
+
const isSystemAdjustment = refType === CASH_IN_OUT_REF_ENUM.SYSTEM_ADJUSTMENT || note.startsWith(CASH_SYSTEM_NOTE_PREFIX);
|
|
62
|
+
const isDrawerCashIn = refType === CASH_IN_OUT_REF_ENUM.DRAWER && type === CASH_IN_OUT_TYPE_ENUM.CASH_IN;
|
|
63
|
+
const isDrawerCashOut = refType === CASH_IN_OUT_REF_ENUM.DRAWER && type === CASH_IN_OUT_TYPE_ENUM.CASH_OUT;
|
|
64
|
+
// The NOTE is authoritative: the source always writes it, so an exact prefix
|
|
65
|
+
// match is trusted outright.
|
|
66
|
+
const isReopen = isDrawerCashIn && !isSystemAdjustment && note.startsWith(CASH_REOPEN_NOTE_PREFIX);
|
|
67
|
+
// The SHAPE is only a fallback, and it cannot distinguish a float from a reopen -
|
|
68
|
+
// both open from a zero balance and both are CASH_IN / DRAWER. Given a note-less
|
|
69
|
+
// row the classifier therefore resolves to the FLOAT (the opening case), and the
|
|
70
|
+
// attribution is CROSS-CHECKED against the session documents instead of guessed:
|
|
71
|
+
// the harness asserts lifecycle.float_amount == sessions.opening_float_amount, so
|
|
72
|
+
// a note-less reopen shows up as a disagreement rather than silently mislabelling.
|
|
73
|
+
// (This is exactly the guessing decision (i3) accepts for v1 and the durable
|
|
74
|
+
// is_lifecycle stamp removes.)
|
|
75
|
+
const isOpeningFloat = isDrawerCashIn &&
|
|
76
|
+
!isSystemAdjustment &&
|
|
77
|
+
!isReopen &&
|
|
78
|
+
(note.startsWith(CASH_OPENING_NOTE_PREFIX) || (note === '' && startsAtZero));
|
|
79
|
+
const isCountOut = isDrawerCashOut && !isSystemAdjustment && (note.startsWith(CASH_COUNT_OUT_NOTE_PREFIX) || note === '');
|
|
80
|
+
const isLifecycle = isReopen || isOpeningFloat || isCountOut || isSystemAdjustment;
|
|
81
|
+
return {
|
|
82
|
+
is_opening_float: isOpeningFloat,
|
|
83
|
+
is_reopen: isReopen,
|
|
84
|
+
is_count_out: isCountOut,
|
|
85
|
+
is_system_adjustment: isSystemAdjustment,
|
|
86
|
+
is_operator: !isLifecycle,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
const emptyFlow = () => ({
|
|
90
|
+
in_count: 0,
|
|
91
|
+
in_amount: 0,
|
|
92
|
+
out_count: 0,
|
|
93
|
+
out_amount: 0,
|
|
94
|
+
});
|
|
95
|
+
/** A zeroed counter block with every ref type present. */
|
|
96
|
+
export function freshCashCounters() {
|
|
97
|
+
const byRef = {};
|
|
98
|
+
for (const refType of Object.values(CASH_IN_OUT_REF_ENUM))
|
|
99
|
+
byRef[refType] = emptyFlow();
|
|
100
|
+
return {
|
|
101
|
+
movements: {
|
|
102
|
+
count: 0,
|
|
103
|
+
in_count: 0,
|
|
104
|
+
in_amount: 0,
|
|
105
|
+
out_count: 0,
|
|
106
|
+
out_amount: 0,
|
|
107
|
+
ledger_net: 0,
|
|
108
|
+
operator: { ...emptyFlow(), count: 0, net: 0 },
|
|
109
|
+
lifecycle: {
|
|
110
|
+
float_count: 0,
|
|
111
|
+
float_amount: 0,
|
|
112
|
+
reopen_count: 0,
|
|
113
|
+
reopen_amount: 0,
|
|
114
|
+
countout_count: 0,
|
|
115
|
+
countout_amount: 0,
|
|
116
|
+
},
|
|
117
|
+
system: { ...emptyFlow(), count: 0, net: 0 },
|
|
118
|
+
by_ref: byRef,
|
|
119
|
+
staff_count: 0,
|
|
120
|
+
staff_ids: [],
|
|
121
|
+
},
|
|
122
|
+
sessions: {
|
|
123
|
+
session_count: 0,
|
|
124
|
+
reopen_count: 0,
|
|
125
|
+
reopened_session_count: 0,
|
|
126
|
+
opening_float_amount: 0,
|
|
127
|
+
net_movements_amount: 0,
|
|
128
|
+
expected_close_amount: 0,
|
|
129
|
+
counted_close_amount: 0,
|
|
130
|
+
variance_amount: 0,
|
|
131
|
+
variance_positive_count: 0,
|
|
132
|
+
variance_negative_count: 0,
|
|
133
|
+
open_sessions_count: 0,
|
|
134
|
+
unmatched_close_count: 0,
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/** A fresh buffer. */
|
|
139
|
+
export function freshCashAccumulator() {
|
|
140
|
+
return { counters: freshCashCounters(), staff: new Set(), seen: new Set() };
|
|
141
|
+
}
|
|
142
|
+
/** Adds one ledger row. Returns false when it was already counted. */
|
|
143
|
+
export function addCashMovement(acc, row) {
|
|
144
|
+
var _a;
|
|
145
|
+
if (acc == null || row == null)
|
|
146
|
+
return false;
|
|
147
|
+
const id = row.cash_in_out_id ?? row._id;
|
|
148
|
+
if (id != null) {
|
|
149
|
+
const key = String(id);
|
|
150
|
+
if (acc.seen.has(key))
|
|
151
|
+
return false;
|
|
152
|
+
acc.seen.add(key);
|
|
153
|
+
}
|
|
154
|
+
const provenance = classifyCashMovement(row);
|
|
155
|
+
const amount = Math.abs(Number(row.amount) || 0);
|
|
156
|
+
const isIn = String(row.type) === CASH_IN_OUT_TYPE_ENUM.CASH_IN;
|
|
157
|
+
const movements = acc.counters.movements;
|
|
158
|
+
if (row.staff_id != null)
|
|
159
|
+
acc.staff.add(String(row.staff_id));
|
|
160
|
+
movements.count += 1;
|
|
161
|
+
if (isIn) {
|
|
162
|
+
movements.in_count += 1;
|
|
163
|
+
movements.in_amount += amount;
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
movements.out_count += 1;
|
|
167
|
+
movements.out_amount += amount;
|
|
168
|
+
}
|
|
169
|
+
movements.ledger_net = movements.in_amount - movements.out_amount;
|
|
170
|
+
// by_ref is a BREAKDOWN of the same rows, so it uses the same classification.
|
|
171
|
+
const refKey = String(row.ref_type ?? CASH_IN_OUT_REF_ENUM.NONE);
|
|
172
|
+
const refFlow = ((_a = movements.by_ref)[refKey] ?? (_a[refKey] = emptyFlow()));
|
|
173
|
+
if (isIn) {
|
|
174
|
+
refFlow.in_count += 1;
|
|
175
|
+
refFlow.in_amount += amount;
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
refFlow.out_count += 1;
|
|
179
|
+
refFlow.out_amount += amount;
|
|
180
|
+
}
|
|
181
|
+
if (provenance.is_operator) {
|
|
182
|
+
const operator = movements.operator;
|
|
183
|
+
operator.count += 1;
|
|
184
|
+
if (isIn) {
|
|
185
|
+
operator.in_count += 1;
|
|
186
|
+
operator.in_amount += amount;
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
operator.out_count += 1;
|
|
190
|
+
operator.out_amount += amount;
|
|
191
|
+
}
|
|
192
|
+
operator.net = operator.in_amount - operator.out_amount;
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
if (provenance.is_system_adjustment) {
|
|
196
|
+
const system = movements.system;
|
|
197
|
+
system.count += 1;
|
|
198
|
+
if (isIn) {
|
|
199
|
+
system.in_count += 1;
|
|
200
|
+
system.in_amount += amount;
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
system.out_count += 1;
|
|
204
|
+
system.out_amount += amount;
|
|
205
|
+
}
|
|
206
|
+
system.net = system.in_amount - system.out_amount;
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
const lifecycle = movements.lifecycle;
|
|
210
|
+
if (provenance.is_opening_float) {
|
|
211
|
+
lifecycle.float_count += 1;
|
|
212
|
+
lifecycle.float_amount += amount;
|
|
213
|
+
}
|
|
214
|
+
else if (provenance.is_reopen) {
|
|
215
|
+
lifecycle.reopen_count += 1;
|
|
216
|
+
lifecycle.reopen_amount += amount;
|
|
217
|
+
}
|
|
218
|
+
else if (provenance.is_count_out) {
|
|
219
|
+
lifecycle.countout_count += 1;
|
|
220
|
+
lifecycle.countout_amount += amount;
|
|
221
|
+
}
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
/** Adds one drawer session document. */
|
|
225
|
+
export function addCashSession(acc, session) {
|
|
226
|
+
if (acc == null || session == null)
|
|
227
|
+
return;
|
|
228
|
+
const sessions = acc.counters.sessions;
|
|
229
|
+
sessions.session_count += 1;
|
|
230
|
+
const reopenedTimes = Number(session.reopened_times) || 0;
|
|
231
|
+
sessions.reopen_count += reopenedTimes;
|
|
232
|
+
if (reopenedTimes > 0)
|
|
233
|
+
sessions.reopened_session_count += 1;
|
|
234
|
+
// The float is preserved across reopens, so it is counted ONCE per session.
|
|
235
|
+
sessions.opening_float_amount += Math.abs(Number(session.opening?.cash_amount) || 0);
|
|
236
|
+
sessions.net_movements_amount += Number(session.net_movements) || 0;
|
|
237
|
+
const closing = session.closing ?? null;
|
|
238
|
+
if (closing == null) {
|
|
239
|
+
// No closing record: a session still IN_USE is normal; anything else is a
|
|
240
|
+
// data-integrity tripwire.
|
|
241
|
+
if (String(session.status) === 'IN_USE')
|
|
242
|
+
sessions.open_sessions_count += 1;
|
|
243
|
+
else
|
|
244
|
+
sessions.unmatched_close_count += 1;
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
// EVERY lifecycle counts, not just the last one.
|
|
248
|
+
//
|
|
249
|
+
// FOUND BY THE DEMO DATASET, not by reading the source: a REOPENED session has two
|
|
250
|
+
// (or more) lifecycles, and the ledger holds an over/short SWEEP for EACH of them -
|
|
251
|
+
// but `closing.difference` only describes the FINAL one. Summing just that made
|
|
252
|
+
// `variance_amount` undercount the day and broke the report's second cross-collection
|
|
253
|
+
// check (`system.net == -variance_amount`) on every reopened drawer-day: measured
|
|
254
|
+
// 120 vs -300 for one drawer, where lifecycle 1 was 420 OVER and lifecycle 2 was 300
|
|
255
|
+
// SHORT, netting to 120. The earlier lifecycle is snapshotted in
|
|
256
|
+
// `reopened_iterations`, so it is recoverable - and summing all of them makes the
|
|
257
|
+
// identity exact again.
|
|
258
|
+
const lifecycles = [closing];
|
|
259
|
+
for (const iteration of Array.isArray(session.reopened_iterations) ? session.reopened_iterations : []) {
|
|
260
|
+
if (iteration != null)
|
|
261
|
+
lifecycles.push(iteration);
|
|
262
|
+
}
|
|
263
|
+
for (const lifecycle of lifecycles) {
|
|
264
|
+
sessions.expected_close_amount += Number(lifecycle.expected_cash_amount) || 0;
|
|
265
|
+
sessions.counted_close_amount += Number(lifecycle.cash_amount) || 0;
|
|
266
|
+
const difference = Number(lifecycle.difference) || 0;
|
|
267
|
+
sessions.variance_amount += difference;
|
|
268
|
+
// SIGN: difference = expected - counted, so POSITIVE = SHORT.
|
|
269
|
+
if (difference > 0)
|
|
270
|
+
sessions.variance_positive_count += 1;
|
|
271
|
+
else if (difference < 0)
|
|
272
|
+
sessions.variance_negative_count += 1;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Materializes the staff roster into the counter block (call before persisting).
|
|
277
|
+
*
|
|
278
|
+
* The IDS are kept alongside the count so a later period fold can take a UNION
|
|
279
|
+
* rather than a sum - a distinct count cannot be added.
|
|
280
|
+
*/
|
|
281
|
+
export function finalizeCashAccumulator(acc) {
|
|
282
|
+
const ids = [...acc.staff].sort();
|
|
283
|
+
acc.counters.movements.staff_ids = ids;
|
|
284
|
+
acc.counters.movements.staff_count = ids.length;
|
|
285
|
+
return acc.counters;
|
|
286
|
+
}
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
import { CASH_KEY_FILTERS, MATERIALIZED_CASH_REPORT_SCOPES, MATERIALIZED_REPORT_TYPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
|
+
import { buildMaterializedTenantReportIteration } from './materialized-tenant-reports-iterations-helpers';
|
|
4
|
+
import { getMaterializedPeriodScopeFields } from './scope-helpers';
|
|
5
|
+
import { freshCashCounters } from './materialized-cash-classification-helpers';
|
|
6
|
+
import { mergeMaterializedCashCounters } from './materialized-cash-accumulator-helpers';
|
|
7
|
+
/**
|
|
8
|
+
* M3 close-day helpers.
|
|
9
|
+
*
|
|
10
|
+
* ## Period counters are RE-FOLDED, never patched
|
|
11
|
+
*
|
|
12
|
+
* The discounted ledgers keep periods current by subtracting a day delta and adding
|
|
13
|
+
* the fresh one. That CANNOT work here, for the same reason it cannot work in the prep
|
|
14
|
+
* time report: cash has a NON-ADDITIVE counter. `staff_count` counts DISTINCT staff,
|
|
15
|
+
* and there is no way to subtract a distinct count - removing the day where a given
|
|
16
|
+
* staff member first appeared would leave a count that no longer corresponds to any
|
|
17
|
+
* set of days. So every touched period is rebuilt from its whole day set.
|
|
18
|
+
*
|
|
19
|
+
* ## The watermark comes from the FOLDED DAYS
|
|
20
|
+
*
|
|
21
|
+
* A folded document that inherited the STORED document watermark would look
|
|
22
|
+
* unchanged to `needsMaterializedCashReportWrite`, and the write would be skipped
|
|
23
|
+
* forever - freezing the period at its first closed day. That bug shipped once in the
|
|
24
|
+
* prep time report and is designed out here from the start.
|
|
25
|
+
*/
|
|
26
|
+
/** Period scopes this report materializes (everything but the day scope). */
|
|
27
|
+
export const MATERIALIZED_CASH_PERIOD_SCOPES = MATERIALIZED_CASH_REPORT_SCOPES.filter((scope) => String(scope) !== 'localDate');
|
|
28
|
+
/** True when the scope is the plain day scope. */
|
|
29
|
+
export const isMaterializedCashDayScope = (scopeContext) => String(scopeContext) === 'localDate';
|
|
30
|
+
const cashScopeKey = (doc) => String(doc.scope_context) + '|' + String(doc.scope_field);
|
|
31
|
+
/**
|
|
32
|
+
* Merges TWO period entry sections, keyed on `entry_key`.
|
|
33
|
+
*
|
|
34
|
+
* Rows are deduplicated by identity rather than concatenated: a source row belongs to
|
|
35
|
+
* exactly one day, so the same `entry_key` appearing twice means the fold saw it
|
|
36
|
+
* twice, and money must never be counted twice.
|
|
37
|
+
*/
|
|
38
|
+
export function mergeMaterializedCashEntrySections(target, source) {
|
|
39
|
+
const rowsByKey = new Map();
|
|
40
|
+
const seenByKey = new Map();
|
|
41
|
+
const absorb = (input) => {
|
|
42
|
+
if (input == null)
|
|
43
|
+
return;
|
|
44
|
+
for (const [key, section] of Object.entries(input)) {
|
|
45
|
+
const rows = rowsByKey.get(key) ?? [];
|
|
46
|
+
const seen = seenByKey.get(key) ?? new Set();
|
|
47
|
+
for (const row of section?.rows ?? []) {
|
|
48
|
+
const identity = String(row.entry_key ?? row.cash_in_out_id ?? '');
|
|
49
|
+
if (identity !== '') {
|
|
50
|
+
if (seen.has(identity))
|
|
51
|
+
continue;
|
|
52
|
+
seen.add(identity);
|
|
53
|
+
}
|
|
54
|
+
rows.push(row);
|
|
55
|
+
}
|
|
56
|
+
rowsByKey.set(key, rows);
|
|
57
|
+
seenByKey.set(key, seen);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
absorb(target);
|
|
61
|
+
absorb(source);
|
|
62
|
+
const out = {};
|
|
63
|
+
for (const [key, rows] of rowsByKey) {
|
|
64
|
+
rows.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime() ||
|
|
65
|
+
(String(a.cash_in_out_id) < String(b.cash_in_out_id) ? 1 : -1));
|
|
66
|
+
out[key] = { rows };
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
/** True when the stored document already carries this exact content. */
|
|
71
|
+
export function needsMaterializedCashReportWrite(existing, next) {
|
|
72
|
+
if (existing == null)
|
|
73
|
+
return true;
|
|
74
|
+
const stored = new Date(existing.source_watermark ?? 0).getTime();
|
|
75
|
+
const incoming = new Date(next.source_watermark ?? 0).getTime();
|
|
76
|
+
if (Number.isNaN(stored) || Number.isNaN(incoming))
|
|
77
|
+
return true;
|
|
78
|
+
return incoming > stored;
|
|
79
|
+
}
|
|
80
|
+
/** True when the stored entry document already carries these exact rows. */
|
|
81
|
+
export function needsMaterializedCashEntriesWrite(existing, next) {
|
|
82
|
+
if (existing == null)
|
|
83
|
+
return true;
|
|
84
|
+
const stored = new Date(existing.source_watermark ?? 0).getTime();
|
|
85
|
+
const incoming = new Date(next.source_watermark ?? 0).getTime();
|
|
86
|
+
if (Number.isNaN(stored) || Number.isNaN(incoming))
|
|
87
|
+
return true;
|
|
88
|
+
return incoming > stored;
|
|
89
|
+
}
|
|
90
|
+
/** Max watermark across the day documents that feed a fold. */
|
|
91
|
+
function watermarkOfDays(dayDocs, fallback) {
|
|
92
|
+
let latest = fallback.getTime();
|
|
93
|
+
for (const doc of dayDocs) {
|
|
94
|
+
const time = new Date(doc?.source_watermark ?? 0).getTime();
|
|
95
|
+
if (!Number.isNaN(time) && time > latest)
|
|
96
|
+
latest = time;
|
|
97
|
+
}
|
|
98
|
+
return new Date(latest);
|
|
99
|
+
}
|
|
100
|
+
/** Folds the counters of every day document that belongs to one period scope. */
|
|
101
|
+
export function foldMaterializedCashPeriodCounters(input) {
|
|
102
|
+
const { periodScope, periodScopeField, dayDocuments } = input;
|
|
103
|
+
const folded = {};
|
|
104
|
+
for (const dayDocument of dayDocuments ?? []) {
|
|
105
|
+
if (!isMaterializedCashDayScope(String(dayDocument?.scope_context)))
|
|
106
|
+
continue;
|
|
107
|
+
const fields = getMaterializedPeriodScopeFields(String(dayDocument.scope_field));
|
|
108
|
+
if (fields == null)
|
|
109
|
+
continue;
|
|
110
|
+
if (fields[periodScope] !== periodScopeField)
|
|
111
|
+
continue;
|
|
112
|
+
const counters = dayDocument.cash_counters ?? {};
|
|
113
|
+
for (const [key, block] of Object.entries(counters)) {
|
|
114
|
+
folded[key] = mergeMaterializedCashCounters(folded[key] ?? freshCashCounters(), block);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return folded;
|
|
118
|
+
}
|
|
119
|
+
/** Folds the statement rows of every day document that belongs to one period scope. */
|
|
120
|
+
export function foldMaterializedCashPeriodRows(input) {
|
|
121
|
+
const { periodScope, periodScopeField, dayDocuments } = input;
|
|
122
|
+
let merged = {};
|
|
123
|
+
for (const dayDocument of dayDocuments ?? []) {
|
|
124
|
+
if (!isMaterializedCashDayScope(String(dayDocument?.scope_context)))
|
|
125
|
+
continue;
|
|
126
|
+
const fields = getMaterializedPeriodScopeFields(String(dayDocument.scope_field));
|
|
127
|
+
if (fields == null)
|
|
128
|
+
continue;
|
|
129
|
+
if (fields[periodScope] !== periodScopeField)
|
|
130
|
+
continue;
|
|
131
|
+
merged = mergeMaterializedCashEntrySections(merged, dayDocument.cash_entries ?? null);
|
|
132
|
+
}
|
|
133
|
+
return merged;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Builds the close-day plan for ONE tenant-day.
|
|
137
|
+
*
|
|
138
|
+
* The DAY scope is written from the compile. Every PERIOD scope the day touches is
|
|
139
|
+
* RE-FOLDED from its whole day set - and the touched set is the union of the closed
|
|
140
|
+
* day's periods AND every removed day's periods, because emptying a day has to clear
|
|
141
|
+
* its contribution from the periods it used to be part of.
|
|
142
|
+
*
|
|
143
|
+
* Returns null on malformed input.
|
|
144
|
+
*/
|
|
145
|
+
export function buildTenantCashCloseDayPlan(input) {
|
|
146
|
+
if (input == null)
|
|
147
|
+
return null;
|
|
148
|
+
const { tenantId, reports, entries, reason } = input;
|
|
149
|
+
if (typeof tenantId !== 'string' || tenantId.length === 0)
|
|
150
|
+
return null;
|
|
151
|
+
if (typeof reason !== 'string' || reason.length === 0)
|
|
152
|
+
return null;
|
|
153
|
+
const computedAt = input.computedAt ?? new Date();
|
|
154
|
+
const compiledReports = (reports ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
|
|
155
|
+
const compiledEntries = (entries ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
|
|
156
|
+
const storedReports = (input.existing ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
|
|
157
|
+
const storedEntries = (input.existingEntries ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
|
|
158
|
+
const compiledDateDoc = compiledReports.find((doc) => isMaterializedCashDayScope(String(doc.scope_context)));
|
|
159
|
+
const localDate = input.localDate ?? (compiledDateDoc == null ? null : String(compiledDateDoc.scope_field));
|
|
160
|
+
if (localDate == null || localDate === '')
|
|
161
|
+
return null;
|
|
162
|
+
const writes = [];
|
|
163
|
+
const entryWrites = [];
|
|
164
|
+
const staleDocuments = [];
|
|
165
|
+
const staleEntryDocuments = [];
|
|
166
|
+
const skippedDocuments = [];
|
|
167
|
+
const skippedEntryDocuments = [];
|
|
168
|
+
const storedReportsByScope = new Map();
|
|
169
|
+
for (const doc of storedReports)
|
|
170
|
+
storedReportsByScope.set(cashScopeKey(doc), doc);
|
|
171
|
+
const storedEntriesByScope = new Map();
|
|
172
|
+
for (const doc of storedEntries)
|
|
173
|
+
storedEntriesByScope.set(cashScopeKey(doc), doc);
|
|
174
|
+
const addWrite = (report) => {
|
|
175
|
+
const persisted = report.iteration == null ? { ...report, iteration: 1 } : report;
|
|
176
|
+
const iteration = buildMaterializedTenantReportIteration(persisted, {
|
|
177
|
+
reportType: MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_REPORTS,
|
|
178
|
+
reason,
|
|
179
|
+
computedAt,
|
|
180
|
+
});
|
|
181
|
+
if (iteration == null)
|
|
182
|
+
return false;
|
|
183
|
+
writes.push({ report: persisted, iteration: iteration });
|
|
184
|
+
return true;
|
|
185
|
+
};
|
|
186
|
+
const addEntryWrite = (report) => {
|
|
187
|
+
const persisted = report.iteration == null ? { ...report, iteration: 1 } : report;
|
|
188
|
+
const iteration = buildMaterializedTenantReportIteration(persisted, {
|
|
189
|
+
reportType: MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES,
|
|
190
|
+
reason,
|
|
191
|
+
computedAt,
|
|
192
|
+
});
|
|
193
|
+
if (iteration == null)
|
|
194
|
+
return false;
|
|
195
|
+
entryWrites.push({ report: persisted, iteration: iteration });
|
|
196
|
+
return true;
|
|
197
|
+
};
|
|
198
|
+
// ---- the DAY scope, straight from the compile ----
|
|
199
|
+
if (compiledDateDoc != null) {
|
|
200
|
+
const scopeKey = cashScopeKey(compiledDateDoc);
|
|
201
|
+
const existing = storedReportsByScope.get(scopeKey);
|
|
202
|
+
const next = {
|
|
203
|
+
...compiledDateDoc,
|
|
204
|
+
_id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
|
|
205
|
+
iteration: Number(existing?.iteration ?? 0) + 1,
|
|
206
|
+
};
|
|
207
|
+
if (needsMaterializedCashReportWrite(existing, next))
|
|
208
|
+
addWrite(next);
|
|
209
|
+
else
|
|
210
|
+
skippedDocuments.push(existing);
|
|
211
|
+
}
|
|
212
|
+
const compiledDateEntries = compiledEntries.find((doc) => isMaterializedCashDayScope(String(doc.scope_context)));
|
|
213
|
+
if (compiledDateEntries != null) {
|
|
214
|
+
const scopeKey = cashScopeKey(compiledDateEntries);
|
|
215
|
+
const existing = storedEntriesByScope.get(scopeKey);
|
|
216
|
+
const next = {
|
|
217
|
+
...compiledDateEntries,
|
|
218
|
+
_id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
|
|
219
|
+
iteration: Number(existing?.iteration ?? 0) + 1,
|
|
220
|
+
};
|
|
221
|
+
if (needsMaterializedCashEntriesWrite(existing, next)) {
|
|
222
|
+
addEntryWrite(next);
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
skippedEntryDocuments.push(existing);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
// ---- DAY deletion: a stored day doc that no longer compiled ----
|
|
229
|
+
const compiledDateKeys = new Set(compiledReports
|
|
230
|
+
.filter((doc) => isMaterializedCashDayScope(String(doc.scope_context)))
|
|
231
|
+
.map((doc) => cashScopeKey(doc)));
|
|
232
|
+
const removedDates = [];
|
|
233
|
+
for (const doc of storedReports) {
|
|
234
|
+
if (!isMaterializedCashDayScope(String(doc.scope_context)))
|
|
235
|
+
continue;
|
|
236
|
+
if (String(doc.scope_field) !== localDate)
|
|
237
|
+
continue;
|
|
238
|
+
if (compiledDateKeys.has(cashScopeKey(doc)))
|
|
239
|
+
continue;
|
|
240
|
+
staleDocuments.push(doc);
|
|
241
|
+
removedDates.push(String(doc.scope_field));
|
|
242
|
+
}
|
|
243
|
+
const compiledDateEntryKeys = new Set(compiledEntries
|
|
244
|
+
.filter((doc) => isMaterializedCashDayScope(String(doc.scope_context)))
|
|
245
|
+
.map((doc) => cashScopeKey(doc)));
|
|
246
|
+
for (const doc of storedEntries) {
|
|
247
|
+
if (!isMaterializedCashDayScope(String(doc.scope_context)))
|
|
248
|
+
continue;
|
|
249
|
+
if (String(doc.scope_field) !== localDate)
|
|
250
|
+
continue;
|
|
251
|
+
if (compiledDateEntryKeys.has(cashScopeKey(doc)))
|
|
252
|
+
continue;
|
|
253
|
+
staleEntryDocuments.push(doc);
|
|
254
|
+
}
|
|
255
|
+
// ---- PERIOD re-fold ----
|
|
256
|
+
const nextDayDocs = storedReports.filter((doc) => {
|
|
257
|
+
if (!isMaterializedCashDayScope(String(doc.scope_context)))
|
|
258
|
+
return false;
|
|
259
|
+
if (String(doc.scope_field) === localDate)
|
|
260
|
+
return false;
|
|
261
|
+
return true;
|
|
262
|
+
});
|
|
263
|
+
const willHaveDay = compiledDateDoc != null;
|
|
264
|
+
if (willHaveDay) {
|
|
265
|
+
nextDayDocs.push(writes.find((w) => isMaterializedCashDayScope(String(w.report.scope_context)))?.report ??
|
|
266
|
+
compiledDateDoc);
|
|
267
|
+
}
|
|
268
|
+
const nextDayEntryDocs = storedEntries.filter((doc) => {
|
|
269
|
+
if (!isMaterializedCashDayScope(String(doc.scope_context)))
|
|
270
|
+
return false;
|
|
271
|
+
return String(doc.scope_field) !== localDate;
|
|
272
|
+
});
|
|
273
|
+
const compiledDateEntryDoc = entryWrites.find((w) => isMaterializedCashDayScope(String(w.report.scope_context)))?.report;
|
|
274
|
+
if (compiledDateEntryDoc != null)
|
|
275
|
+
nextDayEntryDocs.push(compiledDateEntryDoc);
|
|
276
|
+
const touchedPeriods = new Map();
|
|
277
|
+
const noteDay = (day) => {
|
|
278
|
+
const fields = getMaterializedPeriodScopeFields(day);
|
|
279
|
+
if (fields == null)
|
|
280
|
+
return;
|
|
281
|
+
for (const scope of MATERIALIZED_CASH_PERIOD_SCOPES) {
|
|
282
|
+
const field = fields[String(scope)];
|
|
283
|
+
if (field == null || field === '')
|
|
284
|
+
continue;
|
|
285
|
+
touchedPeriods.set(String(scope) + '|' + field, { scope: String(scope), field });
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
noteDay(localDate);
|
|
289
|
+
for (const day of removedDates)
|
|
290
|
+
noteDay(day);
|
|
291
|
+
const periodFieldsOfDay = (day) => getMaterializedPeriodScopeFields(day);
|
|
292
|
+
for (const { scope, field } of touchedPeriods.values()) {
|
|
293
|
+
const folded = foldMaterializedCashPeriodCounters({
|
|
294
|
+
periodScope: scope,
|
|
295
|
+
periodScopeField: field,
|
|
296
|
+
dayDocuments: nextDayDocs,
|
|
297
|
+
});
|
|
298
|
+
const existing = storedReportsByScope.get(scope + '|' + field);
|
|
299
|
+
const keys = Object.keys(folded).sort();
|
|
300
|
+
if (keys.length === 0) {
|
|
301
|
+
if (existing != null)
|
|
302
|
+
staleDocuments.push(existing);
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
const counters = {};
|
|
306
|
+
const counterRows = [];
|
|
307
|
+
for (const key of keys) {
|
|
308
|
+
counters[key] = folded[key];
|
|
309
|
+
counterRows.push({ key, counters: folded[key] });
|
|
310
|
+
}
|
|
311
|
+
const report = {
|
|
312
|
+
_id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
|
|
313
|
+
tenant_id: tenantId,
|
|
314
|
+
brand_id: existing?.brand_id ?? compiledDateDoc?.brand_id ?? '',
|
|
315
|
+
scope_context: scope,
|
|
316
|
+
scope_field: field,
|
|
317
|
+
day_index: null,
|
|
318
|
+
sale_type_key_filters: existing?.sale_type_key_filters ?? [...CASH_KEY_FILTERS],
|
|
319
|
+
cash_counters: counters,
|
|
320
|
+
cash_counter_rows: counterRows,
|
|
321
|
+
iteration: existing?.iteration,
|
|
322
|
+
schema_version: existing?.schema_version ?? 1,
|
|
323
|
+
// FROM THE FOLDED DAYS - never the stored watermark, or the write would be
|
|
324
|
+
// skipped forever and the period would freeze at its first closed day.
|
|
325
|
+
source_watermark: watermarkOfDays(nextDayDocs, existing?.source_watermark ?? computedAt),
|
|
326
|
+
computed_at: computedAt,
|
|
327
|
+
createdAt: existing?.createdAt ?? computedAt,
|
|
328
|
+
updatedAt: computedAt,
|
|
329
|
+
};
|
|
330
|
+
const next = { ...report, iteration: Number(existing?.iteration ?? 0) + 1 };
|
|
331
|
+
if (needsMaterializedCashReportWrite(existing, next)) {
|
|
332
|
+
addWrite(next);
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
skippedDocuments.push(existing);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
for (const { scope, field } of touchedPeriods.values()) {
|
|
339
|
+
const sections = foldMaterializedCashPeriodRows({
|
|
340
|
+
periodScope: scope,
|
|
341
|
+
periodScopeField: field,
|
|
342
|
+
dayDocuments: nextDayEntryDocs,
|
|
343
|
+
});
|
|
344
|
+
const existing = storedEntriesByScope.get(scope + '|' + field);
|
|
345
|
+
const keys = Object.keys(sections);
|
|
346
|
+
if (keys.length === 0) {
|
|
347
|
+
if (existing != null)
|
|
348
|
+
staleEntryDocuments.push(existing);
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
let watermark = existing?.source_watermark ?? null;
|
|
352
|
+
for (const dayDoc of nextDayEntryDocs) {
|
|
353
|
+
const fields = periodFieldsOfDay(String(dayDoc.scope_field));
|
|
354
|
+
if (fields == null)
|
|
355
|
+
continue;
|
|
356
|
+
if (fields[scope] !== field)
|
|
357
|
+
continue;
|
|
358
|
+
const time = new Date(dayDoc.source_watermark ?? 0).getTime();
|
|
359
|
+
if (Number.isNaN(time))
|
|
360
|
+
continue;
|
|
361
|
+
if (watermark == null || time > new Date(watermark).getTime())
|
|
362
|
+
watermark = new Date(time);
|
|
363
|
+
}
|
|
364
|
+
const report = {
|
|
365
|
+
_id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
|
|
366
|
+
tenant_id: tenantId,
|
|
367
|
+
brand_id: existing?.brand_id ?? compiledDateEntries?.brand_id ?? '',
|
|
368
|
+
scope_context: scope,
|
|
369
|
+
scope_field: field,
|
|
370
|
+
day_index: null,
|
|
371
|
+
sale_type_key_filters: existing?.sale_type_key_filters ?? [...CASH_KEY_FILTERS],
|
|
372
|
+
cash_entries: sections,
|
|
373
|
+
iteration: existing?.iteration,
|
|
374
|
+
schema_version: existing?.schema_version ?? 1,
|
|
375
|
+
source_watermark: watermark ?? computedAt,
|
|
376
|
+
computed_at: computedAt,
|
|
377
|
+
createdAt: existing?.createdAt ?? computedAt,
|
|
378
|
+
updatedAt: computedAt,
|
|
379
|
+
};
|
|
380
|
+
const next = { ...report, iteration: Number(existing?.iteration ?? 0) + 1 };
|
|
381
|
+
if (needsMaterializedCashEntriesWrite(existing, next)) {
|
|
382
|
+
addEntryWrite(next);
|
|
383
|
+
}
|
|
384
|
+
else {
|
|
385
|
+
skippedEntryDocuments.push(existing);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return {
|
|
389
|
+
writes,
|
|
390
|
+
entryWrites,
|
|
391
|
+
staleDocuments,
|
|
392
|
+
staleEntryDocuments,
|
|
393
|
+
skippedDocuments,
|
|
394
|
+
skippedEntryDocuments,
|
|
395
|
+
};
|
|
396
|
+
}
|