lazywait-calcs 1.8.5 → 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/esm/Calcs.js CHANGED
@@ -1,19 +1,21 @@
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) {
5
7
  let total = 0;
6
8
  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) => {
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 += preventNaN(addon.price) * preventNaN(addon.quantity);
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 = _.sum(order_item.taxes?.map((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 += _.sum(order_taxes?.map((_tax) => {
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 * preventNaN(order_item.quantity);
36
+ total = price * order_item.quantity;
35
37
  }
36
38
  else {
37
- total = (price + item_tax) * preventNaN(order_item.quantity) * vat;
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
- return _.sum((order_items ?? []).map(calcItem));
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 ?? []).forEach((addon) => {
51
+ let result = order_item.price || 0;
52
+ (order_item?.addons || []).forEach((addon) => {
48
53
  if (addon?.price && addon?.quantity) {
49
- result += preventNaN(addon.price) * preventNaN(addon.quantity);
54
+ result += addon.price * addon.quantity;
50
55
  }
51
56
  });
52
- if (order_item?.price && order_item?.quantity) {
53
- return (result + preventNaN(order_item.price)) * preventNaN(order_item.quantity);
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 ?? []).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);
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
- discounts += amount;
77
- }
78
- });
79
- (order_items ?? []).forEach((order_item) => {
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 = _.sum(addons_arr);
84
- discounts += (preventNaN(order_item?.price) + (addons_total || 0)) * preventNaN(order_item.quantity) * (preventNaN(discount?.amount) / 100);
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?.amount) {
87
- discounts += preventNaN(discount?.amount) * preventNaN(order_item.quantity);
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 + preventNaN(branch_vat) / 100;
94
- discounts += preventNaN(loyalty_amount) / vat;
100
+ const vat = 1 + branch_vat / 100;
101
+ discounts += loyalty_amount / vat;
95
102
  }
96
103
  else {
97
- discounts += preventNaN(loyalty_amount);
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 ?? []).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);
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
- 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);
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?.amount) {
134
- discounts += preventNaN(discount?.amount) * preventNaN(order_item?.quantity);
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 + preventNaN(branch_vat) / 100;
142
- discounts += preventNaN(loyalty_amount) / vat;
149
+ const vat = 1 + branch_vat / 100;
150
+ discounts += loyalty_amount / vat;
143
151
  }
144
152
  else {
145
- discounts += preventNaN(loyalty_amount);
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 += toFixedFloat(preventNaN(addon?.price) * preventNaN(addon?.quantity));
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 += toFixedFloat((preventNaN(order_item?.price) + addons) * preventNaN(order_item?.quantity) * (preventNaN(discount?.amount) / 100));
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 += toFixedFloat(preventNaN(discount?.amount) * preventNaN(order_item?.quantity));
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 : toFixedFloat(price * vat);
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 ?? [], order_items, loyalty_amount ?? 0, branch_vat);
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 = _.sum(order_items.filter((i) => !i.not_taxable).map(calcItem));
186
+ let sub_total = _sum(order_items.filter((i) => !i.not_taxable).map(calcItem));
179
187
  // VAT amount
180
- const vat = ((branch_vat || 0) / 100) * (sub_total + other_charges + deliveries - discount);
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 _.sum((order_items || []).filter((i) => !i.not_taxable).map(calcItem));
194
+ return _sum((order_items || []).filter((i) => !i.not_taxable).map(calcItem));
187
195
  }
188
196
  export function NonTaxableAmount(order_items) {
189
- return _.sum((order_items || []).filter((i) => i.not_taxable).map(calcItem));
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
- (order_deliveries || []).forEach((d) => {
195
- if (d?.delivery_type === 'RADIUS') {
196
- sum += (d?.delivery_fee ?? 0) * (d.delivery_radius || 1);
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?.delivery_fee ?? 0;
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 = 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;
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 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);
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 ?? []).forEach((discount) => {
239
+ (order_item?.discounts || [])?.forEach((discount) => {
229
240
  if (discount?.is_percentage && discount?.amount) {
230
- _discounts += price * (preventNaN(discount?.amount) / 100);
241
+ _discounts += price * (discount.amount / 100);
231
242
  }
232
- else if (discount?.amount) {
233
- _discounts += preventNaN(discount?.amount) * preventNaN(order_item?.quantity);
243
+ else if (discount.amount) {
244
+ _discounts += discount.amount * order_item.quantity;
234
245
  }
235
246
  });
236
- const order_items_count = _.sum((order_items ?? []).map((i) => preventNaN(i?.quantity)));
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 * (preventNaN(discount?.amount) / 100);
250
+ _discounts += price * (discount.amount / 100);
240
251
  }
241
252
  else if (discount?.amount) {
242
- _discounts += (preventNaN(discount?.amount) / order_items_count) * preventNaN(order_item?.quantity);
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 * (preventNaN(tax?.amount) / 100);
262
+ let taxAmount = temp * (tax.amount / 100);
252
263
  if (tax?.minimum_amount) {
253
- const min = preventNaN(tax?.minimum_amount) * preventNaN(order_item?.quantity);
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 += preventNaN(tax?.amount) * preventNaN(order_item?.quantity ?? 0);
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 ?? []).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));
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
- 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)));
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
- (order_item?.taxes ?? []).forEach((tax) => {
304
+ order_item?.taxes?.forEach((tax) => {
291
305
  let _discounts = calcItemDiscounts(order_item, order_discounts, order_items_count);
292
- if (tax?.tax_id) {
306
+ if (tax.tax_id) {
293
307
  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);
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 / preventNaN(order_item?.quantity), preventNaN(tax?.minimum_amount)) * preventNaN(order_item?.quantity);
311
+ charge = Math.max(charge / order_item.quantity, preventNaN(tax?.minimum_amount)) * order_item.quantity;
299
312
  }
300
313
  }
301
- else if (tax?.amount) {
302
- charge = preventNaN(tax?.amount) * preventNaN(order_item?.quantity);
314
+ else if (tax.amount) {
315
+ charge = (tax.amount || 0) * order_item.quantity;
303
316
  }
304
- if (!list.some((t) => t.tax_id === tax?.tax_id)) {
317
+ if (!list.some((t) => t.tax_id === tax.tax_id)) {
305
318
  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,
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: preventNaN(t.total) + charge }));
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 _.concat(taxes, item_taxes);
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 = toFixedFloat(sub - discounts + other_charges + vat + deliveries);
330
- return total > 0 ? total : 0;
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 = preventNaN(invoice?.total ?? 0) - preventNaN(invoice?.received_amount ?? 0);
334
- (branch?.payment_methods ?? []).forEach((payment) => {
335
- due -= preventNaN(payment?.payment_amount ?? 0);
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 = _.map(info?.payments ?? [], (p) => ({
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
- if (!result[acc.name_lan_p])
348
- result[acc.name_lan_p] = [];
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?.closing_cash_sales ?? 0) +
362
- preventNaN(opening_start_cash ?? 0) +
363
- (preventNaN(closing?.closing_cash_add ?? 0) - preventNaN(closing?.closing_cash_remove ?? 0)));
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
- (item?.addons ?? []).forEach((addon) => {
369
- if (addon?.price && addon?.quantity) {
370
- addons += preventNaN(addon?.price) * preventNaN(addon?.quantity);
385
+ item?.addons?.forEach((addon) => {
386
+ if (addon.price && addon.quantity) {
387
+ addons += addon.price * addon.quantity;
371
388
  }
372
389
  });
373
- (item?.discounts ?? []).forEach((discount) => {
374
- if (discount?.is_percentage && discount?.amount) {
375
- discounts += (preventNaN(item?.price) + addons) * (preventNaN(discount?.amount) / 100);
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?.amount) {
378
- discounts += preventNaN(discount?.amount);
394
+ else if (discount.amount) {
395
+ discounts += discount.amount;
379
396
  }
380
397
  });
381
- (order_discounts ?? []).forEach((discount) => {
382
- if (discount?.is_percentage && discount?.amount) {
383
- discounts += (preventNaN(item?.price) + addons) * (preventNaN(discount?.amount) / 100);
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?.amount) {
386
- discounts += preventNaN(discount?.amount) / order_items_count;
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
- (item?.addons ?? []).forEach((addon) => {
394
- if (addon?.price && addon?.quantity) {
395
- addons += preventNaN(addon?.price) * preventNaN(addon?.quantity);
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 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);
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
- return orders;
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 parseFloat(toFixed(price * (1 + branch_vat / 100)));
435
+ return price * (1 + branch_vat / 100);
412
436
  }
413
437
  return price ?? 0;
414
438
  }
@@ -1,4 +1,4 @@
1
- import { Branch, CashierClosing, Order, OrderDiscount, OrderItem, OrderTax, PrintedTax, CashierOpening, OrderDelivery } from './types';
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