@managesome/eposmatic-toolkit 0.4.4 → 0.4.7

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 (60) hide show
  1. package/esm/constants/REPORT_CALENDAR.js +47 -0
  2. package/esm/constants/index.js +1 -0
  3. package/esm/helpers/materialized-reports/index.js +2 -0
  4. package/esm/helpers/materialized-reports/materialized-reports-read-helpers.js +20 -21
  5. package/esm/helpers/materialized-reports/materialized-reports-scope-group-helpers.js +281 -0
  6. package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +473 -0
  7. package/esm/helpers/materialized-reports/tenant-sales-overview-formatter.js +126 -7
  8. package/esm/helpers/materialized-reports/tenant-sales-overview-helpers.js +47 -1
  9. package/esm/helpers/order/generate-order.js +11 -0
  10. package/esm/helpers/store/common-order-actions.js +7 -3
  11. package/esm/helpers/types/index.js +1 -0
  12. package/esm/helpers/types/materialized-reports-apis.js +1 -1
  13. package/esm/helpers/types/report-calendar.js +1 -0
  14. package/lib/constants/REPORT_CALENDAR.d.ts +31 -0
  15. package/lib/constants/REPORT_CALENDAR.js +50 -0
  16. package/lib/constants/index.d.ts +1 -0
  17. package/lib/constants/index.js +1 -0
  18. package/lib/helpers/materialized-reports/index.d.ts +2 -0
  19. package/lib/helpers/materialized-reports/index.js +2 -0
  20. package/lib/helpers/materialized-reports/materialized-reports-read-helpers.d.ts +20 -21
  21. package/lib/helpers/materialized-reports/materialized-reports-read-helpers.js +20 -21
  22. package/lib/helpers/materialized-reports/materialized-reports-scope-group-helpers.d.ts +58 -0
  23. package/lib/helpers/materialized-reports/materialized-reports-scope-group-helpers.js +286 -0
  24. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.d.ts +25 -0
  25. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +479 -0
  26. package/lib/helpers/materialized-reports/tenant-sales-overview-formatter.d.ts +24 -0
  27. package/lib/helpers/materialized-reports/tenant-sales-overview-formatter.js +127 -7
  28. package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.js +47 -1
  29. package/lib/helpers/order/generate-order.d.ts +2 -0
  30. package/lib/helpers/order/generate-order.js +13 -0
  31. package/lib/helpers/store/common-order-actions.js +7 -3
  32. package/lib/helpers/types/index.d.ts +1 -0
  33. package/lib/helpers/types/index.js +1 -0
  34. package/lib/helpers/types/materialized-reports-apis.d.ts +117 -25
  35. package/lib/helpers/types/materialized-reports-apis.js +1 -0
  36. package/lib/helpers/types/report-calendar.d.ts +20 -0
  37. package/lib/helpers/types/report-calendar.js +2 -0
  38. package/lib/schemas/models/internal_invoices.d.ts +3 -3
  39. package/lib/schemas/models/materialized_tenant_sales_overview_reports.d.ts +6 -6
  40. package/lib/schemas/models/orders.d.ts +692 -692
  41. package/lib/schemas/models/tenant_purchase_orders.d.ts +90 -90
  42. package/lib/schemas/models/tenants.d.ts +9 -9
  43. package/lib/schemas/models/warehouse_communications.d.ts +3 -3
  44. package/lib/schemas/models/warehouse_internal_movements.d.ts +48 -48
  45. package/lib/schemas/models/warehouse_purchase_order_rfqs.d.ts +63 -63
  46. package/lib/schemas/models/warehouse_purchase_orders.d.ts +96 -96
  47. package/lib/schemas/models/warehouse_sale_invoices.d.ts +3 -3
  48. package/lib/schemas/models/warehouse_sale_order_rfqs.d.ts +69 -69
  49. package/lib/schemas/models/warehouse_sale_orders.d.ts +45 -45
  50. package/lib/schemas/models/warehouses.d.ts +6 -6
  51. package/lib/tests/materialized-reports/materialized-reports-scope-group-helpers.test.d.ts +1 -0
  52. package/lib/tests/materialized-reports/materialized-reports-scope-group-helpers.test.js +275 -0
  53. package/lib/tests/materialized-reports/materialized-reports-selection-helpers.test.d.ts +1 -0
  54. package/lib/tests/materialized-reports/materialized-reports-selection-helpers.test.js +166 -0
  55. package/lib/tests/materialized-reports/tenant-sales-overview-formatter-aggregated.test.d.ts +1 -0
  56. package/lib/tests/materialized-reports/tenant-sales-overview-formatter-aggregated.test.js +143 -0
  57. package/lib/tests/materialized-reports/tenant-sales-overview-helpers.test.js +22 -0
  58. package/lib/tests/num-of-guests-limit.test.d.ts +1 -0
  59. package/lib/tests/num-of-guests-limit.test.js +76 -0
  60. package/package.json +1 -1
@@ -0,0 +1,286 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MATERIALIZED_REPORT_WINDOW_KIND = void 0;
4
+ exports.compactMaterializedReportDayKeys = compactMaterializedReportDayKeys;
5
+ exports.buildMaterializedReportScopeGroups = buildMaterializedReportScopeGroups;
6
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
7
+ const materialized_reports_read_helpers_1 = require("./materialized-reports-read-helpers");
8
+ /**
9
+ * Smart scope-group compiler for materialized-report reads — turns a
10
+ * selection into the FEWEST exact-scope groups the read needs
11
+ * (`IGetMaterializedReportScopeGroup[]`, the `scope_groups` body field):
12
+ *
13
+ * - **compaction**: a contiguous day set is folded into the largest EXACT
14
+ * periods (year → semester → quarter → month → week) before falling back
15
+ * to `localDate` values — e.g. Sep 1–30 becomes ONE `localMonth: ['2026-09']`
16
+ * instead of 30 day values (one document instead of 30);
17
+ * - **recurring window**: the same hours on every selected day → a single
18
+ * `localHour` group (the dedicated time-of-day-window read);
19
+ * - **span window**: from `x:00` on the first day CONTINUOUSLY through
20
+ * `y:00` on the last day — boundary days carry only their partial hour
21
+ * labels (business-day continuous: the start day runs to its 06:00
22
+ * boundary including the post-midnight tail, the end day starts at 06:00),
23
+ * the full days in between compact through the same period logic. What the
24
+ * span does NOT read (absent documents) counts as zero, and groups NEVER
25
+ * overlap — the backend simply unions them.
26
+ *
27
+ * `compactMaterializedReportDayKeys` is exported for direct testing.
28
+ *
29
+ * Returns `null` (never throws) for malformed input — `null` means the
30
+ * caller passed invalid data.
31
+ */
32
+ /** Window refinement kinds (see the module docblock). */
33
+ exports.MATERIALIZED_REPORT_WINDOW_KIND = {
34
+ RECURRING: 'RECURRING',
35
+ SPAN: 'SPAN',
36
+ };
37
+ /* ------------------------------------------------------------------------ */
38
+ /* Date-key math (UTC — keys are timezone-less) */
39
+ /* ------------------------------------------------------------------------ */
40
+ const KEY_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
41
+ const keyToDate = (key) => {
42
+ if (typeof key !== 'string' || !KEY_PATTERN.test(key))
43
+ return null;
44
+ const [year, month, day] = key.split('-').map(Number);
45
+ const date = new Date(Date.UTC(year, month - 1, day));
46
+ // reject impossible dates (e.g. 2026-02-30)
47
+ if (date.getUTCFullYear() !== year || date.getUTCMonth() !== month - 1 || date.getUTCDate() !== day) {
48
+ return null;
49
+ }
50
+ return date;
51
+ };
52
+ const dateToKey = (date) => `${date.getUTCFullYear()}-${`${date.getUTCMonth() + 1}`.padStart(2, '0')}-${`${date.getUTCDate()}`.padStart(2, '0')}`;
53
+ const addDays = (date, amount) => new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate() + amount));
54
+ const DAY_MS = 86400000;
55
+ /** Whole days from `from` to `to` (negative when `to` precedes `from`). */
56
+ const daysBetween = (from, to) => Math.round((to.getTime() - from.getTime()) / DAY_MS);
57
+ /** Sunday-based week start (the `localWeek` bucket identity). */
58
+ const weekStartOf = (date) => addDays(date, -date.getUTCDay());
59
+ /**
60
+ * Periods that START exactly on `date`, largest first: year → semester →
61
+ * quarter → month → week (Sunday-based). Only exact starts are candidates —
62
+ * a run can only compact at its boundaries.
63
+ */
64
+ const periodCandidatesAt = (date) => {
65
+ const year = date.getUTCFullYear();
66
+ const month = date.getUTCMonth() + 1;
67
+ const day = date.getUTCDate();
68
+ const candidates = [];
69
+ const span = (startMonth, months) => {
70
+ const start = new Date(Date.UTC(year, startMonth - 1, 1));
71
+ const end = addDays(new Date(Date.UTC(year, startMonth - 1 + months, 1)), -1);
72
+ return { startKey: dateToKey(start), endKey: dateToKey(end) };
73
+ };
74
+ if (month === 1 && day === 1) {
75
+ const { startKey, endKey } = span(1, 12);
76
+ candidates.push({ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_YEAR, label: `${year}`, startKey, endKey });
77
+ }
78
+ if ((month === 1 || month === 7) && day === 1) {
79
+ const { startKey, endKey } = span(month, 6);
80
+ candidates.push({
81
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_SEMESTER,
82
+ label: `${year}-S${month === 1 ? 1 : 2}`,
83
+ startKey,
84
+ endKey,
85
+ });
86
+ }
87
+ if ([1, 4, 7, 10].includes(month) && day === 1) {
88
+ const { startKey, endKey } = span(month, 3);
89
+ candidates.push({
90
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_QUARTER,
91
+ label: `${year}-Q${Math.floor((month - 1) / 3) + 1}`,
92
+ startKey,
93
+ endKey,
94
+ });
95
+ }
96
+ if (day === 1) {
97
+ const { startKey, endKey } = span(month, 1);
98
+ candidates.push({
99
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH,
100
+ label: `${year}-${`${month}`.padStart(2, '0')}`,
101
+ startKey,
102
+ endKey,
103
+ });
104
+ }
105
+ if (date.getUTCDay() === 0) {
106
+ const startKey = dateToKey(date);
107
+ const endKey = dateToKey(addDays(date, 6));
108
+ candidates.push({ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK, label: startKey, startKey, endKey });
109
+ }
110
+ return candidates;
111
+ };
112
+ /* ------------------------------------------------------------------------ */
113
+ /* Compaction */
114
+ /* ------------------------------------------------------------------------ */
115
+ const GROUP_ORDER = [
116
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_YEAR,
117
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_SEMESTER,
118
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_QUARTER,
119
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH,
120
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK,
121
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
122
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR,
123
+ ];
124
+ /** Ordered, deduped, non-empty groups from a context → values map. */
125
+ const toGroups = (values) => {
126
+ const groups = [];
127
+ for (const scope_context of GROUP_ORDER) {
128
+ const set = values.get(scope_context);
129
+ if (set != null && set.size > 0) {
130
+ groups.push({ scope_context, scope_field: [...set].sort() });
131
+ }
132
+ }
133
+ return groups;
134
+ };
135
+ const addValue = (values, scope_context, value) => {
136
+ const set = values.get(scope_context) ?? new Set();
137
+ set.add(value);
138
+ values.set(scope_context, set);
139
+ };
140
+ /**
141
+ * Folds a contiguous day set into the largest EXACT periods (year →
142
+ * semester → quarter → month → week) + `localDate` leftovers — never
143
+ * partial coverage: a period compacts only when the WHOLE period is inside
144
+ * a contiguous run. Returns `null` for malformed/empty input.
145
+ */
146
+ function compactMaterializedReportDayKeys(days) {
147
+ if (!Array.isArray(days) || days.length === 0)
148
+ return null;
149
+ const dates = [];
150
+ for (const day of days) {
151
+ const date = keyToDate(day);
152
+ if (date == null)
153
+ return null;
154
+ dates.push(date);
155
+ }
156
+ dates.sort((a, b) => a.getTime() - b.getTime());
157
+ const values = new Map();
158
+ let index = 0;
159
+ while (index < dates.length) {
160
+ // contiguous run [runStart..runEnd]
161
+ const runStart = dates[index];
162
+ let runEnd = runStart;
163
+ let next = index + 1;
164
+ while (next < dates.length && daysBetween(runEnd, dates[next]) === 1) {
165
+ runEnd = dates[next];
166
+ next += 1;
167
+ }
168
+ let cursor = runStart;
169
+ while (cursor.getTime() <= runEnd.getTime()) {
170
+ const candidate = periodCandidatesAt(cursor).find((entry) => keyToDate(entry.endKey).getTime() <= runEnd.getTime());
171
+ if (candidate != null) {
172
+ addValue(values, candidate.scope_context, candidate.label);
173
+ cursor = addDays(keyToDate(candidate.endKey), 1);
174
+ }
175
+ else {
176
+ addValue(values, MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE, dateToKey(cursor));
177
+ cursor = addDays(cursor, 1);
178
+ }
179
+ }
180
+ index = next;
181
+ }
182
+ return toGroups(values);
183
+ }
184
+ /* ------------------------------------------------------------------------ */
185
+ /* Scope groups */
186
+ /* ------------------------------------------------------------------------ */
187
+ const isValidHour = (hour, max) => Number.isInteger(hour) && hour >= 0 && hour <= max;
188
+ /**
189
+ * Builds the read's scope groups from a selection spec (see the module
190
+ * docblock). Returns `null` for malformed input or an unservable window.
191
+ */
192
+ function buildMaterializedReportScopeGroups(spec) {
193
+ if (spec == null)
194
+ return null;
195
+ const { days, window } = spec;
196
+ if (!Array.isArray(days) || days.length === 0)
197
+ return null;
198
+ const dates = [];
199
+ for (const day of days) {
200
+ const date = keyToDate(day);
201
+ if (date == null)
202
+ return null;
203
+ dates.push(date);
204
+ }
205
+ dates.sort((a, b) => a.getTime() - b.getTime());
206
+ const dayKeys = [...new Set(dates.map(dateToKey))];
207
+ const uniqueDates = dayKeys.map((key) => keyToDate(key));
208
+ // A SPAN window reads the CONTINUOUS interval first-day → last-day — a day
209
+ // set with gaps would silently pull the gap days in as full middle days.
210
+ if (window != null && window.kind === exports.MATERIALIZED_REPORT_WINDOW_KIND.SPAN) {
211
+ for (let index = 1; index < uniqueDates.length; index += 1) {
212
+ if (daysBetween(uniqueDates[index - 1], uniqueDates[index]) !== 1) {
213
+ return null;
214
+ }
215
+ }
216
+ }
217
+ // No window → compaction only.
218
+ if (window == null) {
219
+ return compactMaterializedReportDayKeys(dayKeys);
220
+ }
221
+ const { fromHour, toHour, kind } = window;
222
+ if (!isValidHour(fromHour, 23) || !isValidHour(toHour, 23))
223
+ return null;
224
+ // Recurring: the same hours on every selected day.
225
+ if (kind === exports.MATERIALIZED_REPORT_WINDOW_KIND.RECURRING) {
226
+ const labels = (0, materialized_reports_read_helpers_1.getMaterializedHourWindowScopeFields)(dayKeys, fromHour, toHour);
227
+ return labels == null ? null : [{ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR, scope_field: labels }];
228
+ }
229
+ // Span (business-day continuous).
230
+ const startDate = uniqueDates[0];
231
+ const endDate = uniqueDates[uniqueDates.length - 1];
232
+ const x = fromHour;
233
+ const y = toHour;
234
+ // Single calendar day, overnight window (e.g. 18:00 → 2:00): one business
235
+ // day covers it whole.
236
+ if (daysBetween(startDate, endDate) === 0 && x >= 6 && y <= 6) {
237
+ if (x === 6 && y === 6) {
238
+ return compactMaterializedReportDayKeys([dateToKey(startDate)]);
239
+ }
240
+ const labels = (0, materialized_reports_read_helpers_1.getMaterializedHourWindowScopeFields)([dateToKey(startDate)], x, y);
241
+ return labels == null ? null : [{ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR, scope_field: labels }];
242
+ }
243
+ // Boundary business days: a start before 06:00 belongs to the previous
244
+ // business day's tail; an end at/before 06:00 ends the previous business
245
+ // day.
246
+ const startBus = x >= 6 ? startDate : addDays(startDate, -1);
247
+ const endBus = y > 6 ? endDate : addDays(endDate, -1);
248
+ // One business day hosts both boundaries (e.g. a full day 06:00 → 06:00).
249
+ if (startBus.getTime() === endBus.getTime()) {
250
+ if (x === 6 && y === 6) {
251
+ return compactMaterializedReportDayKeys([dateToKey(startBus)]);
252
+ }
253
+ const labels = (0, materialized_reports_read_helpers_1.getMaterializedHourWindowScopeFields)([dateToKey(startBus)], x, y);
254
+ return labels == null ? null : [{ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR, scope_field: labels }];
255
+ }
256
+ const startLabels = x === 6 ? null : (0, materialized_reports_read_helpers_1.getMaterializedHourWindowScopeFields)([dateToKey(startBus)], x, 6);
257
+ const endLabels = y === 6 ? null : (0, materialized_reports_read_helpers_1.getMaterializedHourWindowScopeFields)([dateToKey(endBus)], 6, y);
258
+ if (x !== 6 && startLabels == null)
259
+ return null;
260
+ if (y !== 6 && endLabels == null)
261
+ return null;
262
+ const values = new Map();
263
+ // Full business days between the boundaries — compacted like any day set
264
+ // (a month-long span middle becomes period values, not day values).
265
+ const firstFullDate = x === 6 ? startBus : addDays(startBus, 1);
266
+ const lastFullDate = y === 6 ? endBus : addDays(endBus, -1);
267
+ if (firstFullDate.getTime() <= lastFullDate.getTime()) {
268
+ const fullDays = [];
269
+ for (let cursor = firstFullDate; cursor.getTime() <= lastFullDate.getTime(); cursor = addDays(cursor, 1)) {
270
+ fullDays.push(dateToKey(cursor));
271
+ }
272
+ const compacted = compactMaterializedReportDayKeys(fullDays);
273
+ if (compacted == null)
274
+ return null;
275
+ for (const group of compacted) {
276
+ for (const value of group.scope_field) {
277
+ addValue(values, group.scope_context, value);
278
+ }
279
+ }
280
+ }
281
+ for (const label of [...(startLabels ?? []), ...(endLabels ?? [])]) {
282
+ addValue(values, MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR, label);
283
+ }
284
+ const groups = toGroups(values);
285
+ return groups.length === 0 ? null : groups;
286
+ }
@@ -0,0 +1,25 @@
1
+ import { IGetMaterializedReportScopeGroup } from '../types/materialized-reports-apis';
2
+ import { IReportCalendarSelection } from '../types/report-calendar';
3
+ /**
4
+ * All day keys covered by a selection — keeps only days that match the
5
+ * active weekday refinement, so non-matching days never contribute to
6
+ * summaries, DateContext bounds, or requests.
7
+ */
8
+ export declare const getDayKeysFromCalendarSelection: (selection: IReportCalendarSelection) => string[];
9
+ /**
10
+ * Whether an hour window `[fromHour, toHour)` fits one business day
11
+ * (06:00 → 06:00) — mirrors the toolkit's materialized window rule
12
+ * (`getMaterializedHourWindowScopeFields`): whole-hour buckets, end-exclusive
13
+ * (18 → 20 = hours 18, 19), midnight wrap allowed (22 → 2), but never a walk
14
+ * crossing the 06:00 business-morning boundary (4 → 8 is invalid). The
15
+ * calendar's time field uses this to disable unservable picker options, so an
16
+ * invalid window can never be composed for the read request.
17
+ */
18
+ export declare const isValidBusinessHourWindow: (fromHour: number, toHour: number) => boolean;
19
+ /**
20
+ * Maps a committed calendar selection to the exact `scope_groups` of the
21
+ * overview read. Returns `null` when the selection cannot be served (LIVE
22
+ * without a committed day, a malformed/empty selection, or unservable
23
+ * refinement input).
24
+ */
25
+ export declare const buildMaterializedReportScopeGroupsFromSelection: (selection: IReportCalendarSelection) => IGetMaterializedReportScopeGroup[] | null;