shareneus 1.7.4 → 1.7.6
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/README.md +1 -0
- package/dist/accounting/invoice/invoice-pdf/invoice-pdf.service.js +18 -45
- package/dist/accounting/invoice/reports/excel/analysis-excel.service.js +17 -7
- package/dist/accounting/invoice/reports/excel/category-wise-item-excel.service.js +17 -7
- package/dist/accounting/invoice/reports/excel/cust-wise-sales-details.js +17 -7
- package/dist/accounting/invoice/reports/excel/cust-wise-sales-summary-excel.service.js +17 -7
- package/dist/accounting/invoice/reports/excel/invoice-wise-excel.service.js +17 -7
- package/dist/accounting/invoice/reports/excel/item-wise-doctor-sale-excel.service.js +17 -7
- package/dist/accounting/invoice/reports/excel/items-wise-sales-excel.service.js +17 -7
- package/dist/accounting/invoice/reports/excel/manf-wise-sales-excel.service.js +17 -7
- package/dist/accounting/invoice/reports/excel/operator-wise-details.js +17 -7
- package/dist/accounting/invoice/reports/excel/operator-wise-summary.js +17 -7
- package/dist/accounting/invoice/reports/excel/sa-wise-labor-sales-excel.service.js +17 -7
- package/dist/accounting/invoice/reports/excel/sa-wise-part-sales-excel.service.js +17 -7
- package/dist/accounting/invoice/reports/excel/sale-summary-excel.service.js +17 -7
- package/dist/accounting/invoice/reports/excel/sales-by-service-summary-excel.service.js +17 -7
- package/dist/accounting/invoice/reports/excel/scheduled-drug-summary-excel.service.js +17 -7
- package/dist/accounting/invoice/reports/excel/scheduled-drugs-excel.service.js +17 -7
- package/dist/accounting/invoice/unified-invoice-pdf.service.d.ts +1 -1
- package/dist/accounting/payment-receive/reports/excel/cust-balance-excel.service.js +17 -7
- package/dist/accounting/payment-receive/reports/excel/payment-receive-excel.service.js +17 -7
- package/dist/common/reports/excel/product-without-owner-excel.service.js +17 -7
- package/dist/gst/excel/GSTR-RO-excel.service.js +17 -7
- package/dist/gst/excel/GSTR1-excel.service.js +17 -7
- package/dist/gst/excel/GSTR2-excel.service.js +17 -7
- package/dist/gst/excel/hsn-summary.js +17 -7
- package/dist/gst/excel/tally-sales-import.service.js +17 -7
- package/dist/index.d.ts +1 -1
- package/dist/index.js +6 -6
- package/dist/inventory/items/reports/excel/expiring-drugs-excel.service.js +17 -7
- package/dist/inventory/items/reports/excel/item-wise-mout-details.js +17 -7
- package/dist/inventory/items/reports/excel/item-wise-mout-summary.js +17 -7
- package/dist/inventory/items/reports/excel/spares-issue-excel.service.js +17 -7
- package/dist/inventory/items/reports/excel/stock-excel.service.js +17 -7
- package/dist/purchases/payment-made/reports/excel/payments-made-excel.service.js +17 -7
- package/dist/purchases/payment-made/reports/excel/ven-balance-excel.service.js +17 -7
- package/dist/services/reports/excel/insurance-expire-excel.service.js +17 -7
- package/dist/services/reports/excel/next-service-date-excel.service.js +17 -7
- package/dist/services/reports/excel/repair-orders-excel.service.js +17 -7
- package/dist/shared/table-section/pdf-table.header.d.ts +1 -1
- package/dist/shared/table-section/pdf-table.header.js +14 -2
- package/dist/shared/table-section/pdf-table.section.js +6 -13
- package/dist/{accounting/invoice/invoice-pdf.service.js → shared/transactions-pdf.service.js} +15 -15
- package/package.json +1 -1
- package/src/accounting/counter-sales/auto-sales-receipt-pdf.service.ts +569 -0
- package/src/accounting/counter-sales/pos-receipt-pdf.ts +577 -0
- package/src/accounting/counter-sales/sales-receipt-pdf.service.ts +628 -0
- package/src/accounting/counter-sales/sales-receipt-print.service.ts +506 -0
- package/src/accounting/credit-note/cn-print.service.ts +264 -0
- package/src/accounting/credit-note/credit-note-pdf.service.ts +602 -0
- package/src/accounting/credit-note/credit-note-totals.service.ts +424 -0
- package/src/accounting/debit-note/debit-note-pdf.service.ts +681 -0
- package/src/accounting/debit-note/debit-note-print.service.ts +276 -0
- package/src/accounting/debit-note/debit-note-totals.service.ts +361 -0
- package/src/accounting/invoice/hc-inv-pdf.service.ts +880 -0
- package/src/accounting/invoice/inv-pdf.service.ts +812 -0
- package/src/accounting/invoice/inv-print.service.ts +532 -0
- package/src/accounting/invoice/invoice-landscape-pdf.service.ts +947 -0
- package/src/accounting/invoice/invoice-letterhead-pdf.service.ts +813 -0
- package/src/accounting/invoice/invoice-pdf/invoice-pdf.service.ts +359 -0
- package/src/accounting/invoice/invoice-portrait-pdf.ts +972 -0
- package/src/accounting/invoice/invoice-print.service.ts +2906 -0
- package/src/accounting/invoice/invoice-total.service.ts +834 -0
- package/src/accounting/invoice/reports/excel/analysis-excel.service.ts +291 -0
- package/src/accounting/invoice/reports/excel/category-wise-item-excel.service.ts +267 -0
- package/src/accounting/invoice/reports/excel/cust-wise-sales-details.ts +321 -0
- package/src/accounting/invoice/reports/excel/cust-wise-sales-summary-excel.service.ts +300 -0
- package/src/accounting/invoice/reports/excel/invoice-wise-excel.service.ts +859 -0
- package/src/accounting/invoice/reports/excel/item-wise-doctor-sale-excel.service.ts +255 -0
- package/src/accounting/invoice/reports/excel/items-wise-sales-excel.service.ts +312 -0
- package/src/accounting/invoice/reports/excel/manf-wise-sales-excel.service.ts +273 -0
- package/src/accounting/invoice/reports/excel/operator-wise-details.ts +258 -0
- package/src/accounting/invoice/reports/excel/operator-wise-summary.ts +259 -0
- package/src/accounting/invoice/reports/excel/sa-wise-labor-sales-excel.service.ts +230 -0
- package/src/accounting/invoice/reports/excel/sa-wise-part-sales-excel.service.ts +231 -0
- package/src/accounting/invoice/reports/excel/sale-summary-excel.service.ts +326 -0
- package/src/accounting/invoice/reports/excel/sales-by-service-details-excel.service.ts +0 -0
- package/src/accounting/invoice/reports/excel/sales-by-service-summary-excel.service.ts +432 -0
- package/src/accounting/invoice/reports/excel/scheduled-drug-summary-excel.service.ts +373 -0
- package/src/accounting/invoice/reports/excel/scheduled-drugs-excel.service.ts +372 -0
- package/src/accounting/invoice/reports/pdf/analysis-pdf.service.ts +113 -0
- package/src/accounting/invoice/reports/pdf/category-wise-item-pdf.service.ts +107 -0
- package/src/accounting/invoice/reports/pdf/cust-wise-sales-details-pdf.service.ts +125 -0
- package/src/accounting/invoice/reports/pdf/cust-wise-sales-summary-pdf.service.ts +119 -0
- package/src/accounting/invoice/reports/pdf/item-wise-doctor-sale-pdf.service.ts +180 -0
- package/src/accounting/invoice/reports/pdf/item-wise-sales-pdf.service.ts +193 -0
- package/src/accounting/invoice/reports/pdf/manf-wise-sales-pdf.service.ts +188 -0
- package/src/accounting/invoice/reports/pdf/operator-wise-details-pdf.service.ts +118 -0
- package/src/accounting/invoice/reports/pdf/operator-wise-summary-pdf.ts +116 -0
- package/src/accounting/invoice/reports/pdf/sales-by-service-pdf.service.ts +132 -0
- package/src/accounting/invoice/reports/pdf/scheduled-drug-pdf.service.ts +191 -0
- package/src/accounting/invoice/reports/pdf/scheduled-drug-summary-pdf.service.ts +202 -0
- package/src/accounting/invoice/shared-inv-pdf.service.ts +787 -0
- package/src/accounting/invoice/unified-invoice-pdf.service.ts +937 -0
- package/src/accounting/payment-receive/payment-pdf.service.ts +410 -0
- package/src/accounting/payment-receive/payment-receipt-pdf/receipt-pdf.service.ts +470 -0
- package/src/accounting/payment-receive/receipt-print.service.ts +71 -0
- package/src/accounting/payment-receive/reports/excel/cust-balance-excel.service.ts +298 -0
- package/src/accounting/payment-receive/reports/excel/payment-receive-excel.service.ts +221 -0
- package/src/accounting/payment-receive/reports/pdf/customer-balances-pdf.service.ts +182 -0
- package/src/accounting/payment-receive/reports/pdf/payment-report-pdf.service.ts +116 -0
- package/src/aggregation/aggregation.ts +58 -0
- package/src/appointments/appointments/appointment-total.service.ts +298 -0
- package/src/appointments/consultations/consultation-fee-receipt.service.ts +407 -0
- package/src/appointments/consultations/consultation-full-pdf.service.ts +238 -0
- package/src/appointments/consultations/consultation-letterhead-pdf.service.ts +430 -0
- package/src/appointments/consultations/consultation-pdf.service.ts +417 -0
- package/src/common/reports/excel/product-without-owner-excel.service.ts +308 -0
- package/src/common/reports/pdf/product-without-owner-pdf.service.ts +146 -0
- package/src/enums/cache-enums.ts +33 -0
- package/src/enums/code-enums.ts +291 -0
- package/src/enums/country-enums.ts +9 -0
- package/src/enums/enums.ts +364 -0
- package/src/enums/industry-enums.ts +26 -0
- package/src/enums/treatment-enums.ts +9 -0
- package/src/gst/excel/GSTR-RO-excel.service.ts +926 -0
- package/src/gst/excel/GSTR1-excel.service.ts +313 -0
- package/src/gst/excel/GSTR2-excel.service.ts +314 -0
- package/src/gst/excel/hsn-summary.ts +314 -0
- package/src/gst/excel/tally-sales-import.service.ts +767 -0
- package/src/gst/pdf/hsn-summary-pdf.ts +176 -0
- package/src/index.ts +194 -0
- package/src/inventory/items/adjustment-pdf.service.ts +177 -0
- package/src/inventory/items/issue-parts-pdf.service.ts +795 -0
- package/src/inventory/items/item-bar-code-label-pdf.ts +194 -0
- package/src/inventory/items/item-detais-pdf.ts +141 -0
- package/src/inventory/items/item-price-for-pricelist.ts +368 -0
- package/src/inventory/items/reports/excel/expiring-drugs-excel.service.ts +290 -0
- package/src/inventory/items/reports/excel/item-wise-mout-details.ts +284 -0
- package/src/inventory/items/reports/excel/item-wise-mout-summary.ts +279 -0
- package/src/inventory/items/reports/excel/spares-issue-excel.service.ts +494 -0
- package/src/inventory/items/reports/excel/stock-excel.service.ts +319 -0
- package/src/inventory/items/reports/pdf/expiring-drugs-pdf.service.ts +172 -0
- package/src/inventory/items/reports/pdf/item-wise-mout-details-pdf.ts +122 -0
- package/src/inventory/items/reports/pdf/item-wise-mout-summary-pdf.ts +115 -0
- package/src/inventory/items/reports/pdf/reorder-point-pdf.service.ts +163 -0
- package/src/inventory/material-out/mout-pdf.service.ts +545 -0
- package/src/inventory/transfer-order/transfer-order-pdf.service.ts +154 -0
- package/src/purchases/bills/bill-pdf/bill-pdf.service.ts +211 -0
- package/src/purchases/bills/bill-pdf.service.ts +21 -0
- package/src/purchases/payment-made/reports/excel/payments-made-excel.service.ts +313 -0
- package/src/purchases/payment-made/reports/excel/ven-balance-excel.service.ts +307 -0
- package/src/purchases/payment-made/reports/pdf/vendor-balances-pdf.service.ts +114 -0
- package/src/purchases/purchase-order/po-totals.service.ts +343 -0
- package/src/purchases/purchase-order/purchase-order-pdf.service.ts +1016 -0
- package/src/purchases/purchase-order/purchase-order-print.service.ts +279 -0
- package/src/purchases/purchase-order/purchase-order-totals.service.ts +637 -0
- package/src/purchases/vendor-credit-note/vendor-credit-note-pdf.service.ts +1055 -0
- package/src/purchases/vendor-credit-note/vendor-credit-note-print.service.ts +145 -0
- package/src/purchases/vendor-credit-note/vendor-credit-note-totals.service.ts +399 -0
- package/src/purchases/vendor-debit-note/vendor-debit-note-pdf.service.ts +582 -0
- package/src/purchases/vendor-debit-note/vendor-debit-note-print.service.ts +295 -0
- package/src/purchases/vendor-debit-note/vendor-debit-note-totals.service.ts +377 -0
- package/src/sales/delivery-challan/dc-landscape-pdf.service.ts +922 -0
- package/src/sales/delivery-challan/dc-landscape-without-price-pdf.service.ts +869 -0
- package/src/sales/delivery-challan/dc-without-price-pdf.service.ts +505 -0
- package/src/sales/delivery-challan/delivery-challan-pdf.service.ts +461 -0
- package/src/sales/delivery-challan/delivery-challan-print.service.ts +229 -0
- package/src/sales/delivery-challan/delivery-challan-totals.ts +466 -0
- package/src/sales/sales/equipment-design-pdf.service.ts +153 -0
- package/src/sales/sales/pack-ship-pdf.service.ts +128 -0
- package/src/sales/sales/pack-ship-print.service.ts +198 -0
- package/src/sales/sales/sales-pdf.service.ts +658 -0
- package/src/sales/sales/sales-print.service.ts +376 -0
- package/src/sales/sales/sales-totals.service.ts +500 -0
- package/src/sales-receive/sales-receive-pdf.service.ts +602 -0
- package/src/sales-receive/sales-receive-print.service.ts +242 -0
- package/src/sales-receive/sales-receive-totals.service.ts +651 -0
- package/src/services/checklist-pdf.ts +151 -0
- package/src/services/checklists-pdf.ts +133 -0
- package/src/services/est.print-service.ts +1155 -0
- package/src/services/reports/excel/insurance-expire-excel.service.ts +292 -0
- package/src/services/reports/excel/next-service-date-excel.service.ts +317 -0
- package/src/services/reports/excel/repair-orders-excel.service.ts +249 -0
- package/src/services/reports/pdf/insurance-expire-pdf.service.ts +115 -0
- package/src/services/reports/pdf/next-service-date-pdf.service.ts +198 -0
- package/src/services/reports/pdf/repair-orders-pdf.service.ts +184 -0
- package/src/services/ro-pdf.service.ts +1917 -0
- package/src/services/ro-print-service.ts +881 -0
- package/src/services/ro-totals.service.ts +1314 -0
- package/src/services/separate-wo-print.service.ts +396 -0
- package/src/services/service-history-pdf.service.ts +145 -0
- package/src/services/service-price-for-pricelist.ts +649 -0
- package/src/services/technician-pdf.service.ts +234 -0
- package/src/services/technician-print.service.ts +95 -0
- package/src/shared/header-footer-section/pdf-header-footer.section.ts +519 -0
- package/src/shared/header-footer-section/pdf-shared.utils.ts +46 -0
- package/src/shared/math-operations.ts +208 -0
- package/src/shared/party-details-section/pdf-party-details.section.ts +602 -0
- package/src/shared/shared-pdf.service.ts +3042 -0
- package/src/shared/shared-print.service.ts +879 -0
- package/src/shared/table-section/pdf-table.config.ts +8 -0
- package/src/shared/table-section/pdf-table.header.ts +396 -0
- package/src/shared/table-section/pdf-table.row.ts +248 -0
- package/src/shared/table-section/pdf-table.section.ts +447 -0
- package/src/shared/totals-section/pdf-totals.section.ts +921 -0
- package/src/shared/transactions-pdf.service.ts +191 -0
- package/src/shared/util.ts +101 -0
- package/src/tasks/meetings/meeting-pdf.ts +410 -0
- package/src/tasks/tasks/task-pdf.service.ts +238 -0
- package/src/tasks/tasks/task-reports-pdf.service.ts +313 -0
- package/src/tax/index.ts +86 -0
- package/src/tax/tax-calculator.ts +1025 -0
- package/src/tax/tax.types.ts +535 -0
- package/src/transaction-calculations/discounts-distribution.ts +343 -0
- package/src/transaction-calculations/index.ts +3 -0
- package/src/transaction-calculations/total-calculation.ts +443 -0
- package/src/transaction-calculations/transaction-calculation-engine.ts +903 -0
- package/src/utils/my-date.ts +111 -0
- package/src/utils/tr-utils.ts +104 -0
- package/tsconfig.json +2 -2
- /package/dist/{accounting/invoice/invoice-pdf.service.d.ts → shared/transactions-pdf.service.d.ts} +0 -0
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import { Add, Divide, Multiply, Subtract } from "../shared/math-operations";
|
|
2
|
+
import { TrUtils } from "../utils/tr-utils";
|
|
3
|
+
import { TransactionCalculationEngine } from "./transaction-calculation-engine";
|
|
4
|
+
|
|
5
|
+
export function DiscountAndTaxCalculation(recordData: any, isTaxable: boolean, taxcodes: any = []) {
|
|
6
|
+
|
|
7
|
+
// ---------- STEP 0: FETCH TAX CODES ----------
|
|
8
|
+
// const taxcodes:any = isTaxable
|
|
9
|
+
// ? await this.laborCompSrv.GetTaxCodesByPromise()
|
|
10
|
+
// : [];
|
|
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
|
+
|
|
17
|
+
recordData.LPerc = recordData.LPerc || 0; // Labor-level %
|
|
18
|
+
recordData.PPerc = recordData.PPerc || 0; // Parts-level %
|
|
19
|
+
recordData.Perc = recordData.Perc || 0; // Global %
|
|
20
|
+
|
|
21
|
+
// ---------- STEP 2: CALCULATE NET AMOUNTS ----------
|
|
22
|
+
|
|
23
|
+
// Services (labor)
|
|
24
|
+
const servicesWithNet = (recordData.Ops || []).map((labor: any) => {
|
|
25
|
+
const gross = labor.Amt || 0;
|
|
26
|
+
const discount = labor.Disc || 0;
|
|
27
|
+
const net = Subtract(gross, discount);
|
|
28
|
+
return { ...labor, net };
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Inventory items (parts)
|
|
32
|
+
const itemsWithNet = (recordData.Items || []).map((item: any) => {
|
|
33
|
+
const qty = item.Qty || 0;
|
|
34
|
+
const unitPrice = item.UnPr || 0;
|
|
35
|
+
const gross = item.UnAmt || Multiply(qty, unitPrice);
|
|
36
|
+
const discount = item.Disc || 0;
|
|
37
|
+
const net = Subtract(gross, discount);
|
|
38
|
+
return { ...item, net, UnAmt: gross };
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// ---------- STEP 3: FILTER GROUPS ----------
|
|
42
|
+
const nonDiscountableItems = itemsWithNet.filter((val: any) => val.NoDisc);
|
|
43
|
+
const discountableItems = itemsWithNet.filter((val: any) => !val.NoDisc);
|
|
44
|
+
|
|
45
|
+
const nonDiscountableOps = servicesWithNet.filter((val: any) => val.NoDisc);
|
|
46
|
+
const discountableOps = servicesWithNet.filter((val: any) => !val.NoDisc);
|
|
47
|
+
|
|
48
|
+
const itemTotalNet = discountableItems.reduce((sum: number, item: any) => Add(sum, item.net), 0);
|
|
49
|
+
const serviceTotalNet = discountableOps.reduce((sum: number, service: any) => Add(sum, service.net), 0);
|
|
50
|
+
|
|
51
|
+
// ---------- STEP 4: HANDLE LEVEL DISCOUNTS (LPerc, PPerc) ----------
|
|
52
|
+
if (recordData.LPerc && Number(recordData.LPerc) > 0) {
|
|
53
|
+
const percValue = Number(recordData.LPerc);
|
|
54
|
+
recordData.LDisc = Multiply(serviceTotalNet, Divide(percValue, 100));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (recordData.PPerc && Number(recordData.PPerc) > 0) {
|
|
58
|
+
const percValue = Number(recordData.PPerc);
|
|
59
|
+
recordData.PDisc = Multiply(itemTotalNet, Divide(percValue, 100));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const totalNetAfterLevelDiscounts = Subtract(
|
|
63
|
+
Add(itemTotalNet, serviceTotalNet),
|
|
64
|
+
Add(recordData.LDisc, recordData.PDisc)
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
// ---------- STEP 5: HANDLE GLOBAL DISCOUNT (Perc OR Disc) ----------
|
|
68
|
+
if (recordData.Perc && Number(recordData.Perc) > 0) {
|
|
69
|
+
const percValue = Number(recordData.Perc);
|
|
70
|
+
recordData.Disc = Multiply(totalNetAfterLevelDiscounts, Divide(percValue, 100));
|
|
71
|
+
} else {
|
|
72
|
+
recordData.Disc = recordData.Disc || 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ---------- STEP 6: TAX CALCULATION HELPER ----------
|
|
76
|
+
// ---------- STEP 7: PROCESS LABOR ITEMS ----------
|
|
77
|
+
const finalServices = discountableOps.map((labor: any) => {
|
|
78
|
+
if (labor.NoDisc) return labor;
|
|
79
|
+
|
|
80
|
+
labor.Pr = TrUtils.SetValueToZeroIfNull(labor.Pr);
|
|
81
|
+
labor.Disc = TrUtils.SetValueToZeroIfNull(labor.Disc);
|
|
82
|
+
|
|
83
|
+
const laborLevelDiscShare =
|
|
84
|
+
serviceTotalNet > 0
|
|
85
|
+
? Multiply(Divide(labor.net, serviceTotalNet), recordData.LDisc)
|
|
86
|
+
: 0;
|
|
87
|
+
|
|
88
|
+
const netAfterLDisc = Subtract(labor.net, laborLevelDiscShare);
|
|
89
|
+
|
|
90
|
+
let globalDiscShare = 0;
|
|
91
|
+
if (totalNetAfterLevelDiscounts > 0) {
|
|
92
|
+
globalDiscShare = Multiply(
|
|
93
|
+
Divide(netAfterLDisc, totalNetAfterLevelDiscounts),
|
|
94
|
+
recordData.Disc
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const totalTransactionDiscount = Add(laborLevelDiscShare, globalDiscShare);
|
|
99
|
+
labor.RecDisc = totalTransactionDiscount;
|
|
100
|
+
|
|
101
|
+
const laborafterDiscPrice = Subtract(labor.Amt, labor.Disc);
|
|
102
|
+
const afterDisc = Subtract(laborafterDiscPrice, totalTransactionDiscount);
|
|
103
|
+
|
|
104
|
+
labor.Taxes = recalculateTaxes(afterDisc, labor.TCode, taxcodes, labor.Taxes);
|
|
105
|
+
|
|
106
|
+
return labor;
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// ---------- STEP 8: PROCESS INVENTORY ITEMS ----------
|
|
110
|
+
const finalItems = discountableItems.map((item: any) => {
|
|
111
|
+
if (item.NoDisc) return item;
|
|
112
|
+
|
|
113
|
+
item.UnPr = TrUtils.SetValueToZeroIfNull(item.UnPr);
|
|
114
|
+
item.Disc = TrUtils.SetValueToZeroIfNull(item.Disc);
|
|
115
|
+
|
|
116
|
+
const partLevelDiscShare =
|
|
117
|
+
itemTotalNet > 0
|
|
118
|
+
? Multiply(Divide(item.net, itemTotalNet), recordData.PDisc)
|
|
119
|
+
: 0;
|
|
120
|
+
|
|
121
|
+
const netAfterPDisc = Subtract(item.net, partLevelDiscShare);
|
|
122
|
+
|
|
123
|
+
let globalDiscShare = 0;
|
|
124
|
+
if (totalNetAfterLevelDiscounts > 0) {
|
|
125
|
+
globalDiscShare = Multiply(
|
|
126
|
+
Divide(netAfterPDisc, totalNetAfterLevelDiscounts),
|
|
127
|
+
recordData.Disc
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const totalTransactionDiscount = Add(partLevelDiscShare, globalDiscShare);
|
|
132
|
+
item.RecDisc = totalTransactionDiscount;
|
|
133
|
+
|
|
134
|
+
const ItemafterDiscPrice = Subtract(item.UnAmt, item.Disc);
|
|
135
|
+
const afterDisc = Subtract(ItemafterDiscPrice, totalTransactionDiscount);
|
|
136
|
+
|
|
137
|
+
item.Taxes = recalculateTaxes(afterDisc, item.TCode, taxcodes, item.Taxes);
|
|
138
|
+
|
|
139
|
+
return item;
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// ---------- STEP 9: FINALIZE ----------
|
|
143
|
+
recordData.Items = [...finalItems, ...nonDiscountableItems];
|
|
144
|
+
recordData.Ops = [...finalServices, ...nonDiscountableOps];
|
|
145
|
+
|
|
146
|
+
return recordData;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function recalculateTaxes(amount: any, taxCode: any, taxCodes: any[] = [], existingTaxes: any[] = []): any[] {
|
|
150
|
+
const normalizedTaxCode = TrUtils.IsNull(taxCode) ? null : Number(taxCode);
|
|
151
|
+
const defaultRates = getDefaultTaxRates(taxCode, taxCodes);
|
|
152
|
+
|
|
153
|
+
return (Array.isArray(existingTaxes) ? existingTaxes : []).map((tax: any) => {
|
|
154
|
+
const code = tax?.Code;
|
|
155
|
+
const rate = Number(tax?.Rate);
|
|
156
|
+
const normalizedRate = Number.isFinite(rate) ? rate : (defaultRates[code] || 0);
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
...tax,
|
|
160
|
+
Rate: normalizedRate,
|
|
161
|
+
Amt: Multiply(amount, Divide(normalizedRate, 100)),
|
|
162
|
+
TaxCodeId: !TrUtils.IsNull(tax?.TaxCodeId)
|
|
163
|
+
? Number(tax.TaxCodeId)
|
|
164
|
+
: normalizedTaxCode
|
|
165
|
+
};
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function getDefaultTaxRates(taxCode: any, taxCodes: any[] = []): any {
|
|
170
|
+
const normalizedTaxCode = TrUtils.IsNull(taxCode) ? null : taxCode;
|
|
171
|
+
const taxCodeRow = Array.isArray(taxCodes)
|
|
172
|
+
? taxCodes.find((code: any) => code?._id === normalizedTaxCode)
|
|
173
|
+
: null;
|
|
174
|
+
const components = Array.isArray(taxCodeRow?.Components)
|
|
175
|
+
? taxCodeRow.Components
|
|
176
|
+
: [];
|
|
177
|
+
|
|
178
|
+
const getComponentRate = (code: string): number => {
|
|
179
|
+
const match = components.find((component: any) => {
|
|
180
|
+
const compCode = component?.Code ?? component?.Type ?? component?.Name;
|
|
181
|
+
return compCode === code;
|
|
182
|
+
});
|
|
183
|
+
const rate = Number(match?.Rate);
|
|
184
|
+
return Number.isFinite(rate) ? rate : 0;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
let CGST = getComponentRate('CGST');
|
|
188
|
+
let SGST = getComponentRate('SGST');
|
|
189
|
+
let IGST = getComponentRate('IGST');
|
|
190
|
+
|
|
191
|
+
if (CGST === 0 && SGST === 0 && IGST === 0) {
|
|
192
|
+
const gstRateSum =
|
|
193
|
+
TransactionCalculationEngine.getGSTRateSumByTaxCode(
|
|
194
|
+
normalizedTaxCode,
|
|
195
|
+
taxCodes,
|
|
196
|
+
);
|
|
197
|
+
if (components.length === 1) {
|
|
198
|
+
IGST = gstRateSum;
|
|
199
|
+
} else if (components.length === 2) {
|
|
200
|
+
CGST = gstRateSum / 2;
|
|
201
|
+
SGST = gstRateSum / 2;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return { CGST, SGST, IGST };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Calculates the record-level discount amount (RecDisc) for a single line.
|
|
210
|
+
*
|
|
211
|
+
* @param invoiceData Full invoice data containing Items, Ops and discount fields.
|
|
212
|
+
* @param currentLine Normalised data for the line being edited.
|
|
213
|
+
* @param lineType 'item' for parts/inventory lines, 'op' for service/labor lines.
|
|
214
|
+
*/
|
|
215
|
+
export function calculateRecordDiscountForLine(
|
|
216
|
+
invoiceData: any,
|
|
217
|
+
currentLine: {
|
|
218
|
+
_id: any;
|
|
219
|
+
noDisc: boolean;
|
|
220
|
+
/** Item-only: quantity */
|
|
221
|
+
qty?: number;
|
|
222
|
+
/** Item-only: unit price */
|
|
223
|
+
unpr?: number;
|
|
224
|
+
/** Op-only: gross amount */
|
|
225
|
+
amt?: number;
|
|
226
|
+
disc: number;
|
|
227
|
+
},
|
|
228
|
+
lineType: 'item' | 'op',
|
|
229
|
+
): number {
|
|
230
|
+
if (!!currentLine?.noDisc) {
|
|
231
|
+
return 0;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const toNum = (v: any): number => {
|
|
235
|
+
const n = Number(v);
|
|
236
|
+
return Number.isFinite(n) ? n : 0;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
const rawItems: any[] = Array.isArray(invoiceData?.Items) ? invoiceData.Items : [];
|
|
240
|
+
const rawOps: any[] = Array.isArray(invoiceData?.Ops) ? invoiceData.Ops : [];
|
|
241
|
+
|
|
242
|
+
// --- Build normalised items list, patching the current line ---
|
|
243
|
+
const items = rawItems.map((item: any) => ({
|
|
244
|
+
_id: item?._id,
|
|
245
|
+
qty: toNum(item?.Qty),
|
|
246
|
+
unpr: toNum(item?.UnPr),
|
|
247
|
+
disc: toNum(item?.Disc),
|
|
248
|
+
noDisc: !!item?.NoDisc,
|
|
249
|
+
}));
|
|
250
|
+
|
|
251
|
+
if (lineType === 'item') {
|
|
252
|
+
const currentItem = {
|
|
253
|
+
_id: currentLine._id,
|
|
254
|
+
qty: toNum(currentLine.qty),
|
|
255
|
+
unpr: toNum(currentLine.unpr),
|
|
256
|
+
disc: toNum(currentLine.disc),
|
|
257
|
+
noDisc: currentLine.noDisc,
|
|
258
|
+
};
|
|
259
|
+
const idx = currentLine._id != null ? items.findIndex((i) => i._id === currentLine._id) : -1;
|
|
260
|
+
if (idx !== -1) { items[idx] = currentItem; } else { items.push(currentItem); }
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// --- Build normalised ops list, patching the current line ---
|
|
264
|
+
const ops = rawOps.map((op: any) => ({
|
|
265
|
+
_id: op?._id,
|
|
266
|
+
amt: toNum(op?.Amt ?? op?.amount ?? op?.Total ?? op?.total),
|
|
267
|
+
disc: toNum(op?.Disc ?? op?.disc),
|
|
268
|
+
noDisc: !!op?.NoDisc,
|
|
269
|
+
}));
|
|
270
|
+
|
|
271
|
+
if (lineType === 'op') {
|
|
272
|
+
const currentOp = {
|
|
273
|
+
_id: currentLine._id,
|
|
274
|
+
amt: toNum(currentLine.amt),
|
|
275
|
+
disc: toNum(currentLine.disc),
|
|
276
|
+
noDisc: currentLine.noDisc,
|
|
277
|
+
};
|
|
278
|
+
const idx = currentLine._id != null ? ops.findIndex((o) => o._id === currentLine._id) : -1;
|
|
279
|
+
if (idx !== -1) { ops[idx] = currentOp; } else { ops.push(currentOp); }
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// --- Compute net amounts ---
|
|
283
|
+
const discountableItems = items.filter((i) => !i.noDisc);
|
|
284
|
+
const itemNets = discountableItems.map((item) => {
|
|
285
|
+
const unAmt = TrUtils.FixedTo(toNum(item.qty) * toNum(item.unpr));
|
|
286
|
+
const net = TrUtils.FixedTo(unAmt - toNum(item.disc));
|
|
287
|
+
return { ...item, net };
|
|
288
|
+
});
|
|
289
|
+
const itemTotalNet = itemNets.reduce((sum, i) => Add(sum, i.net), 0);
|
|
290
|
+
|
|
291
|
+
const discountableOps = ops.filter((o) => !o.noDisc);
|
|
292
|
+
const opNets = discountableOps.map((op) => {
|
|
293
|
+
const net = TrUtils.FixedTo(toNum(op.amt) - toNum(op.disc));
|
|
294
|
+
return { ...op, net };
|
|
295
|
+
});
|
|
296
|
+
const serviceTotalNet = opNets.reduce((sum, o) => Add(sum, o.net), 0);
|
|
297
|
+
|
|
298
|
+
// --- Level discount amounts ---
|
|
299
|
+
const pperc = toNum(invoiceData?.PPerc);
|
|
300
|
+
const lperc = toNum(invoiceData?.LPerc);
|
|
301
|
+
const globalPerc = toNum(invoiceData?.Perc);
|
|
302
|
+
|
|
303
|
+
const pDiscAmount = pperc > 0
|
|
304
|
+
? Multiply(itemTotalNet, Divide(pperc, 100))
|
|
305
|
+
: toNum(invoiceData?.PDisc);
|
|
306
|
+
const lDiscAmount = lperc > 0
|
|
307
|
+
? Multiply(serviceTotalNet, Divide(lperc, 100))
|
|
308
|
+
: toNum(invoiceData?.LDisc);
|
|
309
|
+
|
|
310
|
+
const totalNetAfterLevelDiscounts = Subtract(
|
|
311
|
+
Add(itemTotalNet, serviceTotalNet),
|
|
312
|
+
Add(pDiscAmount, lDiscAmount),
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
const globalDiscAmount = globalPerc > 0
|
|
316
|
+
? Multiply(totalNetAfterLevelDiscounts, Divide(globalPerc, 100))
|
|
317
|
+
: toNum(invoiceData?.Disc);
|
|
318
|
+
|
|
319
|
+
// --- Compute share for the current line ---
|
|
320
|
+
if (lineType === 'item') {
|
|
321
|
+
const currentNetRow = itemNets.find((i) => i._id === currentLine._id) ?? itemNets[itemNets.length - 1];
|
|
322
|
+
const currentNet = toNum(currentNetRow?.net);
|
|
323
|
+
const partLevelShare = itemTotalNet > 0
|
|
324
|
+
? Multiply(Divide(currentNet, itemTotalNet), pDiscAmount)
|
|
325
|
+
: 0;
|
|
326
|
+
const netAfterPDisc = Subtract(currentNet, partLevelShare);
|
|
327
|
+
const globalShare = totalNetAfterLevelDiscounts > 0
|
|
328
|
+
? Multiply(Divide(netAfterPDisc, totalNetAfterLevelDiscounts), globalDiscAmount)
|
|
329
|
+
: 0;
|
|
330
|
+
return TrUtils.FixedTo(Math.max(0, Add(partLevelShare, globalShare)));
|
|
331
|
+
} else {
|
|
332
|
+
const currentNetRow = opNets.find((o) => o._id === currentLine._id) ?? opNets[opNets.length - 1];
|
|
333
|
+
const currentNet = toNum(currentNetRow?.net);
|
|
334
|
+
const laborLevelShare = serviceTotalNet > 0
|
|
335
|
+
? Multiply(Divide(currentNet, serviceTotalNet), lDiscAmount)
|
|
336
|
+
: 0;
|
|
337
|
+
const netAfterLDisc = Subtract(currentNet, laborLevelShare);
|
|
338
|
+
const globalShare = totalNetAfterLevelDiscounts > 0
|
|
339
|
+
? Multiply(Divide(netAfterLDisc, totalNetAfterLevelDiscounts), globalDiscAmount)
|
|
340
|
+
: 0;
|
|
341
|
+
return TrUtils.FixedTo(Math.max(0, Add(laborLevelShare, globalShare)));
|
|
342
|
+
}
|
|
343
|
+
}
|