<?php  include('Trial_function.php');?>
 <center><h1><?php //echo getFieldCRM('xpert_organizationdetails','id',$_SESSION['ACGCompanyId'],'organizationname'); ?></h1>  </center>

 <?php
  function CategoryShow($pcode, $PcateID, $dashVal,$branch_name,$sdate)
		{
			global $db;
			$dashVal = $dashVal . "-";
			$sqlCate = "select * from accountgroupmaster  where  MainGroupId = '".$PcateID."'";
			$rsCate  = DB_query($sqlCate,$db);	
			if(DB_num_rows($rsCate)>0)
			{
				while($rowCate = DB_fetch_array($rsCate))
				{
					 $sqlCateSel = "select * from accountgroupmaster where GroupId  =".$rowCate['GroupId']." and CompanyId='".$_SESSION['ACGCompanyId']."' ";
					 $rsCateSel	= DB_query($sqlCateSel,$db);	
					
			         $count_ledger = "select * from accountledger where
					 UnderGroup  =".$rowCate['GroupId']." and CompanyId='".$_SESSION['ACGCompanyId']."' ";					 
					 $rs_ledger	= DB_query($count_ledger,$db);
 					 $numrows = DB_num_rows($rs_ledger);					
					 echo '<tr><td width="60%">'.$dashVal.$rowCate['GroupId'].$rowCate['GroupName']."</td><td width='10%'>";
					 if($numrows>0){
					 $ledid=array();
					   while($rx=DB_fetch_array($rs_ledger))
					   {
					      $ledid[]=$rx['AccountsID'];
					   }
					   $rx=implode(',',$ledid);
					   
					 //BalanceSheetLedger.php?UnderGroup=51&CBranch=1
					 ?>
					 <input name="Val" value='>>' type='button'  onclick='popup("Brought-Forword-BalanceSheetLedger.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&UnderGroup=<?php echo $rowCate['GroupId'];?>&CBranch=<?php echo $branch_name."&sdate=".$_REQUEST['sdate']; ?>",1000,500)'/>
					 <?php }
					 /*$p=TrialBalance_Amount($rx,$branch_name,$sdate);
					 if($p[0]>$p[1]){					 
					 $typeCRDR='CR';
					 $diff=$p[0]-$p[1];
					 }
					 else{
 					 $typeCRDR='DR';
					 $diff=$p[1]-$p[0];
					 }*/
					 
					 echo "</td>";
					 echo "<td width='15%'>";
					 $OB= Brought_OpeningBalance($rowCate['GroupId'],$_SESSION['ACGFinYearId'],$branch_name);
					// print_r($OB);
					if($OB[1]=='CR') $u=$OB[0] ; else $u=0 ;
					 echo "<input name='Brough_Forword_OpeningBalance[]' type='text' size='9' value='".$u."' /></td>";
					  echo"<td width='15%'> ";
					  if($OB[1]=='DR')$u1=$OB[0] ; else  $u1=0 ;
					  	

					 echo "<input name='Brough_Forword_OpeningBalance[]' type='text' size='9' value='".$u1."' />";
					 if($u>$u1){					 
					  $diff=$u-$u1;
					 }
					 else{
 					  $diff=$u1-$u;
					 }
 					 echo "</td></tr>";
   				     CategoryShow( $pcode,$rowCate['GroupId'], $dashVal,$branch_name,$sdate);
				}
			}
		}  
 
 
  
$branch_name=$_REQUEST['CBranch'];
$sdate=$_REQUEST['sdate'];
$dd=explode("#",$sdate);
$todate=$dd[0];
$fromdate=$dd[1];

$sdate='';
?>
  
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><b><div align="center">Balance Sheet - As On (<?php echo $todate;?> to <?php echo $fromdate?> )</div></b></td></tr>
<tr>
  <td> <div align="center"> Branch : 
<?php    echo $branch_name;?>
</div></td>
</tr>
<tr>
  <td>
  <table width="100%" border="0"cellspacing="0" cellpadding="0">
    <tr>
      <td width="50%"><div align="center"><strong>Liabilities</strong></div></td>
      <td width="50%"><div align="center"><strong>Assets</strong></div></td>
    </tr>
    <tr>
      <td valign="top">
	  <table width="100%" border="1" cellspacing="0" cellpadding="0">
        <tr>
          <td width="60%"><div align="center"><strong>Description</strong></div></td>
          <td width="10%"><div align="center"><strong>Sch</strong>.</div></td>
          <td width="15%" ><div align="center"><strong>CR </strong></div></td>
		     <td width="15%"><div align="center"><strong>DR</strong></div></td>
        </tr>
        <?php 
		$table='accountgroupmaster';
		$condition="GroupName='Liabilities' and MainGroupId='0' and CompanyId='".$_SESSION['ACGCompanyId']."'";
		$GetRecord = "Select * from $table where $condition ";
		$ResultData = DB_query($GetRecord,$db);
	  	while($RowData = DB_fetch_array($ResultData))
  		 { 
			$pcode=$RowData['GroupId'];
			$dashVal = "&nbsp;&nbsp;&nbsp;";
			$condition1="MainGroupId='".$RowData['GroupId']."'   and CompanyId='".$_SESSION['ACGCompanyId']."'";
			$data_Liabilities=DB_query( "Select * from $table where $condition1" ,$db);
			while($r_liabilities=DB_fetch_array($data_Liabilities))
			{
 		?>
 		<tr>
         <td colspan="4">
		      <?php echo ucwords($r_liabilities['GroupName']);?>
		  	  <table width="100%" border="1" cellspacing="0" cellpadding="0">
			  <?php  echo CategoryShow($pcode, $r_liabilities['GroupId'], $dashVal,$branch_name, $sdate); ?>
 			  </table>			  </td>
         </tr>
	<?php 
		}
	} ?>	
		  <tr>
		   <td colspan="4" > <div align="right" style="margin-right:5px"> </div></td>
 		  </tr>
      </table></td>
      <td valign="top">
	  <table width="100%" border="1" cellspacing="0" cellpadding="0">
        <tr>
          <td width="60%"><div align="center"><strong>Description</strong></div></td>
          <td width="10%"><div align="center"><strong>Sch.</strong></div></td>
		  <td width="15%" ><div align="center"><strong> CR</strong></div></td>
          <td width="15%"><div align="center"><strong> DR</strong></div></td>
        </tr>
       <?php    
	    $_SESSION['TotalBalanceAmount']=0;
	    $_SESSION['TotalBalanceCRAmount']=0;
		$condition="GroupName='Assets' and MainGroupId='0' and CompanyId='".$_SESSION['ACGCompanyId']."'";
		$GetRecord = "Select * from $table where $condition ";
		$ResultData = DB_query($GetRecord,$db);
		while($RowData = DB_fetch_array($ResultData))
		{ 
		$pcode=$RowData['GroupId'];
		$dashVal = "&nbsp;&nbsp;&nbsp;";
		$condition1="MainGroupId='".$RowData['GroupId']."'   and CompanyId='".$_SESSION['ACGCompanyId']."'";
		$data_Liabilities=DB_query( "Select * from $table where $condition1" ,$db);
		while($r_liabilities=DB_fetch_array($data_Liabilities))
		{
 		?>
			<tr>
			  <td colspan="4"><?php echo ucwords($r_liabilities['GroupName']);?>
			  <table width="100%" border="1" cellspacing="0" cellpadding="0">
			  <?php  echo CategoryShow($pcode, $r_liabilities['GroupId'], $dashVal,$branch_name,$sdate); ?>
			  </table>			  </td>
			  </tr>
		<?php }
		} ?>
		 <tr>
		   <td  colspan="4" > <div align="right" style="margin-right:15px">  </div></td>
 		  </tr>
      </table></td>
    </tr>
  </table></td>
</tr>
</table>
 
   