@managesome/eposmatic-toolkit 0.4.60 → 0.4.61

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 (74) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +177 -4
  2. package/esm/helpers/materialized-reports/index.js +7 -0
  3. package/esm/helpers/materialized-reports/materialized-day-readiness.js +2 -2
  4. package/esm/helpers/materialized-reports/materialized-dinein-accumulator-helpers.js +557 -0
  5. package/esm/helpers/materialized-reports/materialized-dinein-classification-helpers.js +447 -0
  6. package/esm/helpers/materialized-reports/materialized-dinein-close-day-helpers.js +372 -0
  7. package/esm/helpers/materialized-reports/materialized-dinein-compile-helpers.js +500 -0
  8. package/esm/helpers/materialized-reports/materialized-dinein-entry-pages.js +218 -0
  9. package/esm/helpers/materialized-reports/materialized-dinein-key-helpers.js +87 -0
  10. package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +4 -0
  11. package/esm/helpers/materialized-reports/materialized-report-drilldown-rows.js +8 -0
  12. package/esm/helpers/materialized-reports/report-platform/definition.js +93 -1
  13. package/esm/helpers/materialized-reports/tenant-dinein-formatter.js +240 -0
  14. package/esm/helpers/reporting-contract/report-contract.js +3 -0
  15. package/esm/schemas/models/index.js +2 -0
  16. package/esm/schemas/models/materialized_tenant_dinein_entries.js +57 -0
  17. package/esm/schemas/models/materialized_tenant_dinein_reports.js +73 -0
  18. package/esm/schemas/types/index.js +2 -0
  19. package/esm/schemas/types/materialized_report_drilldown_rows.js +1 -0
  20. package/esm/schemas/types/materialized_tenant_dinein_entries.js +1 -0
  21. package/esm/schemas/types/materialized_tenant_dinein_reports.js +6 -0
  22. package/lib/constants/MATERIALIZED_REPORTS.d.ts +160 -4
  23. package/lib/constants/MATERIALIZED_REPORTS.js +178 -5
  24. package/lib/helpers/materialized-reports/index.d.ts +7 -0
  25. package/lib/helpers/materialized-reports/index.js +7 -0
  26. package/lib/helpers/materialized-reports/materialized-day-readiness.d.ts +1 -1
  27. package/lib/helpers/materialized-reports/materialized-day-readiness.js +2 -2
  28. package/lib/helpers/materialized-reports/materialized-dinein-accumulator-helpers.d.ts +67 -0
  29. package/lib/helpers/materialized-reports/materialized-dinein-accumulator-helpers.js +582 -0
  30. package/lib/helpers/materialized-reports/materialized-dinein-classification-helpers.d.ts +245 -0
  31. package/lib/helpers/materialized-reports/materialized-dinein-classification-helpers.js +469 -0
  32. package/lib/helpers/materialized-reports/materialized-dinein-close-day-helpers.d.ts +110 -0
  33. package/lib/helpers/materialized-reports/materialized-dinein-close-day-helpers.js +384 -0
  34. package/lib/helpers/materialized-reports/materialized-dinein-compile-helpers.d.ts +73 -0
  35. package/lib/helpers/materialized-reports/materialized-dinein-compile-helpers.js +504 -0
  36. package/lib/helpers/materialized-reports/materialized-dinein-entry-pages.d.ts +69 -0
  37. package/lib/helpers/materialized-reports/materialized-dinein-entry-pages.js +231 -0
  38. package/lib/helpers/materialized-reports/materialized-dinein-key-helpers.d.ts +44 -0
  39. package/lib/helpers/materialized-reports/materialized-dinein-key-helpers.js +96 -0
  40. package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +4 -0
  41. package/lib/helpers/materialized-reports/materialized-report-drilldown-rows.js +8 -0
  42. package/lib/helpers/materialized-reports/report-platform/definition.js +92 -0
  43. package/lib/helpers/materialized-reports/tenant-dinein-formatter.d.ts +225 -0
  44. package/lib/helpers/materialized-reports/tenant-dinein-formatter.js +243 -0
  45. package/lib/helpers/reporting-contract/report-contract.d.ts +1 -0
  46. package/lib/helpers/reporting-contract/report-contract.js +3 -0
  47. package/lib/schemas/models/index.d.ts +2 -0
  48. package/lib/schemas/models/index.js +2 -0
  49. package/lib/schemas/models/materialized_tenant_day_readiness.d.ts +3 -3
  50. package/lib/schemas/models/materialized_tenant_dinein_entries.d.ts +100 -0
  51. package/lib/schemas/models/materialized_tenant_dinein_entries.js +60 -0
  52. package/lib/schemas/models/materialized_tenant_dinein_reports.d.ts +111 -0
  53. package/lib/schemas/models/materialized_tenant_dinein_reports.js +76 -0
  54. package/lib/schemas/types/index.d.ts +2 -0
  55. package/lib/schemas/types/index.js +2 -0
  56. package/lib/schemas/types/materialized_report_drilldown_rows.d.ts +1 -1
  57. package/lib/schemas/types/materialized_report_drilldown_rows.js +1 -0
  58. package/lib/schemas/types/materialized_tenant_dinein_entries.d.ts +127 -0
  59. package/lib/schemas/types/materialized_tenant_dinein_entries.js +2 -0
  60. package/lib/schemas/types/materialized_tenant_dinein_reports.d.ts +326 -0
  61. package/lib/schemas/types/materialized_tenant_dinein_reports.js +9 -0
  62. package/lib/tests/materialized-reports/dinein-fixtures.d.ts +29 -0
  63. package/lib/tests/materialized-reports/dinein-fixtures.js +130 -0
  64. package/lib/tests/materialized-reports/materialized-day-readiness.test.js +3 -2
  65. package/lib/tests/materialized-reports/materialized-dinein-compile.test.d.ts +1 -0
  66. package/lib/tests/materialized-reports/materialized-dinein-compile.test.js +807 -0
  67. package/lib/tests/materialized-reports/materialized-dinein-plan.test.d.ts +1 -0
  68. package/lib/tests/materialized-reports/materialized-dinein-plan.test.js +419 -0
  69. package/lib/tests/materialized-reports/materialized-tenant-dinein-schema.test.d.ts +1 -0
  70. package/lib/tests/materialized-reports/materialized-tenant-dinein-schema.test.js +185 -0
  71. package/lib/tests/materialized-reports/report-platform/registry-conformance.test.js +15 -2
  72. package/lib/tests/materialized-reports/tenant-dinein-formatter.test.d.ts +1 -0
  73. package/lib/tests/materialized-reports/tenant-dinein-formatter.test.js +299 -0
  74. package/package.json +1 -1
@@ -0,0 +1,447 @@
1
+ import { MATERIALIZED_DINEIN_ORIGIN_ENUM, MATERIALIZED_DINEIN_PAYMENT_CLASS_ENUM, MATERIALIZED_DINEIN_SPLIT_PARENT_DELETE_REASON, MATERIALIZED_DINEIN_SPLIT_ROLE_ENUM, MATERIALIZED_DINEIN_UNKNOWN_TOKEN, } from '../../constants/MATERIALIZED_REPORTS';
2
+ import { PLATFORMS_ENUM } from '../../constants/enums/platform';
3
+ import { emptyMaterializedDineinPaymentRow, freshMaterializedDineinCounters, } from './materialized-dinein-accumulator-helpers';
4
+ const num = (value) => {
5
+ const number = typeof value === 'number' ? value : Number(value);
6
+ return Number.isFinite(number) ? Math.round(number) : 0;
7
+ };
8
+ const toDate = (value) => {
9
+ if (value == null)
10
+ return null;
11
+ const date = value instanceof Date ? value : new Date(value);
12
+ return Number.isNaN(date.getTime()) ? null : date;
13
+ };
14
+ const text = (value) => (value == null ? '' : String(value).trim());
15
+ /** A check belongs to the dine-in population when it carries `dinein_details`. */
16
+ export function isDineinOrder(order) {
17
+ return order != null && order.dinein_details != null;
18
+ }
19
+ /** An auto-deleted split PARENT: never a collectable check. */
20
+ export function isDeletedSplitParent(order) {
21
+ if (order == null)
22
+ return false;
23
+ if (order.is_deleted !== true)
24
+ return false;
25
+ if (text(order.delete_reason) !== MATERIALIZED_DINEIN_SPLIT_PARENT_DELETE_REASON)
26
+ return false;
27
+ return Array.isArray(order.split_child_ids) ? order.split_child_ids.length > 0 : true;
28
+ }
29
+ /** A split CHILD: a collectable check that belongs to a split group. */
30
+ export function isSplitChild(order) {
31
+ return order != null && text(order.split_parent_id) !== '';
32
+ }
33
+ /**
34
+ * The FIRST version of a check.
35
+ *
36
+ * `iterations[0]` is the MOST RECENT previous version and `iterations[last]` is
37
+ * the original one, so the first version is the LAST element — or the current
38
+ * order when there are no iterations. Using `iterations[0]` would read an edit
39
+ * as the first version.
40
+ */
41
+ export function firstVersionOf(order) {
42
+ const iterations = Array.isArray(order?.iterations) ? order.iterations : [];
43
+ if (iterations.length === 0)
44
+ return order;
45
+ return iterations[iterations.length - 1] ?? order;
46
+ }
47
+ /**
48
+ * The frozen origin classification.
49
+ *
50
+ * | first version | staff | consumer phone | origin |
51
+ * | --- | --- | --- | --- |
52
+ * | `POS_DINE-IN` | any | any | `POS_DIRECT` |
53
+ * | `APP_DINE-IN` / `WEB_DINE-IN` | yes | any | `STAFF_TABLET` |
54
+ * | `APP_DINE-IN` / `WEB_DINE-IN` | no | yes | `QR_SELF_ORDER` |
55
+ * | anything else | any | any | `UNKNOWN_ORIGIN` |
56
+ *
57
+ * The phone is read as a boolean only — it is NEVER stored.
58
+ */
59
+ export function classifyMaterializedDineinOrigin(order) {
60
+ if (order == null)
61
+ return MATERIALIZED_DINEIN_ORIGIN_ENUM.UNKNOWN_ORIGIN;
62
+ const first = firstVersionOf(order);
63
+ const platform = text(first.platform ?? order.platform);
64
+ const hasStaff = first.staff_details != null ||
65
+ first.order_taker_details != null;
66
+ const phone = text((first.consumer ?? order.consumer)?.phone);
67
+ const hasConsumerPhone = phone !== '';
68
+ if (platform === PLATFORMS_ENUM.POS_DINE_IN)
69
+ return MATERIALIZED_DINEIN_ORIGIN_ENUM.POS_DIRECT;
70
+ if (platform === PLATFORMS_ENUM.APP_DINE_IN || platform === PLATFORMS_ENUM.WEB_DINE_IN) {
71
+ if (hasStaff)
72
+ return MATERIALIZED_DINEIN_ORIGIN_ENUM.STAFF_TABLET;
73
+ if (hasConsumerPhone)
74
+ return MATERIALIZED_DINEIN_ORIGIN_ENUM.QR_SELF_ORDER;
75
+ }
76
+ return MATERIALIZED_DINEIN_ORIGIN_ENUM.UNKNOWN_ORIGIN;
77
+ }
78
+ /** The role of a counted check in a split bill (a parent is never counted). */
79
+ export function splitRoleOf(order) {
80
+ return isSplitChild(order) ? MATERIALIZED_DINEIN_SPLIT_ROLE_ENUM.CHILD : MATERIALIZED_DINEIN_SPLIT_ROLE_ENUM.NONE;
81
+ }
82
+ /** Total item quantity of one check version. */
83
+ export function itemQuantityOf(order) {
84
+ let total = 0;
85
+ for (const category of order?.categories ?? []) {
86
+ for (const item of category?.items ?? [])
87
+ total += Math.max(0, num(item?.quantity));
88
+ }
89
+ return total;
90
+ }
91
+ /**
92
+ * Item quantity by LINE identity (`_id`, falling back to `item_id`).
93
+ *
94
+ * Two lines of the same menu item are two identities, so an edit that moves one
95
+ * of them is counted on its own line instead of netting against its twin.
96
+ */
97
+ export function itemLineQuantities(order) {
98
+ const quantities = new Map();
99
+ for (const category of order?.categories ?? []) {
100
+ for (const item of category?.items ?? []) {
101
+ const identity = text(item?._id) || text(item?.item_id);
102
+ if (identity === '')
103
+ continue;
104
+ quantities.set(identity, (quantities.get(identity) ?? 0) + Math.max(0, num(item?.quantity)));
105
+ }
106
+ }
107
+ return quantities;
108
+ }
109
+ /**
110
+ * The line delta between two versions of one check.
111
+ *
112
+ * Identity is the item LINE (`_id`, else `item_id`), so moving one of two lines
113
+ * of the same menu item is measured on its own line. A version without
114
+ * categories cannot be compared and contributes nothing.
115
+ */
116
+ export function itemLineDelta(previous, next) {
117
+ if (previous?.categories == null || next?.categories == null) {
118
+ return { additions: 0, removals: 0, net: 0 };
119
+ }
120
+ const before = itemLineQuantities(previous);
121
+ const after = itemLineQuantities(next);
122
+ let additions = 0;
123
+ let removals = 0;
124
+ let net = 0;
125
+ for (const identity of new Set([...before.keys(), ...after.keys()])) {
126
+ const delta = (after.get(identity) ?? 0) - (before.get(identity) ?? 0);
127
+ if (delta > 0)
128
+ additions += delta;
129
+ else if (delta < 0)
130
+ removals += -delta;
131
+ net += delta;
132
+ }
133
+ return { additions, removals, net };
134
+ }
135
+ /** A validated rating (1..5) or null. A 0 / 6 / non-numeric value is NOT a rating. */
136
+ export function validRatingOf(value) {
137
+ const rating = Number(value);
138
+ if (!Number.isInteger(rating) || rating < 1 || rating > 5)
139
+ return null;
140
+ return rating;
141
+ }
142
+ /** A payment's class: `CASH`, `CARD` (`CARD` + `TERMINAL`), everything else `OTHER`. */
143
+ export function paymentClassOf(paymentType) {
144
+ const type = text(paymentType).toUpperCase();
145
+ if (type === 'CASH')
146
+ return MATERIALIZED_DINEIN_PAYMENT_CLASS_ENUM.CASH;
147
+ if (type === 'CARD' || type === 'TERMINAL')
148
+ return MATERIALIZED_DINEIN_PAYMENT_CLASS_ENUM.CARD;
149
+ return MATERIALIZED_DINEIN_PAYMENT_CLASS_ENUM.OTHER;
150
+ }
151
+ /**
152
+ * The collected amount of one payment, NET OF REFUNDS.
153
+ *
154
+ * `amount_paid - amount_refunded`, in integer cents. The report uses the
155
+ * COLLECTED amount (not the order count) for card vs cash, so an order that was
156
+ * partially refunded contributes what was actually kept.
157
+ */
158
+ export function collectedAmountOf(payment) {
159
+ return num(payment?.amount_paid) - num(payment?.amount_refunded);
160
+ }
161
+ /** The refunded amount of one check, summed over its payment rows. */
162
+ export function refundsOf(order) {
163
+ let total = 0;
164
+ for (const payment of order?.payments ?? [])
165
+ total += Math.max(0, num(payment?.amount_refunded));
166
+ return total;
167
+ }
168
+ /** The service-charge rate key: `<PCT|FIXED>:<rate>` (bounded by the document cap). */
169
+ export function serviceChargeRateKeyOf(order) {
170
+ const amount = num(order?.service_fee_amount);
171
+ const isPercentage = order?.is_service_fee_percentage === true;
172
+ if (isPercentage) {
173
+ const value = Number(order?.service_fee);
174
+ const normalized = Number.isFinite(value) ? String(Math.round(value * 100) / 100) : '0';
175
+ return 'PCT:' + normalized;
176
+ }
177
+ return 'FIXED:' + String(amount);
178
+ }
179
+ /** The bounded course key of one course row (`course_id`, falling back to the name). */
180
+ export function courseKeyOf(course) {
181
+ const id = text(course?.course_id);
182
+ if (id !== '')
183
+ return id;
184
+ const name = text(course?.name);
185
+ return name !== '' ? name : MATERIALIZED_DINEIN_UNKNOWN_TOKEN;
186
+ }
187
+ /**
188
+ * Accumulates ONE collectable check into `counters`.
189
+ *
190
+ * `covers` is passed in (already resolved from the fingerprint) because covers
191
+ * are never derived from the order. `tableSessions` are this check's own
192
+ * sessions; the caller already filtered them to joined, deduped fingerprints.
193
+ */
194
+ export function addDineinOrderToCounters(input) {
195
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
196
+ const { counters, order, covers, sessions, rating, splitType } = input;
197
+ const keyStatus = text(order?.order_status) || MATERIALIZED_DINEIN_UNKNOWN_TOKEN;
198
+ const keyPlatform = text(order?.platform) || MATERIALIZED_DINEIN_UNKNOWN_TOKEN;
199
+ const sales = num(order?.amount_earned);
200
+ const serviceCharge = num(order?.service_fee_amount);
201
+ const items = itemQuantityOf(order);
202
+ const isChild = isSplitChild(order);
203
+ counters.orders += 1;
204
+ if (keyStatus === 'COMPLETED')
205
+ counters.completed_orders += 1;
206
+ if (keyStatus === 'CANCELLED')
207
+ counters.cancelled_orders += 1;
208
+ counters.covers += Math.max(0, num(covers));
209
+ counters.items += items;
210
+ counters.sales += sales;
211
+ counters.discounts += num(order?.total_discount_applied ?? order?.order_discount_details?.total_discount);
212
+ counters.tax += num(order?.tax_amount);
213
+ counters.tips += num(order?.tip_amount);
214
+ counters.service_charge_amount += serviceCharge;
215
+ counters.service_charge_taxable_amount += order?.is_service_fee_taxable === true ? serviceCharge : 0;
216
+ counters.surcharge_amount += num(order?.surcharge_amount);
217
+ counters.refunds += refundsOf(order);
218
+ // ---- split bills ----
219
+ if (isChild) {
220
+ counters.split_children += 1;
221
+ counters.split_sales += sales;
222
+ const type = splitType ?? MATERIALIZED_DINEIN_UNKNOWN_TOKEN;
223
+ counters.split_type_counts[type] = num(counters.split_type_counts[type]) + 1;
224
+ }
225
+ // ---- offline ----
226
+ if (order?.synced_offline === true) {
227
+ counters.offline_orders += 1;
228
+ counters.offline_sales += sales;
229
+ }
230
+ // ---- origin ----
231
+ const origin = classifyMaterializedDineinOrigin(order);
232
+ (_a = counters.by_origin)[origin] ?? (_a[origin] = { orders: 0, sales: 0, covers: 0 });
233
+ counters.by_origin[origin].orders += 1;
234
+ counters.by_origin[origin].sales += sales;
235
+ counters.by_origin[origin].covers += Math.max(0, num(covers));
236
+ counters.origin_orders += 1;
237
+ counters.origin_sales += sales;
238
+ counters.origin_covers += Math.max(0, num(covers));
239
+ // ---- service charge composition ----
240
+ if (serviceCharge !== 0) {
241
+ counters.service_charge_orders += 1;
242
+ if (order?.is_service_fee_percentage === true)
243
+ counters.service_charge_percentage_orders += 1;
244
+ else
245
+ counters.service_charge_fixed_orders += 1;
246
+ if (order?.apply_service_fee_on_original_price === true) {
247
+ counters.service_charge_original_price_orders += 1;
248
+ }
249
+ const rateKey = serviceChargeRateKeyOf(order);
250
+ counters.service_charge_rate_counts[rateKey] = num(counters.service_charge_rate_counts[rateKey]) + 1;
251
+ }
252
+ // ---- edits: consecutive versions oldest -> current, current is NOT an edit ----
253
+ const versions = [
254
+ ...(Array.isArray(order?.iterations) ? [...order.iterations].reverse() : []),
255
+ order,
256
+ ];
257
+ const edits = Math.max(0, Array.isArray(order?.iterations) ? order.iterations.length : 0);
258
+ if (edits > 0)
259
+ counters.edited_orders += 1;
260
+ counters.total_edits += edits;
261
+ let additions = 0;
262
+ let removals = 0;
263
+ let net = 0;
264
+ for (let index = 1; index < versions.length; index += 1) {
265
+ // A version without categories cannot be compared; the edit count still
266
+ // counts the version (it IS a recorded edit), the quantity delta does not.
267
+ const delta = itemLineDelta(versions[index - 1], versions[index]);
268
+ additions += delta.additions;
269
+ removals += delta.removals;
270
+ net += delta.net;
271
+ }
272
+ counters.item_additions += additions;
273
+ counters.item_removals += removals;
274
+ counters.net_item_change += net;
275
+ // ---- courses (CURRENT state only: no call history on the order) ----
276
+ for (const course of order?.dinein_details?.courses ?? []) {
277
+ const courseKey = courseKeyOf(course);
278
+ const row = ((_b = counters.by_course)[courseKey] ?? (_b[courseKey] = {
279
+ course_id: courseKey,
280
+ name: text(course?.name) || courseKey,
281
+ assigned: 0,
282
+ called: 0,
283
+ on_hold: 0,
284
+ items: 0,
285
+ }));
286
+ row.assigned += 1;
287
+ if (course?.called === true)
288
+ row.called += 1;
289
+ else
290
+ row.on_hold += 1;
291
+ const courseItems = Array.isArray(course?.item_ids) ? course.item_ids.length : 0;
292
+ row.items += courseItems;
293
+ counters.courses_assigned += 1;
294
+ if (course?.called === true)
295
+ counters.courses_called += 1;
296
+ else
297
+ counters.courses_on_hold += 1;
298
+ counters.course_items += courseItems;
299
+ }
300
+ // ---- card vs cash (collected, net of refunds) ----
301
+ // The AMOUNT is per payment row; the ORDER count is per distinct payment type
302
+ // on the check, so two card rows on one bill count as ONE card order.
303
+ const classes = new Set();
304
+ const amountByType = new Map();
305
+ for (const payment of order?.payments ?? []) {
306
+ const paymentType = text(payment?.payment_type) || MATERIALIZED_DINEIN_UNKNOWN_TOKEN;
307
+ const amount = collectedAmountOf(payment);
308
+ const paymentClass = paymentClassOf(paymentType);
309
+ classes.add(paymentClass);
310
+ if (paymentClass === MATERIALIZED_DINEIN_PAYMENT_CLASS_ENUM.CASH)
311
+ counters.cash_amount += amount;
312
+ else if (paymentClass === MATERIALIZED_DINEIN_PAYMENT_CLASS_ENUM.CARD)
313
+ counters.card_amount += amount;
314
+ else
315
+ counters.other_amount += amount;
316
+ amountByType.set(paymentType, (amountByType.get(paymentType) ?? 0) + amount);
317
+ }
318
+ if (classes.has(MATERIALIZED_DINEIN_PAYMENT_CLASS_ENUM.CASH))
319
+ counters.cash_orders += 1;
320
+ if (classes.has(MATERIALIZED_DINEIN_PAYMENT_CLASS_ENUM.CARD))
321
+ counters.card_orders += 1;
322
+ if (classes.size > 1)
323
+ counters.split_tender_orders += 1;
324
+ for (const [paymentType, amount] of amountByType) {
325
+ const target = ((_c = counters.by_payment_type)[paymentType] ?? (_c[paymentType] = emptyMaterializedDineinPaymentRow()));
326
+ target.amount += amount;
327
+ target.orders += 1;
328
+ }
329
+ // ---- ratings ----
330
+ if (rating != null) {
331
+ counters.rated_orders += 1;
332
+ counters.rating_sum += rating;
333
+ const bucket = ('rating_' + String(rating));
334
+ counters[bucket] += 1;
335
+ }
336
+ if (text(order?.review) !== '')
337
+ counters.review_count += 1;
338
+ // ---- dimensions ----
339
+ // `Number(null)` is 0, so an ABSENT hour must be rejected explicitly — an
340
+ // unbucketed check is never moved to midnight.
341
+ const rawHour = order?.localHour;
342
+ const hour = rawHour == null || rawHour === '' ? NaN : Number(rawHour);
343
+ if (Number.isInteger(hour) && hour >= 0 && hour <= 23) {
344
+ const hourKey = String(hour);
345
+ const hourRow = ((_d = counters.by_hour)[hourKey] ?? (_d[hourKey] = { orders: 0, sales: 0, covers: 0 }));
346
+ hourRow.orders += 1;
347
+ hourRow.sales += sales;
348
+ hourRow.covers += Math.max(0, num(covers));
349
+ }
350
+ // RICH breakdown rows: the page renders checks + covers + sales side by side.
351
+ const statusRow = ((_e = counters.by_status)[keyStatus] ?? (_e[keyStatus] = { orders: 0, covers: 0, sales: 0 }));
352
+ statusRow.orders += 1;
353
+ statusRow.covers += Math.max(0, num(covers));
354
+ statusRow.sales += sales;
355
+ const platformRow = ((_f = counters.by_platform)[keyPlatform] ?? (_f[keyPlatform] = { orders: 0, covers: 0, sales: 0 }));
356
+ platformRow.orders += 1;
357
+ platformRow.covers += Math.max(0, num(covers));
358
+ platformRow.sales += sales;
359
+ const tableId = text(order?.dinein_details?.table_id) || MATERIALIZED_DINEIN_UNKNOWN_TOKEN;
360
+ const floorId = text(order?.dinein_details?.floor_id) || MATERIALIZED_DINEIN_UNKNOWN_TOKEN;
361
+ const tableRow = ((_g = counters.by_table)[tableId] ?? (_g[tableId] = {
362
+ table_id: tableId,
363
+ table_name: text(order?.dinein_details?.table_name),
364
+ floor_id: floorId,
365
+ orders: 0,
366
+ sessions: 0,
367
+ covers: 0,
368
+ occupancy_minutes: 0,
369
+ sales: 0,
370
+ }));
371
+ tableRow.orders += 1;
372
+ tableRow.covers += Math.max(0, num(covers));
373
+ tableRow.sales += sales;
374
+ const floorRow = ((_h = counters.by_floor)[floorId] ?? (_h[floorId] = {
375
+ floor_id: floorId,
376
+ orders: 0,
377
+ sessions: 0,
378
+ covers: 0,
379
+ occupancy_minutes: 0,
380
+ sales: 0,
381
+ }));
382
+ floorRow.orders += 1;
383
+ floorRow.covers += Math.max(0, num(covers));
384
+ floorRow.sales += sales;
385
+ // ---- table sessions / seated occupancy (fingerprint truth) ----
386
+ const tables = new Set();
387
+ for (const session of sessions) {
388
+ tables.add(session.table_id);
389
+ counters.table_sessions += 1;
390
+ counters.occupancy_minutes_sum += session.duration_minutes;
391
+ if (session.fallback_start)
392
+ counters.fallback_occupancy_sessions += 1;
393
+ else
394
+ counters.occupancy_sessions += 1;
395
+ const sessionTable = ((_j = counters.by_table)[_k = session.table_id] ?? (_j[_k] = {
396
+ table_id: session.table_id,
397
+ table_name: session.table_name,
398
+ floor_id: session.floor_id,
399
+ orders: 0,
400
+ sessions: 0,
401
+ covers: 0,
402
+ occupancy_minutes: 0,
403
+ sales: 0,
404
+ }));
405
+ sessionTable.sessions += 1;
406
+ sessionTable.occupancy_minutes += session.duration_minutes;
407
+ const sessionFloor = ((_l = counters.by_floor)[_m = session.floor_id] ?? (_l[_m] = {
408
+ floor_id: session.floor_id,
409
+ orders: 0,
410
+ sessions: 0,
411
+ covers: 0,
412
+ occupancy_minutes: 0,
413
+ sales: 0,
414
+ }));
415
+ sessionFloor.sessions += 1;
416
+ sessionFloor.occupancy_minutes += session.duration_minutes;
417
+ }
418
+ // The check's own table is "served" even when its fingerprint is missing.
419
+ tables.add(tableId);
420
+ counters.tables_served += tables.size;
421
+ }
422
+ /** A fresh accumulator entry for one key. */
423
+ export function freshDineinKeyCounters() {
424
+ return freshMaterializedDineinCounters();
425
+ }
426
+ /** The coverage counters of a compiled day, initialized to zero. */
427
+ export function freshDineinCoverage() {
428
+ return {
429
+ orders_counted: 0,
430
+ orders_out_of_day: 0,
431
+ deleted_split_parents_excluded: 0,
432
+ ratings_from_deleted_parents: 0,
433
+ orders_without_fingerprint: 0,
434
+ fingerprints_total: 0,
435
+ fingerprints_joined: 0,
436
+ fingerprints_duplicate: 0,
437
+ fingerprints_unattached: 0,
438
+ fingerprints_missing_start: 0,
439
+ fingerprints_missing_end: 0,
440
+ fingerprints_negative_duration: 0,
441
+ fingerprints_fallback_start: 0,
442
+ reservations_counted: 0,
443
+ reservations_out_of_day: 0,
444
+ reservations_missing_time: 0,
445
+ };
446
+ }
447
+ export { toDate as toDineinDate, num as toDineinInt };