shareneus 1.7.335 → 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);
|
|
@@ -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(
|
|
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(
|
|
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(
|
|
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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
@@ -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
|
-
|
|
74
|
+
|
|
75
75
|
// opCodesList = this.GetOperationsDiscountPrice(opCodesList);
|
|
76
76
|
|
|
77
77
|
// opCodesList = this.GetOperationsAfterDiscount(opCodesList,IsIndependentTax);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { Add, Divide, Multiply, Subtract } from "../shared/math-operations";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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(
|
|
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(
|
|
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(
|
|
150
|
-
const
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
|
|
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.
|