lazywait-calcs 1.0.4 → 1.0.5

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