lazywait-calcs 1.8.6 → 1.8.7

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