@managesome/eposmatic-toolkit 0.4.23 → 0.4.25
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 +83 -2
- package/esm/helpers/materialized-reports/index.js +3 -0
- package/esm/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +115 -35
- package/esm/helpers/materialized-reports/materialized-discount-classification-helpers.js +39 -12
- package/esm/helpers/materialized-reports/materialized-discount-details-helpers.js +28 -0
- package/esm/helpers/materialized-reports/materialized-discount-source-helpers.js +428 -0
- package/esm/helpers/materialized-reports/tenant-discount-source-formatter.js +140 -0
- package/esm/schemas/models/index.js +1 -0
- package/esm/schemas/models/materialized_tenant_discount_entries.js +12 -0
- package/esm/schemas/models/materialized_tenant_discount_sources.js +165 -0
- package/esm/schemas/types/index.js +1 -0
- package/esm/schemas/types/materialized_tenant_discount_sources.js +1 -0
- package/lib/constants/MATERIALIZED_REPORTS.d.ts +79 -2
- package/lib/constants/MATERIALIZED_REPORTS.js +85 -4
- package/lib/helpers/materialized-reports/index.d.ts +3 -0
- package/lib/helpers/materialized-reports/index.js +3 -0
- package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.d.ts +30 -0
- package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +116 -35
- package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.d.ts +27 -4
- package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.js +38 -11
- package/lib/helpers/materialized-reports/materialized-discount-details-helpers.d.ts +31 -0
- package/lib/helpers/materialized-reports/materialized-discount-details-helpers.js +31 -0
- package/lib/helpers/materialized-reports/materialized-discount-source-helpers.d.ts +73 -0
- package/lib/helpers/materialized-reports/materialized-discount-source-helpers.js +437 -0
- package/lib/helpers/materialized-reports/tenant-discount-source-formatter.d.ts +79 -0
- package/lib/helpers/materialized-reports/tenant-discount-source-formatter.js +144 -0
- package/lib/helpers/types/materialized-reports-apis.d.ts +53 -1
- package/lib/schemas/models/index.d.ts +1 -0
- package/lib/schemas/models/index.js +1 -0
- package/lib/schemas/models/materialized_tenant_discount_entries.d.ts +21 -6
- package/lib/schemas/models/materialized_tenant_discount_entries.js +12 -0
- package/lib/schemas/models/materialized_tenant_discount_sources.d.ts +331 -0
- package/lib/schemas/models/materialized_tenant_discount_sources.js +168 -0
- package/lib/schemas/types/index.d.ts +1 -0
- package/lib/schemas/types/index.js +1 -0
- package/lib/schemas/types/materialized_tenant_discount_entries.d.ts +26 -0
- package/lib/schemas/types/materialized_tenant_discount_sources.d.ts +128 -0
- package/lib/schemas/types/materialized_tenant_discount_sources.js +2 -0
- package/lib/tests/materialized-reports/materialized-discount-classification.test.js +22 -2
- package/lib/tests/materialized-reports/materialized-discount-details-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-discount-details-helpers.test.js +59 -0
- package/lib/tests/materialized-reports/materialized-discount-sources.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-discount-sources.test.js +483 -0
- package/lib/tests/materialized-reports/tenant-discount-source-formatter.test.d.ts +1 -0
- package/lib/tests/materialized-reports/tenant-discount-source-formatter.test.js +203 -0
- package/package.json +1 -1
|
@@ -1,13 +1,47 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createEmptyDiscountSlotCounter = exports.toDiscountHourLabel = void 0;
|
|
4
|
+
exports.classifyComplimentaryOrder = classifyComplimentaryOrder;
|
|
4
5
|
exports.accumulateOrderDiscounts = accumulateOrderDiscounts;
|
|
5
6
|
exports.mergeDiscountSlotCounters = mergeDiscountSlotCounters;
|
|
6
7
|
const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
8
|
+
const order_1 = require("../../constants/enums/order");
|
|
7
9
|
const payments_1 = require("../../constants/enums/payments");
|
|
8
10
|
const materialized_discount_classification_helpers_1 = require("./materialized-discount-classification-helpers");
|
|
9
11
|
Object.defineProperty(exports, "createEmptyDiscountSlotCounter", { enumerable: true, get: function () { return materialized_discount_classification_helpers_1.createEmptyDiscountSlotCounter; } });
|
|
10
12
|
const isComplimentaryPayment = (payment) => payment?.payment_type === payments_1.PAYMENT_TYPE_ENUM.COMPLIMENTARY;
|
|
13
|
+
/**
|
|
14
|
+
* Classifies an order's COMPLIMENTARY tenders. Returns null when there is none.
|
|
15
|
+
*
|
|
16
|
+
* A PARTIAL comp has two real shapes in the POS:
|
|
17
|
+
* 1. one COMPLIMENTARY payment with `amount_paid < amount_due` (the "pay
|
|
18
|
+
* partially" flow, no other tender); or
|
|
19
|
+
* 2. a COMPLIMENTARY payment beside another settled tender.
|
|
20
|
+
* Both are partial. "All payments are COMPLIMENTARY" alone would call shape 1
|
|
21
|
+
* full, so the amount is part of the test too.
|
|
22
|
+
*
|
|
23
|
+
* Used by the current close for every comp row. Pre-release has no legacy
|
|
24
|
+
* comp-row inference path.
|
|
25
|
+
*/
|
|
26
|
+
function classifyComplimentaryOrder(order) {
|
|
27
|
+
const payments = order?.payments ?? [];
|
|
28
|
+
const compPayments = payments.filter(isComplimentaryPayment);
|
|
29
|
+
if (compPayments.length === 0)
|
|
30
|
+
return null;
|
|
31
|
+
const orderAmount = nonNegative(Number(order?.grand_total ?? order?.amount_earned ?? 0));
|
|
32
|
+
const coveredAmount = compPayments.reduce((sum, payment) => sum + nonNegative(Number(payment.amount_paid ?? 0)), 0);
|
|
33
|
+
const hasOtherTender = payments.some((payment) => !isComplimentaryPayment(payment) && nonNegative(Number(payment.amount_paid ?? 0)) > 0);
|
|
34
|
+
return {
|
|
35
|
+
full: !hasOtherTender && (orderAmount <= 0 || coveredAmount >= orderAmount - 0.005),
|
|
36
|
+
covered_amount: coveredAmount,
|
|
37
|
+
order_amount: orderAmount,
|
|
38
|
+
payments: compPayments.map((payment) => ({
|
|
39
|
+
payment_id: payment._id != null ? String(payment._id) : null,
|
|
40
|
+
covered_amount: nonNegative(Number(payment.amount_paid ?? 0)),
|
|
41
|
+
note: typeof payment.note === 'string' && payment.note.length > 0 ? payment.note : null,
|
|
42
|
+
})),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
11
45
|
const nonNegative = (value) => (Number.isFinite(value) && value > 0 ? value : 0);
|
|
12
46
|
/** The line label of a punch hour (null when the hour is unknown). */
|
|
13
47
|
const toDiscountHourLabel = (localDate, hour) => hour == null || hour < 0 || hour > 23 ? null : `${localDate} ${String(hour).padStart(2, '0')}`;
|
|
@@ -19,6 +53,8 @@ function entryBase(order, context, kind) {
|
|
|
19
53
|
order_id: String(order._id),
|
|
20
54
|
order_number: order.order_number ?? null,
|
|
21
55
|
item_id: null,
|
|
56
|
+
catalog_item_id: null,
|
|
57
|
+
catalog_auto: null,
|
|
22
58
|
item_name: null,
|
|
23
59
|
payment_id: null,
|
|
24
60
|
rate: null,
|
|
@@ -28,6 +64,9 @@ function entryBase(order, context, kind) {
|
|
|
28
64
|
name: null,
|
|
29
65
|
reference_number: null,
|
|
30
66
|
reason: null,
|
|
67
|
+
complimentary_full: null,
|
|
68
|
+
covered_amount: null,
|
|
69
|
+
order_amount: null,
|
|
31
70
|
staff_id: order.staff_details?.staff_id ?? null,
|
|
32
71
|
staff_name: order.staff_details?.staff_name ?? null,
|
|
33
72
|
order_taker_id: order.order_taker_details?.staff_id ?? null,
|
|
@@ -100,7 +139,7 @@ function orderSubKindCounter(counters, slot, subKind) {
|
|
|
100
139
|
return subKind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER ? counters.voucher_offer : counters.voucher_other;
|
|
101
140
|
}
|
|
102
141
|
/** Adds one ITEM-LEVEL slot reading into its counters + rows. */
|
|
103
|
-
function accumulateItemSlot(counters, entries, order, context, itemId, itemName, itemValue, reading, resolved, itemTotal) {
|
|
142
|
+
function accumulateItemSlot(counters, entries, order, context, itemId, catalogItemId, itemName, itemValue, reading, resolved, itemTotal, catalogAuto) {
|
|
104
143
|
const raw = nonNegative(reading.rate);
|
|
105
144
|
const money = nonNegative(resolved);
|
|
106
145
|
const isFree = reading.is_percentage && reading.rate >= 100;
|
|
@@ -194,6 +233,8 @@ function accumulateItemSlot(counters, entries, order, context, itemId, itemName,
|
|
|
194
233
|
rate: raw,
|
|
195
234
|
is_percentage: reading.is_percentage,
|
|
196
235
|
item_id: itemId,
|
|
236
|
+
catalog_item_id: catalogItemId,
|
|
237
|
+
catalog_auto: catalogAuto ? true : null,
|
|
197
238
|
item_name: itemName,
|
|
198
239
|
item_value: isFree ? nonNegative(itemValue) : null,
|
|
199
240
|
discount_id: reading.discount_id,
|
|
@@ -214,6 +255,11 @@ function accumulateOrderDiscounts(order, context) {
|
|
|
214
255
|
const orderDetails = order.order_discount_details ?? null;
|
|
215
256
|
const applied = nonNegative(Number(order.total_discount_applied ?? 0));
|
|
216
257
|
const storedOrderTotal = nonNegative(orderDetails?.total_discount != null ? Number(orderDetails.total_discount) : 0);
|
|
258
|
+
// Gift cards and offers both write an ObjectId into the loyalty slot. The
|
|
259
|
+
// order's wallet claims are the authoritative discriminator.
|
|
260
|
+
const giftCardIds = new Set((order.wallet_redemptions?.gift_cards ?? [])
|
|
261
|
+
.map((redemption) => String(redemption?.gift_card_id ?? ''))
|
|
262
|
+
.filter((id) => id.length > 0));
|
|
217
263
|
// ---- order level -------------------------------------------------------
|
|
218
264
|
const orderMoney = (0, materialized_discount_classification_helpers_1.resolveRowMoney)(orderDetails, storedOrderTotal);
|
|
219
265
|
// Slot PRESENCE = a positive rate OR an id. Zero-rate slots that carry an id
|
|
@@ -226,7 +272,7 @@ function accumulateOrderDiscounts(order, context) {
|
|
|
226
272
|
// inflate every occurrence counter while adding nothing to the money; it would
|
|
227
273
|
// also break the "resolved slots explain the row total" invariant. They are
|
|
228
274
|
// therefore excluded here (the row itself is not a discount row).
|
|
229
|
-
const orderReadings = materialized_discount_classification_helpers_1.MATERIALIZED_DISCOUNT_SLOTS.map((slot) => (0, materialized_discount_classification_helpers_1.readDiscountSlot)(orderDetails, slot))
|
|
275
|
+
const orderReadings = materialized_discount_classification_helpers_1.MATERIALIZED_DISCOUNT_SLOTS.map((slot) => (0, materialized_discount_classification_helpers_1.readDiscountSlot)(orderDetails, slot, { gift_card_ids: giftCardIds }))
|
|
230
276
|
.filter((reading) => reading != null && reading.rate >= 0)
|
|
231
277
|
.map((reading) => ({ reading, money: nonNegative(orderMoney.perSlot.get(reading.slot) ?? 0) }))
|
|
232
278
|
.filter((entry) => entry.money > 0);
|
|
@@ -237,8 +283,21 @@ function accumulateOrderDiscounts(order, context) {
|
|
|
237
283
|
counters.order_level_amount += storedOrderTotal;
|
|
238
284
|
// Money the slots cannot explain (no slots at all, several active slots, or
|
|
239
285
|
// a slot value the row total contradicts) is a tripwire: counted, never
|
|
240
|
-
// silently dropped.
|
|
241
|
-
|
|
286
|
+
// silently dropped. It also gets a ROW so the source projection can
|
|
287
|
+
// attribute it to "Other / unattributed" instead of losing it between the
|
|
288
|
+
// entry rows and the headline.
|
|
289
|
+
const unallocated = nonNegative(orderMoney.unallocated);
|
|
290
|
+
counters.unattributed_amount += unallocated;
|
|
291
|
+
if (unallocated > 0) {
|
|
292
|
+
entries.push({
|
|
293
|
+
...entryBase(order, context, MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT),
|
|
294
|
+
slot: null,
|
|
295
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED,
|
|
296
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER,
|
|
297
|
+
entry_key: 'order:unattributed',
|
|
298
|
+
amount: unallocated,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
242
301
|
}
|
|
243
302
|
// ---- item level --------------------------------------------------------
|
|
244
303
|
let itemLevelTotal = 0;
|
|
@@ -259,57 +318,79 @@ function accumulateOrderDiscounts(order, context) {
|
|
|
259
318
|
const netValue = nonNegative(Number(item._net_amount ?? 0));
|
|
260
319
|
const itemValue = netValue > 0 ? netValue : nonNegative(Number(item._gross_amount ?? item.price ?? 0));
|
|
261
320
|
const itemMoney = (0, materialized_discount_classification_helpers_1.resolveRowMoney)(itemDetails, itemTotal);
|
|
262
|
-
|
|
321
|
+
// The catalog-auto path writes the ITEM id as `special_discount_id`; a
|
|
322
|
+
// catalog id is usually a 24-hex ObjectId, indistinguishable by shape
|
|
323
|
+
// from a collection discount. Both ids are passed so the classifier can
|
|
324
|
+
// tell them apart (the order-item id also covers the re-order path).
|
|
325
|
+
const itemSlotContext = {
|
|
326
|
+
order_item_id: String(item._id),
|
|
327
|
+
catalog_item_id: item.item_id != null ? String(item.item_id) : null,
|
|
328
|
+
gift_card_ids: giftCardIds,
|
|
329
|
+
};
|
|
330
|
+
const itemReadings = materialized_discount_classification_helpers_1.MATERIALIZED_DISCOUNT_SLOTS.map((slot) => (0, materialized_discount_classification_helpers_1.readDiscountSlot)(itemDetails, slot, itemSlotContext))
|
|
263
331
|
.filter((reading) => reading != null && reading.rate >= 0)
|
|
264
332
|
.map((reading) => ({ reading, money: nonNegative(itemMoney.perSlot.get(reading.slot) ?? 0) }))
|
|
265
333
|
.filter((entry) => entry.money > 0);
|
|
266
|
-
|
|
267
|
-
//
|
|
268
|
-
//
|
|
269
|
-
//
|
|
334
|
+
for (const { reading: rawReading, money } of itemReadings) {
|
|
335
|
+
// Specials / half-half write their SETUP doc id as the discount id,
|
|
336
|
+
// which is neither the order-item id nor the catalog id. Their auto
|
|
337
|
+
// discount is still a catalog-owned discount: the item type + the
|
|
338
|
+
// constant 'Special Discount' name prove it.
|
|
339
|
+
const catalogAuto = rawReading.sub_kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM ||
|
|
340
|
+
(rawReading.slot === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL &&
|
|
341
|
+
rawReading.name === 'Special Discount' &&
|
|
342
|
+
(item.item_type === order_1.ORDER_ITEM_TYPE_ENUM.SPECIAL || item.item_type === order_1.ORDER_ITEM_TYPE_ENUM.HALF_HALF));
|
|
343
|
+
const reading = catalogAuto && rawReading.sub_kind !== MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM
|
|
344
|
+
? { ...rawReading, sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM }
|
|
345
|
+
: rawReading;
|
|
346
|
+
accumulateItemSlot(counters, entries, order, context, String(item._id), item.item_id != null ? String(item.item_id) : null, item.name ?? null, itemValue, reading, money, itemTotal, catalogAuto);
|
|
347
|
+
}
|
|
348
|
+
// Multi-slot / fixed-slot leftovers, and item money with no usable slot
|
|
349
|
+
// at all, get ONE unattributed row. Keeping this money in a row (not only
|
|
350
|
+
// in the counter) is what lets the source projection reconcile to the
|
|
351
|
+
// headline: Σ source.discount_amount then includes it.
|
|
352
|
+
const itemUnallocated = nonNegative(itemMoney.unallocated);
|
|
353
|
+
if (itemUnallocated > 0) {
|
|
270
354
|
entries.push({
|
|
271
355
|
...entryBase(order, context, MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT),
|
|
272
356
|
slot: null,
|
|
273
357
|
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED,
|
|
274
358
|
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
|
|
275
359
|
entry_key: `item:${String(item._id)}:unattributed`,
|
|
276
|
-
amount:
|
|
360
|
+
amount: itemUnallocated,
|
|
277
361
|
item_id: String(item._id),
|
|
362
|
+
catalog_item_id: item.item_id != null ? String(item.item_id) : null,
|
|
278
363
|
item_name: item.name ?? null,
|
|
279
364
|
});
|
|
280
|
-
continue;
|
|
281
365
|
}
|
|
282
|
-
|
|
283
|
-
accumulateItemSlot(counters, entries, order, context, String(item._id), item.name ?? null, itemValue, reading, money, itemTotal);
|
|
284
|
-
}
|
|
285
|
-
// Multi-slot / fixed-slot leftovers stay visible on the order-level
|
|
286
|
-
// tripwire so anchors E/H keep holding.
|
|
287
|
-
counters.unattributed_amount += nonNegative(itemMoney.unallocated);
|
|
366
|
+
counters.unattributed_amount += itemUnallocated;
|
|
288
367
|
}
|
|
289
368
|
}
|
|
290
369
|
counters.item_level_amount += itemLevelTotal;
|
|
291
370
|
// ---- headline + comp ---------------------------------------------------
|
|
292
371
|
counters.amount += applied;
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
372
|
+
const comp = classifyComplimentaryOrder(order);
|
|
373
|
+
if (comp != null) {
|
|
374
|
+
counters.complimentary.payment_count += comp.payments.length;
|
|
375
|
+
for (const payment of comp.payments) {
|
|
376
|
+
if (payment.note != null)
|
|
377
|
+
counters.complimentary.note_count += 1;
|
|
378
|
+
entries.push({
|
|
379
|
+
...entryBase(order, context, MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY),
|
|
380
|
+
slot: null,
|
|
381
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED,
|
|
382
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.PAYMENT,
|
|
383
|
+
entry_key: `payment:${String(payment.payment_id)}`,
|
|
384
|
+
amount: 0,
|
|
385
|
+
payment_id: payment.payment_id,
|
|
386
|
+
complimentary_full: comp.full,
|
|
387
|
+
covered_amount: payment.covered_amount,
|
|
388
|
+
order_amount: comp.order_amount,
|
|
389
|
+
});
|
|
390
|
+
}
|
|
309
391
|
}
|
|
310
392
|
if (counters.complimentary.payment_count > 0) {
|
|
311
|
-
|
|
312
|
-
counters.complimentary.amount += compAmount;
|
|
393
|
+
counters.complimentary.amount += nonNegative(Number(order.amount_earned ?? 0));
|
|
313
394
|
}
|
|
314
395
|
// ONE count per order with any discount or comp payment — incremented exactly
|
|
315
396
|
// once, after every source has been folded (a double increment here would
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { MATERIALIZED_DISCOUNT_SLOT_ENUM, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
2
|
import { IDiscountDetails } from '../../schemas/types/orders';
|
|
3
3
|
import { IMaterializedDiscountOrderCounters, IMaterializedDiscountSlotCounter } from '../../schemas/types/materialized_tenant_discount_reports';
|
|
4
|
-
/** `'SPECIAL_DISCOUNT'` — the catalog item-discount sentinel (pos-web). */
|
|
5
|
-
export declare const MATERIALIZED_DISCOUNT_CATALOG_ITEM_ID = "SPECIAL_DISCOUNT";
|
|
6
4
|
export declare const MATERIALIZED_DISCOUNT_SLOTS: MATERIALIZED_DISCOUNT_SLOT_ENUM[];
|
|
7
5
|
export interface IDiscountSlotReading {
|
|
8
6
|
slot: MATERIALIZED_DISCOUNT_SLOT_ENUM;
|
|
@@ -15,13 +13,38 @@ export interface IDiscountSlotReading {
|
|
|
15
13
|
reference_number: string | null;
|
|
16
14
|
reason: string | null;
|
|
17
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Identity context a CALLER can supply when it knows more than the slot does.
|
|
18
|
+
*
|
|
19
|
+
* Needed because the catalog "Special Discount" writes the CATALOG ITEM id as
|
|
20
|
+
* `special_discount_id`, and a catalog id is usually a 24-hex ObjectId — the
|
|
21
|
+
* exact shape that otherwise means "collection discount". The accumulator
|
|
22
|
+
* knows both ids; `readDiscountSlot` alone does not.
|
|
23
|
+
*/
|
|
24
|
+
export interface IDiscountSlotContext {
|
|
25
|
+
/** The ORDER ITEM's `_id` (the id reused by a re-order's catalog discount). */
|
|
26
|
+
order_item_id?: string | null;
|
|
27
|
+
/** The CATALOG item's id (the normal catalog-auto discount id). */
|
|
28
|
+
catalog_item_id?: string | null;
|
|
29
|
+
/**
|
|
30
|
+
* Gift-card ids present in `order.wallet_redemptions.gift_cards`. Offers and
|
|
31
|
+
* gift cards both write an ObjectId into the loyalty slot; the wallet
|
|
32
|
+
* evidence is what separates them.
|
|
33
|
+
*/
|
|
34
|
+
gift_card_ids?: ReadonlySet<string> | string[] | null;
|
|
35
|
+
}
|
|
18
36
|
/** Reads ONE slot of an `IDiscountDetails` — null when the slot is empty. */
|
|
19
|
-
export declare function readDiscountSlot(details: IDiscountDetails | null | undefined, slot: MATERIALIZED_DISCOUNT_SLOT_ENUM): IDiscountSlotReading | null;
|
|
37
|
+
export declare function readDiscountSlot(details: IDiscountDetails | null | undefined, slot: MATERIALIZED_DISCOUNT_SLOT_ENUM, context?: IDiscountSlotContext | null): IDiscountSlotReading | null;
|
|
20
38
|
/**
|
|
21
39
|
* Classifies a slot's sub-kind from its id shape (and, for the special slot,
|
|
22
40
|
* the id's namespace).
|
|
41
|
+
*
|
|
42
|
+
* `context` lets an item-level caller distinguish a catalog-auto discount from
|
|
43
|
+
* a collection discount: the catalog path writes the item's own id, which is
|
|
44
|
+
* usually also a 24-hex ObjectId. Callers that do not know the item ids keep
|
|
45
|
+
* the old shape-only behaviour.
|
|
23
46
|
*/
|
|
24
|
-
export declare function classifyDiscountSubKind(slot: MATERIALIZED_DISCOUNT_SLOT_ENUM, discountId: string | null): MATERIALIZED_DISCOUNT_SUB_KIND_ENUM;
|
|
47
|
+
export declare function classifyDiscountSubKind(slot: MATERIALIZED_DISCOUNT_SLOT_ENUM, discountId: string | null, context?: IDiscountSlotContext | null): MATERIALIZED_DISCOUNT_SUB_KIND_ENUM;
|
|
25
48
|
/**
|
|
26
49
|
* The MONEY a row's slots applied.
|
|
27
50
|
*
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createEmptyDiscountOrderCounters = exports.createEmptyDiscountSlotCounter = exports.MATERIALIZED_DISCOUNT_SLOTS =
|
|
3
|
+
exports.createEmptyDiscountOrderCounters = exports.createEmptyDiscountSlotCounter = exports.MATERIALIZED_DISCOUNT_SLOTS = void 0;
|
|
4
4
|
exports.readDiscountSlot = readDiscountSlot;
|
|
5
5
|
exports.classifyDiscountSubKind = classifyDiscountSubKind;
|
|
6
6
|
exports.resolveRowMoney = resolveRowMoney;
|
|
@@ -30,8 +30,6 @@ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
|
30
30
|
* order.
|
|
31
31
|
*/
|
|
32
32
|
const HEX24 = /^[0-9a-f]{24}$/i;
|
|
33
|
-
/** `'SPECIAL_DISCOUNT'` — the catalog item-discount sentinel (pos-web). */
|
|
34
|
-
exports.MATERIALIZED_DISCOUNT_CATALOG_ITEM_ID = 'SPECIAL_DISCOUNT';
|
|
35
33
|
exports.MATERIALIZED_DISCOUNT_SLOTS = [
|
|
36
34
|
MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL,
|
|
37
35
|
MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY,
|
|
@@ -40,7 +38,7 @@ exports.MATERIALIZED_DISCOUNT_SLOTS = [
|
|
|
40
38
|
/** Field-name prefix of a slot in `IDiscountDetails` (`special` → `special_discount`). */
|
|
41
39
|
const slotFieldKey = (slot) => slot.toLowerCase();
|
|
42
40
|
/** Reads ONE slot of an `IDiscountDetails` — null when the slot is empty. */
|
|
43
|
-
function readDiscountSlot(details, slot) {
|
|
41
|
+
function readDiscountSlot(details, slot, context) {
|
|
44
42
|
if (details == null)
|
|
45
43
|
return null;
|
|
46
44
|
const key = slotFieldKey(slot);
|
|
@@ -52,22 +50,38 @@ function readDiscountSlot(details, slot) {
|
|
|
52
50
|
// with only `total_discount`) — they are read but contribute 0.
|
|
53
51
|
if (rate === 0 && discount_id == null)
|
|
54
52
|
return null;
|
|
53
|
+
const reference_number = details[`${key}_reference_number`] ?? null;
|
|
54
|
+
let sub_kind = classifyDiscountSubKind(slot, discount_id, context);
|
|
55
|
+
// A loyalty OFFER never writes a reference number; the gift-card path writes
|
|
56
|
+
// the card code. Use that as a second discriminator when the wallet claims
|
|
57
|
+
// are not available (older orders, re-imported rows).
|
|
58
|
+
if (slot === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY &&
|
|
59
|
+
sub_kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER &&
|
|
60
|
+
reference_number != null &&
|
|
61
|
+
reference_number !== COMMON_1.WALLET_CASHBACK_LOYALTY_REFERENCE) {
|
|
62
|
+
sub_kind = MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.GIFT_CARD;
|
|
63
|
+
}
|
|
55
64
|
return {
|
|
56
65
|
slot,
|
|
57
|
-
sub_kind
|
|
66
|
+
sub_kind,
|
|
58
67
|
rate: Number.isFinite(rate) ? rate : 0,
|
|
59
68
|
is_percentage: details[`is_${key}_discount_percentage`] === true,
|
|
60
69
|
discount_id,
|
|
61
70
|
name: details[`${key}_discount_name`] ?? null,
|
|
62
|
-
reference_number
|
|
71
|
+
reference_number,
|
|
63
72
|
reason: details[`${key}_discount_reason`] ?? null,
|
|
64
73
|
};
|
|
65
74
|
}
|
|
66
75
|
/**
|
|
67
76
|
* Classifies a slot's sub-kind from its id shape (and, for the special slot,
|
|
68
77
|
* the id's namespace).
|
|
78
|
+
*
|
|
79
|
+
* `context` lets an item-level caller distinguish a catalog-auto discount from
|
|
80
|
+
* a collection discount: the catalog path writes the item's own id, which is
|
|
81
|
+
* usually also a 24-hex ObjectId. Callers that do not know the item ids keep
|
|
82
|
+
* the old shape-only behaviour.
|
|
69
83
|
*/
|
|
70
|
-
function classifyDiscountSubKind(slot, discountId) {
|
|
84
|
+
function classifyDiscountSubKind(slot, discountId, context) {
|
|
71
85
|
if (discountId == null)
|
|
72
86
|
return MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED;
|
|
73
87
|
if (slot === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.VOUCHER) {
|
|
@@ -79,14 +93,20 @@ function classifyDiscountSubKind(slot, discountId) {
|
|
|
79
93
|
if (discountId === COMMON_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID) {
|
|
80
94
|
return MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.WALLET_CASHBACK;
|
|
81
95
|
}
|
|
82
|
-
// Offers and gift cards both use ObjectIds
|
|
83
|
-
//
|
|
96
|
+
// Offers and gift cards both use ObjectIds. The wallet's gift-card claims
|
|
97
|
+
// are the authoritative discriminator when the caller has the order; a
|
|
98
|
+
// reference number (the card code) is the next-best signal.
|
|
99
|
+
const giftCardIds = context?.gift_card_ids;
|
|
100
|
+
if (giftCardIds != null &&
|
|
101
|
+
('has' in giftCardIds ? giftCardIds.has(discountId) : giftCardIds.includes(discountId))) {
|
|
102
|
+
return MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.GIFT_CARD;
|
|
103
|
+
}
|
|
84
104
|
return HEX24.test(discountId)
|
|
85
105
|
? MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER
|
|
86
106
|
: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.GIFT_CARD;
|
|
87
107
|
}
|
|
88
108
|
// SPECIAL slot — the namespace prefix is the discriminator.
|
|
89
|
-
if (discountId ===
|
|
109
|
+
if (discountId === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SPECIAL_DISCOUNT_ID) {
|
|
90
110
|
return MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM;
|
|
91
111
|
}
|
|
92
112
|
if (discountId.startsWith(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_PRICE_OVERRIDE_PREFIX)) {
|
|
@@ -95,8 +115,15 @@ function classifyDiscountSubKind(slot, discountId) {
|
|
|
95
115
|
if (discountId.startsWith(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_MANUAL_PREFIX)) {
|
|
96
116
|
return MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.MANUAL;
|
|
97
117
|
}
|
|
98
|
-
if (HEX24.test(discountId))
|
|
118
|
+
if (HEX24.test(discountId)) {
|
|
119
|
+
// A catalog-auto discount writes the item's own id. Both ids are checked:
|
|
120
|
+
// the catalog id is the normal path; the ORDER-item id is the re-order
|
|
121
|
+
// copy path (`order-history.ts`), where the spread preserves the item _id.
|
|
122
|
+
if (context != null && (discountId === context.catalog_item_id || discountId === context.order_item_id)) {
|
|
123
|
+
return MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM;
|
|
124
|
+
}
|
|
99
125
|
return MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.COLLECTION;
|
|
126
|
+
}
|
|
100
127
|
return MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM;
|
|
101
128
|
}
|
|
102
129
|
function resolveRowMoney(details, rowTotal) {
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loaded-row totals for the Discounts drill-down footer.
|
|
3
|
+
*
|
|
4
|
+
* Discounts and COMPLIMENTARY coverage are DIFFERENT money types: a comp row's
|
|
5
|
+
* `amount` is always 0 and its meaningful number is `covered_amount`. Summing
|
|
6
|
+
* both into one `Σ` is meaningless (and was the GAP-1 bug), so this helper
|
|
7
|
+
* keeps them separate and tells the caller which one the current tab shows.
|
|
8
|
+
*
|
|
9
|
+
* `view` is the wire view: `ALL` or one of the stored entry kinds.
|
|
10
|
+
*/
|
|
11
|
+
export interface IDiscountDetailsLoadedTotals {
|
|
12
|
+
/** Σ discount money of loaded non-comp rows (cents). */
|
|
13
|
+
discount_cents: number;
|
|
14
|
+
/** Σ comp coverage of loaded COMPLIMENTARY rows (cents). */
|
|
15
|
+
comp_cents: number;
|
|
16
|
+
/** Number of loaded COMPLIMENTARY rows. */
|
|
17
|
+
comp_count: number;
|
|
18
|
+
/** Loaded row count. */
|
|
19
|
+
row_count: number;
|
|
20
|
+
/** The money the footer shows for the current view (cents). */
|
|
21
|
+
primary_cents: number;
|
|
22
|
+
primary_label: 'discounts' | 'covered';
|
|
23
|
+
/** True when the footer should append the comp coverage breakdown. */
|
|
24
|
+
include_comp_breakdown: boolean;
|
|
25
|
+
}
|
|
26
|
+
export interface IDiscountDetailsLoadedRow {
|
|
27
|
+
kind: string;
|
|
28
|
+
amount?: number | null;
|
|
29
|
+
covered_amount?: number | null;
|
|
30
|
+
}
|
|
31
|
+
export declare function getDiscountDetailsLoadedTotals(rows: IDiscountDetailsLoadedRow[] | null | undefined, view: string): IDiscountDetailsLoadedTotals;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getDiscountDetailsLoadedTotals = getDiscountDetailsLoadedTotals;
|
|
4
|
+
const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
5
|
+
function getDiscountDetailsLoadedTotals(rows, view) {
|
|
6
|
+
const list = Array.isArray(rows) ? rows : [];
|
|
7
|
+
let discountCents = 0;
|
|
8
|
+
let compCents = 0;
|
|
9
|
+
let compCount = 0;
|
|
10
|
+
for (const row of list) {
|
|
11
|
+
if (row.kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY) {
|
|
12
|
+
compCount += 1;
|
|
13
|
+
compCents += Number.isFinite(row.covered_amount) ? Math.max(0, row.covered_amount ?? 0) : 0;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
discountCents += Number.isFinite(row.amount) ? Math.max(0, row.amount ?? 0) : 0;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const isComplimentaryView = view === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY;
|
|
20
|
+
return {
|
|
21
|
+
discount_cents: discountCents,
|
|
22
|
+
comp_cents: compCents,
|
|
23
|
+
comp_count: compCount,
|
|
24
|
+
row_count: list.length,
|
|
25
|
+
primary_cents: isComplimentaryView ? compCents : discountCents,
|
|
26
|
+
primary_label: isComplimentaryView ? 'covered' : 'discounts',
|
|
27
|
+
// Only the ALL view can contain both kinds; the kind tabs are filtered
|
|
28
|
+
// server-side, so they never append a cross-kind breakdown.
|
|
29
|
+
include_comp_breakdown: view === 'ALL' && compCount > 0,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { DAY_INDEX_ENUM } from '../../constants/ENUMS';
|
|
2
|
+
import { MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM, MATERIALIZED_DISCOUNT_LEVEL_ENUM, MATERIALIZED_DISCOUNT_SLOT_ENUM, MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM, MATERIALIZED_REPORT_SCOPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
|
+
import { IMaterializedDiscountEntry, IMaterializedDiscountKeySection } from '../../schemas/types/materialized_tenant_discount_entries';
|
|
4
|
+
import { IMaterializedTenantDiscountSource } from '../../schemas/types/materialized_tenant_discount_sources';
|
|
5
|
+
/**
|
|
6
|
+
* The "By discount" SOURCE classifier (M1).
|
|
7
|
+
*
|
|
8
|
+
* A source is the stable answer to "which discount was this?" — not "which
|
|
9
|
+
* amount was it?". The row's generated ids are therefore never the identity:
|
|
10
|
+
* `MANUAL_50%` and `MANUAL_Rs 500` are the SAME manual bucket, while a
|
|
11
|
+
* collection discount is keyed by its `discounts` `_id` and an offer/voucher by
|
|
12
|
+
* its id (or reference number).
|
|
13
|
+
*
|
|
14
|
+
* Pre-release: the current close always writes the source-relevant metadata
|
|
15
|
+
* (`catalog_auto` and comp full/partial). There is no compatibility branch for
|
|
16
|
+
* documents materialized before those fields existed; such periods are rebuilt
|
|
17
|
+
* from closed tenant-days, not read through a fallback.
|
|
18
|
+
*
|
|
19
|
+
* Pure and total: unknown shapes degrade to `unattributed`, never a throw.
|
|
20
|
+
*/
|
|
21
|
+
/** One source identity derived from one entry row. */
|
|
22
|
+
export interface IDiscountSourceIdentity {
|
|
23
|
+
source_key: string;
|
|
24
|
+
source_id: string | null;
|
|
25
|
+
source_name: string;
|
|
26
|
+
source_class: MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM;
|
|
27
|
+
kind: MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM;
|
|
28
|
+
level: MATERIALIZED_DISCOUNT_LEVEL_ENUM;
|
|
29
|
+
slot: MATERIALIZED_DISCOUNT_SLOT_ENUM | null;
|
|
30
|
+
sub_kind: MATERIALIZED_DISCOUNT_SUB_KIND_ENUM;
|
|
31
|
+
complimentary_full: boolean | null;
|
|
32
|
+
}
|
|
33
|
+
/** `'VISA CARD'` / `'visa-card'` → `'visa_card'`; bounded and lowercase. */
|
|
34
|
+
export declare function normalizeDiscountSourceKeyPart(value: string | null | undefined): string;
|
|
35
|
+
/** `'Discount applied for VISA CARD'` → `'VISA CARD'` (null when absent). */
|
|
36
|
+
export declare function getPaymentTypeFromDiscountReason(reason: string | null | undefined): string | null;
|
|
37
|
+
/**
|
|
38
|
+
* Classifies ONE entry row into its source identity.
|
|
39
|
+
*
|
|
40
|
+
* Ordering matters:
|
|
41
|
+
* 1. COMPLIMENTARY is a tender, not a discount.
|
|
42
|
+
* 2. A catalog-auto id is checked against the row's catalog/order-item ids
|
|
43
|
+
* BEFORE the generic 24-hex → collection rule, because both are ObjectIds.
|
|
44
|
+
* 3. A payment-type discount is a `MANUAL_*` id with a
|
|
45
|
+
* `Discount applied for <type>` reason; it must be separated from a manual
|
|
46
|
+
* discount BEFORE the manual bucket.
|
|
47
|
+
*/
|
|
48
|
+
export declare function classifyDiscountEntrySource(entry: IMaterializedDiscountEntry): IDiscountSourceIdentity;
|
|
49
|
+
export interface IBuildDiscountSourcesInput {
|
|
50
|
+
tenantId: string;
|
|
51
|
+
brandId: string;
|
|
52
|
+
scopeContext: MATERIALIZED_REPORT_SCOPE_ENUM;
|
|
53
|
+
scopeField: string;
|
|
54
|
+
dayIndex?: DAY_INDEX_ENUM | null;
|
|
55
|
+
/** The post-plan entry sections: `{ [ORDER_STATUS:PLATFORM]: { rows } }`. */
|
|
56
|
+
sections: Record<string, IMaterializedDiscountKeySection | {
|
|
57
|
+
rows: IMaterializedDiscountEntry[];
|
|
58
|
+
}> | null | undefined;
|
|
59
|
+
computedAt: Date;
|
|
60
|
+
sourceWatermark?: Date | null;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Builds the source documents for ONE scope from its POST-PLAN entry sections.
|
|
64
|
+
*
|
|
65
|
+
* The caller passes the complete final entry document for the scope (day,
|
|
66
|
+
* hour or accumulated period). Period scopes therefore yield true period
|
|
67
|
+
* sources without a second accumulator: the entry document already holds every
|
|
68
|
+
* sibling day's rows.
|
|
69
|
+
*
|
|
70
|
+
* Returns `[]` for an empty scope (the caller deletes any stored sources for
|
|
71
|
+
* that scope). Returns `null` on malformed input.
|
|
72
|
+
*/
|
|
73
|
+
export declare function buildDiscountSourcesForScope(input: IBuildDiscountSourcesInput | null | undefined): IMaterializedTenantDiscountSource[] | null;
|