<?php  include('includes/SQL_CommonFunctions.inc');
 $PageSecurity = 10;
 include('Trial_function.php');
   include('includes/PaginationClass.php');
 unset($_SESSION['GrandDebit']); 
unset($_SESSION['GrandCredit']);
unset($_SESSION['Grand_Closing_Debit']); 
unset($_SESSION['Grand_Closing_Credit']);

$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
 $url="&modid=".$modid."&frmid=".$frmid;

if($_REQUEST['Search'])
{
 	$CBranch=implode(",",$_REQUEST['branch']);
	$to_date=$_REQUEST['to_date'];
	$from_date=$_REQUEST['from_date'] ;
 	$_SESSION['Trial_GroupName']=implode(",",$_REQUEST['GroupName']); 
 $Trial_GroupName= SeperateByCommes($_SESSION['Trial_GroupName']);

 }
function CheckClient($ledgerid)
{ global $db;
$t=0;
 $Get=DB_query("select * from clientmaster where ledger_id='".$ledgerid."' ",$db);
  	 while($Rowc=DB_fetch_array($Get))  
   	{
 			 $GetGT_invoice=DB_query("select sum(grand_total) as total  from invoice where client_id='".$Rowc['id'] ."'",$db);
			 $RowGT_invoice=DB_fetch_array($GetGT_invoice);
			 $t+= $RowGT_invoice['total'];
    }
	return($t);
 }
function CountMaster_Subledger($ledgerid)
{ 
	global $db;
 $GetRecord=DB_query("select * from mastersubledger where UnderLedger='".$ledgerid."' ",$db);
 $n= DB_num_rows($GetRecord);
 return($n); 
}
 function DisplayLedger( $PcateID,$UnderGroup, $dashVal,$CBranch)
	{
		$modid=$_REQUEST['modid'];
		$frmid=$_REQUEST['frmid'];
		 $url="&modid=".$modid."&frmid=".$frmid;

	 
	   // $Trial_GroupName= SeperateByCommes($_SESSION['Trial_GroupName']);

	    global $db;
		$dashVal .=   $dashVal ;
		$sqlCate = "select * from accountgroupmaster  where  MainGroupId = '$PcateID'  and CompanyId  ='".$_SESSION['ACGCompanyId']."'";//and GroupId in ($Trial_GroupName) 
		$rsCate  = DB_query($sqlCate,$db);
 		if(DB_num_rows($rsCate)>0)
		{?>
	<?php 		
			while($rowCate = DB_fetch_array($rsCate))
			{
			  	$sqlCateSel = "select * from accountgroupmaster where  MainGroupId='".$rowCate['GroupId']."' and   CompanyId  ='".$_SESSION['ACGCompanyId']."'  ";// andGroupId in ($Trial_GroupName)
 				$rsCateSel	= DB_query($sqlCateSel,$db);
				$GTDebit=array();$GTCredit=array();
?>
				<tr    style="background-color:#666666; color:#FFFFFF">
				<td  width="40%"  > 
				<b><?php echo $dashVal;?><?php echo $rowCate['GroupName'];?></b> 
				</td>
				  	 <td width="10%">&nbsp; </td>
					 <td width="10%">&nbsp;</td>
 					 <td width="10%">&nbsp;</td>
 					 <td width="10%">&nbsp;</td>
					  <td width="10%">&nbsp;</td>
					 <td width="10%">&nbsp;</td>
				</tr>
				 <tr  ><td width="40%"  colspan="8"> 
				<!---------------------CaLL ALL LEDGER---------------------------------->
				
				<table border="0"   width="100%"  cellpadding="0" cellspacing="0" >
<?php

$GetAccount=DB_query("select * from accountledger where UnderGroup='".$rowCate['GroupId']."' and CompanyId  ='".$_SESSION['ACGCompanyId']."'",$db);
					if(DB_num_rows($GetAccount)>0)
					{
					 while($RowAccount=DB_fetch_array($GetAccount))
					 {
					 $dr='';$cr=''; $cr_jv='';$dr_jv='';
					 
					
					 $GetLedgerName=GetLederNameByAccountId($RowAccount['AccountsID']);
					 
					 $dr1=array();$cr1=array();
 						$JvEntry= SumJV_ByTrans($RowAccount['AccountsID']);
						for($kp=0;$kp<count($JvEntry);$kp++)
						  {
							$split=$JvEntry[$kp];
							 
							 if($split[0]==0.00)
							 {
								$cr_jv=$split[1];
  							 }
							 else{
								$dr_jv=$split[0];
  							 }
						  }
					 
					 
 					   $CashBankPayment= CountCashBankPayment($RowAccount['AccountsID'],$CBranch);
 						   for($kp=0;$kp<count($CashBankPayment);$kp++)
						  {
							$split=$CashBankPayment[$kp];
							 
							 if($split[0]==0.00)
							 {
								$cr=$split[1];
								//$_SESSION['GrandCredit'][]=$cr;
 							 }
							 else{
								$dr=$split[0];
							//	$_SESSION['GrandDebit'][]=$dr;
 							 }
						  }
						  
 					     $voucherdata= CountCashBankPaymentByPrefix($RowAccount['AccountsID'],$CBranch);
						  for($kp=0;$kp<count($voucherdata);$kp++)
						  {
						  	$split=$voucherdata[$kp];
 							 if($split[0]=='Cr')
							 {
 								$cr1[]=$split[3];
							//	$_SESSION['GrandCredit'][]=$split[3];
								 
							 }
							  if($split[0]=='Dr'){
							   $dr1[]=$split[2];
							  // $_SESSION['GrandDebit'][]=$split[2];
							    
							  }
 						  }
 					   $klp=exciseTax($RowAccount['AccountsID']);	
					   $Sales_TaxByLedger=SalesTaxByLedger($RowAccount['AccountsID'],$CBranch);
					   if( $klp>0)					  
					   {
					   		 $exciseTax=0;
					   	     $exciseval=invoice_ExciseTotalByLedger($RowAccount['AccountsID'],$CBranch);
						     $eduval=invoice_edu_cess_TotalByLedger($RowAccount['AccountsID'],$CBranch);
						     $she_cessval=invoice_she_cess_TotalByLedger($RowAccount['AccountsID'],$CBranch);
 						   	 $exciseTax=$exciseval+$eduval+$she_cessval ;
					   }
 					  //**************************************************************//
 					 $cashBankvoucher_amount=GetCashBank_amount($RowAccount['AccountsID'],$CBranch);
					 if($cashBankvoucher_amount=='')
					 {	 $cashBankvoucher_amount=0;	 }
					  //**************************************************************//
   					   $sql_VPrefix=DB_query("select VPrefix from voucher_master where VoucherType ='".$val1[2]."'",$db);
					   $RVPrefix=DB_fetch_array($sql_VPrefix);
 					     $VPrefix= $RVPrefix['VPrefix'];
 					 ?>
					  <tr class="text"><td class="pad" width="40%">
					  <?php  echo  $RowAccount['AccountsID'];?>
 					  
					<?php   
				 	  $n=CountMaster_Subledger($RowAccount['AccountsID']);
 					if($n>0)
					{
					 $ChkInSub=DB_query("select * from mastersubledger where UnderLedger='". $RowAccount['AccountsID']."'",$db);
  					 if(DB_num_rows($ChkInSub)>0){?>
					  <a href="#"  onclick='popup("main.php?route=TrialSubLedger&LedgId=<?php  echo  $RowAccount['AccountsID'] ?>",900,500)'><input name="+" type="button" value="-" /></a>
 					  <?php }
					 } 
					 // $n1=DirectJournalLedger($RowAccount['AccountsID']);
					  else//if($n1>0)
					  {
					      $clientval=GetClientId_ByCRM($RowAccount['AccountsID']);
					 
						 if($clientval!='')
						 {
						/*  onclick='popup("main.php?route=TrialVoucher&CashBankId=<?php echo $RowAccount['AccountsID'];?>&Lid=<?php  echo  $RowAccount['AccountsID'] ?>&ClientId=<?php echo GetClientId_ByCRM($RowAccount['AccountsID']); ?>",900,500)'*/
					  ?> 
					    <a href="#"  onclick='popup("Popup_Branch.php?CashBankId=<?php echo $RowAccount['AccountsID'];?>&Lid=<?php  echo  $RowAccount['AccountsID'] ;?><?php echo $url;?>&ClientId=<?php echo $clientval;?> ",900,500)'><input name="+" type="button" value="-" /> </a>
						<?php 
						}else
						{
 //	onclick='popup("main.php?route=TrialVoucher&CashBankId=<?php echo $RowAccount['AccountsID'];&Lid=<?php  echo  $RowAccount['AccountsID']  ",900,500)'
						 ?> 
					    <a href="#"  onclick='popup("Popup_Branch.php?CashBankId=<?php echo $RowAccount['AccountsID'];?>&Lid=<?php  echo  $RowAccount['AccountsID'] ;?>&Ledgername=<?php echo $GetLedgerName.$url ; ?>",900,500)' ><input name="+" type="button" value="-" /></a>
						<?php 
						}
					  } 
					  ?>
					  <?php echo $dashVal."-".$RowAccount['AccountName'];
					  
					   $t='';
  				     // echo  $GET_CRM_INVOICE= GetClientLedgerID_ByCRM($RowAccount['AccountsID'],$CBranch);
						$t= array_sum($dr1)+$dr+$dr_jv+invoiceTotalByClient($RowAccount['AccountsID'],$CBranch);
						 $t1='';
						 if( $klp>0){
  						 $t1= array_sum($cr1)+$cr_jv+$cr+invoiceTotalByLedger($RowAccount['AccountsID'],$CBranch)+$exciseTax+$cashBankvoucher_amount ;
						}else
						{
						  $t1=  array_sum($cr1)+$cr_jv+$cr+invoiceTotalByLedger($RowAccount['AccountsID'],$CBranch)+$cashBankvoucher_amount;
						}
						if($Sales_TaxByLedger!=0){
						$t1+=$Sales_TaxByLedger;
						}
						
					  ?></td>
					  <td width="11%">
					  <?php  
					   if($t>$t1)
					   { echo $t-$t1;
					   $_SESSION['Grand_Closing_Debit'][]=$t-$t1;
					   }
 					   ?>
					  
					  </td>
					 <td width="10%"><?php 	 			 
					  if($t1>$t){ 
					  echo $t1-$t;
					  $_SESSION['Grand_Closing_Credit'][]=$t1-$t;
					  }
					  ?></td>
 					 <td width="10%"><?php //echo $Sales_TaxByLedger; ?> &nbsp;</td>
					  
					 <td width="10%">&nbsp;</td>
					  <td width="10%"><?php  // $val= CountJournelVoucher_Enter($RowAccount['AccountsID']);
						if($t!=0)
						echo $t;
						 $GTDebit[]=$t;
						// echo "<br>----<br>";
					if($t!=0){
					 $_SESSION['GrandDebit'][]=$t;
					 }
					   ?></td>
 					  <td width="10%"><?php 
						 
						 
						
						
 					 if($t1!=0)
						echo $t1;
						 $GTCredit[]=$t1;
						 if($t1!=0){
						  $_SESSION['GrandCredit'][]=$t1;
						  }

					  ?></td>	
	  				  </tr>
  					  <tr><td colspan="8"><hr /><?php //echo ViewLedger($RowAccount['AccountsID'],"=>"); ?></td>
 					  </tr>
  					 
  					  
					<?php 			
					 }?>
					  <tr bgcolor="#333333" style="color:#FFFFFF">				  
 						 <td colspan="5" ><div align="right"><strong>Total</strong>&nbsp;&nbsp;&nbsp;</div></td>
						  <td width="10%"><b><?php 
						   if(array_sum($GTDebit)!=0){
						  echo array_sum($GTDebit); 
						  }?></b></td>
						 <td width="10%"><b><?php
						  if(array_sum($GTCredit)!=0){
						   echo array_sum($GTCredit);
						   }
						   ?></b></td>
			         </tr>
					 <?php 
					}
					?>
				</table>
 				<!---------------------CaLL ALL LEDGER---------------------------------->
				</td>
  				</tr>
 				<tr><td>
				<?php			
 			        DisplayLedger($rowCate['GroupId'], $UnderGroup,'&nbsp;&nbsp;&nbsp;&nbsp;',$CBranch);
				  ?>
				  
				  </td></tr>
	<?php 		}?>
	
	    	<!--<tr bgcolor="#CCCCCC"; style="color:#000000; font-weight:bold">
			<td colspan="5"  ><div align="right"><strong>Grand Total</strong></div></td>
			<td > 
			<?php //echo array_sum($_SESSION['GrandDebit']);?></td>
			<td ><?php // echo array_sum($_SESSION['GrandCredit']);?></td>			
			</tr>-->

	<?php
		}
		 
	}
	
	
	?>
 
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td ><div align="right"><A HREF="javascript:history.go(-1)"> [Go Back]</A> </div></td>
  </tr>
</table>
<?php 
if($_REQUEST['Search'])
{
 ?>
	<table width="100%" border="0"  cellpadding="2" cellspacing="2" >
	<tr>
 	<td colspan="8"><div  align="right"><?php echo 	$s->getField('companies','coycode',1,'CoyName');?></div></td>
	<tr>
	 
 	<td colspan="8"><div  align="right">Trial Balance (<?php echo $from_date; ?> To <?php echo $to_date;?>)</div></td>
	<tr>
	 
 	<td colspan="8"><hr /></td>
	<tr>
	 
	<td width="40%">Group</td>

	<td colspan="2">Closing Balance</td>
	<td colspan="2">Opening Balance</td>
	<td colspan="2">During Period</td>
	<tr>
	<th width="40%">Description</th>
 	<th width="10%">Debit(INR)</th>
	<th width="10%">Credit(INR)</th>
	<th width="10%">Debit(INR)</th>
	<th width="10%">Credit(INR)</th>
	<th width="10%">Debit(INR)</th>
	<th width="10%">Credit(INR)</th>
 	</tr>
	</table>
	<div style="overflow:auto; height:450px;">
	<table width="100%" border="0"  cellpadding="2" cellspacing="2" >

 <?php 
		$_SESSION['GrandDebit']=array();
		$_SESSION['GrandCredit']=array();
		$_SESSION['Grand_Closing_Debit']=array();
		$_SESSION['Grand_Closing_Credit']=array();


if($_REQUEST['UnderGroup']!='')
$UnderGroup=$_REQUEST['UnderGroup'];
if($_REQUEST['CBranch']!='')
$CBranch=$_REQUEST['CBranch'];
 
 
 $rx=AccountGroupMaster_Parent($UnderGroup);
if($rx!=0)
$GetRecord=DB_query("select * from accountgroupmaster where MainGroupId=0  and GroupId='".$rx."'and  CompanyId  ='".$_SESSION['ACGCompanyId']."'",$db);
else
$GetRecord=DB_query("select * from accountgroupmaster where MainGroupId=0 and  GroupId in($Trial_GroupName)and  CompanyId  ='".$_SESSION['ACGCompanyId']."'",$db);

 
 while($RowData=DB_fetch_array($GetRecord))
 {
 	?>
	 <tr>
    <td width="100%" bgcolor="#FFCC99" style="color:#000000" colspan="8"><b><?php echo  $RowData['GroupName']; ?> </b>	</td>				
	</tr>
	 <tr>
	   <td colspan="8">
<?php $PcateID=$RowData['GroupId'];		
if($UnderGroup!='')
{ 
  DisplayLedger($PcateID,$UnderGroup,  '-',$CBranch);
}
else{
 			 DisplayLedger($PcateID,'',  '-',$CBranch);
 }
		?></td>
      </tr>
<?php 			 
  }?>
  
  <tr>
  <td><strong>Total</strong></td>  <td> <?php echo array_sum($_SESSION['Grand_Closing_Debit']);?></td>  
  <td> <?php  echo array_sum($_SESSION['Grand_Closing_Credit']);?></td>
   <td></td>
  <td ></td>
  <td><strong>Debit</strong>:	
    <?php 
// echo "<pre>";
// print_r($_SESSION['GrandDebit']);
  echo array_sum($_SESSION['GrandDebit']);?></td>
  <td><strong>Credit</strong>:
    <?php 
	
	 //echo "<pre>";
 //print_r($_SESSION['GrandCredit']);
	 echo array_sum($_SESSION['GrandCredit']);?></td>
   </tr>
   </table>
</div>
<?php } ?>