shareneus 1.6.23 → 1.6.25

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.
@@ -65,9 +65,6 @@ const math_operations_1 = require("../shared/math-operations");
65
65
  function CalculateNetAmt(input) {
66
66
  var _a, _b, _c;
67
67
  const line = input;
68
- if (line.NetAmt != null) {
69
- return (0, util_1.GetNumber)(line.NetAmt);
70
- }
71
68
  const disc = (0, util_1.GetNumber)(input.Disc);
72
69
  const recDisc = (0, util_1.GetNumber)(input.RecDisc);
73
70
  if (line.UnAmt != null || line.Amt != null) {
@@ -0,0 +1,19 @@
1
+ export declare function DiscountAndTaxCalculation(recordData: any, isTaxable: boolean, taxcodes?: any): any;
2
+ /**
3
+ * Calculates the record-level discount amount (RecDisc) for a single line.
4
+ *
5
+ * @param invoiceData Full invoice data containing Items, Ops and discount fields.
6
+ * @param currentLine Normalised data for the line being edited.
7
+ * @param lineType 'item' for parts/inventory lines, 'op' for service/labor lines.
8
+ */
9
+ export declare function calculateRecordDiscountForLine(invoiceData: any, currentLine: {
10
+ _id: any;
11
+ noDisc: boolean;
12
+ /** Item-only: quantity */
13
+ qty?: number;
14
+ /** Item-only: unit price */
15
+ unpr?: number;
16
+ /** Op-only: gross amount */
17
+ amt?: number;
18
+ disc: number;
19
+ }, lineType: 'item' | 'op'): number;
@@ -0,0 +1,274 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DiscountAndTaxCalculation = DiscountAndTaxCalculation;
4
+ exports.calculateRecordDiscountForLine = calculateRecordDiscountForLine;
5
+ const math_operations_1 = require("../shared/math-operations");
6
+ const tr_utils_1 = require("../utils/tr-utils");
7
+ const transaction_calculation_engine_1 = require("./transaction-calculation-engine");
8
+ function DiscountAndTaxCalculation(recordData, isTaxable, taxcodes = []) {
9
+ // ---------- STEP 0: FETCH TAX CODES ----------
10
+ // const taxcodes:any = isTaxable
11
+ // ? await this.laborCompSrv.GetTaxCodesByPromise()
12
+ // : [];
13
+ // ---------- STEP 1: INITIALIZE DEFAULTS ----------
14
+ recordData.LDisc = recordData.LDisc || 0;
15
+ recordData.PDisc = recordData.PDisc || 0;
16
+ recordData.Disc = recordData.Disc || 0;
17
+ recordData.LPerc = recordData.LPerc || 0; // Labor-level %
18
+ recordData.PPerc = recordData.PPerc || 0; // Parts-level %
19
+ recordData.Perc = recordData.Perc || 0; // Global %
20
+ // ---------- STEP 2: CALCULATE NET AMOUNTS ----------
21
+ // Services (labor)
22
+ const servicesWithNet = (recordData.Ops || []).map((labor) => {
23
+ const gross = labor.Amt || 0;
24
+ const discount = labor.Disc || 0;
25
+ const net = (0, math_operations_1.Subtract)(gross, discount);
26
+ return Object.assign(Object.assign({}, labor), { net });
27
+ });
28
+ // Inventory items (parts)
29
+ const itemsWithNet = (recordData.Items || []).map((item) => {
30
+ const qty = item.Qty || 0;
31
+ const unitPrice = item.UnPr || 0;
32
+ const gross = item.UnAmt || (0, math_operations_1.Multiply)(qty, unitPrice);
33
+ const discount = item.Disc || 0;
34
+ const net = (0, math_operations_1.Subtract)(gross, discount);
35
+ return Object.assign(Object.assign({}, item), { net, UnAmt: gross });
36
+ });
37
+ // ---------- STEP 3: FILTER GROUPS ----------
38
+ const nonDiscountableItems = itemsWithNet.filter((val) => val.NoDisc);
39
+ const discountableItems = itemsWithNet.filter((val) => !val.NoDisc);
40
+ const nonDiscountableOps = servicesWithNet.filter((val) => val.NoDisc);
41
+ const discountableOps = servicesWithNet.filter((val) => !val.NoDisc);
42
+ const itemTotalNet = discountableItems.reduce((sum, item) => (0, math_operations_1.Add)(sum, item.net), 0);
43
+ const serviceTotalNet = discountableOps.reduce((sum, service) => (0, math_operations_1.Add)(sum, service.net), 0);
44
+ // ---------- STEP 4: HANDLE LEVEL DISCOUNTS (LPerc, PPerc) ----------
45
+ if (recordData.LPerc && Number(recordData.LPerc) > 0) {
46
+ const percValue = Number(recordData.LPerc);
47
+ recordData.LDisc = (0, math_operations_1.Multiply)(serviceTotalNet, (0, math_operations_1.Divide)(percValue, 100));
48
+ }
49
+ if (recordData.PPerc && Number(recordData.PPerc) > 0) {
50
+ const percValue = Number(recordData.PPerc);
51
+ recordData.PDisc = (0, math_operations_1.Multiply)(itemTotalNet, (0, math_operations_1.Divide)(percValue, 100));
52
+ }
53
+ const totalNetAfterLevelDiscounts = (0, math_operations_1.Subtract)((0, math_operations_1.Add)(itemTotalNet, serviceTotalNet), (0, math_operations_1.Add)(recordData.LDisc, recordData.PDisc));
54
+ // ---------- STEP 5: HANDLE GLOBAL DISCOUNT (Perc OR Disc) ----------
55
+ if (recordData.Perc && Number(recordData.Perc) > 0) {
56
+ const percValue = Number(recordData.Perc);
57
+ recordData.Disc = (0, math_operations_1.Multiply)(totalNetAfterLevelDiscounts, (0, math_operations_1.Divide)(percValue, 100));
58
+ }
59
+ else {
60
+ recordData.Disc = recordData.Disc || 0;
61
+ }
62
+ // ---------- STEP 6: TAX CALCULATION HELPER ----------
63
+ // ---------- STEP 7: PROCESS LABOR ITEMS ----------
64
+ const finalServices = discountableOps.map((labor) => {
65
+ if (labor.NoDisc)
66
+ return labor;
67
+ labor.Pr = tr_utils_1.TrUtils.SetValueToZeroIfNull(labor.Pr);
68
+ labor.Disc = tr_utils_1.TrUtils.SetValueToZeroIfNull(labor.Disc);
69
+ const laborLevelDiscShare = serviceTotalNet > 0
70
+ ? (0, math_operations_1.Multiply)((0, math_operations_1.Divide)(labor.net, serviceTotalNet), recordData.LDisc)
71
+ : 0;
72
+ const netAfterLDisc = (0, math_operations_1.Subtract)(labor.net, laborLevelDiscShare);
73
+ let globalDiscShare = 0;
74
+ if (totalNetAfterLevelDiscounts > 0) {
75
+ globalDiscShare = (0, math_operations_1.Multiply)((0, math_operations_1.Divide)(netAfterLDisc, totalNetAfterLevelDiscounts), recordData.Disc);
76
+ }
77
+ const totalTransactionDiscount = (0, math_operations_1.Add)(laborLevelDiscShare, globalDiscShare);
78
+ labor.RecDisc = totalTransactionDiscount;
79
+ const laborafterDiscPrice = (0, math_operations_1.Subtract)(labor.Amt, labor.Disc);
80
+ const afterDisc = (0, math_operations_1.Subtract)(laborafterDiscPrice, totalTransactionDiscount);
81
+ labor.Taxes = recalculateTaxes(afterDisc, labor.TCode, taxcodes, labor.Taxes);
82
+ return labor;
83
+ });
84
+ // ---------- STEP 8: PROCESS INVENTORY ITEMS ----------
85
+ const finalItems = discountableItems.map((item) => {
86
+ if (item.NoDisc)
87
+ return item;
88
+ item.UnPr = tr_utils_1.TrUtils.SetValueToZeroIfNull(item.UnPr);
89
+ item.Disc = tr_utils_1.TrUtils.SetValueToZeroIfNull(item.Disc);
90
+ const partLevelDiscShare = itemTotalNet > 0
91
+ ? (0, math_operations_1.Multiply)((0, math_operations_1.Divide)(item.net, itemTotalNet), recordData.PDisc)
92
+ : 0;
93
+ const netAfterPDisc = (0, math_operations_1.Subtract)(item.net, partLevelDiscShare);
94
+ let globalDiscShare = 0;
95
+ if (totalNetAfterLevelDiscounts > 0) {
96
+ globalDiscShare = (0, math_operations_1.Multiply)((0, math_operations_1.Divide)(netAfterPDisc, totalNetAfterLevelDiscounts), recordData.Disc);
97
+ }
98
+ const totalTransactionDiscount = (0, math_operations_1.Add)(partLevelDiscShare, globalDiscShare);
99
+ item.RecDisc = totalTransactionDiscount;
100
+ const ItemafterDiscPrice = (0, math_operations_1.Subtract)(item.UnAmt, item.Disc);
101
+ const afterDisc = (0, math_operations_1.Subtract)(ItemafterDiscPrice, totalTransactionDiscount);
102
+ item.Taxes = recalculateTaxes(afterDisc, item.TCode, taxcodes, item.Taxes);
103
+ return item;
104
+ });
105
+ // ---------- STEP 9: FINALIZE ----------
106
+ recordData.Items = [...finalItems, ...nonDiscountableItems];
107
+ recordData.Ops = [...finalServices, ...nonDiscountableOps];
108
+ return recordData;
109
+ }
110
+ function recalculateTaxes(amount, taxCode, taxCodes = [], existingTaxes = []) {
111
+ 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)
118
+ ? Number(tax.TaxCodeId)
119
+ : normalizedTaxCode });
120
+ });
121
+ }
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
+ /**
155
+ * Calculates the record-level discount amount (RecDisc) for a single line.
156
+ *
157
+ * @param invoiceData Full invoice data containing Items, Ops and discount fields.
158
+ * @param currentLine Normalised data for the line being edited.
159
+ * @param lineType 'item' for parts/inventory lines, 'op' for service/labor lines.
160
+ */
161
+ function calculateRecordDiscountForLine(invoiceData, currentLine, lineType) {
162
+ var _a, _b;
163
+ if (!!(currentLine === null || currentLine === void 0 ? void 0 : currentLine.noDisc)) {
164
+ return 0;
165
+ }
166
+ const toNum = (v) => {
167
+ const n = Number(v);
168
+ return Number.isFinite(n) ? n : 0;
169
+ };
170
+ const rawItems = Array.isArray(invoiceData === null || invoiceData === void 0 ? void 0 : invoiceData.Items) ? invoiceData.Items : [];
171
+ const rawOps = Array.isArray(invoiceData === null || invoiceData === void 0 ? void 0 : invoiceData.Ops) ? invoiceData.Ops : [];
172
+ // --- Build normalised items list, patching the current line ---
173
+ const items = rawItems.map((item) => ({
174
+ _id: item === null || item === void 0 ? void 0 : item._id,
175
+ qty: toNum(item === null || item === void 0 ? void 0 : item.Qty),
176
+ unpr: toNum(item === null || item === void 0 ? void 0 : item.UnPr),
177
+ disc: toNum(item === null || item === void 0 ? void 0 : item.Disc),
178
+ noDisc: !!(item === null || item === void 0 ? void 0 : item.NoDisc),
179
+ }));
180
+ if (lineType === 'item') {
181
+ const currentItem = {
182
+ _id: currentLine._id,
183
+ qty: toNum(currentLine.qty),
184
+ unpr: toNum(currentLine.unpr),
185
+ disc: toNum(currentLine.disc),
186
+ noDisc: currentLine.noDisc,
187
+ };
188
+ const idx = currentLine._id != null ? items.findIndex((i) => i._id === currentLine._id) : -1;
189
+ if (idx !== -1) {
190
+ items[idx] = currentItem;
191
+ }
192
+ else {
193
+ items.push(currentItem);
194
+ }
195
+ }
196
+ // --- Build normalised ops list, patching the current line ---
197
+ const ops = rawOps.map((op) => {
198
+ var _a, _b, _c, _d;
199
+ return ({
200
+ _id: op === null || op === void 0 ? void 0 : op._id,
201
+ amt: toNum((_c = (_b = (_a = op === null || op === void 0 ? void 0 : op.Amt) !== null && _a !== void 0 ? _a : op === null || op === void 0 ? void 0 : op.amount) !== null && _b !== void 0 ? _b : op === null || op === void 0 ? void 0 : op.Total) !== null && _c !== void 0 ? _c : op === null || op === void 0 ? void 0 : op.total),
202
+ disc: toNum((_d = op === null || op === void 0 ? void 0 : op.Disc) !== null && _d !== void 0 ? _d : op === null || op === void 0 ? void 0 : op.disc),
203
+ noDisc: !!(op === null || op === void 0 ? void 0 : op.NoDisc),
204
+ });
205
+ });
206
+ if (lineType === 'op') {
207
+ const currentOp = {
208
+ _id: currentLine._id,
209
+ amt: toNum(currentLine.amt),
210
+ disc: toNum(currentLine.disc),
211
+ noDisc: currentLine.noDisc,
212
+ };
213
+ const idx = currentLine._id != null ? ops.findIndex((o) => o._id === currentLine._id) : -1;
214
+ if (idx !== -1) {
215
+ ops[idx] = currentOp;
216
+ }
217
+ else {
218
+ ops.push(currentOp);
219
+ }
220
+ }
221
+ // --- Compute net amounts ---
222
+ const discountableItems = items.filter((i) => !i.noDisc);
223
+ const itemNets = discountableItems.map((item) => {
224
+ const unAmt = tr_utils_1.TrUtils.FixedTo(toNum(item.qty) * toNum(item.unpr));
225
+ const net = tr_utils_1.TrUtils.FixedTo(unAmt - toNum(item.disc));
226
+ return Object.assign(Object.assign({}, item), { net });
227
+ });
228
+ const itemTotalNet = itemNets.reduce((sum, i) => (0, math_operations_1.Add)(sum, i.net), 0);
229
+ const discountableOps = ops.filter((o) => !o.noDisc);
230
+ const opNets = discountableOps.map((op) => {
231
+ const net = tr_utils_1.TrUtils.FixedTo(toNum(op.amt) - toNum(op.disc));
232
+ return Object.assign(Object.assign({}, op), { net });
233
+ });
234
+ const serviceTotalNet = opNets.reduce((sum, o) => (0, math_operations_1.Add)(sum, o.net), 0);
235
+ // --- Level discount amounts ---
236
+ const pperc = toNum(invoiceData === null || invoiceData === void 0 ? void 0 : invoiceData.PPerc);
237
+ const lperc = toNum(invoiceData === null || invoiceData === void 0 ? void 0 : invoiceData.LPerc);
238
+ const globalPerc = toNum(invoiceData === null || invoiceData === void 0 ? void 0 : invoiceData.Perc);
239
+ const pDiscAmount = pperc > 0
240
+ ? (0, math_operations_1.Multiply)(itemTotalNet, (0, math_operations_1.Divide)(pperc, 100))
241
+ : toNum(invoiceData === null || invoiceData === void 0 ? void 0 : invoiceData.PDisc);
242
+ const lDiscAmount = lperc > 0
243
+ ? (0, math_operations_1.Multiply)(serviceTotalNet, (0, math_operations_1.Divide)(lperc, 100))
244
+ : toNum(invoiceData === null || invoiceData === void 0 ? void 0 : invoiceData.LDisc);
245
+ const totalNetAfterLevelDiscounts = (0, math_operations_1.Subtract)((0, math_operations_1.Add)(itemTotalNet, serviceTotalNet), (0, math_operations_1.Add)(pDiscAmount, lDiscAmount));
246
+ const globalDiscAmount = globalPerc > 0
247
+ ? (0, math_operations_1.Multiply)(totalNetAfterLevelDiscounts, (0, math_operations_1.Divide)(globalPerc, 100))
248
+ : toNum(invoiceData === null || invoiceData === void 0 ? void 0 : invoiceData.Disc);
249
+ // --- Compute share for the current line ---
250
+ if (lineType === 'item') {
251
+ const currentNetRow = (_a = itemNets.find((i) => i._id === currentLine._id)) !== null && _a !== void 0 ? _a : itemNets[itemNets.length - 1];
252
+ const currentNet = toNum(currentNetRow === null || currentNetRow === void 0 ? void 0 : currentNetRow.net);
253
+ const partLevelShare = itemTotalNet > 0
254
+ ? (0, math_operations_1.Multiply)((0, math_operations_1.Divide)(currentNet, itemTotalNet), pDiscAmount)
255
+ : 0;
256
+ const netAfterPDisc = (0, math_operations_1.Subtract)(currentNet, partLevelShare);
257
+ const globalShare = totalNetAfterLevelDiscounts > 0
258
+ ? (0, math_operations_1.Multiply)((0, math_operations_1.Divide)(netAfterPDisc, totalNetAfterLevelDiscounts), globalDiscAmount)
259
+ : 0;
260
+ return tr_utils_1.TrUtils.FixedTo(Math.max(0, (0, math_operations_1.Add)(partLevelShare, globalShare)));
261
+ }
262
+ else {
263
+ const currentNetRow = (_b = opNets.find((o) => o._id === currentLine._id)) !== null && _b !== void 0 ? _b : opNets[opNets.length - 1];
264
+ const currentNet = toNum(currentNetRow === null || currentNetRow === void 0 ? void 0 : currentNetRow.net);
265
+ const laborLevelShare = serviceTotalNet > 0
266
+ ? (0, math_operations_1.Multiply)((0, math_operations_1.Divide)(currentNet, serviceTotalNet), lDiscAmount)
267
+ : 0;
268
+ const netAfterLDisc = (0, math_operations_1.Subtract)(currentNet, laborLevelShare);
269
+ const globalShare = totalNetAfterLevelDiscounts > 0
270
+ ? (0, math_operations_1.Multiply)((0, math_operations_1.Divide)(netAfterLDisc, totalNetAfterLevelDiscounts), globalDiscAmount)
271
+ : 0;
272
+ return tr_utils_1.TrUtils.FixedTo(Math.max(0, (0, math_operations_1.Add)(laborLevelShare, globalShare)));
273
+ }
274
+ }
@@ -1 +1,2 @@
1
1
  export { TransactionCalculationEngine } from "./transaction-calculation-engine";
2
+ export { DiscountAndTaxCalculation, calculateRecordDiscountForLine } from "./discounts-distribution";
@@ -1,5 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TransactionCalculationEngine = void 0;
3
+ exports.calculateRecordDiscountForLine = 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; } });
8
+ Object.defineProperty(exports, "calculateRecordDiscountForLine", { enumerable: true, get: function () { return discounts_distribution_1.calculateRecordDiscountForLine; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shareneus",
3
- "version": "1.6.23",
3
+ "version": "1.6.25",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",