summaryrefslogtreecommitdiff
path: root/src/Result.jsx
blob: 3ba9a4e8923debf1ce37f440d5760644db044428 (plain)
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
import {useEffect, useState} from "react";
import {NumericFormat} from "react-number-format";
import {Container, Typography} from "@mui/material";

function Result({listEntry, baseSalary}) {
    const [totalOvertimePay, setTotalOvertimePay] = useState(0);

    useEffect(() => {
        setTotalOvertimePay(getOvertimePayTotal(listEntry, baseSalary));
    }, [listEntry, baseSalary]);

    if (totalOvertimePay < 0) {
        return (
                <Container>
                    <Typography variant='h5'>
                        Waktu selesai mendahului waktu mulai. Mohon diperbaiki input.
                    </Typography>
                </Container>
            )
    }

    return (
        <Container>
            <Typography variant='h5'>
                Total Upah Lembur: Rp<NumericFormat displayType="text" decimalScale={0} thousandSeparator={true}
                                                     value={totalOvertimePay}/>
            </Typography>
            <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 = 0;

    // TODO: find better way
    const multiplierMap = {
        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

    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 * hourlyPay;
}

function getOvertimePayTotal(listEntry, baseSalary) {
    let total = 0;
    listEntry.forEach((entry) => {
        total += calculatePerDay(entry, baseSalary)
    });
    return total;
}

export default Result;