<?
ob_start();
session_start();
if(isset($_SESSION['ses_name']))
{
$user=$_SESSION['ses_name'];
}else{
header("location:index.php");
}
$parent_name='';
$ledger_name='';
$veh_num='';$to_date='';$from_date='';
$sdate_12 ='';
$page='1';
include("include/config.php");
require_once "include/Database_functions.php";
$sqlObj=new sqlclass();
$msg = '';



if(isset($_REQUEST['del']) && $_REQUEST['del']=='delete')
{
	$id = $_REQUEST['id'];
	$sql=mysql_query("delete from cf_voucher where voucher_id='$id'");
	header("location:voucher-list.php?msg=You Have Successfully Deleted");
}

if(isset($_REQUEST['from_date']) && $_REQUEST['from_date']!=''){
$from_date=date('d-m-Y',strtotime($_REQUEST['from_date']));
}
if(isset($_REQUEST['to_date']) && $_REQUEST['to_date']!=''){
$to_date=date('d-m-Y',strtotime($_REQUEST['to_date']));
}
if(isset($_REQUEST['ledger_name']) && $_REQUEST['ledger_name']!=''){
$ledger_name=$_REQUEST['ledger_name'];
}

function print1($selectype,$driver_name)
			{
			
				//$userids = $_SESSION['admin_id'];
				
					$dates = date('Y-m-d');
					$names = $selectype.$dates;
					$outstr = NULL;
					header("Content-type: application/vnd.ms-word");
					header("Content-Disposition: attachment;Filename=".$names.".xls");
			
					//set the directory to get image			
					// Query database to get data
					$i = 1;
					'<div id="header" >';
								
					$outstr.='
						 <table style="font-family:Arial, Helvetica, sans-serif; font-size:12px;width:500px;border:1px solid #000000; " cellspacing=0 cellpadding=0 ">
						 <tr>
					     	<td colspan="10" style="font-size:14px; min-width:100px; text-align:center; padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Driver Advance List</strong></td>
    					</tr>
						<tr>
					     	<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>No</strong></td>
    						<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Trip No</strong></td>
    						<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Vehicle No</strong></td>
							<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Driver name</strong></td>
							
    						<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Date</strong></td>
							<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Advance Amount</strong></td>
							<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Expense</strong></td>
							<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Paid</strong></td>
							<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Net</strong></td>
							<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Paid/Not Paid</strong></td>
  					</tr>
					';
				
				$sq_arr_print="SELECT a.trip_id as trip_id,a.advance_amt as advance_amt,a.trip_newsrno as trip_newsrno ,a.cash_memo as cash_memo ,a.amt_advt_paid  as amt_advt_paid, a.trip_start_date as tripdt,b.drivername,b.driver_id,c.vehiclenum ,1 as type FROM cf_tripsheet a JOIN cf_driver b ON b.driver_id = a.drv_id join cf_vehicle c on a.trip_vechno = c.id where 1 UNION 
SELECT a.driv_adv_id as trip_id,a.amount as advance_amt,0 as trip_newsrno, 0 as cash_memo,a.amt_advt_paid as amt_advt_paid , a.pay_date as tripdt,b.drivername,b.driver_id,0 as vehiclenum ,2 as type FROM cf_driver_adv a JOIN cf_driver b ON b.driver_id = a.drv_id  where 1";

				if($driver_name!=''){
				$sq_arr_print.=" and a.drv_id = '".$driver_name."' ";
				}


				//$sq_arr_print.=" order by a.sal_adv_id asc ";
			
				$resultclientsql=mysql_query($sq_arr_print);	
					
				
				while($row = mysql_fetch_array($resultclientsql))
				{												  
				 if($row['advance_amt'] > $row['cash_memo'])
					{
						//$aa = "receipt";
						$netamt = ($row['amt_advt_paid'] + $row['cash_memo'])-$row['advance_amt'];
					}
					else
					{
						//$aa = "voucher";
						$netamt = ($row['cash_memo'] -$row['amt_advt_paid'])-$row['advance_amt'];
					}
					
								
					 if($netamt==0)
					 $csh= "Paid";
					 else
					 $csh= "Not Paid";
					 
	 		
				$outstr.= '<tr>
				<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000; border-top:1px solid #000000;">'.$i.'</td>
				<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000; border-top:1px solid #000000;">'.$row['trip_newsrno'].'</td>
				<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000; border-top:1px solid #000000;">'.$row['vehiclenum'].'</td>
				<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000; border-top:1px solid #000000;">'.$row['drivername'].'</td>
				<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">'.date('d-m-Y',strtotime($row['tripdt'])).'</td>
				<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">'.$row['advance_amt'].'</td>
				<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">'.$row['cash_memo'].'</td>
				<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">'.$row['amt_advt_paid'].'</td>
				<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">'.$netamt.'</td>
				<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">'.$csh.'</td>
					
				</tr>';
				 
				 $i++;}
			
				$outstr.= ' </table>
		</div>'; 
				echo $outstr;
				exit;
				
			}
			
			
				
			if(isset($_POST['download']))
			{
				$selectype = "Driver-Advance-List";
				$driver_name = $_POST['driver_name'];
								
				print1($selectype,$driver_name);
			}	
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>OIL FILL</title>
<link href="css/styles.css" rel="stylesheet"  />

<link href="css/menu.css" rel="stylesheet" type="text/css" />
<style>
.newcategoryProfile{ background:#FFFFFF; position:absolute; width:400px; height:250px; z-index:5;
					-webkit-box-shadow: 0px 0px 35px 1px rgba(0,0,0,0.75);
					-moz-box-shadow: 0px 0px 35px 1px rgba(0,0,0,0.75);
					box-shadow: 0px 0px 35px 1px rgba(0,0,0,0.75);}


</style>
<link type="text/css" href="css_date/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
<script type='text/javascript' src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="js/trippopup.js" type="text/javascript"></script>
<!----------------------------css for auto complete-------------------------------->
<link rel="stylesheet" href="css/chosen.css">

<!--end-->

<script type='text/javascript' src="js/jquery.autocomplete.js"></script>
<link rel="stylesheet" type="text/css" href="js/jquery.autocomplete.css" />

<script type="text/javascript" src="scripts_date/jquery-calendar-1.7.2.js"></script>
<script language="javascript">

function getXMLHTTP()
	 { //fuction to return the xml http object
	var xmlhttp=false;	
		try
		{
		xmlhttp=new XMLHttpRequest();
		}
		catch(e)	
		{		
			try{			
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
				}
			catch(e)
			{
				try
				{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e1)
				{
				xmlhttp=false;
				}
			}
		}
	return xmlhttp;
	}   


$(document).ready(function(){
$('#pay_date').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});

$(document).ready(function(){
$('#add_date').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});

$(document).ready(function(){
$('#cheque_date').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});

$(document).ready(function(){
$('#from_date').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});

$(document).ready(function(){
$('#to_date').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});

function Searchdate(page,val)
{ 
 	
	 var page=page;	
	 var driver_name='';
		 
	 if(document.voucher_frm.driver_name){
	 driver_name= document.voucher_frm.driver_name.value;
	 } 
	 
	var strURL="ajax/driver_expense_data.php?&page="+page;
	
	if(driver_name!=''){
	strURL+="&driver_name="+driver_name;
	}
	
	//alert(strURL);
	
	var req = getXMLHTTP();
   if (req)
   {
     req.onreadystatechange = function()
     {
		 
			if ((req.readyState == 0)||(req.readyState == 1)||(req.readyState == 2)||(req.readyState == 3))
      		{
			//document.getElementById('order').innerHTML='<div align="center"><img src="'+path+'images/ajax_loader.gif"/></div>';
			//document.getElementById('Cakeorder').innerHTML='<div align="center"><img src="'+path+'images/ajax_loader.gif"/></div>';
			}
		 
		 
      if (req.readyState == 4)
      {
	 // only if "OK"
	 	if (req.status == 200)
	 	{ 
	   document.getElementById('list').innerHTML=req.responseText;
	//	document.getElementById('Cakeorder').innerHTML=req.responseText;
	 	}
	  else 
	 	{
   	   alert("There was a problem while using XMLHTTP:\n" + req.statusText);

	 }
     }
     }
   req.open("GET", strURL, true);
   req.send(null);
   }
 
   
}

</script>
</head>

<body>
<!------------------popup area (view, edit)--------------->



<!---------------popup area ends------------------>
<div class="viewEmployeePopup login-popup"  id="login-box" style="height: 220px;width:500px;
overflow-x: scroll;">
    <a href="#" class="close">
    <img src="" class="btn_close" title="Close Window" alt="Close" />
    </a>
    <div id="view_emp"></div>
</div>
<div class="wrapper">
  <div>
<?php 
	include("header.php");
?>
 
</div>
 
 <div class="tabbedArea">
   
  <div class="tabBody">
   <h1>Driver Advance List </h1>
   <form name="voucher_frm" id="voucher_frm" method="post"  >
   <div class="searchBox" style="width:93%; margin-right:0px;  padding:0px; ">
   <div align="left">
   <div style="padding:20px;">
    <div class="searchField">
     <p class="input1">Driver  Name</p>
     <select class="chzn-select restselect input3" name="driver_name" id="driver_name" >
                    <option value="">Driver Name</option>
                    <?php 
                            $sq_driver1="select * from cf_driver";
                            $arrdrver2=mysql_query($sq_driver1);
                            while($resdriver2=mysql_fetch_array($arrdrver2))
                            {
                        ?>
                    <option value="<?=$resdriver2['driver_id']?>" >
                    <?=$resdriver2['drivername']?>
                    </option>
                    <?php
                                }
                     ?>
                  </select>
    </div>
    
    
   

    <div class="searchField">
    <input name="button" type="button" class="input1" onClick="Searchdate(1);" value="search" />
    </div>
	
	<div class="searchField" style="padding-left:60px;margin-right: 20px;">
    <input name="download" type="submit" class="input1"  value="download" />
    </div>
  </div>
   </div>
   </div>
   </form>
   <div id="popup" align="center"  style="margin-left: 400px;"></div>
   <div id="list">
   <table  border="0" cellspacing="0" cellpadding="0" class="tableList">
  <tr class="heading">
    <td>No</td>    
    <td>Trip No </td>    
    <td>Vehicle No </td>
	<td>Driver name </td>
	<td>Date </td>
	<td>Advance Amount </td>
    <td>Expense</td>
	<td>Paid</td>
    <td>Pending</td>
	<td>Paid/Not Paid</td>
	<td>Pay</td>
  </tr>
  
  <?php


/*
if(isset($_REQUEST['typename'])){
$typenames=$_REQUEST['typename'];
}*/

$i=1;

//$sq_arr="SELECT a.*,b.drivername,b.driver_id,c.vehiclenum FROM cf_tripsheet a JOIN cf_driver b ON b.driver_id = a.drv_id join cf_vehicle c on a.trip_vechno = c.id where 1";

$sq_arr="SELECT a.driv_adv_id as trip_id,a.amount as advance_amt,0 as trip_newsrno, 0 as cash_memo,a.amt_advt_paid as amt_advt_paid , a.pay_date as tripdt,b.drivername,b.driver_id,0 as vehiclenum ,2 as type FROM cf_driver_adv a JOIN cf_driver b ON b.driver_id = a.drv_id  where 1";

//--------To upload After Sanish Request------------------------

/*$sq_arr="SELECT a.trip_id as trip_id,a.advance_amt as advance_amt,a.trip_newsrno as trip_newsrno ,a.cash_memo as cash_memo ,a.amt_advt_paid  as amt_advt_paid, a.trip_start_date as tripdt,b.drivername,b.driver_id,c.vehiclenum ,1 as type FROM cf_tripsheet a JOIN cf_driver b ON b.driver_id = a.drv_id join cf_vehicle c on a.trip_vechno = c.id where 1 UNION 
SELECT a.driv_adv_id as trip_id,a.amount as advance_amt,0 as trip_newsrno, 0 as cash_memo,a.amt_advt_paid as amt_advt_paid , a.pay_date as tripdt,b.drivername,b.driver_id,0 as vehiclenum ,2 as type FROM cf_driver_adv a JOIN cf_driver b ON b.driver_id = a.drv_id  where 1";*/

//--------End upload After Sanish Request------------------------


$query=$sq_arr;

/*if(isset($_REQUEST['ledger_name']) && $_REQUEST['ledger_name']!=''){
$ledger_name=$_REQUEST['ledger_name'];
$sq_arr.=" and   b.ledgername like '%".$ledger_name."%'";
}
if(isset($_REQUEST['from_date']) && $_REQUEST['from_date']!=''){
$from_date=date('Y-m-d',strtotime($_REQUEST['from_date']));
$sq_arr.=" and   a.pay_date >= '$from_date'";
}*/

//$sq_arr.=" order by a.trip_id asc ";

$function="Searchdate";
include("pagingserch.php");
 $sq_arr.=" LIMIT $start, $limit";

if(isset($_REQUEST['page'])){
$page=$_REQUEST['page'];
}else{
$page=20;
}

$netotal=0;
$array=mysql_query($sq_arr);
$totamtadvtpaid=0;
$totadvanceamt =0;
$totcash_memo =0;
if($array && mysql_num_rows($array)>0){
$pagecnt=$start;

while($res=mysql_fetch_array($array))
{

$pagecnt++;

if($res['advance_amt'] > $res['cash_memo'])
{
	//$aa = "receipt";
	$netamt = ($res['amt_advt_paid'] + $res['cash_memo'])-$res['advance_amt'];
}
else
{
	//$aa = "voucher";
	$netamt = ($res['cash_memo'] -$res['amt_advt_paid'])-$res['advance_amt'];
}

$totadvanceamt += $res['advance_amt'];
$totcash_memo += $res['cash_memo'];
$totamtadvtpaid +=$res['amt_advt_paid'];
/*$drvid = $res['driver_id'];
$ledger = mysql_query("select ledgerid from cf_ledger where empid = '$drvid' and groupid ='8'");
$ledgeres = mysql_fetch_array($ledger);
$to = $ledgeres['ledgerid'];*/

  ?> 
  <tr>
    <td><?php echo $pagecnt;?></td>
    <td><?php echo $res['trip_newsrno'];?></td>
	<td><?php echo $res['vehiclenum'];?></td>
    <td><?php echo $res['drivername'];?></td>
	<td><?php echo date('d-m-Y', strtotime($res['tripdt']));?></td>	
    <td><?php echo $res['advance_amt'];?></td>
	<td><?php echo $res['cash_memo'];?></td>
	<td><?php echo $res['amt_advt_paid'];?></td>
	<td><?php echo $netamt;?></td>
	<td><?php
		if($netamt==0)
	 	{
			echo "Paid";
		}else{
			echo "Not Paid";
		}
	
	 ?></td>
	
	<?php
	 if($netamt>=1)
	 {
	 ?>
	 <td>
	 <?php 
	 if($netamt==0)
	 {
	 ?>
	 <a href="#"><img src="images/payslip.jpg" width="20" height="20"></a>
	
	<?php }else{ ?>
	<a href="voucher-form.php?vouchertype=accoppbal&empid=<?=$res['driver_id']?>&salamt=<?=$netamt?>&advt=adv&tripid=<?=$res['trip_id']?>&drvtype=<?=$res['type']?>&to=<?=$res['type']?>"><img src="images/payslip.jpg" width="20" height="20"></a>
	<?php } ?>
	</td>   
    <?php 
	}
	else{
	$netamt = substr($netamt, 1);
	?>
	<td>
	 <?php 
	 if($netamt==0)
	 {
	 ?>
	<a href="#"><img src="images/payslip.jpg" width="20" height="20"></a> 
	
	<?php }else{ ?>
	<a href="reciept-form.php?reciepttype=accrcvble&clientid=<?=$res['driver_id']?>&amt=<?=$netamt?>&advt=adv&tripid=<?=$res['trip_id']?>&drvtype=<?=$res['type']?>"><img src="images/payslip.jpg" width="20" height="20"></a>
	<?php } ?>
	</td>   
	<?php
	}
		
    ?>	 
	
	<?php /*?> <td><?php if($res['cashed']==1)
	 echo "Cash";
	 elseif($res['cashed']==2)
	 echo "Bank";
	 ?></td><?php */?>
   
 
  </tr>
  <?php 
    $netotal += $netamt;}}
  ?>
 
  <tr class="heading">   
    <td></td>
    <td></td>
    <td></td>    
    <td></td>
	<td>Total</td>
	<td><?=$totadvanceamt?></td>
	<td><?=$totcash_memo?></td>
	<td><?=$totamtadvtpaid?></td>
	<td><?=$netotal?></td>  
  </tr>

   
</table>

  <div align="right" style="padding-top:10px;"><?=$pagination?></div> 
  </div>
  </div>
 </div>
 <div class="footer"><p>Powered by <a href="http://www.aadhocc.co.uk/" target="_blank">Aadhocc</a></p></div>
 
</div>
<div class="quicLInksCover">
 <?php 
	include("leftlink.php");
?>

</div>
</body>
<!----------------------- script for autocomplete------------------------------>
		<script src="js/chosen.jquery.min.js" type="text/javascript">
		</script>
		<script type="text/javascript"> 
			$(".chzn-select").chosen({search_contains: true}); 
		</script>

<!----------------end of script -------------------------------->

</html>
