From f4fa43d4686651b62f67e24d3698ea2324082f8e Mon Sep 17 00:00:00 2001
From: Rosyid Haryadi <rosyid_haryadi@protonmail.com>
Date: Sat, 30 Sep 2023 21:03:28 +0700
Subject: a more proper calculation including weekend, libur nasional not
 considered yet

---
 src/Result.jsx | 66 ++++++++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 50 insertions(+), 16 deletions(-)

(limited to 'src/Result.jsx')

diff --git a/src/Result.jsx b/src/Result.jsx
index 0ea4810..685f88d 100644
--- a/src/Result.jsx
+++ b/src/Result.jsx
@@ -1,5 +1,6 @@
 import {useEffect, useState} from "react";
-import entry from "./Entry.jsx";
+import {NumericFormat} from "react-number-format";
+import {Container, Typography} from "@mui/material";
 
 function Result({listEntry, baseSalary}) {
     const [totalOvertimePay, setTotalOvertimePay] = useState(0);
@@ -9,28 +10,61 @@ function Result({listEntry, baseSalary}) {
     }, [listEntry, baseSalary]);
 
     return (
-        <div>
-            Totalnye: Rp{totalOvertimePay}
-            {listEntry.map((entry) => {
-                return (
-                    <div key={entry.id}>
-                        <p>{entry.date.format('DD MMMM YYYY')} dapetnya Rp{calculatePerDay(entry, baseSalary)}</p>
-                    </div>
-                )
-            })}
-        </div>
+        <Container>
+            <Typography variant='h6'>
+                Total Overtime Pay: 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>*/}
+            {/*    )*/}
+            {/*})}*/}
+        </Container>
     )
 }
 
 function calculatePerDay(entry, baseSalary) {
     const hourlyPay = baseSalary / 173;
-    const timeDiff = entry.finish.diff(entry.start, 'hour', true);
-    let multiplier = 1.5; // Jam pertama
-    if (timeDiff > 1) {
-        multiplier += (timeDiff - 1) * 2; // Jam jam berikutnya
+    const overtimeDuration = entry.finish.diff(entry.start, 'hour', true);
+    let multiplier;
+
+    // TODO: hmm... Adakah cara yg lebih cerdas dari ini?
+    const multiplierMap = {
+        workDays: {
+            oneHour: 1.5,
+            moreHours: 3.5 // 1.5 + 2
+        },
+        holidays: {
+            eightHours: 2,
+            nineHours: 5, // 2 + 3
+            moreHours: 9 // 2 + 3 + 4
+        }
+    }
+
+    // Jelek bgt buset
+    if (entry.date.day() === 0 || entry.date.day() === 6) { // 0: Minggu, 6: Sabtu
+        // 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;
+        }
     }
 
-    return multiplier * hourlyPay;
+    return multiplier * overtimeDuration * hourlyPay;
 }
 
 function getOvertimePayTotal(listEntry, baseSalary) {
-- 
cgit v1.2.3-70-g09d2

