@managesome/eposmatic-toolkit 0.4.63 → 0.4.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/helpers/store/common-tenant-purchase-order-actions.js +12 -0
- package/esm/helpers/store/common-warehouse-purchase-order-actions.js +20 -0
- package/esm/helpers/store/tenant-purchase-orders-cart-actions.js +24 -7
- package/esm/helpers/store/warehouse-purchase-orders-cart-actions.js +23 -10
- package/esm/helpers/utils/index.js +1 -0
- package/esm/helpers/utils/tenant-purchase-order-engine.js +47 -0
- package/esm/helpers/utils/warehouse-purchase-order-engine.js +63 -4
- package/esm/schemas/models/tenant_purchase_orders.js +4 -0
- package/esm/schemas/models/warehouse_purchase_orders.js +4 -0
- package/lib/helpers/store/common-tenant-purchase-order-actions.d.ts +6 -1
- package/lib/helpers/store/common-tenant-purchase-order-actions.js +13 -0
- package/lib/helpers/store/common-warehouse-purchase-order-actions.js +20 -0
- package/lib/helpers/store/tenant-purchase-orders-cart-actions.js +23 -6
- package/lib/helpers/store/warehouse-purchase-orders-cart-actions.js +23 -10
- package/lib/helpers/utils/index.d.ts +1 -0
- package/lib/helpers/utils/index.js +1 -0
- package/lib/helpers/utils/tenant-purchase-order-engine.d.ts +24 -0
- package/lib/helpers/utils/tenant-purchase-order-engine.js +51 -0
- package/lib/helpers/utils/warehouse-purchase-order-engine.d.ts +23 -1
- package/lib/helpers/utils/warehouse-purchase-order-engine.js +65 -4
- package/lib/schemas/models/tenant_purchase_orders.d.ts +972 -0
- package/lib/schemas/models/tenant_purchase_orders.js +4 -0
- package/lib/schemas/models/warehouse_purchase_orders.d.ts +972 -0
- package/lib/schemas/models/warehouse_purchase_orders.js +4 -0
- package/lib/schemas/types/tenant_purchase_orders.d.ts +2 -1
- package/lib/schemas/types/warehouse_purchase_orders.d.ts +2 -1
- package/package.json +1 -1
|
@@ -93,6 +93,7 @@ __exportStar(require("./tenant-cash-drawers-helpers"), exports);
|
|
|
93
93
|
__exportStar(require("./tenant-cash-in-out-helpers"), exports);
|
|
94
94
|
__exportStar(require("./tenant-day-sessions-helpers"), exports);
|
|
95
95
|
__exportStar(require("./tenant-instore-shift-sessions-helpers"), exports);
|
|
96
|
+
__exportStar(require("./tenant-purchase-order-engine"), exports);
|
|
96
97
|
__exportStar(require("./tenant-staff-helpers"), exports);
|
|
97
98
|
__exportStar(require("./tenant_cash_in_hand_transactions"), exports);
|
|
98
99
|
__exportStar(require("./time-helpers"), exports);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { IDiscountDetails, ITenantPurchaseOrderItem } from '../../schemas';
|
|
2
|
+
/**
|
|
3
|
+
* Total item-level discount for a tenant purchase order line, in money.
|
|
4
|
+
*
|
|
5
|
+
* The discount is applied per cart line through the cart's "Item Discount"
|
|
6
|
+
* action and lives in `item_discount_details` — the same record an order item
|
|
7
|
+
* uses:
|
|
8
|
+
* - `special_discount` is a **per-unit** amount when fixed, so the discount
|
|
9
|
+
* scales with quantity, and a percentage of the line's gross when
|
|
10
|
+
* `is_special_discount_percentage` is set.
|
|
11
|
+
* - `total_discount` is a fallback for records written without a
|
|
12
|
+
* `special_discount`.
|
|
13
|
+
*
|
|
14
|
+
* The result is clamped to the line's gross value so a discount can never make
|
|
15
|
+
* a line negative.
|
|
16
|
+
*/
|
|
17
|
+
export declare function getTenantPurchaseOrderItemDiscountAmount(orderItem: Pick<ITenantPurchaseOrderItem, 'price' | 'quantity' | 'item_discount_details'>): number;
|
|
18
|
+
/**
|
|
19
|
+
* Refreshes `total_discount` on a line's discount record so the amount it
|
|
20
|
+
* reports matches the line's current price/quantity. Preserves every other
|
|
21
|
+
* field (id, name, reason, reference) and returns null when there is no
|
|
22
|
+
* discount.
|
|
23
|
+
*/
|
|
24
|
+
export declare function withResolvedTenantItemDiscountTotal(orderItem: Pick<ITenantPurchaseOrderItem, 'price' | 'quantity' | 'item_discount_details'>): IDiscountDetails | null;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTenantPurchaseOrderItemDiscountAmount = getTenantPurchaseOrderItemDiscountAmount;
|
|
4
|
+
exports.withResolvedTenantItemDiscountTotal = withResolvedTenantItemDiscountTotal;
|
|
5
|
+
const common_1 = require("./common");
|
|
6
|
+
// ==========================================================><====================================================
|
|
7
|
+
/**
|
|
8
|
+
* Total item-level discount for a tenant purchase order line, in money.
|
|
9
|
+
*
|
|
10
|
+
* The discount is applied per cart line through the cart's "Item Discount"
|
|
11
|
+
* action and lives in `item_discount_details` — the same record an order item
|
|
12
|
+
* uses:
|
|
13
|
+
* - `special_discount` is a **per-unit** amount when fixed, so the discount
|
|
14
|
+
* scales with quantity, and a percentage of the line's gross when
|
|
15
|
+
* `is_special_discount_percentage` is set.
|
|
16
|
+
* - `total_discount` is a fallback for records written without a
|
|
17
|
+
* `special_discount`.
|
|
18
|
+
*
|
|
19
|
+
* The result is clamped to the line's gross value so a discount can never make
|
|
20
|
+
* a line negative.
|
|
21
|
+
*/
|
|
22
|
+
function getTenantPurchaseOrderItemDiscountAmount(orderItem) {
|
|
23
|
+
const details = orderItem.item_discount_details;
|
|
24
|
+
if (!details)
|
|
25
|
+
return 0;
|
|
26
|
+
const quantity = orderItem.quantity ?? 0;
|
|
27
|
+
const grossAmount = (0, common_1.parseNum)((orderItem.price ?? 0) * quantity);
|
|
28
|
+
const value = details.special_discount;
|
|
29
|
+
if (typeof value !== 'number' || !(value > 0)) {
|
|
30
|
+
return (0, common_1.parseNum)(Math.max(0, Math.min(grossAmount, details.total_discount ?? 0)));
|
|
31
|
+
}
|
|
32
|
+
const discountAmount = details.is_special_discount_percentage
|
|
33
|
+
? (0, common_1.getPercentageAmount)(grossAmount, value)
|
|
34
|
+
: value * quantity;
|
|
35
|
+
return (0, common_1.parseNum)(Math.min(grossAmount, Math.max(0, discountAmount)));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Refreshes `total_discount` on a line's discount record so the amount it
|
|
39
|
+
* reports matches the line's current price/quantity. Preserves every other
|
|
40
|
+
* field (id, name, reason, reference) and returns null when there is no
|
|
41
|
+
* discount.
|
|
42
|
+
*/
|
|
43
|
+
function withResolvedTenantItemDiscountTotal(orderItem) {
|
|
44
|
+
const details = orderItem.item_discount_details;
|
|
45
|
+
if (!details)
|
|
46
|
+
return null;
|
|
47
|
+
return {
|
|
48
|
+
...details,
|
|
49
|
+
total_discount: getTenantPurchaseOrderItemDiscountAmount(orderItem),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
@@ -1,4 +1,26 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IDiscountDetails, IWarehouseItem, IWarehousePurchaseOrderItem } from '../../schemas';
|
|
2
|
+
/**
|
|
3
|
+
* Total item-level discount for a purchase order line, in money.
|
|
4
|
+
*
|
|
5
|
+
* The line's discount lives in `item_discount_details` (the same record an order
|
|
6
|
+
* item uses):
|
|
7
|
+
* - `special_discount` is a **per-unit** amount when fixed, so the discount
|
|
8
|
+
* scales with quantity, and a percentage of the line's gross when
|
|
9
|
+
* `is_special_discount_percentage` is set.
|
|
10
|
+
* - `total_discount` is used as a fallback for records written without a
|
|
11
|
+
* `special_discount` (e.g. a discount carried over from elsewhere).
|
|
12
|
+
*
|
|
13
|
+
* The result is clamped to the line's gross value so a discount can never make a
|
|
14
|
+
* line negative.
|
|
15
|
+
*/
|
|
16
|
+
export declare function getWarehousePurchaseOrderItemDiscountAmount(orderItem: Pick<IWarehousePurchaseOrderItem, 'price' | 'quantity' | 'item_discount_details'>): number;
|
|
17
|
+
/**
|
|
18
|
+
* Refreshes `total_discount` on a line's discount record so the amount it
|
|
19
|
+
* reports matches the line's current price/quantity. Preserves every other
|
|
20
|
+
* field (id, name, reason, reference) and returns null when there is no
|
|
21
|
+
* discount.
|
|
22
|
+
*/
|
|
23
|
+
export declare function withResolvedWarehouseItemDiscountTotal(orderItem: Pick<IWarehousePurchaseOrderItem, 'price' | 'quantity' | 'item_discount_details'>): IDiscountDetails | null;
|
|
2
24
|
interface IConvertWarehouseItemToPurchaseOrderItemParams {
|
|
3
25
|
vendorId: string;
|
|
4
26
|
item: IWarehouseItem;
|
|
@@ -3,20 +3,82 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getWarehousePurchaseOrderItemDiscountAmount = getWarehousePurchaseOrderItemDiscountAmount;
|
|
7
|
+
exports.withResolvedWarehouseItemDiscountTotal = withResolvedWarehouseItemDiscountTotal;
|
|
6
8
|
exports.convertWarehouseItemToPurchaseOrderItem = convertWarehouseItemToPurchaseOrderItem;
|
|
7
9
|
exports.getCurrentWarehousePurchaseOrderItemTotalPrice = getCurrentWarehousePurchaseOrderItemTotalPrice;
|
|
8
10
|
const bson_objectid_1 = __importDefault(require("bson-objectid"));
|
|
11
|
+
const common_1 = require("./common");
|
|
12
|
+
// ==========================================================><====================================================
|
|
13
|
+
/**
|
|
14
|
+
* Total item-level discount for a purchase order line, in money.
|
|
15
|
+
*
|
|
16
|
+
* The line's discount lives in `item_discount_details` (the same record an order
|
|
17
|
+
* item uses):
|
|
18
|
+
* - `special_discount` is a **per-unit** amount when fixed, so the discount
|
|
19
|
+
* scales with quantity, and a percentage of the line's gross when
|
|
20
|
+
* `is_special_discount_percentage` is set.
|
|
21
|
+
* - `total_discount` is used as a fallback for records written without a
|
|
22
|
+
* `special_discount` (e.g. a discount carried over from elsewhere).
|
|
23
|
+
*
|
|
24
|
+
* The result is clamped to the line's gross value so a discount can never make a
|
|
25
|
+
* line negative.
|
|
26
|
+
*/
|
|
27
|
+
function getWarehousePurchaseOrderItemDiscountAmount(orderItem) {
|
|
28
|
+
const details = orderItem.item_discount_details;
|
|
29
|
+
if (!details)
|
|
30
|
+
return 0;
|
|
31
|
+
const quantity = orderItem.quantity ?? 0;
|
|
32
|
+
const grossAmount = (0, common_1.parseNum)((orderItem.price ?? 0) * quantity);
|
|
33
|
+
const value = details.special_discount;
|
|
34
|
+
if (typeof value !== 'number' || !(value > 0)) {
|
|
35
|
+
return (0, common_1.parseNum)(Math.max(0, Math.min(grossAmount, details.total_discount ?? 0)));
|
|
36
|
+
}
|
|
37
|
+
const discountAmount = details.is_special_discount_percentage
|
|
38
|
+
? (0, common_1.getPercentageAmount)(grossAmount, value)
|
|
39
|
+
: value * quantity;
|
|
40
|
+
return (0, common_1.parseNum)(Math.min(grossAmount, Math.max(0, discountAmount)));
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Refreshes `total_discount` on a line's discount record so the amount it
|
|
44
|
+
* reports matches the line's current price/quantity. Preserves every other
|
|
45
|
+
* field (id, name, reason, reference) and returns null when there is no
|
|
46
|
+
* discount.
|
|
47
|
+
*/
|
|
48
|
+
function withResolvedWarehouseItemDiscountTotal(orderItem) {
|
|
49
|
+
const details = orderItem.item_discount_details;
|
|
50
|
+
if (!details)
|
|
51
|
+
return null;
|
|
52
|
+
return {
|
|
53
|
+
...details,
|
|
54
|
+
total_discount: getWarehousePurchaseOrderItemDiscountAmount(orderItem),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
9
57
|
function convertWarehouseItemToPurchaseOrderItem({ vendorId, item, orderItem, }) {
|
|
10
58
|
const { _id = (0, bson_objectid_1.default)().toHexString(), price = item.purchase_price?.[vendorId] ?? item.purchase_price?.BASE_PRICE ?? 0, quantity = 1, special_instructions = '', is_ready, } = orderItem ?? {};
|
|
59
|
+
// Item discounts are applied per cart line; an existing order item (split /
|
|
60
|
+
// re-add) keeps the discount it was given.
|
|
61
|
+
const item_discount_details = orderItem?.item_discount_details ?? null;
|
|
62
|
+
const discount_amount = getWarehousePurchaseOrderItemDiscountAmount({
|
|
63
|
+
price,
|
|
64
|
+
quantity,
|
|
65
|
+
item_discount_details,
|
|
66
|
+
});
|
|
11
67
|
const purchaseOrderItem = {
|
|
12
68
|
_id,
|
|
13
69
|
item_id: item._id,
|
|
14
70
|
name: item.name,
|
|
15
71
|
price,
|
|
16
|
-
subtotal: price * quantity,
|
|
72
|
+
subtotal: (0, common_1.parseNum)(price * quantity - discount_amount),
|
|
17
73
|
quantity,
|
|
18
74
|
special_instructions,
|
|
19
75
|
is_ready,
|
|
76
|
+
item_discount_details: item_discount_details == null
|
|
77
|
+
? null
|
|
78
|
+
: {
|
|
79
|
+
...item_discount_details,
|
|
80
|
+
total_discount: discount_amount,
|
|
81
|
+
},
|
|
20
82
|
// These values will be auto calculated
|
|
21
83
|
_discount_amount: 0,
|
|
22
84
|
_gross_amount: 0,
|
|
@@ -27,8 +89,7 @@ function convertWarehouseItemToPurchaseOrderItem({ vendorId, item, orderItem, })
|
|
|
27
89
|
}
|
|
28
90
|
// ==========================================================><====================================================
|
|
29
91
|
function getCurrentWarehousePurchaseOrderItemTotalPrice(orderItem) {
|
|
30
|
-
//
|
|
31
|
-
|
|
32
|
-
const total = orderItem.price * orderItem.quantity;
|
|
92
|
+
// The line's payable value: price × quantity minus any item-level discount.
|
|
93
|
+
const total = (0, common_1.parseNum)(orderItem.price * orderItem.quantity - getWarehousePurchaseOrderItemDiscountAmount(orderItem));
|
|
33
94
|
return total;
|
|
34
95
|
}
|