shareneus 1.6.24 → 1.6.26
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.
- package/dist/tax/index.d.ts +1 -1
- package/dist/tax/index.js +2 -1
- package/dist/tax/tax-calculator.d.ts +28 -11
- package/dist/tax/tax-calculator.js +51 -28
- package/dist/tax/tax.types.d.ts +6 -2
- package/dist/transaction-calculations/discounts-distribution.d.ts +18 -0
- package/dist/transaction-calculations/discounts-distribution.js +122 -0
- package/dist/transaction-calculations/index.d.ts +1 -1
- package/dist/transaction-calculations/index.js +2 -1
- package/package.json +1 -1
package/dist/tax/index.d.ts
CHANGED
|
@@ -6,4 +6,4 @@
|
|
|
6
6
|
*/
|
|
7
7
|
export type { TaxComponentType, CalcMethod, AppliedOn, RoundingMethod, TaxCategory, SupplyType, ResolvedSupplyType, WithholdingType, ResolverType, IRegimeComponent, IRegimeFeatures, IRounding, ITreatment, ITaxRegime, ITaxCodeComponent, ITaxCode, ITaxComponent, ITaxSummaryLine, ITaxSummary, IWithholding, IWithholdingCalcInput, ILineAmountFields, IComponentOverride, ITaxCalcLineInput, ITaxSummaryInput, IRoundingConfig, IDocumentTotalsInput, IDocumentTotals, ITaxIdLabel, ITaxIdEntry, ITaxExemption, ITaxAuthority, IExternalProvider, } from "./tax.types";
|
|
8
8
|
export type { IInclusiveResult } from "./tax-calculator";
|
|
9
|
-
export { CalculateNetAmt, CalculateLineTax, ExtractNetFromInclusive, SumTaxComponents, ComputeTaxSummary, ComputeDocumentTotals, DetermineSupplyType, FindTaxCodeByRateAndSupplyType, CalculateWithholding, RoundAmount, ValidateTaxId, ValidateHSNSACLength, ConvertFlatToTaxes, GetDefaultTaxIdLabels, GetPrintableTaxIds, } from "./tax-calculator";
|
|
9
|
+
export { CalculateGrossAmt, CalculateNetAmt, CalculateLineTax, ExtractNetFromInclusive, SumTaxComponents, ComputeTaxSummary, ComputeDocumentTotals, DetermineSupplyType, FindTaxCodeByRateAndSupplyType, CalculateWithholding, RoundAmount, ValidateTaxId, ValidateHSNSACLength, ConvertFlatToTaxes, GetDefaultTaxIdLabels, GetPrintableTaxIds, } from "./tax-calculator";
|
package/dist/tax/index.js
CHANGED
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
* Import from "shareneus/tax" in both UI and API code.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.GetPrintableTaxIds = exports.GetDefaultTaxIdLabels = exports.ConvertFlatToTaxes = exports.ValidateHSNSACLength = exports.ValidateTaxId = exports.RoundAmount = exports.CalculateWithholding = exports.FindTaxCodeByRateAndSupplyType = exports.DetermineSupplyType = exports.ComputeDocumentTotals = exports.ComputeTaxSummary = exports.SumTaxComponents = exports.ExtractNetFromInclusive = exports.CalculateLineTax = exports.CalculateNetAmt = void 0;
|
|
9
|
+
exports.GetPrintableTaxIds = exports.GetDefaultTaxIdLabels = exports.ConvertFlatToTaxes = exports.ValidateHSNSACLength = exports.ValidateTaxId = exports.RoundAmount = exports.CalculateWithholding = exports.FindTaxCodeByRateAndSupplyType = exports.DetermineSupplyType = exports.ComputeDocumentTotals = exports.ComputeTaxSummary = exports.SumTaxComponents = exports.ExtractNetFromInclusive = exports.CalculateLineTax = exports.CalculateNetAmt = exports.CalculateGrossAmt = void 0;
|
|
10
10
|
var tax_calculator_1 = require("./tax-calculator");
|
|
11
|
+
Object.defineProperty(exports, "CalculateGrossAmt", { enumerable: true, get: function () { return tax_calculator_1.CalculateGrossAmt; } });
|
|
11
12
|
Object.defineProperty(exports, "CalculateNetAmt", { enumerable: true, get: function () { return tax_calculator_1.CalculateNetAmt; } });
|
|
12
13
|
Object.defineProperty(exports, "CalculateLineTax", { enumerable: true, get: function () { return tax_calculator_1.CalculateLineTax; } });
|
|
13
14
|
Object.defineProperty(exports, "ExtractNetFromInclusive", { enumerable: true, get: function () { return tax_calculator_1.ExtractNetFromInclusive; } });
|
|
@@ -15,6 +15,18 @@
|
|
|
15
15
|
* precision issues. Never use plain JS arithmetic (* / + -) for money.
|
|
16
16
|
*/
|
|
17
17
|
import { ITaxCode, ITaxComponent, ITaxSummary, ILineAmountFields, ITaxCalcLineInput, ITaxSummaryInput, IRoundingConfig, IDocumentTotalsInput, IDocumentTotals, ResolvedSupplyType, IWithholding, IWithholdingCalcInput, ITaxIdLabel, ITaxIdEntry } from "./tax.types";
|
|
18
|
+
/**
|
|
19
|
+
* Computes the gross line amount before discounts and tax.
|
|
20
|
+
*
|
|
21
|
+
* Formula: GrossAmt = Qty × UnitPrice
|
|
22
|
+
*
|
|
23
|
+
* If `UnAmt` / `Amt` is already present on the line, that value is treated as
|
|
24
|
+
* the line's gross amount source of truth.
|
|
25
|
+
*
|
|
26
|
+
* This is the amount used for document `SubTotal` under the standard:
|
|
27
|
+
* SubTotal = sum of line values before line/document discounts and before tax.
|
|
28
|
+
*/
|
|
29
|
+
export declare function CalculateGrossAmt(input: ILineAmountFields): number;
|
|
18
30
|
/**
|
|
19
31
|
* Computes the net taxable amount from line item fields.
|
|
20
32
|
*
|
|
@@ -159,22 +171,27 @@ export declare function SumTaxComponents(taxes: ITaxComponent[] | undefined | nu
|
|
|
159
171
|
*/
|
|
160
172
|
export declare function ComputeTaxSummary(input: ITaxSummaryInput): ITaxSummary;
|
|
161
173
|
/**
|
|
162
|
-
* Computes all document-level totals in one call: SubTotal,
|
|
163
|
-
* TaxSummary, Round, and Total.
|
|
174
|
+
* Computes all document-level totals in one call: SubTotal, Discount,
|
|
175
|
+
* TaxableAmount, TaxTotal, TaxSummary, Round, and Total.
|
|
164
176
|
*
|
|
165
177
|
* This is the primary function UI/API should call at save time to produce
|
|
166
178
|
* the stored financial fields. All rounding is applied per the RoundingConfig.
|
|
167
179
|
*
|
|
168
180
|
* Flow:
|
|
169
|
-
* 1. SubTotal = sum of line
|
|
170
|
-
* 2.
|
|
171
|
-
* 3.
|
|
172
|
-
* 4.
|
|
173
|
-
* 5.
|
|
174
|
-
* 6.
|
|
181
|
+
* 1. SubTotal = sum of gross line values before discounts/tax
|
|
182
|
+
* 2. Discount = sum of line discount + prorated record discount allocations
|
|
183
|
+
* 3. TaxableAmount = sum of line NetAmts
|
|
184
|
+
* 4. TaxSummary = grouped by Code+Rate (rounded per TaxComponentTotal)
|
|
185
|
+
* 5. TaxTotal = sum of TaxSummary Amts
|
|
186
|
+
* 6. GrandTotal = TaxableAmount + TaxTotal
|
|
187
|
+
* 7. If DocTotal ON: round GrandTotal, compute Round adjustment
|
|
188
|
+
* 8. Total = GrandTotal + Round
|
|
175
189
|
*
|
|
176
190
|
* NOTE: Document-level Discount, Adjust, and Withholding are NOT handled here.
|
|
177
|
-
*
|
|
191
|
+
* If a document-level discount exists (e.g. India GST invoice discount), the
|
|
192
|
+
* caller must prorate it into line `RecDisc` values before calling this
|
|
193
|
+
* function so each line's taxable base is correct.
|
|
194
|
+
* Additional final settlement adjustments are applied after this function:
|
|
178
195
|
* FinalPayable = Total + Adjust - Withholding.Amt
|
|
179
196
|
*
|
|
180
197
|
* @param input - Line items with Qty/UnitPrice/Disc/RecDisc and Taxes[], plus RoundingConfig
|
|
@@ -195,8 +212,8 @@ export declare function ComputeTaxSummary(input: ITaxSummaryInput): ITaxSummary;
|
|
|
195
212
|
* RegimeCode: "IN_GST",
|
|
196
213
|
* });
|
|
197
214
|
* // Returns: {
|
|
198
|
-
* // SubTotal: 8000,
|
|
199
|
-
* // GrandTotal: 9440,
|
|
215
|
+
* // SubTotal: 8000, Discount: 0, TaxableAmount: 8000,
|
|
216
|
+
* // TaxTotal: 1440, Round: 0, Total: 9440, GrandTotal: 9440,
|
|
200
217
|
* // TaxSummary: [
|
|
201
218
|
* // { Code: "CGST", Rate: 9, TaxableAmt: 8000, Amt: 720 },
|
|
202
219
|
* // { Code: "SGST", Rate: 9, TaxableAmt: 8000, Amt: 720 },
|
|
@@ -19,6 +19,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
19
19
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
20
20
|
};
|
|
21
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.CalculateGrossAmt = CalculateGrossAmt;
|
|
22
23
|
exports.CalculateNetAmt = CalculateNetAmt;
|
|
23
24
|
exports.CalculateLineTax = CalculateLineTax;
|
|
24
25
|
exports.ExtractNetFromInclusive = ExtractNetFromInclusive;
|
|
@@ -40,6 +41,30 @@ const math_operations_1 = require("../shared/math-operations");
|
|
|
40
41
|
// ============================================================
|
|
41
42
|
// Net Amount Calculation
|
|
42
43
|
// ============================================================
|
|
44
|
+
/**
|
|
45
|
+
* Computes the gross line amount before discounts and tax.
|
|
46
|
+
*
|
|
47
|
+
* Formula: GrossAmt = Qty × UnitPrice
|
|
48
|
+
*
|
|
49
|
+
* If `UnAmt` / `Amt` is already present on the line, that value is treated as
|
|
50
|
+
* the line's gross amount source of truth.
|
|
51
|
+
*
|
|
52
|
+
* This is the amount used for document `SubTotal` under the standard:
|
|
53
|
+
* SubTotal = sum of line values before line/document discounts and before tax.
|
|
54
|
+
*/
|
|
55
|
+
function CalculateGrossAmt(input) {
|
|
56
|
+
var _a, _b, _c;
|
|
57
|
+
const line = input;
|
|
58
|
+
if (line.UnAmt != null || line.Amt != null) {
|
|
59
|
+
return (0, util_1.GetNumber)((_a = line.UnAmt) !== null && _a !== void 0 ? _a : line.Amt);
|
|
60
|
+
}
|
|
61
|
+
const unitPrice = (_c = (_b = line.UnitPrice) !== null && _b !== void 0 ? _b : line.UnPr) !== null && _c !== void 0 ? _c : line.Pr;
|
|
62
|
+
if (unitPrice != null) {
|
|
63
|
+
const qty = input.Qty == null ? 1 : (0, util_1.GetNumber)(input.Qty);
|
|
64
|
+
return (0, math_operations_1.Multiply)(qty, (0, util_1.GetNumber)(unitPrice));
|
|
65
|
+
}
|
|
66
|
+
return 0;
|
|
67
|
+
}
|
|
43
68
|
/**
|
|
44
69
|
* Computes the net taxable amount from line item fields.
|
|
45
70
|
*
|
|
@@ -63,22 +88,9 @@ const math_operations_1 = require("../shared/math-operations");
|
|
|
63
88
|
* // Returns: 3000
|
|
64
89
|
*/
|
|
65
90
|
function CalculateNetAmt(input) {
|
|
66
|
-
var _a, _b, _c;
|
|
67
|
-
const line = input;
|
|
68
|
-
if (line.NetAmt != null) {
|
|
69
|
-
return (0, util_1.GetNumber)(line.NetAmt);
|
|
70
|
-
}
|
|
71
91
|
const disc = (0, util_1.GetNumber)(input.Disc);
|
|
72
92
|
const recDisc = (0, util_1.GetNumber)(input.RecDisc);
|
|
73
|
-
|
|
74
|
-
return (0, math_operations_1.Subtract)((0, util_1.GetNumber)((_a = line.UnAmt) !== null && _a !== void 0 ? _a : line.Amt), disc, recDisc);
|
|
75
|
-
}
|
|
76
|
-
const unitPrice = (_c = (_b = line.UnitPrice) !== null && _b !== void 0 ? _b : line.UnPr) !== null && _c !== void 0 ? _c : line.Pr;
|
|
77
|
-
if (unitPrice != null) {
|
|
78
|
-
const qty = input.Qty == null ? 1 : (0, util_1.GetNumber)(input.Qty);
|
|
79
|
-
return (0, math_operations_1.Subtract)((0, math_operations_1.Multiply)(qty, (0, util_1.GetNumber)(unitPrice)), disc, recDisc);
|
|
80
|
-
}
|
|
81
|
-
return 0;
|
|
93
|
+
return (0, math_operations_1.Subtract)(CalculateGrossAmt(input), disc, recDisc);
|
|
82
94
|
}
|
|
83
95
|
// ============================================================
|
|
84
96
|
// Core Tax Calculation
|
|
@@ -436,22 +448,27 @@ function ComputeTaxSummary(input) {
|
|
|
436
448
|
// Document Totals — Full document computation
|
|
437
449
|
// ============================================================
|
|
438
450
|
/**
|
|
439
|
-
* Computes all document-level totals in one call: SubTotal,
|
|
440
|
-
* TaxSummary, Round, and Total.
|
|
451
|
+
* Computes all document-level totals in one call: SubTotal, Discount,
|
|
452
|
+
* TaxableAmount, TaxTotal, TaxSummary, Round, and Total.
|
|
441
453
|
*
|
|
442
454
|
* This is the primary function UI/API should call at save time to produce
|
|
443
455
|
* the stored financial fields. All rounding is applied per the RoundingConfig.
|
|
444
456
|
*
|
|
445
457
|
* Flow:
|
|
446
|
-
* 1. SubTotal = sum of line
|
|
447
|
-
* 2.
|
|
448
|
-
* 3.
|
|
449
|
-
* 4.
|
|
450
|
-
* 5.
|
|
451
|
-
* 6.
|
|
458
|
+
* 1. SubTotal = sum of gross line values before discounts/tax
|
|
459
|
+
* 2. Discount = sum of line discount + prorated record discount allocations
|
|
460
|
+
* 3. TaxableAmount = sum of line NetAmts
|
|
461
|
+
* 4. TaxSummary = grouped by Code+Rate (rounded per TaxComponentTotal)
|
|
462
|
+
* 5. TaxTotal = sum of TaxSummary Amts
|
|
463
|
+
* 6. GrandTotal = TaxableAmount + TaxTotal
|
|
464
|
+
* 7. If DocTotal ON: round GrandTotal, compute Round adjustment
|
|
465
|
+
* 8. Total = GrandTotal + Round
|
|
452
466
|
*
|
|
453
467
|
* NOTE: Document-level Discount, Adjust, and Withholding are NOT handled here.
|
|
454
|
-
*
|
|
468
|
+
* If a document-level discount exists (e.g. India GST invoice discount), the
|
|
469
|
+
* caller must prorate it into line `RecDisc` values before calling this
|
|
470
|
+
* function so each line's taxable base is correct.
|
|
471
|
+
* Additional final settlement adjustments are applied after this function:
|
|
455
472
|
* FinalPayable = Total + Adjust - Withholding.Amt
|
|
456
473
|
*
|
|
457
474
|
* @param input - Line items with Qty/UnitPrice/Disc/RecDisc and Taxes[], plus RoundingConfig
|
|
@@ -472,8 +489,8 @@ function ComputeTaxSummary(input) {
|
|
|
472
489
|
* RegimeCode: "IN_GST",
|
|
473
490
|
* });
|
|
474
491
|
* // Returns: {
|
|
475
|
-
* // SubTotal: 8000,
|
|
476
|
-
* // GrandTotal: 9440,
|
|
492
|
+
* // SubTotal: 8000, Discount: 0, TaxableAmount: 8000,
|
|
493
|
+
* // TaxTotal: 1440, Round: 0, Total: 9440, GrandTotal: 9440,
|
|
477
494
|
* // TaxSummary: [
|
|
478
495
|
* // { Code: "CGST", Rate: 9, TaxableAmt: 8000, Amt: 720 },
|
|
479
496
|
* // { Code: "SGST", Rate: 9, TaxableAmt: 8000, Amt: 720 },
|
|
@@ -482,10 +499,14 @@ function ComputeTaxSummary(input) {
|
|
|
482
499
|
*/
|
|
483
500
|
function ComputeDocumentTotals(input) {
|
|
484
501
|
const { Lines, Rounding, RegimeCode } = input;
|
|
485
|
-
// 1. SubTotal = sum of line
|
|
502
|
+
// 1. SubTotal = sum of gross line values before discounts/tax
|
|
486
503
|
let subTotal = 0;
|
|
504
|
+
let discount = 0;
|
|
505
|
+
let taxableAmount = 0;
|
|
487
506
|
for (const line of Lines) {
|
|
488
|
-
subTotal = (0, math_operations_1.Add)(subTotal,
|
|
507
|
+
subTotal = (0, math_operations_1.Add)(subTotal, CalculateGrossAmt(line));
|
|
508
|
+
discount = (0, math_operations_1.Add)(discount, (0, util_1.GetNumber)(line.Disc), (0, util_1.GetNumber)(line.RecDisc));
|
|
509
|
+
taxableAmount = (0, math_operations_1.Add)(taxableAmount, CalculateNetAmt(line));
|
|
489
510
|
}
|
|
490
511
|
// 2. TaxSummary (handles TaxComponentTotal rounding internally)
|
|
491
512
|
const taxSummary = ComputeTaxSummary({
|
|
@@ -496,7 +517,7 @@ function ComputeDocumentTotals(input) {
|
|
|
496
517
|
// 3. TaxTotal from the (potentially rounded) summary
|
|
497
518
|
const taxTotal = taxSummary.TotalTax;
|
|
498
519
|
// 4. GrandTotal before doc-level rounding
|
|
499
|
-
const grandTotal = (0, math_operations_1.Add)(
|
|
520
|
+
const grandTotal = (0, math_operations_1.Add)(taxableAmount, taxTotal);
|
|
500
521
|
// 5. DocTotal rounding
|
|
501
522
|
let roundedTotal = grandTotal;
|
|
502
523
|
let roundAdj = 0;
|
|
@@ -506,6 +527,8 @@ function ComputeDocumentTotals(input) {
|
|
|
506
527
|
}
|
|
507
528
|
return {
|
|
508
529
|
SubTotal: subTotal,
|
|
530
|
+
Discount: discount,
|
|
531
|
+
TaxableAmount: taxableAmount,
|
|
509
532
|
TaxTotal: taxTotal,
|
|
510
533
|
TaxSummary: taxSummary.Lines,
|
|
511
534
|
GrandTotal: grandTotal,
|
package/dist/tax/tax.types.d.ts
CHANGED
|
@@ -326,13 +326,17 @@ export interface IDocumentTotalsInput {
|
|
|
326
326
|
}
|
|
327
327
|
/** Result of full document totals computation */
|
|
328
328
|
export interface IDocumentTotals {
|
|
329
|
-
/** Sum of all line
|
|
329
|
+
/** Sum of all gross line values before discounts and tax */
|
|
330
330
|
SubTotal: number;
|
|
331
|
+
/** Sum of line discounts + prorated record/document discount allocations */
|
|
332
|
+
Discount: number;
|
|
333
|
+
/** Sum of all line NetAmts after discount allocation, before tax */
|
|
334
|
+
TaxableAmount: number;
|
|
331
335
|
/** Total tax amount — sum of rounded component Amts per RoundingConfig */
|
|
332
336
|
TaxTotal: number;
|
|
333
337
|
/** Rate-wise tax breakdown — grouped by Code+Rate, rounded per config */
|
|
334
338
|
TaxSummary: ITaxSummaryLine[];
|
|
335
|
-
/**
|
|
339
|
+
/** TaxableAmount + TaxTotal (before DocTotal rounding) */
|
|
336
340
|
GrandTotal: number;
|
|
337
341
|
/** Rounding adjustment: RoundedTotal - GrandTotal (0 if DocTotal is off) */
|
|
338
342
|
Round: number;
|
|
@@ -1 +1,19 @@
|
|
|
1
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;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DiscountAndTaxCalculation = DiscountAndTaxCalculation;
|
|
4
|
+
exports.calculateRecordDiscountForLine = calculateRecordDiscountForLine;
|
|
4
5
|
const math_operations_1 = require("../shared/math-operations");
|
|
5
6
|
const tr_utils_1 = require("../utils/tr-utils");
|
|
6
7
|
const transaction_calculation_engine_1 = require("./transaction-calculation-engine");
|
|
@@ -150,3 +151,124 @@ function getDefaultTaxRates(taxCode, taxCodes = []) {
|
|
|
150
151
|
}
|
|
151
152
|
return { CGST, SGST, IGST };
|
|
152
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,2 +1,2 @@
|
|
|
1
1
|
export { TransactionCalculationEngine } from "./transaction-calculation-engine";
|
|
2
|
-
export { DiscountAndTaxCalculation } from "./discounts-distribution";
|
|
2
|
+
export { DiscountAndTaxCalculation, calculateRecordDiscountForLine } from "./discounts-distribution";
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DiscountAndTaxCalculation = 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
6
|
var discounts_distribution_1 = require("./discounts-distribution");
|
|
7
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; } });
|