lazywait-calcs 1.0.4 → 1.0.6
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/lib/cjs/Calcs.js +241 -225
- package/lib/cjs/index.js +27 -21
- package/lib/cjs/setters.js +197 -0
- package/lib/cjs/types/Calcs.d.ts +21 -20
- package/lib/cjs/types/Calcs.d.ts.map +1 -1
- package/lib/cjs/types/index.d.ts +3 -2
- package/lib/cjs/types/index.d.ts.map +1 -1
- package/lib/cjs/types/setters.d.ts +19 -0
- package/lib/cjs/types/setters.d.ts.map +1 -0
- package/lib/cjs/types/types.d.ts +321 -38
- package/lib/cjs/types/types.d.ts.map +1 -1
- package/lib/cjs/types/utils.d.ts +2 -1
- package/lib/cjs/types/utils.d.ts.map +1 -1
- package/lib/cjs/utils.js +13 -15
- package/lib/esm/Calcs.js +230 -237
- package/lib/esm/index.js +3 -2
- package/lib/esm/setters.js +208 -0
- package/lib/esm/types/Calcs.d.ts +21 -20
- package/lib/esm/types/Calcs.d.ts.map +1 -1
- package/lib/esm/types/index.d.ts +3 -2
- package/lib/esm/types/index.d.ts.map +1 -1
- package/lib/esm/types/setters.d.ts +19 -0
- package/lib/esm/types/setters.d.ts.map +1 -0
- package/lib/esm/types/types.d.ts +321 -38
- package/lib/esm/types/types.d.ts.map +1 -1
- package/lib/esm/types/utils.d.ts +2 -1
- package/lib/esm/types/utils.d.ts.map +1 -1
- package/lib/esm/utils.js +10 -13
- package/package.json +1 -1
- package/lib/esm/index.mjs +0 -0
package/lib/esm/Calcs.js
CHANGED
|
@@ -1,176 +1,170 @@
|
|
|
1
1
|
import _ from 'lodash';
|
|
2
2
|
import moment from 'moment';
|
|
3
|
-
import { convertToDate, preventNaN, toFixedFloat } from './utils';
|
|
4
|
-
function ItemTotal(order_item, branch_vat, order_taxes, order_discounts, order_items_count = 1) {
|
|
3
|
+
import { convertToDate, preventNaN, toFixed, toFixedFloat } from './utils';
|
|
4
|
+
export function ItemTotal(order_item, branch_vat, order_taxes, order_discounts, order_items_count = 1) {
|
|
5
5
|
let total = 0;
|
|
6
6
|
const vat = branch_vat ? 1 + branch_vat / 100 : 1;
|
|
7
|
-
if (order_item
|
|
8
|
-
let price = order_item.price;
|
|
9
|
-
(order_item
|
|
10
|
-
if (addon
|
|
11
|
-
price += addon.price * addon.quantity;
|
|
7
|
+
if (order_item?.price && order_item?.quantity) {
|
|
8
|
+
let price = preventNaN(order_item.price);
|
|
9
|
+
(order_item.addons ?? []).forEach((addon) => {
|
|
10
|
+
if (addon?.price && addon?.quantity) {
|
|
11
|
+
price += preventNaN(addon.price) * preventNaN(addon.quantity);
|
|
12
12
|
}
|
|
13
13
|
});
|
|
14
14
|
const discounts = calcItemDiscounts(order_item, order_discounts, order_items_count);
|
|
15
15
|
price -= discounts;
|
|
16
|
-
let item_tax = _.sum(order_item.taxes
|
|
16
|
+
let item_tax = _.sum((order_item.taxes ?? []).map((tax) => {
|
|
17
17
|
return tax.isPercent ? ((tax.amount || 0) / 100) * price : tax.amount || 0;
|
|
18
18
|
}));
|
|
19
19
|
if (order_taxes) {
|
|
20
|
-
price += _.sum(order_taxes
|
|
21
|
-
return _tax.isPercent ? ((_tax.amount
|
|
22
|
-
})
|
|
20
|
+
price += _.sum((order_taxes ?? []).map((_tax) => {
|
|
21
|
+
return _tax.isPercent ? preventNaN((_tax.amount ?? 0) / 100) * price : preventNaN(_tax.amount ?? 0) / order_items_count;
|
|
22
|
+
}) ?? [0]);
|
|
23
23
|
}
|
|
24
24
|
if (order_item.not_taxable) {
|
|
25
|
-
total = price * order_item.quantity;
|
|
25
|
+
total = price * preventNaN(order_item.quantity);
|
|
26
26
|
}
|
|
27
27
|
else {
|
|
28
|
-
total = (price + item_tax) * order_item.quantity * vat;
|
|
28
|
+
total = (price + item_tax) * preventNaN(order_item.quantity) * vat;
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
return total;
|
|
32
32
|
}
|
|
33
|
-
function Subtotal(order_items) {
|
|
34
|
-
|
|
35
|
-
return _.sum(order_items.map(calcItem));
|
|
36
|
-
}
|
|
37
|
-
return 0;
|
|
33
|
+
export function Subtotal(order_items) {
|
|
34
|
+
return _.sum((order_items ?? []).map(calcItem));
|
|
38
35
|
}
|
|
39
|
-
function calcItem(order_item) {
|
|
36
|
+
export function calcItem(order_item) {
|
|
40
37
|
let result = 0;
|
|
41
|
-
(order_item?.addons
|
|
42
|
-
if (addon
|
|
43
|
-
result += addon.price * addon.quantity;
|
|
38
|
+
(order_item?.addons ?? []).forEach((addon) => {
|
|
39
|
+
if (addon?.price && addon?.quantity) {
|
|
40
|
+
result += preventNaN(addon.price) * preventNaN(addon.quantity);
|
|
44
41
|
}
|
|
45
42
|
});
|
|
46
|
-
if (order_item
|
|
47
|
-
return (result + order_item.price) * order_item.quantity;
|
|
43
|
+
if (order_item?.price && order_item?.quantity) {
|
|
44
|
+
return (result + preventNaN(order_item.price)) * preventNaN(order_item.quantity);
|
|
48
45
|
}
|
|
49
46
|
return result;
|
|
50
47
|
}
|
|
51
|
-
function Discounts(order_discounts, order_items, loyalty_amount, branch_vat) {
|
|
48
|
+
export function Discounts(order_discounts, order_items, loyalty_amount, branch_vat) {
|
|
52
49
|
let discounts = 0;
|
|
53
50
|
const subtotal = Subtotal(order_items);
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
amount = discount.amount;
|
|
64
|
-
}
|
|
65
|
-
else if (is_spendAtleast && subtotal < discount.spends_at_least_amount) {
|
|
66
|
-
null;
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
amount = discount.amount;
|
|
70
|
-
}
|
|
71
|
-
discounts += amount;
|
|
51
|
+
(order_discounts ?? []).forEach((discount) => {
|
|
52
|
+
if (discount?.amount) {
|
|
53
|
+
let is_percentage = !!discount?.is_percentage;
|
|
54
|
+
let is_spendAtleast = !!discount?.spends_at_least;
|
|
55
|
+
let amount = 0;
|
|
56
|
+
if (is_percentage)
|
|
57
|
+
amount = (subtotal * preventNaN(discount?.amount)) / 100;
|
|
58
|
+
else if (is_spendAtleast && subtotal >= (discount?.spends_at_least_amount ?? 0)) {
|
|
59
|
+
amount = preventNaN(discount?.amount);
|
|
72
60
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
61
|
+
else if (is_spendAtleast && subtotal < (discount?.spends_at_least_amount ?? 0)) {
|
|
62
|
+
null;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
amount = preventNaN(discount?.amount);
|
|
66
|
+
}
|
|
67
|
+
discounts += amount;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
(order_items ?? []).forEach((order_item) => {
|
|
71
|
+
(order_item?.discounts ?? []).forEach((discount) => {
|
|
72
|
+
if (discount?.is_percentage && discount?.amount) {
|
|
78
73
|
const addons_arr = order_item?.addons?.map((a) => (a.price ?? 0) * a.quantity || 0);
|
|
79
74
|
const addons_total = _.sum(addons_arr);
|
|
80
|
-
discounts += (order_item
|
|
75
|
+
discounts += (preventNaN(order_item?.price) + (addons_total || 0)) * preventNaN(order_item.quantity) * (preventNaN(discount?.amount) / 100);
|
|
81
76
|
}
|
|
82
|
-
else if (discount
|
|
83
|
-
discounts += discount
|
|
77
|
+
else if (discount?.amount) {
|
|
78
|
+
discounts += preventNaN(discount?.amount) * preventNaN(order_item.quantity);
|
|
84
79
|
}
|
|
85
80
|
});
|
|
86
81
|
});
|
|
87
82
|
if (loyalty_amount) {
|
|
88
83
|
if (branch_vat) {
|
|
89
|
-
const vat = 1 + branch_vat / 100;
|
|
90
|
-
discounts += loyalty_amount / vat;
|
|
84
|
+
const vat = 1 + preventNaN(branch_vat) / 100;
|
|
85
|
+
discounts += preventNaN(loyalty_amount) / vat;
|
|
91
86
|
}
|
|
92
87
|
else {
|
|
93
|
-
discounts += loyalty_amount;
|
|
88
|
+
discounts += preventNaN(loyalty_amount);
|
|
94
89
|
}
|
|
95
90
|
}
|
|
96
91
|
return discounts;
|
|
97
92
|
}
|
|
98
|
-
function DiscountsWithVat(order_discounts, order_items, loyalty_amount, branch_vat) {
|
|
93
|
+
export function DiscountsWithVat(order_discounts, order_items, loyalty_amount, branch_vat) {
|
|
99
94
|
let discounts = 0;
|
|
100
95
|
const subtotal = Subtotal(order_items);
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
amount = discount.amount;
|
|
111
|
-
}
|
|
112
|
-
else if (is_spendAtleast && subtotal < discount.spends_at_least_amount) {
|
|
113
|
-
null;
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
amount = discount.amount;
|
|
117
|
-
}
|
|
118
|
-
discounts += amount;
|
|
96
|
+
(order_discounts ?? []).forEach((discount) => {
|
|
97
|
+
if (discount?.amount) {
|
|
98
|
+
let is_percentage = !!discount?.is_percentage;
|
|
99
|
+
let is_spendAtleast = !!discount?.spends_at_least;
|
|
100
|
+
let amount = 0;
|
|
101
|
+
if (is_percentage)
|
|
102
|
+
amount = (subtotal * preventNaN(discount?.amount)) / 100;
|
|
103
|
+
else if (is_spendAtleast && subtotal >= (discount?.spends_at_least_amount ?? 0)) {
|
|
104
|
+
amount = preventNaN(discount?.amount);
|
|
119
105
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
106
|
+
else if (is_spendAtleast && subtotal < (discount?.spends_at_least_amount ?? 0)) {
|
|
107
|
+
null;
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
amount = preventNaN(discount?.amount);
|
|
111
|
+
}
|
|
112
|
+
discounts += amount;
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
(order_items ?? []).forEach((order_item) => {
|
|
116
|
+
(order_item?.discounts ?? []).forEach((discount) => {
|
|
117
|
+
if (!order_item?.not_taxable) {
|
|
118
|
+
if (discount?.is_percentage && discount?.amount) {
|
|
119
|
+
const addons_arr = order_item?.addons?.map((a) => preventNaN(a.price) * preventNaN(a.quantity));
|
|
127
120
|
const addons_total = _.sum(addons_arr);
|
|
128
|
-
discounts +=
|
|
121
|
+
discounts +=
|
|
122
|
+
(preventNaN(order_item?.price) + (addons_total || 0)) * preventNaN(order_item?.quantity) * (preventNaN(discount?.amount) / 100);
|
|
129
123
|
}
|
|
130
|
-
else if (discount
|
|
131
|
-
discounts += discount
|
|
124
|
+
else if (discount?.amount) {
|
|
125
|
+
discounts += preventNaN(discount?.amount) * preventNaN(order_item?.quantity);
|
|
132
126
|
}
|
|
133
127
|
}
|
|
134
128
|
});
|
|
135
129
|
});
|
|
136
130
|
if (loyalty_amount) {
|
|
137
131
|
if (branch_vat) {
|
|
138
|
-
const vat = 1 + branch_vat / 100;
|
|
139
|
-
discounts += loyalty_amount / vat;
|
|
132
|
+
const vat = 1 + preventNaN(branch_vat) / 100;
|
|
133
|
+
discounts += preventNaN(loyalty_amount) / vat;
|
|
140
134
|
}
|
|
141
135
|
else {
|
|
142
|
-
discounts += loyalty_amount;
|
|
136
|
+
discounts += preventNaN(loyalty_amount);
|
|
143
137
|
}
|
|
144
138
|
}
|
|
145
139
|
return discounts;
|
|
146
140
|
}
|
|
147
|
-
function ItemVat(order_item, branch_vat) {
|
|
141
|
+
export function ItemVat(order_item, branch_vat) {
|
|
148
142
|
const vat = branch_vat ? branch_vat / 100 : 1;
|
|
149
143
|
let addons = 0;
|
|
150
|
-
(order_item?.addons
|
|
151
|
-
if (addon
|
|
152
|
-
addons += toFixedFloat(addon
|
|
144
|
+
(order_item?.addons ?? []).forEach((addon) => {
|
|
145
|
+
if (addon?.price && addon?.quantity) {
|
|
146
|
+
addons += toFixedFloat(preventNaN(addon?.price) * preventNaN(addon?.quantity));
|
|
153
147
|
}
|
|
154
148
|
});
|
|
155
149
|
let discounts = 0;
|
|
156
|
-
(order_item?.discounts
|
|
157
|
-
if (discount
|
|
158
|
-
discounts += toFixedFloat((order_item
|
|
150
|
+
(order_item?.discounts ?? []).forEach((discount) => {
|
|
151
|
+
if (discount?.is_percentage && discount?.amount) {
|
|
152
|
+
discounts += toFixedFloat((preventNaN(order_item?.price) + addons) * preventNaN(order_item?.quantity) * (preventNaN(discount?.amount) / 100));
|
|
159
153
|
}
|
|
160
|
-
else if (discount
|
|
161
|
-
discounts += toFixedFloat(discount
|
|
154
|
+
else if (discount?.amount) {
|
|
155
|
+
discounts += toFixedFloat(preventNaN(discount?.amount) * preventNaN(order_item?.quantity));
|
|
162
156
|
}
|
|
163
157
|
});
|
|
164
|
-
const price = order_item
|
|
165
|
-
return order_item
|
|
158
|
+
const price = preventNaN(order_item?.price) + addons - discounts;
|
|
159
|
+
return order_item?.not_taxable ? 0 : toFixedFloat(price * vat);
|
|
166
160
|
}
|
|
167
|
-
function Vat(
|
|
161
|
+
export function Vat(order_items, order_discounts, order_taxes, order_deliveries, loyalty_amount, branch_vat) {
|
|
168
162
|
if (branch_vat && order_items.some((o) => !o.not_taxable)) {
|
|
169
|
-
const discount = DiscountsWithVat(
|
|
163
|
+
const discount = DiscountsWithVat(order_discounts ?? [], order_items, loyalty_amount ?? 0, branch_vat);
|
|
170
164
|
// Other charges for taxable items
|
|
171
|
-
const other_charges = Number(OtherCharges(order_items.filter((i) => !i.not_taxable),
|
|
165
|
+
const other_charges = Number(OtherCharges(order_items.filter((i) => !i.not_taxable), order_discounts, order_taxes, loyalty_amount, branch_vat));
|
|
172
166
|
// Order delivery
|
|
173
|
-
const deliveries = Deliveries(
|
|
167
|
+
const deliveries = Deliveries(order_deliveries || []);
|
|
174
168
|
// subtotal of items with vat
|
|
175
169
|
let sub_total = _.sum(order_items.filter((i) => !i.not_taxable).map(calcItem));
|
|
176
170
|
// VAT amount
|
|
@@ -179,13 +173,13 @@ function Vat(order, order_items, loyalty_amount, branch_vat) {
|
|
|
179
173
|
}
|
|
180
174
|
return 0;
|
|
181
175
|
}
|
|
182
|
-
function TaxableAmount(order_items) {
|
|
176
|
+
export function TaxableAmount(order_items) {
|
|
183
177
|
return _.sum((order_items || []).filter((i) => !i.not_taxable).map(calcItem));
|
|
184
178
|
}
|
|
185
|
-
function NonTaxableAmount(order_items) {
|
|
179
|
+
export function NonTaxableAmount(order_items) {
|
|
186
180
|
return _.sum((order_items || []).filter((i) => i.not_taxable).map(calcItem));
|
|
187
181
|
}
|
|
188
|
-
function Deliveries(order_deliveries) {
|
|
182
|
+
export function Deliveries(order_deliveries) {
|
|
189
183
|
let sum = 0;
|
|
190
184
|
if (order_deliveries) {
|
|
191
185
|
(order_deliveries || []).forEach((d) => {
|
|
@@ -199,140 +193,130 @@ function Deliveries(order_deliveries) {
|
|
|
199
193
|
}
|
|
200
194
|
return sum;
|
|
201
195
|
}
|
|
202
|
-
function OtherCharges(order_items,
|
|
196
|
+
export function OtherCharges(order_items, order_discounts, order_taxes, loyalty_amount, branch_vat) {
|
|
203
197
|
const sub = Subtotal(order_items);
|
|
204
|
-
const discounts = Number(Discounts(
|
|
205
|
-
let other_charges =
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
(order_items || []).forEach((order_item) => {
|
|
218
|
-
const addons_total = _.sum(order_item?.addons?.map((a) => (a.price || 0) * (a.quantity || 0)));
|
|
219
|
-
const price = (order_item.price + addons_total) * order_item.quantity;
|
|
198
|
+
const discounts = Number(Discounts(order_discounts, order_items, loyalty_amount, branch_vat));
|
|
199
|
+
let other_charges = order_taxes
|
|
200
|
+
? _.sum(order_taxes.map((tax) => {
|
|
201
|
+
if (tax.amount) {
|
|
202
|
+
let result = tax.isPercent ? (tax.amount / 100) * (sub - discounts) : tax.amount;
|
|
203
|
+
return result;
|
|
204
|
+
}
|
|
205
|
+
return 0;
|
|
206
|
+
}))
|
|
207
|
+
: 0;
|
|
208
|
+
(order_items ?? []).forEach((order_item) => {
|
|
209
|
+
const addons_total = _.sum(order_item?.addons?.map((a) => preventNaN(a.price ?? 0) * preventNaN(a.quantity ?? 0)));
|
|
210
|
+
const price = (preventNaN(order_item?.price) + addons_total) * preventNaN(order_item?.quantity);
|
|
220
211
|
let _discounts = 0;
|
|
221
|
-
(order_item?.discounts
|
|
222
|
-
if (discount
|
|
223
|
-
_discounts += price * (discount
|
|
212
|
+
(order_item?.discounts ?? []).forEach((discount) => {
|
|
213
|
+
if (discount?.is_percentage && discount?.amount) {
|
|
214
|
+
_discounts += price * (preventNaN(discount?.amount) / 100);
|
|
224
215
|
}
|
|
225
|
-
else if (discount
|
|
226
|
-
_discounts += discount
|
|
216
|
+
else if (discount?.amount) {
|
|
217
|
+
_discounts += preventNaN(discount?.amount) * preventNaN(order_item?.quantity);
|
|
227
218
|
}
|
|
228
219
|
});
|
|
229
|
-
const order_items_count = _.sum(order_items.map((i) => i
|
|
230
|
-
(
|
|
231
|
-
if (discount
|
|
232
|
-
_discounts += price * (discount
|
|
220
|
+
const order_items_count = _.sum((order_items ?? []).map((i) => preventNaN(i?.quantity)));
|
|
221
|
+
(order_discounts ?? []).forEach((discount) => {
|
|
222
|
+
if (discount?.is_percentage && discount?.amount) {
|
|
223
|
+
_discounts += price * (preventNaN(discount?.amount) / 100);
|
|
233
224
|
}
|
|
234
|
-
else if (discount
|
|
235
|
-
_discounts += (discount
|
|
225
|
+
else if (discount?.amount) {
|
|
226
|
+
_discounts += (preventNaN(discount?.amount) / order_items_count) * preventNaN(order_item?.quantity);
|
|
236
227
|
}
|
|
237
228
|
});
|
|
238
|
-
(order_item?.taxes
|
|
239
|
-
if (tax
|
|
229
|
+
(order_item?.taxes ?? []).forEach((tax) => {
|
|
230
|
+
if (tax?.isPercent && tax?.amount) {
|
|
240
231
|
let temp = price - _discounts;
|
|
241
232
|
if (temp < 0) {
|
|
242
233
|
temp = 0;
|
|
243
234
|
}
|
|
244
|
-
other_charges += temp * (tax
|
|
235
|
+
other_charges += temp * (preventNaN(tax?.amount) / 100);
|
|
245
236
|
}
|
|
246
|
-
else if (tax
|
|
247
|
-
other_charges += tax
|
|
237
|
+
else if (tax?.amount) {
|
|
238
|
+
other_charges += preventNaN(tax?.amount) * preventNaN(order_item?.quantity ?? 0);
|
|
248
239
|
}
|
|
249
240
|
});
|
|
250
241
|
});
|
|
251
242
|
return other_charges;
|
|
252
243
|
}
|
|
253
|
-
function printedOrderTaxes(
|
|
244
|
+
export function printedOrderTaxes(order_items, order_discounts, order_taxes, loyalty_amount, branch_vat) {
|
|
254
245
|
let taxes = [];
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
if (
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
amount: tax.amount,
|
|
267
|
-
total: tax.isPercent ? (tax.amount / 100) * (sub - discounts) : tax.amount,
|
|
268
|
-
});
|
|
269
|
-
}
|
|
246
|
+
const sub = Subtotal(order_items);
|
|
247
|
+
const discounts = Number(Discounts(order_discounts ?? [], order_items ?? [], loyalty_amount ?? 0, branch_vat ?? 0));
|
|
248
|
+
(order_taxes ?? []).forEach((tax) => {
|
|
249
|
+
if (tax?.amount) {
|
|
250
|
+
taxes.push({
|
|
251
|
+
tax_id: tax?.tax_id ?? '',
|
|
252
|
+
name_lan_p: tax?.name_lan_p ?? '',
|
|
253
|
+
name_lan_s: tax?.name_lan_s ?? '',
|
|
254
|
+
isPercent: !!tax?.isPercent,
|
|
255
|
+
amount: preventNaN(tax?.amount),
|
|
256
|
+
total: tax?.isPercent ? (preventNaN(tax?.amount) / 100) * (sub - discounts) : preventNaN(tax?.amount),
|
|
270
257
|
});
|
|
271
258
|
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
charge = (tax.amount || 0) * order_item.quantity;
|
|
284
|
-
}
|
|
285
|
-
if (!list.some((t) => t.tax_id === tax.tax_id)) {
|
|
286
|
-
list.push({
|
|
287
|
-
tax_id: tax.tax_id,
|
|
288
|
-
name_lan_p: tax.name_lan_p || '',
|
|
289
|
-
name_lan_s: tax.name_lan_s,
|
|
290
|
-
isPercent: tax.isPercent,
|
|
291
|
-
amount: tax.amount || 0,
|
|
292
|
-
total: charge,
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
else {
|
|
296
|
-
list = list.map((t) => (t.tax_id !== tax.tax_id ? t : { ...t, total: t.total + charge }));
|
|
297
|
-
}
|
|
259
|
+
});
|
|
260
|
+
const order_items_count = _.sum((order_items ?? []).map((i) => preventNaN(i.quantity)));
|
|
261
|
+
const item_taxes = (order_items ?? []).reduce((list, order_item) => {
|
|
262
|
+
const addons_amount = calcItemAddons(order_item);
|
|
263
|
+
(order_item?.taxes ?? []).forEach((tax) => {
|
|
264
|
+
let _discounts = calcItemDiscounts(order_item, order_discounts, order_items_count);
|
|
265
|
+
if (tax?.tax_id) {
|
|
266
|
+
let charge = 0;
|
|
267
|
+
if (tax?.isPercent && tax?.amount) {
|
|
268
|
+
charge =
|
|
269
|
+
((preventNaN(tax?.amount) ?? 0) / 100) * (preventNaN(order_item?.price) + addons_amount - _discounts) * preventNaN(order_item?.quantity);
|
|
298
270
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
271
|
+
else if (tax?.amount) {
|
|
272
|
+
charge = preventNaN(tax?.amount) * preventNaN(order_item?.quantity);
|
|
273
|
+
}
|
|
274
|
+
if (!list.some((t) => t.tax_id === tax?.tax_id)) {
|
|
275
|
+
list.push({
|
|
276
|
+
tax_id: tax?.tax_id,
|
|
277
|
+
name_lan_p: tax?.name_lan_p ?? '',
|
|
278
|
+
name_lan_s: tax?.name_lan_s,
|
|
279
|
+
isPercent: tax?.isPercent,
|
|
280
|
+
amount: tax?.amount ?? 0,
|
|
281
|
+
total: charge,
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
list = list.map((t) => (t.tax_id !== tax.tax_id ? t : { ...t, total: preventNaN(t.total) + charge }));
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
return list;
|
|
290
|
+
}, []);
|
|
291
|
+
return _.concat(taxes, item_taxes);
|
|
305
292
|
}
|
|
306
|
-
function Total(
|
|
293
|
+
export function Total(order_items, order_discounts, order_taxes, order_deliveries, loyalty_amount, branch_vat) {
|
|
307
294
|
const sub = Number(Subtotal(order_items));
|
|
308
|
-
const discounts = Number(Discounts(
|
|
309
|
-
const other_charges = Number(OtherCharges(order_items,
|
|
310
|
-
const vat = Number(Vat(
|
|
311
|
-
const deliveries = Number(Deliveries(
|
|
295
|
+
const discounts = Number(Discounts(order_discounts ?? [], order_items ?? [], loyalty_amount ?? 0, branch_vat ?? 0));
|
|
296
|
+
const other_charges = Number(OtherCharges(order_items ?? [], order_discounts ?? [], order_taxes ?? [], loyalty_amount ?? 0, branch_vat ?? 0));
|
|
297
|
+
const vat = Number(Vat(order_items ?? [], order_discounts ?? [], order_taxes ?? [], order_deliveries ?? [], loyalty_amount ?? 0, branch_vat ?? 0));
|
|
298
|
+
const deliveries = Number(Deliveries(order_deliveries ?? []));
|
|
312
299
|
const total = toFixedFloat(sub - discounts + other_charges + vat + deliveries);
|
|
313
|
-
|
|
314
|
-
return 0;
|
|
315
|
-
return total;
|
|
300
|
+
return total > 0 ? total : 0;
|
|
316
301
|
}
|
|
317
|
-
function calcPaymentBalanceDue(invoice, branch) {
|
|
318
|
-
let due = (invoice
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
});
|
|
323
|
-
}
|
|
302
|
+
export function calcPaymentBalanceDue(invoice, branch) {
|
|
303
|
+
let due = preventNaN(invoice?.total ?? 0) - preventNaN(invoice?.received_amount ?? 0);
|
|
304
|
+
(branch?.payment_methods ?? []).forEach((payment) => {
|
|
305
|
+
due -= preventNaN(payment?.payment_amount ?? 0);
|
|
306
|
+
});
|
|
324
307
|
return due > 0 ? due : 0;
|
|
325
308
|
}
|
|
326
|
-
function calPaymentTotal(info) {
|
|
327
|
-
let groupByPaymnets = _.map(info
|
|
328
|
-
name_lan_p: p.name_lan_p,
|
|
329
|
-
name_lan_s: p.name_lan_s,
|
|
309
|
+
export function calPaymentTotal(info) {
|
|
310
|
+
let groupByPaymnets = _.map(info?.payments ?? [], (p) => ({
|
|
311
|
+
name_lan_p: p.name_lan_p ?? '',
|
|
312
|
+
name_lan_s: p.name_lan_s ?? '',
|
|
330
313
|
amount: p.payment_amount,
|
|
331
314
|
}));
|
|
332
315
|
groupByPaymnets = groupByPaymnets.reduce(function (result, acc) {
|
|
333
316
|
if (acc.name_lan_p !== 'CASH_ADD' && acc.name_lan_p !== 'CASH_REMOVE') {
|
|
334
|
-
|
|
335
|
-
|
|
317
|
+
if (!result[acc.name_lan_p])
|
|
318
|
+
result[acc.name_lan_p] = [];
|
|
319
|
+
let val = toFixedFloat(preventNaN(result[acc.name_lan_p].payment_amount));
|
|
336
320
|
result[acc.name_lan_p] = {
|
|
337
321
|
name_lan_p: acc.name_lan_p,
|
|
338
322
|
name_lan_s: acc.name_lan_s,
|
|
@@ -343,55 +327,64 @@ function calPaymentTotal(info) {
|
|
|
343
327
|
}, {});
|
|
344
328
|
return groupByPaymnets;
|
|
345
329
|
}
|
|
346
|
-
function calcClosingCashBalance(closing, opening_start_cash) {
|
|
347
|
-
return (preventNaN(closing
|
|
348
|
-
preventNaN(opening_start_cash
|
|
349
|
-
(preventNaN(closing
|
|
330
|
+
export function calcClosingCashBalance(closing, opening_start_cash) {
|
|
331
|
+
return (preventNaN(closing?.closing_cash_sales ?? 0) +
|
|
332
|
+
preventNaN(opening_start_cash ?? 0) +
|
|
333
|
+
(preventNaN(closing?.closing_cash_add ?? 0) - preventNaN(closing?.closing_cash_remove ?? 0)));
|
|
350
334
|
}
|
|
351
|
-
function calcItemDiscounts(item, order_discounts, order_items_count) {
|
|
335
|
+
export function calcItemDiscounts(item, order_discounts, order_items_count) {
|
|
352
336
|
let discounts = 0;
|
|
353
337
|
let addons = 0;
|
|
354
|
-
(item?.addons
|
|
355
|
-
if (addon
|
|
356
|
-
addons += addon
|
|
338
|
+
(item?.addons ?? []).forEach((addon) => {
|
|
339
|
+
if (addon?.price && addon?.quantity) {
|
|
340
|
+
addons += preventNaN(addon?.price) * preventNaN(addon?.quantity);
|
|
357
341
|
}
|
|
358
342
|
});
|
|
359
|
-
(item?.discounts
|
|
360
|
-
if (discount
|
|
361
|
-
discounts += (item
|
|
343
|
+
(item?.discounts ?? []).forEach((discount) => {
|
|
344
|
+
if (discount?.is_percentage && discount?.amount) {
|
|
345
|
+
discounts += (preventNaN(item?.price) + addons) * (preventNaN(discount?.amount) / 100);
|
|
362
346
|
}
|
|
363
|
-
else if (discount
|
|
364
|
-
discounts += discount
|
|
347
|
+
else if (discount?.amount) {
|
|
348
|
+
discounts += preventNaN(discount?.amount);
|
|
365
349
|
}
|
|
366
350
|
});
|
|
367
|
-
(order_discounts
|
|
368
|
-
if (discount
|
|
369
|
-
discounts += (item
|
|
351
|
+
(order_discounts ?? []).forEach((discount) => {
|
|
352
|
+
if (discount?.is_percentage && discount?.amount) {
|
|
353
|
+
discounts += (preventNaN(item?.price) + addons) * (preventNaN(discount?.amount) / 100);
|
|
370
354
|
}
|
|
371
|
-
else if (discount
|
|
372
|
-
discounts += discount
|
|
355
|
+
else if (discount?.amount) {
|
|
356
|
+
discounts += preventNaN(discount?.amount) / order_items_count;
|
|
373
357
|
}
|
|
374
358
|
});
|
|
375
359
|
return discounts;
|
|
376
360
|
}
|
|
377
|
-
function calcItemAddons(item) {
|
|
361
|
+
export function calcItemAddons(item) {
|
|
378
362
|
let addons = 0;
|
|
379
|
-
(item?.addons
|
|
380
|
-
if (addon
|
|
381
|
-
addons += addon
|
|
363
|
+
(item?.addons ?? []).forEach((addon) => {
|
|
364
|
+
if (addon?.price && addon?.quantity) {
|
|
365
|
+
addons += preventNaN(addon?.price) * preventNaN(addon?.quantity);
|
|
382
366
|
}
|
|
383
367
|
});
|
|
384
368
|
return addons;
|
|
385
369
|
}
|
|
386
|
-
function getInvoicesDiffrence(openning, invoices) {
|
|
387
|
-
const now = moment();
|
|
388
|
-
const openningTime = moment(convertToDate(openning
|
|
389
|
-
const orders = invoices.
|
|
390
|
-
const invoiceDate = moment(convertToDate(invoice
|
|
391
|
-
|
|
392
|
-
return invoice;
|
|
393
|
-
}
|
|
370
|
+
export function getInvoicesDiffrence(openning, invoices) {
|
|
371
|
+
const now = moment(new Date());
|
|
372
|
+
const openningTime = moment(convertToDate(openning?.cashier_opening_date));
|
|
373
|
+
const orders = (invoices ?? []).filter((invoice) => {
|
|
374
|
+
const invoiceDate = moment(convertToDate(invoice?.order_date));
|
|
375
|
+
return !invoiceDate.isBetween(openningTime, now);
|
|
394
376
|
});
|
|
395
377
|
return orders;
|
|
396
378
|
}
|
|
397
|
-
export
|
|
379
|
+
export function WithVat(branch_vat, price) {
|
|
380
|
+
if (branch_vat && price) {
|
|
381
|
+
return parseFloat(toFixed(price * (1 + branch_vat / 100)));
|
|
382
|
+
}
|
|
383
|
+
return price ?? 0;
|
|
384
|
+
}
|
|
385
|
+
export function WithoutVat(branch_vat, price) {
|
|
386
|
+
if (branch_vat && price) {
|
|
387
|
+
return price / (1 + branch_vat / 100);
|
|
388
|
+
}
|
|
389
|
+
return price ?? 0;
|
|
390
|
+
}
|
package/lib/esm/index.js
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
1
|
+
export * as Calcs from './Calcs';
|
|
2
|
+
export * as Setters from './setters';
|
|
3
|
+
export * as Types from './types';
|