@darkpos/pricing 1.0.151 → 1.0.153
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/__TEST__/order/applyRefund.test.js +387 -0
- package/__TEST__/order/order-payment-modifier.test.js +2 -0
- package/__TEST__/order/order.test.js +1 -1
- package/__TEST__/payment.test.js +158 -0
- package/lib/invoice/index.js +0 -8
- package/lib/item/capOverPaidItem.js +14 -0
- package/lib/item/index.js +2 -2
- package/lib/order/applyRefund.js +125 -0
- package/lib/order/calculate.js +0 -1
- package/lib/order/getOverPaidAmount.js +13 -0
- package/lib/order/getTotalPaidToRedistribute.js +41 -0
- package/lib/order/index.js +8 -2
- package/lib/order/refundOrderItem.js +29 -0
- package/lib/payment/getMethodLabel.js +7 -0
- package/lib/payment/getOverPaidOrders.js +38 -0
- package/lib/payment/getRefundableOrderAmount.js +21 -0
- package/lib/payment/index.js +6 -0
- package/package.json +2 -2
- package/__TEST__/invoice/refundInvoices.test.js +0 -366
- package/lib/invoice/applyRefundToInvoices.js +0 -52
- package/lib/invoice/buildItemRefund.js +0 -23
- package/lib/invoice/computeRefundables.js +0 -59
- package/lib/invoice/resolveItemState.js +0 -25
- package/lib/item/getOverpaidAmount.js +0 -7
- package/lib/order/getOverpaidAmount.js +0 -13
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
module.exports = ({ utils, actions }) =>
|
|
2
|
+
function applyRefund(options, amountToProcess = 0) {
|
|
3
|
+
const optionsWithAmount = options;
|
|
4
|
+
let amount = Math.abs(amountToProcess || 0);
|
|
5
|
+
let refundedAmount = 0;
|
|
6
|
+
|
|
7
|
+
Object.keys(options).forEach(orderId => {
|
|
8
|
+
const order = options[orderId];
|
|
9
|
+
const { items } = order;
|
|
10
|
+
const itemIds = Object.keys(items);
|
|
11
|
+
|
|
12
|
+
itemIds.forEach(itemId => {
|
|
13
|
+
const item = items[itemId];
|
|
14
|
+
|
|
15
|
+
items[itemId] = {
|
|
16
|
+
...item,
|
|
17
|
+
orderId,
|
|
18
|
+
amount: item.amount || 0,
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
itemIds.forEach(itemId => {
|
|
23
|
+
if (amount <= 0) return;
|
|
24
|
+
|
|
25
|
+
const item = items[itemId];
|
|
26
|
+
const overPaidAmount = utils.math.max(
|
|
27
|
+
utils.math.sub(item.totalPaid || 0, item.total || 0),
|
|
28
|
+
0
|
|
29
|
+
);
|
|
30
|
+
const { refunded, item: updatedItem } = actions.refundOrderItem({
|
|
31
|
+
item,
|
|
32
|
+
orderId,
|
|
33
|
+
amountToRefund: utils.math.min(amount, overPaidAmount),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
if (!updatedItem) return;
|
|
37
|
+
|
|
38
|
+
refundedAmount = utils.math.add(refundedAmount, refunded);
|
|
39
|
+
items[itemId] = updatedItem;
|
|
40
|
+
amount = utils.math.sub(amount, refunded);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
itemIds.forEach(itemId => {
|
|
44
|
+
if (amount <= 0) return;
|
|
45
|
+
|
|
46
|
+
const item = items[itemId];
|
|
47
|
+
const { refunded, item: updatedItem } = actions.refundOrderItem({
|
|
48
|
+
item,
|
|
49
|
+
orderId,
|
|
50
|
+
amountToRefund: amount,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
if (!updatedItem) return;
|
|
54
|
+
|
|
55
|
+
refundedAmount = utils.math.add(refundedAmount, refunded);
|
|
56
|
+
items[itemId] = updatedItem;
|
|
57
|
+
amount = utils.math.sub(amount, refunded);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
itemIds.forEach((itemId, index) => {
|
|
61
|
+
const item = items[itemId];
|
|
62
|
+
let excess = utils.math.max(
|
|
63
|
+
utils.math.sub(item.totalPaid || 0, item.total || 0),
|
|
64
|
+
0
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
if (excess <= 0) return;
|
|
68
|
+
|
|
69
|
+
for (
|
|
70
|
+
let nextIndex = index + 1;
|
|
71
|
+
nextIndex < itemIds.length;
|
|
72
|
+
nextIndex += 1
|
|
73
|
+
) {
|
|
74
|
+
if (excess <= 0) break;
|
|
75
|
+
|
|
76
|
+
const nextItemId = itemIds[nextIndex];
|
|
77
|
+
const nextItem = items[nextItemId];
|
|
78
|
+
const capacity = utils.math.max(
|
|
79
|
+
utils.math.sub(nextItem.total || 0, nextItem.totalPaid || 0),
|
|
80
|
+
0
|
|
81
|
+
);
|
|
82
|
+
const amountToMove = utils.math.min(excess, capacity);
|
|
83
|
+
|
|
84
|
+
if (amountToMove > 0) {
|
|
85
|
+
items[itemId] = {
|
|
86
|
+
...items[itemId],
|
|
87
|
+
amount: utils.math.sub(items[itemId].amount || 0, amountToMove),
|
|
88
|
+
totalPaid: utils.math.sub(
|
|
89
|
+
items[itemId].totalPaid || 0,
|
|
90
|
+
amountToMove
|
|
91
|
+
),
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
items[nextItemId] = {
|
|
95
|
+
...nextItem,
|
|
96
|
+
amount: utils.math.add(nextItem.amount || 0, amountToMove),
|
|
97
|
+
totalPaid: utils.math.add(nextItem.totalPaid || 0, amountToMove),
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
excess = utils.math.sub(excess, amountToMove);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
itemIds.forEach(itemId => {
|
|
106
|
+
const item = items[itemId];
|
|
107
|
+
|
|
108
|
+
items[itemId] = {
|
|
109
|
+
...item,
|
|
110
|
+
status: {
|
|
111
|
+
...item.status,
|
|
112
|
+
paid: {
|
|
113
|
+
value: (item.totalPaid || 0) >= (item.total || 0),
|
|
114
|
+
date: new Date(),
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
options: optionsWithAmount,
|
|
123
|
+
refundedAmount,
|
|
124
|
+
};
|
|
125
|
+
};
|
package/lib/order/calculate.js
CHANGED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module.exports = ({ itemActions, utils }) =>
|
|
2
|
+
function getOverPaidAmount({ order }) {
|
|
3
|
+
if (!order) return 0;
|
|
4
|
+
|
|
5
|
+
const { total: itemsTotal } = itemActions.getItemsTotals(order.items || []);
|
|
6
|
+
|
|
7
|
+
const orderTotalPaid = order.totalPaid || 0;
|
|
8
|
+
|
|
9
|
+
if (orderTotalPaid > itemsTotal)
|
|
10
|
+
return utils.math.sub(orderTotalPaid, itemsTotal);
|
|
11
|
+
|
|
12
|
+
return 0;
|
|
13
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module.exports = ({ itemActions, utils }) =>
|
|
2
|
+
function getTotalPaidToRedistribute({ orders = [] }) {
|
|
3
|
+
const redistributableOrders = [];
|
|
4
|
+
let redistributableAmount = 0;
|
|
5
|
+
|
|
6
|
+
const appendIfRedistributable = currentOrder => {
|
|
7
|
+
const itemsTotalPaid = itemActions.getItemsTotalPaid({
|
|
8
|
+
orderItems: currentOrder.items || [],
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const amount = utils.math.max(
|
|
12
|
+
utils.math.sub(currentOrder.totalPaid || 0, itemsTotalPaid),
|
|
13
|
+
0
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
if (amount > 0) {
|
|
17
|
+
redistributableOrders.push({
|
|
18
|
+
...currentOrder,
|
|
19
|
+
redistributableAmount: amount,
|
|
20
|
+
});
|
|
21
|
+
redistributableAmount = utils.math.add(redistributableAmount, amount);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
(orders || []).forEach(order => {
|
|
26
|
+
if (!order || !order._id) return;
|
|
27
|
+
if (order.isParent) {
|
|
28
|
+
const childOrders = order.orders || [];
|
|
29
|
+
if (childOrders.length > 0) {
|
|
30
|
+
childOrders.forEach(appendIfRedistributable);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
appendIfRedistributable(order);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
redistributableOrders,
|
|
39
|
+
redistributableAmount,
|
|
40
|
+
};
|
|
41
|
+
};
|
package/lib/order/index.js
CHANGED
|
@@ -82,6 +82,7 @@ const createSubOrder = require('./createSubOrder');
|
|
|
82
82
|
const manualSplit = require('./manualSplit');
|
|
83
83
|
const manualSplitByQuantity = require('./manualSplitByQuantity');
|
|
84
84
|
const applyPayment = require('./applyPayment');
|
|
85
|
+
const applyRefund = require('./applyRefund');
|
|
85
86
|
const getBalance = require('./getBalance');
|
|
86
87
|
const getLastLocation = require('./getLastLocation');
|
|
87
88
|
const getModifierRelations = require('./getModifierRelations');
|
|
@@ -100,8 +101,10 @@ const removeEmptyNotes = require('./removeEmptyNotes');
|
|
|
100
101
|
const getTaxes = require('./getTaxes');
|
|
101
102
|
const getPickedStatus = require('./getPickedStatus');
|
|
102
103
|
const calculateWithPayment = require('./calculateWithPayment');
|
|
103
|
-
const getOverpaidAmount = require('./getOverpaidAmount');
|
|
104
104
|
const hasSerial = require('./hasSerial');
|
|
105
|
+
const getOverPaidAmount = require('./getOverPaidAmount');
|
|
106
|
+
const getTotalPaidToRedistribute = require('./getTotalPaidToRedistribute');
|
|
107
|
+
const refundOrderItem = require('./refundOrderItem');
|
|
105
108
|
|
|
106
109
|
const orderActions = (deps = {}) => {
|
|
107
110
|
const actions = {};
|
|
@@ -195,6 +198,7 @@ const orderActions = (deps = {}) => {
|
|
|
195
198
|
manualSplit: manualSplit(innerDeps),
|
|
196
199
|
manualSplitByQuantity: manualSplitByQuantity(innerDeps),
|
|
197
200
|
applyPayment: applyPayment(innerDeps),
|
|
201
|
+
applyRefund: applyRefund(innerDeps),
|
|
198
202
|
getOrdersBalance: getOrdersBalance(innerDeps),
|
|
199
203
|
getLastLocation: getLastLocation(innerDeps),
|
|
200
204
|
getModifierRelations: getModifierRelations(innerDeps),
|
|
@@ -213,8 +217,10 @@ const orderActions = (deps = {}) => {
|
|
|
213
217
|
getTaxes: getTaxes(innerDeps),
|
|
214
218
|
getPickedStatus: getPickedStatus(innerDeps),
|
|
215
219
|
calculateWithPayment: calculateWithPayment(innerDeps),
|
|
216
|
-
getOverpaidAmount: getOverpaidAmount(innerDeps),
|
|
217
220
|
hasSerial: hasSerial(innerDeps),
|
|
221
|
+
getOverPaidAmount: getOverPaidAmount(innerDeps),
|
|
222
|
+
getTotalPaidToRedistribute: getTotalPaidToRedistribute(innerDeps),
|
|
223
|
+
refundOrderItem: refundOrderItem(innerDeps),
|
|
218
224
|
});
|
|
219
225
|
|
|
220
226
|
Object.keys(freezedActions).forEach(actionName => {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module.exports = ({ utils }) =>
|
|
2
|
+
function refundOrderItem({ item, orderId, amountToRefund = 0 }) {
|
|
3
|
+
if (amountToRefund <= 0) return { refunded: 0 };
|
|
4
|
+
|
|
5
|
+
const refundableAmount = item.totalPaid || 0;
|
|
6
|
+
const refunded = utils.math.min(amountToRefund, refundableAmount);
|
|
7
|
+
const amountToAssign = utils.math.sub(0, refunded);
|
|
8
|
+
const updatedTotalPaid = utils.math.add(
|
|
9
|
+
item.totalPaid || 0,
|
|
10
|
+
amountToAssign
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
refunded,
|
|
15
|
+
item: {
|
|
16
|
+
...item,
|
|
17
|
+
orderId,
|
|
18
|
+
amount: utils.math.add(item.amount || 0, amountToAssign),
|
|
19
|
+
totalPaid: updatedTotalPaid,
|
|
20
|
+
status: {
|
|
21
|
+
...item.status,
|
|
22
|
+
paid: {
|
|
23
|
+
value: updatedTotalPaid >= (item.total || 0),
|
|
24
|
+
date: new Date(),
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
module.exports = ({ settings, _ }) =>
|
|
2
|
+
function getMethodLabel({ paymentProvider }) {
|
|
3
|
+
const methods = _.get(settings, '_settings.payment.methods', {});
|
|
4
|
+
|
|
5
|
+
if (!paymentProvider || !methods[paymentProvider]) return '';
|
|
6
|
+
return methods[paymentProvider].label || '';
|
|
7
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module.exports = ({ orderActions, utils }) =>
|
|
2
|
+
function getOverPaidOrders({ orders = [] }) {
|
|
3
|
+
const overPaidOrders = [];
|
|
4
|
+
let overPaidAmount = 0;
|
|
5
|
+
|
|
6
|
+
const appendIfOverPaid = currentOrder => {
|
|
7
|
+
const overPaidTotal = orderActions.getOverPaidAmount({
|
|
8
|
+
order: currentOrder,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
if (overPaidTotal > 0) {
|
|
12
|
+
overPaidOrders.push({
|
|
13
|
+
...currentOrder,
|
|
14
|
+
refundableAmount: overPaidTotal,
|
|
15
|
+
});
|
|
16
|
+
overPaidAmount = utils.math.add(overPaidAmount, overPaidTotal);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
(orders || []).forEach(order => {
|
|
21
|
+
if (!order || !order._id) return;
|
|
22
|
+
if (order.isParent) {
|
|
23
|
+
const childOrders = order.orders || [];
|
|
24
|
+
|
|
25
|
+
if (childOrders.length > 0) {
|
|
26
|
+
childOrders.forEach(appendIfOverPaid);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
appendIfOverPaid(order);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
overPaidOrders,
|
|
36
|
+
overPaidAmount,
|
|
37
|
+
};
|
|
38
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module.exports = ({ utils }) => {
|
|
2
|
+
const { math } = utils;
|
|
3
|
+
|
|
4
|
+
return function getRefundableOrderAmount({ order }) {
|
|
5
|
+
if (!order) return 0;
|
|
6
|
+
|
|
7
|
+
const items = Array.isArray(order.items) ? order.items : [];
|
|
8
|
+
const orderTotal =
|
|
9
|
+
typeof order.total === 'number'
|
|
10
|
+
? order.total
|
|
11
|
+
: items.reduce(
|
|
12
|
+
(acc, item) => math.add(acc, Number(item.total || 0)),
|
|
13
|
+
0
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
return math.max(
|
|
17
|
+
math.sub(Number(order.totalPaid || 0), Number(orderTotal || 0)),
|
|
18
|
+
0
|
|
19
|
+
);
|
|
20
|
+
};
|
|
21
|
+
};
|
package/lib/payment/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
//
|
|
2
2
|
const getMaxAmountToRefund = require('./getMaxAmountToRefund');
|
|
3
|
+
const getRefundableOrderAmount = require('./getRefundableOrderAmount');
|
|
4
|
+
const getMethodLabel = require('./getMethodLabel');
|
|
5
|
+
const getOverPaidOrders = require('./getOverPaidOrders');
|
|
3
6
|
|
|
4
7
|
const orderActions = (deps = {}) => {
|
|
5
8
|
const actions = {};
|
|
@@ -11,6 +14,9 @@ const orderActions = (deps = {}) => {
|
|
|
11
14
|
|
|
12
15
|
const freezedActions = Object.freeze({
|
|
13
16
|
getMaxAmountToRefund: getMaxAmountToRefund(innerDeps),
|
|
17
|
+
getRefundableOrderAmount: getRefundableOrderAmount(innerDeps),
|
|
18
|
+
getMethodLabel: getMethodLabel(innerDeps),
|
|
19
|
+
getOverPaidOrders: getOverPaidOrders(innerDeps),
|
|
14
20
|
});
|
|
15
21
|
|
|
16
22
|
Object.keys(freezedActions).forEach(actionName => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darkpos/pricing",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.153",
|
|
4
4
|
"description": "Pricing calculator",
|
|
5
5
|
"author": "Dark POS",
|
|
6
6
|
"license": "ISC",
|
|
@@ -54,5 +54,5 @@
|
|
|
54
54
|
"supertest": "^6.2.3",
|
|
55
55
|
"supervisor": "^0.12.0"
|
|
56
56
|
},
|
|
57
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "495fd0beca96603861fccf52ba2a5618fc632e7a"
|
|
58
58
|
}
|