@managesome/eposmatic-toolkit 0.4.27 → 0.4.29

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 (51) hide show
  1. package/esm/helpers/materialized-reports/index.js +1 -0
  2. package/esm/helpers/materialized-reports/materialized-credit-bills-helpers.js +49 -20
  3. package/esm/helpers/materialized-reports/materialized-day-input-signature.js +103 -0
  4. package/esm/helpers/materialized-reports/materialized-discount-close-day-helpers.js +63 -2
  5. package/esm/helpers/materialized-reports/materialized-discount-compile-helpers.js +18 -0
  6. package/esm/helpers/materialized-reports/scope-helpers.js +27 -0
  7. package/esm/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +122 -9
  8. package/esm/helpers/materialized-reports/tenant-sales-overview-helpers.js +24 -2
  9. package/esm/schemas/models/materialized_credit_bills.js +8 -0
  10. package/esm/schemas/models/materialized_tenant_discount_reports.js +9 -0
  11. package/esm/schemas/models/materialized_tenant_sales_overview_reports.js +9 -0
  12. package/esm/schemas/models/tenant_active_jobs.js +8 -0
  13. package/lib/helpers/materialized-reports/index.d.ts +1 -0
  14. package/lib/helpers/materialized-reports/index.js +1 -0
  15. package/lib/helpers/materialized-reports/materialized-credit-bills-helpers.d.ts +28 -2
  16. package/lib/helpers/materialized-reports/materialized-credit-bills-helpers.js +50 -20
  17. package/lib/helpers/materialized-reports/materialized-day-input-signature.d.ts +66 -0
  18. package/lib/helpers/materialized-reports/materialized-day-input-signature.js +108 -0
  19. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.d.ts +20 -1
  20. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.js +63 -2
  21. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.js +18 -0
  22. package/lib/helpers/materialized-reports/scope-helpers.d.ts +22 -0
  23. package/lib/helpers/materialized-reports/scope-helpers.js +28 -0
  24. package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.d.ts +41 -3
  25. package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +120 -7
  26. package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.d.ts +4 -1
  27. package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.js +24 -2
  28. package/lib/schemas/models/materialized_credit_bills.d.ts +3 -0
  29. package/lib/schemas/models/materialized_credit_bills.js +8 -0
  30. package/lib/schemas/models/materialized_tenant_discount_reports.d.ts +3 -0
  31. package/lib/schemas/models/materialized_tenant_discount_reports.js +9 -0
  32. package/lib/schemas/models/materialized_tenant_sales_overview_reports.d.ts +3 -0
  33. package/lib/schemas/models/materialized_tenant_sales_overview_reports.js +9 -0
  34. package/lib/schemas/models/tenant_active_jobs.d.ts +3 -0
  35. package/lib/schemas/models/tenant_active_jobs.js +8 -0
  36. package/lib/schemas/types/materialized_credit_bills.d.ts +6 -0
  37. package/lib/schemas/types/materialized_tenant_discount_reports.d.ts +6 -0
  38. package/lib/schemas/types/materialized_tenant_reports_iterations.d.ts +9 -0
  39. package/lib/schemas/types/materialized_tenant_sales_overview_reports.d.ts +14 -0
  40. package/lib/schemas/types/tenant_active_jobs.d.ts +8 -0
  41. package/lib/tests/materialized-reports/materialized-day-input-signature.test.d.ts +1 -0
  42. package/lib/tests/materialized-reports/materialized-day-input-signature.test.js +81 -0
  43. package/lib/tests/materialized-reports/materialized-discount-close-day.test.js +89 -0
  44. package/lib/tests/materialized-reports/materialized-discount-scenarios.test.d.ts +1 -0
  45. package/lib/tests/materialized-reports/materialized-discount-scenarios.test.js +676 -0
  46. package/lib/tests/materialized-reports/materialized-discount-sources.test.js +134 -0
  47. package/lib/tests/materialized-reports/real-discount-orders.test.d.ts +1 -0
  48. package/lib/tests/materialized-reports/real-discount-orders.test.js +110 -0
  49. package/lib/tests/materialized-reports/tenant-discount-source-formatter.test.js +11 -2
  50. package/lib/tests/materialized-reports/tenant-sales-overview-close-day-helpers.test.js +162 -0
  51. package/package.json +1 -1
@@ -0,0 +1,676 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const globals_1 = require("@jest/globals");
4
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
5
+ const materialized_discount_compile_helpers_1 = require("../../helpers/materialized-reports/materialized-discount-compile-helpers");
6
+ const materialized_discount_source_helpers_1 = require("../../helpers/materialized-reports/materialized-discount-source-helpers");
7
+ const materialized_integer_contract_1 = require("../../helpers/materialized-reports/materialized-integer-contract");
8
+ /**
9
+ * The Discounts scenarios that the real-order fixture and the classifier tests
10
+ * do not cover, driven through the REAL pipeline: a raw order document ->
11
+ * `compileTenantDiscountReports` -> `buildDiscountSourcesForScope`.
12
+ *
13
+ * The orders below are shaped like the captured production orders
14
+ * (`src/tests/real-discount-orders-2026-10-23.json`): money in integer cents,
15
+ * a percentage RATE in integer BASIS POINTS (`5000` = 50%), the item's money in
16
+ * `_net_amount` / `_gross_amount`.
17
+ *
18
+ * Every case asserts the EXACT money, level, source key, order count, free
19
+ * count and complimentary flag — not the shape.
20
+ */
21
+ const TENANT = 'thailemon.uae';
22
+ const BRAND = 'thailemon.co.nz';
23
+ const DAY = '2026-10-23';
24
+ const MONTH = '2026-10';
25
+ const NOW = new Date('2026-10-23T23:59:00.000Z');
26
+ /** A raw item in the stored shape (money in cents). */
27
+ const rawItem = (over = {}) => ({
28
+ _id: 'item-1',
29
+ item_id: 'catalog-item-1',
30
+ menu_id: 'menu-1',
31
+ item_type: 'ITEM',
32
+ name: 'Pizza',
33
+ price: 10000,
34
+ subtotal: 10000,
35
+ quantity: 1,
36
+ tax_rate: 0,
37
+ _gst_amount: 0,
38
+ _net_amount: 10000,
39
+ _gross_amount: 10000,
40
+ _discount_amount: 0,
41
+ special_instructions: '',
42
+ item_discount_details: null,
43
+ modifiers: [],
44
+ ...over,
45
+ });
46
+ /** A raw order in the stored shape. */
47
+ const rawOrder = (over = {}) => ({
48
+ _id: 'order-1',
49
+ tenant_id: TENANT,
50
+ platform: 'POS_WALK-IN',
51
+ order_status: 'COMPLETED',
52
+ order_number: 1,
53
+ localDate: DAY,
54
+ localHour: 15,
55
+ localMinute: 0,
56
+ categories: [{ category_id: 'cat-1', name: 'FOOD', items: [rawItem()] }],
57
+ payments: [
58
+ {
59
+ _id: 'pay-1',
60
+ payment_type: 'CASH',
61
+ payment_status: 'SETTLED',
62
+ amount_due: 10000,
63
+ amount_paid: 10000,
64
+ amount_refunded: 0,
65
+ card_details: null,
66
+ payment_id: 'pay-1',
67
+ },
68
+ ],
69
+ grand_total: 10000,
70
+ price: 10000,
71
+ amount_earned: 10000,
72
+ amount_collected: 10000,
73
+ total_discount_applied: 0,
74
+ order_discount_details: null,
75
+ is_deleted: false,
76
+ createdAt: new Date('2026-10-23T10:00:00.000Z'),
77
+ updatedAt: new Date('2026-10-23T10:00:00.000Z'),
78
+ order_settled_at: new Date('2026-10-23T10:00:00.000Z'),
79
+ staff_details: null,
80
+ order_taker_details: null,
81
+ wallet_redemptions: null,
82
+ redeemed_offers: null,
83
+ ...over,
84
+ });
85
+ /** Compiles the orders and returns the month scope's source rows. */
86
+ const compile = (orders) => (0, materialized_discount_compile_helpers_1.compileTenantDiscountReports)({
87
+ tenantId: TENANT,
88
+ brandId: BRAND,
89
+ localDate: DAY,
90
+ orders: orders,
91
+ gmtOffset: 5,
92
+ computedAt: NOW,
93
+ });
94
+ const sourcesOf = (orders) => {
95
+ const compiled = compile(orders);
96
+ const entry = compiled.entries.find((candidate) => candidate.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH);
97
+ return {
98
+ compiled,
99
+ sources: (0, materialized_discount_source_helpers_1.buildDiscountSourcesForScope)({
100
+ tenantId: TENANT,
101
+ brandId: BRAND,
102
+ scopeContext: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH,
103
+ scopeField: MONTH,
104
+ dayIndex: null,
105
+ sections: entry.discount_entries,
106
+ computedAt: NOW,
107
+ sourceWatermark: null,
108
+ }),
109
+ };
110
+ };
111
+ const byKey = (sources) => new Map(sources.map((source) => [`${source.source_key}|${source.level}`, source]));
112
+ (0, globals_1.describe)('discount scenarios: voucher', () => {
113
+ (0, globals_1.it)('books an ORDER-level voucher PERCENTAGE as a voucher source in basis points', () => {
114
+ const { compiled, sources } = sourcesOf([
115
+ rawOrder({
116
+ _id: 'order-voucher-pct',
117
+ order_number: 1,
118
+ grand_total: 10000,
119
+ amount_earned: 10000,
120
+ amount_collected: 8000,
121
+ total_discount_applied: 2000,
122
+ order_discount_details: {
123
+ voucher_discount: 2000, // 20% in BASIS POINTS
124
+ voucher_discount_id: 'voucher-offer-1',
125
+ voucher_discount_name: '20% Voucher',
126
+ is_voucher_discount_percentage: true,
127
+ total_discount: 2000,
128
+ },
129
+ }),
130
+ ]);
131
+ expect(compiled.contributingOrders).toBe(1);
132
+ const source = byKey(sources).get(`voucher:voucher-offer-1|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
133
+ expect(source).toBeDefined();
134
+ expect(source.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.VOUCHER);
135
+ expect(source.sub_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER);
136
+ expect(source.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER);
137
+ expect(source.kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT);
138
+ expect(source.rate_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE);
139
+ // 2000 bps = 20%, stored as the exact integer sum and count.
140
+ expect(source.percentage_rate_sum).toBe(2000);
141
+ expect(source.percentage_row_count).toBe(1);
142
+ expect(source.fixed_row_count).toBe(0);
143
+ expect(source.discount_amount).toBe(2000);
144
+ expect(source.order_count).toBe(1);
145
+ expect(source.row_count).toBe(1);
146
+ // NO stored average rate.
147
+ expect('rate' in source).toBe(false);
148
+ });
149
+ });
150
+ (0, globals_1.describe)('discount scenarios: cancelled orders', () => {
151
+ (0, globals_1.it)('keeps a cancelled order OUT of the active totals and IN the cancelled section', () => {
152
+ const { compiled, sources } = sourcesOf([
153
+ rawOrder({
154
+ _id: 'order-active',
155
+ order_number: 1,
156
+ total_discount_applied: 1000,
157
+ order_discount_details: {
158
+ special_discount: 1000,
159
+ special_discount_id: 'MANUAL_Rs 10',
160
+ special_discount_name: 'Apply Discount',
161
+ is_special_discount_percentage: false,
162
+ total_discount: 1000,
163
+ },
164
+ }),
165
+ rawOrder({
166
+ _id: 'order-cancelled',
167
+ order_number: 2,
168
+ order_status: 'CANCELLED',
169
+ total_discount_applied: 2000,
170
+ order_discount_details: {
171
+ special_discount: 2000,
172
+ special_discount_id: 'MANUAL_Rs 20',
173
+ special_discount_name: 'Apply Discount',
174
+ is_special_discount_percentage: false,
175
+ total_discount: 2000,
176
+ },
177
+ }),
178
+ ]);
179
+ // The active (COMPLETED) key carries only the active order's money.
180
+ const report = compiled.reports.find((candidate) => candidate.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH);
181
+ const counters = report.discount_counters;
182
+ expect(counters['COMPLETED:POS_WALK-IN'].amount).toBe(1000);
183
+ expect(counters['COMPLETED:POS_WALK-IN'].count).toBe(1);
184
+ // The cancelled order is present, in its own section.
185
+ expect(counters['CANCELLED:POS_WALK-IN'].amount).toBe(2000);
186
+ expect(counters['CANCELLED:POS_WALK-IN'].count).toBe(1);
187
+ // The source row carries BOTH orders, split per status key — the read
188
+ // filters the per-key metrics, so the cancelled money never reaches the
189
+ // active total (see the operations-service source aggregation).
190
+ const source = byKey(sources).get(`manual:order|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
191
+ expect(source.order_count).toBe(2);
192
+ expect(source.row_count).toBe(2);
193
+ expect(source.discount_amount).toBe(3000);
194
+ const perKey = new Map(source.key_metrics.map((metric) => [metric.key, metric]));
195
+ expect(perKey.get('COMPLETED:POS_WALK-IN')?.discount_amount).toBe(1000);
196
+ expect(perKey.get('CANCELLED:POS_WALK-IN')?.discount_amount).toBe(2000);
197
+ });
198
+ });
199
+ (0, globals_1.describe)('discount scenarios: refunds', () => {
200
+ (0, globals_1.it)('documents the CURRENT contract: a refunded discount is still counted', () => {
201
+ // The materialized discount counters are built from the order's discount
202
+ // rows; a payment refund does NOT net them out. This test pins that
203
+ // behaviour so a future change is deliberate (see the report gap).
204
+ const { sources } = sourcesOf([
205
+ rawOrder({
206
+ _id: 'order-refunded',
207
+ order_number: 1,
208
+ total_discount_applied: 1000,
209
+ order_discount_details: {
210
+ special_discount: 1000,
211
+ special_discount_id: 'MANUAL_Rs 10',
212
+ special_discount_name: 'Apply Discount',
213
+ is_special_discount_percentage: false,
214
+ total_discount: 1000,
215
+ },
216
+ payments: [
217
+ {
218
+ _id: 'pay-1',
219
+ payment_type: 'CARD_TERMINAL',
220
+ payment_status: 'SETTLED',
221
+ amount_due: 10000,
222
+ amount_paid: 10000,
223
+ amount_refunded: 5000, // a PARTIAL refund
224
+ card_details: null,
225
+ payment_id: 'pay-1',
226
+ },
227
+ ],
228
+ }),
229
+ ]);
230
+ const source = byKey(sources).get(`manual:order|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
231
+ // CURRENT contract: the discount is reported in full (no refund netting).
232
+ expect(source.discount_amount).toBe(1000);
233
+ expect(source.order_count).toBe(1);
234
+ });
235
+ });
236
+ (0, globals_1.describe)('discount scenarios: free modifiers', () => {
237
+ (0, globals_1.it)('does NOT create a discount entry or source row for a free modifier', () => {
238
+ const compiled = compile([
239
+ rawOrder({
240
+ _id: 'order-free-modifier',
241
+ order_number: 1,
242
+ categories: [
243
+ {
244
+ category_id: 'cat-1',
245
+ name: 'FOOD',
246
+ items: [
247
+ rawItem({
248
+ modifiers: [
249
+ {
250
+ _id: 'mod-1',
251
+ name: 'Extra Cheese',
252
+ price: 0,
253
+ is_free: true,
254
+ quantity: 1,
255
+ },
256
+ ],
257
+ }),
258
+ ],
259
+ },
260
+ ],
261
+ }),
262
+ ]);
263
+ // No discount slot anywhere -> the order contributes nothing, and no entry
264
+ // document is written for the scope at all.
265
+ expect(compiled.contributingOrders).toBe(0);
266
+ const monthEntry = compiled.entries.find((candidate) => candidate.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH);
267
+ expect(monthEntry).toBeUndefined();
268
+ // And therefore there is no source row either.
269
+ const sections = Object.values((compiled.entries.find((candidate) => candidate.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH)
270
+ ?.discount_entries ?? {}));
271
+ expect(sections.every((section) => (section.rows ?? []).length === 0)).toBe(true);
272
+ });
273
+ });
274
+ (0, globals_1.describe)('discount scenarios: item-level manual', () => {
275
+ (0, globals_1.it)('books a PLAIN FIXED item manual discount as manual:item', () => {
276
+ const { sources } = sourcesOf([
277
+ rawOrder({
278
+ _id: 'order-item-manual',
279
+ order_number: 1,
280
+ categories: [
281
+ {
282
+ category_id: 'cat-1',
283
+ name: 'FOOD',
284
+ items: [
285
+ rawItem({
286
+ item_discount_details: {
287
+ special_discount: 1500, // a FIXED amount in cents
288
+ special_discount_id: 'MANUAL_Rs 15',
289
+ special_discount_name: 'Apply Discount',
290
+ is_special_discount_percentage: false,
291
+ total_discount: 1500,
292
+ },
293
+ }),
294
+ ],
295
+ },
296
+ ],
297
+ }),
298
+ ]);
299
+ const source = byKey(sources).get(`manual:item|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM}`);
300
+ expect(source).toBeDefined();
301
+ expect(source.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.MANUAL);
302
+ expect(source.sub_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.MANUAL);
303
+ expect(source.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM);
304
+ expect(source.kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT);
305
+ expect(source.rate_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.FIXED);
306
+ expect(source.fixed_row_count).toBe(1);
307
+ expect(source.percentage_row_count).toBe(0);
308
+ expect(source.discount_amount).toBe(1500);
309
+ expect(source.order_count).toBe(1);
310
+ expect(source.row_count).toBe(1);
311
+ // A fixed row stores no rate sum.
312
+ expect(source.percentage_rate_sum).toBe(0);
313
+ });
314
+ });
315
+ (0, globals_1.describe)('discount scenarios: collections', () => {
316
+ (0, globals_1.it)('books an ITEM collection FIXED amount as discount:<id>', () => {
317
+ const collectionId = '6ad9fa14ae19ab3c25c18a8a';
318
+ const { sources } = sourcesOf([
319
+ rawOrder({
320
+ _id: 'order-item-collection-fixed',
321
+ order_number: 1,
322
+ categories: [
323
+ {
324
+ category_id: 'cat-1',
325
+ name: 'FOOD',
326
+ items: [
327
+ rawItem({
328
+ item_discount_details: {
329
+ special_discount: 2000,
330
+ special_discount_id: collectionId,
331
+ special_discount_name: 'Bank Discount',
332
+ is_special_discount_percentage: false,
333
+ total_discount: 2000,
334
+ },
335
+ }),
336
+ ],
337
+ },
338
+ ],
339
+ }),
340
+ ]);
341
+ const source = byKey(sources).get(`discount:${collectionId}|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM}`);
342
+ expect(source).toBeDefined();
343
+ expect(source.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COLLECTION);
344
+ expect(source.sub_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.COLLECTION);
345
+ expect(source.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM);
346
+ expect(source.rate_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.FIXED);
347
+ expect(source.discount_amount).toBe(2000);
348
+ expect(source.order_count).toBe(1);
349
+ });
350
+ (0, globals_1.it)('books an ORDER collection PERCENTAGE as discount:<id> with the rate in basis points', () => {
351
+ const collectionId = '6ad9f91eae19ab3c25c18a89';
352
+ const { sources } = sourcesOf([
353
+ rawOrder({
354
+ _id: 'order-order-collection-pct',
355
+ order_number: 1,
356
+ grand_total: 10000,
357
+ amount_earned: 10000,
358
+ amount_collected: 9000,
359
+ total_discount_applied: 1000,
360
+ order_discount_details: {
361
+ special_discount: 1000, // 10% in BASIS POINTS
362
+ special_discount_id: collectionId,
363
+ special_discount_name: 'Bank Discount',
364
+ is_special_discount_percentage: true,
365
+ total_discount: 1000,
366
+ },
367
+ }),
368
+ ]);
369
+ const source = byKey(sources).get(`discount:${collectionId}|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
370
+ expect(source).toBeDefined();
371
+ expect(source.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COLLECTION);
372
+ expect(source.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER);
373
+ expect(source.rate_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE);
374
+ expect(source.percentage_rate_sum).toBe(1000);
375
+ expect(source.percentage_row_count).toBe(1);
376
+ expect(source.discount_amount).toBe(1000);
377
+ expect(source.order_count).toBe(1);
378
+ });
379
+ });
380
+ (0, globals_1.describe)('discount scenarios: several order-level slots', () => {
381
+ (0, globals_1.it)('keeps special + loyalty on one order as INDEPENDENT source rows', () => {
382
+ const loyaltyId = '6ad9fa14ae19ab3c25c18a8a';
383
+ const { compiled, sources } = sourcesOf([
384
+ rawOrder({
385
+ _id: 'order-two-slots',
386
+ order_number: 1,
387
+ grand_total: 10000,
388
+ amount_earned: 10000,
389
+ amount_collected: 7000,
390
+ total_discount_applied: 3000,
391
+ order_discount_details: {
392
+ special_discount: 1000,
393
+ special_discount_id: 'MANUAL_Rs 10',
394
+ special_discount_name: 'Apply Discount',
395
+ is_special_discount_percentage: false,
396
+ loyalty_discount: 2000,
397
+ loyalty_discount_id: loyaltyId,
398
+ loyalty_discount_name: 'Loyalty Offer',
399
+ is_loyalty_discount_percentage: false,
400
+ total_discount: 3000,
401
+ },
402
+ }),
403
+ ]);
404
+ expect(compiled.contributingOrders).toBe(1);
405
+ const map = byKey(sources);
406
+ const manual = map.get(`manual:order|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
407
+ expect(manual).toBeDefined();
408
+ expect(manual.discount_amount).toBe(1000);
409
+ expect(manual.order_count).toBe(1);
410
+ const loyalty = map.get(`loyalty:offer:${loyaltyId}|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
411
+ expect(loyalty).toBeDefined();
412
+ expect(loyalty.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.LOYALTY);
413
+ expect(loyalty.sub_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER);
414
+ expect(loyalty.discount_amount).toBe(2000);
415
+ expect(loyalty.order_count).toBe(1);
416
+ // Two independent source rows for ONE order.
417
+ expect(sources).toHaveLength(2);
418
+ });
419
+ });
420
+ (0, globals_1.describe)('discount scenarios: discount and complimentary together', () => {
421
+ (0, globals_1.it)('books the discount and the complimentary tender as separate rows', () => {
422
+ const { sources } = sourcesOf([
423
+ rawOrder({
424
+ _id: 'order-discount-and-comp',
425
+ order_number: 1,
426
+ grand_total: 10000,
427
+ amount_earned: 10000,
428
+ amount_collected: 0,
429
+ total_discount_applied: 1000,
430
+ order_discount_details: {
431
+ special_discount: 1000,
432
+ special_discount_id: 'MANUAL_Rs 10',
433
+ special_discount_name: 'Apply Discount',
434
+ is_special_discount_percentage: false,
435
+ total_discount: 1000,
436
+ },
437
+ payments: [
438
+ {
439
+ _id: 'pay-comp',
440
+ payment_type: 'COMPLIMENTARY',
441
+ payment_status: 'SETTLED',
442
+ amount_due: 10000,
443
+ amount_paid: 10000,
444
+ amount_refunded: 0,
445
+ card_details: null,
446
+ payment_id: 'pay-comp',
447
+ },
448
+ ],
449
+ }),
450
+ ]);
451
+ const map = byKey(sources);
452
+ const discount = map.get(`manual:order|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
453
+ const comp = map.get(`${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.COMPLIMENTARY_FULL}|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.PAYMENT}`);
454
+ expect(discount).toBeDefined();
455
+ expect(discount.discount_amount).toBe(1000);
456
+ expect(discount.kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT);
457
+ expect(comp).toBeDefined();
458
+ expect(comp.kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY);
459
+ expect(comp.complimentary_full).toBe(true);
460
+ expect(comp.covered_amount).toBe(10000);
461
+ // A complimentary tender is NOT a discount: it carries no discount money.
462
+ expect(comp.discount_amount).toBe(0);
463
+ });
464
+ });
465
+ (0, globals_1.describe)('discount scenarios: complimentary with no discount', () => {
466
+ (0, globals_1.it)('books a FULL comp with no discount at all', () => {
467
+ const { sources } = sourcesOf([
468
+ rawOrder({
469
+ _id: 'order-full-comp',
470
+ order_number: 1,
471
+ grand_total: 10000,
472
+ amount_earned: 10000,
473
+ amount_collected: 0,
474
+ total_discount_applied: 0,
475
+ payments: [
476
+ {
477
+ _id: 'pay-comp',
478
+ payment_type: 'COMPLIMENTARY',
479
+ payment_status: 'SETTLED',
480
+ amount_due: 10000,
481
+ amount_paid: 10000,
482
+ amount_refunded: 0,
483
+ card_details: null,
484
+ payment_id: 'pay-comp',
485
+ },
486
+ ],
487
+ }),
488
+ ]);
489
+ const comp = byKey(sources).get(`${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.COMPLIMENTARY_FULL}|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.PAYMENT}`);
490
+ expect(comp).toBeDefined();
491
+ expect(comp.complimentary_full).toBe(true);
492
+ expect(comp.covered_amount).toBe(10000);
493
+ expect(comp.discount_amount).toBe(0);
494
+ expect(comp.order_count).toBe(1);
495
+ });
496
+ (0, globals_1.it)('books a PARTIAL comp (a comp tender beside another tender) with no discount', () => {
497
+ const { sources } = sourcesOf([
498
+ rawOrder({
499
+ _id: 'order-partial-comp',
500
+ order_number: 1,
501
+ grand_total: 10000,
502
+ amount_earned: 10000,
503
+ amount_collected: 5000,
504
+ total_discount_applied: 0,
505
+ payments: [
506
+ {
507
+ _id: 'pay-comp',
508
+ payment_type: 'COMPLIMENTARY',
509
+ payment_status: 'SETTLED',
510
+ amount_due: 5000,
511
+ amount_paid: 5000,
512
+ amount_refunded: 0,
513
+ card_details: null,
514
+ payment_id: 'pay-comp',
515
+ },
516
+ {
517
+ _id: 'pay-cash',
518
+ payment_type: 'CASH',
519
+ payment_status: 'SETTLED',
520
+ amount_due: 5000,
521
+ amount_paid: 5000,
522
+ amount_refunded: 0,
523
+ card_details: null,
524
+ payment_id: 'pay-cash',
525
+ },
526
+ ],
527
+ }),
528
+ ]);
529
+ const comp = byKey(sources).get(`${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.COMPLIMENTARY_PARTIAL}|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.PAYMENT}`);
530
+ expect(comp).toBeDefined();
531
+ expect(comp.complimentary_full).toBe(false);
532
+ expect(comp.covered_amount).toBe(5000);
533
+ expect(comp.discount_amount).toBe(0);
534
+ expect(comp.order_count).toBe(1);
535
+ });
536
+ });
537
+ (0, globals_1.describe)('discount scenarios: loyalty and voucher at order level', () => {
538
+ (0, globals_1.it)('books an ORDER-level loyalty OFFER as loyalty:offer:<id>', () => {
539
+ const offerId = '6ad9fa14ae19ab3c25c18a8a';
540
+ const { sources } = sourcesOf([
541
+ rawOrder({
542
+ _id: 'order-loyalty-offer',
543
+ order_number: 1,
544
+ grand_total: 10000,
545
+ amount_earned: 10000,
546
+ amount_collected: 7000,
547
+ total_discount_applied: 3000,
548
+ order_discount_details: {
549
+ loyalty_discount: 3000,
550
+ loyalty_discount_id: offerId,
551
+ loyalty_discount_name: 'Loyalty Offer',
552
+ is_loyalty_discount_percentage: false,
553
+ total_discount: 3000,
554
+ },
555
+ }),
556
+ ]);
557
+ const source = byKey(sources).get(`loyalty:offer:${offerId}|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
558
+ expect(source).toBeDefined();
559
+ expect(source.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.LOYALTY);
560
+ expect(source.sub_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER);
561
+ expect(source.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER);
562
+ expect(source.discount_amount).toBe(3000);
563
+ expect(source.order_count).toBe(1);
564
+ });
565
+ (0, globals_1.it)('books an ITEM-level voucher as voucher:<id>', () => {
566
+ const voucherId = '6abf432e5e03cc49e2462a62';
567
+ const { sources } = sourcesOf([
568
+ rawOrder({
569
+ _id: 'order-item-voucher',
570
+ order_number: 1,
571
+ categories: [
572
+ {
573
+ category_id: 'cat-1',
574
+ name: 'FOOD',
575
+ items: [
576
+ rawItem({
577
+ item_discount_details: {
578
+ voucher_discount: 800,
579
+ voucher_discount_id: voucherId,
580
+ voucher_discount_name: 'Item Voucher',
581
+ is_voucher_discount_percentage: false,
582
+ total_discount: 800,
583
+ },
584
+ }),
585
+ ],
586
+ },
587
+ ],
588
+ }),
589
+ ]);
590
+ const source = byKey(sources).get(`voucher:${voucherId}|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM}`);
591
+ expect(source).toBeDefined();
592
+ expect(source.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.VOUCHER);
593
+ expect(source.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM);
594
+ expect(source.discount_amount).toBe(800);
595
+ expect(source.order_count).toBe(1);
596
+ });
597
+ (0, globals_1.it)('books a GIFT CARD as loyalty:gift_card:<id>', () => {
598
+ const { sources } = sourcesOf([
599
+ rawOrder({
600
+ _id: 'order-gift-card',
601
+ order_number: 1,
602
+ grand_total: 10000,
603
+ amount_earned: 10000,
604
+ amount_collected: 8800,
605
+ total_discount_applied: 1200,
606
+ order_discount_details: {
607
+ loyalty_discount: 1200,
608
+ loyalty_discount_id: 'gift-card-1',
609
+ loyalty_discount_name: 'Gift Card',
610
+ loyalty_reference_number: 'GIFT-CODE-1',
611
+ is_loyalty_discount_percentage: false,
612
+ total_discount: 1200,
613
+ },
614
+ }),
615
+ ]);
616
+ const source = byKey(sources).get(`loyalty:gift_card:gift-card-1|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
617
+ expect(source).toBeDefined();
618
+ expect(source.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.LOYALTY);
619
+ expect(source.sub_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.GIFT_CARD);
620
+ expect(source.discount_amount).toBe(1200);
621
+ expect(source.order_count).toBe(1);
622
+ });
623
+ });
624
+ (0, globals_1.describe)('discount scenarios: the compiled documents stay integer-only', () => {
625
+ (0, globals_1.it)('passes the recursive integer contract for every scenario above', () => {
626
+ const orders = [
627
+ rawOrder({
628
+ _id: 'order-voucher-pct',
629
+ order_number: 1,
630
+ total_discount_applied: 2000,
631
+ order_discount_details: {
632
+ voucher_discount: 2000,
633
+ voucher_discount_id: 'voucher-offer-1',
634
+ is_voucher_discount_percentage: true,
635
+ total_discount: 2000,
636
+ },
637
+ }),
638
+ rawOrder({
639
+ _id: 'order-cancelled',
640
+ order_number: 2,
641
+ order_status: 'CANCELLED',
642
+ total_discount_applied: 2000,
643
+ order_discount_details: {
644
+ special_discount: 2000,
645
+ special_discount_id: 'MANUAL_Rs 20',
646
+ is_special_discount_percentage: false,
647
+ total_discount: 2000,
648
+ },
649
+ }),
650
+ rawOrder({
651
+ _id: 'order-item-manual',
652
+ order_number: 3,
653
+ categories: [
654
+ {
655
+ category_id: 'cat-1',
656
+ name: 'FOOD',
657
+ items: [
658
+ rawItem({
659
+ item_discount_details: {
660
+ special_discount: 1500,
661
+ special_discount_id: 'MANUAL_Rs 15',
662
+ is_special_discount_percentage: false,
663
+ total_discount: 1500,
664
+ },
665
+ }),
666
+ ],
667
+ },
668
+ ],
669
+ }),
670
+ ];
671
+ const { compiled, sources } = sourcesOf(orders);
672
+ (0, materialized_integer_contract_1.assertIntegerOnlyDocuments)(compiled.reports, { collection: 'reports' });
673
+ (0, materialized_integer_contract_1.assertIntegerOnlyDocuments)(compiled.entries, { collection: 'entries' });
674
+ (0, materialized_integer_contract_1.assertIntegerOnlyDocuments)(sources, { collection: 'sources' });
675
+ });
676
+ });