<?php   include('includes/SQL_CommonFunctions.inc');
 $PageSecurity = 10;
include('includes/session.inc');
include('Trial_function.php');
$title = _('Chart of Accounts Maintenance');
  include('includes/PaginationClass.php');
 include('includes/function_lib.php');
include('includes/header.inc');?>
 <?php 
 unset($_SESSION['GrandDebit']); 
unset($_SESSION['GrandCredit']); 
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); 
}

?>
<div class="contentmain"> 
<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>
<center><h2>Ledger </h2></center>
<table border="0"   width="100%"  cellpadding="0" cellspacing="0" >
			<tr class="text"><th colspan="4"  >Ledger Name</th>
			 <th>DR</th> 
			<th>CR</th>			
			 </tr>
				<?php
				   $GetAccount=DB_query("select * from accountledger where UnderGroup='". $_REQUEST['UnderGroup']."' 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'],$_REQUEST['CBranch']);
						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'],$_REQUEST['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'],$_REQUEST['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($GetLedgerName);	
					 	$Sales_TaxByLedger=SalesTaxByLedger($GetLedgerName,$_REQUEST['CBranch']);
					   if( $klp>0)					  
					   {
					   		$exciseTax=0;
					   	    $exciseval=invoice_ExciseTotalByLedger($GetLedgerName,$_REQUEST['CBranch']);
						    $eduval=invoice_edu_cess_TotalByLedger($GetLedgerName,$_REQUEST['CBranch']);
						    $she_cessval=invoice_she_cess_TotalByLedger($GetLedgerName,$_REQUEST['CBranch']);
 						   	$exciseTax=$exciseval+$eduval+$she_cessval ;
					   }
					  
					  //**************************************************************//
 
					 $cashBankvoucher_amount=GetCashBank_amount($GetLedgerName,$_REQUEST['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 
					  /*
 					  $Chk_client=DB_query("select * from clientmaster where  ledger_id='".$RowAccount['AccountsID'] ."'  ",$db);
					   if(DB_num_rows($Chk_client)>0){?>
					  <a href="#"  onclick='popup("ViewClientMaster.php?Cid=<?php  echo  $RowAccount['AccountsID'] ?>",700,600)'><input name="+" type="button" value="+" /></a>
					  <?php } */?>
				 
					  <?php
					      $clientval=GetClientId_ByCRM($RowAccount['AccountsID']);
					 
						 if($clientval!='')
						 {
					  ?> 
					    <a href="#"  onclick='popup("Popup_Branch.php?CashBankId=<?php echo $RowAccount['AccountsID'];?>&Lid=<?php  echo  $RowAccount['AccountsID'] ;?>&ClientId=<?php echo $clientval;?>&CBranch=<?php echo $_REQUEST['CBranch']?>",900,500)'><input name="+" type="button" value="-" /></a>
						<?php 
						}else
						{
						 ?> 
					    <a href="#"  onclick='popup("Popup_Branch.php?CashBankId=<?php echo $RowAccount['AccountsID'];?>&Lid=<?php  echo  $RowAccount['AccountsID'] ;?>&Ledgername=<?php echo $GetLedgerName; ?>&CBranch=<?php echo $_REQUEST['CBranch']?>",900,500)'",900,500)" >
						<input name="+" type="button" value="-" /></a>
						<?php 
						}
					    
					  ?>
					  <?php echo $dashVal."-".$RowAccount['AccountName'];?></td>
					   
					  <td width="11%"></td>
					 <td width="10%"><?php 
					// echo  $klp;
					/*echo "<br>1-".$exciseval;
					echo "<br>2-".$eduval;
					echo "<br>3-".$she_cessval;*/
					  ?></td>
 					 <td width="10%"><?php //echo $Sales_TaxByLedger; ?> &nbsp;</td>
					  
					 <td width="10%"><?php  // $val= CountJournelVoucher_Enter($RowAccount['AccountsID']);
		$t=0;					   
		$GET_CRM_INVOICE= GetClientLedgerID_ByCRM($RowAccount['AccountsID'],$_REQUEST['CBranch']);
		  $t= array_sum($dr1)+$dr+$dr_jv+invoiceTotalByClient($RowAccount['AccountsID'],$_REQUEST['CBranch'])+$GET_CRM_INVOICE;
						 
						if($t!=0)
						echo $t;
						 $GTDebit[]=$t;
						// echo "<br>----<br>";
					if($t!=0){
					 $_SESSION['GrandDebit'][]=$t;
					 }
					   ?></td>
					  <td width="10%"><?php 
						  $t1=0;
						  if( $klp>0){

 						$t1= array_sum($cr1)+$cr_jv+$cr+invoiceTotalByLedger($RowAccount['AccountsID'],$_REQUEST['CBranch'])+$exciseTax+$cashBankvoucher_amount ;
						}else
						{
						  $t1=  array_sum($cr1)+$cr_jv+$cr+invoiceTotalByLedger($RowAccount['AccountsID'],$_REQUEST['CBranch'])+$cashBankvoucher_amount;
						}
 						if($Sales_TaxByLedger!=0){
						$t1+=$Sales_TaxByLedger;
						}
 					 if($t1!=0)
						echo $t1;
						 $GTCredit[]=$t1;
						 if($t1!=0){
						  $_SESSION['GrandCredit'][]=$t1;
						  }
 					  ?></td>
  </tr>
   					
  					 
  					  
					<?php 			
					 }?>
					  <tr bgcolor="#333333" style="color:#FFFFFF">				  
 						 <td colspan="4" ><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>
				
				</div>