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.
Files changed (212) hide show
  1. package/README.md +1 -0
  2. package/dist/accounting/invoice/invoice-pdf/invoice-pdf.service.js +18 -45
  3. package/dist/accounting/invoice/reports/excel/analysis-excel.service.js +17 -7
  4. package/dist/accounting/invoice/reports/excel/category-wise-item-excel.service.js +17 -7
  5. package/dist/accounting/invoice/reports/excel/cust-wise-sales-details.js +17 -7
  6. package/dist/accounting/invoice/reports/excel/cust-wise-sales-summary-excel.service.js +17 -7
  7. package/dist/accounting/invoice/reports/excel/invoice-wise-excel.service.js +17 -7
  8. package/dist/accounting/invoice/reports/excel/item-wise-doctor-sale-excel.service.js +17 -7
  9. package/dist/accounting/invoice/reports/excel/items-wise-sales-excel.service.js +17 -7
  10. package/dist/accounting/invoice/reports/excel/manf-wise-sales-excel.service.js +17 -7
  11. package/dist/accounting/invoice/reports/excel/operator-wise-details.js +17 -7
  12. package/dist/accounting/invoice/reports/excel/operator-wise-summary.js +17 -7
  13. package/dist/accounting/invoice/reports/excel/sa-wise-labor-sales-excel.service.js +17 -7
  14. package/dist/accounting/invoice/reports/excel/sa-wise-part-sales-excel.service.js +17 -7
  15. package/dist/accounting/invoice/reports/excel/sale-summary-excel.service.js +17 -7
  16. package/dist/accounting/invoice/reports/excel/sales-by-service-summary-excel.service.js +17 -7
  17. package/dist/accounting/invoice/reports/excel/scheduled-drug-summary-excel.service.js +17 -7
  18. package/dist/accounting/invoice/reports/excel/scheduled-drugs-excel.service.js +17 -7
  19. package/dist/accounting/invoice/unified-invoice-pdf.service.d.ts +1 -1
  20. package/dist/accounting/payment-receive/reports/excel/cust-balance-excel.service.js +17 -7
  21. package/dist/accounting/payment-receive/reports/excel/payment-receive-excel.service.js +17 -7
  22. package/dist/common/reports/excel/product-without-owner-excel.service.js +17 -7
  23. package/dist/gst/excel/GSTR-RO-excel.service.js +17 -7
  24. package/dist/gst/excel/GSTR1-excel.service.js +17 -7
  25. package/dist/gst/excel/GSTR2-excel.service.js +17 -7
  26. package/dist/gst/excel/hsn-summary.js +17 -7
  27. package/dist/gst/excel/tally-sales-import.service.js +17 -7
  28. package/dist/index.d.ts +1 -1
  29. package/dist/index.js +6 -6
  30. package/dist/inventory/items/reports/excel/expiring-drugs-excel.service.js +17 -7
  31. package/dist/inventory/items/reports/excel/item-wise-mout-details.js +17 -7
  32. package/dist/inventory/items/reports/excel/item-wise-mout-summary.js +17 -7
  33. package/dist/inventory/items/reports/excel/spares-issue-excel.service.js +17 -7
  34. package/dist/inventory/items/reports/excel/stock-excel.service.js +17 -7
  35. package/dist/purchases/payment-made/reports/excel/payments-made-excel.service.js +17 -7
  36. package/dist/purchases/payment-made/reports/excel/ven-balance-excel.service.js +17 -7
  37. package/dist/services/reports/excel/insurance-expire-excel.service.js +17 -7
  38. package/dist/services/reports/excel/next-service-date-excel.service.js +17 -7
  39. package/dist/services/reports/excel/repair-orders-excel.service.js +17 -7
  40. package/dist/shared/table-section/pdf-table.header.d.ts +1 -1
  41. package/dist/shared/table-section/pdf-table.header.js +14 -2
  42. package/dist/shared/table-section/pdf-table.section.js +6 -13
  43. package/dist/{accounting/invoice/invoice-pdf.service.js → shared/transactions-pdf.service.js} +15 -15
  44. package/package.json +1 -1
  45. package/src/accounting/counter-sales/auto-sales-receipt-pdf.service.ts +569 -0
  46. package/src/accounting/counter-sales/pos-receipt-pdf.ts +577 -0
  47. package/src/accounting/counter-sales/sales-receipt-pdf.service.ts +628 -0
  48. package/src/accounting/counter-sales/sales-receipt-print.service.ts +506 -0
  49. package/src/accounting/credit-note/cn-print.service.ts +264 -0
  50. package/src/accounting/credit-note/credit-note-pdf.service.ts +602 -0
  51. package/src/accounting/credit-note/credit-note-totals.service.ts +424 -0
  52. package/src/accounting/debit-note/debit-note-pdf.service.ts +681 -0
  53. package/src/accounting/debit-note/debit-note-print.service.ts +276 -0
  54. package/src/accounting/debit-note/debit-note-totals.service.ts +361 -0
  55. package/src/accounting/invoice/hc-inv-pdf.service.ts +880 -0
  56. package/src/accounting/invoice/inv-pdf.service.ts +812 -0
  57. package/src/accounting/invoice/inv-print.service.ts +532 -0
  58. package/src/accounting/invoice/invoice-landscape-pdf.service.ts +947 -0
  59. package/src/accounting/invoice/invoice-letterhead-pdf.service.ts +813 -0
  60. package/src/accounting/invoice/invoice-pdf/invoice-pdf.service.ts +359 -0
  61. package/src/accounting/invoice/invoice-portrait-pdf.ts +972 -0
  62. package/src/accounting/invoice/invoice-print.service.ts +2906 -0
  63. package/src/accounting/invoice/invoice-total.service.ts +834 -0
  64. package/src/accounting/invoice/reports/excel/analysis-excel.service.ts +291 -0
  65. package/src/accounting/invoice/reports/excel/category-wise-item-excel.service.ts +267 -0
  66. package/src/accounting/invoice/reports/excel/cust-wise-sales-details.ts +321 -0
  67. package/src/accounting/invoice/reports/excel/cust-wise-sales-summary-excel.service.ts +300 -0
  68. package/src/accounting/invoice/reports/excel/invoice-wise-excel.service.ts +859 -0
  69. package/src/accounting/invoice/reports/excel/item-wise-doctor-sale-excel.service.ts +255 -0
  70. package/src/accounting/invoice/reports/excel/items-wise-sales-excel.service.ts +312 -0
  71. package/src/accounting/invoice/reports/excel/manf-wise-sales-excel.service.ts +273 -0
  72. package/src/accounting/invoice/reports/excel/operator-wise-details.ts +258 -0
  73. package/src/accounting/invoice/reports/excel/operator-wise-summary.ts +259 -0
  74. package/src/accounting/invoice/reports/excel/sa-wise-labor-sales-excel.service.ts +230 -0
  75. package/src/accounting/invoice/reports/excel/sa-wise-part-sales-excel.service.ts +231 -0
  76. package/src/accounting/invoice/reports/excel/sale-summary-excel.service.ts +326 -0
  77. package/src/accounting/invoice/reports/excel/sales-by-service-details-excel.service.ts +0 -0
  78. package/src/accounting/invoice/reports/excel/sales-by-service-summary-excel.service.ts +432 -0
  79. package/src/accounting/invoice/reports/excel/scheduled-drug-summary-excel.service.ts +373 -0
  80. package/src/accounting/invoice/reports/excel/scheduled-drugs-excel.service.ts +372 -0
  81. package/src/accounting/invoice/reports/pdf/analysis-pdf.service.ts +113 -0
  82. package/src/accounting/invoice/reports/pdf/category-wise-item-pdf.service.ts +107 -0
  83. package/src/accounting/invoice/reports/pdf/cust-wise-sales-details-pdf.service.ts +125 -0
  84. package/src/accounting/invoice/reports/pdf/cust-wise-sales-summary-pdf.service.ts +119 -0
  85. package/src/accounting/invoice/reports/pdf/item-wise-doctor-sale-pdf.service.ts +180 -0
  86. package/src/accounting/invoice/reports/pdf/item-wise-sales-pdf.service.ts +193 -0
  87. package/src/accounting/invoice/reports/pdf/manf-wise-sales-pdf.service.ts +188 -0
  88. package/src/accounting/invoice/reports/pdf/operator-wise-details-pdf.service.ts +118 -0
  89. package/src/accounting/invoice/reports/pdf/operator-wise-summary-pdf.ts +116 -0
  90. package/src/accounting/invoice/reports/pdf/sales-by-service-pdf.service.ts +132 -0
  91. package/src/accounting/invoice/reports/pdf/scheduled-drug-pdf.service.ts +191 -0
  92. package/src/accounting/invoice/reports/pdf/scheduled-drug-summary-pdf.service.ts +202 -0
  93. package/src/accounting/invoice/shared-inv-pdf.service.ts +787 -0
  94. package/src/accounting/invoice/unified-invoice-pdf.service.ts +937 -0
  95. package/src/accounting/payment-receive/payment-pdf.service.ts +410 -0
  96. package/src/accounting/payment-receive/payment-receipt-pdf/receipt-pdf.service.ts +470 -0
  97. package/src/accounting/payment-receive/receipt-print.service.ts +71 -0
  98. package/src/accounting/payment-receive/reports/excel/cust-balance-excel.service.ts +298 -0
  99. package/src/accounting/payment-receive/reports/excel/payment-receive-excel.service.ts +221 -0
  100. package/src/accounting/payment-receive/reports/pdf/customer-balances-pdf.service.ts +182 -0
  101. package/src/accounting/payment-receive/reports/pdf/payment-report-pdf.service.ts +116 -0
  102. package/src/aggregation/aggregation.ts +58 -0
  103. package/src/appointments/appointments/appointment-total.service.ts +298 -0
  104. package/src/appointments/consultations/consultation-fee-receipt.service.ts +407 -0
  105. package/src/appointments/consultations/consultation-full-pdf.service.ts +238 -0
  106. package/src/appointments/consultations/consultation-letterhead-pdf.service.ts +430 -0
  107. package/src/appointments/consultations/consultation-pdf.service.ts +417 -0
  108. package/src/common/reports/excel/product-without-owner-excel.service.ts +308 -0
  109. package/src/common/reports/pdf/product-without-owner-pdf.service.ts +146 -0
  110. package/src/enums/cache-enums.ts +33 -0
  111. package/src/enums/code-enums.ts +291 -0
  112. package/src/enums/country-enums.ts +9 -0
  113. package/src/enums/enums.ts +364 -0
  114. package/src/enums/industry-enums.ts +26 -0
  115. package/src/enums/treatment-enums.ts +9 -0
  116. package/src/gst/excel/GSTR-RO-excel.service.ts +926 -0
  117. package/src/gst/excel/GSTR1-excel.service.ts +313 -0
  118. package/src/gst/excel/GSTR2-excel.service.ts +314 -0
  119. package/src/gst/excel/hsn-summary.ts +314 -0
  120. package/src/gst/excel/tally-sales-import.service.ts +767 -0
  121. package/src/gst/pdf/hsn-summary-pdf.ts +176 -0
  122. package/src/index.ts +194 -0
  123. package/src/inventory/items/adjustment-pdf.service.ts +177 -0
  124. package/src/inventory/items/issue-parts-pdf.service.ts +795 -0
  125. package/src/inventory/items/item-bar-code-label-pdf.ts +194 -0
  126. package/src/inventory/items/item-detais-pdf.ts +141 -0
  127. package/src/inventory/items/item-price-for-pricelist.ts +368 -0
  128. package/src/inventory/items/reports/excel/expiring-drugs-excel.service.ts +290 -0
  129. package/src/inventory/items/reports/excel/item-wise-mout-details.ts +284 -0
  130. package/src/inventory/items/reports/excel/item-wise-mout-summary.ts +279 -0
  131. package/src/inventory/items/reports/excel/spares-issue-excel.service.ts +494 -0
  132. package/src/inventory/items/reports/excel/stock-excel.service.ts +319 -0
  133. package/src/inventory/items/reports/pdf/expiring-drugs-pdf.service.ts +172 -0
  134. package/src/inventory/items/reports/pdf/item-wise-mout-details-pdf.ts +122 -0
  135. package/src/inventory/items/reports/pdf/item-wise-mout-summary-pdf.ts +115 -0
  136. package/src/inventory/items/reports/pdf/reorder-point-pdf.service.ts +163 -0
  137. package/src/inventory/material-out/mout-pdf.service.ts +545 -0
  138. package/src/inventory/transfer-order/transfer-order-pdf.service.ts +154 -0
  139. package/src/purchases/bills/bill-pdf/bill-pdf.service.ts +211 -0
  140. package/src/purchases/bills/bill-pdf.service.ts +21 -0
  141. package/src/purchases/payment-made/reports/excel/payments-made-excel.service.ts +313 -0
  142. package/src/purchases/payment-made/reports/excel/ven-balance-excel.service.ts +307 -0
  143. package/src/purchases/payment-made/reports/pdf/vendor-balances-pdf.service.ts +114 -0
  144. package/src/purchases/purchase-order/po-totals.service.ts +343 -0
  145. package/src/purchases/purchase-order/purchase-order-pdf.service.ts +1016 -0
  146. package/src/purchases/purchase-order/purchase-order-print.service.ts +279 -0
  147. package/src/purchases/purchase-order/purchase-order-totals.service.ts +637 -0
  148. package/src/purchases/vendor-credit-note/vendor-credit-note-pdf.service.ts +1055 -0
  149. package/src/purchases/vendor-credit-note/vendor-credit-note-print.service.ts +145 -0
  150. package/src/purchases/vendor-credit-note/vendor-credit-note-totals.service.ts +399 -0
  151. package/src/purchases/vendor-debit-note/vendor-debit-note-pdf.service.ts +582 -0
  152. package/src/purchases/vendor-debit-note/vendor-debit-note-print.service.ts +295 -0
  153. package/src/purchases/vendor-debit-note/vendor-debit-note-totals.service.ts +377 -0
  154. package/src/sales/delivery-challan/dc-landscape-pdf.service.ts +922 -0
  155. package/src/sales/delivery-challan/dc-landscape-without-price-pdf.service.ts +869 -0
  156. package/src/sales/delivery-challan/dc-without-price-pdf.service.ts +505 -0
  157. package/src/sales/delivery-challan/delivery-challan-pdf.service.ts +461 -0
  158. package/src/sales/delivery-challan/delivery-challan-print.service.ts +229 -0
  159. package/src/sales/delivery-challan/delivery-challan-totals.ts +466 -0
  160. package/src/sales/sales/equipment-design-pdf.service.ts +153 -0
  161. package/src/sales/sales/pack-ship-pdf.service.ts +128 -0
  162. package/src/sales/sales/pack-ship-print.service.ts +198 -0
  163. package/src/sales/sales/sales-pdf.service.ts +658 -0
  164. package/src/sales/sales/sales-print.service.ts +376 -0
  165. package/src/sales/sales/sales-totals.service.ts +500 -0
  166. package/src/sales-receive/sales-receive-pdf.service.ts +602 -0
  167. package/src/sales-receive/sales-receive-print.service.ts +242 -0
  168. package/src/sales-receive/sales-receive-totals.service.ts +651 -0
  169. package/src/services/checklist-pdf.ts +151 -0
  170. package/src/services/checklists-pdf.ts +133 -0
  171. package/src/services/est.print-service.ts +1155 -0
  172. package/src/services/reports/excel/insurance-expire-excel.service.ts +292 -0
  173. package/src/services/reports/excel/next-service-date-excel.service.ts +317 -0
  174. package/src/services/reports/excel/repair-orders-excel.service.ts +249 -0
  175. package/src/services/reports/pdf/insurance-expire-pdf.service.ts +115 -0
  176. package/src/services/reports/pdf/next-service-date-pdf.service.ts +198 -0
  177. package/src/services/reports/pdf/repair-orders-pdf.service.ts +184 -0
  178. package/src/services/ro-pdf.service.ts +1917 -0
  179. package/src/services/ro-print-service.ts +881 -0
  180. package/src/services/ro-totals.service.ts +1314 -0
  181. package/src/services/separate-wo-print.service.ts +396 -0
  182. package/src/services/service-history-pdf.service.ts +145 -0
  183. package/src/services/service-price-for-pricelist.ts +649 -0
  184. package/src/services/technician-pdf.service.ts +234 -0
  185. package/src/services/technician-print.service.ts +95 -0
  186. package/src/shared/header-footer-section/pdf-header-footer.section.ts +519 -0
  187. package/src/shared/header-footer-section/pdf-shared.utils.ts +46 -0
  188. package/src/shared/math-operations.ts +208 -0
  189. package/src/shared/party-details-section/pdf-party-details.section.ts +602 -0
  190. package/src/shared/shared-pdf.service.ts +3042 -0
  191. package/src/shared/shared-print.service.ts +879 -0
  192. package/src/shared/table-section/pdf-table.config.ts +8 -0
  193. package/src/shared/table-section/pdf-table.header.ts +396 -0
  194. package/src/shared/table-section/pdf-table.row.ts +248 -0
  195. package/src/shared/table-section/pdf-table.section.ts +447 -0
  196. package/src/shared/totals-section/pdf-totals.section.ts +921 -0
  197. package/src/shared/transactions-pdf.service.ts +191 -0
  198. package/src/shared/util.ts +101 -0
  199. package/src/tasks/meetings/meeting-pdf.ts +410 -0
  200. package/src/tasks/tasks/task-pdf.service.ts +238 -0
  201. package/src/tasks/tasks/task-reports-pdf.service.ts +313 -0
  202. package/src/tax/index.ts +86 -0
  203. package/src/tax/tax-calculator.ts +1025 -0
  204. package/src/tax/tax.types.ts +535 -0
  205. package/src/transaction-calculations/discounts-distribution.ts +343 -0
  206. package/src/transaction-calculations/index.ts +3 -0
  207. package/src/transaction-calculations/total-calculation.ts +443 -0
  208. package/src/transaction-calculations/transaction-calculation-engine.ts +903 -0
  209. package/src/utils/my-date.ts +111 -0
  210. package/src/utils/tr-utils.ts +104 -0
  211. package/tsconfig.json +2 -2
  212. /package/dist/{accounting/invoice/invoice-pdf.service.d.ts → shared/transactions-pdf.service.d.ts} +0 -0
@@ -0,0 +1,443 @@
1
+ import { Add, Divide, Multiply, Subtract } from "../shared/math-operations";
2
+ import { GetNumber } from "../shared/util";
3
+
4
+ /**
5
+ * Safely handles null/undefined arrays and returns empty array
6
+ */
7
+ function safeArray(arr: any[]): any[] {
8
+ return Array.isArray(arr) ? arr : [];
9
+ }
10
+
11
+ function getLineNetAmount(line: any): number {
12
+ return Subtract(GetNumber(line.UnAmt || line.Amt), Add(GetNumber(line.Disc), GetNumber(line.RecDisc)));
13
+ }
14
+
15
+ function getTaxCodeById(taxCodes: any[], taxCodeId: any) {
16
+ const normalizedTaxCodeId = Number(taxCodeId);
17
+ if (!normalizedTaxCodeId) {
18
+ return null;
19
+ }
20
+
21
+ return taxCodes.find((taxCode: any) => Number(taxCode._id) === normalizedTaxCodeId) || null;
22
+ }
23
+
24
+ function getTaxComponentRate(taxCode: any, code: string): number {
25
+ const component = safeArray(taxCode?.Components).find((item: any) => item?.Code === code);
26
+ if (component) {
27
+ return GetNumber(component.Rate);
28
+ }
29
+
30
+ return GetNumber(taxCode?.[code]);
31
+ }
32
+
33
+ function buildTaxesFromFlatFields(line: any, taxCode: any, netAmount: number): any[] {
34
+ const flatTaxes = [
35
+ { Code: "CGST", Amt: GetNumber(line.CGST) },
36
+ { Code: "SGST", Amt: GetNumber(line.SGST) },
37
+ { Code: "IGST", Amt: GetNumber(line.IGST) }
38
+ ];
39
+
40
+ return flatTaxes
41
+ .filter((tax) => tax.Amt > 0)
42
+ .map((tax) => ({
43
+ Code: tax.Code,
44
+ Amt: tax.Amt,
45
+ Rate: getTaxComponentRate(taxCode, tax.Code) || (netAmount > 0 ? Multiply(Divide(tax.Amt, netAmount), 100) : 0),
46
+ TaxCodeId: GetNumber(line.TCode) || GetNumber(taxCode?._id)
47
+ }));
48
+ }
49
+
50
+ function buildTaxesFromTaxCode(line: any, taxCode: any, netAmount: number): any[] {
51
+ if (!taxCode) {
52
+ return [];
53
+ }
54
+
55
+ const taxComponents = safeArray(taxCode.Components);
56
+ if (taxComponents.length > 0) {
57
+ return taxComponents
58
+ .filter((component: any) => ["CGST", "SGST", "IGST"].includes(component?.Code))
59
+ .map((component: any) => ({
60
+ Code: component.Code,
61
+ Amt: Multiply(netAmount, Divide(GetNumber(component.Rate), 100)),
62
+ Rate: GetNumber(component.Rate),
63
+ TaxCodeId: GetNumber(taxCode._id)
64
+ }))
65
+ .filter((tax: any) => tax.Amt > 0);
66
+ }
67
+
68
+ const legacyTaxes = [
69
+ { Code: "CGST", Rate: GetNumber(taxCode.CGST) },
70
+ { Code: "SGST", Rate: GetNumber(taxCode.SGST) },
71
+ { Code: "IGST", Rate: GetNumber(taxCode.IGST) }
72
+ ];
73
+
74
+ return legacyTaxes
75
+ .filter((tax) => tax.Rate > 0)
76
+ .map((tax) => ({
77
+ Code: tax.Code,
78
+ Amt: Multiply(netAmount, Divide(tax.Rate, 100)),
79
+ Rate: tax.Rate,
80
+ TaxCodeId: GetNumber(taxCode._id)
81
+ }));
82
+ }
83
+
84
+ function getResolvedTaxes(line: any, taxCodes: any[] = []): any[] {
85
+ const lineTaxes = safeArray(line?.Taxes);
86
+ if (lineTaxes.length > 0) {
87
+ return lineTaxes.map((tax: any) => ({
88
+ ...tax,
89
+ Code: tax?.Code,
90
+ Amt: GetNumber(tax?.Amt),
91
+ Rate: GetNumber(tax?.Rate),
92
+ TaxCodeId: GetNumber(tax?.TaxCodeId)
93
+ })).filter((tax: any) => !!tax.Code);
94
+ }
95
+
96
+ const netAmount = getLineNetAmount(line);
97
+ const taxCode = getTaxCodeById(taxCodes, line?.TCode);
98
+
99
+ const taxesFromFlatFields = buildTaxesFromFlatFields(line, taxCode, netAmount);
100
+ if (taxesFromFlatFields.length > 0) {
101
+ return taxesFromFlatFields;
102
+ }
103
+
104
+ return buildTaxesFromTaxCode(line, taxCode, netAmount);
105
+ }
106
+
107
+ function getTaxAmountByCode(taxes: any[], code: string): number {
108
+ return taxes
109
+ .filter((tax: any) => tax.Code === code)
110
+ .reduce((sum: number, tax: any) => Add(sum, GetNumber(tax.Amt)), 0);
111
+ }
112
+
113
+ export function TotalCalculations(items: any[] = [], ops: any[] = [], isTaxable: boolean = false, taxCodes: any[] = [], Adjust: number = 0) {
114
+ // Handle null/undefined arrays
115
+ const safeItems = safeArray(items);
116
+ const safeOps = safeArray(ops);
117
+ const safeTaxCodes = safeArray(taxCodes);
118
+
119
+ // Initialize all totals
120
+ let subtotalOnItems: number = 0;
121
+ let totalTaxOnItems: number = 0;
122
+ let totalDiscountOnItems: number = 0;
123
+
124
+ let subtotalOnLabor: number = 0;
125
+ let totalTaxOnLabor: number = 0;
126
+ let totalDiscountOnLabor: number = 0;
127
+
128
+ let total: number = 0;
129
+ let subtotal: number = 0;
130
+ let totalTax: number = 0;
131
+ let totalDiscount: number = 0;
132
+ let roundOff: number = 0;
133
+ let totalAfterRounded: number = 0;
134
+
135
+ // Returns totals
136
+ let returnSubtotal: number = 0;
137
+ let nonReturnSubTotal: number = 0;
138
+ let returnTax: number = 0;
139
+ let nonReturnTax: number = 0;
140
+ let returnTotal: number = 0;
141
+ let nonReturnTotal: number = 0;
142
+ let returnTotalDiscount: number = 0;
143
+ let nonReturnTotalDiscount: number = 0;
144
+
145
+ // Tax breakdown totals - only calculate if taxable
146
+ let totalSGST: number = 0;
147
+ let totalCGST: number = 0;
148
+ let totalIGST: number = 0;
149
+
150
+ let totalSGSTOnItems: number = 0;
151
+ let totalCGSTOnItems: number = 0;
152
+ let totalIGSTOnItems: number = 0;
153
+
154
+ let totalSGSTOnLabor: number = 0;
155
+ let totalCGSTOnLabor: number = 0;
156
+ let totalIGSTOnLabor: number = 0;
157
+
158
+
159
+ // Calculate items totals with null safety
160
+ safeItems.forEach((item: any) => {
161
+ const itemDisc = GetNumber(item.Disc);
162
+ const itemRecDisc = GetNumber(item.RecDisc);
163
+ const itemUnAmt = GetNumber(item.UnAmt);
164
+
165
+ // Calculate return total for items with Ret = true (explicitly check for true)
166
+ if (item.Ret === true) {
167
+ const itemNetAmount = Subtract(itemUnAmt, Add(itemDisc, itemRecDisc));
168
+
169
+ returnTotalDiscount = Add(returnTotalDiscount, itemDisc, itemRecDisc);
170
+ // Add to return subtotal (net amount after discounts)
171
+ returnSubtotal = Add(returnSubtotal, itemUnAmt);
172
+
173
+ if (isTaxable) {
174
+ const resolvedItemTaxes = getResolvedTaxes(item, safeTaxCodes);
175
+ const itemSGST = getTaxAmountByCode(resolvedItemTaxes, "SGST");
176
+ const itemCGST = getTaxAmountByCode(resolvedItemTaxes, "CGST");
177
+ const itemIGST = getTaxAmountByCode(resolvedItemTaxes, "IGST");
178
+ const itemTotalTax = Add(itemSGST, itemCGST, itemIGST);
179
+
180
+ // Add to return tax
181
+ returnTax = Add(returnTax, itemTotalTax);
182
+
183
+ // Return total includes net amount + tax
184
+ returnTotal = Add(returnTotal, itemNetAmount, itemTotalTax);
185
+ } else {
186
+ // Return total without tax (same as returnSubtotal when not taxable)
187
+ returnTotal = Add(returnTotal, itemNetAmount);
188
+ }
189
+ } else {
190
+ // Process normal items (Ret is false, undefined, or null - all treated as normal items)
191
+ totalDiscountOnItems = Add(totalDiscountOnItems, itemDisc, itemRecDisc);
192
+ subtotalOnItems = Add(subtotalOnItems, itemUnAmt);
193
+
194
+ // Only calculate tax if taxable
195
+ if (isTaxable) {
196
+ const resolvedItemTaxes = getResolvedTaxes(item, safeTaxCodes);
197
+ const itemSGST = getTaxAmountByCode(resolvedItemTaxes, "SGST");
198
+ const itemCGST = getTaxAmountByCode(resolvedItemTaxes, "CGST");
199
+ const itemIGST = getTaxAmountByCode(resolvedItemTaxes, "IGST");
200
+
201
+ // Add individual tax amounts for items
202
+ totalSGSTOnItems = Add(totalSGSTOnItems, itemSGST);
203
+ totalCGSTOnItems = Add(totalCGSTOnItems, itemCGST);
204
+ totalIGSTOnItems = Add(totalIGSTOnItems, itemIGST);
205
+
206
+ totalTaxOnItems = Add(totalTaxOnItems, itemSGST, itemCGST, itemIGST);
207
+ }
208
+ }
209
+ });
210
+
211
+ // Calculate operations/labor totals with null safety
212
+ safeOps.forEach((op: any) => {
213
+ const opDisc = GetNumber(op.Disc);
214
+ const opRecDisc = GetNumber(op.RecDisc);
215
+ const opAmt = GetNumber(op.Amt);
216
+
217
+ totalDiscountOnLabor = Add(totalDiscountOnLabor, opDisc, opRecDisc);
218
+ subtotalOnLabor = Add(subtotalOnLabor, opAmt);
219
+
220
+ // Only calculate tax if taxable
221
+ if (isTaxable) {
222
+ const resolvedOpTaxes = getResolvedTaxes(op, safeTaxCodes);
223
+ const opSGST = getTaxAmountByCode(resolvedOpTaxes, "SGST");
224
+ const opCGST = getTaxAmountByCode(resolvedOpTaxes, "CGST");
225
+ const opIGST = getTaxAmountByCode(resolvedOpTaxes, "IGST");
226
+
227
+ // Add individual tax amounts for operations/labor
228
+ totalSGSTOnLabor = Add(totalSGSTOnLabor, opSGST);
229
+ totalCGSTOnLabor = Add(totalCGSTOnLabor, opCGST);
230
+ totalIGSTOnLabor = Add(totalIGSTOnLabor, opIGST);
231
+
232
+ totalTaxOnLabor = Add(totalTaxOnLabor, opSGST, opCGST, opIGST);
233
+ }
234
+ });
235
+
236
+ // Calculate combined totals
237
+ totalDiscount = Add(totalDiscountOnItems, totalDiscountOnLabor);
238
+ totalTax = Add(totalTaxOnItems, totalTaxOnLabor);
239
+ subtotal = Add(subtotalOnItems, subtotalOnLabor);
240
+ nonReturnSubTotal = subtotal;
241
+ nonReturnTax = totalTax;
242
+ nonReturnTotalDiscount = totalDiscount;
243
+
244
+ // Step 2: Add tax if taxable
245
+ nonReturnTotal = isTaxable ? Add(Subtract(subtotal, totalDiscount), totalTax) : Subtract(subtotal, totalDiscount);
246
+
247
+ // Subtract return subtotal from subtotal
248
+ subtotal = Subtract(subtotal, returnSubtotal);
249
+
250
+ // Subtract return tax from total tax
251
+ totalTax = Subtract(totalTax, returnTax);
252
+
253
+ // Subtract return Discount from total discount
254
+ totalDiscount = Subtract(totalDiscount, returnTotalDiscount);
255
+
256
+ // Calculate combined tax breakdown totals only if taxable
257
+ if (isTaxable) {
258
+ totalSGST = Add(totalSGSTOnItems, totalSGSTOnLabor);
259
+ totalCGST = Add(totalCGSTOnItems, totalCGSTOnLabor);
260
+ totalIGST = Add(totalIGSTOnItems, totalIGSTOnLabor);
261
+ }
262
+
263
+ // FIXED CALCULATION: Correct order of operations
264
+ // Step 1: Calculate net amount after discount
265
+ const netAmountAfterDiscount = Subtract(subtotal, totalDiscount);
266
+
267
+ // Step 2: Add tax if taxable
268
+ total = isTaxable ? Add(netAmountAfterDiscount, totalTax) : netAmountAfterDiscount;
269
+
270
+ // Step 3: Add adjustments
271
+ total = Add(total, GetNumber(Adjust));
272
+
273
+ // // Step 4: Subtract return total from the final total
274
+ // total = Subtract(total, returnTotal);
275
+
276
+ // FIXED ROUNDING: Use Math.round instead of Math.floor
277
+ const roundedTotal = Math.round(total);
278
+ roundOff = Subtract(roundedTotal, total);
279
+
280
+ // Update total to rounded value
281
+ totalAfterRounded = roundedTotal;
282
+
283
+ // Base return object
284
+ const result: any = {
285
+ total,
286
+ totalAfterRounded,
287
+ subtotal,
288
+ totalDiscount,
289
+ roundOff,
290
+ subtotalOnItems,
291
+ subtotalOnLabor,
292
+ totalDiscountOnItems,
293
+ totalDiscountOnLabor,
294
+ netAmountAfterDiscount,
295
+ returnSubtotal,
296
+ returnTax,
297
+ returnTotal,
298
+ returnTotalDiscount,
299
+ nonReturnSubTotal,
300
+ nonReturnTax,
301
+ nonReturnTotal,
302
+ nonReturnTotalDiscount
303
+ };
304
+
305
+ // Only include tax fields and taxSummary if taxable
306
+ if (isTaxable) {
307
+ result.totalTax = totalTax;
308
+ result.totalTaxOnItems = totalTaxOnItems;
309
+ result.totalTaxOnLabor = totalTaxOnLabor;
310
+ result.totalSGST = totalSGST;
311
+ result.totalCGST = totalCGST;
312
+ result.totalIGST = totalIGST;
313
+ result.totalSGSTOnItems = totalSGSTOnItems;
314
+ result.totalCGSTOnItems = totalCGSTOnItems;
315
+ result.totalIGSTOnItems = totalIGSTOnItems;
316
+ result.totalSGSTOnLabor = totalSGSTOnLabor;
317
+ result.totalCGSTOnLabor = totalCGSTOnLabor;
318
+ result.totalIGSTOnLabor = totalIGSTOnLabor;
319
+
320
+ // Calculate tax summary for both new Taxes[] and legacy tax-code based records
321
+ // Filter out items where Ret is true (only include non-return items)
322
+ const nonReturnItems = safeItems.filter((item: any) => item.Ret !== true);
323
+ result.itemsTaxSummary = CalculateTaxSummary(nonReturnItems, safeTaxCodes);
324
+ result.opsTaxSummary = CalculateTaxSummary(safeOps, safeTaxCodes);
325
+ }
326
+
327
+ return result;
328
+ }
329
+
330
+ /**
331
+ * Enhanced version with decimal places control
332
+ */
333
+ export function TotalCalculationsWithDecimals(
334
+ items: any[] = [],
335
+ ops: any[] = [],
336
+ isTaxable: boolean = false,
337
+ taxCodes: any[] = [],
338
+ Adjust: number,
339
+ decimalPlaces: number = 2,
340
+ ) {
341
+ const result = TotalCalculations(items, ops, isTaxable, taxCodes, Adjust);
342
+
343
+ // Round all values to specified decimal places
344
+ const roundToDecimals = (num: number) => Math.round(num * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces);
345
+
346
+ // Base rounded result
347
+ const roundedResult: any = {
348
+ total: roundToDecimals(result.total),
349
+ totalAfterRounded: roundToDecimals(result.totalAfterRounded),
350
+ subtotal: roundToDecimals(result.subtotal),
351
+ totalDiscount: roundToDecimals(result.totalDiscount),
352
+ roundOff: roundToDecimals(result.roundOff),
353
+ subtotalOnItems: roundToDecimals(result.subtotalOnItems),
354
+ subtotalOnLabor: roundToDecimals(result.subtotalOnLabor),
355
+ totalDiscountOnItems: roundToDecimals(result.totalDiscountOnItems),
356
+ totalDiscountOnLabor: roundToDecimals(result.totalDiscountOnLabor),
357
+ // returned related fields
358
+ netAmountAfterDiscount: roundToDecimals(result.netAmountAfterDiscount),
359
+ returnSubtotal: roundToDecimals(result.returnSubtotal),
360
+ returnTax: roundToDecimals(result.returnTax),
361
+ returnTotal: roundToDecimals(result.returnTotal),
362
+ returnTotalDiscount: roundToDecimals(result.returnTotalDiscount),
363
+ nonReturnSubTotal: roundToDecimals(result.nonReturnSubTotal),
364
+ nonReturnTax: roundToDecimals(result.nonReturnTax),
365
+ nonReturnTotal: roundToDecimals(result.nonReturnTotal),
366
+ nonReturnTotalDiscount: roundToDecimals(result.nonReturnTotalDiscount)
367
+ };
368
+
369
+ // Only include tax fields and taxSummary if taxable
370
+ if (isTaxable) {
371
+ roundedResult.totalTax = roundToDecimals(result.totalTax);
372
+ roundedResult.totalTaxOnItems = roundToDecimals(result.totalTaxOnItems);
373
+ roundedResult.totalTaxOnLabor = roundToDecimals(result.totalTaxOnLabor);
374
+ roundedResult.totalSGST = roundToDecimals(result.totalSGST);
375
+ roundedResult.totalCGST = roundToDecimals(result.totalCGST);
376
+ roundedResult.totalIGST = roundToDecimals(result.totalIGST);
377
+ roundedResult.totalSGSTOnItems = roundToDecimals(result.totalSGSTOnItems);
378
+ roundedResult.totalCGSTOnItems = roundToDecimals(result.totalCGSTOnItems);
379
+ roundedResult.totalIGSTOnItems = roundToDecimals(result.totalIGSTOnItems);
380
+ roundedResult.totalSGSTOnLabor = roundToDecimals(result.totalSGSTOnLabor);
381
+ roundedResult.totalCGSTOnLabor = roundToDecimals(result.totalCGSTOnLabor);
382
+ roundedResult.totalIGSTOnLabor = roundToDecimals(result.totalIGSTOnLabor);
383
+
384
+ // Round tax summary only if it exists
385
+ if (result.itemsTaxSummary) {
386
+ roundedResult.itemsTaxSummary = result.itemsTaxSummary.map((tax: any) => ({
387
+ ...tax,
388
+ TaxableAmount: roundToDecimals(tax.TaxableAmount),
389
+ TaxAmount: roundToDecimals(tax.TaxAmount),
390
+ CGSTAmount: roundToDecimals(tax.CGSTAmount),
391
+ SGSTAmount: roundToDecimals(tax.SGSTAmount),
392
+ IGSTAmount: roundToDecimals(tax.IGSTAmount)
393
+ }));
394
+ }
395
+ if (result.opsTaxSummary) {
396
+ roundedResult.opsTaxSummary = result.opsTaxSummary.map((tax: any) => ({
397
+ ...tax,
398
+ TaxableAmount: roundToDecimals(tax.TaxableAmount),
399
+ TaxAmount: roundToDecimals(tax.TaxAmount),
400
+ CGSTAmount: roundToDecimals(tax.CGSTAmount),
401
+ SGSTAmount: roundToDecimals(tax.SGSTAmount),
402
+ IGSTAmount: roundToDecimals(tax.IGSTAmount)
403
+ }));
404
+ }
405
+ }
406
+
407
+ return roundedResult;
408
+ }
409
+
410
+ export function CalculateTaxSummary(items: any[], taxCodes: any[]) {
411
+ return Object.values(
412
+ items.reduce((acc: any, item: any) => {
413
+ const taxes = getResolvedTaxes(item, taxCodes);
414
+ if (taxes.length === 0) return acc;
415
+
416
+ const percent = (taxes.reduce((sum: number, tax: any) => Add(sum, GetNumber(tax.Rate)), 0) + "%");
417
+ const taxable = getLineNetAmount(item);
418
+ const cgstAmount = getTaxAmountByCode(taxes, "CGST");
419
+ const sgstAmount = getTaxAmountByCode(taxes, "SGST");
420
+ const igstAmount = getTaxAmountByCode(taxes, "IGST");
421
+ const totalTaxAmount = Add(cgstAmount, sgstAmount, igstAmount);
422
+
423
+ if (!acc[percent]) {
424
+ acc[percent] = {
425
+ Tax: percent,
426
+ TaxableAmount: 0,
427
+ TaxAmount: 0,
428
+ CGSTAmount: 0,
429
+ SGSTAmount: 0,
430
+ IGSTAmount: 0
431
+ };
432
+ }
433
+
434
+ acc[percent].TaxableAmount = Add(acc[percent].TaxableAmount, taxable);
435
+ acc[percent].TaxAmount = Add(acc[percent].TaxAmount, totalTaxAmount);
436
+ acc[percent].CGSTAmount = Add(acc[percent].CGSTAmount, cgstAmount);
437
+ acc[percent].SGSTAmount = Add(acc[percent].SGSTAmount, sgstAmount);
438
+ acc[percent].IGSTAmount = Add(acc[percent].IGSTAmount, igstAmount);
439
+
440
+ return acc;
441
+ }, {})
442
+ );
443
+ }