<?php
include("../include/config.php");
require_once "../include/Database_functions.php";
?>

  <table  border="0" cellspacing="0" cellpadding="0" class="tableList" style="margin-left:20px; ">
  <tr class="heading">
    <td>No</td>    
    <td>Trip Date</td>
    <td>Client</td>
    <td>Client Name</td> 
	 <td>Distance</td> 
	 <td>Paid Amount</td> 
    <td>Reciept</td>
    <td>Delete</td>
  </tr>
 <?php
$i=1;
$sq_arr="select b.billtype,a.trip_id,a.kms,a.trip_sub_client_id,a.gross_weight,a.trip_sub_client_id,a.trip_client_id,a.trip_date from cf_tripsheet as a,cf_rate as b where 1 ";
if(isset($_REQUEST['client_name'])){
$client_name=trim($_REQUEST['client_name']);
$client_name=mysql_escape_string($client_name);
$sq_arr.=" and trip_sub_client_id='".$client_name."' ";
}
if(isset($_REQUEST['tripdatefrm'])){
$tripdatefrm=trim($_REQUEST['tripdatefrm']);
$tripdatefrm=mysql_escape_string($tripdatefrm);
$sq_arr.=" and trip_date>='".$tripdatefrm."' ";
}
if(isset($_REQUEST['tripdateto'])){
$tripdateto=trim($_REQUEST['tripdateto']);
$tripdateto=mysql_escape_string($tripdateto);
$sq_arr.=" and trip_date<='".$tripdateto."' ";
}
$sq_arr.=" and a.paidstatus = '0' and a.trip_sub_client_id=b.client_code group by a.trip_id";

$sq_arr.=" order by a.trip_id  asc ";
$query=$sq_arr;
//echo $query;
$function="Searchdate";
include("../pagingserch.php");
$sq_arr.=" LIMIT $start, $limit";
$i = 0;
if(isset($_REQUEST['page'])){
$page=$_REQUEST['page'];
}else{
$page=10;
}
$billratetotal=0;
$array=mysql_query($sq_arr);
if($array && mysql_num_rows($array)>0){
while($res=mysql_fetch_array($array))
{
$trip_id=$res['trip_id'];
$kilometers=$res['kms'];
$cclient_code=$res['trip_sub_client_id'];
$croute=$res['trip_route_id'];
$billstype=$res['billtype'];
$gross_weight=$res['gross_weight'];
$amtqry="select trip_location,grossweight,drums,cases,bags from cf_tripsheetsub where trip_id='".$trip_id."' ";
$amtsql=mysql_query($amtqry);
//-if multiple towns
while($amtobj=mysql_fetch_object($amtsql)){
$trip_location=$amtobj->trip_location;
$gross_weighteach=$amtobj->grossweight;
$drums=$amtobj->drums;
$cases=$amtobj->cases;
$bags=$amtobj->bags;

$amtqry1="select rate from cf_rate where client_code='".$cclient_code."' and town_id='".$trip_location."'";
$amtsql1=mysql_query($amtqry1);
$amtobj1=mysql_fetch_object($amtsql1);
//echo "-<br>".$res['trip_id']."-";
$billrate=$amtobj1->rate;//echo '<br>';
if($billstype=="Volume Billing")
{
$billratetotal=$billrate*$gross_weighteach;
}
if($billstype=="KM Billing")
{
$billratetotal=$billrate*$kilometers;
}
if($billstype=="Pack Billing")
{
$billratetotal=($billrate*$drums)+($billrate*$cases)+($billrate*$bags);
}
}
//---------select if only one kilomenter
if($billstype=="KM Billing")
{
$billratetotal=$billrate*$kilometers;
}
if($billstype=="Volume Billing")
{
$billratetotal=$billrate*$gross_weight;
}
if($billstype=="Pack Billing")
{
$billratetotal=($drums+$cases+$bags)*$billrate;
//$sum=$billrate*$gross_weight;
}
  ?>
 <tr>
  
  	<td><input type="checkbox" name="selecteds[]" class="case" value="<?=$res['trip_id']?>"> <?php echo $res['trip_id'];?></td>
    <td><a href="#login-box" class="login-window"><?php echo $res['trip_date'];?></a></td>
	 <td><a href="#login-box" class="login-window"><?php echo $res['trip_sub_client_id'];?></a></td>
    <td><a href="#login-box" class="login-window"><?php echo $res['trip_client_id'];?></a></td>
    
       <td><a href="#login-box" class="login-window"><?php echo $res['trip_client_id'];?></a></td>    
     <td class="tr_td_b">
    <?php echo $billratetotal;?>
     </td>    
    <td class="tr_td_b">
     <?php if($res['paidstatus']=='1') {?>
     <a href="add-hirepayment.php?trip_id=<?php echo $res['trip_id']; ?>" style="color:#009999" >Paid </a><?php } else { ?>
     <a href="account-receipt.php?page=<?php echo $page; ?>&trip_id=<?php echo $res['trip_id']; ?>&ab=<?php echo $i;?>" style="color:#FF9900">Pay </a> <?php } ?>
     </td>  
   
   <td class="tr_td_r" ><a href="tripsheet-list.php?del=delete&id=<?php echo $res['trip_id']; ?>" onClick="return confirm('Do you really want to delete this field?');"><img src="images/delete.png" title="Delete"></a></td>
    
   
  </tr>
  <?php 
    $i++;} ?>
	
	  <?php }
  ?>
   
</table>
  
   <div align="right" style="padding-top:10px;" ><?=$pagination?></div>