<?php
include("../../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}
extract($_REQUEST);
$actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if ($datefrom == "") {
    $datefrom = date('Y-m-d', strtotime("-30 days"));
} else {
    $datefrom = date("Y-m-d", strtotime($datefrom));
}
if ($dateto == "") {
    $dateto = date('Y-m-d');
} else {
    $dateto = date("Y-m-d", strtotime($dateto));
}
$datefrom = date("Y-m-d", strtotime($datefrom));
$dateto = date("Y-m-d", strtotime($dateto));

$qryDate = date("Y-m-d", strtotime($datefrom));
$qryTo = date("Y-m-d", strtotime($dateto));
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('../includes/inc_head.php'); ?>
    <style>
        .table thead > tr > th, .table tbody > tr > th, .table tfoot > tr > th, .table thead > tr > td, .table tbody > tr > td, .table tfoot > tr > td {
            padding: 2px;
        }

        .table > thead > tr > th, .table > tbody > tr > th, .table > tfoot > tr > th, .table > thead > tr > td, .table > tbody > tr > td, .table > tfoot > tr > td {
            border-top: none;
        }
        th{
            text-align: center;
        }
    </style>
</head>
<body>
<section id="container">
    <?php include('../includes/inc_top.php'); ?>
    <section id="main-content">
        <section class="wrapper">
            <div class="panel">
                <div class="panel-body">
                    <div class="">
                        <div class="span12">
                            <div class="">
                                <h2>Attendance Report</h2>
                            </div>
                            <?php if (isset($_GET['sucess'])) { ?>
                                <div class="alert alert-success  fade in">
                                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                                    <strong>*</strong> Successfully Updated.
                                </div>
                            <?php } ?>
                            <?php if (isset($_GET['error'])) { ?>
                                <div class="alert alert-error  fade in">
                                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                                    Error Ocurred. Try Again Later.
                                </div>
                            <?php } ?>
                            <?php if (isset($_GET['availed'])) { ?>
                                <div class="alert alert-error  fade in">
                                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                                    Your leaves limit is over. Contact to admin
                                </div>
                            <?php } ?>
                            <script>
                                $(function () {
                                    $("#datepicker").datepicker({dateFormat: 'yy-mm-dd'});
                                    $("#datepicker1").datepicker({dateFormat: 'yy-mm-dd'});
                                });
                            </script>
                            <table class="table">
                                <form action="" method="get" name="search"
                                      id="searchfrm">
                                    <tr style="border-top: 1px solid #ddd;">
                                        <td class="col-lg-4">Month<br>
                                            <select class="form-control" name="month" id="month">
                                                <?php
                                                if ($month) {
                                                    $monthcount = $month;
                                                } else {
                                                    $monthcount = date('F');
                                                }
                                                for ($m = 1; $m <= 12; ++$m) {
                                                    echo '<option value="' . date('F', mktime(0, 0, 0, $m, 1)) . '"';
                                                    if (date('F', mktime(0, 0, 0, $m, 1)) == trim($monthcount)) {
                                                        echo ' selected';
                                                        $monthCheck = $m;
                                                    }
                                                    echo '>' . date('F', mktime(0, 0, 0, $m, 1)) . '</option>';
                                                }
                                                ?>
                                            </select>
                                            <input type="hidden" name="view" id="view" class="form-control"/>
                                        </td>
                                        <td class="col-lg-4">Year<br>
                                            <select class="form-control" name="year" id="year">
                                                <?php
                                                if ($year) {
                                                    $currentYear = $year;
                                                } else {
                                                    $currentYear = date('Y');
                                                }
                                                for ($i = 1; $i <= 10; $i++) {
                                                    $yearcount = 2016 + $i;
                                                    echo '<option value="' . $yearcount . '"';
                                                    if ($currentYear == $yearcount) {
                                                        echo ' selected';
                                                    }
                                                    echo '>' . $yearcount . '</option>';
                                                }
                                                ?>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>Employee Name<br>
                                            <?php
                                            $qryEmployee = EmployeeSetup::GetEmployeeSetupList();
                                            if ($qryEmployee) {
                                                echo '<select id="employee_id" name="employee_id" class="form-control">';
                                                echo '<option value="">All</option>';
                                                foreach ($qryEmployee as $qery) {
                                                    echo '<option value=' . $qery->id;
                                                    if ($qery->id == $employee_id) {
                                                        echo ' selected ';
                                                    }
                                                    echo '>' . $qery->name . '</option>';
                                                }
                                                echo '</select>';
                                            }
                                            ?>
                                        </td>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td colspan="2" class="col-lg-4">
                                            <button type="submit" class="btn btn-primary btn-large"
                                                    style="float:left;">
                                                Search
                                            </button>
                                            <?php if (isset($_GET['view'])) { ?>
                                            <button
                                                onclick="exportTableToCSV('payment-<?= date('d-m-Y-h-i-a') ?>.csv')"
                                                class="btn btn-primary btn-large"
                                                style="float:left; margin-left: 15px;">
                                                Export CSV
                                            </button>

                        </div>

                        <?php } ?>
                        </td>
                        </tr>
                        </form>
                        <tr>
                            <td colspan="2" class="col-lg-4">
                                <a style="margin-left:30px; display: block; cursor:pointer;font-size: 10px;padding: 1px;"
                                   data-toggle="modal"
                                   href="#myModalPaid"
                                   title="Download">
                                    <button
                                        class="pull-right btn btn-primary btn-large"
                                        style=" margin-left: 15px;">
                                        Download
                                    </button>
                                </a>
                                <a style="margin-left:30px; display: block; cursor:pointer;font-size: 10px;padding: 1px;"
                                   data-toggle="modal"
                                   href="#myModal"
                                   title="Import">
                                    <button
                                        class="pull-right btn btn-primary btn-large"
                                        style=" margin-left: 15px;">
                                        Import
                                    </button>
                                </a>

                                <!-- Modal -->
                                <div id="myModalPaid" class="modal fade"
                                     role="dialog">
                                    <div class="modal-dialog">
                                        <form action="http://192.168.0.20/form/Download" method="post" name="mainform">
                                            <!-- Modal content-->
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <button type="button" class="close"
                                                            data-dismiss="modal">&times;</button>
                                                    <h4 class="modal-title">Download Data From Device</h4>
                                                </div>
                                                <div class="modal-body">
                                                    <p>Please select the period to import data</p>
                                                                <span class="col-lg-4">Date From<br>
                                                                    <input type="text" name="sdate" id="datepicker"
                                                                           placeholder="Date From"
                                                                           value="<?= $datefrom ?>"
                                                                           class="form-control"/>
                                                                </span>
                                                                <span class="col-lg-4"> Date To<br>
                                                                    <input type="text" name="edate" id="datepicker1"
                                                                           placeholder="Date To"
                                                                           value="<?= $dateto ?>" class="form-control"/>
                                                                </span>
                                                    <?php
                                                    $maxEmp = EmployeeSetup::GetMaxMachineIdFromEmployee();
                                                    for ($a = 1; $a <= $maxEmp->machineid; $a++) {
                                                        echo '<input type="hidden" checked="" name="uid" value="' . $a . '">';
                                                    }
                                                    ?>
                                                    <br><br><br>
                                                </div>

                                                <div class="modal-footer">
                                                    <input type="submit" class="btn btn-danger" name="submit"
                                                           id="submit" value="Download"/>
                                                    <button type="button"
                                                            class="btn btn-danger"
                                                            data-dismiss="modal">Close
                                                    </button>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>

                                <!-- Modal -->
                                <div id="myModal" class="modal fade"
                                     role="dialog">
                                    <div class="modal-dialog">
                                        <form action="test.php" method="post" name="testform"
                                              enctype="multipart/form-data">
                                            <!-- Modal content-->
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <button type="button" class="close"
                                                            data-dismiss="modal">&times;</button>
                                                    <h4 class="modal-title">Import Data</h4>
                                                </div>
                                                <div class="modal-body">
                                                    <p>Please select file to import</p>
                                                                <span class="col-lg-4">Import File<br>
                                                                    <input class="form-control" type="file"
                                                                           name="importfile"
                                                                           id="importfile"/>
                                                                </span>
                                                    <br><br><br>
                                                </div>

                                                <div class="modal-footer">
                                                    <input type="submit" class="btn btn-danger" name="submit"
                                                           id="submit" value="Download"/>
                                                    <button type="button"
                                                            class="btn btn-danger"
                                                            data-dismiss="modal">Close
                                                    </button>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        </table>
                        <hr><?php
                        if (isset($_GET['view']) && empty($employee_id)) {
                            ?>
                            <div>
                                <table style="text-align:center;" class="display table table-bordered table-striped">
                                    <tbody>
                                    <tr style="border: 1px solid #666;">
                                        <th>Employee</th>
                                        <?php
                                        if ($year) {
                                            $year = $year;
                                        } else {
                                            $year = date('Y');
                                        }
                                        for ($d = 1; $d <= 31; $d++) {
                                            $time = mktime(12, 0, 0, $monthCheck, $d, $year);
                                            if (date('m', $time) == $monthCheck)
                                                echo '<th>' . date('d', $time) . '</th>';
                                        }
                                        ?>
                                        <th>Late</th>
                                        <th>Absent</th>
                                        <th>AL/CL</th>
                                        <th>Holiday</th>
                                    </tr>
                                    <?php
                                    $getYearMonth = date('Y-m-00', strtotime($year . '-' . $monthCheck . '-01'));
                                    $holidayFilter = ' WHERE STR_TO_DATE(ac_date, "%Y-%m") = "' . $getYearMonth . '"';
                                    $holidays = HolidaySetup::GetHolidaySetupList($holidayFilter);

                                    $filterEmployee = " WHERE status = 'active'";
                                    $qry = EmployeeSetup::GetEmployeeSetupList($filterEmployee);
                                    if ($qry) {
                                        foreach ($qry AS $qery) {
                                            $lateCount = 0;
                                            $absentCount = 0;
                                            $holidayCount = 0;
                                            $leavesCount = 0;
                                            echo '<tr>';
                                            echo '<td>' . $qery->name . '</td>';
                                            for ($d = 1; $d <= 31; $d++) {
                                                $getDate = date('Y-m-d', strtotime($year . '-' . $monthCheck . '-' . $d));

                                                $filterAtt = ' AND (STR_TO_DATE(time_in, "%Y-%m-%d") = "' . $getDate . '" )';
                                                $present = '';
                                                $color = '#000';
                                                $attendanceEmployee = '';
                                                $attendanceEmployee = Attendance::GetAttendanceByEmployeeId($qery->id, $filterAtt);
                                                $background = '';
                                                if($attendanceEmployee[0]->shift == 'day'){
                                                    if ($holidays) {
                                                        foreach ($holidays AS $holidaysRow) {
                                                            if ($getDate == $holidaysRow->ac_date) {
                                                                $background = '#FFA500';
                                                                $holidayCount++;
                                                            }
                                                        }
                                                    }
                                                }else if($attendanceEmployee[0]->shift == 'night'){
                                                    if ($holidays) {
                                                        foreach ($holidays AS $holidaysRow) {
                                                            if ($getDate == date("Y-m-d", strtotime('-1 day', strtotime($holidaysRow->ac_date)))) {
                                                                $background = '#FFA500';
                                                                $holidayCount++;
                                                            }
                                                        }
                                                    }
                                                }else{
                                                    if ($holidays) {
                                                        foreach ($holidays AS $holidaysRow) {
                                                            if ($getDate == $holidaysRow->ac_date) {
                                                                $background = '#FFA500';
                                                                $holidayCount++;
                                                            }
                                                        }
                                                    }
                                                }
                                                //For Check Annual Leave
                                                $LeaveFilter = ' WHERE employee_id = "' . $qery->id . '" AND STR_TO_DATE(leave_date, "%Y-%m-%d") = "' . $getDate . '"';
                                                $Leaves = LeaveSetup::GetLeaveSetupList($LeaveFilter);
                                                $LeavesAvailed = '';
                                                if ($Leaves) {
                                                    $LeavesAvailed = $Leaves[0]->leave_date;
                                                }
                                                if($getDate == $LeavesAvailed){
                                                    $present = 'LE';
                                                    $background = '#9F9';
                                                    $leavesCount++;
                                                }else{
                                                    if (!empty($attendanceEmployee[0]->time_in) && $attendanceEmployee[0]->time_in != '0000-00-00 00:00:00') {
                                                        //echo date('Y-m-d', strtotime($attendanceEmployee[0]->time_in));
                                                        //echo date('Y-m-d').'<br>';
                                                        if ($attendanceEmployee[0]->shift == 'day') {
                                                            if (!empty($attendanceEmployee[0]->late_grace_time)) {
                                                                $startTime = date("Y-m-d", strtotime('-' . $attendanceEmployee[0]->late_grace_time . ' min', strtotime($attendanceEmployee[0]->time_in)));
                                                            } else {
                                                                $startTime = date('Y-m-d', strtotime($attendanceEmployee[0]->time_in));
                                                            }
                                                            if ($startTime <= date('Y-m-d')) {
                                                                if ($attendanceEmployee) {
                                                                    //echo date('H:i:s', strtotime($attendanceEmployee[0]->time_in)) . '<br>';

                                                                    if (date('H:i:s', strtotime($startTime)) <= $attendanceEmployee[0]->shift_start_time) {
                                                                        $present = 'P';
                                                                        $color = '#00FF00';
                                                                    } else {
                                                                        $present = 'L';
                                                                        $color = '#996600';
                                                                        $lateCount++;
                                                                    }
                                                                } else {
                                                                    if (!empty($background)) {
                                                                        $present = 'H';
                                                                    } else {
                                                                        $color = '#FF0000';
                                                                        $present = 'A';
                                                                        $absentCount++;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        else if ($attendanceEmployee[0]->shift == 'night') {
                                                            $param = 1;
//                                                        if($attendanceEmployee[0]->employee_id == '10') {
//                                                            $itrCheck = 0;
//                                                            for ($itr = 0; $itr <= count($attendanceEmployee); $itr++) {
//                                                                if (date('Y-m-d H:i:s', strtotime($attendanceEmployee[$itr]->time_in)) > date('Y-m-d 17:00:00', strtotime($attendanceEmployee[$itr]->time_in))) {
//                                                                    $param = $itrCheck;
//                                                                    $itrCheck++;
//                                                                } else {
//                                                                    echo date('Y-m-d H:i:s', strtotime($attendanceEmployee[$itr]->time_in));
//                                                                    echo date('Y-m-d 17:00:00', strtotime($attendanceEmployee[$itr]->time_in));
//                                                                    echo 'No Record Found <br>';
//                                                                }
//                                                            }
//                                                        }
                                                            if (!empty($attendanceEmployee[$param]->late_grace_time)) {
                                                                $startTime = date("Y-m-d H:i:s", strtotime('-' . $attendanceEmployee[$param]->late_grace_time . ' min', strtotime($attendanceEmployee[$param]->time_in)));
                                                            } else {
                                                                $startTime = date('Y-m-d H:i:s', strtotime($attendanceEmployee[$param]->time_in));
                                                            }
                                                            if ($startTime <= date('Y-m-d 19:00:00')) {
                                                                if ($attendanceEmployee) {
//                                                                if($attendanceEmployee[0]->employee_id == '10'){
//                                                                    echo '<pre>';
//                                                                    print_r($attendanceEmployee);
//                                                                    echo '</pre>';
////                                                                    echo $startTime.' = ';
////                                                                    echo date('H:i:s', strtotime($attendanceEmployee[1]->time_in)) . ' - ';
////                                                                    echo $attendanceEmployee[1]->shift_start_time."<br>";
//                                                                }
                                                                    if (date('H:i:s', strtotime($startTime)) <= $attendanceEmployee[$param]->shift_start_time) {
                                                                        $present = 'P';
                                                                        $color = '#00FF00';
                                                                    } else {
                                                                        $present = 'L';
                                                                        $color = '#996600';
                                                                        $lateCount++;
                                                                    }
                                                                } else {
                                                                    if (!empty($background)) {
                                                                        $present = 'H';
                                                                    } else {
                                                                        $color = '#FF0000';
                                                                        $present = 'A';
                                                                        $absentCount++;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    } else if ($getDate <= date('Y-m-d')) {
                                                        if (!empty($background)) {
                                                            $present = 'H';
                                                        } else {
                                                            $present = 'A';
                                                            $color = '#FF0000';
                                                            $absentCount++;
                                                        }
                                                    } else {
                                                        if (!empty($background)) {
                                                            $present = 'H';
                                                        } else {
                                                            $present = '-';
                                                        }
                                                    }
                                                }

                                                $time = mktime(12, 0, 0, $monthCheck, $d, $year);
                                                if (date('m', $time) == $monthCheck) {
                                                    echo '<td style="background-color:' . $background . '; font-weight:bold; color:' . $color . '; " >' . $present . '</td>';
                                                    //echo '<td>' . date('d', $time) . 'd' . $present . '</td>';
                                                }
                                            }
                                            echo '<td>' . $lateCount . '</td>';
                                            echo '<td>' . $absentCount . '</td>';
                                            echo '<td>' . $leavesCount . '</td>';
                                            echo '<td>' . $holidayCount . '</td>';
                                            echo '</tr>';
                                        }
                                    }
                                    ?>
                                    <tr style="border: 1px solid #666;">
                                        <th>Employee</th>
                                        <?php
                                        if ($year) {
                                            $year = $year;
                                        } else {
                                            $year = date('Y');
                                        }
                                        for ($d = 1; $d <= 31; $d++) {
                                            $time = mktime(12, 0, 0, $monthCheck, $d, $year);
                                            if (date('m', $time) == $monthCheck)
                                                echo '<th>' . date('D', $time) . '</th>';
                                        }
                                        ?>
                                        <th>Late</th>
                                        <th>Absent</th>
                                        <th>AL/CL</th>
                                        <th>Holiday</th>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                            <?php

                        } else if (isset($_GET['view']) && !empty($employee_id)) {

                            $getYearMonth = date('Y-m-00', strtotime($year . '-' . $monthCheck . '-01'));

                            //For Holidays
                            $holidayFilter = ' WHERE STR_TO_DATE(ac_date, "%Y-%m") = "' . $getYearMonth . '"';
                            $holidays = HolidaySetup::GetHolidaySetupList($holidayFilter);

                            //For Employee Detail
                            $qry = EmployeeSetup::GetEmployeeSetupById($employee_id);
                            $salary = $qry->salary;

                            //For Check Advance Salary
                            $AdvSalaryFilter = ' WHERE employee_id = "' . $employee_id . '" AND advance = 1 AND STR_TO_DATE(date_time, "%Y-%m") = "' . $getYearMonth . '"';
                            $advSalary = SalarySetup::GetSalarySetupList($AdvSalaryFilter);
                            if ($advSalary) {
                                $advanceSalary = $advSalary[0]->amount;
                            }

                            //For Check Loan
                            $LoanSalaryFilter = ' WHERE employee_id = "' . $employee_id . '" AND status = "active"';
                            $loanSalary = LoanSetup::GetLoanList($LoanSalaryFilter);
                            if ($loanSalary) {
                                $currentDate = date('Y-m', strtotime($year . '-' . $monthCheck));
                                foreach ($loanSalary AS $loanSalaryRow) {
                                    $startDate = $loanSalaryRow->start_date;
                                    $endDate = date("Y-m", strtotime('+' . $loanSalaryRow->installment . ' month', strtotime($loanSalaryRow->start_date)));
                                    if ($currentDate <= $endDate) {
                                        $loanPerMonth = $loanSalaryRow->amount / $loanSalaryRow->installment;
                                    }
                                }
                            }

                            //For Check Annual Leave
                            $LeaveFilter = ' WHERE employee_id = "' . $employee_id . '" AND STR_TO_DATE(leave_date, "%Y") = "' . $year . '-00-00"';
                            $Leaves = LeaveSetup::GetLeaveSetupList($LeaveFilter);
                            if ($Leaves) {
                                foreach($Leaves AS $LeavesRow){
                                    $LeavesArray[] = $LeavesRow->leave_date;
                                }
                            }
                            ?>
                            <div>
                                <h3 style="text-align: center;"><?= $qry->name ?></h3>
                                <table style="text-align:center;" class="display table table-bordered table-striped">
                                    <tbody>
                                    <?php
                                    if ($qry) {
                                        $countDays = 0;
                                        for ($d = 1; $d <= 31; $d++) {
                                            $getDate = date('Y-m-d', strtotime($year . '-' . $monthCheck . '-' . $d));
                                            $time = mktime(12, 0, 0, $monthCheck, $d, $year);
                                            if (date('m', $time) == $monthCheck) {
                                                if ($holidays) {
                                                    foreach ($holidays AS $holidaysRow) {
                                                        if ($getDate == $holidaysRow->ac_date) {
                                                            $totalHoliday++;
                                                        }
                                                    }
                                                }
                                                $countDays++;
                                            }
                                        }
                                        //$countDays -= $totalHoliday;
                                        $salaryPerUnit = ($qry->salary / $countDays) / 9;
                                        for ($d = 1; $d <= 31; $d++) {
                                            $time = mktime(12, 0, 0, $monthCheck, $d, $year);
                                            if (date('m', $time) == $monthCheck) {
                                                $getDate = date('Y-m-d', strtotime($year . '-' . $monthCheck . '-' . $d));
                                                $getDateNight = date('Y-m-d 19:00:00', strtotime($year . '-' . $monthCheck . '-' . $d));
                                                $getDateNightPlus = date('Y-m-d 18:59:59', strtotime('+1 day', strtotime($year . '-' . $monthCheck . '-' . $d)));
                                                $filterAttDay = ' AND STR_TO_DATE(time_in, "%Y-%m-%d") = "' . $getDate . '" AND shift = "day" ORDER BY time_in ';
                                                $filterAttNight = ' AND STR_TO_DATE(time_in, "%Y-%m-%d %H:%i:%s") >= "' . $getDateNight . '" AND STR_TO_DATE(time_in, "%Y-%m-%d %H:%i:%s") <= "' . $getDateNightPlus . '" AND shift = "night"  ORDER BY time_in ';
                                                $attendanceDay = Attendance::GetAttendanceByEmployeeId($qry->id, $filterAttDay);
                                                $attendanceNight = Attendance::GetAttendanceByEmployeeId($qry->id, $filterAttNight);
//                                                echo '<pre>';
//                                                print_r($attendanceNight);
//                                                echo '</pre>';
                                                if ($d == 1) {
                                                    echo '<tr style="border: 1px solid #666;">
                                                        <th style="width: 10px;">Days</th>
                                                        <th>Work Hours</th>
                                                        <th>Amount / Day</th>
                                                        <th>Attendance</th>
                                                        <th>AL/CL</th>
                                                    </tr>';
                                                }

                                                $getDate = date('Y-m-d', strtotime($year . '-' . $monthCheck . '-' . $d));
                                                $background = '';
                                                if ($holidays) {
                                                    foreach ($holidays AS $holidaysRow) {
                                                        if ($getDate == $holidaysRow->ac_date) {
                                                            $holidaysac[] = $holidaysRow->ac_date;
                                                            $background = '#FFA500';
                                                            $holidayCount++;
                                                            $amountPerDay = 9 * $salaryPerUnit;
                                                            $totalPayamount += $amountPerDay;
                                                        }
                                                    }
                                                }
                                                $present = '';
                                                if ($attendanceDay || $attendanceNight) {
                                                    if (!empty($attendanceDay)) {
                                                        $count = 0;
                                                        $counter = 0;
                                                        $enddatetimes = '';
                                                        foreach ($attendanceDay AS $attendanceRow) {
                                                            if ($attendanceRow->shift == 'day') {
                                                                $counter++;
                                                                if ($counter % 2 == 0) {
                                                                    $timeEnd = '';
                                                                    $timeEnd = $attendanceRow->time_in;

                                                                    $diff = abs(strtotime($timeEnd) - strtotime($timeStart));

                                                                    $totalworkminutes = number_format((($diff % 86400) / 3600), 2);
                                                                    $amountPerDay = $totalworkminutes * $salaryPerUnit;
                                                                    $totalPayamount += $amountPerDay;

                                                                    $years = floor($diff / (365 * 60 * 60 * 24));
                                                                    $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
                                                                    $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));

                                                                    $hours = intval(($diff % 86400) / 3600);
                                                                    $minutes = intval(($diff / 60) % 60);
                                                                    $seconds = intval($diff % 60);
                                                                    $timeTotal[] = date('H:i:s', strtotime($hours . ':' . $minutes . ':' . $seconds));
                                                                } else {
                                                                    if (!empty($attendanceRow->late_grace_time)) {
                                                                        $startTime = date("Y-m-d", strtotime('-' . $attendanceRow->late_grace_time . ' min', strtotime($attendanceRow->time_in)));
                                                                    } else {
                                                                        $startTime = date('Y-m-d', strtotime($attendanceRow->time_in));
                                                                    }
                                                                    if ($startTime <= date('Y-m-d')) {
                                                                        if ($attendanceRow) {
                                                                            //echo date('H:i:s', strtotime($attendanceEmployee[0]->time_in)) . '<br>';

                                                                            if (date('H:i:s', strtotime($startTime)) <= $attendanceRow->shift_start_time) {
                                                                                $present = 'P';
                                                                            } else {
                                                                                $present = 'L';
                                                                                $lateCount++;
                                                                            }
                                                                        } else {
                                                                            if (!empty($background)) {
                                                                                $present = 'H';
                                                                            } else {
                                                                                $present = 'A';
                                                                                $absentCount++;
                                                                            }
                                                                        }
                                                                    }
                                                                    $timeStart = $attendanceRow->time_in;
                                                                }
                                                            }
                                                            $count++;
                                                        }
                                                        if ($timeTotal) {
                                                            $enddatetimes = '';
                                                            $enddatetime = '';
                                                            foreach ($timeTotal AS $timeTotalRow) {
                                                                $enddatetime = strtotime($timeTotalRow) + strtotime($enddatetime) - strtotime('00:00:00');
                                                                $enddatetimes += intval($enddatetime);
                                                            }
                                                        }
                                                        $enddatetimes = $enddatetimes + strtotime('00:00:00');
                                                        $timeTotal = '';
                                                    } else if (!empty($attendanceNight)) {
                                                        $count = 0;
                                                        $counter = 0;
                                                        $enddatetimes = '';
                                                        foreach ($attendanceNight AS $attendanceRow) {
                                                            if ($attendanceRow->shift == 'night') {
                                                                $nightstarttime = date('Y-m-d 19:00:00', strtotime($year . '-' . $monthCheck . '-' . $d));
                                                                $nightendtime = date('Y-m-d 18:59:00', strtotime('+1 day', strtotime($year . '-' . $monthCheck . '-' . $d)));
                                                                if ($attendanceRow->time_in >= $nightstarttime && $attendanceRow->time_in <= $nightendtime) {
                                                                    $counter++;
                                                                    if ($counter % 2 == 0) {
                                                                        $timeEnd = '';
                                                                        $timeEnd = $attendanceRow->time_in;

                                                                        $diff = abs(strtotime($timeEnd) - strtotime($timeStart));

                                                                        $totalworkminutes = number_format((($diff % 86400) / 3600), 2);
                                                                        $amountPerDay = $totalworkminutes * $salaryPerUnit;
                                                                        $totalPayamount += $amountPerDay;
                                                                        //echo $totalworkminutes.'<br>';

                                                                        $years = floor($diff / (365 * 60 * 60 * 24));
                                                                        $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
                                                                        $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));

                                                                        $hours = intval(($diff % 86400) / 3600);
                                                                        //echo $hours . ' - ';
                                                                        $minutes = intval(($diff / 60) % 60);
                                                                        //echo $minutes . '<br>';
                                                                        $seconds = intval($diff % 60);
                                                                        $timeTotal[] = date('H:i:s', strtotime($hours . ':' . $minutes . ':' . $seconds));
                                                                    } else {
                                                                        if (!empty($attendanceRow->late_grace_time)) {
                                                                            $startTime = date("Y-m-d H:i:s", strtotime('-' . $attendanceRow->late_grace_time . ' min', strtotime($attendanceRow->time_in)));
                                                                        } else {
                                                                            $startTime = date('Y-m-d H:i:s', strtotime($attendanceRow->time_in));
                                                                        }
                                                                        if ($startTime <= date('Y-m-d 19:00:00')) {
                                                                            if ($attendanceRow) {
                                                                                if (date('H:i:s', strtotime($startTime)) <= $attendanceRow->shift_start_time) {
                                                                                    $present = 'P';
                                                                                } else {
                                                                                    $present = 'L';
                                                                                    $lateCount++;
                                                                                }
                                                                            } else {
                                                                                if (!empty($background)) {
                                                                                    $present = 'H';
                                                                                } else {
                                                                                    $present = 'A';
                                                                                    $absentCount++;
                                                                                }
                                                                            }
                                                                        }
                                                                        $timeStart = $attendanceRow->time_in;
                                                                    }
                                                                }
                                                            }
                                                            $count++;
                                                        }
                                                        if ($timeTotal) {
                                                            $enddatetimes = '';
                                                            $enddatetime = '';
                                                            foreach ($timeTotal AS $timeTotalRow) {
                                                                $enddatetime = strtotime($timeTotalRow) + strtotime($enddatetime) - strtotime('00:00:00');
                                                                $enddatetimes += intval($enddatetime);
                                                            }
                                                        }
                                                        $enddatetimes = $enddatetimes + strtotime('00:00:00');
                                                        $timeTotal = '';
                                                    }
                                                }
                                                else {
                                                    if (!empty($background)) {
                                                        $present = 'H';
                                                    } else {
                                                        if($LeavesArray) {
                                                            if(in_array($getDate,$LeavesArray)){
                                                                $present = 'LE';
                                                                $background = '#9F6';
                                                            }else{
                                                                $present = '';
                                                            }
                                                        } else if(date('Y-m-d') >= $getDate){
                                                            $present = 'A';
                                                            $absentCount++;
                                                        }else{
                                                            $present = '';
                                                        }
                                                    }
                                                }
                                                echo '<tr>';
                                                echo '<td style="background-color:' . $background . ';">' . $d . '</td>';
                                                echo '<td style="background-color:' . $background . ';">' . (!empty($enddatetimes) ? date("H:i:s", $enddatetimes) : "-") . '</td>';
                                                echo '<td style="background-color:' . $background . ';">' . (!empty($amountPerDay) ? number_format($amountPerDay, 2) : "-") . '</td>';
                                                echo '<td style="background-color:' . $background . ';">' . (!empty($present)?$present:"-") . '</td>';
                                                echo '<td style="background-color:' . $background . ';">';
                                                if($present == 'A'){
                                                    echo '<a style="margin-left:30px; display: block; cursor:pointer;font-size: 10px;padding: 1px;" data-toggle="modal" href="#myModal'.$d.'" title="Import"><button>Add AL/CL</button></a>';
                                                    echo '<!-- Modal -->
                                <div id="myModal'.$d.'" class="modal fade"
                                     role="dialog">
                                    <div class="modal-dialog">
                                        <form action="leaveupdate.php" method="post" name="leaveupdate"
                                              enctype="multipart/form-data">
                                            <!-- Modal content-->
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <button type="button" class="close"
                                                            data-dismiss="modal">&times;</button>
                                                    <h4 class="modal-title">Add Annual/Casual Leave</h4>
                                                </div>
                                                <div class="modal-body">
                                                    <h4 style="text-align:left;" class="col-lg-4">Date: </h4>
                                                    <h4 style="text-align:left;" class="col-lg-8">'.date('d-m-Y', strtotime($year . '-' . $monthCheck . '-' . $d)).'<br>
                                                        <input class="form-control" type="hidden"
                                                               name="leave_date"
                                                               value="'.date('Y-m-d', strtotime($year . '-' . $monthCheck . '-' . $d)).'"
                                                               id="leave_date"/>
                                                        <input class="form-control" type="hidden"
                                                               name="employee_id"
                                                               value="'.$employee_id.'"
                                                               id="employee_id"/>
                                                        <input type="hidden" name="url" value="'.$actual_link.'">
                                                    </h4>
                                                    <br>
                                                    <h5 style="text-align:left;" class="col-lg-4">Leave Type: </h5>
                                                    <span style="text-align:left;" class="col-lg-8">
                                                    <select class="form-control" name="type">
                                                    <option value="AL">Annual Leave</option>
                                                    <option value="CL">Casual Leave</option>
                                                    </select>
                                                    </span>
                                                    <div class="clearfix"></div>
                                                    <br>
                                                    <h5 style="text-align:left;" class="col-lg-4">Description: </h5>
                                                    <span style="text-align:left;" class="col-lg-8">
                                                    <textarea class="form-control" name="description" id="description"></textarea>
                                                    </span>
                                                    <br><br><br><br><br>
                                                </div>

                                                <div class="modal-footer">
                                                    <input type="submit" class="btn btn-success" name="submit"
                                                           id="submit" value="Submit"/>
                                                    <button type="button"
                                                            class="btn btn-danger"
                                                            data-dismiss="modal">Close
                                                    </button>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>';
                                                }
                                                echo '</td>';
                                                echo '</tr>';
                                                $enddatetimes = '';
                                                $amountPerDay = '';
                                            }
                                        }
                                    }
                                    ?>
                                    <tr style="border: 1px solid #666;">
                                        <th>Days</th>
                                        <th>Work Hours</th>
                                        <th>Amount / Day</th>
                                        <th>Attendance</th>
                                        <th>AL/CL</th>
                                    </tr>
                                    </tbody>
                                </table>
                                <?php
                                $totalPayamounts = ($totalPayamount - $advanceSalary) - $loanPerMonth;
                                $lateAbsent = floor($lateCount/3);
                                $lateAdsentdeduction = $lateAbsent * 9 * $salaryPerUnit;
                                $totalPayamounts -= $lateAdsentdeduction;
                                ?>
                                <table class="display table table-bordered table-striped">
                                    <tr>
                                        <th colspan="2">Total Salary</th>
                                    </tr>
                                    <tr>
                                        <td>Holidays</td>
                                        <td><?= $holidayCount ?></td>
                                    </tr>
                                    <tr>
                                        <td>Late</td>
                                        <td><?php
                                            echo !empty($lateCount)?$lateCount:'-';
                                            if($lateAdsentdeduction){
                                             echo ' = '.$lateAbsent.' Absent ('.number_format($lateAdsentdeduction, 0).')';
                                            } ?></td>
                                    </tr>
                                    <?php
                                    $absentCount+=$lateAbsent;
                                    if($absentCount == 0 || $absentCount == ''){
                                        $totalPayamounts+=1000;
                                        ?>
                                        <tr>
                                            <td>Attendance Allowance</td>
                                            <td>1000</td>
                                        </tr>
                                    <?php }?>
                                    <tr>
                                        <td>Salary</td>
                                        <td><?= (!empty($totalPayamount) ? number_format(round($totalPayamount), 0) : "0") . ' / ' . (!empty($salary) ? number_format($salary, 0) : "0") ?></td>
                                    </tr>
                                    <?php
                                    if ($advanceSalary) {
                                        echo '<tr>
                                        <td>Advance Salary</td>
                                        <td>' . $advanceSalary . '</td>
                                    </tr>';
                                    }
                                    if ($loanPerMonth) {
                                        echo '<tr>
                                        <td>Loan</td>
                                        <td>' . $loanPerMonth . '</td>
                                    </tr>';
                                    }
                                    ?>
                                    <tr>
                                        <td>Total Pay Amount</td>
                                        <td><?= (!empty($totalPayamounts) ? number_format($totalPayamounts, 0) : "0") ?></td>
                                    </tr>
                                </table>
                            </div>
                        <?php } ?>
                    </div>
                </div>
            </div>
            <footer>
                <?php include('../includes/inc_footer.php'); ?>
            </footer>
            </div>
        </section>
        <script type="text/javascript" src="<?= PROJECT_URL ?>js/download.js"></script>
        <?php include('../includes/inc_js.php'); ?>
        <style>
            @page {
                size: A4;
                margin: 5mm 0mm;
            }

            @media print {
                table {
                    page-break-inside: auto
                }

                tr {
                    page-break-inside: avoid;
                    page-break-after: auto
                }

                #container {
                    page-break-after: always;
                }

                header, aside, form, footer {
                    display: none;
                }

                .adv-table th, .adv-table td {
                    padding: 5px;
                }

                h2 {
                    font-size: 18px;
                    text-align: center;
                }

                hr {
                    margin-top: 5px;
                    margin-bottom: 5px;
                }
            }
        </style>
</body>
</html>