<?php
$must_login     = false ;
$require_path   = '../../' ;
$require_sub    = '../' ;
require( $require_sub.'header.php' ) ;

$employment_id   = $array['employment_id'] ;
$date_time       = $array['date_time'] ;
$date_group_yest = date('Y-m-d', strtotime($date_time.'-1 days')) ;
$date_group      = date('Y-m-d', strtotime($date_time)) ;
$temperature     = $array['temperature'] ;
$sn              = $array['sn'] ;

if ( $employment_id != '' && $date_time != ''  ){

    $status      = '201' ;

    // get staff
    $staffs_q     = $mysqli->query("SELECT staff_id FROM staff
                                    WHERE deleted_at IS NULL AND staff_idno = '".$employment_id."' LIMIT 1") ;

    if ( $staffs_q->num_rows == 0 ){

        $mysqli->query("INSERT INTO staff
                        (staff_idno, staff_name, staff_email, gender_id, staff_icno, staff_passportno, religion_id, ethnic_id, created_at, updated_at) VALUES
                        ('".$employment_id."', '', '', '0', '', '', '0', '0', '".TODAYDATE."', '".TODAYDATE."')") ;
        $staff_id = $mysqli->insert_id ;

    }else{
        $staff    = $staffs_q->fetch_assoc() ;
        $staff_id = $staff['staff_id'] ;
    }

    $last_attendance_q = $mysqli->query("SELECT type FROM staff_attendance
                                         WHERE deleted_at IS NULL AND staff_id = '".$staff_id."' AND check_group = '".$date_group_yest."' ORDER BY attendance_id DESC LIMIT 1") ;
    $check_type = 'in' ;
    if ( $last_attendance_q->num_rows > 0 ){
        // check if last attendance is in
        $last_attendance = $last_attendance_q->fetch_assoc() ;
        if ( $last_attendance['type'] == 'in' ){
            $date_group = $date_group_yest ;
            $check_type = 'out' ;
        }
    }

    if ( $check_type != 'out' ){
        $current_attendance_q = $mysqli->query("SELECT type FROM staff_attendance
                                                WHERE deleted_at IS NULL AND staff_id = '".$staff_id."' AND check_group = '".$date_group."' ORDER BY attendance_id DESC LIMIT 1") ;
        if ( $current_attendance_q->num_rows > 0 ){
            // check if last attendance is in
            $current_attendance = $current_attendance_q->fetch_assoc() ;
            if ( $current_attendance['type'] == 'in' ){
                $check_type = 'out' ;
            }
        }
    }

    // check if staff and time exists
    $check = $mysqli->query("SELECT * FROM staff_attendance
                             WHERE record_from = 'machine' AND staff_id = '".$staff_id."' AND created_at = '".$date_time."' LIMIT 1") ;
    if ( $check->num_rows == 0 ){

        // set information into staff_attendance
        if ( $mysqli->query("INSERT INTO staff_attendance
                            (staff_id, check_group, type, code, record_from, mac_address, ip_address, latitude, longitude, check_area, temperature, created_at, updated_at) VALUES
                            ('".$staff_id."', '".$date_group."', '".$check_type."', '".$sn."', 'machine', '', '', '', '', 'in', '".$temperature."', '".$date_time."', '".$date_time."')") ){
            $status  = '200' ;
        }

    }else{
        $status  = '200' ;
    }

}

require( $require_sub.'footer.php' ) ;

?>