<?php
include("../include/config.php");
require_once "../include/Database_functions.php";
$i=1;
$id='';
$status='';
$recieved_amnt='';
$pending_amnt='';
$veh_namefield='';
$datefield='';
$frmdate11='';
$todate_11='';
$Rent='';
$flags='';

if(isset($_REQUEST['client']))
{
$id=$_REQUEST['client'];
}
if(isset($_REQUEST['status']))
{
$status=$_REQUEST['status'];
}
if(isset($_REQUEST['ab']))
{
$i=$_REQUEST['ab'];
}
if(isset($_REQUEST['veh_namefield']))
{
$veh_namefield=$_REQUEST['veh_namefield'];
}
if(isset($_REQUEST['datefield']))
{
$datefield=$_REQUEST['datefield'];
}
if($status==''){
$status='O';
}

if(isset($_REQUEST['flags']))
{
$Rent=$_REQUEST['flags'];
}
if($Rent==''){
$Rent='';
}
//------------------------------------------------------//

if($status=='O'){
$table="tbl_voucher";
}
if($status=='R'){
$table="tbl_voucher";
}
if($status=='F'){
$table="tbl_fuel_voucher";
}
if($status=='M'){
$table="tbl_maint_voucher";
}
//---------------------------------------------------------------//
$sql = "SELECT count(*) as cnt FROM ".$table;
$qrysql=mysql_query($sql);
$qrysql=mysql_fetch_object($qrysql);
	if($qrysql->cnt==0){
	$status='M';
	$sql = "SELECT count(*) as cnt FROM tbl_maint_voucher";
	$qrysql=mysql_query($sql);
	$qrysql=mysql_fetch_object($qrysql);
	$table="tbl_maint_voucher";
	}
	if($qrysql->cnt==0){
	$status='F';
	$sql = "SELECT count(*) as cnt FROM tbl_fuel_voucher";
	$qrysql=mysql_query($sql);
	$qrysql=mysql_fetch_object($qrysql);
	$table="tbl_fuel_voucher";
	}
	
	if($qrysql->cnt==0){
	$status='R';
	$sql = "SELECT count(*) as cnt FROM tbl_voucher where category = 'R'";
	$qrysql=mysql_query($sql);
	$qrysql=mysql_fetch_object($qrysql);
	$table="tbl_voucher";
	}
	if($qrysql->cnt==0){
	$status='O';
	$sql = "SELECT count(*) as cnt FROM tbl_voucher where category = 'G'";
	$qrysql=mysql_query($sql);
	$qrysql=mysql_fetch_object($qrysql);
	$table="tbl_voucher";
	}
	
//---------------------------------------------------------------//
if(isset($_REQUEST['frmdate1'])){
$frmdate11=$_REQUEST['frmdate1'];
$frmdate1=explode("-",$frmdate11);
$from=$frmdate1[2].'-'.$frmdate1[1].'-'.$frmdate1[0];
}
if(isset($_REQUEST['todate_1'])){
$todate_11=$_REQUEST['todate_1'];
$todate_1=explode("-",$todate_11);
$to  =$todate_1[2].'-'.$todate_1[1].'-'.$todate_1[0];
}
if($status=='O'){
$table="tbl_voucher";
$datfield='add_date';
}
if($status=='F'){
$table="tbl_fuel_voucher";
$datfield='dates';
}
if($status=='M'){
$table="tbl_maint_voucher";
$datfield='dates';
}
$getqry="select * from ".$table." where 1 ";
if($id!='')
$getqry.=" and id='$id'";
if($frmdate11!='' && $todate_11!=''){
	$getqry.=" and ".$datfield." between '$from' and '$to' ";
	}
	if($frmdate11!='' && $todate_11==''){
	$getqry.=" and ".$datfield.">='".$from."' ";
	}
	if($frmdate11=='' && $todate_11!=''){
	$getqry.=" and ".$datfield." <='".$to."' ";
	}
if($veh_namefield!=''){
	if($status=='F'){
		$getqry.=" and (vehicle_number='".$veh_namefield."' or paid_towards like '".$veh_namefield."')";
	}
	if($status=='M'){
		$getqry.=" and (maint_id='".$veh_namefield."' or paid_towards like '".$veh_namefield."')";
	}
}
if($datefield!=''){
	if($status!='O'){
		$getqry.=" order by dates desc";
	}else{
		$getqry.=" order by add_date desc";
	}
}else{
$getqry.=" order by id desc";
}
$query=$getqry;
	$function="Client";
	include("../paging.php");
	$getqry.="  LIMIT $start, $limit";
$getclient=mysql_query($getqry);

?>
 <table width="98%" border="0" cellspacing="0" cellpadding="0">
     <tr>
      <th width="8%" align="left" valign="middle">Bill No</th>
      <th width="10%" align="left" valign="middle">Client</th>
	  <?
	
	  if($status=='R' || $Rent=='R'){?>
	    <th width="10%" align="left" valign="middle">From - To</th>
	    <th width="10%" align="left" valign="middle">Vech No</th>
	    <th width="17%" align="left" valign="middle">Amount Payabale</th>
	    <th width="10%" align="left" valign="middle">Cost</th>
		<th width="10%" align="left" valign="middle">Pending</th>
		<th width="10%" align="left" valign="middle">Add date</th>
	
	  <? } ?>
	  <?
	  if(empty($Rent)){
	  if($status=='O' || $status=='F' || $status=='M'){?>
      <th width="9%" align="left" valign="middle">Service</th>
      <th width="17%" align="left" valign="middle">Amount Payabale</th>
	  <? }}?>
	  <? if(empty($Rent)){
	  if($status=='O' ){?>
      <th width="9%" align="left" valign="middle">Received</th>
      <th width="8%" align="left" valign="middle">Pending</th>
	  <? }}?>
	   <? if(empty($Rent)){
	  if($status=='O' || $status=='F' || $status=='M'){?>
      <th width="9%" align="left" valign="middle">Due date</th>
	  <? } } ?>
	  <?
	  if($status=='O' ){?>
      <th width="4%" align="left" valign="middle">&nbsp;</th>
      <th width="5%">OPTION</th>
      <th width="5%">&nbsp;</th>
	  <? }else{?>
	  <th width="15%">OPTIONdf</th>
	  <? }?>
    </tr>
  	<?php
	$adjacents = 3;
	$page=1;
		
	$sql = "SELECT * FROM ".$table." where  1 ";
	if($id!='')
	$sql.=" and id='$id'";
	if($status=='O'){
	$sql.=" and category = 'G' ";
	$datfield='add_date';
	}
	if($status=='R'){
	$sql.=" and category = 'R' ";
	$datfield='add_date';
	}
	if($status=='F'){
	$datfield='dates';
	}
	if($status=='M'){
	$datfield='dates';
	}
	if($frmdate11!='' && $todate_11!=''){
	$sql.=" and ".$datfield." between '$from' and '$to' ";
	}
	if($frmdate11!='' && $todate_11==''){
	$sql.=" and ".$datfield.">='".$from."' ";
	}
	if($frmdate11=='' && $todate_11!=''){
	$sql.=" and ".$datfield." <='".$to."' ";
	}
	$lt=10;
	$query=$sql;
	$function="Client";
	include("../paging.php");
	$sql.=" order by id desc LIMIT $start,$lt";
	$result = mysql_query($sql);
	
	$i=1;
	if($qrysql->cnt>0){
	while($arr=mysql_fetch_array($result))
	{
	if($status=='O'){
	?>
	<tr>
      <td class="tr_td"><input type="hidden" id="id<?=$i;?>"  name="id<?=$i;?>" value="<?php echo $arr['id'];?>" />
        <?php echo $arr['autonumber'];?></td>
      <td class="tr_td"><?php echo $arr['deposited_to']; ?>
	    <input type="hidden" name="deposited_topay<?=$i?>" id="deposited_topay<?=$i?>" value="<?php echo $arr['deposited_to']; ?>" />	  </td>
      <td class="tr_td"><?php echo $arr['purpose']; ?>
	    <input type="hidden" name="purpose_topay<?=$i?>" id="purpose_topay<?=$i?>" value="<?php echo $arr['purpose']; ?>" />	  </td>
      <td class="tr_td">
	  <? if(isset($arr['cash']))
	  {
	  if($arr['cash']!=''){
	  ?>
	  <input type="hidden" name="pay<?=$i?>" id="pay<?=$i?>" value="<?php echo $arr['cash']; ?>" /><?php echo $arr['cash']; ?>
	  <? }
	  }else{?>
	   <input type="hidden" name="pay<?=$i?>" id="pay<?=$i?>" />
	  <? }
	  ?>	  </td>
      <td class="tr_td">
	  <? if(isset($arr['recieved_amnt']))
	  {
	  if($arr['recieved_amnt']!=''){
	  ?>
	  <input type="text" name="rec<?=$i?>"  id="rec<?=$i?>" value="<?php echo $arr['recieved_amnt']; ?>" onkeyup="balance('<?=$i?>');"/>
	  <? }
	  }else{?>
	 <input type="text" name="rec<?=$i?>"  id="rec<?=$i?>"  onkeyup="balance('<?=$i?>');"/>
	  <? }
	  ?>	  </td>
      <td class="tr_td">
	   <? if(isset($arr['pending_amnt']))
	  {
	  if($arr['pending_amnt']!=''){
	  ?>
	<input type="text" name="pend<?=$i?>" id="pend<?=$i?>" value="<?php echo $arr['pending_amnt']; ?>" readonly="" />
	  <? }
	  }else{?>
	<input type="text" name="pend<?=$i?>" id="pend<?=$i?>" readonly="" />
	  <? }
	  ?>	<input type="hidden" name="old_pendamt<?=$i?>" id="old_pendamt<?=$i?>" value="<?php echo $arr['pending_amnt']; ?>" readonly="" />  </td>
      <td class="tr_td tr_td_r"><? echo date("m-d-Y",strtotime($arr['add_date']));?></td>
      <td align="center" class="tr_td_b">
	  <a onclick="goedit('<?=$i?>','<?php echo $arr['id']; ?>','debtor');"><img src="images/icon2.jpg" width="18" height="18" /></a></td>
      <td align="center" class="tr_td_b"><img src="images/icon5.jpg" width="20" height="20" onclick="javascript:if(confirm('Click OK if it is cash')){Edit('<?=$i;?>','debtor')}else{document.frm_save.action='edit_voucher.php?id=<?php echo $arr['id']; ?>&cnt=<?php echo $i;?>';document.frm_save.submit();}" /></td>
   	  <td align="center" class="tr_td_r" ><a href="delete_voucher.php?id=<?php echo $arr['id']; ?>&cat=<?php echo $arr['category']; ?>" onclick="return confirm('Do you really want to delete this field?');"><img src="images/icon1.jpg" width="21" height="21" /></a></td>
    </tr>
	<?
	}//other vocuher
		
	//Rental Voucher
	
	if($status=='R') {
	?>
	<tr>
      <td class="tr_td"><input type="hidden" id="id<?=$i;?>"  name="id<?=$i;?>" value="<?php echo $arr['id'];?>" />
        <?php echo $arr['autonumber'];?></td>
      <td class="tr_td"><?php echo $arr['deposited_to']; ?>
	    <input type="hidden" name="deposited_topay<?=$i?>" id="deposited_topay<?=$i?>" value="<?php echo $arr['deposited_to']; ?>" />	  </td>
      <td class="tr_td"><?php echo $arr['fromto']; ?>
	    <input type="hidden" name="purpose_topay<?=$i?>" id="purpose_topay<?=$i?>" value="<?php echo $arr['purpose']; ?>" />	  </td>
		 <td class="tr_td"><?php echo $arr['vechno']; ?>
	    </td>
      <td class="tr_td">
	  <? if(isset($arr['cash']))
	  {
	  if($arr['cash']!=''){
	  ?>
	  <input type="hidden" name="pay<?=$i?>" id="pay<?=$i?>" value="<?php echo $arr['cash']; ?>" /><?php echo $arr['cash']; ?>
	  <? }
	  }else{?>
	   <input type="hidden" name="pay<?=$i?>" id="pay<?=$i?>" />
	  <? }
	  ?>	  </td>
      <td class="tr_td">
	  <? if(isset($arr['recieved_amnt']))
	  {
	  if($arr['recieved_amnt']!=''){
	  ?>
	  <input type="text" name="rec<?=$i?>"  id="rec<?=$i?>" value="<?php echo $arr['recieved_amnt']; ?>" onkeyup="balance('<?=$i?>');"/>
	  <? }
	  }else{?>
	 <input type="text" name="rec<?=$i?>"  id="rec<?=$i?>"  onkeyup="balance('<?=$i?>');"/>
	  <? }
	  ?>	  </td>
      <td class="tr_td">
	   <? if(isset($arr['pending_amnt']))
	  {
	  if($arr['pending_amnt']!=''){
	  ?>
	<input type="text" name="pend<?=$i?>" id="pend<?=$i?>" value="<?php echo $arr['pending_amnt']; ?>" readonly="" />
	  <? }
	  }else{?>
	<input type="text" name="pend<?=$i?>" id="pend<?=$i?>" readonly="" />
	  <? }
	  ?>	<input type="hidden" name="old_pendamt<?=$i?>" id="old_pendamt<?=$i?>" value="<?php echo $arr['pending_amnt']; ?>" readonly="" />  </td>
      <td class="tr_td tr_td_r"><? echo date("m-d-Y",strtotime($arr['add_date']));?></td>
	   <?php if($arr['pending_amnt'] != 0){ ?>
      <td align="center" class="tr_td_b" style="border-right:solid #000000 1px;" >
	  <a onclick="goedit('<?=$i?>','<?php echo $arr['id']; ?>','rental');"><img src="images/icon2.jpg" width="18" height="18" /></a><img src="images/icon5.jpg" width="20" height="20" onclick="javascript:if(confirm('Click OK if it is cash')){Edit('<?=$i;?>','rental')}else{document.frm_save.action='edit_voucher.php?id=<?php echo $arr['id']; ?>&cnt=<?php echo $i;?>&cat=rental';document.frm_save.submit();}" />  <a href="delete_voucher.php?id=<?php echo $arr['id']; ?>&cat=<?php echo $arr['category']; ?>" onclick="return confirm('Do you really want to delete this field?');"><img src="images/icon1.jpg" width="21" height="21" /></a></td>
    </tr>
	<?php } else { ?> <td align="center" class="tr_td_r" style="border-right:none;" > &nbsp;&nbsp;&nbsp;&nbsp;</td> <td align="center" class="tr_td_r" style=" border-right:none;" > &nbsp;&nbsp;&nbsp;&nbsp;</td> <td align="center" class="tr_td_r" > &nbsp;&nbsp;&nbsp;&nbsp;</td>  <?php } ?>
	<?
	}
	
	if($status=='F'){?>
	
	<tr>
      <td class="tr_td"><input type="hidden" id="id<?=$i;?>" value="<?php echo $arr['auto_number'];?>" />
	  <?php echo $arr['id'];?></td>
      <td class="tr_td"><?php echo $arr['vehicle_number']; ?></td>
      <td class="tr_td"><?php echo $arr['paid_towards']; ?></td>
      <td class="tr_td">
	   <?php echo $arr['amount']; ?>	  </td>
      <td class="tr_td tr_td_r"><? echo date("m-d-Y",strtotime($arr['dates']));?></td>
     <!-- <td align="center" class="tr_td_b"><a href="edit_voucher.php?id=<?//php echo $arr['id']; ?>&ab=<?//php echo $i;?>"><img src="images/icon2.jpg" width="18" height="18" /></a></td>
      <td align="center" class="tr_td_b"><img src="images/icon5.jpg" width="20" height="20" onclick="Edit('<?//=$i;?>')" /></td> -->
   	  <td align="center" class="tr_td_r" ><a href="delete_voucher.php?Fid=<?php echo $arr['id']; ?>" onclick="return confirm('Do you really want to delete this field?');"><img src="images/icon1.jpg" width="21" height="21" /></a></td>
    </tr>
	<? }
	//Maintanance voucher
	if($status=='M'){?>
	<tr>
      <td class="tr_td"><input type="hidden" id="id<?=$i;?>" value="<?php echo $arr['auto_number'];?>" />
	  <?php echo $arr['id'];?></td>
      <td class="tr_td"><?php echo $arr['maint_id']; ?></td>
      <td class="tr_td"><?php echo $arr['paid_towards']; ?></td>
      <td class="tr_td">
	   <?php echo $arr['amount']; ?>	  </td>
      <td class="tr_td tr_td_r"><? echo date("m-d-Y",strtotime($arr['dates']));?></td>
      <!--<td align="center" class="tr_td_b"><a href="add_maintenance.php?mid=<?//php echo $arr['id']; ?>&ab=<?//php echo $i;?>"><img src="images/icon2.jpg" width="18" height="18" /></a></td>
      <td align="center" class="tr_td_b"><img src="images/icon5.jpg" width="20" height="20" onclick="Edit('<?//=$i;?>')" /></td> -->
   	  <td align="center" class="tr_td_r" ><a href="delete_voucher.php?Mid=<?php echo $arr['id']; ?>" onclick="return confirm('Do you really want to delete this field?');"><img src="images/icon1.jpg" width="21" height="21" /></a></td>
    </tr>
	
	<? }
	 $i++;
	 }
	 if($status=='F'){?>
	  <tr>
      <td  colspan="10" align="right"><a style="cursor:hand;cursor:pointer;" onclick="Client(1);">More</a></td></tr>
	 <?
	 }if($status=='M'){?>
	  <tr>
      <td  colspan="10" align="right"><a style="cursor:hand;cursor:pointer;" onclick="Client(1);">More</a></td></tr>
	 <?
	 }
	 }else{?>
	 
	 <tr>
      <td class="tr_td_r" colspan="8">No Records Found</td></tr>
	 <? }
	?></table>
 <div align="right" style="padding-top:10px;"><?=$pagination?></div>