<?
 session_start();
 $brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$level=$_SESSION['level'];

 $branch=$_SESSION['branch'];
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); parent.document.location='../../ind.php' </script>";
  return;
}

 include("../../test.php");
if($unit=='Mahindra')
{
$db=mysqli_connect("localhost","root");
mysqli_select_db("mahdb1") or die(mysqli_error($db));
print $unit;
}
else
 include ("../../opendb.php");
$br=str_replace(' ','',$branch);
$br=str_replace('(','',$br);
$br=str_replace(')','',$br);

$tbl='tbcls1'.$brcode.$user;
$tblop='tbclsop1'.$brcode.$user;
$tbl1='tbclssum'.$brcode.$user;
$tbl2='ledger'.$brcode.$user;

$Suppress=$_REQUEST['Suppress'];
$y=$_REQUEST['y'];
$m=$_REQUEST['m'];
$d=$_REQUEST['d'];
$y1=$_REQUEST['y1'];
$m1=$_REQUEST['m1'];
$d1=$_REQUEST['d1'];

mysqli_query($db,"drop table if exists $tbl");
mysqli_query($db,"drop table if exists $tblop");
mysqli_query($db,"drop table if exists $tbl1");
mysqli_query($db,"drop table if exists $tbl2");



mysqli_query($db,"create table if not exists $tbl2(led varchar(200),grp varchar(250),op double(18,2),drcr varchar(3),brcode varchar(20),headgroup varchar(200),nature varchar(20),sno double(10,0) default 0,ghlevel double(10,0) default 0)");



mysqli_query($db,"create table if not exists $tbl(partic varchar(100),totdr double(14,2) default 0,totcr double(14,2)default 0)");

mysqli_query($db,"create table if not exists $tblop(partic varchar(100),totdr double(14,2) default 0,totcr double(14,2)default 0)");

mysqli_query($db,"create table if not exists $tbl1(led varchar(200),opdr double(14,2) default 0,opcr double(14,2)default 0,dr double(14,2) default 0,cr double(14,2)default 0,cldr double(14,2) default 0,clcr double(14,2)default 0,ltype varchar(10),grp1 varchar(50))");
/*
$chkres=mysqli_query($db,$sq);
if(mysqli_num_rows($chkres)>0)
{
 print "<font color=red> Following ledgers could not be found</font> <br>";
 $i=1;
 while($chrow=mysqli_fetch_array($chkres))
 {
   print $i++.". $chrow[0] <br>";
 }
 print "<a href='../../ledge.php'>Create Ledger now </a>";
 return;
}
*/

$df=$y.'-'.$m.'-'.$d;
$dt=$y1.'-'.$m1.'-'.$d1;
mysqli_query($db,"create table if not exists $tbl(partic varchar(100),totdr double(14,2) default 0,totcr double(14,2)default 0)");
mysqli_query($db,"create table if not exists $tblop(partic varchar(100),totdr double(14,2) default 0,totcr double(14,2)default 0)");

$ad=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($ad);

?>

<style>
td{font-family:verdana;font-size:13px}
a{text-decoration:none;color:black}
a:hover{text-decoration:underline}
</style>
<body bgcolor=ivory>
<center>
<table>
    
    <tr><td align=center><b><?print $unit.' '.$branch?></td></tr>
<tr><td align=center>PSN AUTOMOTIVE MARKETING PVT. LTD</td></tr>
<tr><td align=center><p style={letter-spacing:2;font-size:15}><b><?print $brrow['brname']?></TD></TR>
<TR><TD ALIGN=CENTER><font size=2></td></TR>

<TR><TD ALIGN=CENTER><font size=2><?print strtoupper($brrow['braddr']).', '.strtoupper($brrow['brcity'])?></td></tr>

<tr><td align=center><b><?print $unit.' '.$branch?></td></tr>
<tr><td align=center>Trial Balance upto <?print implode('-', array_reverse(explode('-', $dt)))?></td></tr>

<tr><td align=center>[Printed by <?print $user?> on <? date_default_timezone_set("Asia/Kolkata"); print date("d-m-Y  h:i:s a")?>]</td></tr>
</table>
<table border=1>
<th>Ledger <th colspan=2>Opening Balance <th colspan=2>Transaction <th colspan=2>Closing
<tr align=center><td>&nbsp;</td><td>Debit </td><td>Credit</td><td>Debit </td><td>Credit</td><td>Debit </td><td>Credit</td></tr>

<?

$jour_opdet = mysqli_query($db,"insert into $tblop select partic,sum(dramt) as totdr,sum(cramt) as totcr from journal2 where brcode='$brcode' and date < '$df' group by partic") or die(mysqli_error($db));
$jour_det = mysqli_query($db,"insert into $tbl select partic,sum(dramt) as totdr,sum(cramt) as totcr from journal2 where  brcode='$brcode' and date >= '$df' and date<='$dt' group by partic") or die(mysqli_error($db));

$led_det = mysqli_query($db,"insert into $tbl2 (led,grp,op,drcr,brcode,headgroup,nature,ghlevel) select a.led,a.grp,a.op,a.drcr,a.brcode,b.headgroup,b.nature,b.ghlevel from ledger a,acgroups b where a.grp=b.gname and a.brcode='$brcode'") or die(mysqli_error($db));
            
 mysqli_query($db,"update  $tbl2 set sno=1 where nature='Liabilities' ") or die(mysqli_error($db));
 //print $m=("update  $tbl2 set sno=1 where nature='Liabilities' ") or die(mysqli_error($db));
 
 mysqli_query($db,"update  $tbl2 set sno=2 where nature='Assets' ") or die(mysqli_error($db));
 mysqli_query($db,"update  $tbl2 set sno=3 where nature='Income' ") or die(mysqli_error($db));
 mysqli_query($db,"update  $tbl2 set sno=4 where nature='Expenses' ") or die(mysqli_error($db));
 
            



$result = mysqli_query($db,"select * from $tbl2 where brcode='$brcode' order by sno,ghlevel,grp,led") or die("ERR:".mysqli_error($db));

$i=1;
$mopdrtot = 0;
$mopcrtot = 0; 
$mdrtot = 0;
$mcrtot = 0;
$mcldrtot =0;
$mclcrtot =0;
$mgrp = '';
while($row=mysqli_fetch_row($result))
{ 
    
$tled=trim($row[0]);

    $reop=mysqli_query($db,"select * from $tblop where trim(partic)='$tled'") or die("ERR:".mysqli_error($db));
    $roop1=mysqli_fetch_row($reop);

    $re=mysqli_query($db,"select * from $tbl where trim(partic)='$tled'") or die("ERR:".mysqli_error($db));
    $ro=mysqli_fetch_row($re);
    $mopdr = 0;
    $mopcr = 0; 
    if($row[3]=='Dr')
    {
         if ($row[2]>=0)
	      $mopdr = $row[2];
         else
             $mopcr = $row[2] * -1;
    }
    if($row[3]=='Cr')
    {
         if ($row[2]>=0)
	      $mopcr = $row[2];
         else
             $mopdr = $row[2] * -1;
    }
    $mdr = 0;
    $mcr = 0;
    $mopt_trans = 0;
    if($roop1[1]>0) 
        $mopt_trans = $roop1[1];
    if($roop1[2]>0)
	 $mopt_trans = $mopt_trans - $roop1[2];
    $mopdr = $mopdr - $mopcr + $mopt_trans;
    $mopcr = 0;
    if($mopdr < 0)
        {
        $mopcr = $mopdr * -1;
        $mopdr = 0;
        }
    if ($ro[1]>0)
        $mdr = $ro[1];
    if ($ro[2]>0)
        $mcr = $ro[2];    
    $mopdrtot +=$mopdr;
    $mopcrtot +=$mopcr; 
    $mdrtot += $mdr;
    $mcrtot += $mcr;
    $mclbal = $mopdr - $mopcr + $mdr - $mcr;
    $mcldr = 0;
    $mclcr = 0;
    if ($mclbal>=0)
        $mcldr = $mclbal;
    else
        $mclcr = $mclbal * -1;
    $mcldrtot +=$mcldr; 
    $mclcrtot +=$mclcr;
    
    $check=$_REQUEST['check'];
    
    $mopdr=number_format($mopdr,2,".","");
$mopcr=number_format($mopcr,2,".","");

$mdr=number_format($mdr,2,".","");
$mcr=number_format($mcr,2,".","");

$mcldr=number_format($mcldr,2,".","");
$mclcr=number_format($mclcr,2,".","");
    
    if($check=='Suppress')
    {
       if($mopdr>'0' ||$mopcr>'0' || $mdr>'0'|| $mcr>'0')
	{
          if($mgrp!=$row[1])
          {
              print "<tr><td><b><font color='#a41a31'>$row[1]</font></td></tr>";
              $mgrp=$row[1];
           }
          print "<tr><td><a href='../../vmledger.php?cd=".str_replace('&','~',$row[0])."' title='Click to view ledger' target='m'>".strtoupper($row[0])."</a></td><td align='right'>".putComa($mopdr)."</td><td align='right'>".putComa($mopcr)."</td><td align='right'>".putComa($mdr)."</td><td align='right'>".putComa($mcr)."</td><td align='right'>".putComa($mcldr)."</td><td align='right'>".putComa($mclcr)."</td></tr>";
	   $jour_sum = mysqli_query($db,"insert into $tbl1 (led,opdr,opcr,dr,cr,cldr,clcr,grp1) values ('$row[0]','$mopdr','$mopcr','$mdr','$mcr','$mcldr','$mclcr','$row[1]')") or die(mysqli_error($db));
	}
    }
    else
    {
	print "<tr><td>$row[0]</td><td align='right'>".putComa($mopdr)."</td><td align='right'>".putComa($mopcr)."</td><td align='right'>".putComa($mdr)."</td><td align='right'>".putComa($mcr)."</td><td align='right'>".putComa($mcldr)."</td><td align='right'>".putComa($mclcr)."</td></tr>";
       $jour_sum = mysqli_query($db,"insert into $tbl1 (led,opdr,opcr,dr,cr,cldr,clcr,grp1) values ('$row[0]','$mopdr','$mopcr','$mdr','$mcr','$mcldr','$mclcr','$row[1]')") or die(mysqli_error($db));
    }
}

$mopdrtot=number_format($mopdrtot,2,".","");
$mopcrtot=number_format($mopcrtot,2,".","");

$mdrtot=number_format($mdrtot,2,".","");
$mcrtot=number_format($mcrtot,2,".","");

$mcldrtot=number_format($mcldrtot,2,".","");
$mclcrtot=number_format($mclcrtot,2,".","");
if ($mopdrtot>$mopcrtot)
{
	print "<tr><td>Opening Contra</td><td align='right'>".putComa(0)."</td><td align='right'>".putComa($mopdrtot-$mopcrtot)."</td>";
	$mopcrtot=$mopdrtot;
}
elseif ($mopdrtot<$mopcrtot)
{
	print "<tr><td>Opening Contra</td><td align='right'>".putComa($mopcrtot-$mopdrtot)."</td><td align='right'>".putComa(0)."</td>";
	$mopdrtot=$mopcrtot;
}
else 
	print "<tr><td>Opening Contra</td><td align='right'>".putComa(0)."</td><td align='right'>".putComa(0)."</td>";
if ($mdrtot>$mcrtot)
{
	print "<td align='right'>".putComa(0)."</td><td align='right'>".putComa($mdrtot-$mcrtot)."</td>";
	$mcrtot=$mdrtot;
}
elseif ($mdrtot<$mcrtot)
{
	print "<td align='right'>".putComa($mcrtot-$mdrtot)."</td><td align='right'>".putComa(0)."</td>";
	$mdrtot=$mcrtot;
}
else
	print "<td align='right'>".putComa(0)."</td><td align='right'>".putComa(0)."</td>";
if ($mcldrtot>$mclcrtot)
{
	print "<td align='right'>".putComa(0)."</td><td align='right'>".putComa($mcldrtot-$mclcrtot)."</td></tr>";
	$mclcrtot=$mcldrtot;
}
elseif ($mcldrtot<$mclcrtot)
{
	print "<td align='right'>".putComa($mclcrtot-$mcldrtot)."</td><td align='right'>".putComa(0)."</td></tr>";
	$mcldrtot=$mclcrtot;
}
else
	print "<td align='right'>".putComa(0)."</td><td align='right'>".putComa(0)."</td></tr>";
print "<tr><td>T O T A L</td><td align='right'>".putComa($mopdrtot)."</td><td align='right'>".putComa($mopcrtot)."</td><td align='right'>".putComa($mdrtot)."</td><td align='right'>".putComa($mcrtot)."</td><td align='right'>".putComa($mcldrtot)."</td><td align='right'>".putComa($mclcrtot)."</td></tr>";
?>
<script>
 window.open('listclstbsum1.php?df=<?print $df?>&dt=<?print $dt?>');
</script>

