shareneus 1.7.334 → 1.7.336

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.
@@ -55,7 +55,6 @@ class InvoiceTotalsService {
55
55
  }
56
56
  static CalculateLaborValues(opCodesList, IsIndependentTax) {
57
57
  opCodesList = this.ResetLaborValues(opCodesList, IsIndependentTax);
58
- console.log('opCodesList', opCodesList);
59
58
  // opCodesList = this.GetOperationsDiscountPrice(opCodesList);
60
59
  // opCodesList = this.GetOperationsAfterDiscount(opCodesList,IsIndependentTax);
61
60
  // opCodesList = this.GetOperationsAfterTax(opCodesList, IsIndependentTax);
@@ -246,7 +246,7 @@ function WithOutDiscountFieldTable(Parts, Ops, PrintPartNo, ShowTaxColumn, Body,
246
246
  return a;
247
247
  }
248
248
  }
249
- function calculatePartsTotals(Parts, DecimalsNumber) {
249
+ function calculatePartsTotals(ConsolidateGST, Parts, DecimalsNumber) {
250
250
  let Qty = 0;
251
251
  let CGSTAMT = 0;
252
252
  let SGSTAMT = 0;
@@ -258,12 +258,12 @@ function calculatePartsTotals(Parts, DecimalsNumber) {
258
258
  CGSTAMT = (0, math_operations_1.Add)(CGSTAMT, tr_utils_1.TrUtils.FixedTo(part.CGSTAmt, DecimalsNumber));
259
259
  SGSTAMT = (0, math_operations_1.Add)(SGSTAMT, tr_utils_1.TrUtils.FixedTo(part.SGSTAmt, DecimalsNumber));
260
260
  IGSTAMT = (0, math_operations_1.Add)(IGSTAMT, tr_utils_1.TrUtils.FixedTo(part.IGSTAmt, DecimalsNumber));
261
- Taxable = (0, math_operations_1.Add)(Taxable, tr_utils_1.TrUtils.FixedTo(part.AfterPartDisc, DecimalsNumber));
261
+ Taxable = ConsolidateGST ? (0, math_operations_1.Add)(FinalTotal, tr_utils_1.TrUtils.FixedTo(part.AfterPartTax, DecimalsNumber)) : (0, math_operations_1.Add)(Taxable, tr_utils_1.TrUtils.FixedTo(part.AfterPartDisc, DecimalsNumber));
262
262
  FinalTotal = (0, math_operations_1.Add)(FinalTotal, tr_utils_1.TrUtils.FixedTo(part.AfterPartTax, DecimalsNumber));
263
263
  });
264
264
  return { Qty, CGSTAMT, SGSTAMT, IGSTAMT, Taxable, FinalTotal };
265
265
  }
266
- function calculateLaborTotals(Labor, DecimalsNumber) {
266
+ function calculateLaborTotals(ConsolidateGST, Labor, DecimalsNumber) {
267
267
  let CGSTAMT = 0;
268
268
  let SGSTAMT = 0;
269
269
  let IGSTAMT = 0;
@@ -273,13 +273,13 @@ function calculateLaborTotals(Labor, DecimalsNumber) {
273
273
  CGSTAMT = (0, math_operations_1.Add)(CGSTAMT, tr_utils_1.TrUtils.FixedTo(operation.CGSTAmt, DecimalsNumber));
274
274
  SGSTAMT = (0, math_operations_1.Add)(SGSTAMT, tr_utils_1.TrUtils.FixedTo(operation.SGSTAmt, DecimalsNumber));
275
275
  IGSTAMT = (0, math_operations_1.Add)(IGSTAMT, tr_utils_1.TrUtils.FixedTo(operation.IGSTAmt, DecimalsNumber));
276
- Taxable = (0, math_operations_1.Add)(Taxable, tr_utils_1.TrUtils.FixedTo(operation.AfterLaborDisc, DecimalsNumber));
276
+ Taxable = ConsolidateGST ? (0, math_operations_1.Add)(FinalTotal, tr_utils_1.TrUtils.FixedTo(operation.AfterLaborTax, DecimalsNumber)) : (0, math_operations_1.Add)(Taxable, tr_utils_1.TrUtils.FixedTo(operation.AfterLaborDisc, DecimalsNumber));
277
277
  FinalTotal = (0, math_operations_1.Add)(FinalTotal, tr_utils_1.TrUtils.FixedTo(operation.AfterLaborTax, DecimalsNumber));
278
278
  });
279
279
  return { CGSTAMT, SGSTAMT, IGSTAMT, Taxable, FinalTotal };
280
280
  }
281
- function addPartsHeaderAndTotalRows(Parts, partsLabel, partsTotalLabel, ShowTaxColumn, DecimalsNumber) {
282
- const totals = calculatePartsTotals(Parts, DecimalsNumber);
281
+ function addPartsHeaderAndTotalRows(Parts, partsLabel, partsTotalLabel, ShowTaxColumn, DecimalsNumber, ConsolidateGST) {
282
+ const totals = calculatePartsTotals(ConsolidateGST, Parts, DecimalsNumber);
283
283
  let dpartadding1 = {};
284
284
  dpartadding1.SNo = '';
285
285
  dpartadding1.Desc = partsTotalLabel;
@@ -301,8 +301,8 @@ function addPartsHeaderAndTotalRows(Parts, partsLabel, partsTotalLabel, ShowTaxC
301
301
  dummypartadding1.LineTotal = '';
302
302
  Parts.unshift(dummypartadding1);
303
303
  }
304
- function addLaborHeaderAndTotalRows(Labor, laborLabel, laborTotalLabel, ShowTaxColumn, DecimalsNumber) {
305
- const totals = calculateLaborTotals(Labor, DecimalsNumber);
304
+ function addLaborHeaderAndTotalRows(Labor, laborLabel, laborTotalLabel, ShowTaxColumn, DecimalsNumber, ConsolidateGST) {
305
+ const totals = calculateLaborTotals(ConsolidateGST, Labor, DecimalsNumber);
306
306
  let dpartadding1 = {};
307
307
  dpartadding1.SNo = '';
308
308
  dpartadding1.Desc = laborTotalLabel;
@@ -450,11 +450,11 @@ function BuildTableBodyForLaborAndParts(Parts, Labor, PrintPartNo, ShowTaxColumn
450
450
  const laborLabel = 'Service';
451
451
  const laborTotalLabel = 'Service Total';
452
452
  if (Parts.length !== 0) {
453
- addPartsHeaderAndTotalRows(Parts, partsLabel, partsTotalLabel, ShowTaxColumn, DecimalsNumber);
453
+ addPartsHeaderAndTotalRows(Parts, partsLabel, partsTotalLabel, ShowTaxColumn, DecimalsNumber, ConsolidateGST);
454
454
  }
455
455
  buildTableRows(Parts, columns, partsLabel, body);
456
456
  if (Labor.length !== 0) {
457
- addLaborHeaderAndTotalRows(Labor, laborLabel, laborTotalLabel, ShowTaxColumn, DecimalsNumber);
457
+ addLaborHeaderAndTotalRows(Labor, laborLabel, laborTotalLabel, ShowTaxColumn, DecimalsNumber, ConsolidateGST);
458
458
  }
459
459
  if (Body === 2) {
460
460
  for (let i = 0; i < Labor.length; i++) {
@@ -3,8 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DiscountAndTaxCalculation = DiscountAndTaxCalculation;
4
4
  exports.calculateRecordDiscountForLine = calculateRecordDiscountForLine;
5
5
  const math_operations_1 = require("../shared/math-operations");
6
+ const util_1 = require("../shared/util");
7
+ const tax_calculator_1 = require("../tax/tax-calculator");
6
8
  const tr_utils_1 = require("../utils/tr-utils");
7
- const transaction_calculation_engine_1 = require("./transaction-calculation-engine");
8
9
  function DiscountAndTaxCalculation(recordData, isTaxable, taxcodes = []) {
9
10
  // ---------- STEP 0: FETCH TAX CODES ----------
10
11
  // const taxcodes:any = isTaxable
@@ -78,7 +79,7 @@ function DiscountAndTaxCalculation(recordData, isTaxable, taxcodes = []) {
78
79
  labor.RecDisc = totalTransactionDiscount;
79
80
  const laborafterDiscPrice = (0, math_operations_1.Subtract)(labor.Amt, labor.Disc);
80
81
  const afterDisc = (0, math_operations_1.Subtract)(laborafterDiscPrice, totalTransactionDiscount);
81
- labor.Taxes = recalculateTaxes(afterDisc, labor.TCode, taxcodes, labor.Taxes);
82
+ labor.Taxes = recalculateTaxes(labor, afterDisc, taxcodes);
82
83
  return labor;
83
84
  });
84
85
  // ---------- STEP 8: PROCESS INVENTORY ITEMS ----------
@@ -99,7 +100,7 @@ function DiscountAndTaxCalculation(recordData, isTaxable, taxcodes = []) {
99
100
  item.RecDisc = totalTransactionDiscount;
100
101
  const ItemafterDiscPrice = (0, math_operations_1.Subtract)(item.UnAmt, item.Disc);
101
102
  const afterDisc = (0, math_operations_1.Subtract)(ItemafterDiscPrice, totalTransactionDiscount);
102
- item.Taxes = recalculateTaxes(afterDisc, item.TCode, taxcodes, item.Taxes);
103
+ item.Taxes = recalculateTaxes(item, afterDisc, taxcodes);
103
104
  return item;
104
105
  });
105
106
  // ---------- STEP 9: FINALIZE ----------
@@ -107,50 +108,82 @@ function DiscountAndTaxCalculation(recordData, isTaxable, taxcodes = []) {
107
108
  recordData.Ops = [...finalServices, ...nonDiscountableOps];
108
109
  return recordData;
109
110
  }
110
- function recalculateTaxes(amount, taxCode, taxCodes = [], existingTaxes = []) {
111
+ function recalculateTaxes(line, amount, taxCodes = []) {
112
+ const existingTaxes = Array.isArray(line === null || line === void 0 ? void 0 : line.Taxes) ? line.Taxes : [];
113
+ const taxCode = getTaxCodeForLine(line, taxCodes, existingTaxes);
114
+ if (!taxCode) {
115
+ return recalculateExistingTaxes(amount, line === null || line === void 0 ? void 0 : line.TCode, existingTaxes);
116
+ }
117
+ const qty = (0, util_1.GetNumber)(line === null || line === void 0 ? void 0 : line.Qty, 1) || 1;
118
+ const calculatedTaxes = (0, tax_calculator_1.CalculateLineTax)({
119
+ Qty: qty,
120
+ UnitPrice: (0, math_operations_1.Divide)(amount, qty),
121
+ Disc: 0,
122
+ RecDisc: 0,
123
+ TaxCode: normalizeTaxCodeForCalculation(taxCode, existingTaxes),
124
+ ComponentOverrides: line === null || line === void 0 ? void 0 : line.ComponentOverrides,
125
+ });
126
+ const existingByCode = new Map(existingTaxes
127
+ .filter((tax) => tax === null || tax === void 0 ? void 0 : tax.Code)
128
+ .map((tax) => [tax.Code, tax]));
129
+ return calculatedTaxes.map((tax) => (Object.assign(Object.assign({}, (existingByCode.get(tax.Code) || {})), tax)));
130
+ }
131
+ function getTaxCodeForLine(line, taxCodes = [], existingTaxes = []) {
132
+ var _a, _b;
133
+ if ((_a = line === null || line === void 0 ? void 0 : line.TCode) === null || _a === void 0 ? void 0 : _a.Components) {
134
+ return line.TCode;
135
+ }
136
+ const taxCodeId = !tr_utils_1.TrUtils.IsNull(line === null || line === void 0 ? void 0 : line.TCode)
137
+ ? line.TCode
138
+ : (_b = existingTaxes.find((tax) => !tr_utils_1.TrUtils.IsNull(tax === null || tax === void 0 ? void 0 : tax.TaxCodeId))) === null || _b === void 0 ? void 0 : _b.TaxCodeId;
139
+ if (tr_utils_1.TrUtils.IsNull(taxCodeId) || !Array.isArray(taxCodes)) {
140
+ return null;
141
+ }
142
+ return taxCodes.find((code) => String(code === null || code === void 0 ? void 0 : code._id) === String(taxCodeId)) || null;
143
+ }
144
+ function normalizeTaxCodeForCalculation(taxCode, existingTaxes = []) {
145
+ if (Array.isArray(taxCode === null || taxCode === void 0 ? void 0 : taxCode.Components) && taxCode.Components.length > 0) {
146
+ return taxCode;
147
+ }
148
+ return Object.assign(Object.assign({}, taxCode), { Components: buildLegacyComponents(taxCode, existingTaxes) });
149
+ }
150
+ function buildLegacyComponents(taxCode, existingTaxes = []) {
151
+ const components = ["CGST", "SGST", "IGST"]
152
+ .map((code) => ({
153
+ Code: code,
154
+ Name: code,
155
+ Rate: (0, util_1.GetNumber)(taxCode === null || taxCode === void 0 ? void 0 : taxCode[code]),
156
+ Type: "Tax",
157
+ CalcMethod: "Percent",
158
+ AppliedOn: "NetAmt",
159
+ }))
160
+ .filter((component) => component.Rate > 0);
161
+ existingTaxes.forEach((tax) => {
162
+ if (!(tax === null || tax === void 0 ? void 0 : tax.Code) || components.some((component) => component.Code === tax.Code)) {
163
+ return;
164
+ }
165
+ components.push({
166
+ Code: tax.Code,
167
+ Name: tax.Name || tax.Code,
168
+ Rate: (0, util_1.GetNumber)(tax.Rate),
169
+ Type: tax.Type || (String(tax.Code).toUpperCase().includes("CESS") ? "Cess" : "Tax"),
170
+ CalcMethod: tax.CalcMethod || "Percent",
171
+ PerUnitAmt: tax.PerUnitAmt,
172
+ Unit: tax.Unit,
173
+ AppliedOn: tax.AppliedOn || "NetAmt",
174
+ });
175
+ });
176
+ return components;
177
+ }
178
+ function recalculateExistingTaxes(amount, taxCode, existingTaxes = []) {
111
179
  const normalizedTaxCode = tr_utils_1.TrUtils.IsNull(taxCode) ? null : Number(taxCode);
112
- const defaultRates = getDefaultTaxRates(taxCode, taxCodes);
113
- return (Array.isArray(existingTaxes) ? existingTaxes : []).map((tax) => {
114
- const code = tax === null || tax === void 0 ? void 0 : tax.Code;
115
- const rate = Number(tax === null || tax === void 0 ? void 0 : tax.Rate);
116
- const normalizedRate = Number.isFinite(rate) ? rate : (defaultRates[code] || 0);
117
- return Object.assign(Object.assign({}, tax), { Rate: normalizedRate, Amt: (0, math_operations_1.Multiply)(amount, (0, math_operations_1.Divide)(normalizedRate, 100)), TaxCodeId: !tr_utils_1.TrUtils.IsNull(tax === null || tax === void 0 ? void 0 : tax.TaxCodeId)
180
+ return existingTaxes.map((tax) => {
181
+ const rate = (0, util_1.GetNumber)(tax === null || tax === void 0 ? void 0 : tax.Rate);
182
+ return Object.assign(Object.assign({}, tax), { Rate: rate, Amt: (0, math_operations_1.Multiply)(amount, (0, math_operations_1.Divide)(rate, 100)), TaxCodeId: !tr_utils_1.TrUtils.IsNull(tax === null || tax === void 0 ? void 0 : tax.TaxCodeId)
118
183
  ? Number(tax.TaxCodeId)
119
184
  : normalizedTaxCode });
120
185
  });
121
186
  }
122
- function getDefaultTaxRates(taxCode, taxCodes = []) {
123
- const normalizedTaxCode = tr_utils_1.TrUtils.IsNull(taxCode) ? null : taxCode;
124
- const taxCodeRow = Array.isArray(taxCodes)
125
- ? taxCodes.find((code) => (code === null || code === void 0 ? void 0 : code._id) === normalizedTaxCode)
126
- : null;
127
- const components = Array.isArray(taxCodeRow === null || taxCodeRow === void 0 ? void 0 : taxCodeRow.Components)
128
- ? taxCodeRow.Components
129
- : [];
130
- const getComponentRate = (code) => {
131
- const match = components.find((component) => {
132
- var _a, _b;
133
- const compCode = (_b = (_a = component === null || component === void 0 ? void 0 : component.Code) !== null && _a !== void 0 ? _a : component === null || component === void 0 ? void 0 : component.Type) !== null && _b !== void 0 ? _b : component === null || component === void 0 ? void 0 : component.Name;
134
- return compCode === code;
135
- });
136
- const rate = Number(match === null || match === void 0 ? void 0 : match.Rate);
137
- return Number.isFinite(rate) ? rate : 0;
138
- };
139
- let CGST = getComponentRate('CGST');
140
- let SGST = getComponentRate('SGST');
141
- let IGST = getComponentRate('IGST');
142
- if (CGST === 0 && SGST === 0 && IGST === 0) {
143
- const gstRateSum = transaction_calculation_engine_1.TransactionCalculationEngine.getGSTRateSumByTaxCode(normalizedTaxCode, taxCodes);
144
- if (components.length === 1) {
145
- IGST = gstRateSum;
146
- }
147
- else if (components.length === 2) {
148
- CGST = gstRateSum / 2;
149
- SGST = gstRateSum / 2;
150
- }
151
- }
152
- return { CGST, SGST, IGST };
153
- }
154
187
  /**
155
188
  * Calculates the record-level discount amount (RecDisc) for a single line.
156
189
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shareneus",
3
- "version": "1.7.334",
3
+ "version": "1.7.336",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -71,7 +71,7 @@ export class InvoiceTotalsService {
71
71
 
72
72
  static CalculateLaborValues(opCodesList: any, IsIndependentTax: boolean) {
73
73
  opCodesList = this.ResetLaborValues(opCodesList, IsIndependentTax);
74
- console.log('opCodesList', opCodesList);
74
+
75
75
  // opCodesList = this.GetOperationsDiscountPrice(opCodesList);
76
76
 
77
77
  // opCodesList = this.GetOperationsAfterDiscount(opCodesList,IsIndependentTax);
@@ -243,7 +243,7 @@ export function WithOutDiscountFieldTable(Parts: any, Ops: any, PrintPartNo: any
243
243
  }
244
244
  }
245
245
 
246
- function calculatePartsTotals(Parts: any, DecimalsNumber: number) {
246
+ function calculatePartsTotals(ConsolidateGST: boolean, Parts: any, DecimalsNumber: number) {
247
247
  let Qty: number = 0;
248
248
  let CGSTAMT: number = 0;
249
249
  let SGSTAMT: number = 0;
@@ -256,14 +256,14 @@ function calculatePartsTotals(Parts: any, DecimalsNumber: number) {
256
256
  CGSTAMT = Add(CGSTAMT, TrUtils.FixedTo(part.CGSTAmt, DecimalsNumber));
257
257
  SGSTAMT = Add(SGSTAMT, TrUtils.FixedTo(part.SGSTAmt, DecimalsNumber));
258
258
  IGSTAMT = Add(IGSTAMT, TrUtils.FixedTo(part.IGSTAmt, DecimalsNumber));
259
- Taxable = Add(Taxable, TrUtils.FixedTo(part.AfterPartDisc, DecimalsNumber));
259
+ Taxable = ConsolidateGST ? Add(FinalTotal, TrUtils.FixedTo(part.AfterPartTax, DecimalsNumber)) : Add(Taxable, TrUtils.FixedTo(part.AfterPartDisc, DecimalsNumber));
260
260
  FinalTotal = Add(FinalTotal, TrUtils.FixedTo(part.AfterPartTax, DecimalsNumber));
261
261
  });
262
262
 
263
263
  return { Qty, CGSTAMT, SGSTAMT, IGSTAMT, Taxable, FinalTotal };
264
264
  }
265
265
 
266
- function calculateLaborTotals(Labor: any, DecimalsNumber: number) {
266
+ function calculateLaborTotals(ConsolidateGST: boolean, Labor: any, DecimalsNumber: number) {
267
267
  let CGSTAMT: number = 0;
268
268
  let SGSTAMT: number = 0;
269
269
  let IGSTAMT: number = 0;
@@ -274,15 +274,15 @@ function calculateLaborTotals(Labor: any, DecimalsNumber: number) {
274
274
  CGSTAMT = Add(CGSTAMT, TrUtils.FixedTo(operation.CGSTAmt, DecimalsNumber));
275
275
  SGSTAMT = Add(SGSTAMT, TrUtils.FixedTo(operation.SGSTAmt, DecimalsNumber));
276
276
  IGSTAMT = Add(IGSTAMT, TrUtils.FixedTo(operation.IGSTAmt, DecimalsNumber));
277
- Taxable = Add(Taxable, TrUtils.FixedTo(operation.AfterLaborDisc, DecimalsNumber));
277
+ Taxable = ConsolidateGST ? Add(FinalTotal, TrUtils.FixedTo(operation.AfterLaborTax, DecimalsNumber)) : Add(Taxable, TrUtils.FixedTo(operation.AfterLaborDisc, DecimalsNumber));
278
278
  FinalTotal = Add(FinalTotal, TrUtils.FixedTo(operation.AfterLaborTax, DecimalsNumber));
279
279
  });
280
280
 
281
281
  return { CGSTAMT, SGSTAMT, IGSTAMT, Taxable, FinalTotal };
282
282
  }
283
283
 
284
- function addPartsHeaderAndTotalRows(Parts: any, partsLabel: string, partsTotalLabel: string, ShowTaxColumn: any, DecimalsNumber: number) {
285
- const totals = calculatePartsTotals(Parts, DecimalsNumber);
284
+ function addPartsHeaderAndTotalRows(Parts: any, partsLabel: string, partsTotalLabel: string, ShowTaxColumn: any, DecimalsNumber: number, ConsolidateGST: boolean) {
285
+ const totals = calculatePartsTotals(ConsolidateGST, Parts, DecimalsNumber);
286
286
 
287
287
  let dpartadding1: any = {};
288
288
  dpartadding1.SNo = '';
@@ -307,8 +307,8 @@ function addPartsHeaderAndTotalRows(Parts: any, partsLabel: string, partsTotalLa
307
307
  Parts.unshift(dummypartadding1);
308
308
  }
309
309
 
310
- function addLaborHeaderAndTotalRows(Labor: any, laborLabel: string, laborTotalLabel: string, ShowTaxColumn: any, DecimalsNumber: number) {
311
- const totals = calculateLaborTotals(Labor, DecimalsNumber);
310
+ function addLaborHeaderAndTotalRows(Labor: any, laborLabel: string, laborTotalLabel: string, ShowTaxColumn: any, DecimalsNumber: number, ConsolidateGST: boolean) {
311
+ const totals = calculateLaborTotals(ConsolidateGST, Labor, DecimalsNumber);
312
312
 
313
313
  let dpartadding1: any = {};
314
314
  dpartadding1.SNo = '';
@@ -451,13 +451,13 @@ export function BuildTableBodyForLaborAndParts(Parts: any, Labor: any, PrintPart
451
451
  const laborTotalLabel = 'Service Total';
452
452
 
453
453
  if (Parts.length !== 0) {
454
- addPartsHeaderAndTotalRows(Parts, partsLabel, partsTotalLabel, ShowTaxColumn, DecimalsNumber);
454
+ addPartsHeaderAndTotalRows(Parts, partsLabel, partsTotalLabel, ShowTaxColumn, DecimalsNumber, ConsolidateGST);
455
455
  }
456
456
 
457
457
  buildTableRows(Parts, columns, partsLabel, body);
458
458
 
459
459
  if (Labor.length !== 0) {
460
- addLaborHeaderAndTotalRows(Labor, laborLabel, laborTotalLabel, ShowTaxColumn, DecimalsNumber);
460
+ addLaborHeaderAndTotalRows(Labor, laborLabel, laborTotalLabel, ShowTaxColumn, DecimalsNumber, ConsolidateGST);
461
461
  }
462
462
 
463
463
  if (Body === 2) {
@@ -1,6 +1,7 @@
1
- import { Add, Divide, Multiply, Subtract } from "../shared/math-operations";
2
- import { TrUtils } from "../utils/tr-utils";
3
- import { TransactionCalculationEngine } from "./transaction-calculation-engine";
1
+ import { Add, Divide, Multiply, Subtract } from "../shared/math-operations";
2
+ import { GetNumber } from "../shared/util";
3
+ import { CalculateLineTax } from "../tax/tax-calculator";
4
+ import { TrUtils } from "../utils/tr-utils";
4
5
 
5
6
  export function DiscountAndTaxCalculation(recordData: any, isTaxable: boolean, taxcodes: any = []) {
6
7
 
@@ -101,7 +102,7 @@ export function DiscountAndTaxCalculation(recordData: any, isTaxable: boolean, t
101
102
  const laborafterDiscPrice = Subtract(labor.Amt, labor.Disc);
102
103
  const afterDisc = Subtract(laborafterDiscPrice, totalTransactionDiscount);
103
104
 
104
- labor.Taxes = recalculateTaxes(afterDisc, labor.TCode, taxcodes, labor.Taxes);
105
+ labor.Taxes = recalculateTaxes(labor, afterDisc, taxcodes);
105
106
 
106
107
  return labor;
107
108
  });
@@ -134,7 +135,7 @@ export function DiscountAndTaxCalculation(recordData: any, isTaxable: boolean, t
134
135
  const ItemafterDiscPrice = Subtract(item.UnAmt, item.Disc);
135
136
  const afterDisc = Subtract(ItemafterDiscPrice, totalTransactionDiscount);
136
137
 
137
- item.Taxes = recalculateTaxes(afterDisc, item.TCode, taxcodes, item.Taxes);
138
+ item.Taxes = recalculateTaxes(item, afterDisc, taxcodes);
138
139
 
139
140
  return item;
140
141
  });
@@ -146,64 +147,111 @@ export function DiscountAndTaxCalculation(recordData: any, isTaxable: boolean, t
146
147
  return recordData;
147
148
  }
148
149
 
149
- function recalculateTaxes(amount: any, taxCode: any, taxCodes: any[] = [], existingTaxes: any[] = []): any[] {
150
- const normalizedTaxCode = TrUtils.IsNull(taxCode) ? null : Number(taxCode);
151
- const defaultRates = getDefaultTaxRates(taxCode, taxCodes);
152
-
153
- return (Array.isArray(existingTaxes) ? existingTaxes : []).map((tax: any) => {
154
- const code = tax?.Code;
155
- const rate = Number(tax?.Rate);
156
- const normalizedRate = Number.isFinite(rate) ? rate : (defaultRates[code] || 0);
157
-
158
- return {
159
- ...tax,
160
- Rate: normalizedRate,
161
- Amt: Multiply(amount, Divide(normalizedRate, 100)),
162
- TaxCodeId: !TrUtils.IsNull(tax?.TaxCodeId)
163
- ? Number(tax.TaxCodeId)
164
- : normalizedTaxCode
165
- };
166
- });
167
- }
168
-
169
- function getDefaultTaxRates(taxCode: any, taxCodes: any[] = []): any {
170
- const normalizedTaxCode = TrUtils.IsNull(taxCode) ? null : taxCode;
171
- const taxCodeRow = Array.isArray(taxCodes)
172
- ? taxCodes.find((code: any) => code?._id === normalizedTaxCode)
173
- : null;
174
- const components = Array.isArray(taxCodeRow?.Components)
175
- ? taxCodeRow.Components
176
- : [];
177
-
178
- const getComponentRate = (code: string): number => {
179
- const match = components.find((component: any) => {
180
- const compCode = component?.Code ?? component?.Type ?? component?.Name;
181
- return compCode === code;
182
- });
183
- const rate = Number(match?.Rate);
184
- return Number.isFinite(rate) ? rate : 0;
185
- };
186
-
187
- let CGST = getComponentRate('CGST');
188
- let SGST = getComponentRate('SGST');
189
- let IGST = getComponentRate('IGST');
190
-
191
- if (CGST === 0 && SGST === 0 && IGST === 0) {
192
- const gstRateSum =
193
- TransactionCalculationEngine.getGSTRateSumByTaxCode(
194
- normalizedTaxCode,
195
- taxCodes,
196
- );
197
- if (components.length === 1) {
198
- IGST = gstRateSum;
199
- } else if (components.length === 2) {
200
- CGST = gstRateSum / 2;
201
- SGST = gstRateSum / 2;
202
- }
203
- }
204
-
205
- return { CGST, SGST, IGST };
206
- }
150
+ function recalculateTaxes(line: any, amount: any, taxCodes: any[] = []): any[] {
151
+ const existingTaxes = Array.isArray(line?.Taxes) ? line.Taxes : [];
152
+ const taxCode = getTaxCodeForLine(line, taxCodes, existingTaxes);
153
+
154
+ if (!taxCode) {
155
+ return recalculateExistingTaxes(amount, line?.TCode, existingTaxes);
156
+ }
157
+
158
+ const qty = GetNumber(line?.Qty, 1) || 1;
159
+ const calculatedTaxes = CalculateLineTax({
160
+ Qty: qty,
161
+ UnitPrice: Divide(amount, qty),
162
+ Disc: 0,
163
+ RecDisc: 0,
164
+ TaxCode: normalizeTaxCodeForCalculation(taxCode, existingTaxes),
165
+ ComponentOverrides: line?.ComponentOverrides,
166
+ });
167
+
168
+ const existingByCode = new Map(
169
+ existingTaxes
170
+ .filter((tax: any) => tax?.Code)
171
+ .map((tax: any) => [tax.Code, tax])
172
+ );
173
+
174
+ return calculatedTaxes.map((tax: any) => ({
175
+ ...(existingByCode.get(tax.Code) || {}),
176
+ ...tax,
177
+ }));
178
+ }
179
+
180
+ function getTaxCodeForLine(line: any, taxCodes: any[] = [], existingTaxes: any[] = []): any {
181
+ if (line?.TCode?.Components) {
182
+ return line.TCode;
183
+ }
184
+
185
+ const taxCodeId = !TrUtils.IsNull(line?.TCode)
186
+ ? line.TCode
187
+ : existingTaxes.find((tax: any) => !TrUtils.IsNull(tax?.TaxCodeId))?.TaxCodeId;
188
+
189
+ if (TrUtils.IsNull(taxCodeId) || !Array.isArray(taxCodes)) {
190
+ return null;
191
+ }
192
+
193
+ return taxCodes.find((code: any) => String(code?._id) === String(taxCodeId)) || null;
194
+ }
195
+
196
+ function normalizeTaxCodeForCalculation(taxCode: any, existingTaxes: any[] = []): any {
197
+ if (Array.isArray(taxCode?.Components) && taxCode.Components.length > 0) {
198
+ return taxCode;
199
+ }
200
+
201
+ return {
202
+ ...taxCode,
203
+ Components: buildLegacyComponents(taxCode, existingTaxes),
204
+ };
205
+ }
206
+
207
+ function buildLegacyComponents(taxCode: any, existingTaxes: any[] = []): any[] {
208
+ const components: any[] = ["CGST", "SGST", "IGST"]
209
+ .map((code: string) => ({
210
+ Code: code,
211
+ Name: code,
212
+ Rate: GetNumber(taxCode?.[code]),
213
+ Type: "Tax",
214
+ CalcMethod: "Percent",
215
+ AppliedOn: "NetAmt",
216
+ }))
217
+ .filter((component: any) => component.Rate > 0);
218
+
219
+ existingTaxes.forEach((tax: any) => {
220
+ if (!tax?.Code || components.some((component: any) => component.Code === tax.Code)) {
221
+ return;
222
+ }
223
+
224
+ components.push({
225
+ Code: tax.Code,
226
+ Name: tax.Name || tax.Code,
227
+ Rate: GetNumber(tax.Rate),
228
+ Type: tax.Type || (String(tax.Code).toUpperCase().includes("CESS") ? "Cess" : "Tax"),
229
+ CalcMethod: tax.CalcMethod || "Percent",
230
+ PerUnitAmt: tax.PerUnitAmt,
231
+ Unit: tax.Unit,
232
+ AppliedOn: tax.AppliedOn || "NetAmt",
233
+ });
234
+ });
235
+
236
+ return components;
237
+ }
238
+
239
+ function recalculateExistingTaxes(amount: any, taxCode: any, existingTaxes: any[] = []): any[] {
240
+ const normalizedTaxCode = TrUtils.IsNull(taxCode) ? null : Number(taxCode);
241
+
242
+ return existingTaxes.map((tax: any) => {
243
+ const rate = GetNumber(tax?.Rate);
244
+
245
+ return {
246
+ ...tax,
247
+ Rate: rate,
248
+ Amt: Multiply(amount, Divide(rate, 100)),
249
+ TaxCodeId: !TrUtils.IsNull(tax?.TaxCodeId)
250
+ ? Number(tax.TaxCodeId)
251
+ : normalizedTaxCode
252
+ };
253
+ });
254
+ }
207
255
 
208
256
  /**
209
257
  * Calculates the record-level discount amount (RecDisc) for a single line.