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