@qrwise/pos-calculations 0.0.8 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -62,51 +62,43 @@ function getDiscountBreakdown(discountType, discountedAmount) {
62
62
  };
63
63
  }
64
64
  function calculateTotals(input) {
65
- const {
66
- discountableTotalAmount = 0,
67
- nonDiscountableTotalAmount = 0,
68
- serviceChargeableTotalAmount = 0,
69
- togoableTotalAmount = 0,
70
- voucherType = null,
71
- voucherRate = 0,
72
- discountType = null,
73
- discountRate = 0,
74
- vatRate = 12,
75
- serviceChargeRate = 0,
76
- togoCharge = 0
77
- } = input;
78
- const totalOrderAmount = format2(discountableTotalAmount + nonDiscountableTotalAmount);
65
+ const vatRate = Number(input.vatRate || 0) / 100;
66
+ const scRate = Number(input.serviceChargeRate || 0) / 100;
67
+ const discountRate = Number(input.discountRate || 0) / 100;
68
+ const voucherRate = input.voucherType === "FIXED" ? input.voucherRate : Number(input.voucherRate || 0) / 100;
69
+ const totalOrderAmount = input.discountableTotalAmount + input.nonDiscountableTotalAmount;
79
70
  let voucherDiscounted = 0;
80
- if (voucherType === "FIXED") {
81
- voucherDiscounted = -Math.min(toMoneyNumber(voucherRate), totalOrderAmount);
82
- }
83
- if (voucherType === "PERCENTAGE") {
84
- voucherDiscounted = -(totalOrderAmount * (toMoneyNumber(voucherRate) / 100));
71
+ if (voucherRate > 0) {
72
+ if (input.voucherType === "FIXED") {
73
+ voucherDiscounted = -format2(Math.min(toMoneyNumber(voucherRate), totalOrderAmount));
74
+ } else {
75
+ voucherDiscounted = -format2(totalOrderAmount * voucherRate);
76
+ }
85
77
  }
78
+ const togoCharge = input.togoableTotalAmount > 0 ? input.togoCharge : 0;
86
79
  const subtotal = totalOrderAmount + voucherDiscounted;
87
- const voucherVatNet = Number((totalOrderAmount / vatRate).toFixed(2));
88
- const voucherVat = Number((voucherVatNet * 0.12).toFixed(2));
89
- let vatNet = Number((subtotal / vatRate).toFixed(2));
90
- let vat = Number((vatNet * 0.12).toFixed(2));
80
+ const voucherVatNet = format2(totalOrderAmount / vatRate);
81
+ const voucherVat = format2(voucherVatNet * 0.12);
82
+ let vatNet = format2(subtotal / vatRate);
83
+ let vat = format2(vatNet * 0.12);
91
84
  const isSpecial = discountRate > 0;
92
- const isAddVat = isAddVatAfterDiscount(discountType);
93
- const discounted = Number((discountRate * -vatNet).toFixed(2));
85
+ const isAddVat = isAddVatAfterDiscount(input.discountType);
86
+ const discounted = format2(discountRate / 100 * -vatNet);
94
87
  if (isSpecial) {
95
- vatNet = Number((vatNet + discounted).toFixed(2));
88
+ vatNet = format2(vatNet + discounted);
96
89
  if (!isAddVat) {
97
90
  vat = -vat;
98
91
  }
99
92
  }
100
93
  let serviceCharge = 0;
101
- if (serviceChargeRate > 0 && serviceChargeableTotalAmount > 0) {
102
- const scRate = serviceChargeRate / 100;
94
+ if (input.serviceChargeRate > 0 && input.serviceChargeableTotalAmount > 0) {
103
95
  if (isSpecial) {
104
- serviceCharge = Number(((vatNet + Math.abs(discounted)) * scRate).toFixed(2));
96
+ serviceCharge = format2((vatNet + Math.abs(discounted)) * scRate);
105
97
  } else {
106
- serviceCharge = Number((vatNet * scRate).toFixed(2));
98
+ serviceCharge = format2(vatNet * scRate);
107
99
  }
108
100
  }
109
- let totalAmount = vatNet + vat + serviceCharge + (togoableTotalAmount > 0 ? togoCharge : 0);
101
+ let totalAmount = vatNet + vat + serviceCharge + togoCharge;
110
102
  if (isSpecial && !isAddVat) {
111
103
  totalAmount -= vat;
112
104
  }
@@ -119,7 +111,7 @@ function calculateTotals(input) {
119
111
  net: subtotal,
120
112
  vatNet: voucherVatNet,
121
113
  vat: voucherVat,
122
- forBirDiscount: -(voucherType === "FIXED" ? voucherVatNet - Number(voucherRate.toFixed(2)) : voucherVatNet * Number(voucherRate.toFixed(2)))
114
+ forBirDiscount: -(input.voucherType === "FIXED" ? voucherVatNet - Number(voucherRate.toFixed(2)) : voucherVatNet * Number(voucherRate.toFixed(2)))
123
115
  },
124
116
  hasDiscount: discountRate > 0,
125
117
  discount: {
package/dist/index.d.cts CHANGED
@@ -4,15 +4,15 @@ type DiscountType = "SENIOR" | "PWD" | "NAAC" | "SOLO" | "MOV";
4
4
  interface CalculateTotalsInput {
5
5
  discountableTotalAmount: number;
6
6
  nonDiscountableTotalAmount: number;
7
- serviceChargeableTotalAmount?: number;
8
- togoableTotalAmount?: number;
9
- voucherType?: VoucherType | null;
10
- voucherRate?: number;
11
- discountType?: DiscountType | null;
12
- discountRate?: number;
13
- vatRate?: number;
14
- serviceChargeRate?: number;
15
- togoCharge?: number;
7
+ serviceChargeableTotalAmount: number;
8
+ togoableTotalAmount: number;
9
+ voucherType: VoucherType | null;
10
+ voucherRate: number;
11
+ discountType: DiscountType | null;
12
+ discountRate: number;
13
+ vatRate: number;
14
+ serviceChargeRate: number;
15
+ togoCharge: number;
16
16
  }
17
17
  interface CalculateTotalsResult {
18
18
  gross: number;
package/dist/index.d.ts CHANGED
@@ -4,15 +4,15 @@ type DiscountType = "SENIOR" | "PWD" | "NAAC" | "SOLO" | "MOV";
4
4
  interface CalculateTotalsInput {
5
5
  discountableTotalAmount: number;
6
6
  nonDiscountableTotalAmount: number;
7
- serviceChargeableTotalAmount?: number;
8
- togoableTotalAmount?: number;
9
- voucherType?: VoucherType | null;
10
- voucherRate?: number;
11
- discountType?: DiscountType | null;
12
- discountRate?: number;
13
- vatRate?: number;
14
- serviceChargeRate?: number;
15
- togoCharge?: number;
7
+ serviceChargeableTotalAmount: number;
8
+ togoableTotalAmount: number;
9
+ voucherType: VoucherType | null;
10
+ voucherRate: number;
11
+ discountType: DiscountType | null;
12
+ discountRate: number;
13
+ vatRate: number;
14
+ serviceChargeRate: number;
15
+ togoCharge: number;
16
16
  }
17
17
  interface CalculateTotalsResult {
18
18
  gross: number;
package/dist/index.js CHANGED
@@ -30,51 +30,43 @@ function getDiscountBreakdown(discountType, discountedAmount) {
30
30
  };
31
31
  }
32
32
  function calculateTotals(input) {
33
- const {
34
- discountableTotalAmount = 0,
35
- nonDiscountableTotalAmount = 0,
36
- serviceChargeableTotalAmount = 0,
37
- togoableTotalAmount = 0,
38
- voucherType = null,
39
- voucherRate = 0,
40
- discountType = null,
41
- discountRate = 0,
42
- vatRate = 12,
43
- serviceChargeRate = 0,
44
- togoCharge = 0
45
- } = input;
46
- const totalOrderAmount = format2(discountableTotalAmount + nonDiscountableTotalAmount);
33
+ const vatRate = Number(input.vatRate || 0) / 100;
34
+ const scRate = Number(input.serviceChargeRate || 0) / 100;
35
+ const discountRate = Number(input.discountRate || 0) / 100;
36
+ const voucherRate = input.voucherType === "FIXED" ? input.voucherRate : Number(input.voucherRate || 0) / 100;
37
+ const totalOrderAmount = input.discountableTotalAmount + input.nonDiscountableTotalAmount;
47
38
  let voucherDiscounted = 0;
48
- if (voucherType === "FIXED") {
49
- voucherDiscounted = -Math.min(toMoneyNumber(voucherRate), totalOrderAmount);
50
- }
51
- if (voucherType === "PERCENTAGE") {
52
- voucherDiscounted = -(totalOrderAmount * (toMoneyNumber(voucherRate) / 100));
39
+ if (voucherRate > 0) {
40
+ if (input.voucherType === "FIXED") {
41
+ voucherDiscounted = -format2(Math.min(toMoneyNumber(voucherRate), totalOrderAmount));
42
+ } else {
43
+ voucherDiscounted = -format2(totalOrderAmount * voucherRate);
44
+ }
53
45
  }
46
+ const togoCharge = input.togoableTotalAmount > 0 ? input.togoCharge : 0;
54
47
  const subtotal = totalOrderAmount + voucherDiscounted;
55
- const voucherVatNet = Number((totalOrderAmount / vatRate).toFixed(2));
56
- const voucherVat = Number((voucherVatNet * 0.12).toFixed(2));
57
- let vatNet = Number((subtotal / vatRate).toFixed(2));
58
- let vat = Number((vatNet * 0.12).toFixed(2));
48
+ const voucherVatNet = format2(totalOrderAmount / vatRate);
49
+ const voucherVat = format2(voucherVatNet * 0.12);
50
+ let vatNet = format2(subtotal / vatRate);
51
+ let vat = format2(vatNet * 0.12);
59
52
  const isSpecial = discountRate > 0;
60
- const isAddVat = isAddVatAfterDiscount(discountType);
61
- const discounted = Number((discountRate * -vatNet).toFixed(2));
53
+ const isAddVat = isAddVatAfterDiscount(input.discountType);
54
+ const discounted = format2(discountRate / 100 * -vatNet);
62
55
  if (isSpecial) {
63
- vatNet = Number((vatNet + discounted).toFixed(2));
56
+ vatNet = format2(vatNet + discounted);
64
57
  if (!isAddVat) {
65
58
  vat = -vat;
66
59
  }
67
60
  }
68
61
  let serviceCharge = 0;
69
- if (serviceChargeRate > 0 && serviceChargeableTotalAmount > 0) {
70
- const scRate = serviceChargeRate / 100;
62
+ if (input.serviceChargeRate > 0 && input.serviceChargeableTotalAmount > 0) {
71
63
  if (isSpecial) {
72
- serviceCharge = Number(((vatNet + Math.abs(discounted)) * scRate).toFixed(2));
64
+ serviceCharge = format2((vatNet + Math.abs(discounted)) * scRate);
73
65
  } else {
74
- serviceCharge = Number((vatNet * scRate).toFixed(2));
66
+ serviceCharge = format2(vatNet * scRate);
75
67
  }
76
68
  }
77
- let totalAmount = vatNet + vat + serviceCharge + (togoableTotalAmount > 0 ? togoCharge : 0);
69
+ let totalAmount = vatNet + vat + serviceCharge + togoCharge;
78
70
  if (isSpecial && !isAddVat) {
79
71
  totalAmount -= vat;
80
72
  }
@@ -87,7 +79,7 @@ function calculateTotals(input) {
87
79
  net: subtotal,
88
80
  vatNet: voucherVatNet,
89
81
  vat: voucherVat,
90
- forBirDiscount: -(voucherType === "FIXED" ? voucherVatNet - Number(voucherRate.toFixed(2)) : voucherVatNet * Number(voucherRate.toFixed(2)))
82
+ forBirDiscount: -(input.voucherType === "FIXED" ? voucherVatNet - Number(voucherRate.toFixed(2)) : voucherVatNet * Number(voucherRate.toFixed(2)))
91
83
  },
92
84
  hasDiscount: discountRate > 0,
93
85
  discount: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qrwise/pos-calculations",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "Shared POS calculations for QR Wise POS",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",