@darkpos/pricing 1.0.134 → 1.0.136
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 +70 -0
- package/__TEST__/order/order.test.js +98 -0
- package/lib/item/getTaxes.js +18 -0
- package/lib/item/index.js +2 -0
- package/lib/modifier/index.js +2 -0
- package/lib/modifier/isTax.js +6 -0
- package/lib/order/createSubOrder.js +1 -1
- package/lib/order/getTaxes.js +20 -0
- package/lib/order/index.js +2 -0
- package/package.json +2 -2
package/__TEST__/item.test.js
CHANGED
|
@@ -2501,4 +2501,74 @@ describe('Item actions', () => {
|
|
|
2501
2501
|
expect(newItem.price).toBe(100);
|
|
2502
2502
|
expect(newItem.total).toBe(115);
|
|
2503
2503
|
});
|
|
2504
|
+
|
|
2505
|
+
describe('pricingService.item.getTaxes (object of tax totals)', () => {
|
|
2506
|
+
test('Returns {} when item is null', () => {
|
|
2507
|
+
expect(pricingService.item.getTaxes(null)).toEqual({});
|
|
2508
|
+
});
|
|
2509
|
+
|
|
2510
|
+
test('Returns {} when modifiers is not an array', () => {
|
|
2511
|
+
expect(pricingService.item.getTaxes({ modifiers: {} })).toEqual({});
|
|
2512
|
+
});
|
|
2513
|
+
|
|
2514
|
+
test('Returns empty object when no tax modifiers exist', () => {
|
|
2515
|
+
const item = {
|
|
2516
|
+
modifiers: [{ name: 'env', type: 'env', _computed: { amount: 5 } }],
|
|
2517
|
+
};
|
|
2518
|
+
|
|
2519
|
+
expect(pricingService.item.getTaxes(item)).toEqual({});
|
|
2520
|
+
});
|
|
2521
|
+
|
|
2522
|
+
test('Returns a single tax entry with correct amount', () => {
|
|
2523
|
+
const item = {
|
|
2524
|
+
modifiers: [{ name: 'VAT', type: 'tax', _computed: { amount: 3 } }],
|
|
2525
|
+
};
|
|
2526
|
+
|
|
2527
|
+
expect(pricingService.item.getTaxes(item)).toEqual({
|
|
2528
|
+
VAT: 3,
|
|
2529
|
+
});
|
|
2530
|
+
});
|
|
2531
|
+
|
|
2532
|
+
test('Sums amounts for tax modifiers with the same name', () => {
|
|
2533
|
+
const item = {
|
|
2534
|
+
modifiers: [
|
|
2535
|
+
{ name: 'VAT', type: 'tax', _computed: { amount: 2 } },
|
|
2536
|
+
{ name: 'VAT', type: 'tax', _computed: { amount: 3 } },
|
|
2537
|
+
],
|
|
2538
|
+
};
|
|
2539
|
+
|
|
2540
|
+
expect(pricingService.item.getTaxes(item)).toEqual({
|
|
2541
|
+
VAT: 5,
|
|
2542
|
+
});
|
|
2543
|
+
});
|
|
2544
|
+
|
|
2545
|
+
test('Handles multiple different tax names', () => {
|
|
2546
|
+
const item = {
|
|
2547
|
+
modifiers: [
|
|
2548
|
+
{ name: 'VAT', type: 'tax', _computed: { amount: 1 } },
|
|
2549
|
+
{ name: 'Service Tax', type: 'tax', _computed: { amount: 4 } },
|
|
2550
|
+
],
|
|
2551
|
+
};
|
|
2552
|
+
|
|
2553
|
+
expect(pricingService.item.getTaxes(item)).toEqual({
|
|
2554
|
+
VAT: 1,
|
|
2555
|
+
'Service Tax': 4,
|
|
2556
|
+
});
|
|
2557
|
+
});
|
|
2558
|
+
|
|
2559
|
+
test('Ignores non-tax modifiers but includes tax ones', () => {
|
|
2560
|
+
const item = {
|
|
2561
|
+
modifiers: [
|
|
2562
|
+
{ name: 'VAT', type: 'tax', _computed: { amount: 1 } },
|
|
2563
|
+
{ name: 'Env Fee', type: 'env', _computed: { amount: 7 } },
|
|
2564
|
+
{ name: 'City Tax', type: 'tax', _computed: { amount: 3 } },
|
|
2565
|
+
],
|
|
2566
|
+
};
|
|
2567
|
+
|
|
2568
|
+
expect(pricingService.item.getTaxes(item)).toEqual({
|
|
2569
|
+
VAT: 1,
|
|
2570
|
+
'City Tax': 3,
|
|
2571
|
+
});
|
|
2572
|
+
});
|
|
2573
|
+
});
|
|
2504
2574
|
});
|
|
@@ -3966,4 +3966,102 @@ describe('Order actions', () => {
|
|
|
3966
3966
|
expect(removeEmptyNotes(order2)).toEqual(order2);
|
|
3967
3967
|
expect(removeEmptyNotes(order3)).toEqual(order3);
|
|
3968
3968
|
});
|
|
3969
|
+
|
|
3970
|
+
describe('pricingService.order.getTaxes – real integration', () => {
|
|
3971
|
+
test('Returns {} when order is null', () => {
|
|
3972
|
+
expect(pricingService.order.getTaxes(null)).toEqual({});
|
|
3973
|
+
});
|
|
3974
|
+
|
|
3975
|
+
test('Returns {} when order.items is not an array', () => {
|
|
3976
|
+
expect(pricingService.order.getTaxes({ items: null })).toEqual({});
|
|
3977
|
+
});
|
|
3978
|
+
|
|
3979
|
+
test('Returns {} when order has no items', () => {
|
|
3980
|
+
expect(pricingService.order.getTaxes({ items: [] })).toEqual({});
|
|
3981
|
+
});
|
|
3982
|
+
|
|
3983
|
+
test('Aggregates taxes from a single item', () => {
|
|
3984
|
+
const order = {
|
|
3985
|
+
items: [
|
|
3986
|
+
{
|
|
3987
|
+
modifiers: [{ name: 'VAT', type: 'tax', _computed: { amount: 3 } }],
|
|
3988
|
+
},
|
|
3989
|
+
],
|
|
3990
|
+
};
|
|
3991
|
+
|
|
3992
|
+
const result = pricingService.order.getTaxes(order);
|
|
3993
|
+
|
|
3994
|
+
expect(result).toEqual({
|
|
3995
|
+
VAT: 3,
|
|
3996
|
+
});
|
|
3997
|
+
});
|
|
3998
|
+
|
|
3999
|
+
test('Sums taxes from multiple items with the same tax name', () => {
|
|
4000
|
+
const order = {
|
|
4001
|
+
items: [
|
|
4002
|
+
{
|
|
4003
|
+
modifiers: [{ name: 'VAT', type: 'tax', _computed: { amount: 2 } }],
|
|
4004
|
+
},
|
|
4005
|
+
{
|
|
4006
|
+
modifiers: [{ name: 'VAT', type: 'tax', _computed: { amount: 1 } }],
|
|
4007
|
+
},
|
|
4008
|
+
],
|
|
4009
|
+
};
|
|
4010
|
+
|
|
4011
|
+
const result = pricingService.order.getTaxes(order);
|
|
4012
|
+
|
|
4013
|
+
expect(result).toEqual({
|
|
4014
|
+
VAT: 3,
|
|
4015
|
+
});
|
|
4016
|
+
});
|
|
4017
|
+
|
|
4018
|
+
test('Handles multiple different tax names across items', () => {
|
|
4019
|
+
const order = {
|
|
4020
|
+
items: [
|
|
4021
|
+
{
|
|
4022
|
+
modifiers: [{ name: 'VAT', type: 'tax', _computed: { amount: 1 } }],
|
|
4023
|
+
},
|
|
4024
|
+
{
|
|
4025
|
+
modifiers: [
|
|
4026
|
+
{ name: 'ServiceTax', type: 'tax', _computed: { amount: 4 } },
|
|
4027
|
+
],
|
|
4028
|
+
},
|
|
4029
|
+
{
|
|
4030
|
+
modifiers: [{ name: 'VAT', type: 'tax', _computed: { amount: 2 } }],
|
|
4031
|
+
},
|
|
4032
|
+
],
|
|
4033
|
+
};
|
|
4034
|
+
|
|
4035
|
+
const result = pricingService.order.getTaxes(order);
|
|
4036
|
+
|
|
4037
|
+
expect(result).toEqual({
|
|
4038
|
+
VAT: 3, // 1 + 2
|
|
4039
|
+
ServiceTax: 4,
|
|
4040
|
+
});
|
|
4041
|
+
});
|
|
4042
|
+
|
|
4043
|
+
test('Skips non-tax modifiers completely', () => {
|
|
4044
|
+
const order = {
|
|
4045
|
+
items: [
|
|
4046
|
+
{
|
|
4047
|
+
modifiers: [
|
|
4048
|
+
{ name: 'Fee', type: 'fee', _computed: { amount: 10 } },
|
|
4049
|
+
{ name: 'VAT', type: 'tax', _computed: { amount: 2 } },
|
|
4050
|
+
],
|
|
4051
|
+
},
|
|
4052
|
+
{
|
|
4053
|
+
modifiers: [
|
|
4054
|
+
{ name: 'Other', type: 'env', _computed: { amount: 5 } },
|
|
4055
|
+
],
|
|
4056
|
+
},
|
|
4057
|
+
],
|
|
4058
|
+
};
|
|
4059
|
+
|
|
4060
|
+
const result = pricingService.order.getTaxes(order);
|
|
4061
|
+
|
|
4062
|
+
expect(result).toEqual({
|
|
4063
|
+
VAT: 2,
|
|
4064
|
+
});
|
|
4065
|
+
});
|
|
4066
|
+
});
|
|
3969
4067
|
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module.exports = ({ modifierActions, utils }) => {
|
|
2
|
+
const { math } = utils;
|
|
3
|
+
|
|
4
|
+
return function getTaxes(item) {
|
|
5
|
+
if (!item || !Array.isArray(item.modifiers)) return {};
|
|
6
|
+
|
|
7
|
+
const taxes = {};
|
|
8
|
+
|
|
9
|
+
item.modifiers.forEach(mod => {
|
|
10
|
+
if (!modifierActions.isTax(mod)) return;
|
|
11
|
+
|
|
12
|
+
const prevVal = taxes[mod.name] || 0;
|
|
13
|
+
taxes[mod.name] = math.add(prevVal, mod._computed.amount);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
return taxes;
|
|
17
|
+
};
|
|
18
|
+
};
|
package/lib/item/index.js
CHANGED
|
@@ -77,6 +77,7 @@ const isRemoveParentItem = require('./isRemoveParentItem');
|
|
|
77
77
|
const hasRelatedItems = require('./hasRelatedItems');
|
|
78
78
|
const getAddModifiers = require('./getAddModifiers');
|
|
79
79
|
const hasAddModifiers = require('./hasAddModifiers');
|
|
80
|
+
const getTaxes = require('./getTaxes');
|
|
80
81
|
|
|
81
82
|
const itemActions = (deps = {}) => {
|
|
82
83
|
const actions = {};
|
|
@@ -167,6 +168,7 @@ const itemActions = (deps = {}) => {
|
|
|
167
168
|
hasRelatedItems: hasRelatedItems(innerDeps),
|
|
168
169
|
getAddModifiers: getAddModifiers(innerDeps),
|
|
169
170
|
hasAddModifiers: hasAddModifiers(innerDeps),
|
|
171
|
+
getTaxes: getTaxes(innerDeps),
|
|
170
172
|
});
|
|
171
173
|
|
|
172
174
|
Object.keys(freezedActions).forEach(actionName => {
|
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 isTax = require('./isTax');
|
|
185
186
|
|
|
186
187
|
const modifierActions = (deps = {}) => {
|
|
187
188
|
const actions = {};
|
|
@@ -376,6 +377,7 @@ const modifierActions = (deps = {}) => {
|
|
|
376
377
|
getAmountMultiplier: getAmountMultiplier(innerDeps),
|
|
377
378
|
isAmountMultiplier: isAmountMultiplier(innerDeps),
|
|
378
379
|
isRemoveParentItem: isRemoveParentItem(innerDeps),
|
|
380
|
+
isTax: isTax(innerDeps),
|
|
379
381
|
});
|
|
380
382
|
|
|
381
383
|
Object.keys(freezedActions).forEach(actionName => {
|
|
@@ -23,7 +23,7 @@ module.exports = ({ actions, settings, _ }) => {
|
|
|
23
23
|
...rest,
|
|
24
24
|
parentId: orderSettings.allowSuborder ? _id : null,
|
|
25
25
|
status: {
|
|
26
|
-
order: 'open',
|
|
26
|
+
order: status.order || 'open',
|
|
27
27
|
delivery: status.delivery || false,
|
|
28
28
|
detailed: status.detailed || false,
|
|
29
29
|
paid: false,
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module.exports = ({ itemActions, utils }) => {
|
|
2
|
+
const { math } = utils;
|
|
3
|
+
|
|
4
|
+
return function getTaxes(order) {
|
|
5
|
+
if (!order || !Array.isArray(order.items)) return {};
|
|
6
|
+
|
|
7
|
+
const taxes = {};
|
|
8
|
+
|
|
9
|
+
order.items.forEach(item => {
|
|
10
|
+
const itemTaxes = itemActions.getTaxes(item);
|
|
11
|
+
|
|
12
|
+
Object.keys(itemTaxes).forEach(taxName => {
|
|
13
|
+
const prev = taxes[taxName] || 0;
|
|
14
|
+
taxes[taxName] = math.add(prev, itemTaxes[taxName]);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
return taxes;
|
|
19
|
+
};
|
|
20
|
+
};
|
package/lib/order/index.js
CHANGED
|
@@ -96,6 +96,7 @@ const copyItemToParents = require('./copyItemToParents');
|
|
|
96
96
|
const getItemsWithParents = require('./getItemsWithParents');
|
|
97
97
|
const addModifiersToParentItem = require('./addModifiersToParentItem');
|
|
98
98
|
const removeEmptyNotes = require('./removeEmptyNotes');
|
|
99
|
+
const getTaxes = require('./getTaxes');
|
|
99
100
|
|
|
100
101
|
const orderActions = (deps = {}) => {
|
|
101
102
|
const actions = {};
|
|
@@ -203,6 +204,7 @@ const orderActions = (deps = {}) => {
|
|
|
203
204
|
getItemsWithParents: getItemsWithParents(innerDeps),
|
|
204
205
|
addModifiersToParentItem: addModifiersToParentItem(innerDeps),
|
|
205
206
|
removeEmptyNotes: removeEmptyNotes(innerDeps),
|
|
207
|
+
getTaxes: getTaxes(innerDeps),
|
|
206
208
|
});
|
|
207
209
|
|
|
208
210
|
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.136",
|
|
4
4
|
"description": "Pricing calculator",
|
|
5
5
|
"author": "Dark POS",
|
|
6
6
|
"license": "ISC",
|
|
@@ -52,5 +52,5 @@
|
|
|
52
52
|
"supertest": "^6.2.3",
|
|
53
53
|
"supervisor": "^0.12.0"
|
|
54
54
|
},
|
|
55
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "9e4e2bf0729c8be75468d488d0199d2d74ec0139"
|
|
56
56
|
}
|