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