<?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');
$gid=$_SESSION['gid'];
$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']);
}
?>
<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>Profit & Loss A/c Libilities 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>".'Profit & Loss A/c'.'-'.$s->getField('accountgroupmaster','GroupId',$gid,'GroupName').'-'.$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" cellpadding="3" cellspacing="0">
<tr>
<th width="70%">Particulars</th>
<th width="10%">Opening</th>
<th width="10%">Debit</th>
<th width="10%">Credit</th>
</tr>
  <?php 
  $getSubGroups=DB_query("Select * from accountgroupmaster where MainGroupId=$gid",$db);
  $openingCR_array=''; $openingDR_array='';
  while($subGroups=DB_fetch_array($getSubGroups))
  {
  		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!=''){
  		$res=getGroupBalanceSheet($subGroups['GroupId'],$_SESSION['FDATE'],$_SESSION['TDATE']);}
		else
		{$res=getGroupBalanceSheet($subGroups['GroupId'],'','');}
		//print_r($res);
		$TD=$res[0];
		$TC=$res[1];
  ?>
  <tr style=" background-color:#999999;">
  <td style="padding-left:5px"><strong><?php echo $subGroups['GroupName'];?></strong></td>
	 <td style="text-align:right; padding-right:5px"><strong><?php 
	 $DROPEN=$res[2];
	 $CROPEN=$res[3];
	 if($DROPEN>$CROPEN)
	 {echo $TOTALOPDR=$DROPEN-$CROPEN; echo "&nbsp;DR"; $openingDR_array[]=$TOTALOPDR;}
	 elseif($CROPEN>$DROPEN)
	 {echo $TOTALOPCR=$CROPEN-$DROPEN; echo "&nbsp;CR"; $openingCR_array[]=$TOTALOPCR;}
	 
	 
  	
  	
	 
	 
	 ?></strong></td>
  <td style="text-align:right; padding-right:5px"><strong><?php if($TD==0){echo "";}else echo $TD; $TDA[]=$TD;?></strong></td>
  <td style="text-align:right; padding-right:5px"><strong><?php if($TC==0){echo "";}else echo $TC; $TCA[]=$TC;?></strong></td>
  </tr><?php } ?>
  <tr height="20px"></tr>
  <?php //echo $_SESSION['FDATE']; echo "------------";echo $_SESSION['TDATE'];
  $getLedgers=DB_query("Select * from accountledger where UnderGroup=$gid order by AccountName ASC",$db);
  while($Ledgers=DB_fetch_array($getLedgers))
  {
  	$ledger_id=$Ledgers['AccountsID'];
	
	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{$opening_bal=$s->GetFieldValue('accountledger',"AccountsID=$ledger_id and openingdate<='".$_SESSION['TDATE']."'",'openingBalance');}
	else{$opening_bal=$s->getField('accountledger','AccountsID',$ledger_id,'openingBalance');}
	$opening_type=$s->getField('accountledger','AccountsID',$ledger_id,'openingtype');

  	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{$totVoucherDR=Get_AccountByLedger($Ledgers['AccountsID'],'DR',$_SESSION['FDATE'],$_SESSION['TDATE']);
  	$totVoucherCR=Get_AccountByLedger($Ledgers['AccountsID'],'CR',$_SESSION['FDATE'],$_SESSION['TDATE']);}
	else{
	$totVoucherDR=Get_AccountByLedger($Ledgers['AccountsID'],'DR','','');
  	$totVoucherCR=Get_AccountByLedger($Ledgers['AccountsID'],'CR','','');}
	
	$master_party_id=$s->getField('accountledger','AccountsID',$ledger_id,'vender_client');	
	$master_tax_id=$s->getField('accountledger','AccountsID',$ledger_id,'taxid');	
	$ledgertype=$s->getField('accountledger','AccountsID',$ledger_id,'ledgertype');	
	if($ledgertype!=''){
	if($ledgertype=='excisetax'){
	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{$etaxDetails=yearlyETaxAccountByTransactions($master_tax_id,$_SESSION['FDATE'],$_SESSION['TDATE']);}else
		{$etaxDetails=yearlyETaxAccountByTransactions($master_tax_id,'','');} $etaxDR=$etaxDetails[0]; $etaxCR=$etaxDetails[1];
		}else{$etaxDetails='';}
	if($ledgertype=='salestax')
	{
	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{$staxDetails=yearlySTaxAccountByTransactions($master_tax_id,$_SESSION['FDATE'],$_SESSION['TDATE']);}else
		{$staxDetails=yearlySTaxAccountByTransactions($master_tax_id,'','');} $staxDR=$staxDetails[0]; $staxCR=$staxDetails[1];
	}else{$staxDetails='';}
	if($ledgertype=='vendor')
	{
		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
		{$vendorDetails=yearlyVendorAccountByTransactions($master_party_id,$_SESSION['FDATE'],$_SESSION['TDATE']);}else
		{$vendorDetails=yearlyVendorAccountByTransactions($master_party_id,'','');} $purchCR=$vendorDetails;
	}else{$vendorDetails='';}
	if($ledgertype=='client')
	{
		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
		{$clientDetails=yearlyClientAccountByTransactions($master_party_id,$_SESSION['FDATE'],$_SESSION['TDATE']);}
		else{$clientDetails=yearlyClientAccountByTransactions($master_party_id,'','');} $invDR=$clientDetails;
	}else{$clientDetails='';}
	if($ledgertype=='freight')
	{
		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
		{$freightDetails=yearlyFreightAccountByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE']);}
		else{$freightDetails=yearlyFreightAccountByTransactions($ledger_id,'','');} $freightDR=$freightDetails;
	}else{$freightDetails='';$freightDR='';}
	
	if($ledgertype=='surcharge')
	{
		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
		{$surDetails=yearlySurchargeAccountByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE']);}
		else{$surDetails=yearlySurchargeAccountByTransactions($ledger_id,'','');} $surDR=$surDetails;
	}else{$surDetails='';$surDR='';}
	
	if($ledgertype=='pckcharge')
	{
		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
		{$packDetails=yearlyPkgChargesAccountByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE']);}
		else{$packDetails=yearlyPkgChargesAccountByTransactions($ledger_id,'','');} $packDR=$packDetails;
	}else{$packDetails='';$packDR='';}
	
	if($ledgertype=='addcharge')
	{
		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
		{$addDetails=yearlyAddChargesAccountByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE']);}
		else{$addDetails=yearlyAddChargesAccountByTransactions($ledger_id,'','');} $addDR=$addDetails;
	}else{$addDetails='';$addDR='';}
	
	
	
	if($ledgertype=='round')
	{
		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
		{$roundDetails=yearlyRoundOffByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE']);}
		else{$roundDetails=yearlyRoundOffByTransactions($ledger_id,'','');} 
		$roundDR=$roundDetails[0];
		$roundCR=$roundDetails[1];
	}else{$roundDetails='';$roundDR='';$roundCR='';}
	
	
	}
	else
	{
		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
		{$ledgerPosting=yearlyLedgerPostingByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE']);}else{
		$ledgerPosting=yearlyLedgerPostingByTransactions($ledger_id,'','');} $legderDR=$ledgerPosting[0]; $legderCR=$ledgerPosting[1];
		$etaxDetails='';
		$staxDetails='';
		$vendorDetails='';
		$clientDetails='';
		$freightDetails='';
		$packDetails='';
		$addDetails='';
		$roundDetails='';
		$etaxDR=''; $etaxCR=''; $staxDR='';$staxCR=''; $purchCR=''; $invDR='';$freightDR='';$surDR='';$packDR='';$addDR='';
		$roundDR='';$roundCR='';
	}
	
  ?>
  <tr>
  <td style="padding-left:5px"><strong><font color="#000000"><?php echo $Ledgers['AccountName'];?></font></strong></td>
  
  <td style="text-align:right; padding-right:5px"><strong><?php if($opening_bal!=''){echo $opening_bal."&nbsp;".$opening_type; }
  if($opening_type=='CR'){
  $openingCR_array[]=$opening_bal;}
  else{$openingDR_array[]=$opening_bal;}?></strong></td>
  
  <td style="text-align:right; padding-right:5px"><strong><?php $total_DR=$totVoucherDR+$etaxDR+$staxDR+$invDR+$legderDR+$freightDR+$surDR+$packDR+$addDR+$roundDR; if($total_DR==0)echo " "; else echo $total_DR; $ARRDR[]=$total_DR;?></strong></td>
  <td style="text-align:right; padding-right:5px"><strong><?php $total_CR=$totVoucherCR+$etaxCR+$staxCR+$purchCR+$legderCR+$roundCR; if($total_CR==0)echo " "; else echo $total_CR; $ARRCR[]=$total_CR;?></strong></td>
  </tr><?php } ?>
 <tr>
    <th width="60%">Total</th>
	
	 <th width="20%"><?php 
	 
	 
	 if($openingDR_array!='')$opDR=array_sum($openingDR_array);
	 if($openingCR_array!='')$opCR=array_sum($openingCR_array);
	 if($opDR>$opCR){echo $opDR-$opCR; echo "&nbsp;DR";}else{echo $opCR-$opDR; echo "&nbsp;CR";}
	 
	 ?></th>
	
    <th width="20%"><?php 
	if($TDA!=''){$xx=array_sum($TDA);}else{$xx=0;}
	if($ARRDR!=''){$yy=array_sum($ARRDR);}else{$yy=0;}
	echo $xx+$yy;
	?></th>
	<th width="20%"><?php 
	if($TCA!=''){$mm=array_sum($TCA);}else{$mm=0;}
	if($ARRCR!=''){$nn=array_sum($ARRCR);}else{$nn=0;}
	echo $mm+$nn;?></th>
  </tr>
</table>