<?php
include("../../classes/manager.class.php");
$m = new Manager();



/*
$files = array();
if(glob("C:\Users\Admin\Downloads\attlog*.dat")){
    foreach (glob("C:\Users\Admin\Downloads\attlog*.dat") as $file) {
        if (file_exists($file)) {
            if (date("Y m d H:i:s.", filemtime($file)) > $greater) {
                $filename = $file;
                $greater = date("Y m d H:i:s.", filemtime($file));
            }
            //echo "$file was last modified: " . date ("F d Y H:i:s.", filemtime($file)).'<br>';
        }else{
            echo 'file not exist';
        }
        $files[] = $file;
    }
}else{
    echo 'katlo';
}
*/
if (isset($_FILES["importfile"]) && !empty($_FILES["importfile"])) {
    //if (date("Y m d H:i:s.", filemtime($file)) > $greater) {
        $filenam = $_FILES["importfile"]['tmp_name'];
        $filename = $_FILES["importfile"]['name'];
     //   $greater = date("Y m d H:i:s.", filemtime($file));
    //}
    //echo "$file was last modified: " . date ("F d Y H:i:s.", filemtime($file)).'<br>';
}else{
    echo 'file not exist';
}
//print_r($files);
//echo $filenam.'<br>';
//echo $filename;

$attandance = file_get_contents($filenam);
$att_array_rows = explode("\n", $attandance);
//echo '<pre>';
//print_r($att_array_rows);
//echo '</pre>';
//exit;
if($att_array_rows){
    foreach ($att_array_rows AS $att_array_row) {
        $att_array[] = explode("\t", $att_array_row);
    }
}
//echo '<pre>';
//print_r($att_array);
//echo '</pre>';
//exit;
//print_r($maxAtt);
//echo $maxtime;
//exit;
$counter = 0;
$count = 0;
$countchec = 0;
if($att_array){
    foreach ($att_array AS $att_arrayRow) {
        $filter = ' WHERE machine_id = "'.$att_arrayRow[0].'"';
        $maxAtt = Attendance::GetMaxAttendance($filter);
        $maxtime = $maxAtt->maxtimein;
        if(date('Y-m-d H:i:s', strtotime($maxtime))<date('Y-m-d H:i:s', strtotime($att_arrayRow[2]))){
            $countchec++;
            $counterPart = 0;
            $endtrue = false;
            $data = [];
            if (!empty($att_arrayRow[0])) {
                $employee = EmployeeSetup::GetEmployeeSetupByMachineUserId($att_arrayRow[0]);
                if (!empty($employee)) {
                    $employeeCat = EmployeeCategorySetup::GetEmployeeCategorySetupById($employee->category_id);
                    $counter++;
                    if (isset($att_array[$count + 1][0]) && date('Y-m-d', strtotime($att_array[$count + 1][2])) != date('Y-m-d', strtotime($att_arrayRow[2]))) {
                        $counterPart = 1;
                    }
                    $data['time_in'] = $att_arrayRow[2];
                    $data['employee_id'] = $employee->id;
                    $data['machine_id'] = $att_arrayRow[0];
                    $data['shift'] = $employeeCat->shift;
                    $data['shift_start_time'] = $employeeCat->shift_start_time;
                    $data['late_grace_time'] = $employeeCat->late_grace_time;
                    $data['late_deduction'] = $employeeCat->late_deduction;
                    $data['create_date'] = 'NOW()';
//                    echo '<pre>';
//                    print_r($data);
//                    echo '</pre>';
                    Attendance::AddAttendance($data);
                    $data = '';
                    if($counterPart == 1){
                        $counter = 1;
                    }
                }
            }
            $count++;
        }
    }
}
//echo $countchec;
//exit;
header("Location: " . PROJECT_ADMIN_URL."report/attendance.php");
exit();
?>