@managesome/eposmatic-toolkit 0.4.89 → 0.4.90

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 (34) hide show
  1. package/esm/helpers/order/generate-warehouse-internal-movement.js +3 -0
  2. package/esm/helpers/store/common-warehouse-internal-movement-actions.js +124 -4
  3. package/esm/helpers/utils/discount-amounts.js +64 -0
  4. package/esm/helpers/utils/index.js +2 -0
  5. package/esm/helpers/utils/internal-movement-items.js +30 -0
  6. package/esm/helpers/utils/tenant-purchase-order-engine.js +10 -18
  7. package/esm/helpers/utils/warehouse-purchase-order-engine.js +10 -18
  8. package/esm/schemas/models/warehouse_internal_movements.js +54 -0
  9. package/lib/helpers/order/generate-warehouse-internal-movement.js +3 -0
  10. package/lib/helpers/store/common-warehouse-internal-movement-actions.d.ts +33 -1
  11. package/lib/helpers/store/common-warehouse-internal-movement-actions.js +126 -3
  12. package/lib/helpers/utils/discount-amounts.d.ts +50 -0
  13. package/lib/helpers/utils/discount-amounts.js +69 -0
  14. package/lib/helpers/utils/index.d.ts +2 -0
  15. package/lib/helpers/utils/index.js +2 -0
  16. package/lib/helpers/utils/internal-movement-items.d.ts +35 -0
  17. package/lib/helpers/utils/internal-movement-items.js +36 -0
  18. package/lib/helpers/utils/tenant-purchase-order-engine.js +9 -17
  19. package/lib/helpers/utils/warehouse-purchase-order-engine.js +9 -17
  20. package/lib/schemas/models/inventories.d.ts +3 -3
  21. package/lib/schemas/models/inventory_transactions.d.ts +3 -3
  22. package/lib/schemas/models/item_transactions.d.ts +3 -3
  23. package/lib/schemas/models/materialized_tenant_discount_entries.d.ts +3 -3
  24. package/lib/schemas/models/materialized_tenant_prep_time_entries.d.ts +3 -3
  25. package/lib/schemas/models/orders.d.ts +120 -120
  26. package/lib/schemas/models/tenant_inventory_department_transactions.d.ts +3 -3
  27. package/lib/schemas/models/tenant_inventory_department_utilities.d.ts +3 -3
  28. package/lib/schemas/models/warehouse_internal_movements.d.ts +570 -0
  29. package/lib/schemas/models/warehouse_internal_movements.js +54 -0
  30. package/lib/schemas/models/warehouse_inventory_transactions.d.ts +3 -3
  31. package/lib/schemas/models/warehouse_item_transactions.d.ts +3 -3
  32. package/lib/schemas/models/warehouse_items.d.ts +3 -3
  33. package/lib/schemas/types/warehouse_internal_movements.d.ts +17 -1
  34. package/package.json +1 -1
@@ -17,6 +17,9 @@ export function generateWarehouseInternalMovementIteration(movement, partial = t
17
17
  is_gst_inclusive: movement.is_gst_inclusive,
18
18
  gst: movement.gst,
19
19
  gst_amount: movement.gst_amount,
20
+ order_discount_details: movement.order_discount_details,
21
+ total_discount_applied: movement.total_discount_applied,
22
+ exclude_discount_from_gst: movement.exclude_discount_from_gst,
20
23
  labor_charges: movement.labor_charges,
21
24
  labor_charges_amount: movement.labor_charges_amount,
22
25
  is_labor_charges_percentage: movement.is_labor_charges_percentage,
@@ -1,5 +1,31 @@
1
- import { convertDecimalToInt, convertIntToDecimal, getCardChargesAndAmountCollected, getPercentageAmount, parseFloatCeil, parseNum, } from '../utils';
1
+ import { convertDecimalToInt, convertIntToDecimal, getCardChargesAndAmountCollected, getPercentageAmount, parseFloatCeil, parseNum, getItemDiscountAmount, getOrderDiscountAmount, withDiscountTotal, } from '../utils';
2
2
  import { getOrderPaymentsCardDetailsWithPrice } from './common-order-actions';
3
+ /**
4
+ * Resolved money value of one movement line's discount.
5
+ *
6
+ * Mirrors `getWarehousePurchaseOrderItemDiscountAmount`: a flat
7
+ * `special_discount` is PER UNIT (so it scales with quantity) and a percentage
8
+ * applies to the line's gross value. The result is clamped to the line total.
9
+ */
10
+ export function getWarehouseInternalMovementItemDiscountAmount(item) {
11
+ // Shared rule with warehouse/tenant POs; this wrapper keeps the movement
12
+ // cart's currency-decimal rounding.
13
+ return parseNum(getItemDiscountAmount({
14
+ grossAmount: parseNum((item.unit_price ?? 0) * (item.quantity ?? 0)),
15
+ quantity: item.quantity,
16
+ details: item.item_discount_details,
17
+ }));
18
+ }
19
+ /**
20
+ * Refreshes `total_discount` on a line's discount record so it matches the
21
+ * line's current price/quantity, preserving every other field.
22
+ */
23
+ export function withResolvedWarehouseInternalMovementItemDiscountTotal(item) {
24
+ const details = item.item_discount_details;
25
+ if (!details)
26
+ return null;
27
+ return withDiscountTotal(details, getWarehouseInternalMovementItemDiscountAmount(item));
28
+ }
3
29
  /**
4
30
  * Convert unit_price on every internal movement item.
5
31
  */
@@ -9,8 +35,27 @@ export function getWarehouseInternalMovementItemsWithPrice(items, convertToPrice
9
35
  return items.map((item) => ({
10
36
  ...item,
11
37
  unit_price: item.unit_price != null ? convertToPrice(item.unit_price) : undefined,
38
+ ...(item.item_discount_details != null
39
+ ? {
40
+ item_discount_details: getWarehouseInternalMovementItemDiscountDetailsWithPrice(item.item_discount_details, convertToPrice),
41
+ }
42
+ : {}),
12
43
  }));
13
44
  }
45
+ /**
46
+ * Scale a line discount record. A percentage is not a price, so only the fixed
47
+ * per-unit amount and the resolved total are converted.
48
+ */
49
+ function getWarehouseInternalMovementItemDiscountDetailsWithPrice(discount, convertToPrice) {
50
+ const { special_discount, is_special_discount_percentage, total_discount } = discount;
51
+ return {
52
+ ...discount,
53
+ ...(typeof special_discount === 'number' && !is_special_discount_percentage
54
+ ? { special_discount: convertToPrice(special_discount) }
55
+ : {}),
56
+ total_discount: convertToPrice(total_discount ?? 0),
57
+ };
58
+ }
14
59
  /**
15
60
  * Convert monetary fields on payments array.
16
61
  */
@@ -33,6 +78,22 @@ export function getWarehouseInternalMovementIterationsWithPrice(iterations, writ
33
78
  return [];
34
79
  return iterations.map((iteration) => getWarehouseInternalMovementWithPrice(iteration, writeable));
35
80
  }
81
+ /**
82
+ * Convert the order-level discount record. A percentage is not a price, so only
83
+ * the resolved money value (and a fixed `special_discount`) are scaled.
84
+ */
85
+ function getWarehouseInternalMovementDiscountDetailsWithPrice(discount, convertToPrice) {
86
+ if (!discount)
87
+ return null;
88
+ const { special_discount, is_special_discount_percentage, total_discount } = discount;
89
+ return {
90
+ ...discount,
91
+ ...(typeof special_discount === 'number' && !is_special_discount_percentage
92
+ ? { special_discount: convertToPrice(special_discount) }
93
+ : {}),
94
+ total_discount: convertToPrice(total_discount ?? 0),
95
+ };
96
+ }
36
97
  /**
37
98
  * Convert all monetary fields on an internal movement.
38
99
  *
@@ -53,6 +114,14 @@ export function getWarehouseInternalMovementWithPrice(order, writeable) {
53
114
  ...(order.amount_collected != null ? { amount_collected: convertToPrice(order.amount_collected) } : {}),
54
115
  ...(order.amount_earned != null ? { amount_earned: convertToPrice(order.amount_earned) } : {}),
55
116
  ...(order.gst != null ? { gst: convertToPrice(order.gst) } : {}),
117
+ ...(order.order_discount_details != null
118
+ ? {
119
+ order_discount_details: getWarehouseInternalMovementDiscountDetailsWithPrice(order.order_discount_details, convertToPrice),
120
+ }
121
+ : {}),
122
+ ...(order.total_discount_applied != null
123
+ ? { total_discount_applied: convertToPrice(order.total_discount_applied) }
124
+ : {}),
56
125
  ...(order.labor_charges != null && order.is_labor_charges_percentage !== true
57
126
  ? { labor_charges: convertToPrice(order.labor_charges) }
58
127
  : {}),
@@ -75,25 +144,70 @@ export function getWarehouseInternalMovementWithPrice(order, writeable) {
75
144
  : {}),
76
145
  };
77
146
  }
147
+ /**
148
+ * Resolve the order-level discount of an internal movement against its items
149
+ * subtotal. Mirrors `getWarehousePurchaseOrderOrderDiscountDetails`.
150
+ *
151
+ * `special_discount` is a percentage when `is_special_discount_percentage` is
152
+ * true and a flat money amount otherwise. The resolved value is clamped to the
153
+ * subtotal, so a movement can never go negative.
154
+ */
155
+ export function getWarehouseInternalMovementOrderDiscountDetails(discountDetails, baseAmount) {
156
+ if (!discountDetails) {
157
+ return { order_discount_details: null, total_discount_applied: 0 };
158
+ }
159
+ const totalDiscount = parseNum(getOrderDiscountAmount({
160
+ baseAmount,
161
+ details: discountDetails,
162
+ }));
163
+ if (totalDiscount <= 0) {
164
+ return { order_discount_details: null, total_discount_applied: 0 };
165
+ }
166
+ return {
167
+ order_discount_details: withDiscountTotal(discountDetails, totalDiscount),
168
+ total_discount_applied: totalDiscount,
169
+ };
170
+ }
78
171
  /**
79
172
  * Recalculate financial totals for an internal movement — mirrors
80
173
  * `calculateCartWarehousePurchaseOrderInfoAndSyncData` but for the flat
81
174
  * `items` array instead of nested `categories`.
175
+ *
176
+ * Each line's item discount is taken off first, then the order-level discount
177
+ * is applied to the resulting subtotal. GST and percentage charges are
178
+ * calculated on the discounted value. When `exclude_discount_from_gst` is true
179
+ * the order discount is taken off after GST instead.
82
180
  */
83
181
  export function calculateCartInternalMovementInfoAndSyncData(data) {
84
- const subtotal = (data.items ?? []).reduce((sum, item) => sum + parseNum((item.unit_price ?? 0) * (item.quantity ?? 0)), 0);
182
+ let subtotal = 0;
183
+ const items = (data.items ?? []).map((item) => {
184
+ const grossAmount = parseNum((item.unit_price ?? 0) * (item.quantity ?? 0));
185
+ const itemDiscountAmount = getWarehouseInternalMovementItemDiscountAmount(item);
186
+ subtotal += parseNum(Math.max(0, grossAmount - itemDiscountAmount));
187
+ return {
188
+ ...item,
189
+ item_discount_details: withResolvedWarehouseInternalMovementItemDiscountTotal(item),
190
+ };
191
+ });
192
+ subtotal = parseNum(subtotal);
193
+ const { order_discount_details, total_discount_applied } = getWarehouseInternalMovementOrderDiscountDetails(data.order_discount_details, subtotal);
194
+ const applyGSTOnOriginalPrice = data.exclude_discount_from_gst === true;
85
195
  let grand_total = subtotal;
196
+ if (!applyGSTOnOriginalPrice) {
197
+ grand_total -= total_discount_applied;
198
+ }
199
+ const postDiscountSubtotal = parseNum(Math.max(0, subtotal - total_discount_applied));
86
200
  let gst_amount = 0;
87
201
  const gst = data.gst ?? 0;
88
202
  // Order-level charges (labor, freight): entered per movement, either a fixed
89
- // amount or a percentage of the items subtotal. `is_*_charges_taxable`
203
+ // amount or a percentage of the post-discount items subtotal. `is_*_charges_taxable`
90
204
  // (snapshot of the warehouse setting) decides the placement: taxable => added
91
205
  // BEFORE GST, untaxed => added after it.
92
206
  const getOrderCharge = (value, isPercentage) => {
93
207
  const entered = parseNum(Math.max(0, value ?? 0));
94
208
  return {
95
209
  entered,
96
- amount: isPercentage ? parseNum(getPercentageAmount(subtotal, entered)) : entered,
210
+ amount: isPercentage ? parseNum(getPercentageAmount(postDiscountSubtotal, entered)) : entered,
97
211
  };
98
212
  };
99
213
  const labor = getOrderCharge(data.labor_charges, data.is_labor_charges_percentage);
@@ -115,6 +229,9 @@ export function calculateCartInternalMovementInfoAndSyncData(data) {
115
229
  gst_amount = getPercentageAmount(grand_total, gst);
116
230
  grand_total += gst_amount;
117
231
  }
232
+ if (applyGSTOnOriginalPrice) {
233
+ grand_total -= total_discount_applied;
234
+ }
118
235
  const untaxedCharges = (isLaborTaxable ? 0 : labor_charges_amount) + (isFreightTaxable ? 0 : freight_charges_amount);
119
236
  if (untaxedCharges > 0) {
120
237
  grand_total += untaxedCharges;
@@ -124,8 +241,11 @@ export function calculateCartInternalMovementInfoAndSyncData(data) {
124
241
  grand_total = parseNum(grand_total);
125
242
  return {
126
243
  ...data,
244
+ items,
127
245
  grand_total,
128
246
  gst_amount,
247
+ order_discount_details,
248
+ total_discount_applied,
129
249
  labor_charges,
130
250
  labor_charges_amount,
131
251
  freight_charges,
@@ -0,0 +1,64 @@
1
+ const isFiniteNumber = (value) => typeof value === 'number' && Number.isFinite(value);
2
+ const clamp = (value, min, max) => Math.min(max, Math.max(min, value));
3
+ /**
4
+ * Resolve one line's item discount to a money amount.
5
+ *
6
+ * This is the single source of truth for the rule shared by warehouse POs,
7
+ * tenant POs and internal movements:
8
+ * - a fixed `special_discount` is **per unit**, so it scales with quantity;
9
+ * - a percentage applies to the line's gross value;
10
+ * - `total_discount` is a fallback for records without a special discount;
11
+ * - the result is clamped to `[0, gross]`, so a line can never go negative.
12
+ *
13
+ * The function is deliberately rounding-free: callers decide whether they work
14
+ * in currency decimals (`parseNum`) or warehouse wire units (integer maths).
15
+ * Invalid/NaN/Infinity input resolves to `0` instead of poisoning totals.
16
+ */
17
+ export function getItemDiscountAmount({ grossAmount, quantity, details }) {
18
+ if (!details)
19
+ return 0;
20
+ const safeGrossAmount = isFiniteNumber(grossAmount) ? Math.max(0, grossAmount) : 0;
21
+ const safeQuantity = isFiniteNumber(quantity) ? Math.max(0, quantity) : 0;
22
+ if (!(safeGrossAmount > 0))
23
+ return 0;
24
+ const value = details.special_discount;
25
+ if (isFiniteNumber(value) && value > 0) {
26
+ const rawAmount = details.is_special_discount_percentage
27
+ ? (safeGrossAmount * clamp(value, 0, 100)) / 100
28
+ : value * safeQuantity;
29
+ return clamp(rawAmount, 0, safeGrossAmount);
30
+ }
31
+ const fallback = details.total_discount;
32
+ return isFiniteNumber(fallback) ? clamp(fallback, 0, safeGrossAmount) : 0;
33
+ }
34
+ /**
35
+ * Resolve an order-level discount to a money amount.
36
+ *
37
+ * Same value rules as `getItemDiscountAmount` (except a fixed amount is a
38
+ * one-off order amount, not per unit). The result is clamped to the subtotal.
39
+ * Rounding is left to the caller.
40
+ */
41
+ export function getOrderDiscountAmount({ baseAmount, details }) {
42
+ if (!details)
43
+ return 0;
44
+ const base = isFiniteNumber(baseAmount) ? Math.max(0, baseAmount) : 0;
45
+ if (!(base > 0))
46
+ return 0;
47
+ const value = details.special_discount;
48
+ if (isFiniteNumber(value) && value > 0) {
49
+ const rawAmount = details.is_special_discount_percentage ? (base * clamp(value, 0, 100)) / 100 : value;
50
+ return clamp(rawAmount, 0, base);
51
+ }
52
+ const fallback = details.total_discount;
53
+ return isFiniteNumber(fallback) ? clamp(fallback, 0, base) : 0;
54
+ }
55
+ /**
56
+ * Return a copy of a discount record with its resolved money value refreshed.
57
+ * Preserves every other field (id, name, reason, reference).
58
+ */
59
+ export function withDiscountTotal(details, totalDiscount) {
60
+ return {
61
+ ...details,
62
+ total_discount: totalDiscount,
63
+ };
64
+ }
@@ -26,6 +26,7 @@ export * from './image-helpers';
26
26
  export * from './image-url-helpers';
27
27
  export * from './ingredients';
28
28
  export * from './internal-amount-helpers';
29
+ export * from './internal-movement-items';
29
30
  export * from './inventory';
30
31
  export * from './inventory_entries';
31
32
  export * from './inventory_transactions';
@@ -33,6 +34,7 @@ export * from './item-report-helpers';
33
34
  export * from './items-comparison-report-helpers';
34
35
  export * from './kds-helpers';
35
36
  export * from './location-helpers';
37
+ export * from './discount-amounts';
36
38
  export * from './menu-helpers';
37
39
  export * from './minified-catalog';
38
40
  export * from './offer-apply-helpers';
@@ -0,0 +1,30 @@
1
+ import ObjectID from 'bson-objectid';
2
+ /**
3
+ * Convert a cart line to the document line shape.
4
+ *
5
+ * This is the single mapper used by cart summaries and by both submit flows, so
6
+ * a field the pricing engine reads (notably `item_discount_details`) cannot be
7
+ * dropped in one place and kept in another.
8
+ *
9
+ * `item_discount_details` is always present (`null` when absent): a full
10
+ * document edit must be able to clear a discount by sending an explicit null.
11
+ * Partial updates that omit the field are handled separately by the backend.
12
+ */
13
+ export function buildInternalMovementItem(line, options) {
14
+ const parsedQuantity = Number(line.quantity);
15
+ const quantity = Number.isFinite(parsedQuantity) ? Math.max(0, parsedQuantity) : 0;
16
+ const parsedUnitPrice = line.unit_price != null ? Number(line.unit_price) : undefined;
17
+ const unitPrice = parsedUnitPrice != null && Number.isFinite(parsedUnitPrice) ? parsedUnitPrice : undefined;
18
+ const notes = line.notes?.trim();
19
+ return {
20
+ _id: line._id?.trim() || ObjectID().toHexString(),
21
+ item_id: line.item_id,
22
+ inventory_code: line.inventory_code?.trim() || options?.inventoryCodeFallback?.trim() || line.item_id,
23
+ item_name: line.name,
24
+ quantity,
25
+ received_qty: 0,
26
+ ...(unitPrice != null ? { unit_price: unitPrice } : {}),
27
+ item_discount_details: line.item_discount_details ?? null,
28
+ ...(notes ? { notes } : {}),
29
+ };
30
+ }
@@ -1,4 +1,5 @@
1
- import { getPercentageAmount, parseNum } from './common';
1
+ import { parseNum } from './common';
2
+ import { getItemDiscountAmount, withDiscountTotal } from './discount-amounts';
2
3
  // ==========================================================><====================================================
3
4
  /**
4
5
  * Total item-level discount for a tenant purchase order line, in money.
@@ -16,19 +17,13 @@ import { getPercentageAmount, parseNum } from './common';
16
17
  * a line negative.
17
18
  */
18
19
  export function getTenantPurchaseOrderItemDiscountAmount(orderItem) {
19
- const details = orderItem.item_discount_details;
20
- if (!details)
21
- return 0;
22
- const quantity = orderItem.quantity ?? 0;
23
- const grossAmount = parseNum((orderItem.price ?? 0) * quantity);
24
- const value = details.special_discount;
25
- if (typeof value !== 'number' || !(value > 0)) {
26
- return parseNum(Math.max(0, Math.min(grossAmount, details.total_discount ?? 0)));
27
- }
28
- const discountAmount = details.is_special_discount_percentage
29
- ? getPercentageAmount(grossAmount, value)
30
- : value * quantity;
31
- return parseNum(Math.min(grossAmount, Math.max(0, discountAmount)));
20
+ // Shared rule with warehouse POs and internal movements; this wrapper keeps
21
+ // the tenant PO's currency-decimal rounding.
22
+ return parseNum(getItemDiscountAmount({
23
+ grossAmount: parseNum((orderItem.price ?? 0) * (orderItem.quantity ?? 0)),
24
+ quantity: orderItem.quantity,
25
+ details: orderItem.item_discount_details,
26
+ }));
32
27
  }
33
28
  /**
34
29
  * Refreshes `total_discount` on a line's discount record so the amount it
@@ -40,8 +35,5 @@ export function withResolvedTenantItemDiscountTotal(orderItem) {
40
35
  const details = orderItem.item_discount_details;
41
36
  if (!details)
42
37
  return null;
43
- return {
44
- ...details,
45
- total_discount: getTenantPurchaseOrderItemDiscountAmount(orderItem),
46
- };
38
+ return withDiscountTotal(details, getTenantPurchaseOrderItemDiscountAmount(orderItem));
47
39
  }
@@ -1,5 +1,6 @@
1
1
  import ObjectID from 'bson-objectid';
2
- import { getPercentageAmount, parseNum } from './common';
2
+ import { parseNum } from './common';
3
+ import { getItemDiscountAmount, withDiscountTotal } from './discount-amounts';
3
4
  // ==========================================================><====================================================
4
5
  /**
5
6
  * Total item-level discount for a purchase order line, in money.
@@ -16,19 +17,13 @@ import { getPercentageAmount, parseNum } from './common';
16
17
  * line negative.
17
18
  */
18
19
  export function getWarehousePurchaseOrderItemDiscountAmount(orderItem) {
19
- const details = orderItem.item_discount_details;
20
- if (!details)
21
- return 0;
22
- const quantity = orderItem.quantity ?? 0;
23
- const grossAmount = parseNum((orderItem.price ?? 0) * quantity);
24
- const value = details.special_discount;
25
- if (typeof value !== 'number' || !(value > 0)) {
26
- return parseNum(Math.max(0, Math.min(grossAmount, details.total_discount ?? 0)));
27
- }
28
- const discountAmount = details.is_special_discount_percentage
29
- ? getPercentageAmount(grossAmount, value)
30
- : value * quantity;
31
- return parseNum(Math.min(grossAmount, Math.max(0, discountAmount)));
20
+ // Shared rule with tenant POs and internal movements; this wrapper keeps the
21
+ // warehouse PO's currency-decimal rounding.
22
+ return parseNum(getItemDiscountAmount({
23
+ grossAmount: parseNum((orderItem.price ?? 0) * (orderItem.quantity ?? 0)),
24
+ quantity: orderItem.quantity,
25
+ details: orderItem.item_discount_details,
26
+ }));
32
27
  }
33
28
  /**
34
29
  * Refreshes `total_discount` on a line's discount record so the amount it
@@ -40,10 +35,7 @@ export function withResolvedWarehouseItemDiscountTotal(orderItem) {
40
35
  const details = orderItem.item_discount_details;
41
36
  if (!details)
42
37
  return null;
43
- return {
44
- ...details,
45
- total_discount: getWarehousePurchaseOrderItemDiscountAmount(orderItem),
46
- };
38
+ return withDiscountTotal(details, getWarehousePurchaseOrderItemDiscountAmount(orderItem));
47
39
  }
48
40
  export function convertWarehouseItemToPurchaseOrderItem({ vendorId, item, orderItem, }) {
49
41
  const { _id = ObjectID().toHexString(), price = item.purchase_price?.[vendorId] ?? item.purchase_price?.BASE_PRICE ?? 0, quantity = 1, special_instructions = '', is_ready, } = orderItem ?? {};
@@ -16,6 +16,30 @@ const WarehouseInternalMovementActorSchema = new Schema({
16
16
  _id: false,
17
17
  minimize: false,
18
18
  });
19
+ const WarehouseInternalMovementDiscountDetailsSchema = new Schema({
20
+ special_discount: {
21
+ type: Number,
22
+ },
23
+ special_discount_id: {
24
+ type: String,
25
+ },
26
+ special_discount_name: {
27
+ type: String,
28
+ },
29
+ is_special_discount_percentage: {
30
+ type: Boolean,
31
+ },
32
+ special_discount_reason: {
33
+ type: String,
34
+ },
35
+ special_reference_number: {
36
+ type: String,
37
+ },
38
+ total_discount: {
39
+ type: Number,
40
+ required: true,
41
+ },
42
+ }, { _id: false, minimize: false });
19
43
  const WarehouseInternalMovementItemSchema = new Schema({
20
44
  _id: {
21
45
  type: Schema.Types.ObjectId,
@@ -57,6 +81,10 @@ const WarehouseInternalMovementItemSchema = new Schema({
57
81
  min: 0,
58
82
  required: false,
59
83
  },
84
+ item_discount_details: {
85
+ type: WarehouseInternalMovementDiscountDetailsSchema,
86
+ default: null,
87
+ },
60
88
  notes: {
61
89
  type: String,
62
90
  required: false,
@@ -120,6 +148,19 @@ export const WarehouseInternalMovementIterationSchema = new Schema({
120
148
  min: 0,
121
149
  required: false,
122
150
  },
151
+ order_discount_details: {
152
+ type: WarehouseInternalMovementDiscountDetailsSchema,
153
+ required: false,
154
+ },
155
+ total_discount_applied: {
156
+ type: Number,
157
+ min: 0,
158
+ required: false,
159
+ },
160
+ exclude_discount_from_gst: {
161
+ type: Boolean,
162
+ required: false,
163
+ },
123
164
  labor_charges: { type: Number, min: 0, required: false },
124
165
  labor_charges_amount: { type: Number, min: 0, required: false },
125
166
  is_labor_charges_percentage: { type: Boolean, required: false },
@@ -266,6 +307,19 @@ export const WarehouseInternalMovementsSchema = new Schema({
266
307
  min: 0,
267
308
  required: false,
268
309
  },
310
+ order_discount_details: {
311
+ type: WarehouseInternalMovementDiscountDetailsSchema,
312
+ required: false,
313
+ },
314
+ total_discount_applied: {
315
+ type: Number,
316
+ min: 0,
317
+ required: false,
318
+ },
319
+ exclude_discount_from_gst: {
320
+ type: Boolean,
321
+ required: false,
322
+ },
269
323
  labor_charges: { type: Number, min: 0, required: false },
270
324
  labor_charges_amount: { type: Number, min: 0, required: false },
271
325
  is_labor_charges_percentage: { type: Boolean, required: false },
@@ -24,6 +24,9 @@ function generateWarehouseInternalMovementIteration(movement, partial = true) {
24
24
  is_gst_inclusive: movement.is_gst_inclusive,
25
25
  gst: movement.gst,
26
26
  gst_amount: movement.gst_amount,
27
+ order_discount_details: movement.order_discount_details,
28
+ total_discount_applied: movement.total_discount_applied,
29
+ exclude_discount_from_gst: movement.exclude_discount_from_gst,
27
30
  labor_charges: movement.labor_charges,
28
31
  labor_charges_amount: movement.labor_charges_amount,
29
32
  is_labor_charges_percentage: movement.is_labor_charges_percentage,
@@ -1,4 +1,17 @@
1
- import { IWarehouseInternalMovement, IWarehouseInternalMovementItem, IWarehouseInternalMovementIteration } from '../../schemas';
1
+ import { IDiscountDetails, IWarehouseInternalMovement, IWarehouseInternalMovementDiscountDetails, IWarehouseInternalMovementItem, IWarehouseInternalMovementIteration } from '../../schemas';
2
+ /**
3
+ * Resolved money value of one movement line's discount.
4
+ *
5
+ * Mirrors `getWarehousePurchaseOrderItemDiscountAmount`: a flat
6
+ * `special_discount` is PER UNIT (so it scales with quantity) and a percentage
7
+ * applies to the line's gross value. The result is clamped to the line total.
8
+ */
9
+ export declare function getWarehouseInternalMovementItemDiscountAmount(item: Pick<IWarehouseInternalMovementItem, 'unit_price' | 'quantity' | 'item_discount_details'>): number;
10
+ /**
11
+ * Refreshes `total_discount` on a line's discount record so it matches the
12
+ * line's current price/quantity, preserving every other field.
13
+ */
14
+ export declare function withResolvedWarehouseInternalMovementItemDiscountTotal(item: Pick<IWarehouseInternalMovementItem, 'unit_price' | 'quantity' | 'item_discount_details'>): IDiscountDetails | null;
2
15
  /**
3
16
  * Convert unit_price on every internal movement item.
4
17
  */
@@ -14,9 +27,28 @@ export declare function getWarehouseInternalMovementIterationsWithPrice(iteratio
14
27
  * - `writeable = false` → cents → decimal (after receiving from backend)
15
28
  */
16
29
  export declare function getWarehouseInternalMovementWithPrice<T extends Partial<IWarehouseInternalMovement> | IWarehouseInternalMovement | IWarehouseInternalMovementIteration>(order: T, writeable?: boolean): T;
30
+ interface IInternalMovementOrderDiscountResult {
31
+ order_discount_details: IWarehouseInternalMovementDiscountDetails | null;
32
+ total_discount_applied: number;
33
+ }
34
+ /**
35
+ * Resolve the order-level discount of an internal movement against its items
36
+ * subtotal. Mirrors `getWarehousePurchaseOrderOrderDiscountDetails`.
37
+ *
38
+ * `special_discount` is a percentage when `is_special_discount_percentage` is
39
+ * true and a flat money amount otherwise. The resolved value is clamped to the
40
+ * subtotal, so a movement can never go negative.
41
+ */
42
+ export declare function getWarehouseInternalMovementOrderDiscountDetails(discountDetails: Partial<IWarehouseInternalMovementDiscountDetails> | null | undefined, baseAmount: number): IInternalMovementOrderDiscountResult;
17
43
  /**
18
44
  * Recalculate financial totals for an internal movement — mirrors
19
45
  * `calculateCartWarehousePurchaseOrderInfoAndSyncData` but for the flat
20
46
  * `items` array instead of nested `categories`.
47
+ *
48
+ * Each line's item discount is taken off first, then the order-level discount
49
+ * is applied to the resulting subtotal. GST and percentage charges are
50
+ * calculated on the discounted value. When `exclude_discount_from_gst` is true
51
+ * the order discount is taken off after GST instead.
21
52
  */
22
53
  export declare function calculateCartInternalMovementInfoAndSyncData<T extends Partial<IWarehouseInternalMovement> | IWarehouseInternalMovement>(data: T): T;
54
+ export {};