shareneus 1.6.23 → 1.6.24

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.
@@ -0,0 +1 @@
1
+ export declare function DiscountAndTaxCalculation(recordData: any, isTaxable: boolean, taxcodes?: any): any;
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DiscountAndTaxCalculation = DiscountAndTaxCalculation;
4
+ const math_operations_1 = require("../shared/math-operations");
5
+ const tr_utils_1 = require("../utils/tr-utils");
6
+ const transaction_calculation_engine_1 = require("./transaction-calculation-engine");
7
+ function DiscountAndTaxCalculation(recordData, isTaxable, taxcodes = []) {
8
+ // ---------- STEP 0: FETCH TAX CODES ----------
9
+ // const taxcodes:any = isTaxable
10
+ // ? await this.laborCompSrv.GetTaxCodesByPromise()
11
+ // : [];
12
+ // ---------- STEP 1: INITIALIZE DEFAULTS ----------
13
+ recordData.LDisc = recordData.LDisc || 0;
14
+ recordData.PDisc = recordData.PDisc || 0;
15
+ recordData.Disc = recordData.Disc || 0;
16
+ recordData.LPerc = recordData.LPerc || 0; // Labor-level %
17
+ recordData.PPerc = recordData.PPerc || 0; // Parts-level %
18
+ recordData.Perc = recordData.Perc || 0; // Global %
19
+ // ---------- STEP 2: CALCULATE NET AMOUNTS ----------
20
+ // Services (labor)
21
+ const servicesWithNet = (recordData.Ops || []).map((labor) => {
22
+ const gross = labor.Amt || 0;
23
+ const discount = labor.Disc || 0;
24
+ const net = (0, math_operations_1.Subtract)(gross, discount);
25
+ return Object.assign(Object.assign({}, labor), { net });
26
+ });
27
+ // Inventory items (parts)
28
+ const itemsWithNet = (recordData.Items || []).map((item) => {
29
+ const qty = item.Qty || 0;
30
+ const unitPrice = item.UnPr || 0;
31
+ const gross = item.UnAmt || (0, math_operations_1.Multiply)(qty, unitPrice);
32
+ const discount = item.Disc || 0;
33
+ const net = (0, math_operations_1.Subtract)(gross, discount);
34
+ return Object.assign(Object.assign({}, item), { net, UnAmt: gross });
35
+ });
36
+ // ---------- STEP 3: FILTER GROUPS ----------
37
+ const nonDiscountableItems = itemsWithNet.filter((val) => val.NoDisc);
38
+ const discountableItems = itemsWithNet.filter((val) => !val.NoDisc);
39
+ const nonDiscountableOps = servicesWithNet.filter((val) => val.NoDisc);
40
+ const discountableOps = servicesWithNet.filter((val) => !val.NoDisc);
41
+ const itemTotalNet = discountableItems.reduce((sum, item) => (0, math_operations_1.Add)(sum, item.net), 0);
42
+ const serviceTotalNet = discountableOps.reduce((sum, service) => (0, math_operations_1.Add)(sum, service.net), 0);
43
+ // ---------- STEP 4: HANDLE LEVEL DISCOUNTS (LPerc, PPerc) ----------
44
+ if (recordData.LPerc && Number(recordData.LPerc) > 0) {
45
+ const percValue = Number(recordData.LPerc);
46
+ recordData.LDisc = (0, math_operations_1.Multiply)(serviceTotalNet, (0, math_operations_1.Divide)(percValue, 100));
47
+ }
48
+ if (recordData.PPerc && Number(recordData.PPerc) > 0) {
49
+ const percValue = Number(recordData.PPerc);
50
+ recordData.PDisc = (0, math_operations_1.Multiply)(itemTotalNet, (0, math_operations_1.Divide)(percValue, 100));
51
+ }
52
+ const totalNetAfterLevelDiscounts = (0, math_operations_1.Subtract)((0, math_operations_1.Add)(itemTotalNet, serviceTotalNet), (0, math_operations_1.Add)(recordData.LDisc, recordData.PDisc));
53
+ // ---------- STEP 5: HANDLE GLOBAL DISCOUNT (Perc OR Disc) ----------
54
+ if (recordData.Perc && Number(recordData.Perc) > 0) {
55
+ const percValue = Number(recordData.Perc);
56
+ recordData.Disc = (0, math_operations_1.Multiply)(totalNetAfterLevelDiscounts, (0, math_operations_1.Divide)(percValue, 100));
57
+ }
58
+ else {
59
+ recordData.Disc = recordData.Disc || 0;
60
+ }
61
+ // ---------- STEP 6: TAX CALCULATION HELPER ----------
62
+ // ---------- STEP 7: PROCESS LABOR ITEMS ----------
63
+ const finalServices = discountableOps.map((labor) => {
64
+ if (labor.NoDisc)
65
+ return labor;
66
+ labor.Pr = tr_utils_1.TrUtils.SetValueToZeroIfNull(labor.Pr);
67
+ labor.Disc = tr_utils_1.TrUtils.SetValueToZeroIfNull(labor.Disc);
68
+ const laborLevelDiscShare = serviceTotalNet > 0
69
+ ? (0, math_operations_1.Multiply)((0, math_operations_1.Divide)(labor.net, serviceTotalNet), recordData.LDisc)
70
+ : 0;
71
+ const netAfterLDisc = (0, math_operations_1.Subtract)(labor.net, laborLevelDiscShare);
72
+ let globalDiscShare = 0;
73
+ if (totalNetAfterLevelDiscounts > 0) {
74
+ globalDiscShare = (0, math_operations_1.Multiply)((0, math_operations_1.Divide)(netAfterLDisc, totalNetAfterLevelDiscounts), recordData.Disc);
75
+ }
76
+ const totalTransactionDiscount = (0, math_operations_1.Add)(laborLevelDiscShare, globalDiscShare);
77
+ labor.RecDisc = totalTransactionDiscount;
78
+ const laborafterDiscPrice = (0, math_operations_1.Subtract)(labor.Amt, labor.Disc);
79
+ const afterDisc = (0, math_operations_1.Subtract)(laborafterDiscPrice, totalTransactionDiscount);
80
+ labor.Taxes = recalculateTaxes(afterDisc, labor.TCode, taxcodes, labor.Taxes);
81
+ return labor;
82
+ });
83
+ // ---------- STEP 8: PROCESS INVENTORY ITEMS ----------
84
+ const finalItems = discountableItems.map((item) => {
85
+ if (item.NoDisc)
86
+ return item;
87
+ item.UnPr = tr_utils_1.TrUtils.SetValueToZeroIfNull(item.UnPr);
88
+ item.Disc = tr_utils_1.TrUtils.SetValueToZeroIfNull(item.Disc);
89
+ const partLevelDiscShare = itemTotalNet > 0
90
+ ? (0, math_operations_1.Multiply)((0, math_operations_1.Divide)(item.net, itemTotalNet), recordData.PDisc)
91
+ : 0;
92
+ const netAfterPDisc = (0, math_operations_1.Subtract)(item.net, partLevelDiscShare);
93
+ let globalDiscShare = 0;
94
+ if (totalNetAfterLevelDiscounts > 0) {
95
+ globalDiscShare = (0, math_operations_1.Multiply)((0, math_operations_1.Divide)(netAfterPDisc, totalNetAfterLevelDiscounts), recordData.Disc);
96
+ }
97
+ const totalTransactionDiscount = (0, math_operations_1.Add)(partLevelDiscShare, globalDiscShare);
98
+ item.RecDisc = totalTransactionDiscount;
99
+ const ItemafterDiscPrice = (0, math_operations_1.Subtract)(item.UnAmt, item.Disc);
100
+ const afterDisc = (0, math_operations_1.Subtract)(ItemafterDiscPrice, totalTransactionDiscount);
101
+ item.Taxes = recalculateTaxes(afterDisc, item.TCode, taxcodes, item.Taxes);
102
+ return item;
103
+ });
104
+ // ---------- STEP 9: FINALIZE ----------
105
+ recordData.Items = [...finalItems, ...nonDiscountableItems];
106
+ recordData.Ops = [...finalServices, ...nonDiscountableOps];
107
+ return recordData;
108
+ }
109
+ function recalculateTaxes(amount, taxCode, taxCodes = [], existingTaxes = []) {
110
+ const normalizedTaxCode = tr_utils_1.TrUtils.IsNull(taxCode) ? null : Number(taxCode);
111
+ const defaultRates = getDefaultTaxRates(taxCode, taxCodes);
112
+ return (Array.isArray(existingTaxes) ? existingTaxes : []).map((tax) => {
113
+ const code = tax === null || tax === void 0 ? void 0 : tax.Code;
114
+ const rate = Number(tax === null || tax === void 0 ? void 0 : tax.Rate);
115
+ const normalizedRate = Number.isFinite(rate) ? rate : (defaultRates[code] || 0);
116
+ 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)
117
+ ? Number(tax.TaxCodeId)
118
+ : normalizedTaxCode });
119
+ });
120
+ }
121
+ function getDefaultTaxRates(taxCode, taxCodes = []) {
122
+ const normalizedTaxCode = tr_utils_1.TrUtils.IsNull(taxCode) ? null : taxCode;
123
+ const taxCodeRow = Array.isArray(taxCodes)
124
+ ? taxCodes.find((code) => (code === null || code === void 0 ? void 0 : code._id) === normalizedTaxCode)
125
+ : null;
126
+ const components = Array.isArray(taxCodeRow === null || taxCodeRow === void 0 ? void 0 : taxCodeRow.Components)
127
+ ? taxCodeRow.Components
128
+ : [];
129
+ const getComponentRate = (code) => {
130
+ const match = components.find((component) => {
131
+ var _a, _b;
132
+ 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;
133
+ return compCode === code;
134
+ });
135
+ const rate = Number(match === null || match === void 0 ? void 0 : match.Rate);
136
+ return Number.isFinite(rate) ? rate : 0;
137
+ };
138
+ let CGST = getComponentRate('CGST');
139
+ let SGST = getComponentRate('SGST');
140
+ let IGST = getComponentRate('IGST');
141
+ if (CGST === 0 && SGST === 0 && IGST === 0) {
142
+ const gstRateSum = transaction_calculation_engine_1.TransactionCalculationEngine.getGSTRateSumByTaxCode(normalizedTaxCode, taxCodes);
143
+ if (components.length === 1) {
144
+ IGST = gstRateSum;
145
+ }
146
+ else if (components.length === 2) {
147
+ CGST = gstRateSum / 2;
148
+ SGST = gstRateSum / 2;
149
+ }
150
+ }
151
+ return { CGST, SGST, IGST };
152
+ }
@@ -1 +1,2 @@
1
1
  export { TransactionCalculationEngine } from "./transaction-calculation-engine";
2
+ export { DiscountAndTaxCalculation } from "./discounts-distribution";
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TransactionCalculationEngine = void 0;
3
+ exports.DiscountAndTaxCalculation = exports.TransactionCalculationEngine = void 0;
4
4
  var transaction_calculation_engine_1 = require("./transaction-calculation-engine");
5
5
  Object.defineProperty(exports, "TransactionCalculationEngine", { enumerable: true, get: function () { return transaction_calculation_engine_1.TransactionCalculationEngine; } });
6
+ var discounts_distribution_1 = require("./discounts-distribution");
7
+ Object.defineProperty(exports, "DiscountAndTaxCalculation", { enumerable: true, get: function () { return discounts_distribution_1.DiscountAndTaxCalculation; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shareneus",
3
- "version": "1.6.23",
3
+ "version": "1.6.24",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",