-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
130 lines (111 loc) · 3.23 KB
/
index.js
File metadata and controls
130 lines (111 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
let totalBill = 0;
const diskonNominal = 70500;
const serviceFee = 11200;
const kontribusi = {
"REIN : Sundubu Jjigae": 46900,
"REIN : Kimchi": 13900,
"PUT : Sundubu Jjigae": 46900,
"SEA : Topokki Maknae": 35900,
};
const assignedColors = {};
const colorCodes = [
"\x1b[31m",
"\x1b[32m",
"\x1b[33m",
"\x1b[34m",
"\x1b[35m",
"\x1b[36m",
"\x1b[37m",
];
let colorIndex = 0;
function getColorForName(name) {
if (!assignedColors[name]) {
assignedColors[name] = colorCodes[colorIndex];
colorIndex = (colorIndex + 1) % colorCodes.length;
}
return assignedColors[name];
}
function hitungSplitBill(
totalBill,
kontribusi,
diskonNominal = 0,
serviceFee = 0
) {
const totalKontribusi = Object.values(kontribusi).reduce(
(acc, val) => acc + val,
0
);
if (totalKontribusi < totalBill) {
return "Kontribusi tidak mencukupi total tagihan!";
}
const totalSetelahDiskon = totalBill - diskonNominal;
const totalAkhir = Math.max(0, totalSetelahDiskon + serviceFee);
const hasilPembayaran = {};
const totalPerOrang = {};
const totalPerOrangFinal = {};
for (const [orang, kontribusiOrang] of Object.entries(kontribusi)) {
const [nama] = orang.split(":").map((s) => s.trim());
if (!totalPerOrang[nama]) {
totalPerOrang[nama] = 0;
}
totalPerOrang[nama] += kontribusiOrang;
const persentase = kontribusiOrang / totalKontribusi;
hasilPembayaran[orang] = {
dibayar: kontribusiOrang,
harusDibayar: totalAkhir * persentase,
};
}
for (const [nama, kontribusi] of Object.entries(totalPerOrang)) {
const persentase = kontribusi / totalKontribusi;
totalPerOrangFinal[nama] = totalAkhir * persentase;
}
return {
totalBill,
diskonNominal,
serviceFee,
totalAkhir,
hasilPembayaran,
totalPerOrang,
totalPerOrangFinal,
};
}
Object.keys(kontribusi).forEach((v) => {
totalBill += kontribusi[v];
});
const hasil = hitungSplitBill(totalBill, kontribusi, diskonNominal, serviceFee);
console.log("\nDETAIL PEMBAYARAN:");
console.log(` Total Tagihan: Rp ${hasil.totalBill}`);
console.log(` Total Diskon: Rp ${hasil.diskonNominal}`);
console.log(` Service Fee: Rp ${hasil.serviceFee}`);
console.log(
` Total Setelah Diskon dan Service Fee: Rp ${hasil.totalAkhir.toFixed(2)}`
);
console.log("\nHASIL PEMBAYARAN:");
for (const [orang, detail] of Object.entries(hasil.hasilPembayaran)) {
const [nama] = orang.split(":").map((s) => s.trim());
const color = getColorForName(nama);
console.log(
` ${color}${orang} => Harga Rp ${detail.dibayar.toLocaleString(
"ID-id"
)}, Harus Dibayar Rp ${detail.harusDibayar.toLocaleString("ID-id")}\x1b[0m`
);
}
console.log("\nTOTAL TAGIHAN PER ORANG:");
for (const [nama, total] of Object.entries(hasil.totalPerOrang)) {
const color = getColorForName(nama);
console.log(
` ${color}${nama} => Total Kontribusi Rp ${total.toLocaleString(
"ID-id"
)}\x1b[0m`
);
}
console.log("\nTOTAL TAGIHAN AKHIR PER ORANG ( SETELAH DISKON ):");
for (const [nama, totalFinal] of Object.entries(hasil.totalPerOrangFinal)) {
const color = getColorForName(nama);
console.log(
` ${color}${nama} => Total Final Rp ${totalFinal.toLocaleString(
"ID-id"
)}\x1b[0m`
);
}
console.log("\n");