
<form action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
  <tr class="row1">
  	<td width="12%" align="right">Site</td>
  	<td width="20%"><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="12%" align="right">Month</td>
    <td width="20%"><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="12%" align="right">Year</td>
    <td><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>
</tr>
<tr class="row1">
    <td colspan="6" 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 target="_blank" href="ASReport_print.php" class="search">Print</a> <a href="ASReport_print_export.php?vat=true" class="search">Export</a>
  </p>
<table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
  <tr>
    <th width="7%" style="text-align:center;">Date</th>
    <th>Account Ledger</th>
    <th width="12%">Voucher Type</th>
    <th width="12%">Invoice No.</th>
    <th width="10%" style="text-align:center;">Debit Amount</th>
	<th width="10%" style="text-align:center;">Credit Amount</th>
	<th width="7%" style="text-align:center;">View</th>
  </tr>
  
  <?php $apr=0;
  $GT=array();
  $data_asrep=array();
    $filter=array();
    if($_REQUEST['month']!='0')
    $filter[]="month(invoiceDate)='".$month."'";
    if($_REQUEST['site']!='0')
    $filter[]="branch_name='".$_REQUEST['site']."'";	   
    
    if(!empty($filter))
    $condition=implode(" and ",$filter);
    else 
    $condition="1=1";	

     $query= "select *,totalDiscountedPrice  as BSM from $table_name where $condition and year(invoiceDate)='".$years."' ";
 	$result=DB_query($query,$db);
 	$tot_deb=0;
 	
	while($row=DB_fetch_array($result))
	{
	  if($apr%2!=0)
	  $bg="bgcolor='#CCCCCC'";
	  else
	   $bg="bgcolor='#FFFFFF'";
	   ?>
    <tr <?php //echo $bg;?>>
	<td style="text-align:center;"><?php  echo ConvertSQLDate($row['invoiceDate'])?></td>
	<td><?php echo $acledger=$s->getField('accountledger','AccountsID',$row['ledger_id'],'AccountName');?></td>
	<td>Sales</td>
	<td><?php  echo $row['invoiceNo']?></td>
	<td style="text-align:right;"><?php  echo money_format('%!i',$row['BSM']);	$GT[]=$row['BSM'];	?></td>
	<td style="text-align:right;"><?php $debit=$row['discounted_amt']; echo money_format('%!i',$debit); $tot_deb+=$debit; ?></td>	
	<td style="text-align:center;"><a href="index.php?route=InvoiceDetails<?php echo $rurl;?>&action=view&InvoiceId=<?php echo $row['invoice_id'] ?>" target="_blank"><img src="images/search.gif" width="15" height="15" /></a></td>
  </tr>
 
	   <?php 
	   $data_asrep[]=array("invoiceDate"=>$row['invoiceDate'],"acledger"=>$acledger,"Sales"=>"Sales","invoiceNo"=>$row['invoiceNo'],"debit"=>$debit,"BSM"=>$row['BSM']);
	   $apr++;
	}
	$_SESSION['asrep']=$data_asrep;
  ?>
  
     <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td style="text-align:right;">Grand Total</td>      
	  <td style="text-align:right;"><?php  echo money_format('%!i',array_sum($GT));?></td> 
	  <td style="text-align:right;"><?php echo money_format('%!i',$tot_deb); ?></td>
	  <td>&nbsp;</td>
    </tr>
</table>
<?php } ?>