<?php
session_name('CAKEPHP'); session_start();
ob_start();
$PageSecurity = 1;
include('config.php');
include('includes/ConnectDB.inc');
include('includes/function_lib.php');
include('includes/LanguageSetup.php');
include('includes/DateFunctions.inc');
include('Account/fun-Trial.php');
$lid=$_SESSION['lid'];
$from_date='';
$to_date='';
//include('includes/function_lib.php');
if(isset($_SESSION['FDATE']))
{
$from_date=$_SESSION['FDATE'];
}
if(isset($_SESSION['TDATE']))
{
$to_date=$_SESSION['TDATE'];
}

if($from_date!='' && $to_date!='')
{
$period=ConvertSQLDate($from_date).' to '.ConvertSQLDate($to_date);
}
else
{
$period=ConvertSQLDate($_SESSION['financialYrStart']).' to '.ConvertSQLDate($_SESSION['financialYrEnd']);
}
$month_array=array('04'=>'April','05'=>'May','06'=>'June','07'=>'July','08'=>'August','09'=>'September','10'=>'October','11'=>'November','12'=>'December','01'=>'January','02'=>'February','03'=>'March');
	//echo $_SESSION['financialYrStart'];
	//echo $_SESSION['financialYrEnd'];
	
	if($from_date!='' && $to_date!='')
	{
		$opening_bal=$s->GetFieldValue('accountledger',"AccountsID=$lid and openingdate<='".$to_date."'",'openingBalance');
	}
	else{$opening_bal=$s->getField('accountledger','AccountsID',$lid,'openingBalance');}
	
	$opening_type=$s->getField('accountledger','AccountsID',$lid,'openingtype');
	if($opening_type!=''){if($opening_type=='CR'){$openingCR=$opening_bal; $openingDR='';}else{$openingDR=$opening_bal; $openingCR='';}}
?>
<script type="text/javascript" language="javascript">
  function toggle_visibility(id) {
       var e = document.getElementById(id);
	      e.style.display = 'none';
		       //alert(e.style.display);
			   window.print();
    }
  
</script>
<title>Balance Sheet Liabilities Month Wise Report</title>
<link rel="stylesheet" href="css/purchase_requision_print.css">
<div class="body_contain">
<div class="tab_div">
<?php 
$logo="<div>";
$logo.="<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
$logo.="<tr>";
$logo.="<td align='left'><img src='images/cloud_logo.jpg' /></td>";
$logo.="<td align='right'><img src='images/right_img.jpg' /></td>";
$logo.="</tr>";
$logo.="<tr>";
$logo.="<td colspan='2'><h1>".'Balance Sheet'.'-'.$s->getField('accountledger','AccountsID',$lid,'AccountName').'-'.$period."</h1></td>";
$logo.="</tr>";
$logo.="</table>";
$logo.="</div>";
echo $logo;
?>
<div id="foo">
  <p align="center"><input type="button" onclick="javascript:toggle_visibility('foo');" value="Print" /></p>
</div>
<table width="100%" border="1" cellspacing="3" cellpadding="3" >
<tr>
<th width="40%">Months</th>
<th width="20%">Debit</th>
<th width="20%">Credit</th>
<th width="20%">Closing</th>
  </tr>
<?php 
	foreach($month_array as $key=>$val)
	{
		$a=explode('-',$_SESSION['financialYrStart']);
		$b=explode('-',$_SESSION['financialYrEnd']);
		if($key>='04' && $key<='12'){$yr=$a[0];}elseif($key>='01' && $key<'03'){$yr=$b[0];}
		
		$month=$key;
		if($key<4){
		$month_num=$key+12;}else
		{
			if(substr($key,0,1)=='0')
			{$month_num=substr($key,-1);}
			else{$month_num=$key;}
		}
		
		
		
		$color='#FFFFFF';
		$flag='';
		if($from_date!='' && $to_date!='')
		{
			$FDATEBRK=explode('/',$from_date);
			$TDATEBRK=explode('/',$to_date);
			$fmnth=$FDATEBRK[1];
			$tmnth=$TDATEBRK[1];
			
			$from_month_brk=substr($fmnth,0,1);
			$to_month_brk=substr($tmnth,0,1);		
			if($from_month_brk=='0'){$from_month=substr($fmnth,-1);}else{$from_month=$fmnth;}
			if($to_month_brk=='0'){$to_month=substr($tmnth,-1);}else{$to_month=$tmnth;}
			if($to_month<4){$to_month_num=$to_month+12;}else{$to_month_num=$to_month;}
			if($from_month<4){$from_month_num=$from_month+12;}else{$from_month_num=$from_month;}
			
			$flag=0;
			if($fmnth==$tmnth)
			{
				if($key==$fmnth){$flag=1;$color='#CC99FF';}
			}
			elseif($fmnth!=$tmnth)
			{
				if($fmnth==$key){$flag=2;$color='#CC99FF';}else
				if($tmnth==$key){$flag=3;$color='#CC99FF';}else
				if($month_num<$to_month_num && $month_num>$from_month_num){$flag=4;$color='#CC99FF';}
			}
		$amounts=getMonthsBalanceSheet($lid,$key,$yr,$flag);
		}
		else{$amounts=getMonthsBalanceSheet($lid,$key,$yr,'');}
		$totalDR=$amounts[0];
		$totalCR=$amounts[1];
	?>
		<tr bgcolor="<?php echo $color;?>">
		<td style="padding-left:5px"><?php echo $val; //echo "---".$month_num;//."---".$yr,"----".$flag;?></font></strong></td>
		<td style="text-align:right; padding-right:5px;"><strong><?php if($totalDR==0){echo "";}else{echo $totalDR;} $arrayDR[]=$totalDR;?></strong></td>
		<td style="text-align:right; padding-right:5px;"><strong><?php if($totalCR==0){echo "";}else{echo $totalCR;} $arrayCR[]=$totalCR;?></strong></td>
		<td style="text-align:right; padding-right:5px;"><strong><?php 
		if($totalDR==0 && $totalCR==0){$closing=''; $type='';}else
		if($totalDR>$totalCR){$closing=$totalDR-$totalCR; $type="DR";}
		else
		if($totalCR>$totalDR){$closing=$totalCR-$totalDR; $type="CR";}
		echo $closing." ".$type;
		?></strong></td>
		</tr>
	<?php 	
	}
?>
<tr>
<th width="40%">Opening</th>
<th width="20%"><?php echo $openingDR;?></th>
<th width="20%"><?php echo $openingCR;?></th>
<th width="20%">&nbsp;</th>
</tr>
<tr>
<th width="40%">Total</th>
<th width="20%"><?php echo $DR=array_sum($arrayDR);?></th>
<th width="20%"><?php echo $CR=array_sum($arrayCR);?></th>
<th width="20%"><?php //if($DR>$CR){echo $DR-$CR; echo " DR";}elseif($CR>$DR){echo $CR-$DR; echo " CR";}?></th>
</tr>
<tr>
<th width="40%">Total Closing</th>
<?php 

if($opening_type!=''){
if($opening_type=='DR'){$DRR=$DR+$opening_bal; $CRR=$CR;}elseif($opening_type=='CR'){$CRR=$CR+$opening_bal; $DRR=$DR;}}
else
{$DRR=$DR; $CRR=$CR;}
if($DRR>$CRR){$closeDR=$DRR-$CRR;}elseif($CRR>$DRR){$closeCR=$CRR-$DRR;}
?>
<th width="20%"><?php echo $closeDR;?></th>
<th width="20%"><?php echo $closeCR;?></th>
<th width="20%"></th>
</tr>
</table>
</div>
</div>