lazywait-calcs 1.8.6 → 1.8.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/cjs/Calcs.js CHANGED
@@ -3,24 +3,28 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.WithoutVat = exports.WithVat = exports.getInvoicesDiffrence = exports.calcItemAddons = exports.calcItemDiscounts = exports.calcClosingCashBalance = exports.calPaymentTotal = exports.calcPaymentBalanceDue = exports.Total = exports.printedOrderTaxes = exports.OtherCharges = exports.Deliveries = exports.NonTaxableAmount = exports.TaxableAmount = exports.Vat = exports.ItemVat = exports.DiscountsWithVat = exports.Discounts = exports.calcItem = exports.Subtotal = exports.ItemTotal = void 0;
7
- const lodash_1 = __importDefault(require("lodash"));
6
+ exports.WithoutVat = exports.WithVat = exports.calcRefundedLoyaltyAmount = exports.getActiveInvoices = exports.getInvoicesDiffrence = exports.calcItemAddons = exports.calcItemDiscounts = exports.calcClosingCashBalance = exports.calPaymentTotal = exports.calcPaymentBalanceDue = exports.Total = exports.printedOrderTaxes = exports.OtherCharges = exports.Deliveries = exports.NonTaxableAmount = exports.TaxableAmount = exports.Vat = exports.ItemVat = exports.DiscountsWithVat = exports.Discounts = exports.calcItem = exports.Subtotal = exports.ItemTotal = void 0;
7
+ const concat_1 = __importDefault(require("lodash/concat"));
8
+ const map_1 = __importDefault(require("lodash/map"));
9
+ const sum_1 = __importDefault(require("lodash/sum"));
8
10
  const moment_1 = __importDefault(require("moment"));
9
11
  const utils_1 = require("./utils");
10
12
  function ItemTotal(order_item, branch_vat, order_taxes, order_discounts, order_items_count = 1) {
11
13
  var _a, _b;
14
+ if (!order_item)
15
+ return 0;
12
16
  let total = 0;
13
17
  const vat = branch_vat ? 1 + branch_vat / 100 : 1;
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) => {
18
+ if ((order_item.price || order_item.price === 0) && order_item.quantity) {
19
+ let price = order_item.price;
20
+ (_a = order_item.addons) === null || _a === void 0 ? void 0 : _a.forEach((addon) => {
17
21
  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);
22
+ price += addon.price * addon.quantity;
19
23
  }
20
24
  });
21
25
  const discounts = calcItemDiscounts(order_item, order_discounts, order_items_count);
22
26
  price -= discounts;
23
- let item_tax = lodash_1.default.sum(((_b = order_item.taxes) === null || _b === void 0 ? void 0 : _b.map((tax) => {
27
+ let item_tax = (0, sum_1.default)(((_b = order_item.taxes) === null || _b === void 0 ? void 0 : _b.map((tax) => {
24
28
  let taxAmount = tax.isPercent ? ((tax.amount || 0) / 100) * price : tax.amount || 0;
25
29
  if (tax.isPercent && (tax === null || tax === void 0 ? void 0 : tax.minimum_amount)) {
26
30
  taxAmount = price < (tax === null || tax === void 0 ? void 0 : tax.minimum_amount) ? tax === null || tax === void 0 ? void 0 : tax.minimum_amount : taxAmount;
@@ -28,7 +32,7 @@ function ItemTotal(order_item, branch_vat, order_taxes, order_discounts, order_i
28
32
  return taxAmount;
29
33
  })) || []);
30
34
  if (order_taxes) {
31
- price += lodash_1.default.sum((order_taxes === null || order_taxes === void 0 ? void 0 : order_taxes.map((_tax) => {
35
+ price += (0, sum_1.default)((order_taxes === null || order_taxes === void 0 ? void 0 : order_taxes.map((_tax) => {
32
36
  let taxAmount = _tax.isPercent ? ((_tax.amount || 0) / 100) * price : (_tax.amount || 0) / order_items_count;
33
37
  if (_tax.isPercent && (_tax === null || _tax === void 0 ? void 0 : _tax.minimum_amount)) {
34
38
  const min = (0, utils_1.preventNaN)(_tax === null || _tax === void 0 ? void 0 : _tax.minimum_amount) / order_items_count;
@@ -38,29 +42,33 @@ function ItemTotal(order_item, branch_vat, order_taxes, order_discounts, order_i
38
42
  })) || []);
39
43
  }
40
44
  if (order_item.not_taxable) {
41
- total = price * (0, utils_1.preventNaN)(order_item.quantity);
45
+ total = price * order_item.quantity;
42
46
  }
43
47
  else {
44
- total = (price + item_tax) * (0, utils_1.preventNaN)(order_item.quantity) * vat;
48
+ total = (price + item_tax) * order_item.quantity * vat;
45
49
  }
46
50
  }
47
51
  return total;
48
52
  }
49
53
  exports.ItemTotal = ItemTotal;
50
54
  function Subtotal(order_items) {
51
- return lodash_1.default.sum((order_items !== null && order_items !== void 0 ? order_items : []).map(calcItem));
55
+ if (order_items && order_items.length) {
56
+ return (0, sum_1.default)(order_items.map(calcItem));
57
+ }
58
+ return 0;
52
59
  }
53
60
  exports.Subtotal = Subtotal;
54
61
  function calcItem(order_item) {
55
- var _a;
56
- let result = 0;
57
- ((_a = order_item === null || order_item === void 0 ? void 0 : order_item.addons) !== null && _a !== void 0 ? _a : []).forEach((addon) => {
62
+ if (!order_item)
63
+ return 0;
64
+ let result = order_item.price || 0;
65
+ ((order_item === null || order_item === void 0 ? void 0 : order_item.addons) || []).forEach((addon) => {
58
66
  if ((addon === null || addon === void 0 ? void 0 : addon.price) && (addon === null || addon === void 0 ? void 0 : addon.quantity)) {
59
- result += (0, utils_1.preventNaN)(addon.price) * (0, utils_1.preventNaN)(addon.quantity);
67
+ result += addon.price * addon.quantity;
60
68
  }
61
69
  });
62
- 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)) {
63
- return (result + (0, utils_1.preventNaN)(order_item.price)) * (0, utils_1.preventNaN)(order_item.quantity);
70
+ if (order_item.quantity) {
71
+ return result * order_item.quantity;
64
72
  }
65
73
  return result;
66
74
  }
@@ -68,47 +76,47 @@ exports.calcItem = calcItem;
68
76
  function Discounts(order_discounts, order_items, loyalty_amount, branch_vat) {
69
77
  let discounts = 0;
70
78
  const subtotal = Subtotal(order_items);
71
- (order_discounts !== null && order_discounts !== void 0 ? order_discounts : []).forEach((discount) => {
72
- var _a, _b;
73
- if (discount === null || discount === void 0 ? void 0 : discount.amount) {
74
- let is_percentage = !!(discount === null || discount === void 0 ? void 0 : discount.is_percentage);
75
- let is_spendAtleast = !!(discount === null || discount === void 0 ? void 0 : discount.spends_at_least);
76
- let amount = 0;
77
- if (is_percentage)
78
- amount = (subtotal * (0, utils_1.preventNaN)(discount === null || discount === void 0 ? void 0 : discount.amount)) / 100;
79
- else if (is_spendAtleast && subtotal >= ((_a = discount === null || discount === void 0 ? void 0 : discount.spends_at_least_amount) !== null && _a !== void 0 ? _a : 0)) {
80
- amount = (0, utils_1.preventNaN)(discount === null || discount === void 0 ? void 0 : discount.amount);
81
- }
82
- else if (is_spendAtleast && subtotal < ((_b = discount === null || discount === void 0 ? void 0 : discount.spends_at_least_amount) !== null && _b !== void 0 ? _b : 0)) {
83
- null;
84
- }
85
- else {
86
- amount = (0, utils_1.preventNaN)(discount === null || discount === void 0 ? void 0 : discount.amount);
79
+ if (order_discounts) {
80
+ order_discounts.forEach((discount) => {
81
+ if (discount === null || discount === void 0 ? void 0 : discount.amount) {
82
+ let is_percentage = discount.is_percentage || false;
83
+ let is_spendAtleast = discount.spends_at_least || false;
84
+ let amount = 0;
85
+ if (is_percentage)
86
+ amount = (subtotal * discount.amount) / 100;
87
+ else if (is_spendAtleast && subtotal >= discount.spends_at_least_amount) {
88
+ amount = discount.amount;
89
+ }
90
+ else if (is_spendAtleast && subtotal < discount.spends_at_least_amount) {
91
+ null;
92
+ }
93
+ else {
94
+ amount = discount.amount;
95
+ }
96
+ discounts += amount;
87
97
  }
88
- discounts += amount;
89
- }
90
- });
91
- (order_items !== null && order_items !== void 0 ? order_items : []).forEach((order_item) => {
92
- var _a;
93
- ((_a = order_item === null || order_item === void 0 ? void 0 : order_item.discounts) !== null && _a !== void 0 ? _a : []).forEach((discount) => {
98
+ });
99
+ }
100
+ (order_items || []).forEach((order_item) => {
101
+ ((order_item === null || order_item === void 0 ? void 0 : order_item.discounts) || []).forEach((discount) => {
94
102
  var _a;
95
103
  if ((discount === null || discount === void 0 ? void 0 : discount.is_percentage) && (discount === null || discount === void 0 ? void 0 : discount.amount)) {
96
104
  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; });
97
- const addons_total = lodash_1.default.sum(addons_arr);
98
- 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);
105
+ const addons_total = (0, sum_1.default)(addons_arr);
106
+ discounts += (order_item.price + (addons_total || 0)) * order_item.quantity * (discount.amount / 100);
99
107
  }
100
- else if (discount === null || discount === void 0 ? void 0 : discount.amount) {
101
- discounts += (0, utils_1.preventNaN)(discount === null || discount === void 0 ? void 0 : discount.amount) * (0, utils_1.preventNaN)(order_item.quantity);
108
+ else if (discount.amount) {
109
+ discounts += discount.amount * order_item.quantity;
102
110
  }
103
111
  });
104
112
  });
105
113
  if (loyalty_amount) {
106
114
  if (branch_vat) {
107
- const vat = 1 + (0, utils_1.preventNaN)(branch_vat) / 100;
108
- discounts += (0, utils_1.preventNaN)(loyalty_amount) / vat;
115
+ const vat = 1 + branch_vat / 100;
116
+ discounts += loyalty_amount / vat;
109
117
  }
110
118
  else {
111
- discounts += (0, utils_1.preventNaN)(loyalty_amount);
119
+ discounts += loyalty_amount;
112
120
  }
113
121
  }
114
122
  return discounts;
@@ -117,50 +125,49 @@ exports.Discounts = Discounts;
117
125
  function DiscountsWithVat(order_discounts, order_items, loyalty_amount, branch_vat) {
118
126
  let discounts = 0;
119
127
  const subtotal = Subtotal(order_items);
120
- (order_discounts !== null && order_discounts !== void 0 ? order_discounts : []).forEach((discount) => {
121
- var _a, _b;
122
- if (discount === null || discount === void 0 ? void 0 : discount.amount) {
123
- let is_percentage = !!(discount === null || discount === void 0 ? void 0 : discount.is_percentage);
124
- let is_spendAtleast = !!(discount === null || discount === void 0 ? void 0 : discount.spends_at_least);
125
- let amount = 0;
126
- if (is_percentage)
127
- amount = (subtotal * (0, utils_1.preventNaN)(discount === null || discount === void 0 ? void 0 : discount.amount)) / 100;
128
- else if (is_spendAtleast && subtotal >= ((_a = discount === null || discount === void 0 ? void 0 : discount.spends_at_least_amount) !== null && _a !== void 0 ? _a : 0)) {
129
- amount = (0, utils_1.preventNaN)(discount === null || discount === void 0 ? void 0 : discount.amount);
130
- }
131
- else if (is_spendAtleast && subtotal < ((_b = discount === null || discount === void 0 ? void 0 : discount.spends_at_least_amount) !== null && _b !== void 0 ? _b : 0)) {
132
- null;
133
- }
134
- else {
135
- amount = (0, utils_1.preventNaN)(discount === null || discount === void 0 ? void 0 : discount.amount);
128
+ if (order_discounts) {
129
+ order_discounts.forEach((discount) => {
130
+ if (discount === null || discount === void 0 ? void 0 : discount.amount) {
131
+ let is_percentage = discount.is_percentage || false;
132
+ let is_spendAtleast = discount.spends_at_least || false;
133
+ let amount = 0;
134
+ if (is_percentage)
135
+ amount = (subtotal * discount.amount) / 100;
136
+ else if (is_spendAtleast && subtotal >= discount.spends_at_least_amount) {
137
+ amount = discount.amount;
138
+ }
139
+ else if (is_spendAtleast && subtotal < discount.spends_at_least_amount) {
140
+ null;
141
+ }
142
+ else {
143
+ amount = discount.amount;
144
+ }
145
+ discounts += amount;
136
146
  }
137
- discounts += amount;
138
- }
139
- });
140
- (order_items !== null && order_items !== void 0 ? order_items : []).forEach((order_item) => {
141
- var _a;
142
- ((_a = order_item === null || order_item === void 0 ? void 0 : order_item.discounts) !== null && _a !== void 0 ? _a : []).forEach((discount) => {
147
+ });
148
+ }
149
+ (order_items || []).forEach((order_item) => {
150
+ ((order_item === null || order_item === void 0 ? void 0 : order_item.discounts) || []).forEach((discount) => {
143
151
  var _a;
144
- if (!(order_item === null || order_item === void 0 ? void 0 : order_item.not_taxable)) {
145
- if ((discount === null || discount === void 0 ? void 0 : discount.is_percentage) && (discount === null || discount === void 0 ? void 0 : discount.amount)) {
146
- 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));
147
- const addons_total = lodash_1.default.sum(addons_arr);
148
- discounts +=
149
- ((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);
152
+ if (!order_item.not_taxable) {
153
+ if (discount.is_percentage && discount.amount) {
154
+ 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));
155
+ const addons_total = (0, sum_1.default)(addons_arr);
156
+ discounts += (order_item.price + (addons_total || 0)) * order_item.quantity * (discount.amount / 100);
150
157
  }
151
- else if (discount === null || discount === void 0 ? void 0 : discount.amount) {
152
- 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);
158
+ else if (discount.amount) {
159
+ discounts += discount.amount * order_item.quantity;
153
160
  }
154
161
  }
155
162
  });
156
163
  });
157
164
  if (loyalty_amount) {
158
165
  if (branch_vat) {
159
- const vat = 1 + (0, utils_1.preventNaN)(branch_vat) / 100;
160
- discounts += (0, utils_1.preventNaN)(loyalty_amount) / vat;
166
+ const vat = 1 + branch_vat / 100;
167
+ discounts += loyalty_amount / vat;
161
168
  }
162
169
  else {
163
- discounts += (0, utils_1.preventNaN)(loyalty_amount);
170
+ discounts += loyalty_amount;
164
171
  }
165
172
  }
166
173
  return discounts;
@@ -168,60 +175,63 @@ function DiscountsWithVat(order_discounts, order_items, loyalty_amount, branch_v
168
175
  exports.DiscountsWithVat = DiscountsWithVat;
169
176
  function ItemVat(order_item, branch_vat) {
170
177
  var _a, _b;
178
+ if (!order_item)
179
+ return 0;
171
180
  const vat = branch_vat ? branch_vat / 100 : 1;
172
181
  let addons = 0;
173
182
  ((_a = order_item === null || order_item === void 0 ? void 0 : order_item.addons) !== null && _a !== void 0 ? _a : []).forEach((addon) => {
174
183
  if ((addon === null || addon === void 0 ? void 0 : addon.price) && (addon === null || addon === void 0 ? void 0 : addon.quantity)) {
175
- 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));
184
+ 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);
176
185
  }
177
186
  });
178
187
  let discounts = 0;
179
188
  ((_b = order_item === null || order_item === void 0 ? void 0 : order_item.discounts) !== null && _b !== void 0 ? _b : []).forEach((discount) => {
180
189
  if ((discount === null || discount === void 0 ? void 0 : discount.is_percentage) && (discount === null || discount === void 0 ? void 0 : discount.amount)) {
181
- 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));
190
+ discounts += ((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);
182
191
  }
183
192
  else if (discount === null || discount === void 0 ? void 0 : discount.amount) {
184
- 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));
193
+ 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);
185
194
  }
186
195
  });
187
196
  const price = (0, utils_1.preventNaN)(order_item === null || order_item === void 0 ? void 0 : order_item.price) + addons - discounts;
188
- return (order_item === null || order_item === void 0 ? void 0 : order_item.not_taxable) ? 0 : (0, utils_1.toFixedFloat)(price * vat);
197
+ return (order_item === null || order_item === void 0 ? void 0 : order_item.not_taxable) ? 0 : price * vat;
189
198
  }
190
199
  exports.ItemVat = ItemVat;
191
200
  function Vat(order_items, order_discounts, order_taxes, order_deliveries, loyalty_amount, branch_vat) {
192
- if (branch_vat && order_items.some((o) => !o.not_taxable)) {
193
- 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);
201
+ if (!order_items || !Array.isArray(order_items))
202
+ return 0;
203
+ if (branch_vat && order_items.some((o) => !(o === null || o === void 0 ? void 0 : o.not_taxable))) {
204
+ const discount = DiscountsWithVat(order_discounts, order_items, loyalty_amount, branch_vat);
194
205
  // Other charges for taxable items
195
- const other_charges = Number(OtherCharges(order_items.filter((i) => !i.not_taxable), order_discounts, order_taxes, loyalty_amount, branch_vat));
206
+ const other_charges = Number(OtherCharges(order_items.filter((i) => !(i === null || i === void 0 ? void 0 : i.not_taxable)), order_discounts, order_taxes, loyalty_amount, branch_vat));
196
207
  // Order delivery
197
208
  const deliveries = Deliveries(order_deliveries || []);
198
209
  // subtotal of items with vat
199
- let sub_total = lodash_1.default.sum(order_items.filter((i) => !i.not_taxable).map(calcItem));
210
+ let sub_total = (0, sum_1.default)(order_items.filter((i) => !(i === null || i === void 0 ? void 0 : i.not_taxable)).map(calcItem));
200
211
  // VAT amount
201
- const vat = ((branch_vat || 0) / 100) * (sub_total + other_charges + deliveries - discount);
212
+ const vat = (branch_vat / 100) * (sub_total + other_charges + deliveries - discount);
202
213
  return vat >= 0 ? vat : 0;
203
214
  }
204
215
  return 0;
205
216
  }
206
217
  exports.Vat = Vat;
207
218
  function TaxableAmount(order_items) {
208
- return lodash_1.default.sum((order_items || []).filter((i) => !i.not_taxable).map(calcItem));
219
+ return (0, sum_1.default)((order_items || []).filter((i) => !i.not_taxable).map(calcItem));
209
220
  }
210
221
  exports.TaxableAmount = TaxableAmount;
211
222
  function NonTaxableAmount(order_items) {
212
- return lodash_1.default.sum((order_items || []).filter((i) => i.not_taxable).map(calcItem));
223
+ return (0, sum_1.default)((order_items || []).filter((i) => i.not_taxable).map(calcItem));
213
224
  }
214
225
  exports.NonTaxableAmount = NonTaxableAmount;
215
226
  function Deliveries(order_deliveries) {
216
227
  let sum = 0;
217
228
  if (order_deliveries) {
218
- (order_deliveries || []).forEach((d) => {
219
- var _a, _b;
229
+ order_deliveries.forEach((d) => {
220
230
  if ((d === null || d === void 0 ? void 0 : d.delivery_type) === 'RADIUS') {
221
- sum += ((_a = d === null || d === void 0 ? void 0 : d.delivery_fee) !== null && _a !== void 0 ? _a : 0) * (d.delivery_radius || 1);
231
+ sum += (d.delivery_fee || 0) * (d.delivery_radius || 1);
222
232
  }
223
233
  else {
224
- sum += (_b = d === null || d === void 0 ? void 0 : d.delivery_fee) !== null && _b !== void 0 ? _b : 0;
234
+ sum += d.delivery_fee || 0;
225
235
  }
226
236
  });
227
237
  }
@@ -229,56 +239,60 @@ function Deliveries(order_deliveries) {
229
239
  }
230
240
  exports.Deliveries = Deliveries;
231
241
  function OtherCharges(order_items, order_discounts, order_taxes, loyalty_amount, branch_vat) {
242
+ if (!order_items || !Array.isArray(order_items))
243
+ return 0;
232
244
  const sub = Subtotal(order_items);
233
245
  const discounts = Number(Discounts(order_discounts, order_items, loyalty_amount, branch_vat));
234
- let other_charges = order_taxes
235
- ? lodash_1.default.sum(order_taxes.map((tax) => {
236
- if (tax.amount) {
237
- let result = tax.isPercent ? (tax.amount / 100) * (sub - discounts) : tax.amount;
238
- if (tax.isPercent && (tax === null || tax === void 0 ? void 0 : tax.minimum_amount)) {
239
- const min = (0, utils_1.preventNaN)(tax === null || tax === void 0 ? void 0 : tax.minimum_amount);
240
- const base = sub - discounts;
241
- if (base < min) {
242
- result = min;
246
+ let other_charges = 0;
247
+ if (order_taxes) {
248
+ other_charges = order_taxes
249
+ ? (0, sum_1.default)(order_taxes.map((tax) => {
250
+ if (tax.amount) {
251
+ let result = tax.isPercent ? (tax.amount / 100) * (sub - discounts) : tax.amount;
252
+ if (tax.isPercent && (tax === null || tax === void 0 ? void 0 : tax.minimum_amount)) {
253
+ const min = (0, utils_1.preventNaN)(tax === null || tax === void 0 ? void 0 : tax.minimum_amount);
254
+ const base = sub - discounts;
255
+ if (base < min) {
256
+ result = min;
257
+ }
243
258
  }
259
+ return result;
244
260
  }
245
- return result;
246
- }
247
- return 0;
248
- }))
249
- : 0;
250
- (order_items !== null && order_items !== void 0 ? order_items : []).forEach((order_item) => {
261
+ return 0;
262
+ }))
263
+ : 0;
264
+ }
265
+ (order_items || []).forEach((order_item) => {
251
266
  var _a, _b, _c;
252
- 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); }));
253
- 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);
267
+ const addons_total = (0, sum_1.default)((_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)));
268
+ const price = (order_item.price + addons_total) * order_item.quantity;
254
269
  let _discounts = 0;
255
- ((_b = order_item === null || order_item === void 0 ? void 0 : order_item.discounts) !== null && _b !== void 0 ? _b : []).forEach((discount) => {
270
+ (_b = ((order_item === null || order_item === void 0 ? void 0 : order_item.discounts) || [])) === null || _b === void 0 ? void 0 : _b.forEach((discount) => {
256
271
  if ((discount === null || discount === void 0 ? void 0 : discount.is_percentage) && (discount === null || discount === void 0 ? void 0 : discount.amount)) {
257
- _discounts += price * ((0, utils_1.preventNaN)(discount === null || discount === void 0 ? void 0 : discount.amount) / 100);
272
+ _discounts += price * (discount.amount / 100);
258
273
  }
259
- else if (discount === null || discount === void 0 ? void 0 : discount.amount) {
260
- _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);
274
+ else if (discount.amount) {
275
+ _discounts += discount.amount * order_item.quantity;
261
276
  }
262
277
  });
263
- 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)));
278
+ const order_items_count = (0, sum_1.default)(order_items.map((i) => i.quantity));
264
279
  (order_discounts !== null && order_discounts !== void 0 ? order_discounts : []).forEach((discount) => {
265
280
  if ((discount === null || discount === void 0 ? void 0 : discount.is_percentage) && (discount === null || discount === void 0 ? void 0 : discount.amount)) {
266
- _discounts += price * ((0, utils_1.preventNaN)(discount === null || discount === void 0 ? void 0 : discount.amount) / 100);
281
+ _discounts += price * (discount.amount / 100);
267
282
  }
268
283
  else if (discount === null || discount === void 0 ? void 0 : discount.amount) {
269
- _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);
284
+ _discounts += (discount.amount / order_items_count) * order_item.quantity;
270
285
  }
271
286
  });
272
287
  ((_c = order_item === null || order_item === void 0 ? void 0 : order_item.taxes) !== null && _c !== void 0 ? _c : []).forEach((tax) => {
273
- var _a;
274
288
  if ((tax === null || tax === void 0 ? void 0 : tax.isPercent) && (tax === null || tax === void 0 ? void 0 : tax.amount)) {
275
289
  let temp = price - _discounts;
276
290
  if (temp < 0) {
277
291
  temp = 0;
278
292
  }
279
- let taxAmount = temp * ((0, utils_1.preventNaN)(tax === null || tax === void 0 ? void 0 : tax.amount) / 100);
293
+ let taxAmount = temp * (tax.amount / 100);
280
294
  if (tax === null || tax === void 0 ? void 0 : tax.minimum_amount) {
281
- const min = (0, utils_1.preventNaN)(tax === null || tax === void 0 ? void 0 : tax.minimum_amount) * (0, utils_1.preventNaN)(order_item === null || order_item === void 0 ? void 0 : order_item.quantity);
295
+ const min = (0, utils_1.preventNaN)(Number(tax === null || tax === void 0 ? void 0 : tax.minimum_amount)) * order_item.quantity;
282
296
  if (temp < min) {
283
297
  taxAmount = min;
284
298
  }
@@ -286,108 +300,116 @@ function OtherCharges(order_items, order_discounts, order_taxes, loyalty_amount,
286
300
  other_charges += taxAmount;
287
301
  }
288
302
  else if (tax === null || tax === void 0 ? void 0 : tax.amount) {
289
- 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);
303
+ other_charges += tax.amount * ((order_item === null || order_item === void 0 ? void 0 : order_item.quantity) || 0);
290
304
  }
291
305
  });
292
306
  });
293
307
  return other_charges;
294
308
  }
295
309
  exports.OtherCharges = OtherCharges;
296
- function printedOrderTaxes(order_items, order_discounts, order_taxes, loyalty_amount, branch_vat) {
310
+ function printedOrderTaxes(order_items, order_discounts, order_taxes, loyalty_amount, branch_vat, isVoidOrRefund = false) {
311
+ if (!order_items || !Array.isArray(order_items))
312
+ return [];
297
313
  let taxes = [];
314
+ const multiplier = isVoidOrRefund ? -1 : 1;
298
315
  const sub = Subtotal(order_items);
299
316
  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));
300
- (order_taxes !== null && order_taxes !== void 0 ? order_taxes : []).forEach((tax) => {
301
- var _a, _b, _c;
302
- if (tax === null || tax === void 0 ? void 0 : tax.amount) {
303
- let 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);
304
- if (tax.isPercent && (tax === null || tax === void 0 ? void 0 : tax.minimum_amount)) {
305
- total = Math.max(total, (0, utils_1.preventNaN)(tax === null || tax === void 0 ? void 0 : tax.minimum_amount));
317
+ if (order_taxes !== null && order_taxes !== void 0 ? order_taxes : []) {
318
+ (order_taxes !== null && order_taxes !== void 0 ? order_taxes : []).forEach((tax) => {
319
+ if (tax === null || tax === void 0 ? void 0 : tax.amount) {
320
+ let total = tax.isPercent ? (tax.amount / 100) * (sub - discounts) : tax.amount;
321
+ if (tax.isPercent && (tax === null || tax === void 0 ? void 0 : tax.minimum_amount)) {
322
+ total = Math.max(total, (0, utils_1.preventNaN)(tax === null || tax === void 0 ? void 0 : tax.minimum_amount));
323
+ }
324
+ taxes.push({
325
+ tax_id: tax.tax_id || '',
326
+ name_lan_p: tax.name_lan_p || '',
327
+ name_lan_s: tax.name_lan_s || '',
328
+ isPercent: tax.isPercent,
329
+ amount: tax.amount * multiplier,
330
+ total: total * multiplier,
331
+ });
306
332
  }
307
- taxes.push({
308
- tax_id: (_a = tax === null || tax === void 0 ? void 0 : tax.tax_id) !== null && _a !== void 0 ? _a : '',
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: (_c = tax === null || tax === void 0 ? void 0 : tax.name_lan_s) !== null && _c !== void 0 ? _c : '',
311
- isPercent: !!(tax === null || tax === void 0 ? void 0 : tax.isPercent),
312
- amount: (0, utils_1.preventNaN)(tax === null || tax === void 0 ? void 0 : tax.amount),
313
- total: total,
314
- });
315
- }
316
- });
317
- 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)));
333
+ });
334
+ }
335
+ const order_items_count = (0, sum_1.default)((order_items !== null && order_items !== void 0 ? order_items : []).map((i) => (i === null || i === void 0 ? void 0 : i.quantity) || 0));
318
336
  const item_taxes = (order_items !== null && order_items !== void 0 ? order_items : []).reduce((list, order_item) => {
319
337
  var _a;
338
+ if (!order_item)
339
+ return list;
320
340
  const addons_amount = calcItemAddons(order_item);
321
- ((_a = order_item === null || order_item === void 0 ? void 0 : order_item.taxes) !== null && _a !== void 0 ? _a : []).forEach((tax) => {
322
- var _a, _b, _c;
341
+ (_a = order_item === null || order_item === void 0 ? void 0 : order_item.taxes) === null || _a === void 0 ? void 0 : _a.forEach((tax) => {
323
342
  let _discounts = calcItemDiscounts(order_item, order_discounts, order_items_count);
324
- if (tax === null || tax === void 0 ? void 0 : tax.tax_id) {
343
+ if (tax.tax_id) {
325
344
  let charge = 0;
326
- if ((tax === null || tax === void 0 ? void 0 : tax.isPercent) && (tax === null || tax === void 0 ? void 0 : tax.amount)) {
327
- charge =
328
- (((_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);
345
+ if (tax.isPercent && tax.amount) {
346
+ charge = ((tax.amount || 0) / 100) * (order_item.price + addons_amount - _discounts) * order_item.quantity;
329
347
  if (tax === null || tax === void 0 ? void 0 : tax.minimum_amount) {
330
- charge = Math.max(charge / (0, utils_1.preventNaN)(order_item === null || order_item === void 0 ? void 0 : order_item.quantity), (0, utils_1.preventNaN)(tax === null || tax === void 0 ? void 0 : tax.minimum_amount)) * (0, utils_1.preventNaN)(order_item === null || order_item === void 0 ? void 0 : order_item.quantity);
348
+ charge = Math.max(charge / order_item.quantity, (0, utils_1.preventNaN)(tax === null || tax === void 0 ? void 0 : tax.minimum_amount)) * order_item.quantity;
331
349
  }
332
350
  }
333
- else if (tax === null || tax === void 0 ? void 0 : tax.amount) {
334
- 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);
351
+ else if (tax.amount) {
352
+ charge = (tax.amount || 0) * order_item.quantity;
335
353
  }
336
- if (!list.some((t) => t.tax_id === (tax === null || tax === void 0 ? void 0 : tax.tax_id))) {
354
+ if (!list.some((t) => t.tax_id === tax.tax_id)) {
337
355
  list.push({
338
- tax_id: tax === null || tax === void 0 ? void 0 : tax.tax_id,
339
- name_lan_p: (_b = tax === null || tax === void 0 ? void 0 : tax.name_lan_p) !== null && _b !== void 0 ? _b : '',
340
- name_lan_s: tax === null || tax === void 0 ? void 0 : tax.name_lan_s,
341
- isPercent: tax === null || tax === void 0 ? void 0 : tax.isPercent,
342
- amount: (_c = tax === null || tax === void 0 ? void 0 : tax.amount) !== null && _c !== void 0 ? _c : 0,
343
- total: charge,
356
+ tax_id: tax.tax_id,
357
+ name_lan_p: tax.name_lan_p || '',
358
+ name_lan_s: tax.name_lan_s,
359
+ isPercent: tax.isPercent,
360
+ amount: (tax.amount || 0) * multiplier,
361
+ total: charge * multiplier,
344
362
  });
345
363
  }
346
364
  else {
347
- list = list.map((t) => (t.tax_id !== tax.tax_id ? t : Object.assign(Object.assign({}, t), { total: (0, utils_1.preventNaN)(t.total) + charge })));
365
+ list = list.map((t) => (t.tax_id !== tax.tax_id ? t : Object.assign(Object.assign({}, t), { total: t.total + charge })));
348
366
  }
349
367
  }
350
368
  });
351
369
  return list;
352
370
  }, []);
353
- return lodash_1.default.concat(taxes, item_taxes);
371
+ return (0, concat_1.default)(taxes, item_taxes);
354
372
  }
355
373
  exports.printedOrderTaxes = printedOrderTaxes;
356
374
  function Total(order_items, order_discounts, order_taxes, order_deliveries, loyalty_amount, branch_vat) {
375
+ if (!order_items || !Array.isArray(order_items))
376
+ return 0;
357
377
  const sub = Number(Subtotal(order_items));
358
378
  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));
359
379
  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));
360
380
  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));
361
381
  const deliveries = Number(Deliveries(order_deliveries !== null && order_deliveries !== void 0 ? order_deliveries : []));
362
- const total = (0, utils_1.toFixedFloat)(sub - discounts + other_charges + vat + deliveries);
363
- return total > 0 ? total : 0;
382
+ const total = sub - discounts + other_charges + vat + deliveries;
383
+ if (total < 0)
384
+ return 0;
385
+ return total;
364
386
  }
365
387
  exports.Total = Total;
366
388
  function calcPaymentBalanceDue(invoice, branch) {
367
389
  var _a, _b, _c;
368
- 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);
369
- ((_c = branch === null || branch === void 0 ? void 0 : branch.payment_methods) !== null && _c !== void 0 ? _c : []).forEach((payment) => {
370
- var _a;
371
- due -= (0, utils_1.preventNaN)((_a = payment === null || payment === void 0 ? void 0 : payment.payment_amount) !== null && _a !== void 0 ? _a : 0);
372
- });
390
+ if (!invoice)
391
+ return 0;
392
+ let due = ((_a = invoice === null || invoice === void 0 ? void 0 : invoice.total) !== null && _a !== void 0 ? _a : 0) - ((_b = invoice === null || invoice === void 0 ? void 0 : invoice.received_amount) !== null && _b !== void 0 ? _b : 0);
393
+ if ((branch === null || branch === void 0 ? void 0 : branch.payment_methods) && branch.payment_methods.length) {
394
+ (_c = branch === null || branch === void 0 ? void 0 : branch.payment_methods) !== null && _c !== void 0 ? _c : [].forEach((payment) => {
395
+ due -= payment.payment_amount || 0;
396
+ });
397
+ }
373
398
  return due > 0 ? due : 0;
374
399
  }
375
400
  exports.calcPaymentBalanceDue = calcPaymentBalanceDue;
376
401
  function calPaymentTotal(info) {
377
- var _a;
378
- let groupByPaymnets = lodash_1.default.map((_a = info === null || info === void 0 ? void 0 : info.payments) !== null && _a !== void 0 ? _a : [], (p) => {
379
- var _a, _b;
380
- return ({
381
- name_lan_p: (_a = p.name_lan_p) !== null && _a !== void 0 ? _a : '',
382
- name_lan_s: (_b = p.name_lan_s) !== null && _b !== void 0 ? _b : '',
383
- amount: p.payment_amount,
384
- });
385
- });
402
+ if (!info || !info.payments)
403
+ return {};
404
+ let groupByPaymnets = (0, map_1.default)(info.payments, (p) => ({
405
+ name_lan_p: p.name_lan_p,
406
+ name_lan_s: p.name_lan_s,
407
+ amount: p.payment_amount,
408
+ }));
386
409
  groupByPaymnets = groupByPaymnets.reduce(function (result, acc) {
387
410
  if (acc.name_lan_p !== 'CASH_ADD' && acc.name_lan_p !== 'CASH_REMOVE') {
388
- if (!result[acc.name_lan_p])
389
- result[acc.name_lan_p] = [];
390
- let val = (0, utils_1.toFixedFloat)((0, utils_1.preventNaN)(result[acc.name_lan_p].payment_amount));
411
+ result[acc.name_lan_p] = result[acc.name_lan_p] || [];
412
+ const val = (0, utils_1.toFixedFloat)(result[acc.name_lan_p].payment_amount) || 0;
391
413
  result[acc.name_lan_p] = {
392
414
  name_lan_p: acc.name_lan_p,
393
415
  name_lan_s: acc.name_lan_s,
@@ -400,35 +422,36 @@ function calPaymentTotal(info) {
400
422
  }
401
423
  exports.calPaymentTotal = calPaymentTotal;
402
424
  function calcClosingCashBalance(closing, opening_start_cash) {
403
- var _a, _b, _c;
404
- return ((0, utils_1.preventNaN)((_a = closing === null || closing === void 0 ? void 0 : closing.closing_cash_sales) !== null && _a !== void 0 ? _a : 0) +
405
- (0, utils_1.preventNaN)(opening_start_cash !== null && opening_start_cash !== void 0 ? opening_start_cash : 0) +
406
- ((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)));
425
+ return ((0, utils_1.preventNaN)(closing.closing_cash_sales || 0) +
426
+ (0, utils_1.preventNaN)(opening_start_cash || 0) +
427
+ ((0, utils_1.preventNaN)(closing.closing_cash_add || 0) - (0, utils_1.preventNaN)(closing.closing_cash_remove || 0)));
407
428
  }
408
429
  exports.calcClosingCashBalance = calcClosingCashBalance;
409
430
  function calcItemDiscounts(item, order_discounts, order_items_count) {
410
431
  var _a, _b;
432
+ if (!item)
433
+ return 0;
411
434
  let discounts = 0;
412
435
  let addons = 0;
413
- ((_a = item === null || item === void 0 ? void 0 : item.addons) !== null && _a !== void 0 ? _a : []).forEach((addon) => {
414
- if ((addon === null || addon === void 0 ? void 0 : addon.price) && (addon === null || addon === void 0 ? void 0 : addon.quantity)) {
415
- 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);
436
+ (_a = item === null || item === void 0 ? void 0 : item.addons) === null || _a === void 0 ? void 0 : _a.forEach((addon) => {
437
+ if (addon.price && addon.quantity) {
438
+ addons += addon.price * addon.quantity;
416
439
  }
417
440
  });
418
- ((_b = item === null || item === void 0 ? void 0 : item.discounts) !== null && _b !== void 0 ? _b : []).forEach((discount) => {
419
- if ((discount === null || discount === void 0 ? void 0 : discount.is_percentage) && (discount === null || discount === void 0 ? void 0 : discount.amount)) {
420
- 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);
441
+ (_b = item.discounts) === null || _b === void 0 ? void 0 : _b.forEach((discount) => {
442
+ if (discount.is_percentage && discount.amount) {
443
+ discounts += (item.price + addons) * (discount.amount / 100);
421
444
  }
422
- else if (discount === null || discount === void 0 ? void 0 : discount.amount) {
423
- discounts += (0, utils_1.preventNaN)(discount === null || discount === void 0 ? void 0 : discount.amount);
445
+ else if (discount.amount) {
446
+ discounts += discount.amount;
424
447
  }
425
448
  });
426
- (order_discounts !== null && order_discounts !== void 0 ? order_discounts : []).forEach((discount) => {
427
- if ((discount === null || discount === void 0 ? void 0 : discount.is_percentage) && (discount === null || discount === void 0 ? void 0 : discount.amount)) {
428
- 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);
449
+ order_discounts === null || order_discounts === void 0 ? void 0 : order_discounts.forEach((discount) => {
450
+ if (discount.is_percentage && discount.amount) {
451
+ discounts += (item.price + addons) * (discount.amount / 100);
429
452
  }
430
- else if (discount === null || discount === void 0 ? void 0 : discount.amount) {
431
- discounts += (0, utils_1.preventNaN)(discount === null || discount === void 0 ? void 0 : discount.amount) / order_items_count;
453
+ else if (discount.amount) {
454
+ discounts += discount.amount / order_items_count;
432
455
  }
433
456
  });
434
457
  return discounts;
@@ -436,28 +459,48 @@ function calcItemDiscounts(item, order_discounts, order_items_count) {
436
459
  exports.calcItemDiscounts = calcItemDiscounts;
437
460
  function calcItemAddons(item) {
438
461
  var _a;
462
+ if (!item)
463
+ return 0;
439
464
  let addons = 0;
440
- ((_a = item === null || item === void 0 ? void 0 : item.addons) !== null && _a !== void 0 ? _a : []).forEach((addon) => {
441
- if ((addon === null || addon === void 0 ? void 0 : addon.price) && (addon === null || addon === void 0 ? void 0 : addon.quantity)) {
442
- 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);
465
+ (_a = item === null || item === void 0 ? void 0 : item.addons) === null || _a === void 0 ? void 0 : _a.forEach((addon) => {
466
+ if (addon.price && addon.quantity) {
467
+ addons += addon.price * addon.quantity;
443
468
  }
444
469
  });
445
470
  return addons;
446
471
  }
447
472
  exports.calcItemAddons = calcItemAddons;
448
473
  function getInvoicesDiffrence(openning, invoices) {
449
- const now = (0, moment_1.default)(new Date());
450
- const openningTime = (0, moment_1.default)((0, utils_1.convertToDate)(openning === null || openning === void 0 ? void 0 : openning.cashier_opening_date));
451
- const orders = (invoices !== null && invoices !== void 0 ? invoices : []).filter((invoice) => {
452
- const invoiceDate = (0, moment_1.default)((0, utils_1.convertToDate)(invoice === null || invoice === void 0 ? void 0 : invoice.order_date));
453
- return !invoiceDate.isBetween(openningTime, now);
474
+ if (!openning || !invoices || !Array.isArray(invoices))
475
+ return [];
476
+ const openningTime = (0, moment_1.default)((0, utils_1.convertToDate)(openning.cashier_opening_date));
477
+ return invoices.filter((invoice) => {
478
+ if (!invoice)
479
+ return false;
480
+ const invoiceDate = (0, moment_1.default)((0, utils_1.convertToDate)(invoice.order_date));
481
+ return invoiceDate.isBefore(openningTime);
454
482
  });
455
- return orders;
456
483
  }
457
484
  exports.getInvoicesDiffrence = getInvoicesDiffrence;
485
+ function getActiveInvoices(invoices) {
486
+ if (!invoices || !Array.isArray(invoices))
487
+ return [];
488
+ const list = invoices.filter((invoice) => { var _a; return invoice && ((_a = invoice.order_payments) === null || _a === void 0 ? void 0 : _a.length) === 0; });
489
+ return list;
490
+ }
491
+ exports.getActiveInvoices = getActiveInvoices;
492
+ function calcRefundedLoyaltyAmount(order, canceled_items) {
493
+ var _a, _b;
494
+ if (!order || !canceled_items)
495
+ return 0;
496
+ const items_count = (0, sum_1.default)(((_a = order === null || order === void 0 ? void 0 : order.order_items) === null || _a === void 0 ? void 0 : _a.map((o) => (o === null || o === void 0 ? void 0 : o.quantity) || 0)) || [0]) + (0, sum_1.default)(((_b = order === null || order === void 0 ? void 0 : order.canceled_items) === null || _b === void 0 ? void 0 : _b.map((o) => (o === null || o === void 0 ? void 0 : o.quantity) || 0)) || [0]);
497
+ const canceled_items_count = (0, sum_1.default)((canceled_items === null || canceled_items === void 0 ? void 0 : canceled_items.map((o) => (o === null || o === void 0 ? void 0 : o.quantity) || 0)) || [0]);
498
+ return (((order === null || order === void 0 ? void 0 : order.loyalty_amount) || 0) / items_count) * canceled_items_count;
499
+ }
500
+ exports.calcRefundedLoyaltyAmount = calcRefundedLoyaltyAmount;
458
501
  function WithVat(price, branch_vat) {
459
502
  if (branch_vat && price) {
460
- return parseFloat((0, utils_1.toFixed)(price * (1 + branch_vat / 100)));
503
+ return price * (1 + branch_vat / 100);
461
504
  }
462
505
  return price !== null && price !== void 0 ? price : 0;
463
506
  }