@managesome/eposmatic-toolkit 0.4.22 → 0.4.24
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_rider_daily_reports.js +0 -4
- 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/models/tenant_hr_attendance_records.js +5 -6
- package/esm/schemas/models/tenant_hr_shift_schedules.js +1 -1
- 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_rider_daily_reports.js +0 -4
- 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/models/tenant_hr_attendance_records.d.ts +3 -3
- package/lib/schemas/models/tenant_hr_attendance_records.js +5 -6
- package/lib/schemas/models/tenant_hr_shift_schedules.d.ts +3 -3
- package/lib/schemas/models/tenant_hr_shift_schedules.js +1 -1
- 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/schemas/types/tenant_hr_attendance_records.d.ts +1 -1
- package/lib/schemas/types/tenant_hr_shift_schedules.d.ts +1 -1
- 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
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.normalizeDiscountSourceKeyPart = normalizeDiscountSourceKeyPart;
|
|
7
|
+
exports.getPaymentTypeFromDiscountReason = getPaymentTypeFromDiscountReason;
|
|
8
|
+
exports.classifyDiscountEntrySource = classifyDiscountEntrySource;
|
|
9
|
+
exports.buildDiscountSourcesForScope = buildDiscountSourcesForScope;
|
|
10
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
11
|
+
const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
12
|
+
const materialized_tenant_discount_sources_1 = require("../../schemas/models/materialized_tenant_discount_sources");
|
|
13
|
+
/** `'VISA CARD'` / `'visa-card'` → `'visa_card'`; bounded and lowercase. */
|
|
14
|
+
function normalizeDiscountSourceKeyPart(value) {
|
|
15
|
+
if (typeof value !== 'string')
|
|
16
|
+
return '';
|
|
17
|
+
return value
|
|
18
|
+
.trim()
|
|
19
|
+
.toLowerCase()
|
|
20
|
+
.replace(/[^a-z0-9]+/g, '_')
|
|
21
|
+
.replace(/^_+|_+$/g, '')
|
|
22
|
+
.slice(0, 80);
|
|
23
|
+
}
|
|
24
|
+
/** `'Discount applied for VISA CARD'` → `'VISA CARD'` (null when absent). */
|
|
25
|
+
function getPaymentTypeFromDiscountReason(reason) {
|
|
26
|
+
if (typeof reason !== 'string')
|
|
27
|
+
return null;
|
|
28
|
+
const match = /^discount\s+applied\s+for\s+(.+)$/i.exec(reason.trim());
|
|
29
|
+
if (match == null)
|
|
30
|
+
return null;
|
|
31
|
+
const value = match[1].trim();
|
|
32
|
+
return value.length > 0 ? value : null;
|
|
33
|
+
}
|
|
34
|
+
/** `'VISA CARD'` → `'Visa Card'` (display only). */
|
|
35
|
+
function toDisplayWords(value) {
|
|
36
|
+
return value
|
|
37
|
+
.replace(/[_-]+/g, ' ')
|
|
38
|
+
.replace(/\s+/g, ' ')
|
|
39
|
+
.trim()
|
|
40
|
+
.split(' ')
|
|
41
|
+
.filter((part) => part.length > 0)
|
|
42
|
+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase())
|
|
43
|
+
.join(' ');
|
|
44
|
+
}
|
|
45
|
+
/** Short id used in a fallback display name. */
|
|
46
|
+
function shortId(value) {
|
|
47
|
+
return value.length > 8 ? value.slice(-6) : value;
|
|
48
|
+
}
|
|
49
|
+
const cleanName = (value) => {
|
|
50
|
+
if (typeof value !== 'string')
|
|
51
|
+
return null;
|
|
52
|
+
const trimmed = value.trim();
|
|
53
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
54
|
+
};
|
|
55
|
+
const unattributedIdentity = (level) => ({
|
|
56
|
+
source_key: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.UNATTRIBUTED,
|
|
57
|
+
source_id: null,
|
|
58
|
+
source_name: 'Other / unattributed',
|
|
59
|
+
source_class: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.UNATTRIBUTED,
|
|
60
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT,
|
|
61
|
+
level,
|
|
62
|
+
slot: null,
|
|
63
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED,
|
|
64
|
+
complimentary_full: null,
|
|
65
|
+
});
|
|
66
|
+
/**
|
|
67
|
+
* Classifies ONE entry row into its source identity.
|
|
68
|
+
*
|
|
69
|
+
* Ordering matters:
|
|
70
|
+
* 1. COMPLIMENTARY is a tender, not a discount.
|
|
71
|
+
* 2. A catalog-auto id is checked against the row's catalog/order-item ids
|
|
72
|
+
* BEFORE the generic 24-hex → collection rule, because both are ObjectIds.
|
|
73
|
+
* 3. A payment-type discount is a `MANUAL_*` id with a
|
|
74
|
+
* `Discount applied for <type>` reason; it must be separated from a manual
|
|
75
|
+
* discount BEFORE the manual bucket.
|
|
76
|
+
*/
|
|
77
|
+
function classifyDiscountEntrySource(entry) {
|
|
78
|
+
const level = entry.level;
|
|
79
|
+
if (entry.kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY) {
|
|
80
|
+
const full = entry.complimentary_full;
|
|
81
|
+
if (full === true) {
|
|
82
|
+
return {
|
|
83
|
+
source_key: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.COMPLIMENTARY_FULL,
|
|
84
|
+
source_id: null,
|
|
85
|
+
source_name: 'Fully complimentary',
|
|
86
|
+
source_class: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COMPLIMENTARY,
|
|
87
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY,
|
|
88
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.PAYMENT,
|
|
89
|
+
slot: null,
|
|
90
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED,
|
|
91
|
+
complimentary_full: true,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
if (full === false) {
|
|
95
|
+
return {
|
|
96
|
+
source_key: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.COMPLIMENTARY_PARTIAL,
|
|
97
|
+
source_id: null,
|
|
98
|
+
source_name: 'Partially complimentary',
|
|
99
|
+
source_class: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COMPLIMENTARY,
|
|
100
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY,
|
|
101
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.PAYMENT,
|
|
102
|
+
slot: null,
|
|
103
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED,
|
|
104
|
+
complimentary_full: false,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
// Defensive invalid-data bucket. The current close ALWAYS writes
|
|
108
|
+
// `complimentary_full`; null means a corrupt/partial document (or a direct
|
|
109
|
+
// DB write). Mark it unknown, never guess full vs partial.
|
|
110
|
+
return {
|
|
111
|
+
source_key: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.COMPLIMENTARY_UNKNOWN,
|
|
112
|
+
source_id: null,
|
|
113
|
+
source_name: 'Complimentary (unclassified)',
|
|
114
|
+
source_class: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COMPLIMENTARY,
|
|
115
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY,
|
|
116
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.PAYMENT,
|
|
117
|
+
slot: null,
|
|
118
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED,
|
|
119
|
+
complimentary_full: null,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
if (level === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.PAYMENT) {
|
|
123
|
+
return unattributedIdentity(level);
|
|
124
|
+
}
|
|
125
|
+
const slot = entry.slot;
|
|
126
|
+
const discountId = cleanName(entry.discount_id);
|
|
127
|
+
const name = cleanName(entry.name);
|
|
128
|
+
const reference = cleanName(entry.reference_number);
|
|
129
|
+
const subKind = entry.sub_kind;
|
|
130
|
+
if (slot === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL) {
|
|
131
|
+
// Pre-release: catalog auto is identified ONLY by the marker written by the
|
|
132
|
+
// current accumulator or by the current classifier's CATALOG_ITEM value.
|
|
133
|
+
// No id-shape/name fallback for documents materialized before that marker.
|
|
134
|
+
const isCatalogAuto = entry.catalog_auto === true;
|
|
135
|
+
if (isCatalogAuto || subKind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM) {
|
|
136
|
+
return {
|
|
137
|
+
source_key: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.CATALOG_AUTO,
|
|
138
|
+
source_id: null,
|
|
139
|
+
source_name: 'Catalog auto discount',
|
|
140
|
+
source_class: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.CATALOG_AUTO,
|
|
141
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT,
|
|
142
|
+
level,
|
|
143
|
+
slot,
|
|
144
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM,
|
|
145
|
+
complimentary_full: null,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
if (subKind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.PRICE_OVERRIDE) {
|
|
149
|
+
return {
|
|
150
|
+
source_key: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.PRICE_OVERRIDE_ITEM,
|
|
151
|
+
source_id: null,
|
|
152
|
+
source_name: 'Price override',
|
|
153
|
+
source_class: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.PRICE_OVERRIDE,
|
|
154
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT,
|
|
155
|
+
level,
|
|
156
|
+
slot,
|
|
157
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.PRICE_OVERRIDE,
|
|
158
|
+
complimentary_full: null,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
if (subKind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.MANUAL) {
|
|
162
|
+
const paymentType = getPaymentTypeFromDiscountReason(entry.reason);
|
|
163
|
+
if (paymentType != null) {
|
|
164
|
+
const normalized = normalizeDiscountSourceKeyPart(paymentType);
|
|
165
|
+
if (normalized.length > 0) {
|
|
166
|
+
return {
|
|
167
|
+
source_key: `${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY_PREFIX.PAYMENT_TYPE}${normalized}`,
|
|
168
|
+
source_id: normalized,
|
|
169
|
+
source_name: `${toDisplayWords(paymentType)} discount`,
|
|
170
|
+
source_class: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.PAYMENT_TYPE,
|
|
171
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT,
|
|
172
|
+
level,
|
|
173
|
+
slot,
|
|
174
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.MANUAL,
|
|
175
|
+
complimentary_full: null,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
source_key: level === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER
|
|
181
|
+
? MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.MANUAL_ORDER
|
|
182
|
+
: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.MANUAL_ITEM,
|
|
183
|
+
source_id: null,
|
|
184
|
+
source_name: level === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER ? 'Manual / quick discount' : 'Manual item discount',
|
|
185
|
+
source_class: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.MANUAL,
|
|
186
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT,
|
|
187
|
+
level,
|
|
188
|
+
slot,
|
|
189
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.MANUAL,
|
|
190
|
+
complimentary_full: null,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
if (subKind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.COLLECTION && discountId != null) {
|
|
194
|
+
return {
|
|
195
|
+
source_key: `${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY_PREFIX.DISCOUNT}${discountId}`,
|
|
196
|
+
source_id: discountId,
|
|
197
|
+
source_name: name ?? `Discount ${shortId(discountId)}`,
|
|
198
|
+
source_class: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COLLECTION,
|
|
199
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT,
|
|
200
|
+
level,
|
|
201
|
+
slot,
|
|
202
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.COLLECTION,
|
|
203
|
+
complimentary_full: null,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
return unattributedIdentity(level);
|
|
207
|
+
}
|
|
208
|
+
if (slot === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY) {
|
|
209
|
+
if (subKind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.WALLET_CASHBACK) {
|
|
210
|
+
return {
|
|
211
|
+
source_key: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.WALLET_CASHBACK,
|
|
212
|
+
source_id: null,
|
|
213
|
+
source_name: 'Wallet cashback',
|
|
214
|
+
source_class: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.LOYALTY,
|
|
215
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT,
|
|
216
|
+
level,
|
|
217
|
+
slot,
|
|
218
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.WALLET_CASHBACK,
|
|
219
|
+
complimentary_full: null,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
// Offers and gift cards both write an ObjectId into the loyalty slot. A
|
|
223
|
+
// reference number is the gift-card code (the loyalty offer writer
|
|
224
|
+
// explicitly leaves it undefined), so it refines an OFFER row when the
|
|
225
|
+
// reference proves a gift card. The same rule is applied live in
|
|
226
|
+
// `readDiscountSlot` from `wallet_redemptions.gift_cards`.
|
|
227
|
+
const loyaltyIsGiftCard = subKind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.GIFT_CARD ||
|
|
228
|
+
(subKind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER && reference != null && reference !== 'WALLET_CASHBACK');
|
|
229
|
+
if (loyaltyIsGiftCard) {
|
|
230
|
+
const keyPart = discountId ?? reference;
|
|
231
|
+
if (keyPart == null)
|
|
232
|
+
return unattributedIdentity(level);
|
|
233
|
+
return {
|
|
234
|
+
source_key: `${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY_PREFIX.LOYALTY_GIFT_CARD}${keyPart}`,
|
|
235
|
+
source_id: keyPart,
|
|
236
|
+
source_name: name ?? `Gift card ${reference ?? shortId(keyPart)}`,
|
|
237
|
+
source_class: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.LOYALTY,
|
|
238
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT,
|
|
239
|
+
level,
|
|
240
|
+
slot,
|
|
241
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.GIFT_CARD,
|
|
242
|
+
complimentary_full: null,
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
if (subKind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER) {
|
|
246
|
+
const keyPart = discountId ?? reference;
|
|
247
|
+
if (keyPart == null)
|
|
248
|
+
return unattributedIdentity(level);
|
|
249
|
+
return {
|
|
250
|
+
source_key: `${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY_PREFIX.LOYALTY_OFFER}${keyPart}`,
|
|
251
|
+
source_id: keyPart,
|
|
252
|
+
source_name: name ?? `Offer ${shortId(keyPart)}`,
|
|
253
|
+
source_class: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.LOYALTY,
|
|
254
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT,
|
|
255
|
+
level,
|
|
256
|
+
slot,
|
|
257
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER,
|
|
258
|
+
complimentary_full: null,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
return unattributedIdentity(level);
|
|
262
|
+
}
|
|
263
|
+
if (slot === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.VOUCHER) {
|
|
264
|
+
const keyPart = discountId ?? reference;
|
|
265
|
+
if (keyPart == null)
|
|
266
|
+
return unattributedIdentity(level);
|
|
267
|
+
return {
|
|
268
|
+
source_key: `${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY_PREFIX.VOUCHER}${keyPart}`,
|
|
269
|
+
source_id: keyPart,
|
|
270
|
+
source_name: name ?? `Voucher ${reference ?? shortId(keyPart)}`,
|
|
271
|
+
source_class: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.VOUCHER,
|
|
272
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT,
|
|
273
|
+
level,
|
|
274
|
+
slot,
|
|
275
|
+
sub_kind: subKind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER
|
|
276
|
+
? MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER
|
|
277
|
+
: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED,
|
|
278
|
+
complimentary_full: null,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
return unattributedIdentity(level);
|
|
282
|
+
}
|
|
283
|
+
const nonNegative = (value) => (Number.isFinite(value) && value > 0 ? value : 0);
|
|
284
|
+
const round2 = (value) => Math.round((value ?? 0) * 100) / 100;
|
|
285
|
+
const emptyMetrics = () => ({
|
|
286
|
+
orderIds: new Set(),
|
|
287
|
+
row_count: 0,
|
|
288
|
+
discount_amount: 0,
|
|
289
|
+
free_item_count: 0,
|
|
290
|
+
free_item_value: 0,
|
|
291
|
+
covered_amount: 0,
|
|
292
|
+
percentage_row_count: 0,
|
|
293
|
+
percentage_rate_sum: 0,
|
|
294
|
+
fixed_row_count: 0,
|
|
295
|
+
});
|
|
296
|
+
/** Folds one row into a metrics bucket. */
|
|
297
|
+
function addRow(metrics, entry) {
|
|
298
|
+
const orderId = cleanName(entry.order_id);
|
|
299
|
+
if (orderId != null)
|
|
300
|
+
metrics.orderIds.add(orderId);
|
|
301
|
+
metrics.row_count += 1;
|
|
302
|
+
if (entry.kind !== MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY) {
|
|
303
|
+
metrics.discount_amount += nonNegative(entry.amount);
|
|
304
|
+
}
|
|
305
|
+
if (entry.kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.FREE_ITEM) {
|
|
306
|
+
metrics.free_item_count += 1;
|
|
307
|
+
metrics.free_item_value += nonNegative(entry.item_value ?? 0);
|
|
308
|
+
}
|
|
309
|
+
if (entry.kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY) {
|
|
310
|
+
metrics.covered_amount += nonNegative(entry.covered_amount ?? 0);
|
|
311
|
+
}
|
|
312
|
+
if (entry.is_percentage === true) {
|
|
313
|
+
metrics.percentage_row_count += 1;
|
|
314
|
+
metrics.percentage_rate_sum += nonNegative(entry.rate ?? 0);
|
|
315
|
+
}
|
|
316
|
+
else if (entry.is_percentage === false) {
|
|
317
|
+
metrics.fixed_row_count += 1;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
const metricsToKeyMetrics = (key, metrics) => ({
|
|
321
|
+
key,
|
|
322
|
+
order_count: metrics.orderIds.size,
|
|
323
|
+
row_count: metrics.row_count,
|
|
324
|
+
discount_amount: metrics.discount_amount,
|
|
325
|
+
free_item_count: metrics.free_item_count,
|
|
326
|
+
free_item_value: metrics.free_item_value,
|
|
327
|
+
covered_amount: metrics.covered_amount,
|
|
328
|
+
percentage_row_count: metrics.percentage_row_count,
|
|
329
|
+
percentage_rate_sum: metrics.percentage_rate_sum,
|
|
330
|
+
fixed_row_count: metrics.fixed_row_count,
|
|
331
|
+
});
|
|
332
|
+
const rateKindOf = (metrics) => {
|
|
333
|
+
if (metrics.percentage_row_count > 0 && metrics.fixed_row_count > 0) {
|
|
334
|
+
return MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.MIXED;
|
|
335
|
+
}
|
|
336
|
+
if (metrics.percentage_row_count > 0)
|
|
337
|
+
return MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE;
|
|
338
|
+
if (metrics.fixed_row_count > 0)
|
|
339
|
+
return MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.FIXED;
|
|
340
|
+
return null;
|
|
341
|
+
};
|
|
342
|
+
/**
|
|
343
|
+
* Builds the source documents for ONE scope from its POST-PLAN entry sections.
|
|
344
|
+
*
|
|
345
|
+
* The caller passes the complete final entry document for the scope (day,
|
|
346
|
+
* hour or accumulated period). Period scopes therefore yield true period
|
|
347
|
+
* sources without a second accumulator: the entry document already holds every
|
|
348
|
+
* sibling day's rows.
|
|
349
|
+
*
|
|
350
|
+
* Returns `[]` for an empty scope (the caller deletes any stored sources for
|
|
351
|
+
* that scope). Returns `null` on malformed input.
|
|
352
|
+
*/
|
|
353
|
+
function buildDiscountSourcesForScope(input) {
|
|
354
|
+
if (input == null)
|
|
355
|
+
return null;
|
|
356
|
+
const { tenantId, brandId, scopeContext, scopeField, sections, computedAt } = input;
|
|
357
|
+
if (typeof tenantId !== 'string' || tenantId.length === 0)
|
|
358
|
+
return null;
|
|
359
|
+
if (typeof brandId !== 'string' || brandId.length === 0)
|
|
360
|
+
return null;
|
|
361
|
+
if (typeof scopeField !== 'string' || scopeField.length === 0)
|
|
362
|
+
return null;
|
|
363
|
+
if (!(computedAt instanceof Date) || Number.isNaN(computedAt.getTime()))
|
|
364
|
+
return null;
|
|
365
|
+
const buckets = new Map();
|
|
366
|
+
for (const [key, section] of Object.entries(sections ?? {})) {
|
|
367
|
+
for (const entry of section?.rows ?? []) {
|
|
368
|
+
if (entry == null)
|
|
369
|
+
continue;
|
|
370
|
+
const identity = classifyDiscountEntrySource(entry);
|
|
371
|
+
const bucketKey = `${identity.source_key}|${identity.level}`;
|
|
372
|
+
let bucket = buckets.get(bucketKey);
|
|
373
|
+
if (bucket == null) {
|
|
374
|
+
bucket = { identity, totals: emptyMetrics(), byKey: new Map() };
|
|
375
|
+
buckets.set(bucketKey, bucket);
|
|
376
|
+
}
|
|
377
|
+
addRow(bucket.totals, entry);
|
|
378
|
+
const keyMetrics = bucket.byKey.get(key) ?? emptyMetrics();
|
|
379
|
+
addRow(keyMetrics, entry);
|
|
380
|
+
bucket.byKey.set(key, keyMetrics);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
const sourceWatermark = input.sourceWatermark ?? computedAt;
|
|
384
|
+
const docs = [];
|
|
385
|
+
for (const bucket of buckets.values()) {
|
|
386
|
+
const { identity, totals } = bucket;
|
|
387
|
+
const rate = totals.percentage_row_count > 0 ? round2(totals.percentage_rate_sum / totals.percentage_row_count) : null;
|
|
388
|
+
const kind = identity.kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY
|
|
389
|
+
? MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY
|
|
390
|
+
: totals.free_item_count > 0 && totals.free_item_count === totals.row_count
|
|
391
|
+
? MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.FREE_ITEM
|
|
392
|
+
: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT;
|
|
393
|
+
docs.push({
|
|
394
|
+
_id: new mongoose_1.default.Types.ObjectId().toHexString(),
|
|
395
|
+
tenant_id: tenantId,
|
|
396
|
+
brand_id: brandId,
|
|
397
|
+
scope_context: scopeContext,
|
|
398
|
+
scope_field: scopeField,
|
|
399
|
+
day_index: input.dayIndex ?? null,
|
|
400
|
+
source_key: identity.source_key,
|
|
401
|
+
source_id: identity.source_id,
|
|
402
|
+
source_name: identity.source_name,
|
|
403
|
+
source_class: identity.source_class,
|
|
404
|
+
kind,
|
|
405
|
+
level: identity.level,
|
|
406
|
+
slot: identity.slot,
|
|
407
|
+
sub_kind: identity.sub_kind,
|
|
408
|
+
rate,
|
|
409
|
+
rate_kind: rateKindOf(totals),
|
|
410
|
+
percentage_row_count: totals.percentage_row_count,
|
|
411
|
+
percentage_rate_sum: totals.percentage_rate_sum,
|
|
412
|
+
fixed_row_count: totals.fixed_row_count,
|
|
413
|
+
order_count: totals.orderIds.size,
|
|
414
|
+
row_count: totals.row_count,
|
|
415
|
+
discount_amount: totals.discount_amount,
|
|
416
|
+
free_item_count: totals.free_item_count,
|
|
417
|
+
free_item_value: totals.free_item_value,
|
|
418
|
+
complimentary_full: identity.complimentary_full,
|
|
419
|
+
covered_amount: totals.covered_amount,
|
|
420
|
+
key_metrics: [...bucket.byKey.entries()]
|
|
421
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
422
|
+
.map(([key, metrics]) => metricsToKeyMetrics(key, metrics)),
|
|
423
|
+
sale_type_key_filters: [...MATERIALIZED_REPORTS_1.DISCOUNTS_KEY_FILTERS],
|
|
424
|
+
schema_version: materialized_tenant_discount_sources_1.MATERIALIZED_TENANT_DISCOUNT_SOURCES_SCHEMA_VERSION,
|
|
425
|
+
source_watermark: sourceWatermark,
|
|
426
|
+
computed_at: computedAt,
|
|
427
|
+
createdAt: computedAt,
|
|
428
|
+
updatedAt: computedAt,
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
// Biggest money first, then the free/complimentary rows (which carry no
|
|
432
|
+
// discount money), then name — a deterministic order for the write and the
|
|
433
|
+
// tests. The READ sorts again after folding scopes.
|
|
434
|
+
return docs.sort((a, b) => b.discount_amount - a.discount_amount ||
|
|
435
|
+
a.source_name.localeCompare(b.source_name) ||
|
|
436
|
+
a.level.localeCompare(b.level));
|
|
437
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
import { IMaterializedTenantDiscountSourcesReport } from '../types/materialized-reports-apis';
|
|
3
|
+
/**
|
|
4
|
+
* Frontend formatter for `POST /reports/discounts/sources` — the "By discount"
|
|
5
|
+
* tab.
|
|
6
|
+
*
|
|
7
|
+
* Money converts CENTS → decimals exactly once, here. `share_percent` is
|
|
8
|
+
* computed from the formatted rows themselves so the table always sums to
|
|
9
|
+
* 100% (excluding complimentary, which is deliberately not a discount). The
|
|
10
|
+
* endpoint already folded the selection's scope groups and applied the
|
|
11
|
+
* platform / order-status filters, so this formatter never re-aggregates money.
|
|
12
|
+
*/
|
|
13
|
+
export interface IFormattedDiscountSourceRow {
|
|
14
|
+
source_key: string;
|
|
15
|
+
source_name: string;
|
|
16
|
+
source_class: MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM;
|
|
17
|
+
class_label: string;
|
|
18
|
+
kind: string;
|
|
19
|
+
level: string;
|
|
20
|
+
level_label: string;
|
|
21
|
+
type_label: string;
|
|
22
|
+
rate: number | null;
|
|
23
|
+
rate_label: string;
|
|
24
|
+
order_count: number;
|
|
25
|
+
discount_amount: number;
|
|
26
|
+
discount_amount_label: string;
|
|
27
|
+
share_percent: number;
|
|
28
|
+
share_label: string;
|
|
29
|
+
free_item_count: number;
|
|
30
|
+
free_item_value: number;
|
|
31
|
+
covered_amount: number;
|
|
32
|
+
complimentary_full: boolean | null;
|
|
33
|
+
}
|
|
34
|
+
export interface IFormattedDiscountSourcesPage {
|
|
35
|
+
tenant_ids: string[];
|
|
36
|
+
sources: IFormattedDiscountSourceRow[];
|
|
37
|
+
complimentary: IFormattedDiscountSourceRow[];
|
|
38
|
+
totals: {
|
|
39
|
+
discount_amount: number;
|
|
40
|
+
source_count: number;
|
|
41
|
+
order_count: number;
|
|
42
|
+
complimentary_covered_amount: number;
|
|
43
|
+
complimentary_full_count: number;
|
|
44
|
+
complimentary_partial_count: number;
|
|
45
|
+
complimentary_unknown_count: number;
|
|
46
|
+
};
|
|
47
|
+
top_source: IFormattedDiscountSourceRow | null;
|
|
48
|
+
has_data: boolean;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Reconciliation between the summary total and the source rows (decimals).
|
|
52
|
+
*
|
|
53
|
+
* - `gap` = summary > source: unattributed/explained-elsewhere money; shares use
|
|
54
|
+
* the summary denominator and sum below 100%.
|
|
55
|
+
* - `overage` = source > summary: a data-consistency warning; shares use the
|
|
56
|
+
* source denominator so no row can exceed 100%.
|
|
57
|
+
* - `share_basis` is the larger of the two (never negative).
|
|
58
|
+
*/
|
|
59
|
+
export interface IDiscountSourceShareReconciliation {
|
|
60
|
+
share_basis: number;
|
|
61
|
+
gap: number;
|
|
62
|
+
overage: number;
|
|
63
|
+
show_gap: boolean;
|
|
64
|
+
show_overage: boolean;
|
|
65
|
+
}
|
|
66
|
+
export declare function getDiscountSourceShareReconciliation(summaryTotal: number | null | undefined, sourceTotal: number): IDiscountSourceShareReconciliation;
|
|
67
|
+
/**
|
|
68
|
+
* Formats one or more tenants' source records into the page model.
|
|
69
|
+
*
|
|
70
|
+
* `share_basis` (DECIMALS) lets the page use the SAME denominator as the Hero /
|
|
71
|
+
* summary — normally `summary.total_discount` for the already-filtered page.
|
|
72
|
+
* The effective denominator is `max(summary, source total)`: a source total
|
|
73
|
+
* above the summary is a data-consistency warning, not a >100% share.
|
|
74
|
+
*
|
|
75
|
+
* Returns `null` on unformattable input (never throws).
|
|
76
|
+
*/
|
|
77
|
+
export declare function getFormattedTenantDiscountSources(reports: IMaterializedTenantDiscountSourcesReport[] | null | undefined, options?: {
|
|
78
|
+
share_basis?: number | null;
|
|
79
|
+
}): IFormattedDiscountSourcesPage | null;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getDiscountSourceShareReconciliation = getDiscountSourceShareReconciliation;
|
|
4
|
+
exports.getFormattedTenantDiscountSources = getFormattedTenantDiscountSources;
|
|
5
|
+
const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
6
|
+
const price_conversion_1 = require("../utils/price-conversion");
|
|
7
|
+
const round2 = (value) => Math.round((value ?? 0) * 100) / 100;
|
|
8
|
+
function getDiscountSourceShareReconciliation(summaryTotal, sourceTotal) {
|
|
9
|
+
const summary = Number.isFinite(summaryTotal) ? Math.max(0, summaryTotal ?? 0) : 0;
|
|
10
|
+
const source = Number.isFinite(sourceTotal) ? Math.max(0, sourceTotal) : 0;
|
|
11
|
+
const gap = round2(Math.max(0, summary - source));
|
|
12
|
+
const overage = round2(Math.max(0, source - summary));
|
|
13
|
+
return {
|
|
14
|
+
share_basis: round2(Math.max(summary, source)),
|
|
15
|
+
gap,
|
|
16
|
+
overage,
|
|
17
|
+
show_gap: gap > 0.009,
|
|
18
|
+
show_overage: overage > 0.009,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
const toDecimal = (cents) => (0, price_conversion_1.convertPriceFromCents)(cents ?? 0);
|
|
22
|
+
const CLASS_LABELS = {
|
|
23
|
+
[MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COLLECTION]: 'Collection',
|
|
24
|
+
[MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.MANUAL]: 'Manual',
|
|
25
|
+
[MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.PAYMENT_TYPE]: 'Payment type',
|
|
26
|
+
[MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.PRICE_OVERRIDE]: 'Price override',
|
|
27
|
+
[MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.CATALOG_AUTO]: 'Catalog auto',
|
|
28
|
+
[MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.LOYALTY]: 'Loyalty',
|
|
29
|
+
[MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.VOUCHER]: 'Voucher',
|
|
30
|
+
[MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COMPLIMENTARY]: 'Complimentary',
|
|
31
|
+
[MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.UNATTRIBUTED]: 'Other',
|
|
32
|
+
};
|
|
33
|
+
const LEVEL_LABELS = {
|
|
34
|
+
ORDER: 'Order',
|
|
35
|
+
ITEM: 'Item',
|
|
36
|
+
PAYMENT: 'Payment',
|
|
37
|
+
};
|
|
38
|
+
const formatRate = (rate) => {
|
|
39
|
+
if (rate == null || !Number.isFinite(rate))
|
|
40
|
+
return '—';
|
|
41
|
+
return `${Math.round(rate * 100) / 100}%`;
|
|
42
|
+
};
|
|
43
|
+
const rateKindLabel = (rateKind) => {
|
|
44
|
+
switch (rateKind) {
|
|
45
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE:
|
|
46
|
+
return 'Percentage';
|
|
47
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.FIXED:
|
|
48
|
+
return 'Fixed';
|
|
49
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.MIXED:
|
|
50
|
+
return 'Mixed';
|
|
51
|
+
default:
|
|
52
|
+
return '—';
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const makeRow = (source, shareBasis) => {
|
|
56
|
+
const amount = round2(toDecimal(source.discount_amount));
|
|
57
|
+
const levelLabel = LEVEL_LABELS[source.level] ?? source.level;
|
|
58
|
+
const kindLabel = rateKindLabel(source.rate_kind);
|
|
59
|
+
// `shareBasis` is CENTS; the row amount above is decimals. Compute the share
|
|
60
|
+
// in ONE unit so the table sums to 100%.
|
|
61
|
+
const sharePercent = shareBasis > 0 ? round2((source.discount_amount / shareBasis) * 100) : 0;
|
|
62
|
+
return {
|
|
63
|
+
source_key: source.source_key,
|
|
64
|
+
source_name: source.source_name,
|
|
65
|
+
source_class: source.source_class,
|
|
66
|
+
class_label: CLASS_LABELS[source.source_class] ?? source.source_class,
|
|
67
|
+
kind: source.kind,
|
|
68
|
+
level: source.level,
|
|
69
|
+
level_label: levelLabel,
|
|
70
|
+
type_label: kindLabel === '—' ? levelLabel : `${levelLabel} · ${kindLabel}`,
|
|
71
|
+
rate: source.rate,
|
|
72
|
+
rate_label: source.rate_kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.MIXED ? 'mixed' : formatRate(source.rate),
|
|
73
|
+
order_count: source.order_count,
|
|
74
|
+
discount_amount: amount,
|
|
75
|
+
discount_amount_label: amount.toFixed(2),
|
|
76
|
+
share_percent: sharePercent,
|
|
77
|
+
share_label: `${(Math.round(sharePercent * 10) / 10).toFixed(1)}%`,
|
|
78
|
+
free_item_count: source.free_item_count,
|
|
79
|
+
free_item_value: round2(toDecimal(source.free_item_value)),
|
|
80
|
+
covered_amount: round2(toDecimal(source.covered_amount)),
|
|
81
|
+
complimentary_full: source.complimentary_full,
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Formats one or more tenants' source records into the page model.
|
|
86
|
+
*
|
|
87
|
+
* `share_basis` (DECIMALS) lets the page use the SAME denominator as the Hero /
|
|
88
|
+
* summary — normally `summary.total_discount` for the already-filtered page.
|
|
89
|
+
* The effective denominator is `max(summary, source total)`: a source total
|
|
90
|
+
* above the summary is a data-consistency warning, not a >100% share.
|
|
91
|
+
*
|
|
92
|
+
* Returns `null` on unformattable input (never throws).
|
|
93
|
+
*/
|
|
94
|
+
function getFormattedTenantDiscountSources(reports, options) {
|
|
95
|
+
if (!Array.isArray(reports) || reports.some((report) => report == null))
|
|
96
|
+
return null;
|
|
97
|
+
const rawSources = reports.flatMap((report) => report.sources ?? []);
|
|
98
|
+
const discountRows = rawSources.filter((source) => source.source_class !== MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COMPLIMENTARY);
|
|
99
|
+
const complimentaryRows = rawSources.filter((source) => source.source_class === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COMPLIMENTARY);
|
|
100
|
+
const totalCents = discountRows.reduce((sum, source) => sum + (source.discount_amount ?? 0), 0);
|
|
101
|
+
const summaryCents = options?.share_basis != null && Number.isFinite(options.share_basis) && options.share_basis > 0
|
|
102
|
+
? Math.round(options.share_basis * 100)
|
|
103
|
+
: null;
|
|
104
|
+
// Shares use the LARGER of the report summary and the source total. A source
|
|
105
|
+
// total above the summary is a data-consistency warning, but it must never
|
|
106
|
+
// produce a >100% share; a summary above the source total keeps the
|
|
107
|
+
// unattributed gap visible (shares sum below 100%).
|
|
108
|
+
const shareBasisCents = Math.max(summaryCents ?? totalCents, totalCents);
|
|
109
|
+
const sources = discountRows
|
|
110
|
+
.map((source) => makeRow(source, shareBasisCents))
|
|
111
|
+
.sort((a, b) => b.discount_amount - a.discount_amount || a.source_name.localeCompare(b.source_name));
|
|
112
|
+
const complimentary = complimentaryRows
|
|
113
|
+
.map((source) => makeRow(source, 0))
|
|
114
|
+
.sort((a, b) => b.covered_amount - a.covered_amount || a.source_name.localeCompare(b.source_name));
|
|
115
|
+
// An order is counted once per source. Summing source order counts would
|
|
116
|
+
// double-count an order with two different discounts, so the page does NOT
|
|
117
|
+
// present this as a distinct order total; it is kept for display context.
|
|
118
|
+
const orderCount = discountRows.reduce((sum, source) => sum + (source.order_count ?? 0), 0);
|
|
119
|
+
const complimentaryFullCount = complimentaryRows
|
|
120
|
+
.filter((source) => source.complimentary_full === true)
|
|
121
|
+
.reduce((sum, source) => sum + source.order_count, 0);
|
|
122
|
+
const complimentaryPartialCount = complimentaryRows
|
|
123
|
+
.filter((source) => source.complimentary_full === false)
|
|
124
|
+
.reduce((sum, source) => sum + source.order_count, 0);
|
|
125
|
+
const complimentaryUnknownCount = complimentaryRows
|
|
126
|
+
.filter((source) => source.complimentary_full == null)
|
|
127
|
+
.reduce((sum, source) => sum + source.order_count, 0);
|
|
128
|
+
return {
|
|
129
|
+
tenant_ids: [...new Set(reports.map((report) => report.tenant_id))],
|
|
130
|
+
sources,
|
|
131
|
+
complimentary,
|
|
132
|
+
totals: {
|
|
133
|
+
discount_amount: round2(toDecimal(totalCents)),
|
|
134
|
+
source_count: discountRows.length,
|
|
135
|
+
order_count: orderCount,
|
|
136
|
+
complimentary_covered_amount: round2(toDecimal(complimentaryRows.reduce((sum, source) => sum + (source.covered_amount ?? 0), 0))),
|
|
137
|
+
complimentary_full_count: complimentaryFullCount,
|
|
138
|
+
complimentary_partial_count: complimentaryPartialCount,
|
|
139
|
+
complimentary_unknown_count: complimentaryUnknownCount,
|
|
140
|
+
},
|
|
141
|
+
top_source: sources.length > 0 ? sources[0] : null,
|
|
142
|
+
has_data: discountRows.length > 0 || complimentaryRows.length > 0,
|
|
143
|
+
};
|
|
144
|
+
}
|