@managesome/eposmatic-toolkit 0.4.63 → 0.4.65

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 (55) hide show
  1. package/esm/constants/MATERIALIZED_SCHEMA_VERSIONS.js +3 -0
  2. package/esm/helpers/materialized-reports/index.js +1 -1
  3. package/esm/helpers/materialized-reports/materialized-cash-compile-helpers.js +1 -2
  4. package/esm/helpers/materialized-reports/{materialized-discount-source-helpers.js → materialized-discount-source-assembly.js} +13 -3
  5. package/esm/helpers/materialized-reports/report-compile.js +2 -0
  6. package/esm/helpers/store/common-tenant-purchase-order-actions.js +12 -0
  7. package/esm/helpers/store/common-warehouse-purchase-order-actions.js +20 -0
  8. package/esm/helpers/store/tenant-purchase-orders-cart-actions.js +24 -7
  9. package/esm/helpers/store/warehouse-purchase-orders-cart-actions.js +23 -10
  10. package/esm/helpers/utils/index.js +1 -0
  11. package/esm/helpers/utils/tenant-purchase-order-engine.js +47 -0
  12. package/esm/helpers/utils/warehouse-purchase-order-engine.js +63 -4
  13. package/esm/schemas/models/materialized_tenant_cash_entries.js +1 -1
  14. package/esm/schemas/models/materialized_tenant_cash_reports.js +1 -1
  15. package/esm/schemas/models/materialized_tenant_discount_sources.js +1 -1
  16. package/esm/schemas/models/tenant_purchase_orders.js +4 -0
  17. package/esm/schemas/models/warehouse_purchase_orders.js +4 -0
  18. package/lib/constants/MATERIALIZED_SCHEMA_VERSIONS.d.ts +3 -0
  19. package/lib/constants/MATERIALIZED_SCHEMA_VERSIONS.js +4 -1
  20. package/lib/helpers/materialized-reports/index.d.ts +1 -1
  21. package/lib/helpers/materialized-reports/index.js +1 -1
  22. package/lib/helpers/materialized-reports/materialized-cash-compile-helpers.js +3 -4
  23. package/lib/helpers/materialized-reports/{materialized-discount-source-helpers.js → materialized-discount-source-assembly.js} +14 -4
  24. package/lib/helpers/materialized-reports/report-compile.d.ts +2 -0
  25. package/lib/helpers/materialized-reports/report-compile.js +5 -1
  26. package/lib/helpers/store/common-tenant-purchase-order-actions.d.ts +6 -1
  27. package/lib/helpers/store/common-tenant-purchase-order-actions.js +13 -0
  28. package/lib/helpers/store/common-warehouse-purchase-order-actions.js +20 -0
  29. package/lib/helpers/store/tenant-purchase-orders-cart-actions.js +23 -6
  30. package/lib/helpers/store/warehouse-purchase-orders-cart-actions.js +23 -10
  31. package/lib/helpers/utils/index.d.ts +1 -0
  32. package/lib/helpers/utils/index.js +1 -0
  33. package/lib/helpers/utils/tenant-purchase-order-engine.d.ts +24 -0
  34. package/lib/helpers/utils/tenant-purchase-order-engine.js +51 -0
  35. package/lib/helpers/utils/warehouse-purchase-order-engine.d.ts +23 -1
  36. package/lib/helpers/utils/warehouse-purchase-order-engine.js +65 -4
  37. package/lib/schemas/models/materialized_tenant_cash_entries.d.ts +0 -13
  38. package/lib/schemas/models/materialized_tenant_cash_entries.js +3 -3
  39. package/lib/schemas/models/materialized_tenant_cash_reports.d.ts +0 -20
  40. package/lib/schemas/models/materialized_tenant_cash_reports.js +3 -3
  41. package/lib/schemas/models/materialized_tenant_discount_sources.d.ts +0 -19
  42. package/lib/schemas/models/materialized_tenant_discount_sources.js +3 -3
  43. package/lib/schemas/models/tenant_purchase_orders.d.ts +972 -0
  44. package/lib/schemas/models/tenant_purchase_orders.js +4 -0
  45. package/lib/schemas/models/warehouse_purchase_orders.d.ts +972 -0
  46. package/lib/schemas/models/warehouse_purchase_orders.js +4 -0
  47. package/lib/schemas/types/tenant_purchase_orders.d.ts +2 -1
  48. package/lib/schemas/types/warehouse_purchase_orders.d.ts +2 -1
  49. package/lib/tests/materialized-reports/materialized-discount-scenarios.test.js +2 -2
  50. package/lib/tests/materialized-reports/materialized-discount-sources.test.js +41 -41
  51. package/lib/tests/materialized-reports/materialized-integer-contract.test.js +2 -2
  52. package/lib/tests/materialized-reports/real-discount-orders.test.js +2 -2
  53. package/lib/tests/materialized-reports/report-platform/registry-conformance.test.js +2 -1
  54. package/package.json +1 -1
  55. /package/lib/helpers/materialized-reports/{materialized-discount-source-helpers.d.ts → materialized-discount-source-assembly.d.ts} +0 -0
@@ -14,7 +14,7 @@
14
14
  * a worker.
15
15
  */
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.normalizeOverviewAggregatedShape = exports.resolveCreditBillDay = exports.getMaterializedCreditBillScopeRefsForIssue = exports.getMaterializedCreditBillScopeRefsForDay = exports.computeMaterializedCreditBillTotalsFromReportsByKey = exports.buildMaterializedCreditBillSelectionFromScopeGroups = exports.applyCreditResolutionToReportDocuments = exports.buildTenantSalesOverviewCloseDayPlan = exports.sumMaterializedTenantSalesOverviewReports = exports.convertOrdersToMaterializedTenantSalesOverviewReports = exports.buildReportResponse = exports.buildReportMeta = exports.buildReportDetailsResponse = exports.REPORT_MODE_ENUM = exports.REPORT_KEY_ENUM = exports.parseMaterializedLocalDate = exports.getMaterializedPeriodScopeFields = exports.mergeMaterializedDineinEntryPages = exports.dineinKeyPassesFilters = exports.pruneDocumentDineinMaps = exports.foldMaterializedDineinPeriodCounters = exports.buildTenantDineinCloseDayPlan = exports.compileTenantDineinReports = exports.buildTenantDialerCloseDayPlan = exports.compileTenantDialerReports = exports.compileTenantDeliveryGeoCellsWithStats = exports.buildTenantDeliveryCloseDayPlan = exports.compileTenantDeliveryReports = exports.buildTenantDiscountCloseDayPlan = exports.compileTenantDiscountReports = exports.getMaterializedPrepTimeReportPeriodDayScopeFields = exports.getMaterializedPrepTimePeriodDayScopeFields = exports.buildTenantPrepTimeCloseDayPlan = exports.compileTenantPrepTimeReports = exports.REPORT_SALE_TYPE_ENUM = exports.SALES_OVERVIEW_KEY_FILTERS = exports.MATERIALIZED_TENANT_DINEIN_REPORTS_SCHEMA_VERSION = exports.MATERIALIZED_TENANT_DINEIN_ENTRIES_SCHEMA_VERSION = exports.MATERIALIZED_CREDIT_BILLS_SCHEMA_VERSION = exports.MATERIALIZED_REPORT_SCOPE_ENUM = exports.MATERIALIZED_PREP_TIME_REPORT_SCOPES = exports.MATERIALIZED_PREP_TIME_ITEM_SCOPES = exports.MATERIALIZED_DINEIN_UNKNOWN_TOKEN = exports.MATERIALIZED_DINEIN_SPLIT_PARENT_DELETE_REASON = exports.MATERIALIZED_DINEIN_REPORT_SCOPES = exports.MATERIALIZED_DINEIN_ORIGIN_ENUM = void 0;
17
+ exports.normalizeOverviewAggregatedShape = exports.resolveCreditBillDay = exports.getMaterializedCreditBillScopeRefsForIssue = exports.getMaterializedCreditBillScopeRefsForDay = exports.computeMaterializedCreditBillTotalsFromReportsByKey = exports.buildMaterializedCreditBillSelectionFromScopeGroups = exports.applyCreditResolutionToReportDocuments = exports.buildTenantSalesOverviewCloseDayPlan = exports.sumMaterializedTenantSalesOverviewReports = exports.convertOrdersToMaterializedTenantSalesOverviewReports = exports.buildReportResponse = exports.buildReportMeta = exports.buildReportDetailsResponse = exports.REPORT_MODE_ENUM = exports.REPORT_KEY_ENUM = exports.parseMaterializedLocalDate = exports.getMaterializedPeriodScopeFields = exports.mergeMaterializedDineinEntryPages = exports.dineinKeyPassesFilters = exports.pruneDocumentDineinMaps = exports.foldMaterializedDineinPeriodCounters = exports.buildTenantDineinCloseDayPlan = exports.compileTenantDineinReports = exports.buildTenantDialerCloseDayPlan = exports.compileTenantDialerReports = exports.compileTenantDeliveryGeoCellsWithStats = exports.buildTenantDeliveryCloseDayPlan = exports.compileTenantDeliveryReports = exports.compileTenantCashReports = exports.buildDiscountSourcesForScope = exports.buildTenantDiscountCloseDayPlan = exports.compileTenantDiscountReports = exports.getMaterializedPrepTimeReportPeriodDayScopeFields = exports.getMaterializedPrepTimePeriodDayScopeFields = exports.buildTenantPrepTimeCloseDayPlan = exports.compileTenantPrepTimeReports = exports.REPORT_SALE_TYPE_ENUM = exports.SALES_OVERVIEW_KEY_FILTERS = exports.MATERIALIZED_TENANT_DINEIN_REPORTS_SCHEMA_VERSION = exports.MATERIALIZED_TENANT_DINEIN_ENTRIES_SCHEMA_VERSION = exports.MATERIALIZED_CREDIT_BILLS_SCHEMA_VERSION = exports.MATERIALIZED_REPORT_SCOPE_ENUM = exports.MATERIALIZED_PREP_TIME_REPORT_SCOPES = exports.MATERIALIZED_PREP_TIME_ITEM_SCOPES = exports.MATERIALIZED_DINEIN_UNKNOWN_TOKEN = exports.MATERIALIZED_DINEIN_SPLIT_PARENT_DELETE_REASON = exports.MATERIALIZED_DINEIN_REPORT_SCOPES = exports.MATERIALIZED_DINEIN_ORIGIN_ENUM = void 0;
18
18
  var MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
19
19
  Object.defineProperty(exports, "MATERIALIZED_DINEIN_ORIGIN_ENUM", { enumerable: true, get: function () { return MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_ORIGIN_ENUM; } });
20
20
  Object.defineProperty(exports, "MATERIALIZED_DINEIN_REPORT_SCOPES", { enumerable: true, get: function () { return MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_REPORT_SCOPES; } });
@@ -41,6 +41,10 @@ var materialized_discount_compile_helpers_1 = require("./materialized-discount-c
41
41
  Object.defineProperty(exports, "compileTenantDiscountReports", { enumerable: true, get: function () { return materialized_discount_compile_helpers_1.compileTenantDiscountReports; } });
42
42
  var materialized_discount_close_day_helpers_1 = require("./materialized-discount-close-day-helpers");
43
43
  Object.defineProperty(exports, "buildTenantDiscountCloseDayPlan", { enumerable: true, get: function () { return materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan; } });
44
+ var materialized_discount_source_assembly_1 = require("./materialized-discount-source-assembly");
45
+ Object.defineProperty(exports, "buildDiscountSourcesForScope", { enumerable: true, get: function () { return materialized_discount_source_assembly_1.buildDiscountSourcesForScope; } });
46
+ var materialized_cash_compile_helpers_1 = require("./materialized-cash-compile-helpers");
47
+ Object.defineProperty(exports, "compileTenantCashReports", { enumerable: true, get: function () { return materialized_cash_compile_helpers_1.compileTenantCashReports; } });
44
48
  var materialized_delivery_compile_helpers_1 = require("./materialized-delivery-compile-helpers");
45
49
  Object.defineProperty(exports, "compileTenantDeliveryReports", { enumerable: true, get: function () { return materialized_delivery_compile_helpers_1.compileTenantDeliveryReports; } });
46
50
  var materialized_delivery_close_day_helpers_1 = require("./materialized-delivery-close-day-helpers");
@@ -1,8 +1,13 @@
1
- import { IOrderPayment, ITenantPurchaseOrder, ITenantPurchaseOrderCategory, ITenantPurchaseOrderDiscountDetails, ITenantPurchaseOrderItem, ITenantPurchaseOrderIteration } from '../../schemas';
1
+ import { IDiscountDetails, IOrderPayment, ITenantPurchaseOrder, ITenantPurchaseOrderCategory, ITenantPurchaseOrderDiscountDetails, ITenantPurchaseOrderItem, ITenantPurchaseOrderIteration } from '../../schemas';
2
2
  import { SORT_TYPE_ENUM } from '../../constants';
3
3
  import { ITenantGeneralPurchaseOrderItem, ITenantPurchaseOrderCartItemsObj } from '../types/tenant-purchase-order-cart-actions';
4
4
  export declare function getTenantPurchaseOrderIterationsWithPrice(iterations?: ITenantPurchaseOrderIteration[] | undefined, writeable?: boolean): ITenantPurchaseOrderIteration[];
5
5
  export declare function getTenantPurchaseOrderPaymentsWithPrice(payments: IOrderPayment[] | undefined, convertToPrice: (price: number) => number): IOrderPayment[];
6
+ /**
7
+ * An item discount mixes money and percentages, so the shared order-discount
8
+ * conversion is applied: percentages are left untouched.
9
+ */
10
+ export declare function getTenantPurchaseOrderItemDiscountDetailsWithPrice(discount: IDiscountDetails, convertToPrice: (price: number) => number): IDiscountDetails;
6
11
  export declare function getTenantPurchaseOrderItemsWithPrice(items: ITenantPurchaseOrderItem[] | undefined, convertToPrice: (price: number) => number): ITenantPurchaseOrderItem[];
7
12
  export declare function getTenantPurchaseOrderCategoriesWithPrice(categories: ITenantPurchaseOrderCategory[] | undefined, convertToPrice: (price: number) => number): ITenantPurchaseOrderCategory[];
8
13
  export declare function getTenantPurchaseOrderDiscountDetailsWithPrice(discount: ITenantPurchaseOrderDiscountDetails | null | undefined, convertToPrice: (price: number) => number): ITenantPurchaseOrderDiscountDetails | null;
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.getTenantPurchaseOrderIterationsWithPrice = getTenantPurchaseOrderIterationsWithPrice;
7
7
  exports.getTenantPurchaseOrderPaymentsWithPrice = getTenantPurchaseOrderPaymentsWithPrice;
8
+ exports.getTenantPurchaseOrderItemDiscountDetailsWithPrice = getTenantPurchaseOrderItemDiscountDetailsWithPrice;
8
9
  exports.getTenantPurchaseOrderItemsWithPrice = getTenantPurchaseOrderItemsWithPrice;
9
10
  exports.getTenantPurchaseOrderCategoriesWithPrice = getTenantPurchaseOrderCategoriesWithPrice;
10
11
  exports.getTenantPurchaseOrderDiscountDetailsWithPrice = getTenantPurchaseOrderDiscountDetailsWithPrice;
@@ -40,6 +41,13 @@ function getTenantPurchaseOrderPaymentsWithPrice(payments, convertToPrice) {
40
41
  amount_refunded: convertToPrice(payment.amount_refunded),
41
42
  }));
42
43
  }
44
+ /**
45
+ * An item discount mixes money and percentages, so the shared order-discount
46
+ * conversion is applied: percentages are left untouched.
47
+ */
48
+ function getTenantPurchaseOrderItemDiscountDetailsWithPrice(discount, convertToPrice) {
49
+ return getTenantPurchaseOrderDiscountDetailsWithPrice(discount, convertToPrice);
50
+ }
43
51
  function getTenantPurchaseOrderItemsWithPrice(items, convertToPrice) {
44
52
  if (!items)
45
53
  return [];
@@ -48,6 +56,11 @@ function getTenantPurchaseOrderItemsWithPrice(items, convertToPrice) {
48
56
  quantity: (0, utils_1.parseNum)(item.quantity || 1, true),
49
57
  price: convertToPrice(item.price ?? 0),
50
58
  subtotal: convertToPrice(item.subtotal ?? 0),
59
+ ...(item.item_discount_details != null
60
+ ? {
61
+ item_discount_details: getTenantPurchaseOrderItemDiscountDetailsWithPrice(item.item_discount_details, convertToPrice),
62
+ }
63
+ : {}),
51
64
  _gst_amount: convertToPrice(item._gst_amount ?? 0),
52
65
  _net_amount: convertToPrice(item._net_amount ?? 0),
53
66
  _gross_amount: convertToPrice(item._gross_amount ?? 0),
@@ -44,12 +44,32 @@ function getWarehousePurchaseOrderItemsWithPrice(items, convertToPrice) {
44
44
  quantity: (0, utils_1.parseNum)(item.quantity || 1, true),
45
45
  price: convertToPrice(item.price ?? 0),
46
46
  subtotal: convertToPrice(item.subtotal ?? 0),
47
+ // A percentage is not a price, so only fixed discounts are converted.
48
+ ...(item.item_discount_details != null
49
+ ? {
50
+ item_discount_details: getWarehouseItemDiscountDetailsWithPrice(item.item_discount_details, convertToPrice),
51
+ }
52
+ : {}),
47
53
  _gst_amount: convertToPrice(item._gst_amount ?? 0),
48
54
  _net_amount: convertToPrice(item._net_amount ?? 0),
49
55
  _gross_amount: convertToPrice(item._gross_amount ?? 0),
50
56
  _discount_amount: convertToPrice(item._discount_amount ?? 0),
51
57
  }));
52
58
  }
59
+ /**
60
+ * Same conversion as the order-level discount details, but a percentage is left
61
+ * untouched — only money values are scaled.
62
+ */
63
+ function getWarehouseItemDiscountDetailsWithPrice(discount, convertToPrice) {
64
+ const { special_discount, is_special_discount_percentage, total_discount } = discount;
65
+ return {
66
+ ...discount,
67
+ ...(typeof special_discount === 'number' && !is_special_discount_percentage
68
+ ? { special_discount: convertToPrice(special_discount) }
69
+ : {}),
70
+ total_discount: convertToPrice(total_discount ?? 0),
71
+ };
72
+ }
53
73
  function getWarehousePurchaseOrderCategoriesWithPrice(categories, convertToPrice) {
54
74
  if (!categories)
55
75
  return [];
@@ -56,7 +56,13 @@ function getTenantPurchaseOrderOrderDiscountDetails(discountDetails, baseAmount)
56
56
  }
57
57
  function calculateCartTenantPurchaseOrderInfoAndSyncData(data, calculateItemMetadata) {
58
58
  const isCompleted = data.order_status === constants_1.TENANT_PURCHASE_ORDER_STATUS_ENUM.COMPLETED;
59
+ /* ===============================><=============================== */
60
+ // `grossSubtotal` is the list value (price × quantity) of every line;
61
+ // `subtotal` is what the order actually costs after item-level discounts.
62
+ // Order-level discounts are then applied on top of `subtotal`.
63
+ let grossSubtotal = 0;
59
64
  let subtotal = 0;
65
+ let totalItemDiscount = 0;
60
66
  const categories = [];
61
67
  const length = data.categories?.length ?? 0;
62
68
  for (let c = 0; c < length; c++) {
@@ -65,11 +71,16 @@ function calculateCartTenantPurchaseOrderInfoAndSyncData(data, calculateItemMeta
65
71
  const itemsLength = category.items.length;
66
72
  for (let i = 0; i < itemsLength; i++) {
67
73
  const item = category.items[i];
68
- const itemSubtotal = (0, utils_1.parseNum)(item.price * item.quantity);
74
+ // Recalculate from price × quantity so edits to price/quantity are always
75
+ // reflected, then take the item-level discount off the line.
76
+ const itemGrossSubtotal = (0, utils_1.parseNum)(item.price * item.quantity);
77
+ const itemDiscountAmount = (0, utils_1.getTenantPurchaseOrderItemDiscountAmount)(item);
78
+ const itemSubtotal = (0, utils_1.parseNum)(Math.max(0, itemGrossSubtotal - itemDiscountAmount));
69
79
  const newItem = {
70
80
  ...item,
71
81
  is_ready: item.is_ready || isCompleted,
72
82
  subtotal: itemSubtotal,
83
+ item_discount_details: (0, utils_1.withResolvedTenantItemDiscountTotal)(item),
73
84
  _discount_amount: 0,
74
85
  _gross_amount: 0,
75
86
  _gst_amount: 0,
@@ -77,7 +88,9 @@ function calculateCartTenantPurchaseOrderInfoAndSyncData(data, calculateItemMeta
77
88
  };
78
89
  delete newItem.void_reason;
79
90
  newItems.push(newItem);
91
+ grossSubtotal += itemGrossSubtotal;
80
92
  subtotal += itemSubtotal;
93
+ totalItemDiscount += itemDiscountAmount;
81
94
  }
82
95
  categories.push({
83
96
  ...category,
@@ -109,20 +122,24 @@ function calculateCartTenantPurchaseOrderInfoAndSyncData(data, calculateItemMeta
109
122
  const amount_earned = (0, utils_1.parseNum)(grand_total - cardCharges);
110
123
  if (calculateItemMetadata) {
111
124
  const discountedSubtotal = (0, utils_1.parseNum)(Math.max(0, subtotal - total_discount_applied));
112
- let leftGrossAmount = subtotal;
125
+ let leftGrossAmount = grossSubtotal;
113
126
  let leftNetAmount = discountedSubtotal;
114
- let leftDiscountAmount = total_discount_applied;
127
+ let leftDiscountAmount = (0, utils_1.parseNum)(totalItemDiscount + total_discount_applied);
115
128
  let leftGstAmount = gst_amount;
116
129
  categories.forEach((category, categoryIndex) => {
117
130
  const isLastCategory = categoryIndex === categories.length - 1;
118
131
  category.items.forEach((item, itemIndex) => {
119
132
  const isLastItem = isLastCategory && itemIndex === category.items.length - 1;
133
+ // `item.subtotal` is already net of the item-level discount, so the
134
+ // order-level discount is distributed over the discounted subtotal.
120
135
  const itemOrderDiscountAmount = subtotal > 0 ? (item.subtotal / subtotal) * total_discount_applied : 0;
121
- let grossAmount = item.subtotal;
136
+ // Whatever the line lost to its own item-level discount.
137
+ const itemLevelDiscountAmount = (0, utils_1.parseNum)(Math.max(0, item.price * item.quantity - item.subtotal));
138
+ let grossAmount = item.price * item.quantity;
122
139
  let netAmount = item.subtotal - itemOrderDiscountAmount;
123
- let itemTotalDiscountAmount = itemOrderDiscountAmount;
140
+ let itemTotalDiscountAmount = itemLevelDiscountAmount + itemOrderDiscountAmount;
124
141
  let itemGst = 0;
125
- if (gst_amount > 0) {
142
+ if (gst_amount > 0 && subtotal > 0) {
126
143
  itemGst = (item.subtotal / subtotal) * gst_amount;
127
144
  }
128
145
  grossAmount = (0, utils_1.parseFloatCeil)(grossAmount);
@@ -18,6 +18,7 @@ const bson_objectid_1 = __importDefault(require("bson-objectid"));
18
18
  const isEqual_1 = __importDefault(require("lodash/isEqual"));
19
19
  const constants_1 = require("../../constants");
20
20
  const utils_1 = require("../utils");
21
+ const warehouse_purchase_order_engine_1 = require("../utils/warehouse-purchase-order-engine");
21
22
  function getWarehousePurchaseOrderOrderDiscountDetails(discountDetails, baseAmount) {
22
23
  if (!discountDetails) {
23
24
  return { order_discount_details: null, total_discount_applied: 0 };
@@ -50,7 +51,12 @@ function getWarehousePurchaseOrderOrderDiscountDetails(discountDetails, baseAmou
50
51
  function calculateCartWarehousePurchaseOrderInfoAndSyncData(data, calculateItemMetadata) {
51
52
  const isCompleted = data.order_status === constants_1.WAREHOUSE_PURCHASE_ORDER_STATUS_ENUM.COMPLETED;
52
53
  /* ===============================><=============================== */
54
+ // `grossSubtotal` is the list value (price × quantity) of every line;
55
+ // `subtotal` is what the order actually costs after item-level discounts.
56
+ // Order-level discounts are then applied on top of `subtotal`.
57
+ let grossSubtotal = 0;
53
58
  let subtotal = 0;
59
+ let totalItemDiscount = 0;
54
60
  const categories = [];
55
61
  const length = data.categories?.length ?? 0;
56
62
  for (let c = 0; c < length; c++) {
@@ -59,13 +65,16 @@ function calculateCartWarehousePurchaseOrderInfoAndSyncData(data, calculateItemM
59
65
  const itemsLength = category.items.length;
60
66
  for (let i = 0; i < itemsLength; i++) {
61
67
  const item = category.items[i];
62
- // For purchase orders, subtotal is already set as price * quantity
63
- // But to ensure, we can recalculate
64
- const itemSubtotal = (0, utils_1.parseNum)(item.price * item.quantity);
68
+ // Recalculate from price × quantity so edits to price/quantity are always
69
+ // reflected, then take the item-level discount off the line.
70
+ const itemGrossSubtotal = (0, utils_1.parseNum)(item.price * item.quantity);
71
+ const itemDiscountAmount = (0, warehouse_purchase_order_engine_1.getWarehousePurchaseOrderItemDiscountAmount)(item);
72
+ const itemSubtotal = (0, utils_1.parseNum)(Math.max(0, itemGrossSubtotal - itemDiscountAmount));
65
73
  const newItem = {
66
74
  ...item,
67
75
  is_ready: item.is_ready || isCompleted,
68
76
  subtotal: itemSubtotal,
77
+ item_discount_details: (0, warehouse_purchase_order_engine_1.withResolvedWarehouseItemDiscountTotal)(item),
69
78
  // These values will be auto calculated
70
79
  _discount_amount: 0,
71
80
  _gross_amount: 0,
@@ -74,7 +83,9 @@ function calculateCartWarehousePurchaseOrderInfoAndSyncData(data, calculateItemM
74
83
  };
75
84
  delete newItem.void_reason;
76
85
  newItems.push(newItem);
86
+ grossSubtotal += itemGrossSubtotal;
77
87
  subtotal += itemSubtotal;
88
+ totalItemDiscount += itemDiscountAmount;
78
89
  }
79
90
  categories.push({
80
91
  ...category,
@@ -114,22 +125,24 @@ function calculateCartWarehousePurchaseOrderInfoAndSyncData(data, calculateItemM
114
125
  const amount_earned = (0, utils_1.parseNum)(grand_total - cardCharges);
115
126
  if (calculateItemMetadata) {
116
127
  const discountedSubtotal = (0, utils_1.parseNum)(Math.max(0, subtotal - total_discount_applied));
117
- let leftGrossAmount = subtotal;
128
+ let leftGrossAmount = grossSubtotal;
118
129
  let leftNetAmount = discountedSubtotal;
119
- let leftDiscountAmount = total_discount_applied;
130
+ let leftDiscountAmount = (0, utils_1.parseNum)(totalItemDiscount + total_discount_applied);
120
131
  let leftGstAmount = gst_amount;
121
132
  categories.forEach((category, categoryIndex) => {
122
133
  const isLastCategory = categoryIndex === categories.length - 1;
123
134
  category.items.forEach((item, itemIndex) => {
124
135
  const isLastItem = isLastCategory && itemIndex === category.items.length - 1;
125
- // Order-level discounts are distributed proportionally across items,
126
- // mirroring how `IOrder` calculates item metadata.
136
+ // `item.subtotal` is already net of the item-level discount, so the
137
+ // order-level discount is distributed over the discounted subtotal.
127
138
  const itemOrderDiscountAmount = subtotal > 0 ? (item.subtotal / subtotal) * total_discount_applied : 0;
128
- let grossAmount = item.subtotal;
139
+ // Whatever the line lost to its own item-level discount.
140
+ const itemLevelDiscountAmount = (0, utils_1.parseNum)(Math.max(0, item.price * item.quantity - item.subtotal));
141
+ let grossAmount = item.price * item.quantity;
129
142
  let netAmount = item.subtotal - itemOrderDiscountAmount;
130
- let itemTotalDiscountAmount = itemOrderDiscountAmount;
143
+ let itemTotalDiscountAmount = itemLevelDiscountAmount + itemOrderDiscountAmount;
131
144
  let itemGst = 0;
132
- if (gst_amount > 0) {
145
+ if (gst_amount > 0 && subtotal > 0) {
133
146
  itemGst = (item.subtotal / subtotal) * gst_amount;
134
147
  }
135
148
  grossAmount = (0, utils_1.parseFloatCeil)(grossAmount);
@@ -77,6 +77,7 @@ export * from './tenant-cash-drawers-helpers';
77
77
  export * from './tenant-cash-in-out-helpers';
78
78
  export * from './tenant-day-sessions-helpers';
79
79
  export * from './tenant-instore-shift-sessions-helpers';
80
+ export * from './tenant-purchase-order-engine';
80
81
  export * from './tenant-staff-helpers';
81
82
  export * from './tenant_cash_in_hand_transactions';
82
83
  export * from './time-helpers';
@@ -93,6 +93,7 @@ __exportStar(require("./tenant-cash-drawers-helpers"), exports);
93
93
  __exportStar(require("./tenant-cash-in-out-helpers"), exports);
94
94
  __exportStar(require("./tenant-day-sessions-helpers"), exports);
95
95
  __exportStar(require("./tenant-instore-shift-sessions-helpers"), exports);
96
+ __exportStar(require("./tenant-purchase-order-engine"), exports);
96
97
  __exportStar(require("./tenant-staff-helpers"), exports);
97
98
  __exportStar(require("./tenant_cash_in_hand_transactions"), exports);
98
99
  __exportStar(require("./time-helpers"), exports);
@@ -0,0 +1,24 @@
1
+ import { IDiscountDetails, ITenantPurchaseOrderItem } from '../../schemas';
2
+ /**
3
+ * Total item-level discount for a tenant purchase order line, in money.
4
+ *
5
+ * The discount is applied per cart line through the cart's "Item Discount"
6
+ * action and lives in `item_discount_details` — the same record an order item
7
+ * uses:
8
+ * - `special_discount` is a **per-unit** amount when fixed, so the discount
9
+ * scales with quantity, and a percentage of the line's gross when
10
+ * `is_special_discount_percentage` is set.
11
+ * - `total_discount` is a fallback for records written without a
12
+ * `special_discount`.
13
+ *
14
+ * The result is clamped to the line's gross value so a discount can never make
15
+ * a line negative.
16
+ */
17
+ export declare function getTenantPurchaseOrderItemDiscountAmount(orderItem: Pick<ITenantPurchaseOrderItem, 'price' | 'quantity' | 'item_discount_details'>): number;
18
+ /**
19
+ * Refreshes `total_discount` on a line's discount record so the amount it
20
+ * reports matches the line's current price/quantity. Preserves every other
21
+ * field (id, name, reason, reference) and returns null when there is no
22
+ * discount.
23
+ */
24
+ export declare function withResolvedTenantItemDiscountTotal(orderItem: Pick<ITenantPurchaseOrderItem, 'price' | 'quantity' | 'item_discount_details'>): IDiscountDetails | null;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getTenantPurchaseOrderItemDiscountAmount = getTenantPurchaseOrderItemDiscountAmount;
4
+ exports.withResolvedTenantItemDiscountTotal = withResolvedTenantItemDiscountTotal;
5
+ const common_1 = require("./common");
6
+ // ==========================================================><====================================================
7
+ /**
8
+ * Total item-level discount for a tenant purchase order line, in money.
9
+ *
10
+ * The discount is applied per cart line through the cart's "Item Discount"
11
+ * action and lives in `item_discount_details` — the same record an order item
12
+ * uses:
13
+ * - `special_discount` is a **per-unit** amount when fixed, so the discount
14
+ * scales with quantity, and a percentage of the line's gross when
15
+ * `is_special_discount_percentage` is set.
16
+ * - `total_discount` is a fallback for records written without a
17
+ * `special_discount`.
18
+ *
19
+ * The result is clamped to the line's gross value so a discount can never make
20
+ * a line negative.
21
+ */
22
+ function getTenantPurchaseOrderItemDiscountAmount(orderItem) {
23
+ const details = orderItem.item_discount_details;
24
+ if (!details)
25
+ return 0;
26
+ const quantity = orderItem.quantity ?? 0;
27
+ const grossAmount = (0, common_1.parseNum)((orderItem.price ?? 0) * quantity);
28
+ const value = details.special_discount;
29
+ if (typeof value !== 'number' || !(value > 0)) {
30
+ return (0, common_1.parseNum)(Math.max(0, Math.min(grossAmount, details.total_discount ?? 0)));
31
+ }
32
+ const discountAmount = details.is_special_discount_percentage
33
+ ? (0, common_1.getPercentageAmount)(grossAmount, value)
34
+ : value * quantity;
35
+ return (0, common_1.parseNum)(Math.min(grossAmount, Math.max(0, discountAmount)));
36
+ }
37
+ /**
38
+ * Refreshes `total_discount` on a line's discount record so the amount it
39
+ * reports matches the line's current price/quantity. Preserves every other
40
+ * field (id, name, reason, reference) and returns null when there is no
41
+ * discount.
42
+ */
43
+ function withResolvedTenantItemDiscountTotal(orderItem) {
44
+ const details = orderItem.item_discount_details;
45
+ if (!details)
46
+ return null;
47
+ return {
48
+ ...details,
49
+ total_discount: getTenantPurchaseOrderItemDiscountAmount(orderItem),
50
+ };
51
+ }
@@ -1,4 +1,26 @@
1
- import { IWarehousePurchaseOrderItem, IWarehouseItem } from '../../schemas';
1
+ import { IDiscountDetails, IWarehouseItem, IWarehousePurchaseOrderItem } from '../../schemas';
2
+ /**
3
+ * Total item-level discount for a purchase order line, in money.
4
+ *
5
+ * The line's discount lives in `item_discount_details` (the same record an order
6
+ * item uses):
7
+ * - `special_discount` is a **per-unit** amount when fixed, so the discount
8
+ * scales with quantity, and a percentage of the line's gross when
9
+ * `is_special_discount_percentage` is set.
10
+ * - `total_discount` is used as a fallback for records written without a
11
+ * `special_discount` (e.g. a discount carried over from elsewhere).
12
+ *
13
+ * The result is clamped to the line's gross value so a discount can never make a
14
+ * line negative.
15
+ */
16
+ export declare function getWarehousePurchaseOrderItemDiscountAmount(orderItem: Pick<IWarehousePurchaseOrderItem, 'price' | 'quantity' | 'item_discount_details'>): number;
17
+ /**
18
+ * Refreshes `total_discount` on a line's discount record so the amount it
19
+ * reports matches the line's current price/quantity. Preserves every other
20
+ * field (id, name, reason, reference) and returns null when there is no
21
+ * discount.
22
+ */
23
+ export declare function withResolvedWarehouseItemDiscountTotal(orderItem: Pick<IWarehousePurchaseOrderItem, 'price' | 'quantity' | 'item_discount_details'>): IDiscountDetails | null;
2
24
  interface IConvertWarehouseItemToPurchaseOrderItemParams {
3
25
  vendorId: string;
4
26
  item: IWarehouseItem;
@@ -3,20 +3,82 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getWarehousePurchaseOrderItemDiscountAmount = getWarehousePurchaseOrderItemDiscountAmount;
7
+ exports.withResolvedWarehouseItemDiscountTotal = withResolvedWarehouseItemDiscountTotal;
6
8
  exports.convertWarehouseItemToPurchaseOrderItem = convertWarehouseItemToPurchaseOrderItem;
7
9
  exports.getCurrentWarehousePurchaseOrderItemTotalPrice = getCurrentWarehousePurchaseOrderItemTotalPrice;
8
10
  const bson_objectid_1 = __importDefault(require("bson-objectid"));
11
+ const common_1 = require("./common");
12
+ // ==========================================================><====================================================
13
+ /**
14
+ * Total item-level discount for a purchase order line, in money.
15
+ *
16
+ * The line's discount lives in `item_discount_details` (the same record an order
17
+ * item uses):
18
+ * - `special_discount` is a **per-unit** amount when fixed, so the discount
19
+ * scales with quantity, and a percentage of the line's gross when
20
+ * `is_special_discount_percentage` is set.
21
+ * - `total_discount` is used as a fallback for records written without a
22
+ * `special_discount` (e.g. a discount carried over from elsewhere).
23
+ *
24
+ * The result is clamped to the line's gross value so a discount can never make a
25
+ * line negative.
26
+ */
27
+ function getWarehousePurchaseOrderItemDiscountAmount(orderItem) {
28
+ const details = orderItem.item_discount_details;
29
+ if (!details)
30
+ return 0;
31
+ const quantity = orderItem.quantity ?? 0;
32
+ const grossAmount = (0, common_1.parseNum)((orderItem.price ?? 0) * quantity);
33
+ const value = details.special_discount;
34
+ if (typeof value !== 'number' || !(value > 0)) {
35
+ return (0, common_1.parseNum)(Math.max(0, Math.min(grossAmount, details.total_discount ?? 0)));
36
+ }
37
+ const discountAmount = details.is_special_discount_percentage
38
+ ? (0, common_1.getPercentageAmount)(grossAmount, value)
39
+ : value * quantity;
40
+ return (0, common_1.parseNum)(Math.min(grossAmount, Math.max(0, discountAmount)));
41
+ }
42
+ /**
43
+ * Refreshes `total_discount` on a line's discount record so the amount it
44
+ * reports matches the line's current price/quantity. Preserves every other
45
+ * field (id, name, reason, reference) and returns null when there is no
46
+ * discount.
47
+ */
48
+ function withResolvedWarehouseItemDiscountTotal(orderItem) {
49
+ const details = orderItem.item_discount_details;
50
+ if (!details)
51
+ return null;
52
+ return {
53
+ ...details,
54
+ total_discount: getWarehousePurchaseOrderItemDiscountAmount(orderItem),
55
+ };
56
+ }
9
57
  function convertWarehouseItemToPurchaseOrderItem({ vendorId, item, orderItem, }) {
10
58
  const { _id = (0, bson_objectid_1.default)().toHexString(), price = item.purchase_price?.[vendorId] ?? item.purchase_price?.BASE_PRICE ?? 0, quantity = 1, special_instructions = '', is_ready, } = orderItem ?? {};
59
+ // Item discounts are applied per cart line; an existing order item (split /
60
+ // re-add) keeps the discount it was given.
61
+ const item_discount_details = orderItem?.item_discount_details ?? null;
62
+ const discount_amount = getWarehousePurchaseOrderItemDiscountAmount({
63
+ price,
64
+ quantity,
65
+ item_discount_details,
66
+ });
11
67
  const purchaseOrderItem = {
12
68
  _id,
13
69
  item_id: item._id,
14
70
  name: item.name,
15
71
  price,
16
- subtotal: price * quantity,
72
+ subtotal: (0, common_1.parseNum)(price * quantity - discount_amount),
17
73
  quantity,
18
74
  special_instructions,
19
75
  is_ready,
76
+ item_discount_details: item_discount_details == null
77
+ ? null
78
+ : {
79
+ ...item_discount_details,
80
+ total_discount: discount_amount,
81
+ },
20
82
  // These values will be auto calculated
21
83
  _discount_amount: 0,
22
84
  _gross_amount: 0,
@@ -27,8 +89,7 @@ function convertWarehouseItemToPurchaseOrderItem({ vendorId, item, orderItem, })
27
89
  }
28
90
  // ==========================================================><====================================================
29
91
  function getCurrentWarehousePurchaseOrderItemTotalPrice(orderItem) {
30
- // For purchase orders, total is simply price * quantity
31
- // No modifiers, discounts, etc.
32
- const total = orderItem.price * orderItem.quantity;
92
+ // The line's payable value: price × quantity minus any item-level discount.
93
+ const total = (0, common_1.parseNum)(orderItem.price * orderItem.quantity - getWarehousePurchaseOrderItemDiscountAmount(orderItem));
33
94
  return total;
34
95
  }
@@ -1,17 +1,4 @@
1
1
  import { Schema } from 'mongoose';
2
- /**
3
- * Mongoose schema for `materialized_tenant_cash_in_out_entries` - the STATEMENT
4
- * rows behind the cash report's drill-down (owner decision (e3): SPLIT).
5
- *
6
- * Scopes are capped at `LOCAL_MONTH`. Each row mirrors what the POS ledger already
7
- * renders (Detail / Before / signed Amount / After), and carries the four
8
- * lifecycle FLAGS computed at compile time so the read never re-derives them.
9
- */
10
- /**
11
- * Version of the cash ENTRY rules. Bump when row semantics change - historical
12
- * documents must then be re-materialized. PRE-RELEASE: folds into version `1`.
13
- */
14
- export declare const MATERIALIZED_TENANT_CASH_ENTRIES_SCHEMA_VERSION = 1;
15
2
  declare const CashEntrySchema: Schema<any, import("mongoose").Model<any, any, any, any, any, any>, {}, {}, {}, {}, {
16
3
  _id: false;
17
4
  }, {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MATERIALIZED_TENANT_CASH_ENTRIES_INDEX_OPTIONS = exports.MATERIALIZED_TENANT_CASH_ENTRIES_COLLECTION_INDEXES = exports.MATERIALIZED_TENANT_CASH_ENTRIES_COLLECTION_NAME = exports.MaterializedTenantCashEntriesSchema = exports.CashEntrySchema = exports.MATERIALIZED_TENANT_CASH_ENTRIES_SCHEMA_VERSION = void 0;
3
+ exports.MATERIALIZED_TENANT_CASH_ENTRIES_INDEX_OPTIONS = exports.MATERIALIZED_TENANT_CASH_ENTRIES_COLLECTION_INDEXES = exports.MATERIALIZED_TENANT_CASH_ENTRIES_COLLECTION_NAME = exports.MaterializedTenantCashEntriesSchema = exports.CashEntrySchema = void 0;
4
4
  const mongoose_1 = require("mongoose");
5
5
  const ENUMS_1 = require("../../constants/ENUMS");
6
6
  const cash_1 = require("../../constants/enums/cash");
@@ -17,7 +17,7 @@ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
17
17
  * Version of the cash ENTRY rules. Bump when row semantics change - historical
18
18
  * documents must then be re-materialized. PRE-RELEASE: folds into version `1`.
19
19
  */
20
- exports.MATERIALIZED_TENANT_CASH_ENTRIES_SCHEMA_VERSION = 1;
20
+ const MATERIALIZED_SCHEMA_VERSIONS_1 = require("../../constants/MATERIALIZED_SCHEMA_VERSIONS");
21
21
  const CashEntrySchema = new mongoose_1.Schema({
22
22
  cash_in_out_id: { type: String, required: true },
23
23
  entry_key: { type: String, required: true },
@@ -67,7 +67,7 @@ exports.MaterializedTenantCashEntriesSchema = new mongoose_1.Schema({
67
67
  type: Number,
68
68
  required: true,
69
69
  immutable: true,
70
- default: exports.MATERIALIZED_TENANT_CASH_ENTRIES_SCHEMA_VERSION,
70
+ default: MATERIALIZED_SCHEMA_VERSIONS_1.MATERIALIZED_TENANT_CASH_ENTRIES_SCHEMA_VERSION,
71
71
  },
72
72
  source_watermark: { type: mongoose_1.Schema.Types.Date, required: true },
73
73
  computed_at: { type: mongoose_1.Schema.Types.Date, required: true },
@@ -1,24 +1,4 @@
1
1
  import { Schema } from 'mongoose';
2
- /**
3
- * Mongoose schema for `materialized_tenant_cash_in_out_reports` (M3).
4
- *
5
- * Counters ONLY - the statement rows live in the SIBLING entries collection
6
- * (`materialized_tenant_cash_in_out_entries`, decision (e3)).
7
- *
8
- * The counters are stored TWICE on purpose: a key-addressed MAP (`cash_counters`, the
9
- * store + formatter shape) and the SAME counters as an ARRAY OF ROWS
10
- * (`cash_counter_rows`, the READ shape), so the read aggregation is the canonical
11
- * unwind/group/direct-sum pattern instead of dynamic-key navigation.
12
- *
13
- * Key grammar is `<DRAWER_ID>:<REF_TYPE>` (decision (b3)) - cash has neither an
14
- * order status nor a platform.
15
- */
16
- /**
17
- * Version of the cash COUNTER rules. Bump when counter semantics change -
18
- * historical documents must then be re-materialized. PRE-RELEASE: folds into
19
- * version `1`.
20
- */
21
- export declare const MATERIALIZED_TENANT_CASH_REPORTS_SCHEMA_VERSION = 1;
22
2
  export declare const MaterializedTenantCashReportSchema: Schema<any, import("mongoose").Model<any, any, any, any, any, any>, {}, {}, {}, {}, {
23
3
  timestamps: true;
24
4
  versionKey: false;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MATERIALIZED_TENANT_CASH_REPORTS_INDEX_OPTIONS = exports.MATERIALIZED_TENANT_CASH_REPORTS_COLLECTION_INDEXES = exports.MATERIALIZED_TENANT_CASH_REPORTS_COLLECTION_NAME = exports.MaterializedTenantCashReportSchema = exports.MATERIALIZED_TENANT_CASH_REPORTS_SCHEMA_VERSION = void 0;
3
+ exports.MATERIALIZED_TENANT_CASH_REPORTS_INDEX_OPTIONS = exports.MATERIALIZED_TENANT_CASH_REPORTS_COLLECTION_INDEXES = exports.MATERIALIZED_TENANT_CASH_REPORTS_COLLECTION_NAME = exports.MaterializedTenantCashReportSchema = void 0;
4
4
  const mongoose_1 = require("mongoose");
5
5
  const ENUMS_1 = require("../../constants/ENUMS");
6
6
  const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
@@ -23,7 +23,7 @@ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
23
23
  * historical documents must then be re-materialized. PRE-RELEASE: folds into
24
24
  * version `1`.
25
25
  */
26
- exports.MATERIALIZED_TENANT_CASH_REPORTS_SCHEMA_VERSION = 1;
26
+ const MATERIALIZED_SCHEMA_VERSIONS_1 = require("../../constants/MATERIALIZED_SCHEMA_VERSIONS");
27
27
  exports.MaterializedTenantCashReportSchema = new mongoose_1.Schema({
28
28
  tenant_id: { type: String, required: true },
29
29
  brand_id: { type: String, required: true },
@@ -49,7 +49,7 @@ exports.MaterializedTenantCashReportSchema = new mongoose_1.Schema({
49
49
  type: Number,
50
50
  required: true,
51
51
  immutable: true,
52
- default: exports.MATERIALIZED_TENANT_CASH_REPORTS_SCHEMA_VERSION,
52
+ default: MATERIALIZED_SCHEMA_VERSIONS_1.MATERIALIZED_TENANT_CASH_REPORTS_SCHEMA_VERSION,
53
53
  },
54
54
  source_watermark: { type: mongoose_1.Schema.Types.Date, required: true },
55
55
  computed_at: { type: mongoose_1.Schema.Types.Date, required: true },
@@ -1,23 +1,4 @@
1
1
  import { Schema } from 'mongoose';
2
- /**
3
- * Mongoose schema for `materialized_tenant_discount_sources` — the DERIVED
4
- * "by discount" projection of the discounts report (see
5
- * `schemas/types/materialized_tenant_discount_sources.ts`).
6
- *
7
- * One document per (tenant, scope context, scope field, source key, level).
8
- * The top-level metrics are the sum of `key_metrics`, which carries the
9
- * `ORDER_STATUS:PLATFORM` split so the report filters stay exact.
10
- *
11
- * This collection is REBUILT from the post-plan entry documents in the close
12
- * transaction. It has no iteration history of its own: the parent entry
13
- * documents are the audit trail.
14
- */
15
- /**
16
- * Version of the source-projection rules. Bump when the source identity,
17
- * grouping or rate rule changes — historical documents must then be rebuilt
18
- * with the backfill script.
19
- */
20
- export declare const MATERIALIZED_TENANT_DISCOUNT_SOURCES_SCHEMA_VERSION = 1;
21
2
  declare const DiscountSourceKeyMetricsSchema: Schema<any, import("mongoose").Model<any, any, any, any, any, any>, {}, {}, {}, {}, {
22
3
  _id: false;
23
4
  }, {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_INDEX_OPTIONS = exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_INDEXES = exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_NAME = exports.DiscountSourceKeyMetricsSchema = exports.MaterializedTenantDiscountSourceSchema = exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_SCHEMA_VERSION = void 0;
3
+ exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_INDEX_OPTIONS = exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_INDEXES = exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_NAME = exports.DiscountSourceKeyMetricsSchema = exports.MaterializedTenantDiscountSourceSchema = void 0;
4
4
  const mongoose_1 = require("mongoose");
5
5
  const ENUMS_1 = require("../../constants/ENUMS");
6
6
  const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
@@ -22,7 +22,7 @@ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
22
22
  * grouping or rate rule changes — historical documents must then be rebuilt
23
23
  * with the backfill script.
24
24
  */
25
- exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_SCHEMA_VERSION = 1;
25
+ const MATERIALIZED_SCHEMA_VERSIONS_1 = require("../../constants/MATERIALIZED_SCHEMA_VERSIONS");
26
26
  const DiscountSourceKeyMetricsSchema = new mongoose_1.Schema({
27
27
  key: { type: String, required: true },
28
28
  order_count: { type: Number, required: true, min: 0, default: 0 },
@@ -110,7 +110,7 @@ exports.MaterializedTenantDiscountSourceSchema = new mongoose_1.Schema({
110
110
  type: Number,
111
111
  required: true,
112
112
  immutable: true,
113
- default: exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_SCHEMA_VERSION,
113
+ default: MATERIALIZED_SCHEMA_VERSIONS_1.MATERIALIZED_TENANT_DISCOUNT_SOURCES_SCHEMA_VERSION,
114
114
  },
115
115
  source_watermark: { type: mongoose_1.Schema.Types.Date, required: true },
116
116
  computed_at: { type: mongoose_1.Schema.Types.Date, required: true },