<?php
include("../../db.php"); 
header("Access-Control-Allow-Origin: * ");

	$idno=$_REQUEST['idno'];
	$session=$_REQUEST['session'];
	$school_id=$_REQUEST['school_id'];
	$class=$_REQUEST['class'];
// 	$idno=mysql_real_escape_string($idno);
// 	$session=mysql_real_escape_string($session);
// 	$school_id=mysql_real_escape_string($school_id);
	

function arraycount($array, $value){
    $counter = 0;
    foreach($array as $thisvalue) /*go through every value in the array*/
     {
           if($thisvalue === $value){ /*if this one value of the array is equal to the value we are checking*/
           $counter++; /*increase the count by 1*/
           }
     }
     return $counter;
     }

	
function total_sundays($month,$year)
{
$sundays=0;
    $m=date("m");
    $d=date("d");
    if($month==$m){
        $total_days=$d;
      }else{
        $total_days=cal_days_in_month(CAL_GREGORIAN, $month, $year);
      }
for($i=1;$i<=$total_days;$i++)
if(date('N',strtotime($year.'-'.$month.'-'.$i))==7)
$sundays++;
return $sundays;
}

function total_sun($month,$year)
{
    $sundays=0;
    $total_days=cal_days_in_month(CAL_GREGORIAN, $month, $year);
    for($i=1;$i<=$total_days;$i++)
    if(date('N',strtotime($year.'-'.$month.'-'.$i))==7)
    $sundays++;
    return $sundays;
}
//echo total_sun(7,2019);	

	$year=explode("-",$session);
    	$y=$year[0];
		$y1=$year[1];

//echo $y;exit;
$monthsList = array(
    "1"=>'April',
    "2"=>'May',
    "3"=>'June',
    "4"=>'July',
    "5"=>'August',
    "6"=>'September',
    "7"=>'October',
    "8"=>'November',
    "9"=>'December',
	"10"=>'January',
    "11"=>'February',
    "12"=>'March'
    
);

//print_r($monthsList);die;	
$ij=0;$data=array();$a=0;

	foreach ($monthsList as $key => $value){$ij++;
		$m=$key;
		//$month = date("F", mktime(0, 0, 0, $m));
		$month=$value;
		
		$mn = date('m',strtotime($y."-".$month."-1"));
		//echo $mn;exit;
		 $current_month =  date("M");
		if($month==$current_month){
		$maxDays = date("d");
		}else{
		$maxDays = cal_days_in_month(CAL_GREGORIAN, $mn, $y);
		}
		
		//$maxDays = cal_days_in_month(CAL_GREGORIAN, $mn, $y);
		$data[$ij]["order"]=$ij;
		
		$data[$ij]["month"]=$month;
		
		//echo $ij.'<br>';
		
		//echo $y1;
		//echo '<br>';
		
               if($m<10)$data[$ij]["year"]=$y;
		else $data[$ij]["year"]=$y1;
		
		
		if($ij<10)
		{
		$y=$year[0];
		}
		else {$y=$year[1];}
		//echo $y1; die;
		//$j=0;
		/*for($d=1; $d<=31; $d++){
			
 		   $time=mktime(12, 0, 0, 2, $d, $y);          
   		 
			 if(date('m', $time)==2)       
    	   
			    $list[]=date('Y-m-d-D', $time);
		}
echo "<pre>";
print_r($list);
echo "</pre>";die();*/

//echo $m; die;
		$data1=array();$ps=0;
		for($i=1; $i<= $maxDays;$i++){ $day=date('D',strtotime($y."-".$mn."-".$i));
		
		$chkHoli=$db->getVal("select status from holiday where school_id='".$school_id."' and date='".$y."-".$mn."-".$i."' and session='".$session."' and month='".$month."' and class='".$class."' ");
		$chkAtt=$db->getVal("select status from attandenc where school_id='".$school_id."' and date='".$y."-".$mn."-".$i."' and type='2' and idno='".$idno."' and session='".$session."' and month='".$month."'");
		
		$chkLeave=$db->getVal("select status from attandenc where school_id='".$school_id."' and date='".$y."-".$mn."-".$i."' and type='3' and idno='".$idno."' and session='".$session."' and month='".$month."'");
					//echo $db->getLastQuery();exit;
					if(strtolower($day)=="sun")  $data1[$i]['att']="H"; 
					
					elseif($chkHoli===1) $data1[$i]['att']="H"; 
					
					elseif($chkAtt===0) $data1[$i]['att']="A"; 
					
					elseif($chkLeave===0) $data1[$i]['att']="L"; 
					
					else $data1[$i]['att']="P";   $ps++;
					 
					$current_date=date("Y-F-d");
					
					if($i<10) $ii="0".$i; else $ii=$i;
					
					$c_date=$y."-".ucwords($month)."-".$ii;
					
				if($current_date == $c_date){ $a=1; break 1;}
		}
		

//echo $ps++; "<pre>-";

   $u=total_sun($mn,$y);

	$chkAtt_a=$db->getRows("select idno from attandenc where school_id='".$school_id."'  and type='2' and status='0' and idno='".$idno."' and session='".$session."' and month='".$month."'");
	
	$chkL=$db->getRows("select status from attandenc where school_id='".$school_id."'  and type='3' and status='0' and idno='".$idno."' and session='".$session."' and month='".$month."'");
	
	$chkH=$db->getRows("select status from holiday where school_id='".$school_id."'  and session='".$session."' and month='".$month."' ");


////////////////////////////////
        $month_name = $monthsList[$ij];

        $monthnum = date('m',strtotime($month_name));

        $yr_month = $data[$ij]["year"]."-".$monthnum;

        $absent_res = $db->getRow("SELECT COUNT(attandenc.idno) as absent FROM attandenc WHERE idno='$idno' AND month='$month_name' AND session='".$session."' and date LIKE '$yr_month%' ");

        $holidays = $db->getRows("SELECT COUNT(*) FROM holiday WHERE session='".$session."' AND month='$month_name' AND class='$class' GROUP BY id HAVING COUNT(*) >= '1' ");

        $totalholidays=count($holidays);

        $totalsundays=total_sun_new($monthnum, $data[$ij]["year"],count($data1));

        //$total_working_days_new = (count($data1) - ($totalsundays + $totalholidays));
        /////////////////////////////////////////////////

	$total_absent=count($chkAtt_a)+count($chkL)+count($chkH)+$u;
    // print date("j");
   
  
	if(count($data1) >= $total_absent)
	{
	$p=count($data1)-$total_absent;
	}
	else
	{
	$p=$total_absent-count($data1);    
	    
	}
	 
		$data[$ij]["days"]=$data1;
		
  	$DS=0;
	foreach($data[$ij]["days"] as $dp)
	{
	    if($dp['att']=='P')
	    {
	     	$DS++;  
	    }
	

	}
	 $DS;		
		
		
		$d["present"]=$DS;
		$d["absent"]= $absent_res['absent'];//count($chkAtt_a);
		$d["leave"]=count($chkL);
		$d["holidays"] = $totalholidays+$totalsundays; //count($chkH)+$u;
		$d["total"]=count($data1);

        $d['extra_param'] = $totalsundays;

		$data[$ij]["attendance_counts"]=$d;

		if($current_date == $c_date)break;
		//if($month=="May") die("first".$i."==".$a."--".$c_date."==".$current_date);
		//$datee=date('F Y');
		//$current=$month." ".$y;
		//if($datee == $current) break;
	}


	if(is_array($data) && count($data)>0)
	{

        $teacher_att_period = $db->getVal("select teacher_attendance_period from tbl_info where id='1'");

		echo json_encode(array('status' => 1, 'msg'=>'Message found Successfully..!', "data"=>$data, 'is_att_period'=>$teacher_att_period));
			
	}else
	{		
	 	echo json_encode(array('status' => 0, 'msg'=>'No message Found..!'));
	}


function total_sun_new($month,$year,$total_days)
{
    $sundays=0;
    //$total_days=cal_days_in_month(CAL_GREGORIAN, $month, $year);
    for($i=1;$i<=$total_days;$i++){
        if(date('N',strtotime($year.'-'.$month.'-'.$i))==7){
            $sundays++;
        }
    }
    return $sundays;
}