<?php include('includes/fun-php.php'); ?>
<form action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
  <tr class="row1">
  	<td width="7%" align="right">Branch</td>
  	<td width="18%"><select name="site"  onchange="getLedgerName(this.value);">
  <option value="0">All</option>
  <?php 
 	   $ss="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'"; 
$site_sql=DB_query($ss,$db);
while($site_row=DB_fetch_array($site_sql))
{?>
  <option value="<?php echo $site_row['loccode'];?>"<?php echo($_REQUEST['site']==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
  <?php 	
}
?>
</select></td>
    <td width="7%" align="right">Month</td>
    <td width="18%"><select name="month" >
	<?php  $amonth=array('ALL','Jan','Feb','March','Apr','May','June','July','Aug','Sept','Oct','Nov','Dec');
	  for($i=0;$i<count($amonth) ;$i++)
	  {
	   $p='';
	   if($_REQUEST['month']==$i)
	   $p='selected';
	   
	    echo "<option value='".$i."' $p>$amonth[$i]</option>";
	  }
	?></select></td>
    <td width="7%" align="right">Year</td>
    <td width="18%"><select name="years" >
	<?php 
	  for($i=date('Y');$i>=2000;$i--)
	  {
	  $p='';
	   if($_REQUEST['years']==$i)
	   $p='selected';
	    echo "<option value='".$i."' $p>$i</option>";
	  }
	?></select></td>
	<td width="7%" align="right">Vendor</td>
    <td><?php 
  		$sql=DB_query("select * from suppliers",$db);		
		if(!$sql)
		{
		   die('Error.'.DB_error_msg($db));
		}
		echo "<select name='vender' style='width:98%;'>";
		echo "<option value='0'>----</option>";
		while($row=DB_fetch_array($sql))
		{
		   $p='';
		   if($row['id']==$_REQUEST['vender'])
		   $p='selected';
		  echo "<option value='".$row['id']."' $p >".$row['vendor_name']."</option>";
		}
		echo "</select>";?></td>
</tr>
<tr class="row1"> 
  <td colspan="8" style="text-align:center;"><input type="submit" name="search" value="Search" class="search" /></td>
  </tr>
</table>
</form>

<?php

if($_REQUEST['search'])
{
$month=$_REQUEST['month'];
$years=$_REQUEST['years'];
$vendor_code=$_REQUEST['vender'];
 ?>
<p align="right">
  <a href="APRpaybleRport_print.php" target="_blank" class="search">Print</a> <a href="APRpaybleRport_print_export.php?vat=true" class="search">Export</a>
  </p>
<table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
  <tr>
	<th>Vendor</th>
	<th width="10%">Bill Ref.</th>
	<th width="6%" style="text-align:center;">Bill Ref. date</th>
	<th width="8%" style="text-align:center;">Bill Amount</th>
	<th>Pending amount</th>
	<th width="6%" style="text-align:center;">Due Date</th>
	<th width="6%" style="text-align:center;">Due Days</th>
	<th width="6%" style="text-align:center;">Status</th>
	<th width="6%" style="text-align:center;">&lt;30 days</th>
	<th width="6%" style="text-align:center;">31 to 60 Days</th>
	<th width="6%" style="text-align:center;">61 to 90 Days</th>
	<th width="6%" style="text-align:center;">91 to 120 days</th>
	<th width="6%" style="text-align:center;">>120 days</th>
    <th width="4%" style="text-align:center;">View</th>
  </tr>  
  
  <?php
   $apr=0;
    $GT=array();
    $search=array();
    $filter=array();
    if($_REQUEST['month']!='0')
    $filter[]="month(bill_book_date)='".$month."'";
    if($_REQUEST['site']!='0')
    $filter[]="site='".$_REQUEST['site']."'";	   
    if($_REQUEST['vender']!='0')
    $filter[]="vendor='".$vendor_code."'";
    if(!empty($filter))
    $condition=implode(" and ",$filter);
    else 
    $condition="1=1";	  
 
 $query= "select *,total_item_amount as BSM, DATEDIFF(NOW(),due_date) AS DiffDate from $table_name where  $condition and year(bill_book_date)='".$years."' ORDER BY due_date ASC";
	$result=DB_query($query,$db);
	while($row=DB_fetch_array($result))
	{
		    $bill_status=$s->GetFieldValue("account_book","trans_type='PURCHASE BILL' and trans_type_id='".$row['id']."'","bill_status");
		   if($bill_status=='Pending')
			$pending_amount=$row['GrandTotal'];
		    else
			$pending_amount=$s->GetFieldValue("account_book","trans_type='PURCHASE BILL' and trans_type_id='".$row['id']."'","pending_amount");
	  if($apr%2!=0)
	  $bg="bgcolor='#CCCCCC'";
	  else
	   $bg="bgcolor='#FFFFFF'";
	if($pending_amount!=0)
	{
	   ?>
    <tr <?php //echo $bg; ?>>
	<td><?php echo $acname= $s->getField('suppliers','id',$row['vendor'],'vendor_name'); ?></td>	
	<td><?php  echo $row['bill_no']?></td>
	<td style="text-align:center;"><?php  echo ConvertSQLDate($row['bill_date']); ?></td>
	<td style="text-align:right"><?php  echo money_format('%!i',$row['GrandTotal']);
	 $GT[]= $row['GrandTotal'];
	?></td>
	 <td style="text-align:right"><?php  echo money_format('%!i',$pending_amount);
	$duedate=$row['DiffDate'];
if($duedate>0)
{
      		$PT1[]=$pending_amount;
		    }else{
		      $PT2[]=$pending_amount;
		    }
			  ?></td>
			<td><?php  echo ConvertSQLDate($row['due_date'])?></td>
			<td><?php 
			/* if ($row['DiffDate'] > 0)
				{
				   echo $remdays=$row['DiffDate'];
				}else{
					echo $remdays=0;
				}	 */
              $cur_date=date('Y-m-d');
              $date1=date_create($row['due_date']);
              $date2=date_create($cur_date);
              $diff=date_diff($date1,$date2);
              echo $remdays=$diff->format("%a");
                 
			?></td>
			<td style="text-align:center;"><?php echo $status= ($duedate>0)?"Overdue":"Pending"; ?></td>
			<td style="text-align:right"><?php  echo $days30= ($duedate<30)?money_format('%!i',$row['GrandTotal']):'';?></td>
			<td style="text-align:right"><?php  echo $days3060= ( $duedate>=30 && $duedate<61)?money_format('%!i',$row['GrandTotal']):''?></td>
			<td style="text-align:right"><?php  echo $days6090= ( $duedate>60 && $duedate<91)?money_format('%!i',$row['GrandTotal']):''?></td>
			<td style="text-align:right"><?php  echo $days90120= ( $duedate>90 && $duedate<121)?money_format('%!i',$row['GrandTotal']):''?></td>
			<td style="text-align:right"><?php  echo $days121=  ( $duedate>120)?money_format('%!i',$row['GrandTotal']):''?></td>
			<td style="text-align:center;"><a href="index.php?route=view_purchBillBook&viewId=<?php  echo $row['id']?><?php  echo $rurl?>" target="_blank"><img src="images/search.gif" width="10" height="10" /></a></td>
		  </tr>		    
		 
			   <?php $apr++; 

			    $data_array[]=array("bill_date"=>$row['bill_book_date'],"bill_book_no"=>$row['bill_book_no'],"vendor"=>$acname,"bill_no"=>$row['bill_no'],"bill_book_date"=>$row['bill_date'],"GrandTotal"=>$row['GrandTotal'],"pending_amt"=>$pending_amount,"due_date"=>$row['due_date'],"status"=>$status,"due_days"=>$remdays,"30days"=>$days30,"3060days"=>$days3060,"6090days"=>$days6090,"90120days"=>$days90120,"120days"=>$days120,"121days"=>$days121);
			}		
	}
  ?>
    <tr bgcolor="#fff">
        <td></td>
        <td></td>
        <td></td>
        <td align="right">Over Due:</td>
        <td style="text-align:right"><?php echo money_format('%!i',(!empty($PT1))?$PGT1=array_sum($PT1):0); ?></td>
        <td></td>
        <td></td>
        <td></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>          
    </tr>
    <tr bgcolor="#fff">
        <td></td>
        <td></td>
        <td ></td>
        <td align="right">Pending:</td>
        <td style="text-align:right"><?php echo money_format('%!i',(!empty($PT2))?$PGT2=array_sum($PT2):0); ?></td>
        <td></td>
        <td></td>
        <td></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>          
    </tr>
    <tr  bgcolor="#fff">
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td align="right">Grand Total:</td>
      <td style="text-align:right"><?php echo money_format('%!i',$PGT1+$PGT2); ?></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
       <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
       <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
</table>
<?php
$_SESSION['acc_payble']=$data_array;
 } ?>

