shareneus 1.7.338 → 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.
|
@@ -71,6 +71,7 @@ function getReverseTaxBreakdown(taxes, qty) {
|
|
|
71
71
|
var _a, _b, _c;
|
|
72
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
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 });
|
|
74
75
|
switch (calcMethod) {
|
|
75
76
|
case 'PerUnit':
|
|
76
77
|
summary.fixedTaxAmount = (0, math_operations_1.Add)(summary.fixedTaxAmount, getPerUnitTaxAmount(row, qty));
|
|
@@ -169,6 +170,7 @@ class TransactionCalculationEngine {
|
|
|
169
170
|
return total;
|
|
170
171
|
}
|
|
171
172
|
const taxBreakdown = getReverseTaxBreakdown(input.taxRows, toNum(input.qty) || 1);
|
|
173
|
+
console.log('Reverse tax breakdown', taxBreakdown);
|
|
172
174
|
const totalAfterFixedTax = (0, math_operations_1.Subtract)(total, taxBreakdown.fixedTaxAmount);
|
|
173
175
|
if (taxBreakdown.percentTaxRate) {
|
|
174
176
|
const divisor = (0, math_operations_1.Add)(1, (0, math_operations_1.Divide)(taxBreakdown.percentTaxRate, 100));
|
package/package.json
CHANGED
|
@@ -65,88 +65,88 @@ function sumTaxAmountFromTaxes(taxes: any): number {
|
|
|
65
65
|
);
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
function sumTaxRateFromTaxes(taxes: any): number {
|
|
69
|
-
const rows = getTaxesArray(taxes);
|
|
70
|
-
return rows.reduce(
|
|
71
|
-
(sum, row) => Add(sum, toNum(row?.Rate ?? row?.rate)),
|
|
72
|
-
0,
|
|
73
|
-
);
|
|
74
|
-
}
|
|
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
|
-
|
|
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
|
-
}
|
|
68
|
+
function sumTaxRateFromTaxes(taxes: any): number {
|
|
69
|
+
const rows = getTaxesArray(taxes);
|
|
70
|
+
return rows.reduce(
|
|
71
|
+
(sum, row) => Add(sum, toNum(row?.Rate ?? row?.rate)),
|
|
72
|
+
0,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
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
150
|
|
|
151
151
|
/**
|
|
152
152
|
* Distribute a flat discount amount across a set of rows proportionally
|
|
@@ -228,30 +228,31 @@ export class TransactionCalculationEngine {
|
|
|
228
228
|
: baseAmount;
|
|
229
229
|
}
|
|
230
230
|
|
|
231
|
-
static calculateSubtotalFromTotalUsingTaxes(input: {
|
|
232
|
-
total: number;
|
|
233
|
-
taxRows: unknown;
|
|
234
|
-
includeTaxInTotal: boolean;
|
|
235
|
-
qty?: number;
|
|
236
|
-
}): number {
|
|
237
|
-
const total = toNum(input.total);
|
|
238
|
-
if (!input.includeTaxInTotal) {
|
|
239
|
-
return total;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
const taxBreakdown = getReverseTaxBreakdown(
|
|
243
|
-
input.taxRows,
|
|
244
|
-
toNum(input.qty) || 1,
|
|
245
|
-
);
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
231
|
+
static calculateSubtotalFromTotalUsingTaxes(input: {
|
|
232
|
+
total: number;
|
|
233
|
+
taxRows: unknown;
|
|
234
|
+
includeTaxInTotal: boolean;
|
|
235
|
+
qty?: number;
|
|
236
|
+
}): number {
|
|
237
|
+
const total = toNum(input.total);
|
|
238
|
+
if (!input.includeTaxInTotal) {
|
|
239
|
+
return total;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const taxBreakdown = getReverseTaxBreakdown(
|
|
243
|
+
input.taxRows,
|
|
244
|
+
toNum(input.qty) || 1,
|
|
245
|
+
);
|
|
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);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return totalAfterFixedTax;
|
|
255
|
+
}
|
|
255
256
|
|
|
256
257
|
static reverseCalculateAmountAndUnitPriceFromTotal(input: {
|
|
257
258
|
qty: number;
|
|
@@ -264,12 +265,12 @@ export class TransactionCalculationEngine {
|
|
|
264
265
|
}): { amount: number; unitPrice: number } {
|
|
265
266
|
const qty = toNum(input.qty) || 1;
|
|
266
267
|
const subtotal =
|
|
267
|
-
TransactionCalculationEngine.calculateSubtotalFromTotalUsingTaxes({
|
|
268
|
-
total: input.total,
|
|
269
|
-
taxRows: input.taxRows,
|
|
270
|
-
includeTaxInTotal: input.includeTaxInTotal,
|
|
271
|
-
qty,
|
|
272
|
-
});
|
|
268
|
+
TransactionCalculationEngine.calculateSubtotalFromTotalUsingTaxes({
|
|
269
|
+
total: input.total,
|
|
270
|
+
taxRows: input.taxRows,
|
|
271
|
+
includeTaxInTotal: input.includeTaxInTotal,
|
|
272
|
+
qty,
|
|
273
|
+
});
|
|
273
274
|
|
|
274
275
|
const disc = toNum(input.disc);
|
|
275
276
|
const recDisc = toNum(input.recDisc);
|
|
@@ -554,15 +555,15 @@ export class TransactionCalculationEngine {
|
|
|
554
555
|
return { ...item };
|
|
555
556
|
}
|
|
556
557
|
|
|
557
|
-
// If total doesn't include tax, treat it as "already stripped".
|
|
558
|
-
const subtotal = includeTaxInTotal
|
|
559
|
-
? TransactionCalculationEngine.calculateSubtotalFromTotalUsingTaxes({
|
|
560
|
-
total,
|
|
561
|
-
taxRows: item?.Taxes ?? item?.taxes,
|
|
562
|
-
includeTaxInTotal,
|
|
563
|
-
qty,
|
|
564
|
-
})
|
|
565
|
-
: total;
|
|
558
|
+
// If total doesn't include tax, treat it as "already stripped".
|
|
559
|
+
const subtotal = includeTaxInTotal
|
|
560
|
+
? TransactionCalculationEngine.calculateSubtotalFromTotalUsingTaxes({
|
|
561
|
+
total,
|
|
562
|
+
taxRows: item?.Taxes ?? item?.taxes,
|
|
563
|
+
includeTaxInTotal,
|
|
564
|
+
qty,
|
|
565
|
+
})
|
|
566
|
+
: total;
|
|
566
567
|
|
|
567
568
|
if (item.NoDisc) {
|
|
568
569
|
const unpr = Divide(subtotal, qty);
|