<?php
session_start();
$i=1;
include("../include/config.php");
require_once "../include/Database_functions.php";
?>
 <style>
  .newList li{ float:left; text-align:center; margin-right:27px;}
  ul.newList{ display:block;}
  </style>
  <table  border="0" cellspacing="0" cellpadding="0" class="tableList">
  
  <tr class="heading">
    <td>No</td>  
    <td>Vechicle No</td>  
    <td>Trip Date</td>
    <td>Paid Date</td>
    <td>Hirepayment No</td>
    <td>Credit</td>     
     <td colspan="2">
    <p style="border-bottom:1px dashed #333; text-transform:uppercase; text-align:center; padding-bottom:4px; margin-bottom:2px;"> Debit</p>
    <ul class="newList"><li>Advance amount</li><li>Paid</li></ul>      </td> 
    <td>Pending</td>  
  </tr>
  
  <?php

$vechnos ='';
$tripdate ='';
$fromdate='';
$todate='';
$paidfromdate='';
$paidtodate='';

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

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));
}

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

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

$i=1;
if(($_SESSION['typename'] == 'SuperAdmin')) {
	$sq_arr="select cf_tripsheet.trip_id as tripids,cf_tripsheet.trip_vechno as trip_vechno ,cf_tripsheet.trip_date,cf_tripsheet.hire_newsrno,cf_tripsheet.advt_amt,cf_tripsheet.hire_amt,cf_account_hire.trip_id as tripacc_id,cf_account_hire.acc_date from cf_tripsheet join cf_account_hire on cf_tripsheet.trip_id = cf_account_hire.trip_id where delete_status = 0 and trip_vechno = '$vechnos' ";
}

if(($_SESSION['typename'] == 'SubAdmin')) {
	$sq_arr="select * from cf_tripsheet join cf_account_hire on cf_tripsheet.trip_id = cf_account_hire where delete_status = 0 and trip_vechno = '$vechnos' ";
}

/*if(($_SESSION['typename'] == 'SubAdmin')) {
	$userids = $_SESSION['admin_id'];
	$sq_arr="select * from cf_account_hire where acc_vechno = '$vechnos' and user_id = '$userids' "; 	
}*/
//$sq_arr="select * from cf_account_hire where 1 ";


/*if($vechnos!=''){
$sq_arr.=" and acc_vechno like '%".$vechnos."%' ";
}*/

if($fromdate!="" && $todate!=""){
$sq_arr.=" and trip_date between '$fromdate1' and '$todate1'";
}

if($paidfromdate!="" && $paidtodate!=""){
$sq_arr.=" and acc_date between '$paidfromdate1' and '$paidtodate1'";
}

/*if($sdate_12!=''){
$sq_arr.=" and date='".$sdate."' ";
}*/
$sq_arr.=" order by cf_tripsheet.trip_id  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;
}

$array=mysql_query($sq_arr);
if($array && mysql_num_rows($array)>0){
$pagecnt=$start;
$pendval='';
$totcrdt='';
$totadv='';
$creditval = '';
$debitval = '';
while($res=mysql_fetch_array($array))
{
$pagecnt++;
?>
 <tr>
  
  	<td><?php echo $pagecnt;?></td>
    <td><a href="#login-box" class="login-window"><?php echo $res['trip_vechno'];?></a></td>
    <?php
		$tripid = $res['tripids'];
		$sq_drv=mysql_query("SELECT * FROM `cf_tripsheet`  where trip_id = '$tripid'  ");
		$row2 = mysql_fetch_array($sq_drv);
	?>
    <td><a href="#login-box" class="login-window"><?php echo date('d-m-Y', strtotime($res['trip_date']));?></a></td>
    <?php  
		$tripid = $res['tripids'];
		$sq_acchire=mysql_query("select * from cf_account_hire where trip_id = '$tripid'  ");
		$row3 = mysql_fetch_array($sq_acchire);
	?>	
    <td><?php 
		$accdt = date('d-m-Y', strtotime($row3['acc_date']));
		if($accdt == '01-01-1970')
		{
			echo "--";
		}
		else
		{
			echo $accdt;
		};?></td> 
    <td><a href="#login-box" class="login-window"><?php echo $res['hire_newsrno'];?></a></td>
    <td class="tr_td_b">
    <?php  
		$tripid = $res['tripids'];
		$sq_acchire=mysql_query("select * from cf_account_hire where trip_id = '$tripid'  ");
		$row3 = mysql_fetch_array($sq_acchire);
		
		if(mysql_num_rows($sq_acchire) > 0)
		{
			$totcrdt =  $row3['acc_amt'] + $res['advt_amt']; 
			echo $totcrdt;	
		}
		else
		{
			echo $totcrdt = $res['hire_amt'];
		}
		
		if(mysql_num_rows($sq_acchire) == 0 && $res['hire_amt'] == '')
		{
			echo $totcrdt = $res['advt_amt'];
		}
	
	
		
    	$creditval+= $totcrdt;
    ?>
     </td>    
    <?php /*?> <td class="tr_td_b">
    <?php
	 echo $row2['advt_amt']; echo " | ";  
	 if($res['acc_status']=='1') {
	 
     echo $res['acc_amt'];
	 $debitval+= $res['acc_amt'];
     } ?>
     </td> <?php */?>
     
    <td width="11%"> <?php
	 $totadv+= $res['advt_amt'];
	 echo $res['advt_amt']; ?> </td>
    
    <td width="9%">
	<?php if($row3['acc_status']=='1') 
	{
		 echo $row3['acc_amt'];
	 	 $debitval+= $row3['acc_amt']; 
	 } ?> 
     </td>  
     
     <td width="10%">
	<?php if($row3['acc_status']=='0') 
	{
     	$pendval+= $row3['acc_amt'];
		echo $row3['acc_amt'];
    } 
	
	if(mysql_num_rows($sq_acchire) == 0)
	{	
		echo $pen = $totcrdt - $res['advt_amt'];
		$pendval+=$pen;
	}
	
	
	?>
    </td>   
   
   
  </tr>
  
   <?php }}
  ?>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td>Total</td>
    <td><?=$creditval?></td>
    <td><?=$totadv?></td>
    <td><?=$debitval?></td>   
    <td><?=$pendval?></td>     
  </tr>
 


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