<?php

function calculateSalary($salary, $user, $firstDayLast, $lastDayLast)
{

    $sql = "SELECT  
    COUNT(CASE WHEN `attendance_status` = 'Absent' THEN 1 ELSE NULL END) AS `absent`,
    COUNT(CASE WHEN (`attendance_status` = 'Half Day' or attendance_status='Set off with Week Off HD') THEN 1 ELSE NULL END) AS `half_day`,
    COUNT(CASE WHEN (`attendance_status` = 'Present' or attendance_status='Worked on Week Off' or attendance_status='Worked on Week Off HD' or attendance_status='Set off Half Day') THEN 1 ELSE NULL END) AS `present`,
    COUNT(CASE WHEN (`attendance_status` = 'Week Off' or attendance_status='Set off with Week Off') THEN 1 ELSE NULL END) AS `week_off`,
    COUNT(CASE WHEN `attendance_status` = 'Holiday' THEN 1 ELSE NULL END) AS `holiday`,
    COUNT(CASE WHEN `attendance_status` = 'Work From Home' THEN 1 ELSE NULL END) AS `work_from_home`
    FROM `attendance`
    WHERE `employee_id` = '$user[employee_id]'
    AND str_to_date(`which_month`, '%M %d %Y') BETWEEN '$firstDayLast' AND '$lastDayLast' ";
    $attendance_status = mysqli_fetch_assoc(mysqli_query($GLOBALS['db'], $sql));


    // days count between $firstDayLast and $lastDayLast
    // $salaryCalculationDays = (strtotime($lastDayLast) - strtotime($firstDayLast)) / (60 * 60 * 24) + 1;
    $salaryCalculationDays = date("t", strtotime($firstDayLast));

    // month Days
    $l = $attendance_status['absent'];
    $hd = $attendance_status['half_day'];
    $p = $attendance_status['present'];
    $wo = $attendance_status['week_off'];
    $ho = $attendance_status['holiday'];
    $wfh = $attendance_status['work_from_home'];
    $monthDay = $l + $hd + $p  + $wo + $ho + $wfh;

    $currentRangeDays = date("t", strtotime($firstDayLast));

    //total working days
    // $leaveOffer = $GLOBALS['finalLeaveOffer'];
    $totalLeave = ($hd * 0.5) + $l;
    // if ($GLOBALS['finalLeaveOffer'] > 0) {
    //     $GLOBALS['finalLeaveOffer'] = $GLOBALS['finalLeaveOffer'] - $totalLeave;
    //     if ($totalLeave > $leaveOffer) {
    //         $totalLeave = $totalLeave - $leaveOffer;
    //     } else {
    //         $totalLeave = 0;
    //     }
    // }

    $totalWorkingDay = $monthDay - $totalLeave;

    // calculate $totalWorkingDay according to $firstDayLast and $lastDayLast
    // $totalWorkingDay = $lastDayLast 

    // // according to 1 day all details
    // $basic_salary = (($salary['basic_salary'] / 12)) / $currentRangeDays;
    // $hra = (($salary['hra'] / 12)) / $currentRangeDays;
    // $books_per = (($salary['books_per'] / 12)) / $currentRangeDays;
    // $sp_allowance = (($salary['special_allowance'] / 12)) / $currentRangeDays;
    // $comp_epf = (($salary['company_epf'] / 12)) / $currentRangeDays;


    // //According to working days details
    // $working_basic_salary = ceil($basic_salary * $totalWorkingDay);
    // $working_hra = ceil($hra * $totalWorkingDay);
    // $working_books_per = ceil($books_per * $totalWorkingDay);
    // $working_sp_allowance = ceil($sp_allowance * $totalWorkingDay);


    // // according to count days
    // $basic_salary *= $monthDay;
    // $hra *= $monthDay;
    // $books_per *= $monthDay;
    // $sp_allowance *= $monthDay;
    // $comp_epf *= $monthDay;


    // netCTC, netPayable, leaves
    $leaves = [
        'Absent' => $l,
        'Half Day' => $hd,
        'Present' => $p,
        'Week Off' => $wo,
        'Holiday' => $ho,
        'Work From Home' => $wfh,
        'Total Leaves' => $totalLeave,
        'Total Working Days' => $totalWorkingDay,
        'Month Days' => $monthDay,
        'Current Range Days' => $currentRangeDays,
        'Salary Calculation Days' => $salaryCalculationDays,
        'firstDayLast' => $firstDayLast,
        'lastDayLast' => $lastDayLast,
    ];

    $fixed_pay = !empty($salary['fixed']) ? json_decode($salary['fixed'], true) : [];
    $add_on = !empty($salary['add_on']) ? json_decode($salary['add_on'], true) : [];
    $deduction = !empty($salary['deduction']) ? json_decode($salary['deduction'], true) : [];
    // calculate according to 1 day all details
    foreach ($fixed_pay as $key => $value) {
        if ($value['yearly_amount'] != 0) {
            $fixed_pay[$key]['per_day_amount'] = ($value['yearly_amount'] / 12) / $currentRangeDays;
            $fixed_pay[$key]['monthly_amount'] = $fixed_pay[$key]['per_day_amount'] * $salaryCalculationDays;
            $fixed_pay[$key]['working_amount'] = $fixed_pay[$key]['per_day_amount'] * $totalWorkingDay;
            $fixed_pay[$key]['monthly_amount_payable'] = $fixed_pay[$key]['per_day_amount'] * $totalWorkingDay;
        }
    }
    foreach ($add_on as $key => $value) {
        if ($value['yearly_amount'] != 0) {
            if($value['title'] == 'ESIC Employer Share @ 3.25% of (A)'){
                $add_on[$key]['per_day_amount'] = ($value['yearly_amount'] / 12) / $currentRangeDays;
                $add_on[$key]['monthly_amount'] = $add_on[$key]['per_day_amount'] * $salaryCalculationDays;
                $add_on[$key]['working_amount'] = $add_on[$key]['per_day_amount'] * $totalWorkingDay;
                $add_on[$key]['monthly_amount_payable'] = $add_on[$key]['per_day_amount'] * $totalWorkingDay;
            }else{
                $add_on[$key]['monthly_amount'] = ($value['yearly_amount'] / 12);
            }
        }
    }
    foreach ($deduction as $key => $value) {
        if ($value['yearly_amount'] != 0) {
            if($value['title'] == 'ESIC Employee Share @ 0.75% of (A)'){
                $deduction[$key]['per_day_amount'] = ($value['yearly_amount'] / 12) / $currentRangeDays;
                $deduction[$key]['monthly_amount'] = $deduction[$key]['per_day_amount'] * $salaryCalculationDays;
                $deduction[$key]['working_amount'] = $deduction[$key]['per_day_amount'] * $totalWorkingDay;
                $deduction[$key]['monthly_amount_payable'] = $deduction[$key]['per_day_amount'] * $totalWorkingDay;
            }else{
                $deduction[$key]['monthly_amount'] = ($value['yearly_amount'] / 12);
            }
        }
    }
    // echo "<script>console.log(".json_encode($fixed_pay).");</script>";
    
    
    $data = [
        'leaves' => $leaves,
        'fixed_pay' => $fixed_pay,
        'add_on' => $add_on,
        'deduction' => $deduction,
    ];
    // echo "<script>console.log(".json_encode($data).");</script>";


    return $data;

    // return array(
    //     // 'l' => $l, 'hd' => $hd, 'p' => $p, 'wo' => $wo, 'ho' => $ho, 'wfh' => $wfh, 'totalLeave' => $totalLeave, 
    //     // 'basic_salary' => $basic_salary, 'hra' => $hra, 'books_per' => $books_per, 'sp_allowance' => $sp_allowance, 'comp_epf' => $comp_epf, 
    //     // 'working_basic_salary' => $working_basic_salary, 'working_hra' => $working_hra, 'working_books_per' => $working_books_per, 'working_sp_allowance' => $working_sp_allowance, 
    //     // 'totalWorkingDay' => $totalWorkingDay, 
    //     // 'month_day' => $monthDay,
    // );
}