@darkpos/pricing 1.0.137 → 1.0.139
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__/item.test.js +107 -0
- package/__TEST__/order/addItem.test.js +1 -0
- package/__TEST__/order/order-payment-modifier.test.js +1845 -12
- package/__TEST__/order/order.test.js +882 -3
- package/lib/index.js +11 -0
- package/lib/invoice/getStatusByItems.js +22 -0
- package/lib/invoice/getTotalByItems.js +12 -0
- package/lib/invoice/index.js +24 -0
- package/lib/item/applyPayment.js +22 -0
- package/lib/item/calculate.js +13 -7
- package/lib/item/getAmountToPayById.js +6 -0
- package/lib/item/getBalanceForPaymentModifier.js +30 -0
- package/lib/item/getPaidStatus.js +29 -0
- package/lib/item/index.js +10 -2
- package/lib/item/isMergeWithParentItem.js +4 -0
- package/lib/item/patchItem.js +3 -1
- package/lib/modifier/index.js +2 -0
- package/lib/modifier/isMergeWithParentItem.js +9 -0
- package/lib/modifier/patchModifier.js +4 -0
- package/lib/order/addItem.js +23 -4
- package/lib/order/calculate.js +47 -3
- package/lib/order/calculateWithPayment.js +36 -0
- package/lib/order/getPaidStatus.js +35 -0
- package/lib/order/getPickedStatus.js +4 -0
- package/lib/order/index.js +8 -2
- package/lib/order/mergeRelatedItemWithParentItem.js +94 -0
- package/lib/order/updatePaidStatus.js +0 -0
- package/package.json +4 -3
- package/lib/item/getUpdatedStatus.js +0 -17
- package/lib/order/getUpdatedStatus.js +0 -17
package/lib/index.js
CHANGED
|
@@ -9,6 +9,7 @@ const makeItemActions = require('./item');
|
|
|
9
9
|
const makeOrderActions = require('./order');
|
|
10
10
|
const makeModifierActions = require('./modifier');
|
|
11
11
|
const makePaymentActions = require('./payment');
|
|
12
|
+
const makeInvoiceActions = require('./invoice');
|
|
12
13
|
|
|
13
14
|
const constants = require('./constants');
|
|
14
15
|
|
|
@@ -50,12 +51,22 @@ module.exports = session => {
|
|
|
50
51
|
storeActions,
|
|
51
52
|
});
|
|
52
53
|
|
|
54
|
+
const invoiceActions = makeInvoiceActions({
|
|
55
|
+
...deps,
|
|
56
|
+
itemActions,
|
|
57
|
+
modifierActions,
|
|
58
|
+
orderActions,
|
|
59
|
+
storeActions,
|
|
60
|
+
paymentActions,
|
|
61
|
+
});
|
|
62
|
+
|
|
53
63
|
return {
|
|
54
64
|
item: itemActions,
|
|
55
65
|
order: orderActions,
|
|
56
66
|
modifier: modifierActions,
|
|
57
67
|
store: storeActions,
|
|
58
68
|
payment: paymentActions,
|
|
69
|
+
invoice: invoiceActions,
|
|
59
70
|
constants,
|
|
60
71
|
};
|
|
61
72
|
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module.exports = ({ utils, _ }) =>
|
|
2
|
+
function getStatusByItems({ items }) {
|
|
3
|
+
const { math } = utils;
|
|
4
|
+
if (!Array.isArray(items) || items.length === 0) return 'pending';
|
|
5
|
+
|
|
6
|
+
const totalPaid = items.reduce(
|
|
7
|
+
(total, item) => math.add(total, item.totalPaid),
|
|
8
|
+
0
|
|
9
|
+
);
|
|
10
|
+
const isInvoicePaid = items.every(
|
|
11
|
+
item => !!_.get(item, 'status.paid.value', false)
|
|
12
|
+
);
|
|
13
|
+
const isInvoicePartialPaid =
|
|
14
|
+
!isInvoicePaid &&
|
|
15
|
+
items.some(item => !!_.get(item, 'status.paid.value', false));
|
|
16
|
+
|
|
17
|
+
if (isInvoicePaid) return 'paid';
|
|
18
|
+
if (isInvoicePartialPaid || (!isInvoicePartialPaid && totalPaid > 0))
|
|
19
|
+
return 'partial';
|
|
20
|
+
|
|
21
|
+
return 'pending';
|
|
22
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module.exports = ({ utils }) =>
|
|
2
|
+
function getTotalByItems({ items }) {
|
|
3
|
+
const { math } = utils;
|
|
4
|
+
if (!Array.isArray(items) || items.length === 0) return 0;
|
|
5
|
+
|
|
6
|
+
const total = items.reduce(
|
|
7
|
+
(itemsTotal, item) => math.add(itemsTotal, item.total),
|
|
8
|
+
0
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
return total;
|
|
12
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const getStatusByItems = require('./getStatusByItems');
|
|
2
|
+
const getTotalByItems = require('./getTotalByItems');
|
|
3
|
+
|
|
4
|
+
const invoiceActions = (deps = {}) => {
|
|
5
|
+
const actions = {};
|
|
6
|
+
|
|
7
|
+
const innerDeps = {
|
|
8
|
+
...deps,
|
|
9
|
+
actions,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const freezedActions = Object.freeze({
|
|
13
|
+
getStatusByItems: getStatusByItems(innerDeps),
|
|
14
|
+
getTotalByItems: getTotalByItems(innerDeps),
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
Object.keys(freezedActions).forEach(actionName => {
|
|
18
|
+
actions[actionName] = freezedActions[actionName];
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
return freezedActions;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
module.exports = invoiceActions;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module.exports = () =>
|
|
2
|
+
function applyPayment({ paymentOrderItems, orderItem }) {
|
|
3
|
+
const itemOption = paymentOrderItems.find(
|
|
4
|
+
({ orderItemId }) => String(orderItemId) === String(orderItem._id)
|
|
5
|
+
);
|
|
6
|
+
|
|
7
|
+
if (!itemOption) return orderItem;
|
|
8
|
+
|
|
9
|
+
const item = orderItem;
|
|
10
|
+
|
|
11
|
+
const paid =
|
|
12
|
+
itemOption.status && itemOption.status.paid && itemOption.status.paid;
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
...item,
|
|
16
|
+
status: {
|
|
17
|
+
...item.status,
|
|
18
|
+
...itemOption.status,
|
|
19
|
+
paid,
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
};
|
package/lib/item/calculate.js
CHANGED
|
@@ -8,8 +8,11 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
8
8
|
...inputItem,
|
|
9
9
|
quantity: math.max(0, Number(inputItem.quantity) || 0),
|
|
10
10
|
});
|
|
11
|
-
const amountToPay =
|
|
12
|
-
|
|
11
|
+
const amountToPay = actions.getAmountToPayById({
|
|
12
|
+
amountsToPay: opts.amountsToPay,
|
|
13
|
+
id: inputItem._id,
|
|
14
|
+
});
|
|
15
|
+
|
|
13
16
|
const paymentMethod = opts.paymentMethod || null;
|
|
14
17
|
const paymentType = opts.paymentType || null;
|
|
15
18
|
const isPrepay = opts.isPrepay || false;
|
|
@@ -148,16 +151,19 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
148
151
|
let _modifier = modifier;
|
|
149
152
|
|
|
150
153
|
if (modifierActions.isPaymentModifier(modifier)) {
|
|
154
|
+
const balance = actions.getBalanceForPaymentModifier({
|
|
155
|
+
item,
|
|
156
|
+
computedPrice,
|
|
157
|
+
modifier,
|
|
158
|
+
});
|
|
159
|
+
|
|
151
160
|
_modifier = modifierActions.calculatePaymentModifier({
|
|
152
161
|
paymentModifier: modifier,
|
|
153
162
|
amountToPay:
|
|
154
163
|
accumulatedAmount < 0 && amountToPay > 0
|
|
155
164
|
? math.add(amountToPay, math.abs(accumulatedAmount))
|
|
156
165
|
: amountToPay,
|
|
157
|
-
itemBalance:
|
|
158
|
-
typeof item.totalPaid === 'number' && item.totalPaid > 0
|
|
159
|
-
? actions.getItemsBalance({ items: [item] })
|
|
160
|
-
: math.sub(computedPrice, item.totalPaid),
|
|
166
|
+
itemBalance: math.max(0, balance),
|
|
161
167
|
paymentId,
|
|
162
168
|
});
|
|
163
169
|
}
|
|
@@ -255,7 +261,7 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
255
261
|
modifiers: [...modifiersToNotCompute, ...modifiers],
|
|
256
262
|
subTotals,
|
|
257
263
|
total,
|
|
258
|
-
|
|
264
|
+
...(item.status || {}),
|
|
259
265
|
};
|
|
260
266
|
};
|
|
261
267
|
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
module.exports = ({ utils, actions, modifierActions }) => {
|
|
2
|
+
const { math } = utils;
|
|
3
|
+
return function getBalanceForPaymentModifier({
|
|
4
|
+
item,
|
|
5
|
+
computedPrice,
|
|
6
|
+
modifier,
|
|
7
|
+
}) {
|
|
8
|
+
let balance =
|
|
9
|
+
typeof item.totalPaid === 'number' && item.totalPaid > 0
|
|
10
|
+
? actions.getItemsBalance({ items: [item] })
|
|
11
|
+
: math.sub(computedPrice, item.totalPaid);
|
|
12
|
+
|
|
13
|
+
const modifiersAmount = item.modifiers.reduce((amount, mod) => {
|
|
14
|
+
if (
|
|
15
|
+
modifierActions.isCalculatedPaymentModifier(mod) &&
|
|
16
|
+
mod.locked &&
|
|
17
|
+
modifier.modifierId === mod.modifierId
|
|
18
|
+
) {
|
|
19
|
+
return math.add(amount, mod._computed.amount);
|
|
20
|
+
}
|
|
21
|
+
return amount;
|
|
22
|
+
}, 0);
|
|
23
|
+
|
|
24
|
+
if (modifiersAmount > 0) {
|
|
25
|
+
balance = math.sub(balance, modifiersAmount);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return balance;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module.exports = ({ actions, settings }) =>
|
|
2
|
+
function getPaidStatus({
|
|
3
|
+
status,
|
|
4
|
+
total: totalParam,
|
|
5
|
+
totalPaid: totalPaidParam,
|
|
6
|
+
}) {
|
|
7
|
+
const localStatus = status || {};
|
|
8
|
+
|
|
9
|
+
const total = totalParam || 0;
|
|
10
|
+
const totalPaid = totalPaidParam || 0;
|
|
11
|
+
|
|
12
|
+
if (
|
|
13
|
+
actions.isPaid({ item: { status: localStatus } }) &&
|
|
14
|
+
total !== 0 &&
|
|
15
|
+
total > totalPaid
|
|
16
|
+
) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (!settings || !settings.order || !settings.order.autoMarkAsPaid) {
|
|
21
|
+
return localStatus.paid;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (!actions.isPaid({ item: { status: localStatus } }) && total === 0) {
|
|
25
|
+
return { value: true, date: new Date() };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return localStatus.paid;
|
|
29
|
+
};
|
package/lib/item/index.js
CHANGED
|
@@ -59,7 +59,7 @@ const getSubtotal = require('./getSubtotal');
|
|
|
59
59
|
const isSomeTagsMatch = require('./isSomeTagsMatch');
|
|
60
60
|
const getTotals = require('./getTotals');
|
|
61
61
|
const patchItem = require('./patchItem');
|
|
62
|
-
const
|
|
62
|
+
const getPaidStatus = require('./getPaidStatus');
|
|
63
63
|
const isOverwrittenPrice = require('./isOverwrittenPrice');
|
|
64
64
|
const isOverwrittenQuantity = require('./isOverwrittenQuantity');
|
|
65
65
|
const overrideNotes = require('./overrideNotes');
|
|
@@ -74,10 +74,14 @@ const isSerialLengthValid = require('./isSerialLengthValid');
|
|
|
74
74
|
const getSerialStatus = require('./getSerialStatus');
|
|
75
75
|
const removeDepartmentModifiers = require('./removeDepartmentModifiers');
|
|
76
76
|
const isRemoveParentItem = require('./isRemoveParentItem');
|
|
77
|
+
const isMergeWithParentItem = require('./isMergeWithParentItem');
|
|
77
78
|
const hasRelatedItems = require('./hasRelatedItems');
|
|
78
79
|
const getAddModifiers = require('./getAddModifiers');
|
|
79
80
|
const hasAddModifiers = require('./hasAddModifiers');
|
|
80
81
|
const getTaxes = require('./getTaxes');
|
|
82
|
+
const getAmountToPayById = require('./getAmountToPayById');
|
|
83
|
+
const applyPayment = require('./applyPayment');
|
|
84
|
+
const getBalanceForPaymentModifier = require('./getBalanceForPaymentModifier');
|
|
81
85
|
|
|
82
86
|
const itemActions = (deps = {}) => {
|
|
83
87
|
const actions = {};
|
|
@@ -150,7 +154,7 @@ const itemActions = (deps = {}) => {
|
|
|
150
154
|
isSomeTagsMatch: isSomeTagsMatch(innerDeps),
|
|
151
155
|
getTotals: getTotals(innerDeps),
|
|
152
156
|
patchItem: patchItem(innerDeps),
|
|
153
|
-
|
|
157
|
+
getPaidStatus: getPaidStatus(innerDeps),
|
|
154
158
|
isOverwrittenPrice: isOverwrittenPrice(innerDeps),
|
|
155
159
|
isOverwrittenQuantity: isOverwrittenQuantity(innerDeps),
|
|
156
160
|
overrideNotes: overrideNotes(innerDeps),
|
|
@@ -165,10 +169,14 @@ const itemActions = (deps = {}) => {
|
|
|
165
169
|
getSerialStatus: getSerialStatus(innerDeps),
|
|
166
170
|
removeDepartmentModifiers: removeDepartmentModifiers(innerDeps),
|
|
167
171
|
isRemoveParentItem: isRemoveParentItem(innerDeps),
|
|
172
|
+
isMergeWithParentItem: isMergeWithParentItem(innerDeps),
|
|
168
173
|
hasRelatedItems: hasRelatedItems(innerDeps),
|
|
169
174
|
getAddModifiers: getAddModifiers(innerDeps),
|
|
170
175
|
hasAddModifiers: hasAddModifiers(innerDeps),
|
|
171
176
|
getTaxes: getTaxes(innerDeps),
|
|
177
|
+
getAmountToPayById: getAmountToPayById(innerDeps),
|
|
178
|
+
applyPayment: applyPayment(innerDeps),
|
|
179
|
+
getBalanceForPaymentModifier: getBalanceForPaymentModifier(innerDeps),
|
|
172
180
|
});
|
|
173
181
|
|
|
174
182
|
Object.keys(freezedActions).forEach(actionName => {
|
package/lib/item/patchItem.js
CHANGED
|
@@ -5,7 +5,9 @@ module.exports = ({ actions, modifierActions }) =>
|
|
|
5
5
|
const localItem = { ...item };
|
|
6
6
|
|
|
7
7
|
const modifierIndex = item.modifiers.findIndex(
|
|
8
|
-
mod =>
|
|
8
|
+
mod =>
|
|
9
|
+
modifierActions.isValid(mod) &&
|
|
10
|
+
(modifierActions.isPercentage(mod) || !modifierActions.isDirect(mod))
|
|
9
11
|
);
|
|
10
12
|
|
|
11
13
|
localItem.modifiers[modifierIndex] = modifierActions.patchModifier({
|
package/lib/modifier/index.js
CHANGED
|
@@ -182,6 +182,7 @@ const getCountPerCustomer = require('./getCountPerCustomer');
|
|
|
182
182
|
const getAmountMultiplier = require('./getAmountMultiplier');
|
|
183
183
|
const isAmountMultiplier = require('./isAmountMultiplier');
|
|
184
184
|
const isRemoveParentItem = require('./isRemoveParentItem');
|
|
185
|
+
const isMergeWithParentItem = require('./isMergeWithParentItem');
|
|
185
186
|
const isTax = require('./isTax');
|
|
186
187
|
|
|
187
188
|
const modifierActions = (deps = {}) => {
|
|
@@ -377,6 +378,7 @@ const modifierActions = (deps = {}) => {
|
|
|
377
378
|
getAmountMultiplier: getAmountMultiplier(innerDeps),
|
|
378
379
|
isAmountMultiplier: isAmountMultiplier(innerDeps),
|
|
379
380
|
isRemoveParentItem: isRemoveParentItem(innerDeps),
|
|
381
|
+
isMergeWithParentItem: isMergeWithParentItem(innerDeps),
|
|
380
382
|
isTax: isTax(innerDeps),
|
|
381
383
|
});
|
|
382
384
|
|
package/lib/order/addItem.js
CHANGED
|
@@ -253,7 +253,7 @@ module.exports = ({ actions, itemActions, modifierActions, settings, _ }) => {
|
|
|
253
253
|
nextItemIndex,
|
|
254
254
|
});
|
|
255
255
|
|
|
256
|
-
if (reArrangedOrder && newIndex) {
|
|
256
|
+
if (reArrangedOrder && newIndex >= 0) {
|
|
257
257
|
nextOrder = { ...reArrangedOrder };
|
|
258
258
|
nextItemIndex = newIndex;
|
|
259
259
|
}
|
|
@@ -262,7 +262,7 @@ module.exports = ({ actions, itemActions, modifierActions, settings, _ }) => {
|
|
|
262
262
|
|
|
263
263
|
if (combined && pendingItemIndex > -1 && idx !== itemIndex) {
|
|
264
264
|
const idxToRemove = actions.getPendingItemIndex(nextOrder);
|
|
265
|
-
nextOrder.items.splice(idxToRemove, 1);
|
|
265
|
+
if (idxToRemove > -1) nextOrder.items.splice(idxToRemove, 1);
|
|
266
266
|
}
|
|
267
267
|
|
|
268
268
|
nextOrder = actions.addModifiersToParentItem({
|
|
@@ -270,10 +270,29 @@ module.exports = ({ actions, itemActions, modifierActions, settings, _ }) => {
|
|
|
270
270
|
relatedItem: orderItem,
|
|
271
271
|
});
|
|
272
272
|
|
|
273
|
+
const relatedItemFromOrder =
|
|
274
|
+
actions.getSelectedItem({ order: nextOrder, itemIndex: nextItemIndex }) ||
|
|
275
|
+
nextItem ||
|
|
276
|
+
orderItem;
|
|
277
|
+
|
|
278
|
+
let newItem = relatedItemFromOrder;
|
|
279
|
+
|
|
280
|
+
if (itemActions.isMergeWithParentItem(relatedItemFromOrder)) {
|
|
281
|
+
const result = actions.mergeRelatedItemWithParentItem({
|
|
282
|
+
order: nextOrder,
|
|
283
|
+
relatedItem: relatedItemFromOrder,
|
|
284
|
+
originalItem: item,
|
|
285
|
+
});
|
|
286
|
+
nextOrder = result.order;
|
|
287
|
+
newItem = result.parent;
|
|
288
|
+
}
|
|
289
|
+
|
|
273
290
|
return {
|
|
274
291
|
updatedOrder: nextOrder,
|
|
275
|
-
|
|
276
|
-
|
|
292
|
+
item: newItem,
|
|
293
|
+
itemIndex: nextOrder.items.findIndex(
|
|
294
|
+
eachItem => eachItem._id === newItem._id
|
|
295
|
+
),
|
|
277
296
|
};
|
|
278
297
|
};
|
|
279
298
|
};
|
package/lib/order/calculate.js
CHANGED
|
@@ -126,8 +126,9 @@ module.exports = ({
|
|
|
126
126
|
!itemActions.isPaid(item) &&
|
|
127
127
|
item.modifiers.some(
|
|
128
128
|
mod =>
|
|
129
|
-
modifierActions.
|
|
130
|
-
|
|
129
|
+
modifierActions.isValid(mod) &&
|
|
130
|
+
(modifierActions.isPercentage(mod) ||
|
|
131
|
+
!modifierActions.isDirect(mod))
|
|
131
132
|
)
|
|
132
133
|
);
|
|
133
134
|
|
|
@@ -153,13 +154,56 @@ module.exports = ({
|
|
|
153
154
|
const pieceCount = calculatedItems.length
|
|
154
155
|
? itemActions.getItemsTotalPieces(calculatedItems)
|
|
155
156
|
: order.properties.pieceCount;
|
|
157
|
+
|
|
158
|
+
calculatedItems = calculatedItems.map(calcItem => {
|
|
159
|
+
let itemTotalPaid = calcItem.totalPaid;
|
|
160
|
+
|
|
161
|
+
const amountToPay = itemActions.getAmountToPayById({
|
|
162
|
+
amountsToPay: opts.amountsToPay,
|
|
163
|
+
id: calcItem._id,
|
|
164
|
+
});
|
|
165
|
+
if (amountToPay) {
|
|
166
|
+
itemTotalPaid = utils.math.add(itemTotalPaid, amountToPay);
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
...calcItem,
|
|
170
|
+
totalPaid: itemTotalPaid,
|
|
171
|
+
status: {
|
|
172
|
+
...calcItem.status,
|
|
173
|
+
paid: itemActions.getPaidStatus({
|
|
174
|
+
status: calcItem.status,
|
|
175
|
+
total: calcItem.total,
|
|
176
|
+
totalPaid: itemTotalPaid,
|
|
177
|
+
}),
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
const totalPaid = itemActions.getItemsTotalPaid({
|
|
183
|
+
orderItems: calculatedItems,
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const picked = actions.getPickedStatus({ items: calculatedItems });
|
|
187
|
+
const paid = actions.getPaidStatus({
|
|
188
|
+
items: calculatedItems,
|
|
189
|
+
status: order.status,
|
|
190
|
+
total,
|
|
191
|
+
totalPaid,
|
|
192
|
+
});
|
|
193
|
+
|
|
156
194
|
return {
|
|
157
195
|
...order,
|
|
158
196
|
subTotals,
|
|
159
197
|
subTotal,
|
|
160
198
|
total,
|
|
161
199
|
items: calculatedItems,
|
|
162
|
-
status:
|
|
200
|
+
status: {
|
|
201
|
+
...(order.status || {}),
|
|
202
|
+
picked,
|
|
203
|
+
paid,
|
|
204
|
+
order: picked && paid ? 'closed' : 'open',
|
|
205
|
+
},
|
|
206
|
+
totalPaid,
|
|
163
207
|
properties: {
|
|
164
208
|
...(order.properties || {}),
|
|
165
209
|
pieceCount,
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
module.exports = ({ actions, itemActions }) =>
|
|
2
|
+
function calculateWithPayment({
|
|
3
|
+
order,
|
|
4
|
+
paymentOrderItems,
|
|
5
|
+
paymentMethod,
|
|
6
|
+
paymentType,
|
|
7
|
+
isPrepay,
|
|
8
|
+
paymentId,
|
|
9
|
+
}) {
|
|
10
|
+
const amountsToPay = paymentOrderItems
|
|
11
|
+
.filter(each => order.items.find(item => each.orderItemId === item._id))
|
|
12
|
+
.reduce(
|
|
13
|
+
(prevVal, val) => ({
|
|
14
|
+
...prevVal,
|
|
15
|
+
[val.orderItemId]: val.amount,
|
|
16
|
+
}),
|
|
17
|
+
{}
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
return actions.calculate(
|
|
21
|
+
{
|
|
22
|
+
...order,
|
|
23
|
+
items: order.items.map(orderItem =>
|
|
24
|
+
itemActions.applyPayment({ paymentOrderItems, orderItem })
|
|
25
|
+
),
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
paymentMethod,
|
|
29
|
+
paymentType,
|
|
30
|
+
isPrepay,
|
|
31
|
+
amountsToPay,
|
|
32
|
+
lockPaymentModifiers: true,
|
|
33
|
+
paymentId,
|
|
34
|
+
}
|
|
35
|
+
);
|
|
36
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module.exports = ({ actions, settings, _ }) =>
|
|
2
|
+
function getPaidStatus({
|
|
3
|
+
items,
|
|
4
|
+
status,
|
|
5
|
+
total: totalParam,
|
|
6
|
+
totalPaid: totalPaidParam,
|
|
7
|
+
}) {
|
|
8
|
+
const localStatus = status || {};
|
|
9
|
+
|
|
10
|
+
const total = totalParam || 0;
|
|
11
|
+
const totalPaid = totalPaidParam || 0;
|
|
12
|
+
|
|
13
|
+
const areAllItemsPaid = items.every(
|
|
14
|
+
item => !!_.get(item, 'status.paid.value', false)
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
if (areAllItemsPaid) return true;
|
|
18
|
+
|
|
19
|
+
if (
|
|
20
|
+
actions.isPaid({ order: { status: localStatus } }) &&
|
|
21
|
+
total !== 0 &&
|
|
22
|
+
total > totalPaid
|
|
23
|
+
) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!settings || !settings.order || !settings.order.autoMarkAsPaid)
|
|
28
|
+
return !!localStatus.paid;
|
|
29
|
+
|
|
30
|
+
if (!actions.isPaid({ order: { status: localStatus } }) && total === 0) {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return !!localStatus.paid;
|
|
35
|
+
};
|
package/lib/order/index.js
CHANGED
|
@@ -90,13 +90,16 @@ const splitItems = require('./splitItems');
|
|
|
90
90
|
const getNewItems = require('./getNewItems');
|
|
91
91
|
const mapSubOrders = require('./mapSubOrders');
|
|
92
92
|
const isPaid = require('./isPaid');
|
|
93
|
-
const
|
|
93
|
+
const getPaidStatus = require('./getPaidStatus');
|
|
94
94
|
const setPieces = require('./setPieces');
|
|
95
95
|
const copyItemToParents = require('./copyItemToParents');
|
|
96
96
|
const getItemsWithParents = require('./getItemsWithParents');
|
|
97
97
|
const addModifiersToParentItem = require('./addModifiersToParentItem');
|
|
98
|
+
const mergeRelatedItemWithParentItem = require('./mergeRelatedItemWithParentItem');
|
|
98
99
|
const removeEmptyNotes = require('./removeEmptyNotes');
|
|
99
100
|
const getTaxes = require('./getTaxes');
|
|
101
|
+
const getPickedStatus = require('./getPickedStatus');
|
|
102
|
+
const calculateWithPayment = require('./calculateWithPayment');
|
|
100
103
|
|
|
101
104
|
const orderActions = (deps = {}) => {
|
|
102
105
|
const actions = {};
|
|
@@ -198,13 +201,16 @@ const orderActions = (deps = {}) => {
|
|
|
198
201
|
getNewItems: getNewItems(innerDeps),
|
|
199
202
|
mapSubOrders: mapSubOrders(innerDeps),
|
|
200
203
|
isPaid: isPaid(innerDeps),
|
|
201
|
-
|
|
204
|
+
getPaidStatus: getPaidStatus(innerDeps),
|
|
202
205
|
setPieces: setPieces(innerDeps),
|
|
203
206
|
copyItemToParents: copyItemToParents(innerDeps),
|
|
204
207
|
getItemsWithParents: getItemsWithParents(innerDeps),
|
|
205
208
|
addModifiersToParentItem: addModifiersToParentItem(innerDeps),
|
|
209
|
+
mergeRelatedItemWithParentItem: mergeRelatedItemWithParentItem(innerDeps),
|
|
206
210
|
removeEmptyNotes: removeEmptyNotes(innerDeps),
|
|
207
211
|
getTaxes: getTaxes(innerDeps),
|
|
212
|
+
getPickedStatus: getPickedStatus(innerDeps),
|
|
213
|
+
calculateWithPayment: calculateWithPayment(innerDeps),
|
|
208
214
|
});
|
|
209
215
|
|
|
210
216
|
Object.keys(freezedActions).forEach(actionName => {
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
module.exports = ({ actions, itemActions, utils }) => {
|
|
2
|
+
const { math } = utils;
|
|
3
|
+
|
|
4
|
+
return function mergeRelatedItemWithParentItem({
|
|
5
|
+
order,
|
|
6
|
+
relatedItem,
|
|
7
|
+
originalItem,
|
|
8
|
+
}) {
|
|
9
|
+
if (
|
|
10
|
+
!order ||
|
|
11
|
+
!itemActions.isRelatedItem(relatedItem) ||
|
|
12
|
+
(!itemActions.isMergeWithParentItem(originalItem) &&
|
|
13
|
+
!itemActions.isMergeWithParentItem(relatedItem))
|
|
14
|
+
) {
|
|
15
|
+
return order;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (!Array.isArray(order.items)) return order;
|
|
19
|
+
|
|
20
|
+
const nextOrder = { ...order };
|
|
21
|
+
|
|
22
|
+
const parentItem = itemActions.getParentItem(nextOrder.items, relatedItem);
|
|
23
|
+
if (!parentItem) return nextOrder;
|
|
24
|
+
|
|
25
|
+
const parentIndex = actions.getItemIndex({
|
|
26
|
+
order: nextOrder,
|
|
27
|
+
item: parentItem,
|
|
28
|
+
});
|
|
29
|
+
if (parentIndex < 0) return nextOrder;
|
|
30
|
+
|
|
31
|
+
const relatedIndex = actions.getItemIndex({
|
|
32
|
+
order: nextOrder,
|
|
33
|
+
item: relatedItem,
|
|
34
|
+
});
|
|
35
|
+
if (relatedIndex < 0) return nextOrder;
|
|
36
|
+
|
|
37
|
+
let updatedParent = parentItem;
|
|
38
|
+
|
|
39
|
+
if (relatedItem && relatedItem.name) {
|
|
40
|
+
if (!updatedParent.name) updatedParent.name = relatedItem.name;
|
|
41
|
+
else if (!updatedParent.name.includes(relatedItem.name)) {
|
|
42
|
+
updatedParent.name = `${updatedParent.name} - ${relatedItem.name}`;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (typeof relatedItem.price === 'number') {
|
|
47
|
+
updatedParent.price = math.add(
|
|
48
|
+
updatedParent.price || 0,
|
|
49
|
+
relatedItem.price
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
nextOrder.items = [...nextOrder.items];
|
|
54
|
+
nextOrder.items[parentIndex] = updatedParent;
|
|
55
|
+
|
|
56
|
+
const relatedModifiers = Array.isArray(relatedItem.modifiers)
|
|
57
|
+
? relatedItem.modifiers
|
|
58
|
+
: [];
|
|
59
|
+
|
|
60
|
+
if (relatedModifiers.length) {
|
|
61
|
+
const parentModifierIds = (updatedParent.modifiers || [])
|
|
62
|
+
.map(mod => mod.modifierId || mod._id)
|
|
63
|
+
.filter(Boolean);
|
|
64
|
+
|
|
65
|
+
const modifiersToAdd = relatedModifiers.filter(mod => {
|
|
66
|
+
const modId = mod.modifierId || mod._id;
|
|
67
|
+
return modId && !parentModifierIds.includes(modId);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
if (modifiersToAdd.length) {
|
|
71
|
+
const orderWithModifiers = modifiersToAdd.reduce(
|
|
72
|
+
(acc, modifier) =>
|
|
73
|
+
actions.addItemModifier({
|
|
74
|
+
itemIndex: parentIndex,
|
|
75
|
+
order: acc,
|
|
76
|
+
modifier,
|
|
77
|
+
originalItem: updatedParent,
|
|
78
|
+
}),
|
|
79
|
+
nextOrder
|
|
80
|
+
);
|
|
81
|
+
nextOrder.items = orderWithModifiers.items;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
updatedParent =
|
|
86
|
+
actions.getSelectedItem({ order: nextOrder, itemIndex: parentIndex }) ||
|
|
87
|
+
updatedParent;
|
|
88
|
+
|
|
89
|
+
nextOrder.items[parentIndex] = updatedParent;
|
|
90
|
+
nextOrder.items.splice(relatedIndex, 1);
|
|
91
|
+
|
|
92
|
+
return { order: nextOrder, parent: updatedParent };
|
|
93
|
+
};
|
|
94
|
+
};
|
|
File without changes
|