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

$userid = $_GET['userid'];
$leavetype = $_GET['leavetype'];

if(!empty($_GET['day_from']))
{
$day_from= $_GET['day_from'];
}
else
$day_from=date('d-m-Y');


if(!empty($_GET['day_to']))
{
$day_to= $_GET['day_to'];
}
else
$day_to=date('d-m-Y');


$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 sum(no_of_days) as leave_taken from payroll_user_leave_requests where employee_id=$userid and leave_id=$leavetype 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=".$userid." and leave_id=".$leavetype." and is_cancelled!=1 and is_blocked!=1 AND day_to<=CURDATE() AND imd_status=1 ");

$app_leave  = $leaves[0]['leave_taken']?$leaves[0]['leave_taken']:0;
$sac_leaves = $sanctioned_leaves[0]['leave_taken']?$sanctioned_leaves[0]['leave_taken']:0;
$leaves     = $app_leave+$sac_leaves;
$leavesnums = $db->fetchQuery("SELECT number_of_days from payroll_leave where lid=$leavetype");

 $leavesnums= $leavesnums[0]['number_of_days'];
if($leavetype!=6)
{
$balance=$leavesnums-$leaves;
$balance=$balance-$date_diff-1;
}
else
{
$balance='general';
}
echo '{"leaves":'.json_encode($balance).'}';



?>