shareneus 1.7.337 → 1.7.339

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.
@@ -31,6 +31,7 @@ export declare class TransactionCalculationEngine {
31
31
  total: number;
32
32
  taxRows: unknown;
33
33
  includeTaxInTotal: boolean;
34
+ qty?: number;
34
35
  }): number;
35
36
  static reverseCalculateAmountAndUnitPriceFromTotal(input: {
36
37
  qty: number;
@@ -48,6 +48,60 @@ function sumTaxRateFromTaxes(taxes) {
48
48
  const rows = getTaxesArray(taxes);
49
49
  return rows.reduce((sum, row) => { var _a; return (0, math_operations_1.Add)(sum, toNum((_a = row === null || row === void 0 ? void 0 : row.Rate) !== null && _a !== void 0 ? _a : row === null || row === void 0 ? void 0 : row.rate)); }, 0);
50
50
  }
51
+ function normalizeCalcMethod(calcMethod) {
52
+ return calcMethod === 'Percentage' || isNullOrEmpty(calcMethod)
53
+ ? 'Percent'
54
+ : String(calcMethod);
55
+ }
56
+ function getTaxRowQty(row, fallbackQty) {
57
+ var _a, _b, _c;
58
+ return toNum((_c = (_b = (_a = row === null || row === void 0 ? void 0 : row.Qty) !== null && _a !== void 0 ? _a : row === null || row === void 0 ? void 0 : row.qty) !== null && _b !== void 0 ? _b : row === null || row === void 0 ? void 0 : row.Quantity) !== null && _c !== void 0 ? _c : row === null || row === void 0 ? void 0 : row.quantity) || fallbackQty;
59
+ }
60
+ function getPerUnitTaxAmount(row, fallbackQty, fallbackToAmt = true) {
61
+ var _a, _b;
62
+ const perUnitAmt = toNum((_a = row === null || row === void 0 ? void 0 : row.PerUnitAmt) !== null && _a !== void 0 ? _a : row === null || row === void 0 ? void 0 : row.perUnitAmt);
63
+ const qty = getTaxRowQty(row, fallbackQty);
64
+ if (perUnitAmt !== 0 && qty !== 0) {
65
+ return (0, math_operations_1.Multiply)(perUnitAmt, qty);
66
+ }
67
+ return fallbackToAmt ? toNum((_b = row === null || row === void 0 ? void 0 : row.Amt) !== null && _b !== void 0 ? _b : row === null || row === void 0 ? void 0 : row.amt) : 0;
68
+ }
69
+ function getReverseTaxBreakdown(taxes, qty) {
70
+ return getTaxesArray(taxes).reduce((summary, row) => {
71
+ var _a, _b, _c;
72
+ const calcMethod = normalizeCalcMethod((_a = row === null || row === void 0 ? void 0 : row.CalcMethod) !== null && _a !== void 0 ? _a : row === null || row === void 0 ? void 0 : row.calcMethod);
73
+ const rate = toNum((_b = row === null || row === void 0 ? void 0 : row.Rate) !== null && _b !== void 0 ? _b : row === null || row === void 0 ? void 0 : row.rate);
74
+ console.log('Reverse tax row', { row, calcMethod, rate, qty });
75
+ switch (calcMethod) {
76
+ case 'PerUnit':
77
+ summary.fixedTaxAmount = (0, math_operations_1.Add)(summary.fixedTaxAmount, getPerUnitTaxAmount(row, qty));
78
+ break;
79
+ case 'PerUnitPlusPercent':
80
+ summary.fixedTaxAmount = (0, math_operations_1.Add)(summary.fixedTaxAmount, getPerUnitTaxAmount(row, qty, false));
81
+ summary.percentTaxRate = (0, math_operations_1.Add)(summary.percentTaxRate, rate);
82
+ break;
83
+ case 'MaxOfPercentOrPerUnit': {
84
+ const amt = toNum((_c = row === null || row === void 0 ? void 0 : row.Amt) !== null && _c !== void 0 ? _c : row === null || row === void 0 ? void 0 : row.amt);
85
+ const perUnitAmt = getPerUnitTaxAmount(row, qty);
86
+ if (amt !== 0) {
87
+ summary.fixedTaxAmount = (0, math_operations_1.Add)(summary.fixedTaxAmount, amt);
88
+ }
89
+ else if (perUnitAmt !== 0) {
90
+ summary.fixedTaxAmount = (0, math_operations_1.Add)(summary.fixedTaxAmount, perUnitAmt);
91
+ }
92
+ else {
93
+ summary.percentTaxRate = (0, math_operations_1.Add)(summary.percentTaxRate, rate);
94
+ }
95
+ break;
96
+ }
97
+ case 'Percent':
98
+ default:
99
+ summary.percentTaxRate = (0, math_operations_1.Add)(summary.percentTaxRate, rate);
100
+ break;
101
+ }
102
+ return summary;
103
+ }, { fixedTaxAmount: 0, percentTaxRate: 0 });
104
+ }
51
105
  /**
52
106
  * Distribute a flat discount amount across a set of rows proportionally
53
107
  * by each row's `baseKey` value. Returns an array of share amounts aligned
@@ -115,12 +169,14 @@ class TransactionCalculationEngine {
115
169
  if (!input.includeTaxInTotal) {
116
170
  return total;
117
171
  }
118
- const taxSummary = TransactionCalculationEngine.getTaxesSummary(input.taxRows);
119
- if (taxSummary.taxRate) {
120
- const divisor = (0, math_operations_1.Add)(1, (0, math_operations_1.Divide)(taxSummary.taxRate, 100));
121
- return divisor === 0 ? total : (0, math_operations_1.Divide)(total, divisor);
172
+ const taxBreakdown = getReverseTaxBreakdown(input.taxRows, toNum(input.qty) || 1);
173
+ console.log('Reverse tax breakdown', taxBreakdown);
174
+ const totalAfterFixedTax = (0, math_operations_1.Subtract)(total, taxBreakdown.fixedTaxAmount);
175
+ if (taxBreakdown.percentTaxRate) {
176
+ const divisor = (0, math_operations_1.Add)(1, (0, math_operations_1.Divide)(taxBreakdown.percentTaxRate, 100));
177
+ return divisor === 0 ? totalAfterFixedTax : (0, math_operations_1.Divide)(totalAfterFixedTax, divisor);
122
178
  }
123
- return (0, math_operations_1.Subtract)(total, taxSummary.taxAmount);
179
+ return totalAfterFixedTax;
124
180
  }
125
181
  static reverseCalculateAmountAndUnitPriceFromTotal(input) {
126
182
  const qty = toNum(input.qty) || 1;
@@ -128,6 +184,7 @@ class TransactionCalculationEngine {
128
184
  total: input.total,
129
185
  taxRows: input.taxRows,
130
186
  includeTaxInTotal: input.includeTaxInTotal,
187
+ qty,
131
188
  });
132
189
  const disc = toNum(input.disc);
133
190
  const recDisc = toNum(input.recDisc);
@@ -369,15 +426,14 @@ class TransactionCalculationEngine {
369
426
  if (qty === 0) {
370
427
  return Object.assign({}, item);
371
428
  }
372
- const taxSummary = TransactionCalculationEngine.getTaxesSummary((_a = item === null || item === void 0 ? void 0 : item.Taxes) !== null && _a !== void 0 ? _a : item === null || item === void 0 ? void 0 : item.taxes);
373
429
  // If total doesn't include tax, treat it as "already stripped".
374
430
  const subtotal = includeTaxInTotal
375
- ? taxSummary.taxAmount !== 0
376
- ? (0, math_operations_1.Subtract)(total, taxSummary.taxAmount)
377
- : (() => {
378
- const divisor = (0, math_operations_1.Add)(1, (0, math_operations_1.Divide)(taxSummary.taxRate, 100));
379
- return (0, math_operations_1.Divide)(total, divisor === 0 ? 1 : divisor);
380
- })()
431
+ ? TransactionCalculationEngine.calculateSubtotalFromTotalUsingTaxes({
432
+ total,
433
+ taxRows: (_a = item === null || item === void 0 ? void 0 : item.Taxes) !== null && _a !== void 0 ? _a : item === null || item === void 0 ? void 0 : item.taxes,
434
+ includeTaxInTotal,
435
+ qty,
436
+ })
381
437
  : total;
382
438
  if (item.NoDisc) {
383
439
  const unpr = (0, math_operations_1.Divide)(subtotal, qty);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shareneus",
3
- "version": "1.7.337",
3
+ "version": "1.7.339",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -73,6 +73,81 @@ function sumTaxRateFromTaxes(taxes: any): number {
73
73
  );
74
74
  }
75
75
 
76
+ function normalizeCalcMethod(calcMethod: any): string {
77
+ return calcMethod === 'Percentage' || isNullOrEmpty(calcMethod)
78
+ ? 'Percent'
79
+ : String(calcMethod);
80
+ }
81
+
82
+ function getTaxRowQty(row: any, fallbackQty: number): number {
83
+ return toNum(row?.Qty ?? row?.qty ?? row?.Quantity ?? row?.quantity) || fallbackQty;
84
+ }
85
+
86
+ function getPerUnitTaxAmount(
87
+ row: any,
88
+ fallbackQty: number,
89
+ fallbackToAmt = true,
90
+ ): number {
91
+ const perUnitAmt = toNum(row?.PerUnitAmt ?? row?.perUnitAmt);
92
+ const qty = getTaxRowQty(row, fallbackQty);
93
+
94
+ if (perUnitAmt !== 0 && qty !== 0) {
95
+ return Multiply(perUnitAmt, qty);
96
+ }
97
+
98
+ return fallbackToAmt ? toNum(row?.Amt ?? row?.amt) : 0;
99
+ }
100
+
101
+ function getReverseTaxBreakdown(taxes: any, qty: number): {
102
+ fixedTaxAmount: number;
103
+ percentTaxRate: number;
104
+ } {
105
+ return getTaxesArray(taxes).reduce(
106
+ (summary, row) => {
107
+ const calcMethod = normalizeCalcMethod(row?.CalcMethod ?? row?.calcMethod);
108
+ const rate = toNum(row?.Rate ?? row?.rate);
109
+ console.log('Reverse tax row', { row, calcMethod, rate, qty });
110
+ switch (calcMethod) {
111
+ case 'PerUnit':
112
+ summary.fixedTaxAmount = Add(
113
+ summary.fixedTaxAmount,
114
+ getPerUnitTaxAmount(row, qty),
115
+ );
116
+ break;
117
+
118
+ case 'PerUnitPlusPercent':
119
+ summary.fixedTaxAmount = Add(
120
+ summary.fixedTaxAmount,
121
+ getPerUnitTaxAmount(row, qty, false),
122
+ );
123
+ summary.percentTaxRate = Add(summary.percentTaxRate, rate);
124
+ break;
125
+
126
+ case 'MaxOfPercentOrPerUnit': {
127
+ const amt = toNum(row?.Amt ?? row?.amt);
128
+ const perUnitAmt = getPerUnitTaxAmount(row, qty);
129
+ if (amt !== 0) {
130
+ summary.fixedTaxAmount = Add(summary.fixedTaxAmount, amt);
131
+ } else if (perUnitAmt !== 0) {
132
+ summary.fixedTaxAmount = Add(summary.fixedTaxAmount, perUnitAmt);
133
+ } else {
134
+ summary.percentTaxRate = Add(summary.percentTaxRate, rate);
135
+ }
136
+ break;
137
+ }
138
+
139
+ case 'Percent':
140
+ default:
141
+ summary.percentTaxRate = Add(summary.percentTaxRate, rate);
142
+ break;
143
+ }
144
+
145
+ return summary;
146
+ },
147
+ { fixedTaxAmount: 0, percentTaxRate: 0 },
148
+ );
149
+ }
150
+
76
151
  /**
77
152
  * Distribute a flat discount amount across a set of rows proportionally
78
153
  * by each row's `baseKey` value. Returns an array of share amounts aligned
@@ -157,21 +232,26 @@ export class TransactionCalculationEngine {
157
232
  total: number;
158
233
  taxRows: unknown;
159
234
  includeTaxInTotal: boolean;
235
+ qty?: number;
160
236
  }): number {
161
237
  const total = toNum(input.total);
162
238
  if (!input.includeTaxInTotal) {
163
239
  return total;
164
240
  }
165
241
 
166
- const taxSummary = TransactionCalculationEngine.getTaxesSummary(
242
+ const taxBreakdown = getReverseTaxBreakdown(
167
243
  input.taxRows,
244
+ toNum(input.qty) || 1,
168
245
  );
169
- if (taxSummary.taxRate) {
170
- const divisor = Add(1, Divide(taxSummary.taxRate, 100));
171
- return divisor === 0 ? total : Divide(total, divisor);
246
+ console.log('Reverse tax breakdown', taxBreakdown);
247
+ const totalAfterFixedTax = Subtract(total, taxBreakdown.fixedTaxAmount);
248
+
249
+ if (taxBreakdown.percentTaxRate) {
250
+ const divisor = Add(1, Divide(taxBreakdown.percentTaxRate, 100));
251
+ return divisor === 0 ? totalAfterFixedTax : Divide(totalAfterFixedTax, divisor);
172
252
  }
173
253
 
174
- return Subtract(total, taxSummary.taxAmount);
254
+ return totalAfterFixedTax;
175
255
  }
176
256
 
177
257
  static reverseCalculateAmountAndUnitPriceFromTotal(input: {
@@ -189,6 +269,7 @@ export class TransactionCalculationEngine {
189
269
  total: input.total,
190
270
  taxRows: input.taxRows,
191
271
  includeTaxInTotal: input.includeTaxInTotal,
272
+ qty,
192
273
  });
193
274
 
194
275
  const disc = toNum(input.disc);
@@ -474,18 +555,14 @@ export class TransactionCalculationEngine {
474
555
  return { ...item };
475
556
  }
476
557
 
477
- const taxSummary = TransactionCalculationEngine.getTaxesSummary(
478
- item?.Taxes ?? item?.taxes,
479
- );
480
-
481
558
  // If total doesn't include tax, treat it as "already stripped".
482
559
  const subtotal = includeTaxInTotal
483
- ? taxSummary.taxAmount !== 0
484
- ? Subtract(total, taxSummary.taxAmount)
485
- : (() => {
486
- const divisor = Add(1, Divide(taxSummary.taxRate, 100));
487
- return Divide(total, divisor === 0 ? 1 : divisor);
488
- })()
560
+ ? TransactionCalculationEngine.calculateSubtotalFromTotalUsingTaxes({
561
+ total,
562
+ taxRows: item?.Taxes ?? item?.taxes,
563
+ includeTaxInTotal,
564
+ qty,
565
+ })
489
566
  : total;
490
567
 
491
568
  if (item.NoDisc) {