<?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']);
}
$groupType=DB_fetch_array(DB_query("Select Type from accountgroupmaster where GroupId=".$gid."",$db));
$Gtype=$groupType['Type'];

   $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>
<style type="text/css"> 
/* CSS Document */
body{ margin:0px; padding:0px; font-size:10px; font-family: Arial, Helvetica, sans-serif;  font-weight:normal;}
h1,h2,h3,h4,h5,p{ margin:0px; padding:0px;}
p{ text-align:left;}
.wrapper{width: 1070px; background-color:#FFF; border:1px solid #000; margin: auto;}
.clr{clear:both;}
.div_first{width:1070px; border-bottom:#000000 1px solid; padding:5px 0px 5px 0px;}
.div_first .left{width:149px; float:left; text-align:center; 
border-right:#000 1px solid; height:105px; display:table-cell; vertical-align:middle;}
.div_first .right{width:910px; float:left; text-align:center;}
h2{font-size: 14px; font-weight:normal; font-family:"Times New Roman",Times,serif; margin:4px;
padding:0px; color:#000; text-transform: uppercase;}
h3{color: #000; font-family: "Times New Roman",Times,serif; font-size: 18px; 
 text-align: center; text-decoration: none; text-transform: uppercase; margin:0px; padding:0px; }
.div_second{width:1070px; border-bottom:#000000 1px solid;}

.brdr_btm{ border-bottom:#000 1px solid;}
.brdr_rgt{ border-right:#000 1px solid;}

.div_third{width:1070px; margin:auto;}
.div_third table tr td{font-size:11px; color:#000; font-family:Arial, Helvetica, sans-serif;  }

.div_third table tr td:empty:before{ content:" ";}




hr{ border-color: black;
 
    border-style: solid;
    border-width: 1px 0 0;
    height: 1px;}

.div_forth{width:1070px; margin:auto; border:#000 1px solid; border-top:none;}
.div_fifth{
  width:1070px;
  margin:auto;
  border-top:none;
}


table tr td:empty:before{ content:" ";}

.div_sixth {}
.f12{ font-family:Arial, Helvetica, sans-serif; font-weight: bold; color:#000; font-size:13px; line-height:16px;}
.f11{ font-family:Arial, Helvetica, sans-serif; color:#000; font-size:12px; line-height:16px;}
.f14{ font-family:Arial, Helvetica, sans-serif; color:#000; font-size:16px; line-height:24px;}
.border_top{border-top:#000 1px solid;}
.border_bottom{border-bottom:#000 1px solid;}
.upper{text-transform:uppercase;}
@media print {
     .wrapper{
        border:1px solid #000 !important;
    }
.div_first .right{width:910px;}
.f12{ font-family:Arial, Helvetica, sans-serif; font-weight: bold; color:#000; font-size:16px; line-height:16px;}
.f11{ font-family:Arial, Helvetica, sans-serif; color:#000; font-size:15px; line-height:16px;}
.f14{ font-family:Arial, Helvetica, sans-serif; color:#000; font-size:20px; line-height:24px;}
 h2{font-size: 18px; font-weight:normal; font-family:"Times New Roman",Times,serif; margin:0px;
padding:0px; color:#000; text-transform: uppercase;}
h3{color: #003399; font-family: "Times New Roman",Times,serif; font-size: 24px; margin:0px; padding:0px;  text-align: center; text-decoration: none; text-transform: uppercase;} 
}
.f121 {font-family:Arial, Helvetica, sans-serif; font-weight: bold; color:#000; font-size:13px; line-height:16px;}
.f121 {font-family:Arial, Helvetica, sans-serif; font-weight: bold; color:#000; font-size:16px; line-height:16px;}
</style>
<title>Trial Balance Sheet Liabilities Report</title>
<link rel="stylesheet" href="css/purchase_requision_print.css">
<div class="wrapper">
  
<?php 
$logo="<div>";
$logo.="<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
$logo.="<tr>";
$logo.="<td align='left'></td>";
$logo.="<td align='right'></td>";
$logo.="</tr>";
$logo.="<tr>";
$logo.="<td colspan='2'><h2><b>".'Group Wise Trial Balance'.'-'.$s->getField('accountgroupmaster','GroupId',$gid,'GroupName').'-'.$period."</b></h2></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>-->
 <div class="div_third">

<table width="100%" border="1" cellpadding="2" cellspacing="0">
<?php 
$Header="<tr>";
$Header.="<td><strong>Particulars</strong></td>";
$Header.="<td><strong>Opening DR</strong></td>";
$Header.="<td><strong>Opening CR</strong></td>";
$Header.="<td><strong>Debit</strong></td>";
$Header.="<td><strong>Credit</strong></td>";
$Header.="<td><strong>Closing</strong></td>";
$Header.="</tr>";
echo $Header;
 if($Gtype=='G'){
  $getSubGroups=DB_query("Select * from accountgroupmaster where MainGroupId=$gid",$db);

}
  else if($Gtype=='N'){
  $getSubGroups=DB_query("Select * from accountgroupmaster where MainGroupId=0 and Type='G'
   and NatureOfGroup=".$gid."",$db);

}

  $openingCR_array=''; $openingDR_array='';
  //$closing='';
 // $getSubGroups=DB_query("Select * from accountgroupmaster where MainGroupId=$gid",$db);
//  $openingCR_array=''; $openingDR_array='';
  $i=0;
  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];
    if($res[0]!='' || $res[1]!='' || $res[2]!='' || $res[3]!=''){
  ?>
  <tr >
  <td style="padding-left:5px"><strong><?php echo $subGroups['GroupName'];?></td>
<?php 
    $DROPEN=$res[2];
   $CROPEN=$res[3];
   if($DROPEN>$CROPEN)
   { $TOTALOPDR=$DROPEN-$CROPEN; /*echo "&nbsp;DR";*/ $openingDR_array[]=$TOTALOPDR; $TOTALOPCR='';}
   elseif($CROPEN>$DROPEN)
   { $TOTALOPCR=$CROPEN-$DROPEN; /*echo "&nbsp;CR"; */$openingCR_array[]=$TOTALOPCR; $TOTALOPDR='';}else{$TOTALOPCR=''; $TOTALOPDR='';}
   ?>
  <td style="text-align:right; padding-right:5px"><strong><?php echo $TOTALOPDR;?></strong></td>
  <td style="text-align:right; padding-right:5px"><strong><?php echo $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>
  <?php 
 $Gtotal_DR=$TOTALOPDR+$TD;
 $Gtotal_CR=$TOTALOPCR+$TC;
 if($Gtotal_DR>$Gtotal_CR){$closing=$Gtotal_DR-$Gtotal_CR; $type="DR";}
 elseif($Gtotal_CR>$Gtotal_DR){$closing=$Gtotal_CR-$Gtotal_DR; $type="CR";}else{$closing='';$type="";} 
 ?>
  <td style="text-align:right; padding-right:5px"><strong><?php echo $closing."&nbsp;".$type; //echo "<br />".$Gtotal_DR."<br />".$Gtotal_CR; echo "<br />------------<br />".$TOTALOPDR."----".$TD; echo "<br />------------<br />".$TOTALOPCR."----".$TC;?></strong></td>
  </tr><?php $i++;} }?>
  <tr ></tr>
  <?php $j=$i;//echo $_SESSION['FDATE']; echo "------------";echo $_SESSION['TDATE'];
 if($Gtype=='G'){
 $getLedgers=DB_query("Select * from accountledger where UnderGroup=$gid order by AccountName ASC",$db);
  while($Ledgers=DB_fetch_array($getLedgers))
  {
    $ledger_id=$Ledgers['AccountsID'];
  
  $Ledgerstype=$s->getField('accountledger','AccountsID',$ledger_id,'ledgertype');
  if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
  {
    
    $query_details="Select (B.openingBalance) as openingBalance,B.openingtype as openingtype from accountledger A inner join accountledger_dtls B on A.AccountsID=B.accountledger_id
     where A.AccountsID in ($ledger_id) and B.openingdate<='".$_SESSION['TDATE']."'";
     
  //$opening_bal=$s->GetFieldValue('accountledger',"AccountsID=$ledger_id and openingdate<='".$_SESSION['TDATE']."'",'openingBalance');
     }
  else
  {
   
    $query_details="Select (B.openingBalance) as openingBalance,B.openingtype as openingtype from accountledger A inner join accountledger_dtls B on A.AccountsID=B.accountledger_id
     where A.AccountsID in ($ledger_id) ";
     

  //$opening_bal=$s->getField('accountledger','AccountsID',$ledger_id,'openingBalance');
    }
    $sql_check_dtls=DB_query($query_details,$db);
     while($row_dtls=DB_fetch_array($sql_check_dtls))
          {

           $opening_amt[$row_dtls['openingtype']][]=$row_dtls['openingBalance']; 
          }


        if(!empty($opening_amt[DR]))
        {
            $openingDR=array_sum($opening_amt[DR]);
            $opening_type='DR';  
         }   
            if(!empty($opening_amt[CR]))
            { 
            $openingCR=array_sum($opening_amt[CR]);
             $opening_type='CR';  
            }

    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','','');}
  
  
  if($opening_bal!='' || ($totVoucherDR)!='' || ($totVoucherCR)!=''){
  ?>
  <tr>
  <td style="padding-left:5px"><strong><font color="#000000"><?php echo $Ledgers['AccountName']; //echo "----".$openingCR."-------".$openingDR;//print_r($ledgerPosting);?></font></strong></td>
 <?php if($opening_bal!=''){ $opening_bal."&nbsp;".$opening_type; }
  if($opening_type=='CR'){
  $openingCR_array[]=$opening_bal; $OPCR=$opening_bal; $OPDR='';}
  else{$openingDR_array[]=$opening_bal; $OPDR=$opening_bal; $OPCR='';}?>
  <td style="text-align:right; padding-right:5px"><strong><?php echo $OPDR;?></strong></td>
  <td style="text-align:right; padding-right:5px"><strong><?php echo $OPCR;?></strong></td>
  
  <td style="text-align:right; padding-right:5px"><strong><?php  $total_DR=$totVoucherDR+$etaxDR+$staxDR+$invDR+$legderDR; 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; if($total_CR==0)echo " "; else echo $total_CR; $ARRCR[]=$total_CR;?></strong></td>
  <?php 
  $Gtotal_DR1=$OPDR+$total_DR;
  $Gtotal_CR1=$OPCR+$total_CR;
  if($Gtotal_DR1>$Gtotal_CR1){$closing=$Gtotal_DR1-$Gtotal_CR1; $type="DR"; }
  elseif($Gtotal_CR1>$Gtotal_DR1){$closing=$Gtotal_CR1-$Gtotal_DR1; $type="CR";}else{$closing='';$type="";}
  ?>
  <td style="text-align:right; padding-right:5px"><strong><?php echo $closing."&nbsp;".$type;?></strong></td>
  </tr><?php }} } ?>
<tr>
    <td style="text-align:right" ><strong>Total</strong></td>
  <?php 
   if($openingDR_array!='')$opDR=array_sum($openingDR_array);
   if($openingCR_array!='')$opCR=array_sum($openingCR_array);
   if($opDR>$opCR){$tot_op_DR=$opDR-$opCR;}else{$tot_op_CR=$opCR-$opDR;}
   ?>
  <td  style="text-align:right"><?php echo $opDR;?></td>
  <td style="text-align:right"><?php echo $opCR;?></td> 
    <td style="text-align:right"><b><?php 
  if($TDA!=''){$xx=array_sum($TDA);}else{$xx=0;}
  if($ARRDR!=''){$yy=array_sum($ARRDR);}else{$yy=0;}
  echo $xx+$yy;
  ?></b></td>
  <td style="text-align:right"><b><?php 
  if($TCA!=''){$mm=array_sum($TCA);}else{$mm=0;}
  if($ARRCR!=''){$nn=array_sum($ARRCR);}else{$nn=0;}
  echo $mm+$nn;?></b></td>
  <?php 
  $FinalDR=$tot_op_DR+$xx+$yy;
  $FinalCR=$tot_op_CR+$mm+$nn;
  if($FinalDR>$FinalCR){$closing=$FinalDR-$FinalCR; $type="DR"; }
  elseif($FinalCR>$FinalDR){$closing=$FinalCR-$FinalDR; $type="CR";}else{$closing='';$type="";}
  ?>
  <td style="text-align:right" ><b><?php echo $closing."&nbsp;".$type;?></b></td>
  </tr>
</table>
</div>
</div>
<?php 
date_default_timezone_set('Asia/Kolkata');
$filename = "group_summary_".date('y-m-d-h:i:s'); 
header("Content-Type: application/x-msdownload"); 
header("Content-Disposition: attachment; filename=$filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
?>