@qrwise/pos-calculations 0.0.1

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 ADDED
@@ -0,0 +1,100 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/index.ts
20
+ var index_exports = {};
21
+ __export(index_exports, {
22
+ calculateTotals: () => calculateTotals
23
+ });
24
+ module.exports = __toCommonJS(index_exports);
25
+
26
+ // src/calculate-totals.ts
27
+ function round(value) {
28
+ return Number(value.toFixed(2));
29
+ }
30
+ function isVatExemptDiscount(discountType) {
31
+ return ["SENIOR", "PWD", "NAAC"].includes(String(discountType || "").toUpperCase());
32
+ }
33
+ function calculateTotals(input) {
34
+ const {
35
+ discountableTotalAmount,
36
+ nonDiscountableTotalAmount,
37
+ serviceChargeableTotalAmount,
38
+ voucherType,
39
+ voucherRate = 0,
40
+ discountType,
41
+ discountRate = 0,
42
+ vatRate,
43
+ serviceChargeRate,
44
+ togoCharge,
45
+ diningOption = "FOR_HERE"
46
+ } = input;
47
+ const grossAmount = round(discountableTotalAmount + nonDiscountableTotalAmount);
48
+ let voucherDiscount = 0;
49
+ if (voucherType === "FIXED") {
50
+ voucherDiscount = Math.min(voucherRate, grossAmount);
51
+ }
52
+ if (voucherType === "PERCENTAGE") {
53
+ voucherDiscount = grossAmount * (voucherRate / 100);
54
+ }
55
+ voucherDiscount = round(voucherDiscount);
56
+ const subtotalAfterVoucher = round(grossAmount - voucherDiscount);
57
+ const vatDivisor = 1 + vatRate / 100;
58
+ let vatNet = round(subtotalAfterVoucher / vatDivisor);
59
+ let vat = round(subtotalAfterVoucher - vatNet);
60
+ let discount = 0;
61
+ if (discountType) {
62
+ const discountableVatNet = round(discountableTotalAmount / vatDivisor);
63
+ discount = round(discountableVatNet * (discountRate / 100));
64
+ vatNet = round(vatNet - discount);
65
+ if (isVatExemptDiscount(discountType)) {
66
+ vat = 0;
67
+ }
68
+ }
69
+ let serviceCharge = 0;
70
+ if (diningOption === "FOR_HERE" && serviceChargeRate > 0) {
71
+ serviceCharge = round(serviceChargeableTotalAmount * (serviceChargeRate / 100));
72
+ }
73
+ const finalTogoCharge = diningOption === "TO_GO" ? togoCharge : 0;
74
+ const totalAmount = round(Math.max(0, vatNet + vat + serviceCharge + finalTogoCharge));
75
+ return {
76
+ grossAmount,
77
+ voucherDiscount: -voucherDiscount,
78
+ subtotalAfterVoucher,
79
+ vatNet,
80
+ vat,
81
+ discount: -discount,
82
+ serviceCharge,
83
+ togoCharge: finalTogoCharge,
84
+ totalAmount,
85
+ bir: {
86
+ vatSales: vat > 0 ? vatNet + Math.abs(discount) : 0,
87
+ vatExemptSales: vat === 0 ? vatNet + Math.abs(discount) : 0,
88
+ vatZeroRatedSales: 0,
89
+ nonTaxableSales: 0,
90
+ vat,
91
+ discount,
92
+ serviceCharge,
93
+ amountPayable: totalAmount
94
+ }
95
+ };
96
+ }
97
+ // Annotate the CommonJS export names for ESM import in node:
98
+ 0 && (module.exports = {
99
+ calculateTotals
100
+ });
@@ -0,0 +1,41 @@
1
+ type DiningOption = "FOR_HERE" | "TO_GO";
2
+ type VoucherType = "FIXED" | "PERCENTAGE";
3
+ type DiscountType = "SENIOR" | "PWD" | "NAAC" | "SOLO" | "MOV";
4
+ interface CalculateTotalsInput {
5
+ discountableTotalAmount: number;
6
+ nonDiscountableTotalAmount: number;
7
+ serviceChargeableTotalAmount: number;
8
+ voucherType?: VoucherType | null;
9
+ voucherRate?: number;
10
+ discountType?: DiscountType | null;
11
+ discountRate?: number;
12
+ vatRate: number;
13
+ serviceChargeRate: number;
14
+ togoCharge: number;
15
+ diningOption?: DiningOption;
16
+ }
17
+ interface CalculateTotalsResult {
18
+ grossAmount: number;
19
+ voucherDiscount: number;
20
+ subtotalAfterVoucher: number;
21
+ vatNet: number;
22
+ vat: number;
23
+ discount: number;
24
+ serviceCharge: number;
25
+ togoCharge: number;
26
+ totalAmount: number;
27
+ bir: {
28
+ vatSales: number;
29
+ vatExemptSales: number;
30
+ vatZeroRatedSales: number;
31
+ nonTaxableSales: number;
32
+ vat: number;
33
+ discount: number;
34
+ serviceCharge: number;
35
+ amountPayable: number;
36
+ };
37
+ }
38
+
39
+ declare function calculateTotals(input: CalculateTotalsInput): CalculateTotalsResult;
40
+
41
+ export { type CalculateTotalsInput, type CalculateTotalsResult, type DiningOption, type DiscountType, type VoucherType, calculateTotals };
@@ -0,0 +1,41 @@
1
+ type DiningOption = "FOR_HERE" | "TO_GO";
2
+ type VoucherType = "FIXED" | "PERCENTAGE";
3
+ type DiscountType = "SENIOR" | "PWD" | "NAAC" | "SOLO" | "MOV";
4
+ interface CalculateTotalsInput {
5
+ discountableTotalAmount: number;
6
+ nonDiscountableTotalAmount: number;
7
+ serviceChargeableTotalAmount: number;
8
+ voucherType?: VoucherType | null;
9
+ voucherRate?: number;
10
+ discountType?: DiscountType | null;
11
+ discountRate?: number;
12
+ vatRate: number;
13
+ serviceChargeRate: number;
14
+ togoCharge: number;
15
+ diningOption?: DiningOption;
16
+ }
17
+ interface CalculateTotalsResult {
18
+ grossAmount: number;
19
+ voucherDiscount: number;
20
+ subtotalAfterVoucher: number;
21
+ vatNet: number;
22
+ vat: number;
23
+ discount: number;
24
+ serviceCharge: number;
25
+ togoCharge: number;
26
+ totalAmount: number;
27
+ bir: {
28
+ vatSales: number;
29
+ vatExemptSales: number;
30
+ vatZeroRatedSales: number;
31
+ nonTaxableSales: number;
32
+ vat: number;
33
+ discount: number;
34
+ serviceCharge: number;
35
+ amountPayable: number;
36
+ };
37
+ }
38
+
39
+ declare function calculateTotals(input: CalculateTotalsInput): CalculateTotalsResult;
40
+
41
+ export { type CalculateTotalsInput, type CalculateTotalsResult, type DiningOption, type DiscountType, type VoucherType, calculateTotals };
package/dist/index.js ADDED
@@ -0,0 +1,74 @@
1
+ // src/calculate-totals.ts
2
+ function round(value) {
3
+ return Number(value.toFixed(2));
4
+ }
5
+ function isVatExemptDiscount(discountType) {
6
+ return ["SENIOR", "PWD", "NAAC"].includes(String(discountType || "").toUpperCase());
7
+ }
8
+ function calculateTotals(input) {
9
+ const {
10
+ discountableTotalAmount,
11
+ nonDiscountableTotalAmount,
12
+ serviceChargeableTotalAmount,
13
+ voucherType,
14
+ voucherRate = 0,
15
+ discountType,
16
+ discountRate = 0,
17
+ vatRate,
18
+ serviceChargeRate,
19
+ togoCharge,
20
+ diningOption = "FOR_HERE"
21
+ } = input;
22
+ const grossAmount = round(discountableTotalAmount + nonDiscountableTotalAmount);
23
+ let voucherDiscount = 0;
24
+ if (voucherType === "FIXED") {
25
+ voucherDiscount = Math.min(voucherRate, grossAmount);
26
+ }
27
+ if (voucherType === "PERCENTAGE") {
28
+ voucherDiscount = grossAmount * (voucherRate / 100);
29
+ }
30
+ voucherDiscount = round(voucherDiscount);
31
+ const subtotalAfterVoucher = round(grossAmount - voucherDiscount);
32
+ const vatDivisor = 1 + vatRate / 100;
33
+ let vatNet = round(subtotalAfterVoucher / vatDivisor);
34
+ let vat = round(subtotalAfterVoucher - vatNet);
35
+ let discount = 0;
36
+ if (discountType) {
37
+ const discountableVatNet = round(discountableTotalAmount / vatDivisor);
38
+ discount = round(discountableVatNet * (discountRate / 100));
39
+ vatNet = round(vatNet - discount);
40
+ if (isVatExemptDiscount(discountType)) {
41
+ vat = 0;
42
+ }
43
+ }
44
+ let serviceCharge = 0;
45
+ if (diningOption === "FOR_HERE" && serviceChargeRate > 0) {
46
+ serviceCharge = round(serviceChargeableTotalAmount * (serviceChargeRate / 100));
47
+ }
48
+ const finalTogoCharge = diningOption === "TO_GO" ? togoCharge : 0;
49
+ const totalAmount = round(Math.max(0, vatNet + vat + serviceCharge + finalTogoCharge));
50
+ return {
51
+ grossAmount,
52
+ voucherDiscount: -voucherDiscount,
53
+ subtotalAfterVoucher,
54
+ vatNet,
55
+ vat,
56
+ discount: -discount,
57
+ serviceCharge,
58
+ togoCharge: finalTogoCharge,
59
+ totalAmount,
60
+ bir: {
61
+ vatSales: vat > 0 ? vatNet + Math.abs(discount) : 0,
62
+ vatExemptSales: vat === 0 ? vatNet + Math.abs(discount) : 0,
63
+ vatZeroRatedSales: 0,
64
+ nonTaxableSales: 0,
65
+ vat,
66
+ discount,
67
+ serviceCharge,
68
+ amountPayable: totalAmount
69
+ }
70
+ };
71
+ }
72
+ export {
73
+ calculateTotals
74
+ };
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@qrwise/pos-calculations",
3
+ "version": "0.0.1",
4
+ "description": "Shared POS calculations for QR Wise POS",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "files": [
10
+ "dist",
11
+ "README.md"
12
+ ],
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js",
17
+ "require": "./dist/index.cjs"
18
+ }
19
+ },
20
+ "scripts": {
21
+ "build": "tsup src/index.ts --format esm,cjs --dts --clean",
22
+ "prepublishOnly": "npm run build"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "devDependencies": {
28
+ "tsup": "^8.0.0",
29
+ "typescript": "^5.0.0"
30
+ }
31
+ }