<?php
include_once("../class/class.MySQL.php");
$db	=new MySQL();

$userid = $_GET['userid'];


$day_from=strtotime($day_from);
$day_to=strtotime($day_to);

$date_difference = $day_to - $day_from;
$date_diff= $date_difference/(60*60*24);

/*$leaves = $db->fetchQuery("SELECT pl.title,pl.number_of_days as total_leave,CASE
    WHEN sum(lr.no_of_days)!='NULL' THEN sum(lr.no_of_days)
ELSE 0 end  as leave_taken  FROM  payroll_user_leave  as pul  JOIN payroll_leave pl ON pl.lid=pul.leave_id LEFT JOIN payroll_user_leave_requests lr
ON lr.leave_id=pul.leave_id WHERE  pul.is_blocked=0 AND pul.employee_id=".$userid." GROUP BY pul.leave_id");*/



$leaves = $db->fetchQuery("SELECT *
FROM payroll_user_leave AS pul
JOIN payroll_leave pl ON pl.lid = pul.leave_id
WHERE pul.is_blocked =0
AND pul.employee_id =".$userid." group by pl.lid  ");
//print_r($leaves);
$i=0;
foreach($leaves as $leave)
{
	$data[$i]['employee_id']= $leave['employee_id'];
	$data[$i]['leave_id']=$leave['leave_id'];
	$data[$i]['title']=$leave['title'];
	$data[$i]['description']=$leave['description'];
	$data[$i]['total_leave']=$leave['number_of_days'];
	
	$user_leaves = $db->fetchQuery("SELECT sum(no_of_days) as leave_taken from payroll_user_leave_requests where employee_id=".$leave['employee_id']." and leave_id=".$leave['leave_id']." and is_cancelled!=1 and is_blocked!=1 AND day_to>CURDATE() ");
	$sanctioned_leaves = $db->fetchQuery("SELECT sum(no_of_days) as leave_taken from payroll_user_leave_requests where employee_id=".$leave['employee_id']." and leave_id=".$leave['leave_id']." and is_cancelled!=1 and is_blocked!=1 AND day_to<=CURDATE() AND imd_status=1 ");
	
	$leave_taken = $user_leaves[0]['leave_taken']?$user_leaves[0]['leave_taken']:"0";
	$leave_taken1 = $sanctioned_leaves[0]['leave_taken']?$sanctioned_leaves[0]['leave_taken']:"0";
	$total_leave_taken	= $leave_taken +$leave_taken1; 
	$data[$i]['leave_taken']= $total_leave_taken;
	$data[$i]['balance_leave']= $leave['number_of_days']-$total_leave_taken;
	$i++;
}
//print_r($data);
echo '{"leaves":'.json_encode($data).'}';



?>