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

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;
mysqli_query($db,"drop table $tbl");
mysqli_query($db,"drop table $tblop");
mysqli_query($db,"drop table $tbl1");
mysqli_query($db,"delete from $tbl");
mysqli_query($db,"delete from $tblop");
mysqli_query($db,"delete from $tbl1");
mysqli_query($db,"create table $tbl(partic varchar(100),totdr double(14,2) default 0,totcr double(14,2)default 0)");
mysqli_query($db,"create table $tblop(partic varchar(100),totdr double(14,2) default 0,totcr double(14,2)default 0)");
mysqli_query($db,"create table $tbl1(led varchar(100),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))");
$d1=$_REQUEST['d1'];
$m1=$_REQUEST['m1'];
$y1=$_REQUEST['y1'];
$d=$_REQUEST['d'];
$m=$_REQUEST['m'];
$y=$_REQUEST['y'];
$mon=$_REQUEST['m1'];
$df=$y.'-'.$m.'-'.$d;
$dt=$y1.'-'.$m1.'-'.$d1;
mysqli_query($db,"create table $tbl(partic varchar(100),totdr double(14,2) default 0,totcr double(14,2)default 0");
mysqli_query($db,"create table $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);
$sm=$_REQUEST['m'];
$em=$_REQUEST['m1'];
$sale = 0;
if($sm=='1')
{
    $sm='13';
}
if($sm=='2')
{
    $sm='14';
}
if($sm=='3')
{
    $sm='15';
}
if($em=='1')
{
    $em='13';
}
if($em=='2')
{
    $em='14';
}
if($em=='3')
{
    $em='15';
}
?>
<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><br><U><B><h3><? print $jtype?></h3> </B></U></TD></TR>
<tr><td align=center>Profit & Loss from <?print implode('-', array_reverse(explode('-', $df))).' to '.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")?>]</td></tr>
</table>
<table border=1>
<tr><td>Ledger</td> <td>As on Total   </td><td>April</td><td>May </td><td>June </td><td>July </td><td>August </td><td>Sept </td><td>Oct </td><td>Nov </td><td>Dec </td><td>Jan </td><td>Feb </td><td>March </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));

//$resultg = mysqli_query($db,"select distinct grp from ledger order by grp") or die("ERR:".mysqli_error($db));
$resultg = mysqli_query($db,"select gname from acgroups where gname in (select distinct grp from ledger where brcode='$brcode' ) and (nature='Expenses' or nature='income') and ghlevel<=10 order by ghlevel") or die("ERR:".mysqli_error($db));
//print "select gname from acgroups where gname in (select distinct grp from ledger where brcode='$brcode' ) and (nature='Expenses' or nature='income') and ghlevel<=10 order by ghlevel";
$i=1;
$mopdrtot = 0;
$mopcrtot = 0; 
$mdrtot = 0;
$mcrtot = 0;
$mcldrtot =0;
$mclcrtot =0;
$gppp=0;
$gp=0;
$pr=0;
$jj=1;
$stot=0;
$mgrp = '';
unset($vr_total);
$vr_total = array();
unset($pl_tot);
$pl_tot = array();
unset($sale_total);
$sale_total = array();
unset($gp_total);
$gp_total = array();
$mtot_gross=0;
while($rowg=mysqli_fetch_row($resultg))
{ 
    //print $stot;
    $group=trim($rowg[0]);
    if($jj>1)
    {
        if($mgrp!=$group)
        {
            if($mgrp=='')
            {
                $mgrp='INVENTORIES';
                print "<tr><td><font color=black><b>$mgrp</b></font></td><td align='right'><font color=black><b>".abs(number_format($op_stock[$sm],2,".",""))."</b></font></td>";
                
                $mtot_gross=+$op_stock[$sm];
                
            }
            else
            {
                if(substr($mgrp,0,23)=='REVENUE FROM OPERATIONS')
                {
                    $sale = $sale + abs($stot);
                }
                print "<tr><td><font color=black><b>$mgrp</b></font></td><td align='right'><font color=black><b>".abs(number_format($stot,2,".",""))."</b></font></td>";
            }
	        $stot=0;
            for($month=$sm; $month<=$em; $month++)
            {
                $k=$month;
                if($month=='13')
                {
                    $k=1;
                }
                if($k=='14')
                {
                    $k=2;
                }
                if($month=='15')
                {
                    $k=3;
                }
                //$k= sprintf("%02s", $k);
                ///// $gr=$gr_total[$k];  
                if($mgrp=='INVENTORIES')
                {
                    $gr= $op_stock[$k]; 
                }
                else
                {
                    $gr=$gr_total[$k];  
                }
                print " <td align='right'><font color=black><b>".abs(number_format($gr,2,".",""))."</b></font></td>";     
                $gr_total[$k]=0;
            }
            $mgrp=$group; 
        }
    }
    $jj++;       
    /////print"<tr><td><b>$rowg[0]</b></td><td></td></tr>";
    $result = mysqli_query($db,"select * from ledger where grp='$rowg[0]' and brcode='$brcode' order by led") or die("ERR:".mysqli_error($db));
    while($row=mysqli_fetch_row($result))
    { 
        $group=trim($row[1]);
        $reop=mysqli_query($db,"select * from $tblop where partic='$row[0]' ") or die("ERR:".mysqli_error($db));
        $roop1=mysqli_fetch_row($reop);

        $re=mysqli_query($db,"select * from $tbl where partic='$row[0]' ") 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;
        $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($rowg[0]=='REVENUE FROM OPERATIONS SPARE AND LUB')
        {
           // $sale= $sale + abs($mclcr);    
        }
        if($check=='Suppress')
        {
            if($mopdr>'0' ||$mopcr>'0' || $mdr>'0'|| $mcr>'0' || $rowg[0]=='INVENTORIES')
	        {
                if($mcldr==0)
                {
                    $fig=-($mclcr);
                    $mtot_gross=+$mtot_gross-$mclcr;
                }
                else
                {
                    $fig=$mcldr;   
                    $mtot_gross=+$mtot_gross+$mcldr;
                }
                $fig=number_format($fig,2,".","");
                if($rowg[0]=='INVENTORIES')
                {
                    $j1= mysqli_query($db,"select opstock from closingstock where brcode='$brcode' and month=$sm" );
                    $c1= mysqli_fetch_array($j1);
                    $ops = $c1[0];
                    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'>".abs($ops)."</td>";
                }
                else
                    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'>".abs($fig)."</td>";                
                $stot+=$fig;
                for($month=$sm; $month<=$em; $month++)
                {
                    $k=$month;
                    if($month=='13')
                    {
                        $k=1;
                    }
                    if($k=='14')
                    {
                        $k=2;
                    }
                    if($month=='15')
                    {
                        $k=3;
                    }
                    //$k= sprintf("%02s", $k);
                    //print  $rowg[0];
                    if($rowg[0]=='INVENTORIES')
                    {
                        $jqr1 = mysqli_query($db,"select opstock from closingstock where brcode='$brcode' and month=$k" );
                        $cqr1=mysqli_fetch_array($jqr1);
                        $op_stock[$k]=$cqr1[0];
                        $op_total[$k]+=$cqr1[0];
                    }      
                    else
                    {
                        $jqr2 = mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where partic='$row[0]' and brcode='$brcode' and month(date)='$k'" );
                        $cqr2=mysqli_fetch_array($jqr2); 
                        $vr_total[$k]+=$cqr2[0];
                        $gr_total[$k]+=$cqr2[0];
                    }
                    if($rowg[0]=='REVENUE FROM OPERATIONS SPARE AND LUB')
                    {
                        $jjqr3 = mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where partic='$row[0]' and brcode='$brcode' and month(date)=$k" );
                        $ccqr3=mysqli_fetch_array($jjqr3); 
                        //$sale_total[$k]+=$cc[0];
                    }
                    print " <td align='right'>".abs(number_format($ccqr3[0],2,".",""))."</td>";
                    if(substr($mgrp,0,23)=='REVENUE FROM OPERATIONS')
                    {
                        $sale_total[$k]+=abs($cqr2[0]);
                    }
                }  
                print "</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,".","");
$re=mysqli_query($db,"select clstock from closingstock where month='$mon' and brcode='$brcode'") or die(mysqli_error($db));
$r=mysqli_fetch_row($re);
$closingstock= $r[0];
$mopcrtot+=$closingstock;
$mclcrtot+=$closingstock;
if ($mcldrtot>$mclcrtot)
{
    $j1 = mysqli_query($db,"select clstock from closingstock where brcode='$brcode' and month=$m1" );
    $c1=mysqli_fetch_array($j1);
    $mcl_stck =$c1[0];
    print "<tr><td><font color=black><b>CLOSING STOCK</b></font></td><td align='right'><font color=black><b>".putComa(-$mcl_stck)."</b></font></td>";
    $mtot_gross=$mtot_gross-$c1[0];
    for($month=$sm; $month<=$em; $month++)
    {
        $k=$month;
        if($month=='13')
        {
            $k=1;
        }
        if($k=='14')
        {
            $k=2;
        }
        if($month=='15')
        {
            $k=3;
        }
        //$k= sprintf("%02s", $k);
        $j = mysqli_query($db,"select clstock from closingstock where brcode='$brcode' and month=$k" );
        $c=mysqli_fetch_array($j);
        $cl_total[$k]+=$c[0];
        print " <td align='right'><font color=black><b>".number_format($c[0],2,".","")."</b></font></td>";
    }
    print "</tr>";
    $gp=$mcldrtot-$mclcrtot;
    $mtot_gross=-1*$mtot_gross;
	print "<tr><td><font color=black><b>GROSS LOSS</b></font></td><td align='right'><font color=black><b>".putComa($mtot_gross)."</b></font></td>";
	for($month=$sm; $month<=$em; $month++)
    {
        $k=$month;
        if($month=='13')
        {
            $k=1;
        }
        if($k=='14')
        {
            $k=2;
        }
        if($month=='15')
        {
            $k=3;
        }
        //$k= sprintf("%02s", $k);
        $vr_total[$k]-=$cl_total[$k];
        $vr_total[$k]+=$op_total[$k];
        $pr=$vr_total[$k]*-1;
        print " <td align='right'><font color=black><b>".number_format($pr,2,".","")."</b></font></td>";     
        $gp_total[$k]=$pr;
    }
    print "</tr>";
	$mopcrtot=$mopdrtot;
    if($check!='closing')
    {
        //print "<tr><td>T O T A L 11</td><td align='right'>".putComa($mcldrtot)."</td><td align='right'>".putComa($mclcrtot)."</td></tr>";
    }
    $gpp=($gp/$sale)*100;
    $gpp=number_format($gpp,"2",".","");
    //print "<tr><td>GP %</td><td align='right'>".putComa(0)."</td><td align='right'>".putComa($gpp)."</td></tr>";
    print "<tr><td><font color=black><b>LOSS %</b></font></td><td align='right'><font color=black><b>".putComa($gpp)."</b></font></td>";
	for($month=$sm; $month<=$em; $month++)
    {
        $k=$month;
        if($month=='13')
        {
            $k=1;
        }
        if($k=='14')
        {
            $k=2;
        }
        if($month=='15')
        {
            $k=3;
        }
        //$k= sprintf("%02s", $k);
        $gppp=$gp_total[$k]/$sale_total[$k]*100;
        $gppp=$gppp*-1;
        print " <td align='right'><font color=black><b>".number_format($gppp,2,".","")."</b></font></td>";     
        $gp_total[$k]=$pr;
    }
	print "</tr>";
}
if ($mcldrtot<=$mclcrtot)
{
    if($group=='STOCK TRANSFER OUT')
    {
        $sale = $sale + abs($stot);
    }
    print "<tr><td><font color=black><b>$group</b></font></td><td align='right'><font color=black><b>".abs(number_format($stot,2,".",""))."</b></font></td>";
    for($month=$sm; $month<=$em; $month++)
    {
        $k=$month;
        if($month=='13')
        {
            $k=1;
        }
        if($k=='14')
        {
            $k=2;
        }
        if($month=='15')
        {
            $k=3;
        }
        //$k= sprintf("%02s", $k);
        if($group=='STOCK TRANSFER OUT')
           $sale_total[$k]+=abs($gr_total[$k]);
        print " <td align='right'><font color=black><b>".abs(number_format($gr_total[$k],2,".",""))."</b></font></td>";     
    }
	print "</tr>";
	$j1 = mysqli_query($db,"select clstock from closingstock where brcode='$brcode' and month=$m1" );
    $c1=mysqli_fetch_array($j1);
    $mcl_stck =$c1[0];
    print "<tr><td><font color=black><b>CLOSING STOCK</b></font></td><td align='right'><font color=black><b>".putComa($mcl_stck)."</b></font></td>";
    $mtot_gross=$mtot_gross-$mcl_stck;
    for($month=$sm; $month<=$em; $month++)
    {
        $k=$month;
        if($month=='13')
        {
            $k=1;
        }
        if($k=='14')
        {
            $k=2;
        }
        if($month=='15')
        {  
            $k=3;
        }
        //$k= sprintf("%02s", $k);
        $j = mysqli_query($db,"select clstock from closingstock where brcode='$brcode' and month=$k" );
        $c=mysqli_fetch_array($j);
        $cl_total[$k]+=$c[0];
        print " <td align='right'><font color=black><b>".number_format($c[0],2,".","")."</b></font></td>";
    }
    print "</tr>";
    $gp=$mclcrtot-$mcldrtot;
    $mtot_gross=-1*$mtot_gross;    
    print "<tr><td><font color=black><b>GROSS PROFIT</b></font></td><td align='right'><font color=black><b>".putComa($mtot_gross)."</b></font></td>";
	for($month=$sm; $month<=$em; $month++)
    {
        $k=$month;
        if($month=='13')
        {
            $k=1;
        }
        if($k=='14')
        {
            $k=2;
        }
        if($month=='15')
        {
            $k=3;
        }
        //$k= sprintf("%02s", $k);
        $vr_total[$k]-=$cl_total[$k];
        $vr_total[$k]+=$op_total[$k];
        $pr=$vr_total[$k]*-1;
        print " <td align='right'><font color=black><b>".number_format($pr,2,".","")."</b></font></td>";     
        $gp_total[$k]=$pr;
    }
	print "</tr>";
    $gpp=($gp/$sale)*100;
    $gpp=number_format($gpp,"2",".","");
    $gpp_tot = round(($mtot_gross/$sale)*100,2);
	print "<tr><td><font color=black><b>GP %</b></font></td><td align='right'><font color=black><b>".putComa($gpp_tot)."</b></font></td>";
	for($month=$sm; $month<=$em; $month++)
    {
        $k=$month;
        if($month=='13')
        {
            $k=1;
        }
        if($k=='14')
        {
            $k=2;
        }
        if($month=='15')
        {
            $k=3;
        }
        //$k= sprintf("%02s", $k);
        $gppp=$gp_total[$k]/$sale_total[$k]*100;
        $gppp=$gppp;
        print " <td align='right'><font color=black><b>".abs(number_format($gppp,2,".",""))."</b></font></td>";     
        $gp_total[$k]=$pr;
    }
	print "</tr>";
}
//  start discount
$resultg = mysqli_query($db,"select gname from acgroups where gname in (select distinct grp from ledger where brcode='$brcode' ) and (nature='Expenses' or nature='income') and ghlevel>=11 and ghlevel<=14 order by ghlevel") or die("ERR:".mysqli_error($db));
$jj=1;
$stot=0;
unset ($gr_total);
while($rowg=mysqli_fetch_row($resultg))
{ 
    $group=trim($rowg[0]);
    if($jj>1)
    {
        if($mgrp!=$group)
        {
            print "<tr><td><font color=black><b>Total3</b></font></td><td align='right'><font color=black><b>".abs(number_format($stot,2,".",""))."</b></font></td>";
		    $stot=0;
	        for($month=$sm; $month<=$em; $month++)
            {
                $k=$month;
                if($month=='13')
                {
                    $k=1;
                }
                if($k=='14')
                {
                    $k=2;
                }
                if($month=='15')
                {
                    $k=3;
                }
                //$k= sprintf("%02s", $k);
                $gr=$gr_total[$k];
                print " <td align='right'><font color=black><b>".abs(number_format($gr,2,".",""))."</b></font></td>";     
                $mgrp=$group; 
                $gr_total[$k]=0;
            }
        }
    }
    $jj++;       
    $result = mysqli_query($db,"select * from ledger where grp='$rowg[0]' and brcode='$brcode' order by led") or die("ERR:".mysqli_error($db));
    while($row=mysqli_fetch_row($result))
    { 
        $groupp=trim($row[1]);
        $reop=mysqli_query($db,"select * from $tblop where partic='$row[0]' ") or die("ERR:".mysqli_error($db));
        $roop1=mysqli_fetch_row($reop);
        $re=mysqli_query($db,"select * from $tbl where partic='$row[0]' ") 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;
        $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($mcldr==0)
                {
                    $fig=-($mclcr);
                }
                else
                {
                    $fig=$mcldr;   
                }
                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'>".number_format($fig,2,".","")."</td>";
                $stot+=$fig;
                for($month=$sm; $month<=$em; $month++)
                {
                    $k=$month;
                    if($month=='13')
                    {
                        $k=1;
                    }
                    if($k=='14')
                    {
                        $k=2;
                    }
                    if($month=='15')
                    {
                        $k=3;
                    }
                    //$k= sprintf("%02s", $k);
                    $j = mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where partic='$row[0]' and brcode='$brcode' and month(date)=$k" );
                    $c=mysqli_fetch_array($j);
                    $vr_total[$k]+=$c[0];
                    $gr_total[$k]+=$c[0];
                    print "<td align='right'>".number_format($c[0],2,".","")."</td>";
                }  
                print "</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 ($mcldrtot>$mclcrtot)
{
    for($month=$sm; $month<=$em; $month++)
    {
        $k=$month;
        if($month=='13')
        {
            $k=1;
        }
        if($k=='14')
        {
            $k=2;
        }
        if($month=='15')
        {
            $k=3;
        }
        //$k= sprintf("%02s", $k);
        $np=$vr_total[$k]*-1;
        $gp_total[$k]=$pr;
    }
	print "</tr>";
	$mopcrtot=$mopdrtot;
}
$mdisc_tot = $stot;
print "<tr><td><font color=black><b>$group</b></font></td><td align='right'><font color=black><b>".putComa($stot)."</b></font></td>";
$stot=0;
for($month=$sm; $month<=$em; $month++)
{
    $k=$month;
    if($month=='13')
    {
        $k=1;
    }
    if($k=='14')
    {
        $k=2;
    }
    if($month=='15')
    {
        $k=3;
    }
    //$k= sprintf("%02s", $k);
    print " <td align='right'><font color=black><b>".number_format($gr_total[$k],2,".","")."</b></font></td>";     
}
print "</tr>";
// end discount
//start profit after discount
$gp=$mclcrtot-$mcldrtot;
$mtot_gross=$mtot_gross-$mdisc_tot;
print "<tr><td><font color=black><b>GROSS PROFIT AFTER DISCOUNT</b></font></td><td align='right'><font color=black><b>".number_format($mtot_gross,2,".","")."</b></font></td>";
$gp_adisc+=$gp;
for($month=$sm; $month<=$em; $month++)
{
    $k=$month;
    if($month=='13')
    {
        $k=1;
    }
    if($k=='14')
    {
        $k=2;
    }
    if($month=='15')
    {
        $k=3;
    }
    //$k= sprintf("%02s", $k);
    $pr=-($vr_total[$k]);
    print " <td align='right'><font color=black><b>".(number_format($pr,2,".",""))."</b></font></td>";     
    $gp_total[$k]=$pr;
    $gp_total_ad[$k]=$pr;
}
print "</tr>";
$gpp=($gp/$sale)*100;
$gpp=number_format($gpp,"2",".","");
$gpp_per = round($mtot_gross/$sale*100,2);
print "<tr><td><font color=black><b>GP% AFTER DISCOUNT</b></font></td><td align='right'><font color=black><b>".(number_format($gpp_per,2,".",""))."</b></font></td>";
for($month=$sm; $month<=$em; $month++)
{
    $k=$month;
    if($month=='13')
    {
        $k=1;
    }
    if($k=='14')
    {
        $k=2;
    }
    if($month=='15')
    {
        $k=3;
    }
    //$k= sprintf("%02s", $k);
    $gppp=$gp_total[$k]/$sale_total[$k]*100;
    $gppp=$gppp*-1;
    print " <td align='right'><font color=black><b>".(number_format(abs($gppp),2,".",""))."</b></font></td>";     
    $gp_total[$k]=$pr;
}
print "</tr>";
// end profit after discount
//  start labour
$resultg = mysqli_query($db,"select gname from acgroups where gname in (select distinct grp from ledger where brcode='$brcode' ) and (nature='Expenses' or nature='income') and ghlevel>=15 and ghlevel<=16 order by ghlevel") or die("ERR:".mysqli_error($db));
$mgrp = '';
$jj=1;
$stot=0;
unset ($gr_total);
while($rowg=mysqli_fetch_row($resultg))
{ 
    $group=trim($rowg[0]);
    if($jj>1)
    {
        if($mgrp!=$group)
        {
            if($mgrp=='')
            {
                $mgrp='REVENUE FROM OPERATIONS LABOUR';
            }
            print "<tr><td><font color=black><b>$mgrp</b></font></td><td align='right'><font color=black><b>".abs(number_format($stot,2,".",""))."</b></font></td>";
	        $stot=0;
            for($month=$sm; $month<=$em; $month++)
            {
                $k=$month;
                if($month=='13')
                {
                    $k=1;
                }
                if($k=='14')
                {
                    $k=2;
                }
                if($month=='15')
                {
                    $k=3;
                }
                //$k= sprintf("%02s", $k);
                $gr=$gr_total[$k];
                print " <td align='right'><font color=black><b>".abs(number_format($gr,2,".",""))."</b></font></td>";     
                $mgrp=$group; 
                $gr_total[$k]=0;
            }
        }
    }
    $jj++;       
    $result = mysqli_query($db,"select * from ledger where grp='$rowg[0]' and brcode='$brcode' order by led") or die("ERR:".mysqli_error($db));
    while($row=mysqli_fetch_row($result))
    { 
        $groupp=trim($row[1]);
        $reop=mysqli_query($db,"select * from $tblop where partic='$row[0]' ") or die("ERR:".mysqli_error($db));
        $roop1=mysqli_fetch_row($reop);
        $re=mysqli_query($db,"select * from $tbl where partic='$row[0]' ") 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;
        $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($rowg[0]=='REVENUE FROM OPERATIONS LABOUR')
        {
            $labsale+=$mclcr;    
        }
        if($check=='Suppress')
        {
            if($mopdr>'0' ||$mopcr>'0' || $mdr>'0'|| $mcr>'0')
	        {
	            if($mcldr==0)
                {
                    $fig=-($mclcr);
                }
                else
                {
                    $fig=$mcldr;   
                }
                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'>".abs(number_format($fig,2,".",""))."</td>";
                $stot+=$fig;
                $llab +=$fig;
                for($month=$sm; $month<=$em; $month++)
                {
                    $k=$month;
                    if($month=='13')
                    {
                        $k=1;
                    }
                    if($k=='14')
                    {
                        $k=2;
                    }
                    if($month=='15')
                    {
                        $k=3;
                    }
                    //$k= sprintf("%02s", $k);
                    $j = mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where partic='$row[0]' and brcode='$brcode' and month(date)=$k" );
                    $c=mysqli_fetch_array($j);
                    $vr_total[$k]+=$c[0];
                    $gr_total[$k]+=$c[0];
                    $labour_total[$k]+=$c[0];
                    if($rowg[0]=='REVENUE FROM OPERATIONS LABOUR')
                    {
                        $lab_total[$k]+=$c[0];
                    }   
                    print "<td align='right'>".abs(number_format($c[0],2,".",""))."</td>";
                }  
                print "</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 ($mcldrtot>$mclcrtot)
{
    for($month=$sm; $month<=$em; $month++)
    {
        $k=$month;
        if($month=='13')
        {
            $k=1;
        }
        if($k=='14')
        {
            $k=2;
        }
        if($month=='15')
        {
            $k=3;
        }
        //$k= sprintf("%02s", $k);
        $np=$vr_total[$k]*-1;
        $gp_total[$k]=$pr;
    }
	print "</tr>";
	$mopcrtot=$mopdrtot;
}
print "<tr><td><font color=black><b>$group</b></font></td><td align='right'><font color=black><b>".abs(number_format($stot,2,".",""))."</b></font></td>";
$stot=0;
for($month=$sm; $month<=$em; $month++)
{
    $k=$month;
    if($month=='13')
    {
        $k=1;
    }
    if($k=='14')
    {
        $k=2;
    }
    if($month=='15')
    {
        $k=3;
    }
    //$k= sprintf("%02s", $k);
    print " <td align='right'><font color=black><b>".abs(number_format($gr_total[$k],2,".",""))."</b></font></td>";     
}
print "</tr>";
//end labour
//start NET LABOUR
$labour=$mclcrtot-$mcldrtot;
print "<tr><td><font color=black><b>NET LABOUR</b></font></td><td align='right'><font color=black><b>".abs(number_format($llab,2,".",""))."</b></font></td>";
$mnet_lab = $llab;
for($month=$sm; $month<=$em; $month++)
{
    $k=$month;
    if($month=='13')
    {
        $k=1;
    }
    if($k=='14')
    {
        $k=2;
    }
    if($month=='15')
    {
        $k=3;
    }
    //$k= sprintf("%02s", $k);
    //  $vr_total[$k]-=$cl_total[$k];
    // $vr_total[$k]+=$op_total[$k];
    $prr=$labour_total[$k];
    print " <td align='right'><font color=black><b>".abs(number_format($prr,2,".",""))."</b></font></td>";     
    $gp_total[$k]=$prr;
}
print "</tr>";
$gpp=($llab/$labsale)*100;
$gpp=number_format($gpp,"2",".","");
$gpp=$gpp*-1;
print "<tr><td><font color=black><b>NET LABOUR %</b></font></td><td align='right'><font color=black><b>".abs(number_format($gpp,2,".",""))."</b></font></td>";
for($month=$sm; $month<=$em; $month++)
{
    $k=$month;
    if($month=='13')
    {
        $k=1;
    }
    if($k=='14')
    {
        $k=2;
    }
    if($month=='15')
    {
        $k=3;
    }
    //$k= sprintf("%02s", $k);
    $gppp=$gp_lab[$k]/$lab_total[$k]*100;
    $gppp=$gppp*-1;
    $g=$labour_total[$k]/$lab_total[$k]*100;
    print " <td align='right'><font color=black><b>".abs(number_format($g,2,".",""))."</b></font></td>";     
    $gp_total[$k]=$pr;
}
print "</tr>";
// end NET LABOUR
//  start other income
$resultg = mysqli_query($db,"select gname from acgroups where gname in (select distinct grp from ledger where brcode='$brcode' ) and (nature='Expenses' or nature='income') and ghlevel>=17 and ghlevel<=24 order by ghlevel") or die("ERR:".mysqli_error($db));
$jj=1;
$stot=0;
unset ($gr_total);
while($rowg=mysqli_fetch_row($resultg))
{ 
    $group=trim($rowg[0]);
    if($jj>1)
    {
        if($mgrp!=$group)
        {
            print "<tr><td><font color=black><b>Total8</b></font></td><td align='right'><font color=black><b>".abs(number_format($stot,2,".",""))."</b></font></td>";
	        $stot=0;
	        for($month=$sm; $month<=$em; $month++)
            {
                $k=$month;
                if($month=='13')
                {
                    $k=1;
                }
                if($k=='14')
                {
                    $k=2;
                }
                if($month=='15')
                {
                    $k=3;
                }
                //$k= sprintf("%02s", $k);
                $gr=$gr_total[$k];
                print " <td align='right'><font color=black><b>".abs(number_format($gr,2,".",""))."</b></font></td>";     
                $mgrp=$group; 
                $gr_total[$k]=0;
            }
        }
    }
    $jj++;       
    $result = mysqli_query($db,"select * from ledger where grp='$rowg[0]' and brcode='$brcode' order by led") or die("ERR:".mysqli_error($db));
    while($row=mysqli_fetch_row($result))
    { 
        $groupp=trim($row[1]);
        $reop=mysqli_query($db,"select * from $tblop where partic='$row[0]' ") or die("ERR:".mysqli_error($db));
        $roop1=mysqli_fetch_row($reop);
        $re=mysqli_query($db,"select * from $tbl where partic='$row[0]' ") 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;
        $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($rowg[0]=='REVENUE FROM OPERATIONS LABOUR')
        {
            $labsale+=$mclcr;    
        }
        if($check=='Suppress')
        {
            if($mopdr>'0' ||$mopcr>'0' || $mdr>'0'|| $mcr>'0')
	        {
                if($mcldr==0)
                {
                    $fig=-($mclcr);
                }
                else
                {
                    $fig=$mcldr;   
                }   
                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'>".abs(number_format($fig,2,".",""))."</td>";
                $stot+=$fig;
                $llab +=$fig;
                for($month=$sm; $month<=$em; $month++)
                {
                    $k=$month;
                    if($month=='13')
                    {
                        $k=1;
                    }
                    if($k=='14')
                    {
                        $k=2;
                    }
                    if($month=='15')
                    {
                        $k=3;
                    }
                    //$k= sprintf("%02s", $k);
                    $j = mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where partic='$row[0]' and brcode='$brcode' and month(date)=$k" );
                    $c=mysqli_fetch_array($j);
                    $vr_total[$k]+=$c[0];
                    $gr_total[$k]+=$c[0];
                    $labour_total[$k]+=$c[0];
                    if($rowg[0]=='REVENUE FROM OPERATIONS LABOUR')
                    {
                        $lab_total[$k]+=$c[0];
                    }   
                    print "<td align='right'>".abs(number_format($c[0],2,".",""))."</td>";
                }  
                print "</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 ($mcldrtot>$mclcrtot)
{
    for($month=$sm; $month<=$em; $month++)
    {
        $k=$month;
        if($month=='13')
        {
            $k=1;
        }
        if($k=='14')
        {
            $k=2;
        }
        if($month=='15')
        {
            $k=3;
        }
        //$k= sprintf("%02s", $k);
        $np=$vr_total[$k]*-1;
 //         print " <td align='right'><font color=black><b>".number_format($np,2,".","")."</b></font></td>";     
        $gp_total[$k]=$pr;
    }
    print "</tr>";
	$mopcrtot=$mopdrtot;
}
// print "<tr><td><font color=black><b>Total2</b></font></td><td></td>";
print "<tr><td><font color=black><b>$group</b></font></td><td align='right'><font color=black><b>".abs(number_format($stot,2,".",""))."</b></font></td>";
$moth_inc = $stot;
$stot=0;
for($month=$sm; $month<=$em; $month++)
{
    $k=$month;
    if($month=='13')
    {
        $k=1;
    }
    if($k=='14')
    {
        $k=2;
    }
    if($month=='15')
    {
        $k=3;
    }
    //$k= sprintf("%02s", $k);
    print " <td align='right'><font color=black><b>".abs(number_format($gr_total[$k],2,".",""))."</b></font></td>";     
}
print "</tr>";
//end labour
//start NET LABOUR
$tot=$gp_adisc+$llab;
//print $gp_adisc;
//print $llab;
$net_pl = $mtot_gross-$llab;
$labour=$mclcrtot-$mcldrtot;
$tot_lab = abs($mtot_gross)+abs($mnet_lab)+abs($moth_inc);
$mnet_tot = $tot_lab;

print "<tr><td><font color=black><b>NET INCOME</b></font></td><td align='right'><font color=black><b>".abs(number_format($tot_lab,2,".",""))."</b></font></td>";
for($month=$sm; $month<=$em; $month++)
{
    $k=$month;
    if($month=='13')
    {
        $k=1;
    }
    if($k=='14')
    {
        $k=2;
    }
    if($month=='15')
    {
        $k=3;
    }
    //$k= sprintf("%02s", $k);
    //  $vr_total[$k]-=$cl_total[$k];
    // $vr_total[$k]+=$op_total[$k];
    $gp_total_ad[$k];
    $prr=abs($labour_total[$k])+abs($gp_total_ad[$k]);
    print " <td align='right'><font color=black><b>".abs(number_format($prr,2,".",""))."</b></font></td>";     
    $gp_total[$k]=$prr;
    if($k==5)
        print "$pl_tot[$k]=$prr";
    $pl_tot[$k]=$prr;
}
print "</tr>";
$gpp=($llab/$labsale)*100;
$gpp=number_format($gpp,2,".","");
$gpp=$gpp*-1;
//	print "<tr><td><font color=black><b>Net other income %</b></font></td><td align='right'><font color=black><b>".putComa($gpp)."</b></font></td>";
for($month=$sm; $month<=$em; $month++)
{
    $k=$month;
    if($month=='13')
    {
        $k=1;
    }
    if($k=='14')
    {
        $k=2;
    }
    if($month=='15')
    {
        $k=3;
    }
    //$k= sprintf("%02s", $k);
    $gppp=$gp_lab[$k]/$lab_total[$k]*100;
    $gppp=$gppp*-1;
    $g=$labour_total[$k]/$lab_total[$k]*100;
    //      print " <td align='right'><font color=black><b>".number_format($g,2,".","")."</b></font></td>";     
    $gp_total[$k]=$pr;
}
print "</tr>";
// end other income
$resultg = mysqli_query($db,"select gname from acgroups where gname in (select distinct grp from ledger where brcode='$brcode' ) and (nature='Expenses' or nature='income') and ghlevel>=25 order by ghlevel") or die("ERR:".mysqli_error($db));
$mgrp = '';
$jj=1;
$stot=0;
unset ($gr_total);
while($rowg=mysqli_fetch_row($resultg))
{ 
    $group=trim($rowg[0]);
    if($jj>1)
    {
        if($mgrp!=$group)
        {
            if($mgrp=='')
            {
                $mgrp='DIRECT EXPENSES';
            }
            $net_pl = $net_pl-$stot;
            if($mgrp=='DISCOUNT AND INCENTIVE (INCOME)')
            {
                $mnet_tot = $mnet_tot+abs($stot);
            }
            else
            {
                $mnet_tot = $mnet_tot-abs($stot);
            }
    	    print "<tr><td><font color=black><b>$mgrp</b></font></td><td align='right'><font color=black><b>".abs(number_format($stot,2,".",""))."</b></font></td>";
    	    $mgrp_1=$mgrp;
            $stot=0;
	        for($month=$sm; $month<=$em; $month++)
            {
                $k=$month;
                if($month=='13')
                {
                    $k=1;
                }
                if($k=='14')
                {
                    $k=2;
                }
                if($month=='15')
                {
                    $k=3;
                }
                //$k= sprintf("%02s", $k);
                $gr=$gr_total[$k];
                print " <td align='right'><font color=black><b>".abs(number_format($gr,2,".",""))."</b></font></td>";     
                
                if($mgrp_1=='DISCOUNT AND INCENTIVE (INCOME)')
                {
                    $pl_tot[$k] = $pl_tot[$k]+abs($gr);
                }
                else
                {
                    $pl_tot[$k] = $pl_tot[$k]-abs($gr);
                }   
                $mgrp=$group; 
                $gr_total[$k]=0;
            }
        }
    }
    $jj++;       
    /////print"<tr><td><b>$rowg[0]</b></td><td></td></tr>";
    $result = mysqli_query($db,"select * from ledger where grp='$rowg[0]' and brcode='$brcode' order by led") or die("ERR:".mysqli_error($db));
    while($row=mysqli_fetch_row($result))
    { 
        $groupp=trim($row[1]);
        $reop=mysqli_query($db,"select * from $tblop where partic='$row[0]' ") or die("ERR:".mysqli_error($db));
        $roop1=mysqli_fetch_row($reop);
        $re=mysqli_query($db,"select * from $tbl where partic='$row[0]' ") 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;
        $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($mcldr==0)
                {
                    $fig=-($mclcr);
                }
                else    
                {
                    $fig=$mcldr;   
                }
                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'>".abs(number_format($fig,2,".",""))."</td>";
                $stot+=$fig;
                for($month=$sm; $month<=$em; $month++)
                {
                    $k=$month;
                    if($month=='13')
                    {   
                        $k=1;
                    }
                    if($k=='14')
                    {
                        $k=2;
                    }
                    if($month=='15')
                    {
                        $k=3;
                    }
                    //$k= sprintf("%02s", $k);
                    $j = mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where partic='$row[0]' and brcode='$brcode' and month(date)=$k" );
                    $c=mysqli_fetch_array($j);
                    $vr_total[$k]+=$c[0];
                    $gr_total[$k]+=$c[0];
                    print "<td align='right'>".abs(number_format($c[0],2,".",""))."</td>";
                }  
                print "</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 ($mcldrtot>$mclcrtot)
{
    for($month=$sm; $month<=$em; $month++)
    {
        $k=$month;
        if($month=='13')
        {
            $k=1;
        }
        if($k=='14')
        {
            $k=2;
        }
        if($month=='15')
        {
            $k=3;
        }
        //$k= sprintf("%02s", $k);
        $np=$vr_total[$k]*-1;
 //         print " <td align='right'><font color=black><b>".number_format($np,2,".","")."</b></font></td>";     
          
        $gp_total[$k]=$pr;
        
    }
	print "</tr>";
	$mopcrtot=$mopdrtot;
}
print "<tr><td><font color=black><b>$group</b></font></td><td align='right'><font color=black><b>".number_format($stot,2,".","")."</b></font></td>";
$net_pl = $net_pl+$stot;
$stot=0;
for($month=$sm; $month<=$em; $month++)
{
    $k=$month;
    if($month=='13')
    {
        $k=1;
    }
    if($k=='14')
    {
        $k=2;
    }
    if($month=='15')
    {
        $k=3;
    }
    //$k= sprintf("%02s", $k);
    print " <td align='right'><font color=black><b>".abs(number_format($gr_total[$k],2,".",""))."</b></font></td>";     
}
print "</tr>";

print "<tr><td><font color=black><b>Net Profit/Loss</b></font></td><td align='right'><font color=black><b>".putComa($mnet_tot)."</b></font></td>";
for($month=$sm; $month<=$em; $month++)
{
    $k=$month;
    if($month=='13')
    {
        $k=1;
    }
    if($k=='14')
    {
        $k=2;
    }
    if($month=='15')
    {
        $k=3;
    }
    //$k= sprintf("%02s", $k);
    $np=$vr_total[$k]*-1;
    print " <td align='right'><font color=black><b>".number_format($pl_tot[$k],2,".","")."</b></font></td>";     
    $gp_total[$k]=$pr;
}
print "</tr>";
$mopdrtot=$mopcrtot;
$mcldrtot=$mclcrtot;
?>
<script>
 //window.open('pandl3.php?df=<?print $df?>&dt=<?print $dt?>');
</script>

