<?php  error_reporting(0); include('Trial_function.php');?>
 <center><h1><?php //echo getFieldCRM('xpert_organizationdetails','id',$_SESSION['ACGCompanyId'],'organizationname'); ?></h1>  </center>
<script language="javascript">
var formblock;
var forminputs;
 
function prepare() {
  formblock= document.getElementById('form_id');
  forminputs = formblock.getElementsByTagName('input');
}
 
function select_all(name, value) {
   for (i = 0; i < forminputs.length; i++) {
    // regex here to check name attribute
    var regex = new RegExp(name, "i");
    if (regex.test(forminputs[i].getAttribute('name'))) {
      if (value == '1') {
        forminputs[i].checked = true;
      } else {
        forminputs[i].checked = false;
  }
    }
  }
}
 
if (window.addEventListener) {
  window.addEventListener("load", prepare, false);
} else if (window.attachEvent) {
  window.attachEvent("onload", prepare)
} else if (document.getElementById) {
  window.onload = prepare;
}
 </script>
 <?php 
if($_REQUEST['Go'])
{
 $branch_name=implode(',',$_REQUEST['branch_name']);
$todate=$_REQUEST['todate'];
$fromdate=$_REQUEST['fromdate'];
}
?>
<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>
 <form action="" method="post"   id="form_id" name="myform">
<table width="50%" border="0" align="center" cellpadding="0" cellspacing="0" style="border:1px solid #c5c5c5; padding:10px; background:#f6f6f6;">
  <tr>
    <td>Branch</td>
	<td>	
	  <?php   
 	$SQL_branch=DB_query("SELECT loccode,prefix,locationname   from locations Where companyid='".$_SESSION['ACGCompanyId']."'",$db);
	?>
	<div style="overflow:auto; height:auto; width:100%;" >
	 <?php
	while($row_branch=DB_fetch_array($SQL_branch))
	{
	     $px='';
	      if (in_array($row_branch['prefix'], $branch_name))
		  $px="checked";
 	 ?>
	<input name="branch_name[]" id="branch_name" type="checkbox" value="<?php echo $row_branch['prefix'] ?>"  <?php echo $px; ?>    /><?php echo $row_branch['locationname'] ?><br />
	<?php }?>
 	</div>
	<a href="#" onClick="select_all('branch_name', '1');">Check All </a> | <a href="#" onClick="select_all('branch_name', '0');">Uncheck All 
</a>    
	  </td>
    <td>
	<table width="100%" cellpadding="0" cellspacing="0" border="0" align="center">
		
	<?php 
	if($_REQUEST['balancesheet_date']!='')
	$balancesheetdate=$_REQUEST['balancesheet_date'];
	else
	$balancesheetdate=date('Y-m-d');
	
	?>
	<tr>
		<td>To</td>
<td><input name="todate" type="text" class="datepicker" id="todate" style="width:100%"   value="<?php  echo $todate?>" size="15" /></td>
</tr>
<tr>
<td>From:</td>
<td><input name="fromdate" type="text" class="datepicker" id="fromdate" style="width:100%"   value="<?php  echo $fromdate?>" size="15" /></td>
</tr>
<tr>
	<td colspan="2" align="right" style="text-align:right;"><input type="submit" name="Go" value="View" class="add" /></td>
</tr>
</table>
</tr>
      </p>
	 
     </td>
  </tr>   
</table>
</form>
 <?php
  function CategoryShow($pcode, $PcateID, $dashVal,$branch_name)
		{
			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 height="25"><td width="50%" style="border-bottom:1px solid #ccc;">'.$dashVal.$rowCate['GroupName']."</td><td width='25%'style='border-bottom:1px solid #ccc;'>";
					 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("BalanceSheetLedger.php?UnderGroup=<?php echo $rowCate['GroupId'];?>&CBranch=<?php echo $branch_name; ?>",1000,500)'/>
					 <?php }
					 $p=TrialBalance_Amount($rx,$branch_name);
					 if($p[0]>$p[1]){
					 
					 $typeCRDR='CR';
					 $diff=$p[0]-$p[1];
					 }
					 else{
 					 $typeCRDR='DR';
					 $diff=$p[1]-$p[0];
					 }
					 
					 echo "</td><td width='25%'style='border-bottom:1px solid #ccc;'>";
					 if($diff>0){
					 echo $diff."(".$typeCRDR.")";
					 $_SESSION['TotalBalanceCRAmount']+=$diff; 
					 $_SESSION['TotalBalanceAmount']+=$diff;
					 }
					 echo "</td></tr>";
   				     CategoryShow( $pcode,$rowCate['GroupId'], $dashVal,$branch_name);
				}
			}
		}  
 ?>
 
<?php 
if($_REQUEST['Go'])
{
$branch_name=implode(',',$_REQUEST['branch_name']);
$todate=$_REQUEST['todate'];
$fromdate=$_REQUEST['fromdate'];
//echo $balancesheet_date=FormatDateForSQL($_REQUEST['balancesheet_date']);
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr><td class="headingbold" align="center" style="text-align:center;">Balance Sheet - As On (<?php echo $todate;?> to <?php echo $fromdate?> )</td></tr>
<tr>
  <td height="25"> <div align="center"> Branch : 
<?php    echo $branch_name;?>
</div></td>
</tr>
<tr>
  <td>
  <table width="100%" border="0"cellspacing="0" cellpadding="0" class="lvt">
    <tr>
      <td width="50%" class="boldtitlehead"><div align="center"><strong>Liabilities</strong></div></td>
      <td width="50%" class="boldtitlehead"><div align="center"><strong>Assets</strong></div></td>
    </tr>
    <tr>
      <td valign="top">
	  <table width="99%" border="0" cellspacing="1" cellpadding="0">
        <tr>
          <td width="50%" height="25" class="lvtCol"><div align="center"><strong>Description</strong></div></td>
          <td width="25%" class="lvtCol"><div align="center"><strong>Sch</strong>.</div></td>
          <td width="25%" class="lvtCol"><div align="center"><strong>Amount(INR)</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 bgcolor="#ffffff">
         <td colspan="3" style="padding-left:10px;">
		      <?php echo ucwords($r_liabilities['GroupName']);?>
		  	  <table width="100%" border="0" cellspacing="0" cellpadding="0">
			  <?php  echo CategoryShow($pcode, $r_liabilities['GroupId'], $dashVal,$branch_name); ?>
 			  </table>			  </td>
         </tr>
 		
	<?php 
		}
	 
	} ?>	
		  <tr>
 		  <td>&nbsp;</td>
 		  <td>&nbsp;</td>
		   <td><?php echo $_SESSION['TotalBalanceAmount'];?></td>
 		  </tr>
      </table></td>
      <td valign="top">
	  <table width="100%" border="0" cellspacing="1" cellpadding="0">
        <tr>
          <td width="50%" height="25" class="lvtCol"><div align="center"><strong>Description</strong></div></td>
          <td width="25%" class="lvtCol"><div align="center"><strong>Sch.</strong></div></td>
          <td width="25%" class="lvtCol"><div align="center"><strong>Amount(INR)</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 bgcolor="#ffffff" height="25">
			  <td colspan="3" style="padding-left:10px;"><?php echo ucwords($r_liabilities['GroupName']);?>
			  <table width="100%" border="0" cellspacing="0" cellpadding="0">
			  <?php  echo CategoryShow($pcode, $r_liabilities['GroupId'], $dashVal,$branch_name); ?>
			  </table>			  </td>
			  </tr>
		<?php }
		
		} ?>
		 <tr>
 		  <td>&nbsp;</td>
 		  <td>&nbsp;</td>
		   <td><?php echo $_SESSION['TotalBalanceCRAmount'];?></td>
 		  </tr>
      </table></td>
    </tr>
  </table></td>
</tr>
</table>
<?php } ?>
 
   