<?php
session_start();
// ============
include("../../asset/theme/css/myfunc.php");
include("../../php_crud/my_class.php");
$obj=new my_class();
// ============

extract($_GET);
//echo "ssdsd";
$today_date=date_create($today_date);
 date_format($today_date,'Y-m-d');
// $today_date=$_GET['to_date'];
 
  $today_date=date_format($today_date,'Y-m-d');
$medium_id;
$atd_status_id;

$del_class_data=$obj->Delete_Data("_atd_daily_by_class","DATE(_entry_date)='$today_date'");

$del_class_status=$obj->Delete_Data("_atd_daily_status_class","DATE(_entry_date)='$today_date'");
	

 $att_data=array(
                'status'=>'0' //student Out Time
                );
                $a = $obj->Update_Data("_atd_device_log",$att_data,"DATE(`CHECK_TIME`)='$today_date' AND `CARD_NO` IN (SELECT `_id_card` FROM `_pf_std_personal_info`  WHERE _id_card IS NOT NULL)");

	

?>
<?php
//include("../class/db_class.php");
//$obj = new my_class();

//$today_date = date('Y-m-d');

//$today_date="2017-04-08";
//$today_date=$_GET['to_date'];



foreach ($obj->View_All_By_Cond("_atd_device_log","STATUS='0' AND DATE(CHECK_TIME)='$today_date' ORDER BY ID,FLAG") AS $v1) 
{

    $id = $v1['ID'];
     $cardno = $v1['CARD_NO'];
    $time = $v1['CHECK_TIME'];
	$input_time = $v1['CHECK_TIME'];
	
    $flag = $v1['FLAG'];
    $mechine_id = '1';
    $login_time = date("H:i:s", strtotime($time));
    $ck_date = date( 'Y-m-d', strtotime($time));
    

    if($ck_date==$today_date){
	
	  
        $std_details = $obj->View_students_personal_details_by_card_id($cardno);
		
		 $name = $std_details->_full_name;
                    $st_id = $std_details->_st_id;
                    $section_id = $std_details->_section_id;
                    $ins_id = $std_details->_institute_id;
                    $br_id = $std_details->_branch_id;
                    $medium_id = $std_details->_medium_id;
                    $shift_time = $std_details->_shif_in_time;

            		$checkTime = date("H:i:s", strtotime($shift_time));
                    $loginTime = date("H:i:s", strtotime($time));

                    $checkTime_1 = date("His", strtotime($shift_time));
                    $loginTime_1 = date("His", strtotime($time));

                    $checkTime = strtotime($checkTime);
                    $loginTime = strtotime($loginTime);
                    $diff = $checkTime-$loginTime;

                    if($checkTime_1<$loginTime_1){
                        $att_status = 'LA';
                        $l_time = (int)(abs($diff)/60);
                    }
                    else{
                        $att_status = 'P';
                        $l_time = '0';
                    }
            		
		
		
		
		
		
            if($flag=='I'){
            // ============= Start Student Attendance ===============
                $table_name = '';
                //$std_details = $obj->View_students_personal_details_by_uniq_id($cardno);
                if (count($std_details) > 0 && $std_details != NULL) {


                    

                //****** Step 1 ********\\

                    $table_name = '_atd_daily_by_class';
                    $where_clause = "_section_id='" . $section_id . "' AND _std_id=$st_id AND DATE(_entry_date)='$today_date'";
                    $std = $obj->Total_Count_By_Cond($table_name, $where_clause);
                    

                    if ($std == 0) {

                        if ($section_id) {
                            $student_list = $obj->sudents_id_by_section($section_id);
                            foreach ($student_list as $index => $value) {

                                $form_data = array(
                                    '_section_id' => $section_id,
                                    '_std_id' => $value->_id,
                                    '_atd_status' => 'A',
                                    '_atd_type' => 'Machine',
                                    '_entry_date' => $time,
									
                                );

                                $last_id = $obj->Insert_Data_By_Cond("_atd_daily_by_class", $form_data, "_std_id='$std_id' AND DATE(_entry_date)='$today_date'");
                            }

                            if ($medium_id) {
                                $table_name = '_atd_daily_status_class';
                                $where_clause = " _section_id='" . $section_id . "' AND DATE(_entry_date)='$today_date'";
                                $to = $obj->Total_Count_By_Cond($table_name, $where_clause);
                                if ($to == 0) {
                                    foreach ($obj->View_Section("$medium_id") as $value) {
                                        $section_val = $value['_sec_id'];
                                        $form_data = array(
                                            '_section_id' => $section_val,
                                            '_atd_status' => 0,
                                            '_approve_satus' => 0,
                                            '_entry_date' => $time
                                        );
                                        $obj->Insert_Data_By_Cond("_atd_daily_status_class", $form_data, "_section_id='$section_val' AND DATE(_entry_date)='$today_date'");
                                    }
                                }
                            }
                        }
                    }

                //****** Step Two ********\\	
                    $cond3 = '_section_id =' . $section_id . ' AND DATE(_entry_date)="' . $today_date . '" AND _approve_satus=1';
					
                    $num_att = $obj->num_of_rows("_atd_daily_status_class", $cond3);
					
					
					
                    if ($num_att->num <= 0) {
                        $form_data1 = array(
                            '_atd_status' => 1,
							
                            '_update_by' => isset($_SESSION['LOGIN_USERID']) ? $_SESSION['LOGIN_USERID'] : ''
                        );
						
						
                        $cond1 = '_section_id =' . $section_id . ' AND DATE(_entry_date)="' . $today_date . '"';
                        $obj->Update_Data("_atd_daily_status_class", $form_data1, $cond1);


                        $form_data = array(
                            '_atd_status' => $att_status,
                            '_late_time' => $l_time,
                            '_inst_id' => $ins_id,
                            'machine_id' => $mechine_id,
							'_in_time'=>$login_time,
							'_out_time'=>'',
							
                            '_entry_date' => $time
                        );
                        $cond = '_section_id =' . $section_id . ' AND _std_id=' . $st_id . ' AND DATE(_entry_date)="' . $today_date . '"';
                        $upresult = $obj->Update_Data("_atd_daily_by_class", $form_data, $cond);


                        
                        $att_data=array(
                        'STATUS'=>'1'
                        );
                        $a= $obj->Update_Data("_atd_device_log", $att_data, "ID='$id'");
                        

                    }
					
					
					
                }
					
                    // else{
                    //     echo 'Failed To Update';
                    // }
                }
				
				
				
                 else{
				 
				 //echo "--------------------".$st_id;
				 if ($st_id!="")
				 {
                echo $cond = '_section_id =' . $section_id . ' AND _std_id=' . $st_id . ' AND DATE(_entry_date)="' . $today_date . '"';
				
				echo "<br/>";
					$atd_status = $obj->Details_By_Cond("_atd_daily_by_class",$cond);
				  }  
                    if($atd_status['_atd_status']=='P')
				
					
					{
					echo "$$$$$$$$$$$";
                    $form_data = array(
                          '_out_time'=>$login_time
							
                          
                        );
                        $cond = '_section_id =' . $section_id . ' AND _std_id=' . $st_id . ' AND DATE(_entry_date)="' . $today_date . '"';
                        $upresult = $obj->Update_Data("_atd_daily_by_class", $form_data, $cond);
						
						$att_data=array(
                        'STATUS'=>'1'
                        );
                        $a= $obj->Update_Data("_atd_device_log", $att_data, "ID='$id'");
                        

                    }
				 }
            }
            else{
			
                $att_data=array(
                'STATUS'=>'3' //student Out Time
                );
                $a = $obj->Update_Data("_atd_device_log",$att_data,"ID='$id'");
            }
        }
		
		echo "operation success for date:$today_date ";
		?>