shareneus 1.7.340 → 1.7.341

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.
@@ -37,6 +37,7 @@ export declare class TransactionCalculationEngine {
37
37
  qty: number;
38
38
  total: number;
39
39
  disc: number;
40
+ perc?: string;
40
41
  recDisc: number;
41
42
  noDisc: boolean;
42
43
  taxRows: unknown;
@@ -71,7 +71,6 @@ 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 });
75
74
  switch (calcMethod) {
76
75
  case 'PerUnit':
77
76
  summary.fixedTaxAmount = (0, math_operations_1.Add)(summary.fixedTaxAmount, getPerUnitTaxAmount(row, qty));
@@ -170,7 +169,6 @@ class TransactionCalculationEngine {
170
169
  return total;
171
170
  }
172
171
  const taxBreakdown = getReverseTaxBreakdown(input.taxRows, toNum(input.qty) || 1);
173
- console.log('Reverse tax breakdown', taxBreakdown);
174
172
  const totalAfterFixedTax = (0, math_operations_1.Subtract)(total, taxBreakdown.fixedTaxAmount);
175
173
  if (taxBreakdown.percentTaxRate) {
176
174
  const divisor = (0, math_operations_1.Add)(1, (0, math_operations_1.Divide)(taxBreakdown.percentTaxRate, 100));
@@ -186,15 +184,23 @@ class TransactionCalculationEngine {
186
184
  includeTaxInTotal: input.includeTaxInTotal,
187
185
  qty,
188
186
  });
189
- console.log('Subtotal from total using taxes', subtotal);
190
187
  const disc = toNum(input.disc);
188
+ const perc = toNum(input.perc);
191
189
  const recDisc = toNum(input.recDisc);
192
- console.log('Discounts', { disc, recDisc });
193
- const amount = input.noDisc
194
- ? subtotal
195
- : (0, math_operations_1.Add)(subtotal, (0, math_operations_1.Add)(disc, recDisc));
190
+ let amount = subtotal;
191
+ if (!input.noDisc) {
192
+ if (!isNullOrEmpty(input.perc) && perc !== 0) {
193
+ const discountRate = (0, math_operations_1.Divide)(perc, 100);
194
+ const priceFactor = (0, math_operations_1.Subtract)(1, discountRate);
195
+ amount = priceFactor === 0
196
+ ? subtotal
197
+ : (0, math_operations_1.Divide)((0, math_operations_1.Add)(subtotal, recDisc), priceFactor);
198
+ }
199
+ else {
200
+ amount = (0, math_operations_1.Add)(subtotal, (0, math_operations_1.Add)(disc, recDisc));
201
+ }
202
+ }
196
203
  const unitPrice = (0, math_operations_1.Divide)(amount, qty);
197
- console.log('Reverse calculated amount and unit price', { amount, unitPrice });
198
204
  return { amount, unitPrice };
199
205
  }
200
206
  // Shared reverse-calculation formula. Components only need to supply their own field mapping.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shareneus",
3
- "version": "1.7.340",
3
+ "version": "1.7.341",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -106,7 +106,7 @@ function getReverseTaxBreakdown(taxes: any, qty: number): {
106
106
  (summary, row) => {
107
107
  const calcMethod = normalizeCalcMethod(row?.CalcMethod ?? row?.calcMethod);
108
108
  const rate = toNum(row?.Rate ?? row?.rate);
109
- console.log('Reverse tax row', { row, calcMethod, rate, qty });
109
+
110
110
  switch (calcMethod) {
111
111
  case 'PerUnit':
112
112
  summary.fixedTaxAmount = Add(
@@ -243,7 +243,6 @@ export class TransactionCalculationEngine {
243
243
  input.taxRows,
244
244
  toNum(input.qty) || 1,
245
245
  );
246
- console.log('Reverse tax breakdown', taxBreakdown);
247
246
  const totalAfterFixedTax = Subtract(total, taxBreakdown.fixedTaxAmount);
248
247
 
249
248
  if (taxBreakdown.percentTaxRate) {
@@ -254,14 +253,15 @@ export class TransactionCalculationEngine {
254
253
  return totalAfterFixedTax;
255
254
  }
256
255
 
257
- static reverseCalculateAmountAndUnitPriceFromTotal(input: {
258
- qty: number;
259
- total: number;
260
- disc: number;
261
- recDisc: number;
262
- noDisc: boolean;
263
- taxRows: unknown;
264
- includeTaxInTotal: boolean;
256
+ static reverseCalculateAmountAndUnitPriceFromTotal(input: {
257
+ qty: number;
258
+ total: number;
259
+ disc: number;
260
+ perc?: string;
261
+ recDisc: number;
262
+ noDisc: boolean;
263
+ taxRows: unknown;
264
+ includeTaxInTotal: boolean;
265
265
  }): { amount: number; unitPrice: number } {
266
266
  const qty = toNum(input.qty) || 1;
267
267
  const subtotal =
@@ -271,17 +271,28 @@ export class TransactionCalculationEngine {
271
271
  includeTaxInTotal: input.includeTaxInTotal,
272
272
  qty,
273
273
  });
274
- console.log('Subtotal from total using taxes', subtotal);
275
- const disc = toNum(input.disc);
276
- const recDisc = toNum(input.recDisc);
277
- console.log('Discounts', { disc, recDisc });
278
- const amount = input.noDisc
279
- ? subtotal
280
- : Add(subtotal, Add(disc, recDisc));
281
- const unitPrice = Divide(amount, qty);
282
- console.log('Reverse calculated amount and unit price', { amount, unitPrice });
283
- return { amount, unitPrice };
284
- }
274
+
275
+ const disc = toNum(input.disc);
276
+ const perc = toNum(input.perc);
277
+ const recDisc = toNum(input.recDisc);
278
+ let amount = subtotal;
279
+
280
+ if (!input.noDisc) {
281
+ if (!isNullOrEmpty(input.perc) && perc !== 0) {
282
+ const discountRate = Divide(perc, 100);
283
+ const priceFactor = Subtract(1, discountRate);
284
+ amount = priceFactor === 0
285
+ ? subtotal
286
+ : Divide(Add(subtotal, recDisc), priceFactor);
287
+ } else {
288
+ amount = Add(subtotal, Add(disc, recDisc));
289
+ }
290
+ }
291
+
292
+ const unitPrice = Divide(amount, qty);
293
+
294
+ return { amount, unitPrice };
295
+ }
285
296
 
286
297
  // Shared reverse-calculation formula. Components only need to supply their own field mapping.
287
298
  static calculatePriceFromTotal(input: ReverseCalculationInput): number {
@@ -315,7 +326,7 @@ export class TransactionCalculationEngine {
315
326
  ? taxCode.Components
316
327
  : [];
317
328
 
318
- return components.reduce((sum: number, comp: any) => Add(sum, toNum(comp?.Rate)), 0);
329
+ return components.reduce((sum:number, comp: any) => Add(sum, toNum(comp?.Rate)), 0);
319
330
  }
320
331
 
321
332
  // -------------------------------------------------------------------------
@@ -559,11 +570,11 @@ export class TransactionCalculationEngine {
559
570
  // If total doesn't include tax, treat it as "already stripped".
560
571
  const subtotal = includeTaxInTotal
561
572
  ? TransactionCalculationEngine.calculateSubtotalFromTotalUsingTaxes({
562
- total,
563
- taxRows: item?.Taxes ?? item?.taxes,
564
- includeTaxInTotal,
565
- qty,
566
- })
573
+ total,
574
+ taxRows: item?.Taxes ?? item?.taxes,
575
+ includeTaxInTotal,
576
+ qty,
577
+ })
567
578
  : total;
568
579
 
569
580
  if (item.NoDisc) {