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