shareneus 1.6.18 → 1.6.19

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.
@@ -11,6 +11,86 @@ const util_1 = require("../shared/util");
11
11
  function safeArray(arr) {
12
12
  return Array.isArray(arr) ? arr : [];
13
13
  }
14
+ function getLineNetAmount(line) {
15
+ return (0, math_operations_1.Subtract)((0, util_1.GetNumber)(line.UnAmt || line.Amt), (0, math_operations_1.Add)((0, util_1.GetNumber)(line.Disc), (0, util_1.GetNumber)(line.RecDisc)));
16
+ }
17
+ function getTaxCodeById(taxCodes, taxCodeId) {
18
+ const normalizedTaxCodeId = Number(taxCodeId);
19
+ if (!normalizedTaxCodeId) {
20
+ return null;
21
+ }
22
+ return taxCodes.find((taxCode) => Number(taxCode._id) === normalizedTaxCodeId) || null;
23
+ }
24
+ function getTaxComponentRate(taxCode, code) {
25
+ const component = safeArray(taxCode === null || taxCode === void 0 ? void 0 : taxCode.Components).find((item) => (item === null || item === void 0 ? void 0 : item.Code) === code);
26
+ if (component) {
27
+ return (0, util_1.GetNumber)(component.Rate);
28
+ }
29
+ return (0, util_1.GetNumber)(taxCode === null || taxCode === void 0 ? void 0 : taxCode[code]);
30
+ }
31
+ function buildTaxesFromFlatFields(line, taxCode, netAmount) {
32
+ const flatTaxes = [
33
+ { Code: "CGST", Amt: (0, util_1.GetNumber)(line.CGST) },
34
+ { Code: "SGST", Amt: (0, util_1.GetNumber)(line.SGST) },
35
+ { Code: "IGST", Amt: (0, util_1.GetNumber)(line.IGST) }
36
+ ];
37
+ return flatTaxes
38
+ .filter((tax) => tax.Amt > 0)
39
+ .map((tax) => ({
40
+ Code: tax.Code,
41
+ Amt: tax.Amt,
42
+ Rate: getTaxComponentRate(taxCode, tax.Code) || (netAmount > 0 ? (0, math_operations_1.Multiply)((0, math_operations_1.Divide)(tax.Amt, netAmount), 100) : 0),
43
+ TaxCodeId: (0, util_1.GetNumber)(line.TCode) || (0, util_1.GetNumber)(taxCode === null || taxCode === void 0 ? void 0 : taxCode._id)
44
+ }));
45
+ }
46
+ function buildTaxesFromTaxCode(line, taxCode, netAmount) {
47
+ if (!taxCode) {
48
+ return [];
49
+ }
50
+ const taxComponents = safeArray(taxCode.Components);
51
+ if (taxComponents.length > 0) {
52
+ return taxComponents
53
+ .filter((component) => ["CGST", "SGST", "IGST"].includes(component === null || component === void 0 ? void 0 : component.Code))
54
+ .map((component) => ({
55
+ Code: component.Code,
56
+ Amt: (0, math_operations_1.Multiply)(netAmount, (0, math_operations_1.Divide)((0, util_1.GetNumber)(component.Rate), 100)),
57
+ Rate: (0, util_1.GetNumber)(component.Rate),
58
+ TaxCodeId: (0, util_1.GetNumber)(taxCode._id)
59
+ }))
60
+ .filter((tax) => tax.Amt > 0);
61
+ }
62
+ const legacyTaxes = [
63
+ { Code: "CGST", Rate: (0, util_1.GetNumber)(taxCode.CGST) },
64
+ { Code: "SGST", Rate: (0, util_1.GetNumber)(taxCode.SGST) },
65
+ { Code: "IGST", Rate: (0, util_1.GetNumber)(taxCode.IGST) }
66
+ ];
67
+ return legacyTaxes
68
+ .filter((tax) => tax.Rate > 0)
69
+ .map((tax) => ({
70
+ Code: tax.Code,
71
+ Amt: (0, math_operations_1.Multiply)(netAmount, (0, math_operations_1.Divide)(tax.Rate, 100)),
72
+ Rate: tax.Rate,
73
+ TaxCodeId: (0, util_1.GetNumber)(taxCode._id)
74
+ }));
75
+ }
76
+ function getResolvedTaxes(line, taxCodes = []) {
77
+ const lineTaxes = safeArray(line === null || line === void 0 ? void 0 : line.Taxes);
78
+ if (lineTaxes.length > 0) {
79
+ return lineTaxes.map((tax) => (Object.assign(Object.assign({}, tax), { Code: tax === null || tax === void 0 ? void 0 : tax.Code, Amt: (0, util_1.GetNumber)(tax === null || tax === void 0 ? void 0 : tax.Amt), Rate: (0, util_1.GetNumber)(tax === null || tax === void 0 ? void 0 : tax.Rate), TaxCodeId: (0, util_1.GetNumber)(tax === null || tax === void 0 ? void 0 : tax.TaxCodeId) }))).filter((tax) => !!tax.Code);
80
+ }
81
+ const netAmount = getLineNetAmount(line);
82
+ const taxCode = getTaxCodeById(taxCodes, line === null || line === void 0 ? void 0 : line.TCode);
83
+ const taxesFromFlatFields = buildTaxesFromFlatFields(line, taxCode, netAmount);
84
+ if (taxesFromFlatFields.length > 0) {
85
+ return taxesFromFlatFields;
86
+ }
87
+ return buildTaxesFromTaxCode(line, taxCode, netAmount);
88
+ }
89
+ function getTaxAmountByCode(taxes, code) {
90
+ return taxes
91
+ .filter((tax) => tax.Code === code)
92
+ .reduce((sum, tax) => (0, math_operations_1.Add)(sum, (0, util_1.GetNumber)(tax.Amt)), 0);
93
+ }
14
94
  function SalesTotalCalculations(items = [], ops = [], isTaxable = false, taxCodes = [], Adjust = 0) {
15
95
  // Handle null/undefined arrays
16
96
  const safeItems = safeArray(items);
@@ -60,9 +140,10 @@ function SalesTotalCalculations(items = [], ops = [], isTaxable = false, taxCode
60
140
  // Add to return subtotal (net amount after discounts)
61
141
  returnSubtotal = (0, math_operations_1.Add)(returnSubtotal, itemUnAmt);
62
142
  if (isTaxable) {
63
- const itemSGST = (0, util_1.GetNumber)(item.SGST);
64
- const itemCGST = (0, util_1.GetNumber)(item.CGST);
65
- const itemIGST = (0, util_1.GetNumber)(item.IGST);
143
+ const resolvedItemTaxes = getResolvedTaxes(item, safeTaxCodes);
144
+ const itemSGST = getTaxAmountByCode(resolvedItemTaxes, "SGST");
145
+ const itemCGST = getTaxAmountByCode(resolvedItemTaxes, "CGST");
146
+ const itemIGST = getTaxAmountByCode(resolvedItemTaxes, "IGST");
66
147
  const itemTotalTax = (0, math_operations_1.Add)(itemSGST, itemCGST, itemIGST);
67
148
  // Add to return tax
68
149
  returnTax = (0, math_operations_1.Add)(returnTax, itemTotalTax);
@@ -80,9 +161,10 @@ function SalesTotalCalculations(items = [], ops = [], isTaxable = false, taxCode
80
161
  subtotalOnItems = (0, math_operations_1.Add)(subtotalOnItems, itemUnAmt);
81
162
  // Only calculate tax if taxable
82
163
  if (isTaxable) {
83
- const itemSGST = (0, util_1.GetNumber)(item.SGST);
84
- const itemCGST = (0, util_1.GetNumber)(item.CGST);
85
- const itemIGST = (0, util_1.GetNumber)(item.IGST);
164
+ const resolvedItemTaxes = getResolvedTaxes(item, safeTaxCodes);
165
+ const itemSGST = getTaxAmountByCode(resolvedItemTaxes, "SGST");
166
+ const itemCGST = getTaxAmountByCode(resolvedItemTaxes, "CGST");
167
+ const itemIGST = getTaxAmountByCode(resolvedItemTaxes, "IGST");
86
168
  // Add individual tax amounts for items
87
169
  totalSGSTOnItems = (0, math_operations_1.Add)(totalSGSTOnItems, itemSGST);
88
170
  totalCGSTOnItems = (0, math_operations_1.Add)(totalCGSTOnItems, itemCGST);
@@ -100,9 +182,10 @@ function SalesTotalCalculations(items = [], ops = [], isTaxable = false, taxCode
100
182
  subtotalOnLabor = (0, math_operations_1.Add)(subtotalOnLabor, opAmt);
101
183
  // Only calculate tax if taxable
102
184
  if (isTaxable) {
103
- const opSGST = (0, util_1.GetNumber)(op.SGST);
104
- const opCGST = (0, util_1.GetNumber)(op.CGST);
105
- const opIGST = (0, util_1.GetNumber)(op.IGST);
185
+ const resolvedOpTaxes = getResolvedTaxes(op, safeTaxCodes);
186
+ const opSGST = getTaxAmountByCode(resolvedOpTaxes, "SGST");
187
+ const opCGST = getTaxAmountByCode(resolvedOpTaxes, "CGST");
188
+ const opIGST = getTaxAmountByCode(resolvedOpTaxes, "IGST");
106
189
  // Add individual tax amounts for operations/labor
107
190
  totalSGSTOnLabor = (0, math_operations_1.Add)(totalSGSTOnLabor, opSGST);
108
191
  totalCGSTOnLabor = (0, math_operations_1.Add)(totalCGSTOnLabor, opCGST);
@@ -180,11 +263,11 @@ function SalesTotalCalculations(items = [], ops = [], isTaxable = false, taxCode
180
263
  result.totalSGSTOnLabor = totalSGSTOnLabor;
181
264
  result.totalCGSTOnLabor = totalCGSTOnLabor;
182
265
  result.totalIGSTOnLabor = totalIGSTOnLabor;
183
- // Calculate tax summary only if taxable and taxCodes provided
266
+ // Calculate tax summary for both new Taxes[] and legacy tax-code based records
184
267
  // Filter out items where Ret is true (only include non-return items)
185
268
  const nonReturnItems = safeItems.filter((item) => item.Ret !== true);
186
- result.itemsTaxSummary = safeTaxCodes.length > 0 ? CalculateTaxSummary(nonReturnItems, safeTaxCodes) : [];
187
- result.opsTaxSummary = safeTaxCodes.length > 0 ? CalculateTaxSummary(safeOps, safeTaxCodes) : [];
269
+ result.itemsTaxSummary = CalculateTaxSummary(nonReturnItems, safeTaxCodes);
270
+ result.opsTaxSummary = CalculateTaxSummary(safeOps, safeTaxCodes);
188
271
  }
189
272
  return result;
190
273
  }
@@ -242,15 +325,16 @@ function SalesTotalCalculationsWithDecimals(items = [], ops = [], isTaxable = fa
242
325
  return roundedResult;
243
326
  }
244
327
  function CalculateTaxSummary(items, taxCodes) {
245
- // Create lookup for tax codes
246
- const taxMap = Object.fromEntries(taxCodes.map(tc => [tc._id, tc]));
247
328
  return Object.values(items.reduce((acc, item) => {
248
- const tax = taxMap[item.TCode];
249
- if (!tax)
329
+ const taxes = getResolvedTaxes(item, taxCodes);
330
+ if (taxes.length === 0)
250
331
  return acc;
251
- const percent = (((tax.IGST || 0) + (tax.CGST || 0) + (tax.SGST || 0)) + "%");
252
- // taxable value after discounts
253
- const taxable = (0, math_operations_1.Subtract)((0, util_1.GetNumber)(item.UnAmt || item.Amt), (0, math_operations_1.Add)((0, util_1.GetNumber)(item.Disc), (0, util_1.GetNumber)(item.RecDisc)));
332
+ const percent = (taxes.reduce((sum, tax) => (0, math_operations_1.Add)(sum, (0, util_1.GetNumber)(tax.Rate)), 0) + "%");
333
+ const taxable = getLineNetAmount(item);
334
+ const cgstAmount = getTaxAmountByCode(taxes, "CGST");
335
+ const sgstAmount = getTaxAmountByCode(taxes, "SGST");
336
+ const igstAmount = getTaxAmountByCode(taxes, "IGST");
337
+ const totalTaxAmount = (0, math_operations_1.Add)(cgstAmount, sgstAmount, igstAmount);
254
338
  if (!acc[percent]) {
255
339
  acc[percent] = {
256
340
  Tax: percent,
@@ -262,10 +346,10 @@ function CalculateTaxSummary(items, taxCodes) {
262
346
  };
263
347
  }
264
348
  acc[percent].TaxableAmount = (0, math_operations_1.Add)(acc[percent].TaxableAmount, taxable);
265
- acc[percent].TaxAmount = (0, math_operations_1.Add)(acc[percent].TaxAmount, (0, math_operations_1.Add)((0, math_operations_1.Divide)((0, math_operations_1.Multiply)(taxable, (0, util_1.GetNumber)(tax.CGST)), 100), (0, math_operations_1.Divide)((0, math_operations_1.Multiply)(taxable, (0, util_1.GetNumber)(tax.SGST)), 100), (0, math_operations_1.Divide)((0, math_operations_1.Multiply)(taxable, (0, util_1.GetNumber)(tax.IGST)), 100)));
266
- acc[percent].CGSTAmount = (0, math_operations_1.Add)(acc[percent].CGSTAmount, (0, math_operations_1.Divide)((0, math_operations_1.Multiply)(taxable, (0, util_1.GetNumber)(tax.CGST)), 100));
267
- acc[percent].SGSTAmount = (0, math_operations_1.Add)(acc[percent].SGSTAmount, (0, math_operations_1.Divide)((0, math_operations_1.Multiply)(taxable, (0, util_1.GetNumber)(tax.SGST)), 100));
268
- acc[percent].IGSTAmount = (0, math_operations_1.Add)(acc[percent].IGSTAmount, (0, math_operations_1.Divide)((0, math_operations_1.Multiply)(taxable, (0, util_1.GetNumber)(tax.IGST)), 100));
349
+ acc[percent].TaxAmount = (0, math_operations_1.Add)(acc[percent].TaxAmount, totalTaxAmount);
350
+ acc[percent].CGSTAmount = (0, math_operations_1.Add)(acc[percent].CGSTAmount, cgstAmount);
351
+ acc[percent].SGSTAmount = (0, math_operations_1.Add)(acc[percent].SGSTAmount, sgstAmount);
352
+ acc[percent].IGSTAmount = (0, math_operations_1.Add)(acc[percent].IGSTAmount, igstAmount);
269
353
  return acc;
270
354
  }, {}));
271
355
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shareneus",
3
- "version": "1.6.18",
3
+ "version": "1.6.19",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",