<?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='';}}
$Compamies=DB_query("select * from companies",$db);
  $CompamiesRow=DB_fetch_array($Compamies);
  $sql1 = "SELECT * FROM companies where CompanyId='".$_SESSION['ACGCompanyId']."'";
  $sqlres = DB_query($sql1,$db);
  $companyRow = DB_fetch_array($sqlres);
?>

<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>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
  <title>Group Wise Trial Balance Report</title>

<style type="text/css" media="print">
  @page port {size: portrait;}
@page land {size: landscape;}
 .portrait {page: port;}
.wrapper {page: land;}

</style>

<style type="text/css"> 
@print {size: portrait; margin:10px;}

@media print{
body{ margin:0px; padding:0px;  font-size:12px; line-height: 18px; font-family: Arial, Helvetica, sans-serif; color:#000; font-weight:normal;}

h1,h2,h3,h4,h5,p{ margin:0px; padding:0px;}
p{ text-align:left;}

h1{
  font-size: 20px; line-height: 24px;
  font-weight:normal;
  font-family:Arial, Helvetica, sans-serif !important;
}
h2{
  font-size: 18px; line-height: 22px;
  font-weight:normal;
  font-family:Arial, Helvetica, sans-serif !important;
}
thead{display: table-header-group;}
.wrapper{width:99%; height: auto; float:left; border:#000000 1px solid; border-bottom:#000000 1px solid;}
.div_first, .div_first .full{
  width: 100%; height: auto;
  /*border-bottom: #000000 1px solid;
  border-top: #000000 1px solid;*/
  /*border-left: #000000 1px solid;
  border-right: #000000 1px solid;*/
  float: left; position: relative;
}
 .div_first .left
  {
    width:18%; text-align:left; height:auto; vertical-align:middle; height:auto; padding: 1%; float:left;
  }
  .div_first .middle
  {
    width:58%; text-align:center; padding: 1%; margin:0 auto; float: left;
  }
  .div_first .right
  {
    width:18%; text-align:right; padding: 1%; float: left;
  }

.div_first .full{text-align: center; border: 0;}
.div_second, .div_third, .div_forth{
  width: 100%;
  height:auto;
  border-top: #000000 1px solid;
  /*border-bottom: #000000 1px solid;*/
  /*border-left: #000000 1px solid;
  border-right: #000000 1px solid;*/
  float: left;
}
.div_second table tr th, .div_second table tr td{border-right:#000000 1px solid; border-bottom:#000000 1px solid;}
.div_second table tr th{text-align: left;}
.div_second table tr th:last-child, .div_second table tr td:last-child{border-right:0;}
.div_second table tr:last-child td{border-bottom:0;}
.div_second table tr td > table tr td{border-right:#000000 1px solid; border-bottom:#000000 1px solid;}
.div_second table tr td > table tr td:last-child{border-right:0;}
.div_second table tr td > table tr:last-child td{border-bottom:0;}

.div_third table tr th, .div_third table tr td{ border-bottom:#000 1px solid; border-right:#000 1px solid;}
.div_third table tr th{background-color:transparent; text-align: left;}
.div_third table tr th:last-child, .div_third table tr td:last-child{border-right:none;}
.div_third table tr:last-child td{border-bottom:none;}

.div_forth table tr th, .div_forth table tr td{ border-bottom:#000 1px solid; border-right:#000 1px solid;}
.div_forth table tr th{background-color: #ccc; text-align: left;}
.div_forth table tr th:last-child, .div_forth table tr td:last-child{border-right:none;}
.div_forth table tr:last-child td{border-bottom:none;}

.f12{ font-weight: bold; color:#000; font-size:12px;}
.f11{color:#000; font-size:12px; font-weight: normal; line-height:18px;}
.f14{color:#000; font-size:18px;}
.mt26{margin-top:4px; width:90%;}
.mt27{margin-top:0px; width:70%;}
.mt28{margin-top:0px; width:100%;}
.border_top{border-top:#000 1px solid;}
.border_bottom{border-bottom:#000 1px solid;}
.wdt1{width:8%;}
.wdt2{width:100px;}
.wdt3{width: 66px;}
.wdt4{width: 80px;}
.wdt5{width: 52px;}
.wdt6{width: 60px;}
}

@media screen{
/* CSS Document */
body{ margin:0px; padding:0px; background-color:#fff; font-size:12px; line-height: 18px; font-family: Arial, Helvetica, sans-serif; color:#000; font-weight:normal;}
h1,h2,h3,h4,h5,p{ margin:0px; padding:0px;}
p{ text-align:left;}

#foo{width:1070px; height: auto; margin:0px auto;}
.wrapper{width:1070px; height: auto; margin:10px auto; border-top:#000000 1px solid;}

h1{
  font-size: 24px; line-height: 28px;
  font-weight:bold;
  font-family:Arial, Helvetica, sans-serif !important;
}
h2{
  font-size: 18px; line-height: 22px;
  font-weight:normal;
  font-family:Arial, Helvetica, sans-serif !important;  
}
.div_first, .div_first .full{
  width: 100%; height: auto;
  /*border-bottom: #000000 1px solid;
  border-top: #000000 1px solid;*/
  border-left: #000000 1px solid;
  border-right: #000000 1px solid;
  float: left; position: relative; display: table;
}
.div_first .full{text-align: center; border: 0; margin:10px 0 0;}
.div_first .left, .div_first .middle, .div_first .right{float: left; display: table-cell; vertical-align: middle;}
.div_first .left
{
  width:18%; text-align:center; height:auto; vertical-align:middle; padding: 1%; 
}
.div_first .middle
{
  width:58%; text-align:center; padding: 1%; position: relative; margin:0 auto;
}
.div_first .right
{
  width:18%; text-align:center; padding:1%; 
}
.div_second, .div_third, .div_forth{
  width: 100%; height: auto;
  /*border-bottom: #000000 1px solid;*/
  border-top: #000000 1px solid;
  border-left: #000000 1px solid;
  border-right: #000000 1px solid;
  float: left;
}
.div_second table tr th, .div_second table tr td{border-right:#000000 1px solid; border-bottom:#000000 1px solid;}
.div_second table tr th{background-color: #ccc; text-align: left;}
.div_second table tr th:last-child, .div_second table tr td:last-child{border-right:0;}
/*.div_second table tr:last-child td{border-bottom:0;}*/
.div_second table tr td > table tr td{border-right:#000000 1px solid; border-bottom:#000000 1px solid;}
.div_second table tr td > table tr td:last-child{border-right:0;}
.div_second table tr td > table tr:last-child td{border-bottom:0;}

.div_third table tr th, .div_third table tr td{ border-bottom:#000 1px solid; border-right:#000 1px solid;}
.div_third table tr th{background-color: #ccc; text-align: left;}
.div_third table tr th:last-child, .div_third table tr td:last-child{border-right:none;}
.div_third table tr:last-child td{border-bottom:none;}

.div_forth{border-bottom:#000000 1px solid;}

.f12{font-weight: bold; color:#000; font-size:12px;}
.f11{ color:#000; font-size:12px; font-weight: normal; line-height:18px;}
.f14{ color:#000; font-size:18px;}
.mt26{margin-top:4px; width:90%;}
.mt27{margin-top:0px; width:100%;}
.mt28{margin-top:0px; width:90%;}
.border_top{border-top:#000 1px solid;}
.border_bottom{border-bottom:#000 1px solid;}
.wdt1{width:10%;}
.wdt2{width:180px;}
.wdt3{width: 66px;}
.wdt4{width: 120px;}
.wdt5{width: 52px;}
.wdt6{width: 70px;}
}
</style>
 </head>
 <body>
<div id="foo">
  <p style="text-align:right;"><input type="button" onclick="javascript:toggle_visibility('foo');" value="Print" /></p>
</div>

<div class="wrapper">

<div class="div_first">
<?php $logoImage=($CompamiesRow['CompanyLogo']=='')?'electra.jpg':$CompamiesRow['CompanyLogo']; ?>
      <div class="left"><img src="Setup/CompanyLogo/<?php echo $logoImage;  ?>" class="mt26" /></div>
      <div class="middle"><?php 
    if($CompamiesRow['CompanyName']!="") {echo "<h1>".$CompamiesRow['CompanyName']."</h1>"; } else { echo "";}
    
    if($InoRow['deladd1']!="") { echo $InoRow['deladd1']; } else { echo "";}
    if($InoRow['deladd2']!="") { echo ", ". $InoRow['deladd2']; } else { echo "";}
    if($InoRow['deladd3']!="") { echo ", ". $InoRow['deladd3']; } else { echo "";}
    if($InoRow['city']!="") { echo ", ". $s->getFieldValue("city_master", "id='".$InoRow['city']."'", "city"); } else { echo "";}
    if($InoRow['pincode']!="") { echo " - ".$InoRow['pincode']; } else { echo "";}
    if($InoRow['state']!="") { echo ", ". $s->getFieldValue("state_master", "id='".$InoRow['state']."'", "state"); } else { echo "";}
    if($InoRow['country']!="") { echo ", ". $s->getFieldValue("country_master", "id='".$InoRow['country']."'", "country");
    
     } else { echo "";}
    ?>
     <div class="full"><span class="f11"><?php echo "Phone No.:".$CompamiesRow['Phone'].",   Fax No.: ".$CompamiesRow['fax'].", Email ID.: ".$CompamiesRow['email'].",   Website: ".$CompamiesRow['website']; ?><br>
  <?php  echo $_SESSION['mess']; ?>
  </span></div>
      <h2>Group Wise Trial Balance - <?php echo $period; ?></h2>
      </div>
      <div class="right"></div>
</div>

<div class="div_second">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<?php 
$Header="<tr class='f12'>";
$Header.="<th>Account Group</th>";
$Header.="<th width='16%' style='text-align:center;'>Opening DR</th>";
$Header.="<th width='16%' style='text-align:center;'>Opening CR</th>";
$Header.="<th width='16%' style='text-align:center;'>Total Debit</th>";
$Header.="<th width='16%' style='text-align:center;'>Total Credit</th>";
$Header.="<th width='16%' style='text-align:center;'>Closing Balance</th>";
$Header.="</tr>";
echo $Header;
?>
  <?php $i=0;
  $account_groups="Select * from accountgroupmaster where MainGroupId=0 and Type='G' order by MainGroupId asc, GroupName asc";
  $account_group_res=DB_query($account_groups,$db);
  
  while($account_group_row=DB_fetch_array($account_group_res))
  {
	$link="index.php?route=Account/AccountingReport&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."&tabid=".$_REQUEST['tabid']."&page=".$_REQUEST['page']."&gid=".$account_group_row['GroupId']."";
	//echo "<br>";echo "Select * from accountledger where UnderGroup='".$account_group_row['GroupId']."'";
	//echo $_SESSION['FDATE']; echo "&nbsp;".$_SESSION['TDATE'];
	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{
  		$prevdate=date('Y-m-d', strtotime('-1 day', strtotime($_SESSION['FDATE'])));
		$groupDetails=getGroupBalanceSheet($account_group_row['GroupId'],$_SESSION['FDATE'],$_SESSION['TDATE']);
	}
	else
	{$groupDetails=getGroupBalanceSheet($account_group_row['GroupId'],'','');}
	
	//$groupDetails=getGroupBalanceSheet($account_group_row['GroupId'],$from_date='',$to_date='');
	//print_r($groupDetails);
	$grand_DR=$groupDetails[0];
	$grand_CR=$groupDetails[1];
	$opening_DR=$groupDetails[2];
	$opening_CR=$groupDetails[3];
	if($opening_DR>$opening_CR){$OP=$opening_DR-$opening_CR; $OPTYPE="DR"; $OPDR1=$opening_DR-$opening_CR;$OPCR1='';}elseif($opening_CR>$opening_DR){$OP=$opening_CR-$opening_DR; $OPTYPE="CR"; $OPCR1=$opening_CR-$opening_DR;$OPDR1='';}else{$OP=''; $OPTYPE='';$OPDR1='';$OPCR1='';}
	
	if($grand_DR!='' || $grand_CR!='' || $opening_DR!='' || $opening_CR!=''){
	  $i++;
  ?>
  <tr class="f11">
    <td><?php 
	//echo "<pre>";print_r($groupDetails); echo "<br />";
	echo $account_group_row['GroupName'];?></td>
    <td style="text-align:right;"><?php echo $OPDR1; $opdrarray[]=$OPDR1;?></td>
	<td style="text-align:right;"><?php echo $OPCR1; $opcrarray[]=$OPCR1;?></td>
	<td style="text-align:right;"><?php if($grand_DR=='0'){echo "";}else{echo $grand_DR;}$DR_ARR[]=$grand_DR;?></td>
    <td style="text-align:right;"><?php if($grand_CR=='0'){echo " ";}else{echo $grand_CR;}$CR_ARR[]=$grand_CR;?></td>
    <?php 
	if($OP!='')
	{
		if($OPTYPE=='DR'){$GDR=$OP+$grand_DR; $GCR=$grand_CR;}
		elseif($OPTYPE=='CR'){$GCR=$OP+$grand_CR; $GDR=$grand_DR;}
	}
	else{$GDR=$grand_DR; $GCR=$grand_CR;}
	
	if($GDR>$GCR){$closing=$GDR-$GCR; $type="DR"; $closingd=$GDR-$GCR;$closingc='';  $closingdarr[]=$closingd;}
	elseif($GCR>$GDR){$closing=$GCR-$GDR; $type="CR"; $closingc=$GCR-$GDR;$closingd='';$closingcarr[]=$closingc;}
	else{$closing='';$type='';}
	?>
	<td style="text-align:right;"><?php 
	
	if($closing=='0'){echo "";}else{echo $closing."&nbsp;".$type;}?></td>
  </tr>
  <?php 
if($i%16==0)
{?>
</table>
</div>
<div class="div_third">
<?php echo $logo; ?>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<?php echo $Header; ?>
<?php }
  }
} ?>
 <tr class="f12">
    <td style="text-align:right;">Total</td>
    <td width='16%' style="text-align:right;"><?php echo array_sum($opdrarray);?></td>
	<td width='16%' style="text-align:right;"><?php echo array_sum($opcrarray);?></td>
	<td width='16%' style="text-align:right;"><?php if($DR_ARR!='')echo array_sum($DR_ARR);?> </td>
    <td width='16%' style="text-align:right;"><?php if($CR_ARR!='')echo array_sum($CR_ARR);?>  </td>
    <td width='16%' style="text-align:right;"><?php if($closingdarr!=''){$arrsum1=array_sum($closingdarr);}else{$arrsum1='';}
						if($closingcarr!=''){$arrsum2=array_sum($closingcarr);}else{$arrsum2='';}
						if($arrsum1>$arrsum2){echo $arrsum1-$arrsum2. "&nbsp;DR";}
						elseif($arrsum2>$arrsum1){echo $arrsum2-$arrsum1. "&nbsp;CR";}
						?></td>
  </tr>
</table>
</div>
</div>

</body>
</html>