<?php
if(isset($_SESSION['msg']))
{
	echo $_SESSION['msg'];
	unset($_SESSION['msg']);	
}
function find_attend_type($symbol)
{
    $sql        = mysql_query("SELECT `id` FROM `tbl_attend_type` WHERE `symbol`='$symbol' ")or die(mysql_error());
    $data       = mysql_fetch_array($sql);
    return $data['id'];
}
?>
<div class="row-fluid" >
<div class="span12">
<div class="head clearfix">
<div class="isw-ok"></div>
<h1>Attendence Excel Upload 1</h1>
</div>
<div class="block-fluid">
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="id" placeholder="Page Name"  id="txt_name" autocomplete="off" />
<div class="row-form clearfix">
<div class="span3">
<label>Excel File (.csv format)</label>
<input type="file"  name="file" />
</div>
<div class="span3">
<label>Date of Month</label>
<input type="text" class="datetimepicker2" required autocomplete="off" name="date" placeholder="Any Date of Month" required id="dob"/>
</div>
<div class="span3">
<label>&nbsp;</label>
<input type= "submit" name="upload" value ="Upload Attandence" />
</div>
<div class="span3">
<label>Click Here For</label>
<a href="images/attandence_list.csv" download>Download Excel Format</a> 
</div>                        
</div>
</div>  
</div>
</div>
<?php
if(isset($_POST['upload']))
{
    $wrong_format        = "";
    $not_exists          = "";
    $message             = "";
	$file       = $_FILES['file']['tmp_name'];
    $date       = date("Y-m-d",strtotime($_POST['date']));
    $year       = date("Y",strtotime($date));
    $month      = date("m",strtotime($date)); 
    $last_date  = date("Y-m-t", strtotime($date));
    $start_date = date("Y-m-d",strtotime("01-".$month."-".$year)); 
    $end_date   = date("Y-m-d",strtotime($last_date."-".$month."-".$year));
	$handle     = fopen($file, "r");
	$i		= 0;$j		= 0;$k		= 0;
	while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
	{
		$aadhar_no		= $filesop[0];
		if($i>0)
		{
		  $aadhar_no    = trim(str_replace('-','',$aadhar_no));
          $aadhar_no    = ltrim($aadhar_no,',');
          if(strlen($aadhar_no)==12)
          {
            $sql_1 		= mysql_query(" select * from `tbl_emp_records` where `aadhar_no`='$aadhar_no' and `status`='0' ")or die(mysql_error());            
            $start_date = $start_date;
			if(mysql_num_rows($sql_1)>0)
			{
			    $data_1 = mysql_fetch_array($sql_1);
                $emp_id = $data_1['id'];    
                $state  = $data_1['state'];     
			    $j      = 1; 
			    for($k=$start_date;$k<=$end_date;)
                {                   
                   $mark       = find_attend_type(strtoupper($filesop[$j]));
                   $sql_2 	   = mysql_query(" INSERT `tbl_attendence` SET `emp_id`='$emp_id',`state`='$state',`date`='$k',`mark`='$mark' ")or die(mysql_error());
                   $k          = date('Y-m-d', strtotime($k . ' +1 day'));
                   $j++;                   
                }
			}
            else
            {
               $not_exists = $not_exists.",".$aadhar_no;  
            }			
          }
          else
          {
            $wrong_format = $wrong_format.",".$aadhar_no; 
          }
	    }
	$i++;
	}
    if(!empty($not_exists))
    {
        $message  .= "<br />Aadhar not matched ".ltrim($not_exists,',');
    }
    if(!empty($wrong_format))
    {
        $message  .= "<br />Aadhar number not in a format ".ltrim($wrong_format,',');
    }  
	if($i>0)
	{
		$_SESSION['msg'] = $message;
	}
	header("location:home.php?page=atten_upload");    
}
 ?>