@managesome/eposmatic-toolkit 0.4.73 → 0.4.75

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 (35) hide show
  1. package/esm/helpers/utils/halfhalf-setup-helpers.js +22 -4
  2. package/esm/helpers/utils/hr-helpers.js +0 -10
  3. package/esm/helpers/utils/setup-discount-helpers.js +79 -0
  4. package/esm/helpers/utils/specials-helpers.js +13 -1
  5. package/esm/schemas/models/tenant_hr_attendance_records.js +2 -2
  6. package/esm/schemas/models/tenant_hr_employees.js +0 -10
  7. package/esm/schemas/models/tenant_hr_shift_schedules.js +3 -38
  8. package/esm/schemas/models/tenant_hr_shifts.js +27 -13
  9. package/lib/helpers/utils/halfhalf-setup-helpers.js +22 -4
  10. package/lib/helpers/utils/hr-helpers.d.ts +0 -2
  11. package/lib/helpers/utils/hr-helpers.js +0 -10
  12. package/lib/helpers/utils/setup-discount-helpers.d.ts +29 -0
  13. package/lib/helpers/utils/setup-discount-helpers.js +82 -0
  14. package/lib/helpers/utils/specials-helpers.js +13 -1
  15. package/lib/schemas/models/tenant_hr_attendance_records.d.ts +3 -3
  16. package/lib/schemas/models/tenant_hr_attendance_records.js +2 -2
  17. package/lib/schemas/models/tenant_hr_employees.d.ts +0 -6
  18. package/lib/schemas/models/tenant_hr_employees.js +0 -10
  19. package/lib/schemas/models/tenant_hr_shift_schedules.d.ts +0 -19
  20. package/lib/schemas/models/tenant_hr_shift_schedules.js +3 -38
  21. package/lib/schemas/models/tenant_hr_shifts.d.ts +12 -30
  22. package/lib/schemas/models/tenant_hr_shifts.js +27 -13
  23. package/lib/schemas/types/tenant_hr_attendance_records.d.ts +2 -2
  24. package/lib/schemas/types/tenant_hr_employees.d.ts +1 -3
  25. package/lib/schemas/types/tenant_hr_shift_schedules.d.ts +2 -9
  26. package/lib/schemas/types/tenant_hr_shifts.d.ts +6 -6
  27. package/lib/tests/setup-quantity-discount-scaling.test.d.ts +1 -0
  28. package/lib/tests/setup-quantity-discount-scaling.test.js +274 -0
  29. package/lib/tests/tenant_hr_employees.test.d.ts +1 -0
  30. package/lib/tests/tenant_hr_employees.test.js +82 -0
  31. package/lib/tests/tenant_hr_shift_schedules.test.d.ts +1 -0
  32. package/lib/tests/tenant_hr_shift_schedules.test.js +61 -0
  33. package/lib/tests/tenant_hr_shifts.test.d.ts +1 -0
  34. package/lib/tests/tenant_hr_shifts.test.js +61 -0
  35. package/package.json +1 -1
@@ -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);
@@ -42,16 +42,6 @@ export function getHrEmployeeWithPrice(employee, writeable) {
42
42
  : {}),
43
43
  ...(employee.salary_per_hour != null ? { salary_per_hour: convert(employee.salary_per_hour) } : {}),
44
44
  ...(employee.monthly_salary != null ? { monthly_salary: convert(employee.monthly_salary) } : {}),
45
- ...(employee.hr_working_days_per_month != null
46
- ? {
47
- hr_working_days_per_month: convert(employee.hr_working_days_per_month),
48
- }
49
- : {}),
50
- ...(employee.hr_hours_per_working_day != null
51
- ? {
52
- hr_hours_per_working_day: convert(employee.hr_hours_per_working_day),
53
- }
54
- : {}),
55
45
  ...(employee.hr_overtime_multiplier != null
56
46
  ? { hr_overtime_multiplier: convert(employee.hr_overtime_multiplier) }
57
47
  : {}),
@@ -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.
@@ -54,10 +54,10 @@ export const TenantHrAttendanceRecordSchema = new Schema({
54
54
  required: true,
55
55
  match: /^\d{4}-\d{2}-\d{2}$/,
56
56
  },
57
- schedule_id: {
57
+ shift_id: {
58
58
  type: String,
59
59
  required: true,
60
- ref: 'tenant_hr_shift_schedules',
60
+ ref: 'tenant_hr_shifts',
61
61
  },
62
62
  clock_time: {
63
63
  type: [hrClockTimeSchema],
@@ -124,16 +124,6 @@ export const TenantHrEmployeeSchema = new Schema({
124
124
  type: Number,
125
125
  default: null,
126
126
  },
127
- hr_working_days_per_month: {
128
- type: Number,
129
- required: true,
130
- default: 0,
131
- },
132
- hr_hours_per_working_day: {
133
- type: Number,
134
- required: true,
135
- default: 0,
136
- },
137
127
  hr_overtime_multiplier: {
138
128
  type: Number,
139
129
  required: true,
@@ -1,5 +1,4 @@
1
1
  import { Schema } from 'mongoose';
2
- import { ALL_DAY_INDEX, ALL_HR_SHIFT_STATUS_NAMES, ALL_HR_SHIFT_TYPE_NAMES, HR_SHIFT_STATUS_ENUM, HR_SHIFT_TYPE_ENUM, } from '../../constants';
3
2
  export const TenantHrShiftScheduleSchema = new Schema({
4
3
  _id: {
5
4
  type: Schema.Types.ObjectId,
@@ -18,11 +17,10 @@ export const TenantHrShiftScheduleSchema = new Schema({
18
17
  immutable: true,
19
18
  ref: 'tenant_hr_employees',
20
19
  },
21
- day_indexes: {
22
- type: [String],
23
- enum: ALL_DAY_INDEX,
20
+ shift_id: {
21
+ type: String,
24
22
  required: true,
25
- default: [],
23
+ ref: 'tenant_hr_shifts',
26
24
  },
27
25
  start_date: {
28
26
  type: Date,
@@ -32,43 +30,10 @@ export const TenantHrShiftScheduleSchema = new Schema({
32
30
  type: Date,
33
31
  default: null,
34
32
  },
35
- start_time: {
36
- type: Number,
37
- required: true,
38
- min: 0,
39
- max: 86399,
40
- },
41
- end_time: {
42
- type: Number,
43
- required: true,
44
- min: 0,
45
- max: 86399,
46
- },
47
- position: {
48
- type: String,
49
- default: null,
50
- },
51
- type: {
52
- type: String,
53
- required: true,
54
- enum: ALL_HR_SHIFT_TYPE_NAMES,
55
- default: HR_SHIFT_TYPE_ENUM.REGULAR,
56
- },
57
- status: {
58
- type: String,
59
- required: true,
60
- enum: ALL_HR_SHIFT_STATUS_NAMES,
61
- default: HR_SHIFT_STATUS_ENUM.SCHEDULED,
62
- },
63
33
  notes: {
64
34
  type: String,
65
35
  default: null,
66
36
  },
67
- shift_id: {
68
- type: String,
69
- default: null,
70
- ref: 'tenant_hr_shifts',
71
- },
72
37
  createdAt: {
73
38
  type: Schema.Types.Date,
74
39
  required: true,
@@ -1,14 +1,5 @@
1
1
  import { Schema } from 'mongoose';
2
- const hrRequiredPositionSchema = new Schema({
3
- position: {
4
- type: String,
5
- required: true,
6
- },
7
- count: {
8
- type: Number,
9
- required: true,
10
- },
11
- }, { _id: false, minimize: false });
2
+ import { ALL_DAY_INDEX } from '../../constants';
12
3
  export const TenantHrShiftSchema = new Schema({
13
4
  _id: {
14
5
  type: Schema.Types.ObjectId,
@@ -37,13 +28,36 @@ export const TenantHrShiftSchema = new Schema({
37
28
  min: 0,
38
29
  max: 86399,
39
30
  },
31
+ break_start_time: {
32
+ type: Number,
33
+ default: null,
34
+ min: 0,
35
+ max: 86399,
36
+ },
37
+ break_end_time: {
38
+ type: Number,
39
+ default: null,
40
+ min: 0,
41
+ max: 86399,
42
+ },
43
+ working_days: {
44
+ type: [String],
45
+ enum: ALL_DAY_INDEX,
46
+ required: true,
47
+ validate: {
48
+ validator: (days) => Array.isArray(days) && days.length > 0,
49
+ message: 'working_days must contain at least one day',
50
+ },
51
+ },
40
52
  grace_period_minutes: {
41
53
  type: Number,
42
54
  default: null,
43
55
  },
44
- required_positions: {
45
- type: [hrRequiredPositionSchema],
46
- default: [],
56
+ overtime_start_time: {
57
+ type: Number,
58
+ default: null,
59
+ min: 0,
60
+ max: 86399,
47
61
  },
48
62
  is_active: {
49
63
  type: Boolean,
@@ -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);
@@ -22,8 +22,6 @@ export declare function getHrPayrollsFetchedData(data: ITenantHrPayroll[]): ITen
22
22
  export interface IHrEmployeePriceFields {
23
23
  salary_per_hour?: number | null;
24
24
  monthly_salary?: number | null;
25
- hr_working_days_per_month?: number | null;
26
- hr_hours_per_working_day?: number | null;
27
25
  hr_overtime_multiplier?: number | null;
28
26
  leave_allowance?: Partial<Record<HR_LEAVE_TYPE_ENUM, number>> | null;
29
27
  }
@@ -48,16 +48,6 @@ function getHrEmployeeWithPrice(employee, writeable) {
48
48
  : {}),
49
49
  ...(employee.salary_per_hour != null ? { salary_per_hour: convert(employee.salary_per_hour) } : {}),
50
50
  ...(employee.monthly_salary != null ? { monthly_salary: convert(employee.monthly_salary) } : {}),
51
- ...(employee.hr_working_days_per_month != null
52
- ? {
53
- hr_working_days_per_month: convert(employee.hr_working_days_per_month),
54
- }
55
- : {}),
56
- ...(employee.hr_hours_per_working_day != null
57
- ? {
58
- hr_hours_per_working_day: convert(employee.hr_hours_per_working_day),
59
- }
60
- : {}),
61
51
  ...(employee.hr_overtime_multiplier != null
62
52
  ? { hr_overtime_multiplier: convert(employee.hr_overtime_multiplier) }
63
53
  : {}),
@@ -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.
@@ -14,6 +14,7 @@ export declare const TenantHrAttendanceRecordSchema: Schema<any, import("mongoos
14
14
  tenant_id: string;
15
15
  status: HR_ATTENDANCE_STATUS_ENUM;
16
16
  employee_id: string;
17
+ shift_id: string;
17
18
  break_time: import("mongoose").Types.DocumentArray<{
18
19
  start_time: number;
19
20
  end_time: number;
@@ -25,7 +26,6 @@ export declare const TenantHrAttendanceRecordSchema: Schema<any, import("mongoos
25
26
  end_time: number;
26
27
  }>;
27
28
  attendance_date: string;
28
- schedule_id: string;
29
29
  clock_time: import("mongoose").Types.DocumentArray<{
30
30
  type: import("../../constants").HR_CLOCK_TIME_TYPE_ENUM;
31
31
  method: import("../../constants").HR_CHECK_IN_METHOD_ENUM;
@@ -51,6 +51,7 @@ export declare const TenantHrAttendanceRecordSchema: Schema<any, import("mongoos
51
51
  tenant_id: string;
52
52
  status: HR_ATTENDANCE_STATUS_ENUM;
53
53
  employee_id: string;
54
+ shift_id: string;
54
55
  break_time: import("mongoose").Types.DocumentArray<{
55
56
  start_time: number;
56
57
  end_time: number;
@@ -62,7 +63,6 @@ export declare const TenantHrAttendanceRecordSchema: Schema<any, import("mongoos
62
63
  end_time: number;
63
64
  }>;
64
65
  attendance_date: string;
65
- schedule_id: string;
66
66
  clock_time: import("mongoose").Types.DocumentArray<{
67
67
  type: import("../../constants").HR_CLOCK_TIME_TYPE_ENUM;
68
68
  method: import("../../constants").HR_CHECK_IN_METHOD_ENUM;
@@ -88,6 +88,7 @@ export declare const TenantHrAttendanceRecordSchema: Schema<any, import("mongoos
88
88
  tenant_id: string;
89
89
  status: HR_ATTENDANCE_STATUS_ENUM;
90
90
  employee_id: string;
91
+ shift_id: string;
91
92
  break_time: import("mongoose").Types.DocumentArray<{
92
93
  start_time: number;
93
94
  end_time: number;
@@ -99,7 +100,6 @@ export declare const TenantHrAttendanceRecordSchema: Schema<any, import("mongoos
99
100
  end_time: number;
100
101
  }>;
101
102
  attendance_date: string;
102
- schedule_id: string;
103
103
  clock_time: import("mongoose").Types.DocumentArray<{
104
104
  type: import("../../constants").HR_CLOCK_TIME_TYPE_ENUM;
105
105
  method: import("../../constants").HR_CHECK_IN_METHOD_ENUM;
@@ -57,10 +57,10 @@ exports.TenantHrAttendanceRecordSchema = new mongoose_1.Schema({
57
57
  required: true,
58
58
  match: /^\d{4}-\d{2}-\d{2}$/,
59
59
  },
60
- schedule_id: {
60
+ shift_id: {
61
61
  type: String,
62
62
  required: true,
63
- ref: 'tenant_hr_shift_schedules',
63
+ ref: 'tenant_hr_shifts',
64
64
  },
65
65
  clock_time: {
66
66
  type: [hrClockTimeSchema],
@@ -25,8 +25,6 @@ export declare const TenantHrEmployeeSchema: Schema<any, import("mongoose").Mode
25
25
  monthly_salary: number;
26
26
  pay_type: HR_PAY_TYPE_ENUM;
27
27
  leave_allowance: Map<string, number>;
28
- hr_working_days_per_month: number;
29
- hr_hours_per_working_day: number;
30
28
  hr_overtime_multiplier: number;
31
29
  staff_auth_code: string;
32
30
  address: string;
@@ -351,8 +349,6 @@ export declare const TenantHrEmployeeSchema: Schema<any, import("mongoose").Mode
351
349
  monthly_salary: number;
352
350
  pay_type: HR_PAY_TYPE_ENUM;
353
351
  leave_allowance: Map<string, number>;
354
- hr_working_days_per_month: number;
355
- hr_hours_per_working_day: number;
356
352
  hr_overtime_multiplier: number;
357
353
  staff_auth_code: string;
358
354
  address: string;
@@ -677,8 +673,6 @@ export declare const TenantHrEmployeeSchema: Schema<any, import("mongoose").Mode
677
673
  monthly_salary: number;
678
674
  pay_type: HR_PAY_TYPE_ENUM;
679
675
  leave_allowance: Map<string, number>;
680
- hr_working_days_per_month: number;
681
- hr_hours_per_working_day: number;
682
676
  hr_overtime_multiplier: number;
683
677
  staff_auth_code: string;
684
678
  address: string;