<?php
session_start();
$i=1;
include("../include/config.php");
require_once "../include/Database_functions.php";

$vechicle_no ='';
$pagetot = '';
$grandtot = '';
$driver_name = '';
$fromdate='';
$todate='';

if(isset($_REQUEST['driver_name'])){
$driver_name=$_REQUEST['driver_name'];
}

if(isset($_REQUEST['fromdate'])){
$fromdate=$_REQUEST['fromdate'];
$fromdate1 = date('Y-m-d', strtotime($fromdate));
}

if(isset($_REQUEST['todate'])){
$todate=$_REQUEST['todate'];
$todate1 = date('Y-m-d', strtotime($todate));
}


$i=1;
if(($_SESSION['typename'] == 'SuperAdmin')) {

	$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.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.amount as advance_amt,0 as trip_newsrno, 0 as cash_memo,0 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";*/

  /* $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------------------------


	//$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";	
	
	if($driver_name!=''){
	$sq_arr.=" and a.drv_id = '".$driver_name."' ";
	}
	
	if($fromdate!="" && $todate!=""){
	$sq_arr.=" and a.trip_start_date between '$fromdate1' and '$todate1'";
	}
	
	
}


/*if($sdate_12!=''){
$sq_arr.=" and date='".$sdate."' ";
}*/
$sq_arr.=" order by a.trip_start_date desc ";
$query=$sq_arr;
//echo $query;
$function="Searchdate";
include("../pagingserch.php");
$sq_arr.=" LIMIT $start, $limit";

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

$netotal=0;
$array=mysql_query($sq_arr);
$totamtadvtpaid=0;
$totadvanceamt =0;
$totcash_memo =0;
$totdriverbatta =0;
$tothaultchg_amt =0;
/*$sumarray=mysql_query($query);
	while($rw1 = mysql_fetch_array($sumarray))
	{
		$grandtot = $grandtot + $rw1['advt_amt'];
	}*/
?>

  <table  border="0" cellspacing="0" cellpadding="0" class="tableList" style="margin-top:10px;">
  <tr class="heading">
    <td>No</td>    
    <td>Trip No </td>    
    <td>Vehicle No </td>
	<td>Driver name </td>
	<td>Start Date </td>
	<td>Finish Date </td>
	<td>Advance Amount </td>
    <td>Expense</td>
	<td>Batta</td>
    <td>Haulting Charge</td>
  </tr>
  
  <?php


if($array && mysql_num_rows($array)>0){
$pagecnt=$start;
$netotal=0;
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'];
  ?> 
 <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['trip_start_date']));?></td>	
	<td><?php echo date('d-m-Y', strtotime($res['trip_finish_date']));?></td>	
    <td><?php echo $res['advance_amt'];?></td>
	<td><?php echo $res['cash_memo'];?></td>
	<td><?php echo round($res['drv_batta'],2);?></td>
	<td><?php echo $res['hault_chg_amt'];?></td>
	
 
  </tr>
  <?php 
    $totdriverbatta +=$res['drv_batta'];
	$tothaultchg_amt +=$res['hault_chg_amt'];
    $netotal += $netamt;}}
  ?>
 
  <tr class="heading">   
    <td></td>
    <td></td>
    <td></td>    
    <td></td>
	<td></td>
	<td></td>
	<td><?=$totadvanceamt?></td>
	<td><?=$totcash_memo?></td>
	<td><?=$totdriverbatta?></td>
	<td><?=$tothaultchg_amt?></td>  
  </tr>

   
</table>
  
   <div align="right" style="padding-top:10px;"><?=$pagination?></div>