<?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');
 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); 
}




?>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<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>

<?php 
 if($_REQUEST['Save_Brought_forword'])
{
     for($i=0;$i<count($_REQUEST['Brought_forword_LedgerId']);$i++)
   {
   	
		$condition="ledger_id='".$_REQUEST['Brought_forword_LedgerId'][$i]."' and Companyid='".$_SESSION['ACGCompanyId']."' and Finyear='".$_SESSION['ACGFinYearId']."' and BranchId='".$_REQUEST['CBranch']."'  ";
		$chkstatus=DB_query($s->GetRecord('brought_opening_amount',  $condition),$db);
		 
		if(DB_num_rows($chkstatus)>0)
		{
			$s->DeleteRecord('brought_opening_amount',  $condition);
		}
 					if($_REQUEST['ACG_LedgerDR'][$i]!='')
					{
						$data_led1['ledger_id']=$_REQUEST['Brought_forword_LedgerId'][$i];
						$data_led1['finyear']=$_SESSION['ACGFinYearId'];	
						$data_led1['Amount']=$_REQUEST['ACG_LedgerDR'][$i];
						$data_led1['CompanyId']=$_SESSION['ACGCompanyId'];
						$data_led1['BranchId']=$_REQUEST['CBranch'];	
						$data_led1['amounttype']="DR";
						$rowinsert=$s->RecordInsert('brought_opening_amount', $data_led1);
						if(!$rowinsert){die('Error 101'.DB_error());} 
					}
					if($_REQUEST['ACG_LedgerCR'][$i]!='')
					{
						$data_led['ledger_id']=$_REQUEST['Brought_forword_LedgerId'][$i];
						$data_led['finyear']=$_SESSION['ACGFinYearId'];	
						$data_led['CompanyId']=$_SESSION['ACGCompanyId'];
						$data_led['amounttype']="CR";
						$data_led['BranchId']=$_REQUEST['CBranch'];						
						$data_led['Amount']=$_REQUEST['ACG_LedgerCR'][$i];		
						$rowinsert=$s->RecordInsert('brought_opening_amount', $data_led);
						if(!$rowinsert){die('Error 101'.DB_error());} 
					}
	       	
       }
// 	$s->pageLocation("Brought-Forword-BalanceSheetLedger.php?UnderGroup=".$_REQUEST['UnderGroup']."&CBranch=".$_REQUEST['CBranch']."");?>
<script language="javascript">
  opener.window.location="index.php?route=Brought-Forword-BalanceSheet&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&CBranch=<?php echo $_REQUEST['CBranch']?>&sdate=<?php echo $_REQUEST['sdate']?>";self.close(); 
</script>
<?php 
}
?>


 

<form action="" method="post">
<table border="0"   width="100%"  cellpadding="0" cellspacing="0" >
			<tr class="text"><th   >Ledger Name</th>
			 <th><strong>Opening DR</strong></th> 
			<th><strong>Opening CR</strong></th>			
			 </tr>
				<?php
				   $GetAccount=DB_query("select * from accountledger where UnderGroup='". $_REQUEST['UnderGroup']."' and CompanyId  ='".$_SESSION['ACGCompanyId']."'",$db);

					if(DB_num_rows($GetAccount)>0)
					{
					  $t='';
					  $t1='';
					 while($RowAccount=DB_fetch_array($GetAccount))
					 {
 					   $OpeningBalance= OpeningBalance($RowAccount['AccountsID'],$_REQUEST['CBranch']);
					   
					 ?>
					 <tr class="text"><td class="pad" width="40%">
					 <?php //print_r($OpeningBalance); ?>
					 <input name="Brought_forword_LedgerId[]" type="hidden" value="<?php  echo $RowAccount['AccountsID'];?>"  size="2"/>
					  <?php    
					      $clientval=GetClientId_ByCRM($RowAccount['AccountsID']);
 						 if($clientval!='')
						 { 
					  ?> 
					     <a href="#"  onclick='popup("Brought-TrialVoucherByClientId.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("Brought-TrialVoucherByClientId.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="10%">
					 <?php
 					    if($OpeningBalance[0]!=0)
					 	 $t[]=$OpeningBalance[0];
					    ?>
<input name="ACG_LedgerDR[]" type="text" value="<?php echo $OpeningBalance[0];?>" /></td>
					  <td width="10%">
					  <?php 
					     if($OpeningBalance[1]!=0)
					 	 $t1[]=$OpeningBalance[1];
					  ?>
					  <input name="ACG_LedgerCR[]" type="text" value="<?php echo $OpeningBalance[1];?>" />
  					   </td>
  </tr>
   					  
					<?php 			
					 }?>
					  <tr bgcolor="#333333" style="color:#FFFFFF">				  
 						 <td  ><div align="right"><strong>Total</strong>&nbsp;&nbsp;&nbsp;</div></td>
						  <td width="10%"><b><?php 
						   if(array_sum($t)!=0){
						    echo array_sum($t); 
						    }
						  ?></b></td>
						 <td width="10%"><b><?php
						   if(array_sum($t1)!=0){
						   echo array_sum($t1);
						   } ?></b></td>
			         </tr>
					  <tr  >
					    <td colspan="6" >
						<div align="center">
						<input name="Save_Brought_forword" type="submit"  value="Save"/></div></td>
                      </tr>
					 <?php 
					}
					?>
				</table>
 </form>				