@unchainedshop/core-orders 2.4.3 → 2.5.1
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.
|
@@ -2,57 +2,80 @@ import { BasePricingSheet } from '@unchainedshop/utils';
|
|
|
2
2
|
import { OrderPricingRowCategory, } from '@unchainedshop/types/orders.pricing.js';
|
|
3
3
|
export const OrderPricingSheet = (params) => {
|
|
4
4
|
const basePricingSheet = BasePricingSheet(params);
|
|
5
|
+
const addTaxIfAvailabile = (category, taxAmount, meta, discountId) => {
|
|
6
|
+
if (taxAmount) {
|
|
7
|
+
basePricingSheet.calculation.push({
|
|
8
|
+
category: OrderPricingRowCategory.Taxes,
|
|
9
|
+
amount: taxAmount,
|
|
10
|
+
baseCategory: category,
|
|
11
|
+
discountId,
|
|
12
|
+
meta,
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
};
|
|
5
16
|
const pricingSheet = {
|
|
6
17
|
...basePricingSheet,
|
|
7
|
-
addItems({ amount, meta }) {
|
|
18
|
+
addItems({ amount, taxAmount, meta }) {
|
|
8
19
|
basePricingSheet.calculation.push({
|
|
9
20
|
category: OrderPricingRowCategory.Items,
|
|
10
21
|
amount,
|
|
11
22
|
meta,
|
|
12
23
|
});
|
|
24
|
+
addTaxIfAvailabile(OrderPricingRowCategory.Items, taxAmount, meta);
|
|
13
25
|
},
|
|
14
|
-
addDiscount({ amount, discountId, meta }) {
|
|
26
|
+
addDiscount({ amount, taxAmount, discountId, meta }) {
|
|
15
27
|
basePricingSheet.calculation.push({
|
|
16
28
|
category: OrderPricingRowCategory.Discounts,
|
|
17
29
|
amount,
|
|
18
30
|
discountId,
|
|
19
31
|
meta,
|
|
20
32
|
});
|
|
33
|
+
addTaxIfAvailabile(OrderPricingRowCategory.Discounts, taxAmount, meta, discountId);
|
|
21
34
|
},
|
|
22
|
-
|
|
23
|
-
basePricingSheet.calculation.push({
|
|
24
|
-
category: OrderPricingRowCategory.Taxes,
|
|
25
|
-
amount,
|
|
26
|
-
meta,
|
|
27
|
-
});
|
|
28
|
-
},
|
|
29
|
-
addDelivery({ amount, meta }) {
|
|
35
|
+
addDelivery({ amount, taxAmount, meta }) {
|
|
30
36
|
basePricingSheet.calculation.push({
|
|
31
37
|
category: OrderPricingRowCategory.Delivery,
|
|
32
38
|
amount,
|
|
33
39
|
meta,
|
|
34
40
|
});
|
|
41
|
+
addTaxIfAvailabile(OrderPricingRowCategory.Delivery, taxAmount, meta);
|
|
35
42
|
},
|
|
36
|
-
addPayment({ amount, meta }) {
|
|
43
|
+
addPayment({ amount, taxAmount, meta }) {
|
|
37
44
|
basePricingSheet.calculation.push({
|
|
38
45
|
category: OrderPricingRowCategory.Payment,
|
|
39
46
|
amount,
|
|
40
47
|
meta,
|
|
41
48
|
});
|
|
49
|
+
addTaxIfAvailabile(OrderPricingRowCategory.Payment, taxAmount, meta);
|
|
42
50
|
},
|
|
43
|
-
|
|
44
|
-
// tax is included 2 times, this is only true for Order Pricing!
|
|
45
|
-
return basePricingSheet.sum() - pricingSheet.taxSum();
|
|
46
|
-
},
|
|
47
|
-
taxSum() {
|
|
51
|
+
taxSum(filter) {
|
|
48
52
|
return basePricingSheet.sum({
|
|
49
53
|
category: OrderPricingRowCategory.Taxes,
|
|
54
|
+
...(filter || {}),
|
|
50
55
|
});
|
|
51
56
|
},
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
57
|
+
gross() {
|
|
58
|
+
// tax is included 2 times, this is only true for Order Pricing!
|
|
59
|
+
return basePricingSheet.sum() - this.taxSum();
|
|
60
|
+
},
|
|
61
|
+
net() {
|
|
62
|
+
return basePricingSheet.sum() - this.taxSum() - this.taxSum();
|
|
63
|
+
},
|
|
64
|
+
total({ category, useNetPrice } = { useNetPrice: false }) {
|
|
65
|
+
if (!category) {
|
|
66
|
+
// WORKAROUND WHEN NO CATEGORY IS GIVEN
|
|
67
|
+
return {
|
|
68
|
+
amount: Math.round(useNetPrice ? this.net() : this.gross()),
|
|
69
|
+
currency: params.currency,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
const grossAmountForCategory = this.sum({ category });
|
|
73
|
+
const taxAmountForCategory = this.taxSum({ baseCategory: category });
|
|
74
|
+
const amount = Math.round(useNetPrice ? grossAmountForCategory - taxAmountForCategory : grossAmountForCategory);
|
|
75
|
+
return {
|
|
76
|
+
amount,
|
|
77
|
+
currency: params.currency,
|
|
78
|
+
};
|
|
56
79
|
},
|
|
57
80
|
discountSum(discountId) {
|
|
58
81
|
return basePricingSheet.sum({
|
|
@@ -87,26 +110,6 @@ export const OrderPricingSheet = (params) => {
|
|
|
87
110
|
discountId,
|
|
88
111
|
});
|
|
89
112
|
},
|
|
90
|
-
getItemsRows() {
|
|
91
|
-
return basePricingSheet.filterBy({
|
|
92
|
-
category: OrderPricingRowCategory.Items,
|
|
93
|
-
});
|
|
94
|
-
},
|
|
95
|
-
getTaxRows() {
|
|
96
|
-
return basePricingSheet.filterBy({
|
|
97
|
-
category: OrderPricingRowCategory.Taxes,
|
|
98
|
-
});
|
|
99
|
-
},
|
|
100
|
-
getDeliveryRows() {
|
|
101
|
-
return basePricingSheet.filterBy({
|
|
102
|
-
category: OrderPricingRowCategory.Delivery,
|
|
103
|
-
});
|
|
104
|
-
},
|
|
105
|
-
getPaymentRows() {
|
|
106
|
-
return basePricingSheet.filterBy({
|
|
107
|
-
category: OrderPricingRowCategory.Payment,
|
|
108
|
-
});
|
|
109
|
-
},
|
|
110
113
|
};
|
|
111
114
|
return pricingSheet;
|
|
112
115
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OrderPricingSheet.js","sourceRoot":"","sources":["../../src/director/OrderPricingSheet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAEL,uBAAuB,GAExB,MAAM,wCAAwC,CAAC;AAGhD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CA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BACX,CAAC,CAAC;gBACH,IAAI,CAAC,MAAM,EAAE;oBACX,OAAO,IAAI,CAAC;iBACb;gBACD,OAAO;oBACL,UAAU;oBACV,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;oBAC1B,QAAQ,EAAE,gBAAgB,CAAC,QAAQ;iBACpC,CAAC;YACJ,CAAC,CAAC;iBACD,MAAM,CAAC,OAAO,CAAC,CAAC;QACrB,CAAC;QAED,eAAe,CAAC,UAAU;YACxB,OAAO,gBAAgB,CAAC,QAAQ,CAAC;gBAC/B,QAAQ,EAAE,uBAAuB,CAAC,SAAS;gBAC3C,UAAU;aACX,CAAC,CAAC;QACL,CAAC;KACF,CAAC;IAEF,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/core-orders",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.1",
|
|
4
4
|
"main": "lib/orders-index.js",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./lib/orders-index.js",
|
|
@@ -31,14 +31,14 @@
|
|
|
31
31
|
},
|
|
32
32
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@unchainedshop/events": "^2.
|
|
35
|
-
"@unchainedshop/logger": "^2.
|
|
36
|
-
"@unchainedshop/utils": "^2.
|
|
34
|
+
"@unchainedshop/events": "^2.5.1",
|
|
35
|
+
"@unchainedshop/logger": "^2.5.1",
|
|
36
|
+
"@unchainedshop/utils": "^2.5.1",
|
|
37
37
|
"simpl-schema": "=3.2.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@types/node": "^18.17.
|
|
41
|
-
"@unchainedshop/types": "^2.
|
|
40
|
+
"@types/node": "^18.17.15",
|
|
41
|
+
"@unchainedshop/types": "^2.5.1",
|
|
42
42
|
"jest": "^29.6.4",
|
|
43
43
|
"ts-jest": "^29.1.1",
|
|
44
44
|
"typescript": "^5.2.2"
|
|
@@ -11,65 +11,96 @@ export const OrderPricingSheet = (
|
|
|
11
11
|
): IOrderPricingSheet => {
|
|
12
12
|
const basePricingSheet: IBasePricingSheet<OrderPricingCalculation> = BasePricingSheet(params);
|
|
13
13
|
|
|
14
|
+
const addTaxIfAvailabile = (category: string, taxAmount?: number, meta?: any, discountId?: string) => {
|
|
15
|
+
if (taxAmount) {
|
|
16
|
+
basePricingSheet.calculation.push({
|
|
17
|
+
category: OrderPricingRowCategory.Taxes,
|
|
18
|
+
amount: taxAmount,
|
|
19
|
+
baseCategory: category,
|
|
20
|
+
discountId,
|
|
21
|
+
meta,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
14
26
|
const pricingSheet: IOrderPricingSheet = {
|
|
15
27
|
...basePricingSheet,
|
|
16
28
|
|
|
17
|
-
addItems({ amount, meta }) {
|
|
29
|
+
addItems({ amount, taxAmount, meta }) {
|
|
18
30
|
basePricingSheet.calculation.push({
|
|
19
31
|
category: OrderPricingRowCategory.Items,
|
|
20
32
|
amount,
|
|
21
33
|
meta,
|
|
22
34
|
});
|
|
35
|
+
|
|
36
|
+
addTaxIfAvailabile(OrderPricingRowCategory.Items, taxAmount, meta);
|
|
23
37
|
},
|
|
24
38
|
|
|
25
|
-
addDiscount({ amount,
|
|
39
|
+
addDiscount({ amount, taxAmount, discountId, meta }) {
|
|
26
40
|
basePricingSheet.calculation.push({
|
|
27
41
|
category: OrderPricingRowCategory.Discounts,
|
|
28
42
|
amount,
|
|
29
43
|
discountId,
|
|
30
44
|
meta,
|
|
31
45
|
});
|
|
32
|
-
},
|
|
33
46
|
|
|
34
|
-
|
|
35
|
-
basePricingSheet.calculation.push({
|
|
36
|
-
category: OrderPricingRowCategory.Taxes,
|
|
37
|
-
amount,
|
|
38
|
-
meta,
|
|
39
|
-
});
|
|
47
|
+
addTaxIfAvailabile(OrderPricingRowCategory.Discounts, taxAmount, meta, discountId);
|
|
40
48
|
},
|
|
41
49
|
|
|
42
|
-
addDelivery({ amount, meta }) {
|
|
50
|
+
addDelivery({ amount, taxAmount, meta }) {
|
|
43
51
|
basePricingSheet.calculation.push({
|
|
44
52
|
category: OrderPricingRowCategory.Delivery,
|
|
45
53
|
amount,
|
|
46
54
|
meta,
|
|
47
55
|
});
|
|
56
|
+
|
|
57
|
+
addTaxIfAvailabile(OrderPricingRowCategory.Delivery, taxAmount, meta);
|
|
48
58
|
},
|
|
49
59
|
|
|
50
|
-
addPayment({ amount, meta }) {
|
|
60
|
+
addPayment({ amount, taxAmount, meta }) {
|
|
51
61
|
basePricingSheet.calculation.push({
|
|
52
62
|
category: OrderPricingRowCategory.Payment,
|
|
53
63
|
amount,
|
|
54
64
|
meta,
|
|
55
65
|
});
|
|
56
|
-
},
|
|
57
66
|
|
|
58
|
-
|
|
59
|
-
// tax is included 2 times, this is only true for Order Pricing!
|
|
60
|
-
return basePricingSheet.sum() - pricingSheet.taxSum();
|
|
67
|
+
addTaxIfAvailabile(OrderPricingRowCategory.Payment, taxAmount, meta);
|
|
61
68
|
},
|
|
62
69
|
|
|
63
|
-
taxSum() {
|
|
70
|
+
taxSum(filter) {
|
|
64
71
|
return basePricingSheet.sum({
|
|
65
72
|
category: OrderPricingRowCategory.Taxes,
|
|
73
|
+
...(filter || {}),
|
|
66
74
|
});
|
|
67
75
|
},
|
|
68
76
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
77
|
+
gross() {
|
|
78
|
+
// tax is included 2 times, this is only true for Order Pricing!
|
|
79
|
+
return basePricingSheet.sum() - this.taxSum();
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
net() {
|
|
83
|
+
return basePricingSheet.sum() - this.taxSum() - this.taxSum();
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
total({ category, useNetPrice } = { useNetPrice: false }) {
|
|
87
|
+
if (!category) {
|
|
88
|
+
// WORKAROUND WHEN NO CATEGORY IS GIVEN
|
|
89
|
+
return {
|
|
90
|
+
amount: Math.round(useNetPrice ? this.net() : this.gross()),
|
|
91
|
+
currency: params.currency,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const grossAmountForCategory = this.sum({ category });
|
|
96
|
+
const taxAmountForCategory = this.taxSum({ baseCategory: category });
|
|
97
|
+
const amount = Math.round(
|
|
98
|
+
useNetPrice ? grossAmountForCategory - taxAmountForCategory : grossAmountForCategory,
|
|
99
|
+
);
|
|
100
|
+
return {
|
|
101
|
+
amount,
|
|
102
|
+
currency: params.currency,
|
|
103
|
+
};
|
|
73
104
|
},
|
|
74
105
|
|
|
75
106
|
discountSum(discountId) {
|
|
@@ -108,30 +139,6 @@ export const OrderPricingSheet = (
|
|
|
108
139
|
discountId,
|
|
109
140
|
});
|
|
110
141
|
},
|
|
111
|
-
|
|
112
|
-
getItemsRows() {
|
|
113
|
-
return basePricingSheet.filterBy({
|
|
114
|
-
category: OrderPricingRowCategory.Items,
|
|
115
|
-
});
|
|
116
|
-
},
|
|
117
|
-
|
|
118
|
-
getTaxRows() {
|
|
119
|
-
return basePricingSheet.filterBy({
|
|
120
|
-
category: OrderPricingRowCategory.Taxes,
|
|
121
|
-
});
|
|
122
|
-
},
|
|
123
|
-
|
|
124
|
-
getDeliveryRows() {
|
|
125
|
-
return basePricingSheet.filterBy({
|
|
126
|
-
category: OrderPricingRowCategory.Delivery,
|
|
127
|
-
});
|
|
128
|
-
},
|
|
129
|
-
|
|
130
|
-
getPaymentRows() {
|
|
131
|
-
return basePricingSheet.filterBy({
|
|
132
|
-
category: OrderPricingRowCategory.Payment,
|
|
133
|
-
});
|
|
134
|
-
},
|
|
135
142
|
};
|
|
136
143
|
|
|
137
144
|
return pricingSheet;
|