@managesome/eposmatic-toolkit 0.4.74 → 0.4.76

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.
@@ -2,6 +2,7 @@ import ObjectID from 'bson-objectid';
2
2
  import { MODIFIER_OPTION_REF_TYPE_ENUM, ORDER_ITEM_TYPE_ENUM } from '../../constants';
3
3
  import { calculateItemTotalWithTax } from '../store';
4
4
  import { applyChooseExpensiveSegmentPrice, convertItemDiscountToOrderItemDiscount, syncOrderEngineItem, } from './order-engine';
5
+ import { getSetupDiscountDetailsForQuantity } from './setup-discount-helpers';
5
6
  import { convertItemToWorkableItem, findApplicableSchedule, getExcludedPlatformsFromIncludedPlatforms, getPriceEnum, } from './platform-catalog';
6
7
  import { convertPriceObjFromCents, convertPriceObjToCents, convertPriceObjToSegmentPrice, convertSpecialHoursFromCents, convertSpecialHoursToCents, mergePriceObjs, } from './platform-num';
7
8
  import { buildSelectionRefQueues, buildStoredFromSelection, consumeSelectionMatch, } from './selection-model';
@@ -479,16 +480,25 @@ export function convertHalfHalfToOrderItem({ halfHalfItem, selectedHalfHalfItems
479
480
  // Save direction of the selection model: `_id`s preserved when carried from
480
481
  // the stored order item, fresh ObjectIds otherwise.
481
482
  const modifiers = buildStoredFromSelection(groups);
483
+ // Fixed setup discounts are PER-UNIT amounts; resolve the line-level slot for
484
+ // the requested quantity (edit-safe: handles loyalty/voucher slots, a manual
485
+ // special slot, and a definition added/removed since the line was created).
486
+ // Percentage discounts stay line-based downstream.
487
+ const definitionDiscountDetails = convertItemDiscountToOrderItemDiscount(halfHalfItem.discount_amount, halfHalfItem.is_discount_percentage, platform, halfHalfItem._id);
488
+ const lineDiscountDetails = getSetupDiscountDetailsForQuantity({
489
+ setupId: halfHalfItem._id,
490
+ definitionDiscountDetails,
491
+ existingOrderItem,
492
+ quantity,
493
+ });
482
494
  const orderItem = {
483
- // Half-half setup discount (schedule-resolved) is a special_discount on the
484
- // order item — same as specials.
485
- item_discount_details: convertItemDiscountToOrderItemDiscount(halfHalfItem.discount_amount, halfHalfItem.is_discount_percentage, platform, halfHalfItem._id),
486
495
  special_instructions: '',
487
496
  _net_amount: 0,
488
497
  _gross_amount: 0,
489
498
  _gst_amount: 0,
490
499
  _discount_amount: 0,
491
500
  ...existingOrderItem,
501
+ item_discount_details: lineDiscountDetails,
492
502
  // Keep the original order-item id on edit (otherwise getCartItemsObj would
493
503
  // treat it as removed + new); generate a fresh ObjectId on create, same as
494
504
  // regular items.
@@ -679,7 +689,15 @@ export function convertHalfHalfToOrderEngineItem({ halfHalfItem, selectedHalfHal
679
689
  quantity,
680
690
  image_url: halfHalfItem.image_url,
681
691
  special_instructions: existingOrderItem?.special_instructions ?? '',
682
- item_discount_details: convertItemDiscountToOrderItemDiscount(halfHalfItem.discount_amount, halfHalfItem.is_discount_percentage, platform, halfHalfItem._id),
692
+ // Fixed setup discounts are per-unit; scale to the selected quantity before
693
+ // syncOrderEngineItem recomputes subtotal/total_discount. (The engine path
694
+ // rebuilds the line from the definition and does not preserve existing
695
+ // discount slots — unchanged behavior.) Percentage discounts stay untouched.
696
+ item_discount_details: getSetupDiscountDetailsForQuantity({
697
+ setupId: halfHalfItem._id,
698
+ definitionDiscountDetails: convertItemDiscountToOrderItemDiscount(halfHalfItem.discount_amount, halfHalfItem.is_discount_percentage, platform, halfHalfItem._id),
699
+ quantity,
700
+ }),
683
701
  modifiers,
684
702
  };
685
703
  const syncedOrderEngineItem = syncOrderEngineItem(orderEngineItem);
@@ -0,0 +1,79 @@
1
+ import { getItemQuantityChangeResults } from '../store';
2
+ /**
3
+ * Resolves the line-level discount details for a setup (special / half-half)
4
+ * order line at the requested quantity.
5
+ *
6
+ * Setup discounts (`discount_amount` / `is_discount_percentage`) are PER-UNIT
7
+ * amounts. The totals math (`calculateItemTotalWithTax`, `syncOrderEngineItem`)
8
+ * subtracts a fixed discount once per line, so the setup slot must be scaled to
9
+ * the line quantity here — the same convention the cart and classic drawer use
10
+ * in `getItemQuantityChangeResults`.
11
+ *
12
+ * Edit handling:
13
+ * - other slots (loyalty / voucher) hold LINE amounts for the OLD quantity and
14
+ * are first rescaled to the new quantity;
15
+ * - a setup-owned `special` slot is rebuilt from the current definition (this
16
+ * also heals lines persisted before per-unit scaling was applied);
17
+ * - a manual `special` slot (different id) is kept and rescaled;
18
+ * - when the definition no longer has a discount, the stale setup slot is
19
+ * cleared while any loyalty/voucher slots are kept.
20
+ *
21
+ * Percentage discounts are returned unchanged — they are applied against the
22
+ * line total downstream.
23
+ */
24
+ export function getSetupDiscountDetailsForQuantity({ setupId, definitionDiscountDetails, existingOrderItem, quantity, }) {
25
+ const existingDetails = existingOrderItem?.item_discount_details ?? null;
26
+ const prevQuantity = existingOrderItem?.quantity ?? 1;
27
+ // Rescale the existing LINE amounts (loyalty/voucher/manual special) from the
28
+ // old quantity to the new one. Percentage slots are untouched.
29
+ const scaledExistingDetails = existingDetails != null
30
+ ? getItemQuantityChangeResults({
31
+ prevQuantity,
32
+ newQuantity: quantity,
33
+ prevItemDiscountDetails: existingDetails,
34
+ })
35
+ : null;
36
+ const existingSpecialId = existingDetails?.special_discount_id;
37
+ // A missing special slot counts as the setup's own slot (nothing manual to
38
+ // preserve); only a special slot with a DIFFERENT id is a manual discount.
39
+ const hasSetupSpecialSlot = existingSpecialId == null || existingSpecialId === setupId;
40
+ if (definitionDiscountDetails == null) {
41
+ if (!hasSetupSpecialSlot || scaledExistingDetails == null) {
42
+ return scaledExistingDetails;
43
+ }
44
+ // The setup no longer defines a discount — drop the stale setup slot while
45
+ // keeping any (already rescaled) loyalty/voucher slots.
46
+ const remainingDetails = withoutSpecialDiscountSlot(scaledExistingDetails);
47
+ return hasAnyDiscountSlot(remainingDetails) ? remainingDetails : null;
48
+ }
49
+ if (!hasSetupSpecialSlot) {
50
+ // Manual special discount applied by staff — keep it, rescaled.
51
+ return scaledExistingDetails;
52
+ }
53
+ const setupDiscountForQuantity = getItemQuantityChangeResults({
54
+ prevQuantity: 1,
55
+ newQuantity: quantity,
56
+ prevItemDiscountDetails: definitionDiscountDetails,
57
+ });
58
+ // Keep the rescaled loyalty/voucher slots and stamp the setup slot from the
59
+ // definition (clearing any stale setup-slot metadata first).
60
+ const mergedDetails = withoutSpecialDiscountSlot(scaledExistingDetails ?? { total_discount: 0 });
61
+ return {
62
+ ...mergedDetails,
63
+ ...(setupDiscountForQuantity ?? {}),
64
+ total_discount: 0,
65
+ };
66
+ }
67
+ function withoutSpecialDiscountSlot(details) {
68
+ const next = { ...details, total_discount: 0 };
69
+ delete next.special_discount;
70
+ delete next.special_discount_id;
71
+ delete next.special_discount_name;
72
+ delete next.is_special_discount_percentage;
73
+ delete next.special_discount_reason;
74
+ delete next.special_reference_number;
75
+ return next;
76
+ }
77
+ function hasAnyDiscountSlot(details) {
78
+ return (details.special_discount_id != null || details.loyalty_discount_id != null || details.voucher_discount_id != null);
79
+ }
@@ -4,6 +4,7 @@ import { MODIFIER_OPTION_REF_TYPE_ENUM, ORDER_ITEM_TYPE_ENUM } from '../../const
4
4
  import { calculateItemTotalWithTax, getComparableOrderItem } from '../store';
5
5
  import { convertHalfHalfToItem } from './halfhalf-setup-helpers';
6
6
  import { convertItemDiscountToOrderItemDiscount } from './order-engine';
7
+ import { getSetupDiscountDetailsForQuantity } from './setup-discount-helpers';
7
8
  import { convertItemToWorkableItem, findApplicableSchedule, getExcludedPlatformsFromIncludedPlatforms, getPriceEnum, } from './platform-catalog';
8
9
  import { convertPriceObjFromCents, convertPriceObjToCents, convertSpecialHoursFromCents, convertSpecialHoursToCents, mergePriceObjs, } from './platform-num';
9
10
  import { convertPriceFromCents, convertPriceToCents } from './price-conversion';
@@ -334,14 +335,25 @@ export function convertSpecialItemToOrderItem({ specialItem, modifierSpecials, q
334
335
  // Save direction of the selection model: `_id`s preserved when carried from
335
336
  // the stored order item, fresh ObjectIds otherwise.
336
337
  const modifiers = buildStoredFromSelection(groups);
338
+ // Fixed setup discounts are PER-UNIT amounts; resolve the line-level slot for
339
+ // the requested quantity (edit-safe: handles loyalty/voucher slots, a manual
340
+ // special slot, and a definition added/removed since the line was created).
341
+ // Percentage discounts stay line-based downstream.
342
+ const definitionDiscountDetails = convertItemDiscountToOrderItemDiscount(specialItem.discount_amount, specialItem.is_discount_percentage, platform, specialItem._id);
343
+ const lineDiscountDetails = getSetupDiscountDetailsForQuantity({
344
+ setupId: specialItem._id,
345
+ definitionDiscountDetails,
346
+ existingOrderItem,
347
+ quantity,
348
+ });
337
349
  const orderItem = {
338
- item_discount_details: convertItemDiscountToOrderItemDiscount(specialItem.discount_amount, specialItem.is_discount_percentage, platform, specialItem._id),
339
350
  special_instructions: '',
340
351
  _net_amount: 0,
341
352
  _gross_amount: 0,
342
353
  _gst_amount: 0,
343
354
  _discount_amount: 0,
344
355
  ...existingOrderItem,
356
+ item_discount_details: lineDiscountDetails,
345
357
  // Keep the original order-item id on edit (otherwise getCartItemsObj would
346
358
  // treat it as removed + new); generate a fresh ObjectId on create, same as
347
359
  // regular items.
@@ -399,6 +399,11 @@ export const TenantSchema = new Schema({
399
399
  enum: ALL_TRACK_ORDER_NUMBER_TYPES,
400
400
  default: TRACK_ORDER_NUMBER_ENUM.DAY,
401
401
  },
402
+ track_purchase_order_number_by: {
403
+ type: String,
404
+ enum: ALL_TRACK_ORDER_NUMBER_TYPES,
405
+ default: TRACK_ORDER_NUMBER_ENUM.DAY,
406
+ },
402
407
  order_number_grouping_type: {
403
408
  type: String,
404
409
  enum: ALL_ORDER_NUMBER_GROUPING_TYPES,
@@ -18,6 +18,7 @@ const bson_objectid_1 = __importDefault(require("bson-objectid"));
18
18
  const constants_1 = require("../../constants");
19
19
  const store_1 = require("../store");
20
20
  const order_engine_1 = require("./order-engine");
21
+ const setup_discount_helpers_1 = require("./setup-discount-helpers");
21
22
  const platform_catalog_1 = require("./platform-catalog");
22
23
  const platform_num_1 = require("./platform-num");
23
24
  const selection_model_1 = require("./selection-model");
@@ -495,16 +496,25 @@ function convertHalfHalfToOrderItem({ halfHalfItem, selectedHalfHalfItems, quant
495
496
  // Save direction of the selection model: `_id`s preserved when carried from
496
497
  // the stored order item, fresh ObjectIds otherwise.
497
498
  const modifiers = (0, selection_model_1.buildStoredFromSelection)(groups);
499
+ // Fixed setup discounts are PER-UNIT amounts; resolve the line-level slot for
500
+ // the requested quantity (edit-safe: handles loyalty/voucher slots, a manual
501
+ // special slot, and a definition added/removed since the line was created).
502
+ // Percentage discounts stay line-based downstream.
503
+ const definitionDiscountDetails = (0, order_engine_1.convertItemDiscountToOrderItemDiscount)(halfHalfItem.discount_amount, halfHalfItem.is_discount_percentage, platform, halfHalfItem._id);
504
+ const lineDiscountDetails = (0, setup_discount_helpers_1.getSetupDiscountDetailsForQuantity)({
505
+ setupId: halfHalfItem._id,
506
+ definitionDiscountDetails,
507
+ existingOrderItem,
508
+ quantity,
509
+ });
498
510
  const orderItem = {
499
- // Half-half setup discount (schedule-resolved) is a special_discount on the
500
- // order item — same as specials.
501
- item_discount_details: (0, order_engine_1.convertItemDiscountToOrderItemDiscount)(halfHalfItem.discount_amount, halfHalfItem.is_discount_percentage, platform, halfHalfItem._id),
502
511
  special_instructions: '',
503
512
  _net_amount: 0,
504
513
  _gross_amount: 0,
505
514
  _gst_amount: 0,
506
515
  _discount_amount: 0,
507
516
  ...existingOrderItem,
517
+ item_discount_details: lineDiscountDetails,
508
518
  // Keep the original order-item id on edit (otherwise getCartItemsObj would
509
519
  // treat it as removed + new); generate a fresh ObjectId on create, same as
510
520
  // regular items.
@@ -695,7 +705,15 @@ function convertHalfHalfToOrderEngineItem({ halfHalfItem, selectedHalfHalfItems,
695
705
  quantity,
696
706
  image_url: halfHalfItem.image_url,
697
707
  special_instructions: existingOrderItem?.special_instructions ?? '',
698
- item_discount_details: (0, order_engine_1.convertItemDiscountToOrderItemDiscount)(halfHalfItem.discount_amount, halfHalfItem.is_discount_percentage, platform, halfHalfItem._id),
708
+ // Fixed setup discounts are per-unit; scale to the selected quantity before
709
+ // syncOrderEngineItem recomputes subtotal/total_discount. (The engine path
710
+ // rebuilds the line from the definition and does not preserve existing
711
+ // discount slots — unchanged behavior.) Percentage discounts stay untouched.
712
+ item_discount_details: (0, setup_discount_helpers_1.getSetupDiscountDetailsForQuantity)({
713
+ setupId: halfHalfItem._id,
714
+ definitionDiscountDetails: (0, order_engine_1.convertItemDiscountToOrderItemDiscount)(halfHalfItem.discount_amount, halfHalfItem.is_discount_percentage, platform, halfHalfItem._id),
715
+ quantity,
716
+ }),
699
717
  modifiers,
700
718
  };
701
719
  const syncedOrderEngineItem = (0, order_engine_1.syncOrderEngineItem)(orderEngineItem);
@@ -0,0 +1,29 @@
1
+ import { IDiscountDetails, IOrderItem } from '../../schemas';
2
+ /**
3
+ * Resolves the line-level discount details for a setup (special / half-half)
4
+ * order line at the requested quantity.
5
+ *
6
+ * Setup discounts (`discount_amount` / `is_discount_percentage`) are PER-UNIT
7
+ * amounts. The totals math (`calculateItemTotalWithTax`, `syncOrderEngineItem`)
8
+ * subtracts a fixed discount once per line, so the setup slot must be scaled to
9
+ * the line quantity here — the same convention the cart and classic drawer use
10
+ * in `getItemQuantityChangeResults`.
11
+ *
12
+ * Edit handling:
13
+ * - other slots (loyalty / voucher) hold LINE amounts for the OLD quantity and
14
+ * are first rescaled to the new quantity;
15
+ * - a setup-owned `special` slot is rebuilt from the current definition (this
16
+ * also heals lines persisted before per-unit scaling was applied);
17
+ * - a manual `special` slot (different id) is kept and rescaled;
18
+ * - when the definition no longer has a discount, the stale setup slot is
19
+ * cleared while any loyalty/voucher slots are kept.
20
+ *
21
+ * Percentage discounts are returned unchanged — they are applied against the
22
+ * line total downstream.
23
+ */
24
+ export declare function getSetupDiscountDetailsForQuantity({ setupId, definitionDiscountDetails, existingOrderItem, quantity, }: {
25
+ setupId: string;
26
+ definitionDiscountDetails: IDiscountDetails | null;
27
+ existingOrderItem?: IOrderItem;
28
+ quantity: number;
29
+ }): IDiscountDetails | null;
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSetupDiscountDetailsForQuantity = getSetupDiscountDetailsForQuantity;
4
+ const store_1 = require("../store");
5
+ /**
6
+ * Resolves the line-level discount details for a setup (special / half-half)
7
+ * order line at the requested quantity.
8
+ *
9
+ * Setup discounts (`discount_amount` / `is_discount_percentage`) are PER-UNIT
10
+ * amounts. The totals math (`calculateItemTotalWithTax`, `syncOrderEngineItem`)
11
+ * subtracts a fixed discount once per line, so the setup slot must be scaled to
12
+ * the line quantity here — the same convention the cart and classic drawer use
13
+ * in `getItemQuantityChangeResults`.
14
+ *
15
+ * Edit handling:
16
+ * - other slots (loyalty / voucher) hold LINE amounts for the OLD quantity and
17
+ * are first rescaled to the new quantity;
18
+ * - a setup-owned `special` slot is rebuilt from the current definition (this
19
+ * also heals lines persisted before per-unit scaling was applied);
20
+ * - a manual `special` slot (different id) is kept and rescaled;
21
+ * - when the definition no longer has a discount, the stale setup slot is
22
+ * cleared while any loyalty/voucher slots are kept.
23
+ *
24
+ * Percentage discounts are returned unchanged — they are applied against the
25
+ * line total downstream.
26
+ */
27
+ function getSetupDiscountDetailsForQuantity({ setupId, definitionDiscountDetails, existingOrderItem, quantity, }) {
28
+ const existingDetails = existingOrderItem?.item_discount_details ?? null;
29
+ const prevQuantity = existingOrderItem?.quantity ?? 1;
30
+ // Rescale the existing LINE amounts (loyalty/voucher/manual special) from the
31
+ // old quantity to the new one. Percentage slots are untouched.
32
+ const scaledExistingDetails = existingDetails != null
33
+ ? (0, store_1.getItemQuantityChangeResults)({
34
+ prevQuantity,
35
+ newQuantity: quantity,
36
+ prevItemDiscountDetails: existingDetails,
37
+ })
38
+ : null;
39
+ const existingSpecialId = existingDetails?.special_discount_id;
40
+ // A missing special slot counts as the setup's own slot (nothing manual to
41
+ // preserve); only a special slot with a DIFFERENT id is a manual discount.
42
+ const hasSetupSpecialSlot = existingSpecialId == null || existingSpecialId === setupId;
43
+ if (definitionDiscountDetails == null) {
44
+ if (!hasSetupSpecialSlot || scaledExistingDetails == null) {
45
+ return scaledExistingDetails;
46
+ }
47
+ // The setup no longer defines a discount — drop the stale setup slot while
48
+ // keeping any (already rescaled) loyalty/voucher slots.
49
+ const remainingDetails = withoutSpecialDiscountSlot(scaledExistingDetails);
50
+ return hasAnyDiscountSlot(remainingDetails) ? remainingDetails : null;
51
+ }
52
+ if (!hasSetupSpecialSlot) {
53
+ // Manual special discount applied by staff — keep it, rescaled.
54
+ return scaledExistingDetails;
55
+ }
56
+ const setupDiscountForQuantity = (0, store_1.getItemQuantityChangeResults)({
57
+ prevQuantity: 1,
58
+ newQuantity: quantity,
59
+ prevItemDiscountDetails: definitionDiscountDetails,
60
+ });
61
+ // Keep the rescaled loyalty/voucher slots and stamp the setup slot from the
62
+ // definition (clearing any stale setup-slot metadata first).
63
+ const mergedDetails = withoutSpecialDiscountSlot(scaledExistingDetails ?? { total_discount: 0 });
64
+ return {
65
+ ...mergedDetails,
66
+ ...(setupDiscountForQuantity ?? {}),
67
+ total_discount: 0,
68
+ };
69
+ }
70
+ function withoutSpecialDiscountSlot(details) {
71
+ const next = { ...details, total_discount: 0 };
72
+ delete next.special_discount;
73
+ delete next.special_discount_id;
74
+ delete next.special_discount_name;
75
+ delete next.is_special_discount_percentage;
76
+ delete next.special_discount_reason;
77
+ delete next.special_reference_number;
78
+ return next;
79
+ }
80
+ function hasAnyDiscountSlot(details) {
81
+ return (details.special_discount_id != null || details.loyalty_discount_id != null || details.voucher_discount_id != null);
82
+ }
@@ -19,6 +19,7 @@ const constants_1 = require("../../constants");
19
19
  const store_1 = require("../store");
20
20
  const halfhalf_setup_helpers_1 = require("./halfhalf-setup-helpers");
21
21
  const order_engine_1 = require("./order-engine");
22
+ const setup_discount_helpers_1 = require("./setup-discount-helpers");
22
23
  const platform_catalog_1 = require("./platform-catalog");
23
24
  const platform_num_1 = require("./platform-num");
24
25
  const price_conversion_1 = require("./price-conversion");
@@ -349,14 +350,25 @@ function convertSpecialItemToOrderItem({ specialItem, modifierSpecials, quantity
349
350
  // Save direction of the selection model: `_id`s preserved when carried from
350
351
  // the stored order item, fresh ObjectIds otherwise.
351
352
  const modifiers = (0, selection_model_1.buildStoredFromSelection)(groups);
353
+ // Fixed setup discounts are PER-UNIT amounts; resolve the line-level slot for
354
+ // the requested quantity (edit-safe: handles loyalty/voucher slots, a manual
355
+ // special slot, and a definition added/removed since the line was created).
356
+ // Percentage discounts stay line-based downstream.
357
+ const definitionDiscountDetails = (0, order_engine_1.convertItemDiscountToOrderItemDiscount)(specialItem.discount_amount, specialItem.is_discount_percentage, platform, specialItem._id);
358
+ const lineDiscountDetails = (0, setup_discount_helpers_1.getSetupDiscountDetailsForQuantity)({
359
+ setupId: specialItem._id,
360
+ definitionDiscountDetails,
361
+ existingOrderItem,
362
+ quantity,
363
+ });
352
364
  const orderItem = {
353
- item_discount_details: (0, order_engine_1.convertItemDiscountToOrderItemDiscount)(specialItem.discount_amount, specialItem.is_discount_percentage, platform, specialItem._id),
354
365
  special_instructions: '',
355
366
  _net_amount: 0,
356
367
  _gross_amount: 0,
357
368
  _gst_amount: 0,
358
369
  _discount_amount: 0,
359
370
  ...existingOrderItem,
371
+ item_discount_details: lineDiscountDetails,
360
372
  // Keep the original order-item id on edit (otherwise getCartItemsObj would
361
373
  // treat it as removed + new); generate a fresh ObjectId on create, same as
362
374
  // regular items.
@@ -95,6 +95,7 @@ export declare const TenantSchema: Schema<any, import("mongoose").Model<any, any
95
95
  staff_ordering_domain: string;
96
96
  business_type: string;
97
97
  track_order_number_by: TRACK_ORDER_NUMBER_ENUM;
98
+ track_purchase_order_number_by: TRACK_ORDER_NUMBER_ENUM;
98
99
  order_number_grouping_type: ORDER_NUMBER_GROUPING_TYPE_ENUM;
99
100
  web_show_featured_items: boolean;
100
101
  featured_category_name: string;
@@ -276,6 +277,7 @@ export declare const TenantSchema: Schema<any, import("mongoose").Model<any, any
276
277
  staff_ordering_domain: string;
277
278
  business_type: string;
278
279
  track_order_number_by: TRACK_ORDER_NUMBER_ENUM;
280
+ track_purchase_order_number_by: TRACK_ORDER_NUMBER_ENUM;
279
281
  order_number_grouping_type: ORDER_NUMBER_GROUPING_TYPE_ENUM;
280
282
  web_show_featured_items: boolean;
281
283
  featured_category_name: string;
@@ -457,6 +459,7 @@ export declare const TenantSchema: Schema<any, import("mongoose").Model<any, any
457
459
  staff_ordering_domain: string;
458
460
  business_type: string;
459
461
  track_order_number_by: TRACK_ORDER_NUMBER_ENUM;
462
+ track_purchase_order_number_by: TRACK_ORDER_NUMBER_ENUM;
460
463
  order_number_grouping_type: ORDER_NUMBER_GROUPING_TYPE_ENUM;
461
464
  web_show_featured_items: boolean;
462
465
  featured_category_name: string;
@@ -402,6 +402,11 @@ exports.TenantSchema = new mongoose_1.Schema({
402
402
  enum: constants_1.ALL_TRACK_ORDER_NUMBER_TYPES,
403
403
  default: constants_1.TRACK_ORDER_NUMBER_ENUM.DAY,
404
404
  },
405
+ track_purchase_order_number_by: {
406
+ type: String,
407
+ enum: constants_1.ALL_TRACK_ORDER_NUMBER_TYPES,
408
+ default: constants_1.TRACK_ORDER_NUMBER_ENUM.DAY,
409
+ },
405
410
  order_number_grouping_type: {
406
411
  type: String,
407
412
  enum: constants_1.ALL_ORDER_NUMBER_GROUPING_TYPES,
@@ -29,6 +29,7 @@ export declare const WarehouseSchema: Schema<any, import("mongoose").Model<any,
29
29
  coordinates: number[];
30
30
  brand_color: string;
31
31
  public_business_name: string;
32
+ track_purchase_order_number_by: TRACK_ORDER_NUMBER_ENUM;
32
33
  im_card_gst: number;
33
34
  im_cash_gst: number;
34
35
  default_im_gst_type: GST_TYPE_ENUM;
@@ -47,7 +48,6 @@ export declare const WarehouseSchema: Schema<any, import("mongoose").Model<any,
47
48
  invoice_card_gst: number;
48
49
  invoice_cash_gst: number;
49
50
  default_invoice_gst_type: GST_TYPE_ENUM;
50
- track_purchase_order_number_by: TRACK_ORDER_NUMBER_ENUM;
51
51
  track_invoice_number_by: TRACK_ORDER_NUMBER_ENUM;
52
52
  enable_lot_serial_tracking: boolean;
53
53
  default_removal_strategy: WAREHOUSE_REMOVAL_STRATEGY_ENUM;
@@ -83,6 +83,7 @@ export declare const WarehouseSchema: Schema<any, import("mongoose").Model<any,
83
83
  coordinates: number[];
84
84
  brand_color: string;
85
85
  public_business_name: string;
86
+ track_purchase_order_number_by: TRACK_ORDER_NUMBER_ENUM;
86
87
  im_card_gst: number;
87
88
  im_cash_gst: number;
88
89
  default_im_gst_type: GST_TYPE_ENUM;
@@ -101,7 +102,6 @@ export declare const WarehouseSchema: Schema<any, import("mongoose").Model<any,
101
102
  invoice_card_gst: number;
102
103
  invoice_cash_gst: number;
103
104
  default_invoice_gst_type: GST_TYPE_ENUM;
104
- track_purchase_order_number_by: TRACK_ORDER_NUMBER_ENUM;
105
105
  track_invoice_number_by: TRACK_ORDER_NUMBER_ENUM;
106
106
  enable_lot_serial_tracking: boolean;
107
107
  default_removal_strategy: WAREHOUSE_REMOVAL_STRATEGY_ENUM;
@@ -137,6 +137,7 @@ export declare const WarehouseSchema: Schema<any, import("mongoose").Model<any,
137
137
  coordinates: number[];
138
138
  brand_color: string;
139
139
  public_business_name: string;
140
+ track_purchase_order_number_by: TRACK_ORDER_NUMBER_ENUM;
140
141
  im_card_gst: number;
141
142
  im_cash_gst: number;
142
143
  default_im_gst_type: GST_TYPE_ENUM;
@@ -155,7 +156,6 @@ export declare const WarehouseSchema: Schema<any, import("mongoose").Model<any,
155
156
  invoice_card_gst: number;
156
157
  invoice_cash_gst: number;
157
158
  default_invoice_gst_type: GST_TYPE_ENUM;
158
- track_purchase_order_number_by: TRACK_ORDER_NUMBER_ENUM;
159
159
  track_invoice_number_by: TRACK_ORDER_NUMBER_ENUM;
160
160
  enable_lot_serial_tracking: boolean;
161
161
  default_removal_strategy: WAREHOUSE_REMOVAL_STRATEGY_ENUM;
@@ -97,6 +97,7 @@ export interface ITenant {
97
97
  business_type: string;
98
98
  tax_business_number: string;
99
99
  track_order_number_by: TRACK_ORDER_NUMBER_ENUM;
100
+ track_purchase_order_number_by: TRACK_ORDER_NUMBER_ENUM;
100
101
  order_number_grouping_type: ORDER_NUMBER_GROUPING_TYPE_ENUM;
101
102
  public_phone_numbers: string[];
102
103
  web_show_featured_items: boolean;
@@ -0,0 +1,274 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const constants_1 = require("../constants");
4
+ const halfhalf_setup_helpers_1 = require("../helpers/utils/halfhalf-setup-helpers");
5
+ const specials_helpers_1 = require("../helpers/utils/specials-helpers");
6
+ // ============================================================================
7
+ // Fixed setup discounts must scale with quantity.
8
+ //
9
+ // `convertItemDiscountToOrderItemDiscount` stores `special_discount` as a
10
+ // PER-UNIT amount. The shared totals math subtracts a fixed discount once per
11
+ // line, so the setup converters must scale it to the selected quantity (the
12
+ // same convention the cart/classic drawer use in getItemQuantityChangeResults).
13
+ //
14
+ // These tests pin:
15
+ // - fixed discounts at quantity 1 / 2,
16
+ // - percentage discounts staying unscaled (applied to the line total),
17
+ // - edit scaling (stored line amount -> new quantity),
18
+ // - self-healing of legacy lines that kept the per-unit amount at qty > 1,
19
+ // - manual discount slots scaling from the previous quantity,
20
+ // - the same behavior in both half-half converters.
21
+ // ============================================================================
22
+ const PLATFORM = constants_1.PLATFORMS_ENUM.POS_WALK_IN;
23
+ // ---------------------------------------------------------------------------
24
+ // Fixtures
25
+ // ---------------------------------------------------------------------------
26
+ function makeSlotItem(itemId, price) {
27
+ return {
28
+ _id: `slot-${itemId}`,
29
+ item_id: itemId,
30
+ menu_id: 'menu-1',
31
+ item_type: constants_1.ORDER_ITEM_TYPE_ENUM.ITEM,
32
+ name: `Item ${itemId}`,
33
+ price,
34
+ subtotal: price,
35
+ quantity: 1,
36
+ special_instructions: '',
37
+ item_discount_details: null,
38
+ tax_rate: 0,
39
+ modifiers: null,
40
+ _discount_amount: 0,
41
+ _gross_amount: 0,
42
+ _gst_amount: 0,
43
+ _net_amount: 0,
44
+ };
45
+ }
46
+ function makeWorkableSpecial({ price = 1000, discount = null, isPercentage = false, } = {}) {
47
+ return {
48
+ _id: 'special-1',
49
+ name: 'Deal',
50
+ description: '',
51
+ sold_out: [],
52
+ image_url: '',
53
+ category_id: 'combo-cat',
54
+ price: { BASE_PRICE: price },
55
+ discount_amount: discount == null ? null : { BASE_PRICE: discount },
56
+ is_discount_percentage: isPercentage,
57
+ display_price: null,
58
+ show_starting_from: false,
59
+ modifier_specials: [
60
+ {
61
+ _id: 'mod-1',
62
+ name: 'Group',
63
+ quantity: 1,
64
+ num_free_items: 0,
65
+ items: [],
66
+ exclusive_refs: [],
67
+ excluded_refs: [],
68
+ },
69
+ ],
70
+ };
71
+ }
72
+ function convertSpecial(quantity, options = {}, existingOrderItem) {
73
+ return (0, specials_helpers_1.convertSpecialItemToOrderItem)({
74
+ specialItem: makeWorkableSpecial(options),
75
+ modifierSpecials: [{ _id: 'mod-1', selectedItems: [makeSlotItem('slot-item', 0)] }],
76
+ quantity,
77
+ platform: PLATFORM,
78
+ existingOrderItem,
79
+ });
80
+ }
81
+ function makeExistingSpecialLine({ quantity, specialDiscount, specialDiscountId = 'special-1', loyaltyDiscount, }) {
82
+ const item_discount_details = { total_discount: 0 };
83
+ if (specialDiscount != null) {
84
+ item_discount_details.special_discount = specialDiscount;
85
+ item_discount_details.special_discount_id = specialDiscountId;
86
+ item_discount_details.special_discount_name = 'Special Discount';
87
+ item_discount_details.is_special_discount_percentage = false;
88
+ item_discount_details.total_discount += specialDiscount;
89
+ }
90
+ if (loyaltyDiscount != null) {
91
+ item_discount_details.loyalty_discount = loyaltyDiscount;
92
+ item_discount_details.loyalty_discount_id = 'loyalty-offer-1';
93
+ item_discount_details.loyalty_discount_name = 'Loyalty Discount';
94
+ item_discount_details.is_loyalty_discount_percentage = false;
95
+ item_discount_details.total_discount += loyaltyDiscount;
96
+ }
97
+ return {
98
+ ...makeSlotItem('slot-item', 0),
99
+ _id: 'existing-line',
100
+ item_id: 'special-1',
101
+ item_type: constants_1.ORDER_ITEM_TYPE_ENUM.SPECIAL,
102
+ name: 'Deal',
103
+ price: 1000,
104
+ subtotal: 0,
105
+ quantity,
106
+ item_discount_details,
107
+ };
108
+ }
109
+ function makeWorkableHalfHalf({ discount = null, isPercentage = false, } = {}) {
110
+ return {
111
+ _id: 'halfhalf-1',
112
+ name: 'Half/Half',
113
+ description: '',
114
+ sold_out: [],
115
+ image_url: '',
116
+ choose_expensive_segment_price: false,
117
+ segments: [
118
+ { _id: 'seg-1', name: 'First Half', sort_id: 1 },
119
+ { _id: 'seg-2', name: 'Second Half', sort_id: 2 },
120
+ ],
121
+ category_id: 'pizza-cat',
122
+ excluded_refs: [],
123
+ exclusive_refs: [],
124
+ // Item-level price + two 500 segment options => 1100 per unit.
125
+ upsell_price: { BASE_PRICE: 100 },
126
+ discount_amount: discount == null ? null : { BASE_PRICE: discount },
127
+ is_discount_percentage: isPercentage,
128
+ items: [],
129
+ };
130
+ }
131
+ function makeHalfHalfSelections() {
132
+ return [makeSlotItem('half-a', 500), makeSlotItem('half-b', 500)];
133
+ }
134
+ function makeExistingHalfHalfLine({ quantity, specialDiscount, }) {
135
+ return {
136
+ ...makeSlotItem('half-a', 500),
137
+ _id: 'existing-halfhalf-line',
138
+ item_id: 'halfhalf-1',
139
+ item_type: constants_1.ORDER_ITEM_TYPE_ENUM.HALF_HALF,
140
+ name: 'Half/Half',
141
+ price: 100,
142
+ subtotal: 0,
143
+ quantity,
144
+ item_discount_details: {
145
+ special_discount: specialDiscount,
146
+ special_discount_id: 'halfhalf-1',
147
+ special_discount_name: 'Special Discount',
148
+ is_special_discount_percentage: false,
149
+ total_discount: specialDiscount,
150
+ },
151
+ };
152
+ }
153
+ // ---------------------------------------------------------------------------
154
+ // Specials
155
+ // ---------------------------------------------------------------------------
156
+ describe('convertSpecialItemToOrderItem: fixed discount scales with quantity', () => {
157
+ it('keeps quantity 1 unchanged', () => {
158
+ const orderItem = convertSpecial(1, { discount: 150 });
159
+ expect(orderItem.subtotal).toBe(850); // 1000 - 150
160
+ expect(orderItem.item_discount_details?.total_discount).toBe(150);
161
+ });
162
+ it('applies the discount once per unit at quantity 2', () => {
163
+ const orderItem = convertSpecial(2, { discount: 150 });
164
+ expect(orderItem.subtotal).toBe(1700); // 1000*2 - 150*2
165
+ expect(orderItem.item_discount_details?.total_discount).toBe(300);
166
+ });
167
+ it('does not scale percentage discounts (they apply to the line total)', () => {
168
+ const orderItem = convertSpecial(2, { discount: 10, isPercentage: true });
169
+ expect(orderItem.subtotal).toBe(1800); // 2000 - 10%
170
+ expect(orderItem.item_discount_details?.total_discount).toBe(200);
171
+ });
172
+ });
173
+ describe('convertSpecialItemToOrderItem: edit keeps the discount in sync', () => {
174
+ it('rebuilds the setup slot from the definition at the new quantity', () => {
175
+ const orderItem = convertSpecial(3, { discount: 150 }, makeExistingSpecialLine({ quantity: 2, specialDiscount: 300 }));
176
+ expect(orderItem.subtotal).toBe(2550); // 3000 - 150*3
177
+ expect(orderItem.item_discount_details?.total_discount).toBe(450);
178
+ expect(orderItem._id).toBe('existing-line');
179
+ });
180
+ it('heals legacy lines that stored the per-unit amount at quantity > 1', () => {
181
+ const orderItem = convertSpecial(2, { discount: 150 }, makeExistingSpecialLine({ quantity: 2, specialDiscount: 150 }));
182
+ expect(orderItem.subtotal).toBe(1700);
183
+ expect(orderItem.item_discount_details?.total_discount).toBe(300);
184
+ });
185
+ it('scales a manual discount slot from the previous quantity instead of rebuilding it', () => {
186
+ const orderItem = convertSpecial(3, { discount: 150 }, makeExistingSpecialLine({ quantity: 2, specialDiscount: 100, specialDiscountId: 'manual-discount' }));
187
+ expect(orderItem.subtotal).toBe(2850); // 3000 - (100 / 2 * 3)
188
+ expect(orderItem.item_discount_details?.special_discount_id).toBe('manual-discount');
189
+ expect(orderItem.item_discount_details?.total_discount).toBe(150);
190
+ });
191
+ it('rescales loyalty/voucher line amounts from the previous quantity', () => {
192
+ const orderItem = convertSpecial(3, { discount: 150 }, makeExistingSpecialLine({ quantity: 2, specialDiscount: 300, loyaltyDiscount: 200 }));
193
+ // Setup slot rebuilt at 3 units (450) + loyalty scaled 200/2*3 = 300.
194
+ expect(orderItem.item_discount_details?.special_discount).toBe(450);
195
+ expect(orderItem.item_discount_details?.loyalty_discount).toBe(300);
196
+ expect(orderItem.item_discount_details?.total_discount).toBe(750);
197
+ expect(orderItem.subtotal).toBe(2250); // 3000 - 750
198
+ });
199
+ it('applies a setup discount added after the line was created', () => {
200
+ const orderItem = convertSpecial(2, { discount: 150 }, makeExistingSpecialLine({ quantity: 2, loyaltyDiscount: 200 }));
201
+ expect(orderItem.item_discount_details?.special_discount_id).toBe('special-1');
202
+ expect(orderItem.item_discount_details?.special_discount).toBe(300);
203
+ expect(orderItem.item_discount_details?.loyalty_discount).toBe(200);
204
+ expect(orderItem.item_discount_details?.total_discount).toBe(500);
205
+ expect(orderItem.subtotal).toBe(1500); // 2000 - 500
206
+ });
207
+ it('clears a setup discount removed from the definition and keeps other slots', () => {
208
+ const orderItem = convertSpecial(2, {}, makeExistingSpecialLine({ quantity: 2, specialDiscount: 300, loyaltyDiscount: 200 }));
209
+ expect(orderItem.item_discount_details?.special_discount).toBeUndefined();
210
+ expect(orderItem.item_discount_details?.special_discount_id).toBeUndefined();
211
+ expect(orderItem.item_discount_details?.loyalty_discount).toBe(200);
212
+ expect(orderItem.item_discount_details?.total_discount).toBe(200);
213
+ expect(orderItem.subtotal).toBe(1800); // 2000 - 200
214
+ });
215
+ });
216
+ // ---------------------------------------------------------------------------
217
+ // Half-half
218
+ // ---------------------------------------------------------------------------
219
+ describe('convertHalfHalfToOrderItem: fixed discount scales with quantity', () => {
220
+ it('applies the discount once per unit at quantity 2', () => {
221
+ const orderItem = (0, halfhalf_setup_helpers_1.convertHalfHalfToOrderItem)({
222
+ halfHalfItem: makeWorkableHalfHalf({ discount: 150 }),
223
+ selectedHalfHalfItems: makeHalfHalfSelections(),
224
+ quantity: 2,
225
+ platform: PLATFORM,
226
+ });
227
+ expect(orderItem.subtotal).toBe(1900); // (100 + 500 + 500) * 2 - 300
228
+ expect(orderItem.item_discount_details?.total_discount).toBe(300);
229
+ });
230
+ it('does not scale percentage discounts', () => {
231
+ const orderItem = (0, halfhalf_setup_helpers_1.convertHalfHalfToOrderItem)({
232
+ halfHalfItem: makeWorkableHalfHalf({ discount: 10, isPercentage: true }),
233
+ selectedHalfHalfItems: makeHalfHalfSelections(),
234
+ quantity: 2,
235
+ platform: PLATFORM,
236
+ });
237
+ expect(orderItem.subtotal).toBe(1980); // 2200 - 10%
238
+ expect(orderItem.item_discount_details?.total_discount).toBe(220);
239
+ });
240
+ it('rebuilds the setup slot from the definition when editing', () => {
241
+ const orderItem = (0, halfhalf_setup_helpers_1.convertHalfHalfToOrderItem)({
242
+ halfHalfItem: makeWorkableHalfHalf({ discount: 150 }),
243
+ selectedHalfHalfItems: makeHalfHalfSelections(),
244
+ quantity: 3,
245
+ platform: PLATFORM,
246
+ existingOrderItem: makeExistingHalfHalfLine({ quantity: 2, specialDiscount: 150 }),
247
+ });
248
+ expect(orderItem.subtotal).toBe(2850); // 3300 - 450
249
+ expect(orderItem.item_discount_details?.total_discount).toBe(450);
250
+ expect(orderItem._id).toBe('existing-halfhalf-line');
251
+ });
252
+ });
253
+ describe('convertHalfHalfToOrderEngineItem: fixed discount scales with quantity', () => {
254
+ it('applies the discount once per unit at quantity 2', () => {
255
+ const engine = (0, halfhalf_setup_helpers_1.convertHalfHalfToOrderEngineItem)({
256
+ halfHalfItem: makeWorkableHalfHalf({ discount: 150 }),
257
+ selectedHalfHalfItems: makeHalfHalfSelections(),
258
+ quantity: 2,
259
+ platform: PLATFORM,
260
+ });
261
+ expect(engine.subtotal).toBe(1900);
262
+ expect(engine.item_discount_details?.total_discount).toBe(300);
263
+ });
264
+ it('does not scale percentage discounts', () => {
265
+ const engine = (0, halfhalf_setup_helpers_1.convertHalfHalfToOrderEngineItem)({
266
+ halfHalfItem: makeWorkableHalfHalf({ discount: 10, isPercentage: true }),
267
+ selectedHalfHalfItems: makeHalfHalfSelections(),
268
+ quantity: 2,
269
+ platform: PLATFORM,
270
+ });
271
+ expect(engine.subtotal).toBe(1980);
272
+ expect(engine.item_discount_details?.total_discount).toBe(220);
273
+ });
274
+ });
@@ -38,14 +38,7 @@ describe('tenant_hr_shift_schedules schema (a dated assignment)', () => {
38
38
  describe('no shift data on the assignment', () => {
39
39
  // These used to be real columns. If any of them comes back, the model has
40
40
  // regressed to the pre-thin shape and shift edits would stop propagating.
41
- const forbidden = [
42
- 'day_indexes',
43
- 'start_time',
44
- 'end_time',
45
- 'position',
46
- 'type',
47
- 'status',
48
- ];
41
+ const forbidden = ['day_indexes', 'start_time', 'end_time', 'position', 'type', 'status'];
49
42
  it.each(forbidden)('does not declare `%s`', (path) => {
50
43
  expect(tenant_hr_shift_schedules_1.TenantHrShiftScheduleSchema.path(path)).toBeUndefined();
51
44
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.4.74",
3
+ "version": "0.4.76",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",