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