<?php
$must_login              = true ;
$require_path            = '../../' ;
$require_sub             = '../' ;
require( $require_path.'plugins/geoPHP/geoPHP.inc' ) ;
require( $require_sub.'header.php' ) ;

$boolean_access          = false ;
$input_type              = $array['input_type'] ;
$qrcode                  = $array['qrcode'] ;
$latitude                = $array['latitude'] ;
$longitude               = $array['longitude'] ;
$mac_address             = '' ;
$ip_address              = '' ;

$staff_settings          = [] ;
$staff_id                = '' ;
$branch_id               = '' ;

if ( $input_type        == 'selfpunch' ){
    $status              = '290' ;

    $select_qrcode       = $mysqli->query( "SELECT staff_id, branch_id, staff_settings FROM staff
                                            WHERE staff_idno = '".$qrcode."' AND ( staff_date_resigned IS NULL || staff_date_resigned = '0000-00-00' || staff_date_resigned >= '".TODAYDATE."' ) AND deleted_at IS NULL " ) ;
    if ( $select_qrcode->num_rows > 0 ){
        $boolean_access  = true ;
        $data_qrcode     = $select_qrcode->fetch_assoc() ;

        $staff_id        = $data_qrcode['staff_id'] ;
        $branch_id       = $data_qrcode['branch_id'] ;
        $staff_settings  = $data_qrcode['staff_settings'] ;
    }

}else{
    if ( $boolean_login ){
        $boolean_access  = true ;

        $staff_id        = $staff_info['staff_id'] ;
        $branch_id       = $staff_info['branch_id'] ;
        $staff_settings  = $staff_info['staff_settings'] ;
    }
}

if ( $boolean_access ){
    $status              = '300' ;

    if ( ( $input_type  == 'qrcode' && $qrcode != '' ) || $input_type == 'button' || $input_type == 'selfpunch' ){
        $status          = '272' ;

        $first_string    = substr( $qrcode, 0, 1 ) ;

        $boolean_qr      = false ;
        if ( $input_type == 'button' || $input_type == 'selfpunch' ){
            $boolean_qr  = true ;
            $date_code   = TODAYDATE ;
            $code_status = '0' ;
        }else{
            $check       = $mysqli->query( "SELECT qrcode_id, status, created_at FROM qrcodes
                                            WHERE deleted_at IS NULL AND type = 'checkin' AND code = '".$qrcode."' LIMIT 1" ) ;
            if ( $check->num_rows > 0 ){
                $get         = $check->fetch_assoc() ;
                $boolean_qr  = true ;
                $date_code   = $get['created_at'] ;
                $code_status = $get['status'] ;
            }
        }


        if ( $boolean_qr ){
            $status         = '277' ;

            $date_current   = TODAYDATE ;
            $date_time      = date('Y-m-d H:i:s', strtotime($date_current . ' -5 minutes')) ;
            $date_time_res  = date('Y-m-d H:i:s', strtotime($date_current . ' -15 minutes')) ;
            $date_group     = date('Y-m-d', strtotime($date_current)) ;

            // check if code not yet expired.
            if ( $date_code > $date_time ){
                $status     = '276' ;

                // check code status
                if ( $code_status == '0' ){
                    $status = '275' ;

                    // check last check in out time
                    // get previous check in & out type
                    $last_attendance_q = $mysqli->query("SELECT type, check_group, created_at FROM staff_attendance
                                                         WHERE deleted_at IS NULL AND staff_id = '".$staff_id."' ORDER BY created_at DESC LIMIT 1") ;
                    $boolean_last    = false ;
                    $boolean_last_att = false ;
                    if ( $last_attendance_q->num_rows > 0 ){
                        $last_attendance = $last_attendance_q->fetch_assoc() ;
                        if ( $date_time_res > $last_attendance['created_at'] ){
                            $boolean_last     = true ;
                            $boolean_last_att = true ;
                        }
                    }else{
                        $boolean_last = true ;
                    }

                    if ( $boolean_last ){

                        $status     = '270' ;

                        $check_area = 'out' ;
                        if ( $latitude != '' && $longitude != '' ){

                            // get this staff branch
                            $get_branch = $mysqli->query("SELECT branch_geometry FROM branch
                                                          WHERE deleted_at IS NULL AND branch_id = '".$branch_id."' LIMIT 1") ;
                            if ( $get_branch->num_rows > 0 ){

                                $branch          = $get_branch->fetch_assoc() ;
                                $branch_geometry = $branch['branch_geometry'] ;
                                if ( $branch_geometry != '' ){
                                    $pointLatLng = geoPHP::load("POINT(".$longitude." ".$latitude.")","wkt") ;
                                    $polygon     = geoPHP::load($branch_geometry,'wkt') ;
                                    $inpolygon   = $polygon->pointInPolygon($pointLatLng) ;
                                    if ( $inpolygon ){
                                        $check_area  = 'in' ;
                                    }
                                }

                            }
                        }

                        if ( $check_area == 'in' || $staff_settings['without_geometry'] == 'yes' || $input_type == 'selfpunch' ){

                            // start commit
                            $error      = 0 ;
                            $mysqli->autocommit( false ) ;

                            if ( $input_type == 'qrcode' ){
                                // update qrcode
                                if ( $mysqli->query("UPDATE qrcodes SET
                                                     staff_id        = '".$staff_id."',
                                                     status          = '1',
                                                     updated_at      = '".TODAYDATE."'
                                                     WHERE qrcode_id = '".$get['qrcode_id']."'") ){ }else{
                                    $error++ ;
                                }
                            }

                            $check_type = 'in' ;
                            if ( $boolean_last_att ){
                                // check if last attendance is in
                                if ( $last_attendance['type'] == 'in' ){
                                    $date_group = $last_attendance['check_group'] ;
                                    $check_type = 'out' ;
                                }
                            }

                            // insert new attendance record
                            if ( $mysqli->query("INSERT INTO staff_attendance  
                                                 ( staff_id, check_group, type, code, record_from, mac_address, ip_address, latitude, longitude, check_area, created_at, updated_at ) VALUES
                                                 ( '".$staff_id."', '".$date_group."', '".$check_type."', '".$qrcode."', '".$input_type."', '".$mac_address."', '".$ip_address."', '".$latitude."', '".$longitude."', '".$check_area."', '".TODAYDATE."', '".TODAYDATE."' )") ){ }else{
                                $error++ ;
                            }

                            if ( $error == 0 ) {
                                $mysqli->commit() ; 
                                $status      = '200' ;
                            }else{
                                $mysqli->rollback() ;
                                $status = '304' ;
                            }
                        }

                    }

                }

            }

        }


    }

}

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