<?php 
setlocale(LC_MONETARY, 'en_IN');
$table_name="purchase_billbook";
?>

<form action="" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr class="row1">
  	<td width="10%" align="right">Branch</td>
  	<td width="15%"><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="10%" align="right">Month</td>
    <td width="15%"><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="10%" align="right">Year</td>
    <td width="15%"><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="10%" 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'];
 ?>
 <p align="right">
  <a href="APReport_print1.php" target="_blank" class="search">Print</a> <a href="APReport_print_export.php?vat=true" class="search">Export</a>
  </p>
<table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
 <tr>
    <th width="8%">Voucher Date</th>
	<th width="12%">Voucher No.</th>
	<th>Vendor</th>
	<th width="12%">GST Registration No.</th>
	<th width="10%" style="text-align:center;">Bill Ref.</th>
	<th width="8%" style="text-align:center;">Bill Ref. Date</th>  
    <th width="10%" style="text-align:center;">Voucher Type</th>  
    <th width="8%" style="text-align:center;">Debit Amount</th>
	<th width="8%" style="text-align:center;">Credit Amount</th>
	<th width="4%" style="text-align:center;">View</th>
  </tr>
  
  <?php $apr=0;
  $GT=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='".$_REQUEST['vender']."'";
    if(!empty($filter))
    $condition=implode(" and ",$filter);
    else 
    $condition="1=1";
    $query= "select *,total_item_amount  BSM,discounted_amt,GrandTotal from $table_name where $condition and year(bill_book_date)='".$years."'";
 	$result=DB_query($query,$db);
	while($row=DB_fetch_array($result))
	{
	
	  if($apr%2!=0)
	  $bg="bgcolor='#CCCCCC'";
	  else
	   $bg="bgcolor='#FFFFFF'";
	   ?>
    <tr <?php //echo $bg;?>>
		<td><?php echo ConvertSQLDate($row['bill_book_date'])?></td>
		<td><?php echo $row['bill_book_no']?></td>
		<td><?php echo $s->getField('suppliers','id',$row['vendor'],'vendor_name'); ?></td>	
		<td><?php echo $s->getField('suppliers','id',$row['vendor'],'gst_no');  ?></td>
		<td><?php echo $row['bill_no']?></td>
		<td style="text-align:center;"><?php echo ConvertSQLDate($row['bill_date']);?></td>
		<td>Purchase</td>
		<td align="right"><?php echo $bsm=$row['GrandTotal'];
		//echo money_format('%!i',$bsm);
			$GT[]=$row['GrandTotal']; ?></td>
	    <td></td>	
		<td style="text-align:center;"><a href="index.php?route=Edit_DirectpurchBillBook&action=Edit&editId=<?php echo $row['id'];?>&action=viewReport&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>" target="_blank"><img src="images/search.gif" width="15" height="15" title="View" /></a></td>
  	</tr>
 
	   <?php 
      /* $apreport_print[]=array("voucher_date"=>ConvertSQLDate($row['bill_book_date']),"voucher_no"=>$row['bill_book_no'],"account_ledger"=>$s->getField('accountledger','AccountsID',$row['ledger'],'AccountName'),"bill_ref"=>$row['bill_no'],"bill_ref_date"=>ConvertSQLDate($row['bill_date']),"voucher_type"=>"Purchase","debit_amount"=>$row['BSM'],"credit_amount"=>"");
	   $apr++;
	   $_SESSION['apreport_print']=$apreport_print;*/
	   $data_array[]=array("bill_date"=>$row['bill_book_date'],"billno"=>$row['bill_book_no'],"ACname"=>$row['vendor'],"bill_no"=>$row['bill_no'],"amt"=>$bsm);
	}
  ?>
  
     <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>       
      <td>&nbsp;</td>
      <td align="right"><strong>Grand Total</strong></td>
      <td align="right"><strong><?php echo money_format('%!i',array_sum($GT));?></strong></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
</table>
<?php
$_SESSION['bill_purchase']=$data_array;
 } ?>