<?php //include('includes/fun-php.php')?>
<?php 
if(isset($_REQUEST['reset'])){
  unset($_SESSION['FDATE']);
  unset($_SESSION['TDATE']);
}
error_reporting(E_ERROR | E_WARNING | E_PARSE);
?>
<script>
function chkdteformat(obj){
var pattern =/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;
  var input=$(obj).val();
  if(!pattern.test(input))
  {
      alert("Date should be in dd/mm/yyyy Format");
      $(obj).val("");
  }
}
</script>
 <form method="post" action="">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
  <tr class="row1">
   <td width="20%" align="right">From Date</td>
   <td width="30%"><input type="text" value="<?php if(isset($_SESSION['FDATE'])) echo ConvertSQLDate($_SESSION['FDATE']); ?>" name="fromDate" class="datepicker" style="width:98%;" placeholder="dd/mm/yyyy" onchange="chkdteformat(this)" /></td>
  <td width="20%" align="right">To Date</td>
  <td><input type="text" value="<?php if(isset($_SESSION['TDATE'])) echo ConvertSQLDate($_SESSION['TDATE']); ?>" name="toDate" class="datepicker" style="width:98%;" placeholder="dd/mm/yyyy" onchange="chkdteformat(this)" /></td>
</tr>
<tr class="row1">
  <td colspan="4" align="center" style="text-align:center;"><input type="hidden" name="tab" value="tab6" /><input type="submit" name="search6" value="Search"  class="search"/>
  <input type="submit" name="reset" value="Reset" class="search" /></td>
  </tr>
</table>
 </form>
 
<p align="right">
  <a href="APRExtractTrialReport_print.php?search6=Search" target="_blank" class="search">Print</a> <a href="APRExtractTrialReport_export.php?search6=Search" class="search">Export</a>
  </p>
 
 <table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
  <tr>
    <th>Ledger Name</th>
    <th width="12%" style="text-align:center;">Debit Amount</th>
    <th width="12%" style="text-align:center;">Credit Amount</th>
  </tr>
<?php 
        
if($_REQUEST['fromDate']!='' && $_REQUEST['toDate']!='')
$condition="A.Dates between '".FormatDateForSQL($_REQUEST['fromDate'])."' and '".FormatDateForSQL($_REQUEST['toDate'])."'";
else 
$condition="A.Dates between '".$_SESSION['financialYrStart']."' and '".$_SESSION['financialYrEnd']."'"  ;      

  $GetRec ="SELECT B.ledgerid,B.amount,B.amount_type  FROM account_book A inner join account_book_details B
    on A.id=B.account_bookid  WHERE  $condition and A.cancel_status=0 and A.trans_type='PURCHASE BILL'  
     ORDER BY A.id ASC ";
      $ResultData5=DB_query($GetRec,$db);
      $numrows5 = DB_num_rows($ResultData5);
      if($numrows5>0)
      {
      while($RowData5=DB_fetch_array($ResultData5))
      {
        $ledgerid[]=$RowData5['ledgerid'];
        if($RowData5['amount_type']=='CR')
        $ledgerid_cr[$RowData5['ledgerid']][]=$RowData5['amount'];
       if($RowData5['amount_type']=='DR')
        $ledgerid_dr[$RowData5['ledgerid']][]=$RowData5['amount'];
      }        
            
     $ledgerid=array_unique($ledgerid);        
                    
      if(!empty($ledgerid))
        {   
          $AMTDR=0; $AMTCR=0; 
        foreach($ledgerid as $k=>$val)
         { 
         ?>        
      
      <tr>      
        <td><?php  echo $ledger_name= ucfirst($s->GetFieldValue('accountledger',"AccountsID='".$val."'",'AccountName'));    ?></td>           
        <td style="text-align:right;"><?php  $AMTDR=0;        
          if(!empty($ledgerid_dr[$val])) echo $AMTDR=array_sum($ledgerid_dr[$val]); else  echo $AMTDR=0;    $TDR[]= $AMTDR;
         ?></td>
        <td style="text-align:right;"><?php $AMTCR=0;         
         if(!empty($ledgerid_cr[$val])) echo $AMTCR=array_sum($ledgerid_cr[$val]); else  echo $AMTCR=0;
        $TCR[]= $AMTCR;
        ?></td>       
      </tr>
      <?php  
       $data_pr_trial[]=array("ledger"=>$ledger_name,"debit"=>$AMTDR,"credit"=>$AMTCR);       
         }
         ?>
      <tr>      
        <td style="text-align:right">Total</strong></td>           
        <td style="text-align:right;"><?php echo array_sum($TDR); ?></strong></td>
        <td style="text-align:right;"><?php echo array_sum($TCR); ?></strong></td>       
      </tr>
         <?php
         $_SESSION['data_pr_trial']=$data_pr_trial;
       }      
       else
       {
      ?><tr ><td colspan="3" valign="top" style="border-bottom:1px solid #333333;"></td>
         </tr>
     <?php }
         }
        
      ?>      
     </table>
