<?php include('fun-Trial.php'); ?>

<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 name="form1" method="post" action=""> 

<table width="100%" border="0" cellpadding="3" cellspacing="0">
  <tr class="row1">
    <td width="12%" align="right">Ledger</td>
	<td width="20%">
 	  <select name="GroupSummaryLedger" id="GroupSummaryLedger" >	    
    <?php 
	$dashVal = "&nbsp;&nbsp;&nbsp;";
	$sqlCate = "select * from  accountledger  where  1=1 order by AccountName";
	$rsCate  = DB_query($sqlCate,$db);
	if(DB_num_rows($rsCate)>0)
	{
		while($rowCate =DB_fetch_array($rsCate))
		{
 			 
				$px='';
				 if($rowCate['AccountsID']==$_REQUEST['GroupSummaryLedger'])
				 $px="selected";
		
		  ?>
		<option value="<?php echo $rowCate['AccountsID'];?>"<?php echo $px ?>>		<?php echo  $rowCate['AccountName'] ;?></option>
 		<?php			
		 
		}
	}
?></select>
	</td>    
	<td width="12%" align="right">From Date</td>
    <td width="20%"><input type="text" name="from_date" value="<?php if(isset($_REQUEST['from_date']))echo ($_REQUEST['from_date']);?>" class="datepicker" placeholder="dd/mm/yyyy" onchange="chkdteformat(this)"  /></td>
	<td width="12%" align="right">To Date</td>
	<td><input type="text" name="to_date" value="<?php if(isset($_REQUEST['to_date']))echo ($_REQUEST['to_date']);?>" class="datepicker" placeholder="dd/mm/yyyy" onchange="chkdteformat(this)"  /></td>
</tr>
<tr class="row1">
    <td colspan="6" align="center" valign="middle">
      <input type="submit" name="Go" value="Search" class="search" /> <input type="submit" name="Reset" value="Reset" class="search" />
    </td>
  </tr>
</table>
</form>

<?php 
  if($_REQUEST['Go'])
  {
	   $AccountsID=$_REQUEST['GroupSummaryLedger'];
	 $_SESSION['AccountsID']=$AccountsID;
	 
	 
	 
  ?>
   <p align="right">
  <a href="APRLedgerWiseSummary-print.php" target="_blank" class="search">Print</a> <a href="APRLedgerWiseSummary_exl.php" class="search">Export</a>
  </p>
  <table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
  <tr>
    <th>Particulars</th>
	<th width="10%" style="text-align:center;">Opening DR</th>
	<th width="10%" style="text-align:center;">Opening CR</th>
    <th width="10%" style="text-align:center;">Debit Amount</th>
    <th width="10%" style="text-align:center;">Credit Amount</th>
	<th width="10%" style="text-align:center;">Closing Amount</th>
  </tr>
  
    <?php   //echo "select * from accountledger where AccountsID ='". $AccountsID."'";
	$TDR=array();$TCR=array();
  	
	if($AccountsID!='000')
  	 $GetLedger=DB_query("select * from accountledger where AccountsID ='".$AccountsID."' ",$db);
	 else
	 $GetLedger=DB_query("select * from accountledger where 1=1",$db);
	 
	 $opening_amt='';
	$opening_bal='';
	$opening_type='';
	$dramt='';
	$cramt='';
	 
	if(DB_num_rows($GetLedger)>0)
	{
	 while($RowGetLedger=DB_fetch_array($GetLedger))
	 {
	 	if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
		{
			  $from_date=ConvertSQLDate($_REQUEST['from_date']);
		      $to_date=ConvertSQLDate($_REQUEST['to_date']);			  
		
			$query_details="Select (B.openingBalance) as openingBalance,B.openingtype as openingtype from accountledger A inner join accountledger_dtls B on A.AccountsID=B.accountledger_id
			 where A.AccountsID in ($AccountsID) and B.openingdate<='".$from_date."'";		
     }
	else
	{	 
		 $query_details="Select (B.openingBalance) as openingBalance,B.openingtype as openingtype from accountledger A inner join accountledger_dtls B on A.AccountsID=B.accountledger_id
		 where A.AccountsID in ($AccountsID) ";	    

	//$opening_bal=$s->getField('accountledger','AccountsID',$ledger_id,'openingBalance');
    }
    //echo $query_details;
    $sql_check_dtls=DB_query($query_details,$db);
     while($row_dtls=DB_fetch_array($sql_check_dtls))
	        {

	         $opening_amt[$row_dtls['openingtype']][]=$row_dtls['openingBalance']; 
	        }         

	      if(!empty($opening_amt[DR]))
	      {
	          $openingDR=array_sum($opening_amt[DR]);
	          $opening_type='DR';  
	       }   
	          if(!empty($opening_amt[CR]))
	          {	
	          $openingCR=array_sum($opening_amt[CR]);
	           $opening_type='CR';  
	          }  	 
	 
	 if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	 {
	 	   $from_date=ConvertSQLDate($_REQUEST['from_date']);
	       $to_date=ConvertSQLDate($_REQUEST['to_date']);		  
		   $TDR= Get_AccountByLedger($RowGetLedger['AccountsID'],'DR',$from_date,$to_date);
	  	   $TCR= Get_AccountByLedger($RowGetLedger['AccountsID'],'CR',$from_date,$to_date);
			//echo "++++".$TDR."<br />";	
	 }
	 else
	 {
	   $TDR= Get_AccountByLedger($RowGetLedger['AccountsID'],'DR','','');
	   $TCR= Get_AccountByLedger($RowGetLedger['AccountsID'],'CR','',''); 
	   
	 }
	 
	 $total_DR=  $TDR;
	 $total_CR=  $TCR;	 
	
		//echo "<br /><br />DR1---".$openingDR;
		//echo "<br /><br />CR1---".$openingCR;
	$OPDR=$openingDR; $OPCR=$openingCR;
	 ?>
  <tr>
        <td><a onclick='popup("LedgerSummary.php?id=<?php  echo $RowGetLedger['AccountsID'].$rurl?> ",700,500)' style="cursor:pointer" ><?php echo $RowGetLedger['AccountName']; ?></a></td>
	    <td align="right"><?php echo $OPDR;?></td>
	    <td align="right"><?php echo $OPCR;?></td>
	    <td align="right"><?php echo $total_DR; ?></td>
	    <td align="right"><?php echo $total_CR; ?></td>
		<td align="right"><b>
		<?php 
		$GtotalDR=$OPDR+$total_DR;
		$GtotalCR=$OPCR+$total_CR;
		if($GtotalDR>$GtotalCR){$closing=$GtotalDR-$GtotalCR;$closing_type="DR";}elseif($GtotalCR>$GtotalDR){$closing=$GtotalCR-$GtotalDR;$closing_type="CR";}
		else{$GtotalDR='';$GtotalCR='';}
		echo $closing.' '.$closing_type;
		?></b></td>
  </tr>
  <?php } 
  }?>
</table>
  <?php
  }
?> 