shareneus 1.5.15 → 1.5.17

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.
@@ -1,4 +1,4 @@
1
- export declare function GetItemPriceForPriceList(item: any, priceList: any, includeMargin?: boolean): any;
1
+ export declare function GetItemPriceForPriceList(item: any, priceList: any, includeMargin?: boolean, quantity?: number): any;
2
2
  /**
3
3
  * Calculates the final sale price of an item after applying a discount and optional markup.
4
4
  * Optionally includes margin calculation based on cost.
@@ -4,58 +4,87 @@ exports.GetItemPriceForPriceList = GetItemPriceForPriceList;
4
4
  exports.CalcItemFinalSalePrice = CalcItemFinalSalePrice;
5
5
  const math_operations_1 = require("../../shared/math-operations");
6
6
  const util_1 = require("../../shared/util");
7
- function GetItemPriceForPriceList(item, priceList, includeMargin = false) {
8
- var _a, _b;
9
- let salePricesByPriceListId = [];
7
+ function GetItemPriceForPriceList(item, priceList, includeMargin = false, quantity = 1) {
8
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
9
+ if (!priceList) {
10
+ return item;
11
+ }
12
+ const resolvedQty = Math.max(1, Number(quantity) || 1);
10
13
  const calcDisc = (_a = priceList.CalcDisc) !== null && _a !== void 0 ? _a : {};
11
- let markUp = (_b = calcDisc.Mark) !== null && _b !== void 0 ? _b : false;
12
- if ((0, util_1.IsNotNull)(item.SalePrices) && item.SalePrices.length > 0) {
13
- salePricesByPriceListId = item.SalePrices.filter((price) => priceList._id.toString() == price.PrListId.toString());
14
+ const markUp = (_b = calcDisc.Mark) !== null && _b !== void 0 ? _b : false;
15
+ const priceListId = (_d = (_c = priceList === null || priceList === void 0 ? void 0 : priceList._id) === null || _c === void 0 ? void 0 : _c.toString) === null || _d === void 0 ? void 0 : _d.call(_c);
16
+ let salePricesByPriceListId = [];
17
+ if (priceListId && (0, util_1.IsNotNull)(item.SalePrices) && item.SalePrices.length > 0) {
18
+ salePricesByPriceListId = item.SalePrices.filter((price) => (price === null || price === void 0 ? void 0 : price.PrListId) && price.PrListId.toString() === priceListId);
14
19
  }
20
+ item.PriceTiers = (_e = priceList === null || priceList === void 0 ? void 0 : priceList.QtyTiers) !== null && _e !== void 0 ? _e : undefined;
21
+ item.AppliedPriceTier = undefined;
15
22
  if (salePricesByPriceListId.length > 0) {
16
23
  item.SalePrices = salePricesByPriceListId;
17
- if (item.Track == 'TR' || item.Track == 'SN') {
24
+ const activeSalePrice = salePricesByPriceListId[0];
25
+ const activeTierList = (_f = activeSalePrice === null || activeSalePrice === void 0 ? void 0 : activeSalePrice.QtyTiers) !== null && _f !== void 0 ? _f : priceList === null || priceList === void 0 ? void 0 : priceList.QtyTiers;
26
+ if (activeTierList) {
27
+ item.PriceTiers = activeTierList;
28
+ }
29
+ if (item.Track === "TR" || item.Track === "SN") {
18
30
  item.Sale["MRP"] = item.Sale.Price;
19
- if ((0, util_1.IsNotNull)(item.SalePrices[0].Price) && (0, util_1.IsNotNull)(item.SalePrices[0].Disc)) {
20
- item.Sale.Price = CalcItemFinalSalePrice(item.SalePrices[0].Price, item.SalePrices[0].Disc, priceList, markUp);
31
+ const tier = ResolveTier(resolvedQty, activeSalePrice === null || activeSalePrice === void 0 ? void 0 : activeSalePrice.QtyTiers);
32
+ if (tier) {
33
+ const baseForTier = (0, util_1.IsNotNull)(activeSalePrice.Price) ? activeSalePrice.Price : item.Sale.Price;
34
+ item.Sale.Price = ApplyTierAdjustment(baseForTier, tier, priceList, false);
35
+ item.AppliedPriceTier = BuildAppliedTierMeta("ITEM", tier);
36
+ }
37
+ else if ((0, util_1.IsNotNull)(activeSalePrice.Price) && (0, util_1.IsNotNull)(activeSalePrice.Disc)) {
38
+ item.Sale.Price = CalcItemFinalSalePrice(activeSalePrice.Price, activeSalePrice.Disc, priceList, markUp);
21
39
  }
22
- else if ((0, util_1.IsNotNull)(item.SalePrices[0].Price) && (0, util_1.IsNull)(item.SalePrices[0].Disc)) {
23
- item.Sale.Price = ApplyRounding(item.SalePrices[0].Price, priceList);
40
+ else if ((0, util_1.IsNotNull)(activeSalePrice.Price) && (0, util_1.IsNull)(activeSalePrice.Disc)) {
41
+ item.Sale.Price = ApplyRounding(activeSalePrice.Price, priceList);
24
42
  }
25
- else if ((0, util_1.IsNull)(item.SalePrices[0].Price) && (0, util_1.IsNotNull)(item.SalePrices[0].Disc)) {
26
- item.Sale.Price = CalcItemFinalSalePrice(item.Sale.Price, item.SalePrices[0].Disc, priceList, markUp);
43
+ else if ((0, util_1.IsNull)(activeSalePrice.Price) && (0, util_1.IsNotNull)(activeSalePrice.Disc)) {
44
+ item.Sale.Price = CalcItemFinalSalePrice(item.Sale.Price, activeSalePrice.Disc, priceList, markUp);
27
45
  }
28
- if (includeMargin) {
46
+ if (includeMargin && (0, util_1.IsNotNull)(item.Pur) && (0, util_1.IsNotNull)(item.Pur.Cost)) {
29
47
  item.Margin = (0, math_operations_1.GetMarginValue)(item.Sale.Price, item.Pur.Cost);
30
48
  }
31
49
  }
32
- else if (item.Track == 'BN') {
33
- for (let index = 0; index < item.Batches.length; index++) {
34
- const element = item.Batches[index];
35
- let batchSalePrice = item.SalePrices.filter((batch) => priceList._id.toString() == batch.PrListId.toString() && element.BN == batch.BN);
36
- if (batchSalePrice.length == 0) {
37
- batchSalePrice = item.SalePrices.filter((batch) => priceList._id.toString() == batch.PrListId.toString() && batch.BN == null);
50
+ else if (item.Track === "BN" && Array.isArray(item.Batches)) {
51
+ for (const element of item.Batches) {
52
+ const batchSalePrice = item.SalePrices.filter((batch) => {
53
+ var _a;
54
+ return priceListId === ((_a = batch === null || batch === void 0 ? void 0 : batch.PrListId) === null || _a === void 0 ? void 0 : _a.toString()) &&
55
+ (element.BN === batch.BN || batch.BN == null);
56
+ });
57
+ const activeBatchSale = batchSalePrice.length > 0 ? batchSalePrice[0] : undefined;
58
+ const tierList = (_h = (_g = activeBatchSale === null || activeBatchSale === void 0 ? void 0 : activeBatchSale.QtyTiers) !== null && _g !== void 0 ? _g : activeSalePrice === null || activeSalePrice === void 0 ? void 0 : activeSalePrice.QtyTiers) !== null && _h !== void 0 ? _h : priceList === null || priceList === void 0 ? void 0 : priceList.QtyTiers;
59
+ if (tierList) {
60
+ element.PriceTiers = tierList;
38
61
  }
39
62
  element.MRP = element.Price;
40
- if (batchSalePrice.length > 0) {
41
- if ((0, util_1.IsNotNull)(batchSalePrice[0].Price) && (0, util_1.IsNotNull)(batchSalePrice[0].Disc)) {
42
- element.Price = CalcItemFinalSalePrice(batchSalePrice[0].Price, batchSalePrice[0].Disc, priceList, markUp);
63
+ const tier = ResolveTier(resolvedQty, (_j = activeBatchSale === null || activeBatchSale === void 0 ? void 0 : activeBatchSale.QtyTiers) !== null && _j !== void 0 ? _j : activeSalePrice === null || activeSalePrice === void 0 ? void 0 : activeSalePrice.QtyTiers);
64
+ if (tier) {
65
+ const baseForTier = (0, util_1.IsNotNull)(activeBatchSale === null || activeBatchSale === void 0 ? void 0 : activeBatchSale.Price) ? activeBatchSale.Price : element.Price;
66
+ element.Price = ApplyTierAdjustment(baseForTier, tier, priceList, false);
67
+ element.AppliedPriceTier = BuildAppliedTierMeta("ITEM", tier);
68
+ }
69
+ else if (activeBatchSale) {
70
+ if ((0, util_1.IsNotNull)(activeBatchSale.Price) && (0, util_1.IsNotNull)(activeBatchSale.Disc)) {
71
+ element.Price = CalcItemFinalSalePrice(activeBatchSale.Price, activeBatchSale.Disc, priceList, markUp);
43
72
  }
44
- else if ((0, util_1.IsNotNull)(batchSalePrice[0].Price) && (0, util_1.IsNull)(batchSalePrice[0].Disc)) {
45
- element.Price = ApplyRounding(batchSalePrice[0].Price, priceList);
73
+ else if ((0, util_1.IsNotNull)(activeBatchSale.Price) && (0, util_1.IsNull)(activeBatchSale.Disc)) {
74
+ element.Price = ApplyRounding(activeBatchSale.Price, priceList);
46
75
  }
47
- else if ((0, util_1.IsNull)(batchSalePrice[0].Price) && (0, util_1.IsNotNull)(batchSalePrice[0].Disc)) {
48
- element.Price = CalcItemFinalSalePrice(element.Price, batchSalePrice[0].Disc, priceList, markUp);
76
+ else if ((0, util_1.IsNull)(activeBatchSale.Price) && (0, util_1.IsNotNull)(activeBatchSale.Disc)) {
77
+ element.Price = CalcItemFinalSalePrice(element.Price, activeBatchSale.Disc, priceList, markUp);
49
78
  }
50
79
  }
51
- if (includeMargin) {
80
+ if (includeMargin && (0, util_1.IsNotNull)(element.Cost)) {
52
81
  element.Margin = (0, math_operations_1.GetMarginValue)(element.Price, element.Cost);
53
82
  }
54
83
  }
55
84
  }
56
85
  return item;
57
86
  }
58
- return CalculatePriceBasedOnPriceList(item, priceList, includeMargin);
87
+ return CalculatePriceBasedOnPriceList(item, priceList, includeMargin, resolvedQty);
59
88
  }
60
89
  /**
61
90
  * Calculates the final sale price of an item after applying a discount and optional markup.
@@ -70,93 +99,116 @@ function GetItemPriceForPriceList(item, priceList, includeMargin = false) {
70
99
  * @returns The final price or an object containing margin and final price if includeMargin is true.
71
100
  */
72
101
  function CalcItemFinalSalePrice(price, disc, priceList, markup = false, includeMargin = false, cost = 0) {
73
- // Calculate the price after applying discount or markup
74
102
  const calculatedPrice = (0, math_operations_1.CalcDiscPrice)(price, disc, markup);
75
- // Apply rounding rules from the price list
76
103
  const finalPrice = ApplyRounding(calculatedPrice, priceList);
77
- // If margin calculation is requested, calculate and return both margin and final price
78
104
  if (includeMargin) {
79
105
  const margin = (0, math_operations_1.GetMarginValue)(finalPrice, cost);
80
106
  return { margin: margin, finalPrice: finalPrice };
81
107
  }
82
- // Otherwise, return only the final price
83
108
  return finalPrice;
84
109
  }
85
- function CalculatePriceBasedOnPriceList(item, priceList, includeMargin) {
86
- var _a, _b;
87
- if (priceList != null) {
88
- if (priceList.PrCalc == "FIXED") {
89
- if (priceList.Fixed && (0, util_1.IsNotNull)(priceList.Fixed.Price)) {
90
- const fixedPrice = priceList.Fixed.Price;
91
- const fixedDisc = priceList.Fixed.Disc || 0;
92
- if (item.Track == 'TR' || item.Track == 'SN') {
93
- item.Sale["MRP"] = item.Sale.Price;
110
+ function CalculatePriceBasedOnPriceList(item, priceList, includeMargin, quantity) {
111
+ var _a, _b, _c;
112
+ if (!priceList) {
113
+ return item;
114
+ }
115
+ const calcDisc = (_a = priceList.CalcDisc) !== null && _a !== void 0 ? _a : {};
116
+ const markUp = (_b = calcDisc.Mark) !== null && _b !== void 0 ? _b : false;
117
+ const resolvedQty = Math.max(1, Number(quantity) || 1);
118
+ const tier = ResolveTier(resolvedQty, priceList.QtyTiers);
119
+ if (tier) {
120
+ item.PriceTiers = (_c = priceList.QtyTiers) !== null && _c !== void 0 ? _c : undefined;
121
+ if (item.Track === "TR" || item.Track === "SN") {
122
+ item.Sale["MRP"] = item.Sale.Price;
123
+ const basePrice = DetermineBasePriceForTier(item, priceList);
124
+ item.Sale.Price = ApplyTierAdjustment(basePrice, tier, priceList, markUp);
125
+ item.AppliedPriceTier = BuildAppliedTierMeta("PRICELIST", tier);
126
+ if (includeMargin && (0, util_1.IsNotNull)(item.Pur) && (0, util_1.IsNotNull)(item.Pur.Cost)) {
127
+ item.Margin = (0, math_operations_1.GetMarginValue)(item.Sale.Price, item.Pur.Cost);
128
+ }
129
+ }
130
+ else if (item.Track === "BN" && Array.isArray(item.Batches)) {
131
+ for (const batch of item.Batches) {
132
+ batch.MRP = batch.Price;
133
+ const basePrice = DetermineBatchBasePriceForTier(batch, item, priceList);
134
+ batch.Price = ApplyTierAdjustment(basePrice, tier, priceList, markUp);
135
+ batch.AppliedPriceTier = BuildAppliedTierMeta("PRICELIST", tier);
136
+ if (includeMargin && (0, util_1.IsNotNull)(batch.Cost)) {
137
+ batch.Margin = (0, math_operations_1.GetMarginValue)(batch.Price, batch.Cost);
138
+ }
139
+ }
140
+ }
141
+ return item;
142
+ }
143
+ if (priceList.PrCalc === "FIXED") {
144
+ if (priceList.Fixed && (0, util_1.IsNotNull)(priceList.Fixed.Price)) {
145
+ const fixedPrice = priceList.Fixed.Price;
146
+ const fixedDisc = priceList.Fixed.Disc || 0;
147
+ if (item.Track === "TR" || item.Track === "SN") {
148
+ item.Sale["MRP"] = item.Sale.Price;
149
+ if ((0, util_1.IsNotNull)(fixedDisc) && fixedDisc > 0) {
150
+ item.Sale.Price = CalcItemFinalSalePrice(fixedPrice, fixedDisc, priceList);
151
+ }
152
+ else {
153
+ item.Sale.Price = ApplyRounding(fixedPrice, priceList);
154
+ }
155
+ if (includeMargin && (0, util_1.IsNotNull)(item.Pur) && (0, util_1.IsNotNull)(item.Pur.Cost)) {
156
+ item.Margin = (0, math_operations_1.GetMarginValue)(item.Sale.Price, item.Pur.Cost);
157
+ }
158
+ }
159
+ else if (item.Track === "BN" && Array.isArray(item.Batches)) {
160
+ for (const batch of item.Batches) {
161
+ batch.MRP = batch.Price;
94
162
  if ((0, util_1.IsNotNull)(fixedDisc) && fixedDisc > 0) {
95
- item.Sale.Price = CalcItemFinalSalePrice(fixedPrice, fixedDisc, priceList);
163
+ batch.Price = CalcItemFinalSalePrice(fixedPrice, fixedDisc, priceList);
96
164
  }
97
165
  else {
98
- item.Sale.Price = ApplyRounding(fixedPrice, priceList);
166
+ batch.Price = ApplyRounding(fixedPrice, priceList);
99
167
  }
100
- if (includeMargin) {
101
- item.Margin = (0, math_operations_1.GetMarginValue)(item.Sale.Price, item.Pur.Cost);
168
+ if (includeMargin && (0, util_1.IsNotNull)(batch.Cost)) {
169
+ batch.Margin = (0, math_operations_1.GetMarginValue)(batch.Price, batch.Cost);
102
170
  }
103
171
  }
104
- else if (item.Track == 'BN') {
105
- for (let batch of item.Batches) {
106
- batch.MRP = batch.Price;
107
- if ((0, util_1.IsNotNull)(fixedDisc) && fixedDisc > 0) {
108
- batch.Price = CalcItemFinalSalePrice(fixedPrice, fixedDisc, priceList);
109
- }
110
- else {
111
- batch.Price = ApplyRounding(fixedPrice, priceList);
112
- }
113
- if (includeMargin) {
114
- batch.Margin = (0, math_operations_1.GetMarginValue)(batch.Price, batch.Cost);
115
- }
172
+ }
173
+ }
174
+ }
175
+ else if (priceList.PrCalc === "DISC") {
176
+ const discPerc = calcDisc.Perc || 0;
177
+ if (priceList.BasedOn === "SP") {
178
+ if (item.Track === "TR" || item.Track === "SN") {
179
+ item.Sale["MRP"] = item.Sale.Price;
180
+ item.Sale.Price = CalcItemFinalSalePrice(item.Sale.Price, discPerc, priceList, markUp);
181
+ if (includeMargin && (0, util_1.IsNotNull)(item.Pur) && (0, util_1.IsNotNull)(item.Pur.Cost)) {
182
+ item.Margin = (0, math_operations_1.GetMarginValue)(item.Sale.Price, item.Pur.Cost);
183
+ }
184
+ }
185
+ else if (item.Track === "BN" && Array.isArray(item.Batches)) {
186
+ for (const batch of item.Batches) {
187
+ batch.MRP = batch.Price;
188
+ batch.Price = CalcItemFinalSalePrice(batch.Price, discPerc, priceList, markUp);
189
+ if (includeMargin && (0, util_1.IsNotNull)(batch.Cost)) {
190
+ batch.Margin = (0, math_operations_1.GetMarginValue)(batch.Price, batch.Cost);
116
191
  }
117
192
  }
118
193
  }
119
194
  }
120
- else if (priceList.PrCalc == "DISC") {
121
- const calcDisc = (_a = priceList.CalcDisc) !== null && _a !== void 0 ? _a : {};
122
- let discPerc = calcDisc.Perc || 0;
123
- let markUp = (_b = calcDisc.Mark) !== null && _b !== void 0 ? _b : false;
124
- if (priceList.BasedOn == "SP") {
125
- if (item.Track == 'TR' || item.Track == 'SN') {
126
- item.Sale["MRP"] = item.Sale.Price;
127
- item.Sale.Price = CalcItemFinalSalePrice(item.Sale.Price, discPerc, priceList, markUp);
195
+ else if (priceList.BasedOn === "PC") {
196
+ if (item.Track === "TR" || item.Track === "SN") {
197
+ item.Sale["MRP"] = item.Sale.Price;
198
+ if ((0, util_1.IsNotNull)(item.Pur) && (0, util_1.IsNotNull)(item.Pur.Cost)) {
199
+ item.Sale.Price = CalcItemFinalSalePrice(item.Pur.Cost, discPerc, priceList, markUp);
128
200
  if (includeMargin) {
129
201
  item.Margin = (0, math_operations_1.GetMarginValue)(item.Sale.Price, item.Pur.Cost);
130
202
  }
131
203
  }
132
- else if (item.Track == 'BN') {
133
- for (let batch of item.Batches) {
134
- batch.MRP = batch.Price;
135
- batch.Price = CalcItemFinalSalePrice(batch.Price, discPerc, priceList, markUp);
136
- if (includeMargin) {
137
- batch.Margin = (0, math_operations_1.GetMarginValue)(batch.Price, batch.Cost);
138
- }
139
- }
140
- }
141
204
  }
142
- else if (priceList.BasedOn == "PC") {
143
- if (item.Track == 'TR' || item.Track == 'SN') {
144
- item.Sale["MRP"] = item.Sale.Price;
145
- if ((0, util_1.IsNotNull)(item.Pur) && (0, util_1.IsNotNull)(item.Pur.Cost)) {
146
- item.Sale.Price = CalcItemFinalSalePrice(item.Pur.Cost, discPerc, priceList, markUp);
205
+ else if (item.Track === "BN" && Array.isArray(item.Batches)) {
206
+ for (const batch of item.Batches) {
207
+ batch.MRP = batch.Price;
208
+ if ((0, util_1.IsNotNull)(batch.Cost)) {
209
+ batch.Price = CalcItemFinalSalePrice(batch.Cost, discPerc, priceList, markUp);
147
210
  if (includeMargin) {
148
- item.Margin = (0, math_operations_1.GetMarginValue)(item.Sale.Price, item.Pur.Cost);
149
- }
150
- }
151
- }
152
- else if (item.Track == 'BN') {
153
- for (let batch of item.Batches) {
154
- batch.MRP = batch.Price;
155
- if ((0, util_1.IsNotNull)(batch.Cost)) {
156
- batch.Price = CalcItemFinalSalePrice(batch.Cost, discPerc, priceList, markUp);
157
- if (includeMargin) {
158
- batch.Margin = (0, math_operations_1.GetMarginValue)(batch.Price, batch.Cost);
159
- }
211
+ batch.Margin = (0, math_operations_1.GetMarginValue)(batch.Price, batch.Cost);
160
212
  }
161
213
  }
162
214
  }
@@ -189,3 +241,74 @@ function ApplyRounding(price, priceList) {
189
241
  return price;
190
242
  }
191
243
  }
244
+ function ResolveTier(quantity, tiers) {
245
+ if (!Array.isArray(tiers) || tiers.length === 0) {
246
+ return null;
247
+ }
248
+ const sorted = [...tiers]
249
+ .filter((tier) => (0, util_1.IsNotNull)(tier) && (0, util_1.IsNotNull)(tier.MinQty))
250
+ .sort((a, b) => a.MinQty - b.MinQty);
251
+ let selected = null;
252
+ for (const tier of sorted) {
253
+ if (quantity >= tier.MinQty) {
254
+ selected = tier;
255
+ }
256
+ else {
257
+ break;
258
+ }
259
+ }
260
+ return selected;
261
+ }
262
+ function ApplyTierAdjustment(basePrice, tier, priceList, markUp) {
263
+ if ((0, util_1.IsNotNull)(tier === null || tier === void 0 ? void 0 : tier.Price)) {
264
+ return ApplyRounding(tier.Price, priceList);
265
+ }
266
+ if ((0, util_1.IsNotNull)(tier === null || tier === void 0 ? void 0 : tier.Disc)) {
267
+ const targetBase = (0, util_1.IsNotNull)(basePrice) ? basePrice : 0;
268
+ return CalcItemFinalSalePrice(targetBase, tier.Disc, priceList, markUp);
269
+ }
270
+ return ApplyRounding(basePrice, priceList);
271
+ }
272
+ function BuildAppliedTierMeta(source, tier) {
273
+ if (!tier) {
274
+ return undefined;
275
+ }
276
+ return {
277
+ Source: source,
278
+ MinQty: (0, util_1.IsNotNull)(tier.MinQty) ? tier.MinQty : null,
279
+ Price: (0, util_1.IsNotNull)(tier.Price) ? tier.Price : null,
280
+ Disc: (0, util_1.IsNotNull)(tier.Disc) ? tier.Disc : null,
281
+ };
282
+ }
283
+ function DetermineBasePriceForTier(item, priceList) {
284
+ var _a;
285
+ if (priceList.PrCalc === "FIXED") {
286
+ if ((0, util_1.IsNotNull)((_a = priceList.Fixed) === null || _a === void 0 ? void 0 : _a.Price)) {
287
+ return priceList.Fixed.Price;
288
+ }
289
+ return item.Sale.Price;
290
+ }
291
+ if (priceList.PrCalc === "DISC") {
292
+ if (priceList.BasedOn === "PC" && (0, util_1.IsNotNull)(item.Pur) && (0, util_1.IsNotNull)(item.Pur.Cost)) {
293
+ return item.Pur.Cost;
294
+ }
295
+ return item.Sale.Price;
296
+ }
297
+ return item.Sale.Price;
298
+ }
299
+ function DetermineBatchBasePriceForTier(batch, item, priceList) {
300
+ var _a;
301
+ if (priceList.PrCalc === "FIXED") {
302
+ if ((0, util_1.IsNotNull)((_a = priceList.Fixed) === null || _a === void 0 ? void 0 : _a.Price)) {
303
+ return priceList.Fixed.Price;
304
+ }
305
+ return batch.Price;
306
+ }
307
+ if (priceList.PrCalc === "DISC") {
308
+ if (priceList.BasedOn === "PC" && (0, util_1.IsNotNull)(batch.Cost)) {
309
+ return batch.Cost;
310
+ }
311
+ return batch.Price;
312
+ }
313
+ return batch.Price;
314
+ }
@@ -9,6 +9,8 @@ export declare class SalesTotalsService {
9
9
  static GetSubTotalFor(ItemsList: any): any;
10
10
  static GetItemsTotalAfterDiscount(ItemsList: any): any;
11
11
  static GetDetailedTotalsData(finalTotalsData: any, IsIndependentTax: boolean, ItemsList: any, TaxCodes: any, DecimalsNumber: any): any;
12
+ static GetTaxGroupingForPartsByPerc(Parts: any, TaxCodes: any, DecimalsNumber: any): any[];
13
+ static ComparePartsByPerc(Parts: any, TaxCodes: any): any[];
12
14
  static GetLaborDiscountedTotal(LaborList: any): any;
13
15
  static GetItemDiscountedTotal(ItemsList: any): any;
14
16
  static GetItemsTaxTotalFor(ItemsList: any, IsIndependentTax: boolean): any;
@@ -22,6 +24,6 @@ export declare class SalesTotalsService {
22
24
  static GetTaxGroupingForItemsByHSN(Items: any, TaxCodes: any, DecimalsNumber: number): any[];
23
25
  static GetCombinedTaxPercentage(Items: any, TaxCodes: any): any;
24
26
  static CompareItemsByHSN(Items: any, TaxCodes: any): any[];
25
- static GetTaxGroupingInfoByHSN(FinalMatchedItems: any, TaxCodes: any, DecimalsNumber: number): any[];
27
+ static GetTaxGroupingInfoByHSN(FinalMatchedParts: any, TaxCodes: any, DecimalsNumber: any): any[];
26
28
  static GetGSTValueBasedOnTaxCode(TCode: any, TaxCodes: any): any[];
27
29
  }
@@ -122,6 +122,7 @@ class SalesTotalsService {
122
122
  // finalTotalsData.CustLaborAfterTax = TrUtils.FixedTo(finalTotalsData.CustLaborITax + finalTotalsData.CustLaborTotalAfterDisc);
123
123
  finalTotalsData.CustItemAfterTax = (0, math_operations_1.Add)(finalTotalsData.CustItemITax, finalTotalsData.CustItemsTotalAfterDisc);
124
124
  finalTotalsData.CustTaxGroupData = this.GetTaxGroupingForItemsByHSN(ItemsList, TaxCodes, DecimalsNumber);
125
+ finalTotalsData.CustTaxGroupDataByPerc = this.GetTaxGroupingForPartsByPerc(ItemsList, TaxCodes, DecimalsNumber);
125
126
  finalTotalsData.CustTotalRoundedBy = tr_utils_1.TrUtils.FixedTo(finalTotalsData.Round, DecimalsNumber);
126
127
  finalTotalsData.CustRoundedTotal = tr_utils_1.TrUtils.FixedTo(finalTotalsData.Total, DecimalsNumber);
127
128
  for (var key in finalTotalsData) {
@@ -134,6 +135,43 @@ class SalesTotalsService {
134
135
  }
135
136
  return finalTotalsData;
136
137
  }
138
+ static GetTaxGroupingForPartsByPerc(Parts, TaxCodes, DecimalsNumber) {
139
+ Parts = this.GetCombinedTaxPercentage(tr_utils_1.TrUtils.Stringify(Parts), TaxCodes);
140
+ // Parts = this.ResetPartsHSNIfInvalid(Parts);
141
+ let FinalMatchedParts = this.ComparePartsByPerc(Parts, TaxCodes);
142
+ return this.GetTaxGroupingInfoByHSN(FinalMatchedParts, TaxCodes, DecimalsNumber);
143
+ }
144
+ static ComparePartsByPerc(Parts, TaxCodes) {
145
+ let FinalMatchedParts = [];
146
+ Parts === null || Parts === void 0 ? void 0 : Parts.forEach((Part) => {
147
+ let MatchedPartsBasedOnHSN = Parts.filter((argPart) => {
148
+ let PartFound = false;
149
+ FinalMatchedParts.forEach((FinalArgParts) => {
150
+ let PartIndex = FinalArgParts.findIndex((FinalArgPart) => {
151
+ return FinalArgPart._id === argPart._id;
152
+ });
153
+ if (PartIndex !== -1) {
154
+ PartFound = true;
155
+ }
156
+ });
157
+ // let argGSTValues: any[] = this.GetGSTValueBasedOnTaxCode(argPart.TCode, TaxCodes);
158
+ // let argCGST = argGSTValues[0];
159
+ // let argSGST = argGSTValues[1];
160
+ // let argIGST = argGSTValues[2];
161
+ // let partGSTValues: any[] = this.GetGSTValueBasedOnTaxCode(Part.TCode, TaxCodes);
162
+ // let partCGST = partGSTValues[0];
163
+ // let partSGST = partGSTValues[1];
164
+ // let partIGST = partGSTValues[2];
165
+ return (!PartFound) &&
166
+ (argPart.CGSTAmt === Part.CGSTAmt) && (argPart.IGSTAmt === Part.IGSTAmt) &&
167
+ (argPart.SGSTAmt === Part.SGSTAmt);
168
+ });
169
+ if (MatchedPartsBasedOnHSN.length !== 0) {
170
+ FinalMatchedParts.push(MatchedPartsBasedOnHSN);
171
+ }
172
+ });
173
+ return FinalMatchedParts;
174
+ }
137
175
  static GetLaborDiscountedTotal(LaborList) {
138
176
  let LaborDiscountedTotal = 0;
139
177
  LaborList.forEach((Labor) => {
@@ -283,31 +321,40 @@ class SalesTotalsService {
283
321
  });
284
322
  return FinalMatchedItems;
285
323
  }
286
- static GetTaxGroupingInfoByHSN(FinalMatchedItems, TaxCodes, DecimalsNumber) {
324
+ static GetTaxGroupingInfoByHSN(FinalMatchedParts, TaxCodes, DecimalsNumber) {
287
325
  let HSNTaxInfo = [];
288
- FinalMatchedItems.forEach((MatchedItem) => {
326
+ FinalMatchedParts.forEach((MatchedPart) => {
289
327
  let TaxInfo = {};
290
328
  let TaxOnAmount = 0;
291
329
  let TaxAmount = 0;
292
- let HSN = MatchedItem[0].HSN;
293
- TaxInfo.CombinedTaxPercentage = MatchedItem[0].CombinedTaxPercentage;
294
- MatchedItem.forEach((argMatchedGSTItem) => {
295
- TaxOnAmount = (0, math_operations_1.Add)(TaxOnAmount, argMatchedGSTItem.AfterItemDisc);
296
- TaxAmount = (0, math_operations_1.Add)(TaxAmount, argMatchedGSTItem.CGST);
297
- TaxAmount = (0, math_operations_1.Add)(TaxAmount, argMatchedGSTItem.IGST);
298
- TaxAmount = (0, math_operations_1.Add)(TaxAmount, argMatchedGSTItem.SGST);
330
+ let CGSTAmt = 0;
331
+ let SGSTAmt = 0;
332
+ let IGSTAmt = 0;
333
+ let HSN = MatchedPart[0].HSN;
334
+ TaxInfo.CombinedTaxPercentage = MatchedPart[0].CombinedTaxPercentage;
335
+ MatchedPart.forEach((argMatchedGSTPart) => {
336
+ TaxOnAmount = (0, math_operations_1.Add)(TaxOnAmount, tr_utils_1.TrUtils.FixedTo(argMatchedGSTPart.AfterPartDisc, DecimalsNumber));
337
+ TaxAmount = (0, math_operations_1.Add)(TaxAmount, tr_utils_1.TrUtils.FixedTo(argMatchedGSTPart.CGST, DecimalsNumber));
338
+ TaxAmount = (0, math_operations_1.Add)(TaxAmount, tr_utils_1.TrUtils.FixedTo(argMatchedGSTPart.IGST, DecimalsNumber));
339
+ TaxAmount = (0, math_operations_1.Add)(TaxAmount, tr_utils_1.TrUtils.FixedTo(argMatchedGSTPart.SGST, DecimalsNumber));
340
+ CGSTAmt = (0, math_operations_1.Add)(CGSTAmt, tr_utils_1.TrUtils.FixedTo(argMatchedGSTPart.CGST, DecimalsNumber));
341
+ SGSTAmt = (0, math_operations_1.Add)(SGSTAmt, tr_utils_1.TrUtils.FixedTo(argMatchedGSTPart.SGST, DecimalsNumber));
342
+ IGSTAmt = (0, math_operations_1.Add)(IGSTAmt, tr_utils_1.TrUtils.FixedTo(argMatchedGSTPart.IGST, DecimalsNumber));
299
343
  });
300
344
  TaxInfo.HSN = HSN;
345
+ TaxInfo.CGSTAmt = CGSTAmt;
346
+ TaxInfo.SGSTAmt = SGSTAmt;
347
+ TaxInfo.IGSTAmt = IGSTAmt;
301
348
  TaxInfo.TotalTaxableAmount = tr_utils_1.TrUtils.FixPriceValue(TaxOnAmount, DecimalsNumber);
302
349
  TaxInfo.TotalTaxAmount = tr_utils_1.TrUtils.FixPriceValue(TaxAmount, DecimalsNumber);
303
- TaxInfo.ItemsCount = MatchedItem.length;
304
- // let GSTValues: any[] = this.GetGSTValueBasedOnTaxCode(MatchedItem[0].TCode, TaxCodes);
350
+ TaxInfo.ItemsCount = MatchedPart.length;
351
+ // let GSTValues: any[] = this.GetGSTValueBasedOnTaxCode(MatchedPart[0].TCode, TaxCodes);
305
352
  // TaxInfo.CGST = GSTValues[0];
306
353
  // TaxInfo.SGST = GSTValues[1];
307
354
  // TaxInfo.IGST = GSTValues[2];
308
- TaxInfo.CGST = MatchedItem[0].CGSTAmt;
309
- TaxInfo.IGST = MatchedItem[0].IGSTAmt;
310
- TaxInfo.SGST = MatchedItem[0].SGSTAmt;
355
+ TaxInfo.CGST = MatchedPart[0].CGSTAmt;
356
+ TaxInfo.IGST = MatchedPart[0].IGSTAmt;
357
+ TaxInfo.SGST = MatchedPart[0].SGSTAmt;
311
358
  if (TaxOnAmount !== 0) {
312
359
  HSNTaxInfo.push(TaxInfo);
313
360
  }
@@ -17,18 +17,22 @@ function SalesTotalCalculations(items = [], ops = [], isTaxable = false, taxCode
17
17
  const safeOps = safeArray(ops);
18
18
  const safeTaxCodes = safeArray(taxCodes);
19
19
  // Initialize all totals
20
- let total = 0;
21
20
  let subtotalOnItems = 0;
21
+ let totalTaxOnItems = 0;
22
+ let totalDiscountOnItems = 0;
22
23
  let subtotalOnLabor = 0;
24
+ let totalTaxOnLabor = 0;
25
+ let totalDiscountOnLabor = 0;
26
+ let total = 0;
23
27
  let subtotal = 0;
24
28
  let totalTax = 0;
25
- let totalTaxOnItems = 0;
26
- let totalTaxOnLabor = 0;
27
29
  let totalDiscount = 0;
28
- let totalDiscountOnItems = 0;
29
- let totalDiscountOnLabor = 0;
30
30
  let roundOff = 0;
31
31
  let totalAfterRounded = 0;
32
+ // Returns totals
33
+ let returnSubtotal = 0;
34
+ let returnTax = 0;
35
+ let returnTotal = 0;
32
36
  // Tax breakdown totals - only calculate if taxable
33
37
  let totalSGST = 0;
34
38
  let totalCGST = 0;
@@ -44,18 +48,41 @@ function SalesTotalCalculations(items = [], ops = [], isTaxable = false, taxCode
44
48
  const itemDisc = (0, util_1.GetNumber)(item.Disc);
45
49
  const itemRecDisc = (0, util_1.GetNumber)(item.RecDisc);
46
50
  const itemUnAmt = (0, util_1.GetNumber)(item.UnAmt);
47
- totalDiscountOnItems = (0, math_operations_1.Add)(totalDiscountOnItems, itemDisc, itemRecDisc);
48
- subtotalOnItems = (0, math_operations_1.Add)(subtotalOnItems, itemUnAmt);
49
- // Only calculate tax if taxable
50
- if (isTaxable) {
51
- const itemSGST = (0, util_1.GetNumber)(item.SGST);
52
- const itemCGST = (0, util_1.GetNumber)(item.CGST);
53
- const itemIGST = (0, util_1.GetNumber)(item.IGST);
54
- // Add individual tax amounts for items
55
- totalSGSTOnItems = (0, math_operations_1.Add)(totalSGSTOnItems, itemSGST);
56
- totalCGSTOnItems = (0, math_operations_1.Add)(totalCGSTOnItems, itemCGST);
57
- totalIGSTOnItems = (0, math_operations_1.Add)(totalIGSTOnItems, itemIGST);
58
- totalTaxOnItems = (0, math_operations_1.Add)(totalTaxOnItems, itemSGST, itemCGST, itemIGST);
51
+ // Calculate return total for items with Ret = true
52
+ if (item.Ret === true) {
53
+ const itemNetAmount = (0, math_operations_1.Subtract)(itemUnAmt, (0, math_operations_1.Add)(itemDisc, itemRecDisc));
54
+ // Add to return subtotal (net amount after discounts)
55
+ returnSubtotal = (0, math_operations_1.Add)(returnSubtotal, itemNetAmount);
56
+ if (isTaxable) {
57
+ const itemSGST = (0, util_1.GetNumber)(item.SGST);
58
+ const itemCGST = (0, util_1.GetNumber)(item.CGST);
59
+ const itemIGST = (0, util_1.GetNumber)(item.IGST);
60
+ const itemTotalTax = (0, math_operations_1.Add)(itemSGST, itemCGST, itemIGST);
61
+ // Add to return tax
62
+ returnTax = (0, math_operations_1.Add)(returnTax, itemTotalTax);
63
+ // Return total includes net amount + tax
64
+ returnTotal = (0, math_operations_1.Add)(returnTotal, itemNetAmount, itemTotalTax);
65
+ }
66
+ else {
67
+ // Return total without tax (same as returnSubtotal when not taxable)
68
+ returnTotal = (0, math_operations_1.Add)(returnTotal, itemNetAmount);
69
+ }
70
+ }
71
+ else {
72
+ // Process normal items (not returns)
73
+ totalDiscountOnItems = (0, math_operations_1.Add)(totalDiscountOnItems, itemDisc, itemRecDisc);
74
+ subtotalOnItems = (0, math_operations_1.Add)(subtotalOnItems, itemUnAmt);
75
+ // Only calculate tax if taxable
76
+ if (isTaxable) {
77
+ const itemSGST = (0, util_1.GetNumber)(item.SGST);
78
+ const itemCGST = (0, util_1.GetNumber)(item.CGST);
79
+ const itemIGST = (0, util_1.GetNumber)(item.IGST);
80
+ // Add individual tax amounts for items
81
+ totalSGSTOnItems = (0, math_operations_1.Add)(totalSGSTOnItems, itemSGST);
82
+ totalCGSTOnItems = (0, math_operations_1.Add)(totalCGSTOnItems, itemCGST);
83
+ totalIGSTOnItems = (0, math_operations_1.Add)(totalIGSTOnItems, itemIGST);
84
+ totalTaxOnItems = (0, math_operations_1.Add)(totalTaxOnItems, itemSGST, itemCGST, itemIGST);
85
+ }
59
86
  }
60
87
  });
61
88
  // Calculate operations/labor totals with null safety
@@ -81,6 +108,10 @@ function SalesTotalCalculations(items = [], ops = [], isTaxable = false, taxCode
81
108
  totalDiscount = (0, math_operations_1.Add)(totalDiscountOnItems, totalDiscountOnLabor);
82
109
  totalTax = (0, math_operations_1.Add)(totalTaxOnItems, totalTaxOnLabor);
83
110
  subtotal = (0, math_operations_1.Add)(subtotalOnItems, subtotalOnLabor);
111
+ // Subtract return subtotal from subtotal
112
+ subtotal = (0, math_operations_1.Subtract)(subtotal, returnSubtotal);
113
+ // Subtract return tax from total tax
114
+ totalTax = (0, math_operations_1.Subtract)(totalTax, returnTax);
84
115
  // Calculate combined tax breakdown totals only if taxable
85
116
  if (isTaxable) {
86
117
  totalSGST = (0, math_operations_1.Add)(totalSGSTOnItems, totalSGSTOnLabor);
@@ -92,7 +123,10 @@ function SalesTotalCalculations(items = [], ops = [], isTaxable = false, taxCode
92
123
  const netAmountAfterDiscount = (0, math_operations_1.Subtract)(subtotal, totalDiscount);
93
124
  // Step 2: Add tax if taxable
94
125
  total = isTaxable ? (0, math_operations_1.Add)(netAmountAfterDiscount, totalTax) : netAmountAfterDiscount;
126
+ // Step 3: Add adjustments
95
127
  total = (0, math_operations_1.Add)(total, (0, util_1.GetNumber)(Adjust));
128
+ // Step 4: Subtract return total from the final total
129
+ total = (0, math_operations_1.Subtract)(total, returnTotal);
96
130
  // FIXED ROUNDING: Use Math.round instead of Math.floor
97
131
  const roundedTotal = Math.round(total);
98
132
  roundOff = (0, math_operations_1.Subtract)(roundedTotal, total);
@@ -109,7 +143,10 @@ function SalesTotalCalculations(items = [], ops = [], isTaxable = false, taxCode
109
143
  subtotalOnLabor,
110
144
  totalDiscountOnItems,
111
145
  totalDiscountOnLabor,
112
- netAmountAfterDiscount
146
+ netAmountAfterDiscount,
147
+ returnSubtotal,
148
+ returnTax,
149
+ returnTotal
113
150
  };
114
151
  // Only include tax fields and taxSummary if taxable
115
152
  if (isTaxable) {
@@ -149,7 +186,10 @@ function SalesTotalCalculationsWithDecimals(items = [], ops = [], isTaxable = fa
149
186
  subtotalOnLabor: roundToDecimals(result.subtotalOnLabor),
150
187
  totalDiscountOnItems: roundToDecimals(result.totalDiscountOnItems),
151
188
  totalDiscountOnLabor: roundToDecimals(result.totalDiscountOnLabor),
152
- netAmountAfterDiscount: roundToDecimals(result.netAmountAfterDiscount)
189
+ netAmountAfterDiscount: roundToDecimals(result.netAmountAfterDiscount),
190
+ returnSubtotal: roundToDecimals(result.returnSubtotal),
191
+ returnTax: roundToDecimals(result.returnTax),
192
+ returnTotal: roundToDecimals(result.returnTotal)
153
193
  };
154
194
  // Only include tax fields and taxSummary if taxable
155
195
  if (isTaxable) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shareneus",
3
- "version": "1.5.15",
3
+ "version": "1.5.17",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",