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/esm/Calcs.js CHANGED
@@ -1,19 +1,23 @@
1
- import _ from 'lodash';
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, toFixed, toFixedFloat } from './utils';
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?.price && order_item?.quantity) {
8
- let price = preventNaN(order_item.price);
9
- (order_item.addons ?? []).forEach((addon) => {
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 += preventNaN(addon.price) * preventNaN(addon.quantity);
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 = _.sum(order_item.taxes?.map((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 += _.sum(order_taxes?.map((_tax) => {
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 * preventNaN(order_item.quantity);
38
+ total = price * order_item.quantity;
35
39
  }
36
40
  else {
37
- total = (price + item_tax) * preventNaN(order_item.quantity) * vat;
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
- return _.sum((order_items ?? []).map(calcItem));
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
- let result = 0;
47
- (order_item?.addons ?? []).forEach((addon) => {
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 += preventNaN(addon.price) * preventNaN(addon.quantity);
58
+ result += addon.price * addon.quantity;
50
59
  }
51
60
  });
52
- if (order_item?.price && order_item?.quantity) {
53
- return (result + preventNaN(order_item.price)) * preventNaN(order_item.quantity);
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 ?? []).forEach((discount) => {
61
- if (discount?.amount) {
62
- let is_percentage = !!discount?.is_percentage;
63
- let is_spendAtleast = !!discount?.spends_at_least;
64
- let amount = 0;
65
- if (is_percentage)
66
- amount = (subtotal * preventNaN(discount?.amount)) / 100;
67
- else if (is_spendAtleast && subtotal >= (discount?.spends_at_least_amount ?? 0)) {
68
- amount = preventNaN(discount?.amount);
69
- }
70
- else if (is_spendAtleast && subtotal < (discount?.spends_at_least_amount ?? 0)) {
71
- null;
72
- }
73
- else {
74
- amount = preventNaN(discount?.amount);
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
- discounts += amount;
77
- }
78
- });
79
- (order_items ?? []).forEach((order_item) => {
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 = _.sum(addons_arr);
84
- discounts += (preventNaN(order_item?.price) + (addons_total || 0)) * preventNaN(order_item.quantity) * (preventNaN(discount?.amount) / 100);
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?.amount) {
87
- discounts += preventNaN(discount?.amount) * preventNaN(order_item.quantity);
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 + preventNaN(branch_vat) / 100;
94
- discounts += preventNaN(loyalty_amount) / vat;
104
+ const vat = 1 + branch_vat / 100;
105
+ discounts += loyalty_amount / vat;
95
106
  }
96
107
  else {
97
- discounts += preventNaN(loyalty_amount);
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 ?? []).forEach((discount) => {
106
- if (discount?.amount) {
107
- let is_percentage = !!discount?.is_percentage;
108
- let is_spendAtleast = !!discount?.spends_at_least;
109
- let amount = 0;
110
- if (is_percentage)
111
- amount = (subtotal * preventNaN(discount?.amount)) / 100;
112
- else if (is_spendAtleast && subtotal >= (discount?.spends_at_least_amount ?? 0)) {
113
- amount = preventNaN(discount?.amount);
114
- }
115
- else if (is_spendAtleast && subtotal < (discount?.spends_at_least_amount ?? 0)) {
116
- null;
117
- }
118
- else {
119
- amount = preventNaN(discount?.amount);
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
- discounts += amount;
122
- }
123
- });
124
- (order_items ?? []).forEach((order_item) => {
125
- (order_item?.discounts ?? []).forEach((discount) => {
126
- if (!order_item?.not_taxable) {
127
- if (discount?.is_percentage && discount?.amount) {
128
- const addons_arr = order_item?.addons?.map((a) => preventNaN(a.price) * preventNaN(a.quantity));
129
- const addons_total = _.sum(addons_arr);
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?.amount) {
134
- discounts += preventNaN(discount?.amount) * preventNaN(order_item?.quantity);
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 + preventNaN(branch_vat) / 100;
142
- discounts += preventNaN(loyalty_amount) / vat;
153
+ const vat = 1 + branch_vat / 100;
154
+ discounts += loyalty_amount / vat;
143
155
  }
144
156
  else {
145
- discounts += preventNaN(loyalty_amount);
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 += toFixedFloat(preventNaN(addon?.price) * preventNaN(addon?.quantity));
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 += toFixedFloat((preventNaN(order_item?.price) + addons) * preventNaN(order_item?.quantity) * (preventNaN(discount?.amount) / 100));
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 += toFixedFloat(preventNaN(discount?.amount) * preventNaN(order_item?.quantity));
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 : toFixedFloat(price * vat);
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 (branch_vat && order_items.some((o) => !o.not_taxable)) {
172
- const discount = DiscountsWithVat(order_discounts ?? [], order_items, loyalty_amount ?? 0, branch_vat);
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.not_taxable), order_discounts, order_taxes, loyalty_amount, branch_vat));
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 = _.sum(order_items.filter((i) => !i.not_taxable).map(calcItem));
194
+ let sub_total = _sum(order_items.filter((i) => !i?.not_taxable).map(calcItem));
179
195
  // VAT amount
180
- const vat = ((branch_vat || 0) / 100) * (sub_total + other_charges + deliveries - discount);
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 _.sum((order_items || []).filter((i) => !i.not_taxable).map(calcItem));
202
+ return _sum((order_items || []).filter((i) => !i.not_taxable).map(calcItem));
187
203
  }
188
204
  export function NonTaxableAmount(order_items) {
189
- return _.sum((order_items || []).filter((i) => i.not_taxable).map(calcItem));
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
- (order_deliveries || []).forEach((d) => {
210
+ order_deliveries.forEach((d) => {
195
211
  if (d?.delivery_type === 'RADIUS') {
196
- sum += (d?.delivery_fee ?? 0) * (d.delivery_radius || 1);
212
+ sum += (d.delivery_fee || 0) * (d.delivery_radius || 1);
197
213
  }
198
214
  else {
199
- sum += d?.delivery_fee ?? 0;
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 = order_taxes
209
- ? _.sum(order_taxes.map((tax) => {
210
- if (tax.amount) {
211
- let result = tax.isPercent ? (tax.amount / 100) * (sub - discounts) : tax.amount;
212
- if (tax.isPercent && tax?.minimum_amount) {
213
- const min = preventNaN(tax?.minimum_amount);
214
- const base = sub - discounts;
215
- if (base < min) {
216
- result = min;
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 result;
220
- }
221
- return 0;
222
- }))
223
- : 0;
224
- (order_items ?? []).forEach((order_item) => {
225
- const addons_total = _.sum(order_item?.addons?.map((a) => preventNaN(a.price ?? 0) * preventNaN(a.quantity ?? 0)));
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 ?? []).forEach((discount) => {
249
+ (order_item?.discounts || [])?.forEach((discount) => {
229
250
  if (discount?.is_percentage && discount?.amount) {
230
- _discounts += price * (preventNaN(discount?.amount) / 100);
251
+ _discounts += price * (discount.amount / 100);
231
252
  }
232
- else if (discount?.amount) {
233
- _discounts += preventNaN(discount?.amount) * preventNaN(order_item?.quantity);
253
+ else if (discount.amount) {
254
+ _discounts += discount.amount * order_item.quantity;
234
255
  }
235
256
  });
236
- const order_items_count = _.sum((order_items ?? []).map((i) => preventNaN(i?.quantity)));
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 * (preventNaN(discount?.amount) / 100);
260
+ _discounts += price * (discount.amount / 100);
240
261
  }
241
262
  else if (discount?.amount) {
242
- _discounts += (preventNaN(discount?.amount) / order_items_count) * preventNaN(order_item?.quantity);
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 * (preventNaN(tax?.amount) / 100);
272
+ let taxAmount = temp * (tax.amount / 100);
252
273
  if (tax?.minimum_amount) {
253
- const min = preventNaN(tax?.minimum_amount) * preventNaN(order_item?.quantity);
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 += preventNaN(tax?.amount) * preventNaN(order_item?.quantity ?? 0);
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 ?? []).forEach((tax) => {
272
- if (tax?.amount) {
273
- let total = tax?.isPercent ? (preventNaN(tax?.amount) / 100) * (sub - discounts) : preventNaN(tax?.amount);
274
- if (tax.isPercent && tax?.minimum_amount) {
275
- total = Math.max(total, preventNaN(tax?.minimum_amount));
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
- taxes.push({
278
- tax_id: tax?.tax_id ?? '',
279
- name_lan_p: tax?.name_lan_p ?? '',
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
- (order_item?.taxes ?? []).forEach((tax) => {
318
+ order_item?.taxes?.forEach((tax) => {
291
319
  let _discounts = calcItemDiscounts(order_item, order_discounts, order_items_count);
292
- if (tax?.tax_id) {
320
+ if (tax.tax_id) {
293
321
  let charge = 0;
294
- if (tax?.isPercent && tax?.amount) {
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 / preventNaN(order_item?.quantity), preventNaN(tax?.minimum_amount)) * preventNaN(order_item?.quantity);
325
+ charge = Math.max(charge / order_item.quantity, preventNaN(tax?.minimum_amount)) * order_item.quantity;
299
326
  }
300
327
  }
301
- else if (tax?.amount) {
302
- charge = preventNaN(tax?.amount) * preventNaN(order_item?.quantity);
328
+ else if (tax.amount) {
329
+ charge = (tax.amount || 0) * order_item.quantity;
303
330
  }
304
- if (!list.some((t) => t.tax_id === tax?.tax_id)) {
331
+ if (!list.some((t) => t.tax_id === tax.tax_id)) {
305
332
  list.push({
306
- tax_id: tax?.tax_id,
307
- name_lan_p: tax?.name_lan_p ?? '',
308
- name_lan_s: tax?.name_lan_s,
309
- isPercent: tax?.isPercent,
310
- amount: tax?.amount ?? 0,
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: preventNaN(t.total) + charge }));
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 _.concat(taxes, item_taxes);
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 = toFixedFloat(sub - discounts + other_charges + vat + deliveries);
330
- return total > 0 ? total : 0;
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
- let due = preventNaN(invoice?.total ?? 0) - preventNaN(invoice?.received_amount ?? 0);
334
- (branch?.payment_methods ?? []).forEach((payment) => {
335
- due -= preventNaN(payment?.payment_amount ?? 0);
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
- let groupByPaymnets = _.map(info?.payments ?? [], (p) => ({
341
- name_lan_p: p.name_lan_p ?? '',
342
- name_lan_s: p.name_lan_s ?? '',
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
- if (!result[acc.name_lan_p])
348
- result[acc.name_lan_p] = [];
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?.closing_cash_sales ?? 0) +
362
- preventNaN(opening_start_cash ?? 0) +
363
- (preventNaN(closing?.closing_cash_add ?? 0) - preventNaN(closing?.closing_cash_remove ?? 0)));
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
- (item?.addons ?? []).forEach((addon) => {
369
- if (addon?.price && addon?.quantity) {
370
- addons += preventNaN(addon?.price) * preventNaN(addon?.quantity);
407
+ item?.addons?.forEach((addon) => {
408
+ if (addon.price && addon.quantity) {
409
+ addons += addon.price * addon.quantity;
371
410
  }
372
411
  });
373
- (item?.discounts ?? []).forEach((discount) => {
374
- if (discount?.is_percentage && discount?.amount) {
375
- discounts += (preventNaN(item?.price) + addons) * (preventNaN(discount?.amount) / 100);
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?.amount) {
378
- discounts += preventNaN(discount?.amount);
416
+ else if (discount.amount) {
417
+ discounts += discount.amount;
379
418
  }
380
419
  });
381
- (order_discounts ?? []).forEach((discount) => {
382
- if (discount?.is_percentage && discount?.amount) {
383
- discounts += (preventNaN(item?.price) + addons) * (preventNaN(discount?.amount) / 100);
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?.amount) {
386
- discounts += preventNaN(discount?.amount) / order_items_count;
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
- (item?.addons ?? []).forEach((addon) => {
394
- if (addon?.price && addon?.quantity) {
395
- addons += preventNaN(addon?.price) * preventNaN(addon?.quantity);
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
- const now = moment(new Date());
402
- const openningTime = moment(convertToDate(openning?.cashier_opening_date));
403
- const orders = (invoices ?? []).filter((invoice) => {
404
- const invoiceDate = moment(convertToDate(invoice?.order_date));
405
- return !invoiceDate.isBetween(openningTime, now);
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
- return orders;
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 parseFloat(toFixed(price * (1 + branch_vat / 100)));
467
+ return price * (1 + branch_vat / 100);
412
468
  }
413
469
  return price ?? 0;
414
470
  }