From a4f1b82242b37cc96047d41af04f9f689a8232bd Mon Sep 17 00:00:00 2001
From: Rosyid Haryadi <rosyid_haryadi@protonmail.com>
Date: Sun, 1 Oct 2023 14:10:48 +0700
Subject: Fix calculation logic

---
 src/Result.jsx | 84 +++++++++++++++++++++++++++++++---------------------------
 1 file changed, 45 insertions(+), 39 deletions(-)

(limited to 'src/Result.jsx')

diff --git a/src/Result.jsx b/src/Result.jsx
index fac2b18..dd977c5 100644
--- a/src/Result.jsx
+++ b/src/Result.jsx
@@ -11,62 +11,68 @@ function Result({listEntry, baseSalary}) {
 
     return (
         <Container>
-            <Typography variant='h6'>
+            <Typography variant='h5'>
                 Total Upah Lembur: Rp<NumericFormat displayType="text" decimalScale={0} thousandSeparator={true}
                                                      value={totalOvertimePay}/>
             </Typography>
-            {/*{listEntry.map((entry) => {*/}
-            {/*    return (*/}
-            {/*        <div key={entry.id}>*/}
-            {/*            <p>*/}
-            {/*                {entry.date.format('DD MMMM YYYY')} dapetnya Rp<NumericFormat displayType="text" decimalScale={0} thousandSeparator={true} value={calculatePerDay(entry, baseSalary)} />*/}
-            {/*            </p>*/}
-            {/*        </div>*/}
-            {/*    )*/}
-            {/*})}*/}
+            <Typography>Rincian</Typography>
+            { listEntry.map((entry) => {
+                return (
+                    <div key={entry.id}>
+                        <Typography>
+                            {entry.date.format('DD MMMM YYYY')}: Rp<NumericFormat displayType="text" decimalScale={0} thousandSeparator={true} value={calculatePerDay(entry, baseSalary)} />
+                        </Typography>
+                    </div>
+                )
+            }) }
         </Container>
     )
 }
 
+function segmentTime(duration, segments) {
+    const segmentSum = segments.reduce((partialSum, a) => partialSum + a, 0);
+    const segmentsFilled = [...segments, 24 - segmentSum];
+    const segmentedHours = [];
+
+    for (const[index, segmentDuration] of segmentsFilled.entries()) {
+        if (index < segmentsFilled.length - 1) {
+            const hours = Math.min(duration, segmentDuration);
+            segmentedHours.push(hours);
+            duration -= hours;
+        } else {
+            segmentedHours.push(duration);
+        }
+    }
+    return segmentedHours;
+}
+
 function calculatePerDay(entry, baseSalary) {
     const hourlyPay = baseSalary / 173;
     const overtimeDuration = entry.finish.diff(entry.start, 'hour', true);
-    let multiplier;
+    let multiplier = 0;
 
-    // TODO: hmm... Adakah cara yg lebih cerdas dari ini?
+    // TODO: find better way
     const multiplierMap = {
-        workDays: {
-            oneHour: 1.5,
-            moreHours: 3.5 // 1.5 + 2
-        },
-        holidays: {
-            eightHours: 2,
-            nineHours: 5, // 2 + 3
-            moreHours: 9 // 2 + 3 + 4
-        }
+        workDays: [
+            1.5, // jam pertama
+            2
+        ],
+        holidays: [
+            2, // 8 jam pertama
+            3, // jam ke 9
+            4
+        ]
     }
 
     const isWeekend = entry.date.day() === 0 || entry.date.day() === 6; // 0: Minggu, 6: Sabtu
-    // Jelek bgt buset
-    if (isWeekend) {
-        // Kerja di hari libur. Temennya Yohana wkwk
-        if (0 < overtimeDuration <= 8) {
-            multiplier = multiplierMap.holidays.eightHours;
-        } else if (8 < overtimeDuration <= 9){
-            multiplier = multiplierMap.holidays.nineHours;
-        } else {
-            multiplier = multiplierMap.holidays.moreHours;
-        }
-    } else {
-        // Lembur hari kerja
-        if (0 < overtimeDuration <= 8) {
-            multiplier = multiplierMap.workDays.oneHour;
-        } else {
-            multiplier = multiplierMap.workDays.moreHours;
-        }
+
+    const segmentedDuration = segmentTime(overtimeDuration, [1]);
+    const usedMap = isWeekend ? multiplierMap.holidays : multiplierMap.workDays;
+    for (let i = 0; i < segmentedDuration.length; i++) {
+        multiplier += segmentedDuration[i] * usedMap[i];
     }
 
-    return multiplier * overtimeDuration * hourlyPay;
+    return multiplier * hourlyPay;
 }
 
 function getOvertimePayTotal(listEntry, baseSalary) {
-- 
cgit v1.2.3-70-g09d2

