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

$fr=$_REQUEST['fr'];
$to=$_REQUEST['to'];
$yr=$_REQUEST['yr'];




if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
include('opendb.php');
include('test.php');
include_once("includes/function.php");
$us=mysqli_query($db,"select level from bruser where username='$user'")or die(mysqli_error($db));
$r=mysqli_fetch_row($us);
$user_level=$r[0];

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>AMPL EICHER</title>
 		<script src=" rightclick_deactive.js"></script> 
	</head>

 		<script type="text/javascript" src="scripts/jquery-latest.js"></script>
		<link type="text/css" href="includes/style1.css" rel="stylesheet" />
      		<link type="text/css" href="css/form.css" rel="stylesheet" />
		<script type="text/javascript" src="js/jquery-1.6.1.min.js"></script>

   		 <script type="text/javascript">
       		 $(function() {

          			if ($.browser.msie && $.browser.version.substr(0,1)<7)
          			{
					$('li').has('ul').mouseover(function(){
					$(this).children('ul').show();
					}).mouseout(function(){
					$(this).children('ul').hide();
					})
          			}
        		});        
    		</script>



		<script type="text/javascript">

    			$(document).ready(function() {
       		 		var cont_left = $("#container").position().left;
       		 		$("a img").hover(function() {
          
           			 	$(this).animate({
               		 	height: "110",
               	 		width: "110",
              
            				}, "fast");
                    			}, function() {
            
                    			$(this).animate({
                    			height: "80",
                    			width: "80",
                
                    			}, "fast");
        	     	 });
        
        	      	$(".img").each(function(index) {

            				var left = (index * 160) + cont_left;
            				$(this).css("left", left + "px");
        	        });

                   });


	             (function($){

    					$.fn.enterNext = function(){
       				var _i =0;
       				$('input[type=text], select',this)
            				.each(function(index){
           				 _i = index;
            			 	$(this)
                			.addClass('tab'+index)
                			.keydown(function(event){
                    			if(event.keyCode==13){

                        			$('.tab'+(index+1)).focus();
                        			event.preventDefault();
                    			}
                	  });

             		})

     			$( "input[type=submit]",this ).addClass('tab'+(_i+1));

              	}})(jQuery);

            </script>



		<style>

 			th{
 
    				font-weight: bold;  
    				color: white; 
    				border:1px solid #D7D7D1;
   			       border-collapse:collapse;
    				background: rgb(181,189,200); /* Old browsers */
    				background: -moz-linear-gradient(top, rgba(181,189,200,1) 0%, rgba(130,140,149,1) 36%, rgba(40,52,59,1) 100%); /* FF3.6+ */
    				background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(181,189,200,1)), color-stop(36%,rgba(130,140,149,1)), color-stop(100%,rgba(40,52,59,1))); /* Chrome,Safari4+ */
    				background: -webkit-linear-gradient(top, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* Chrome10+,Safari5.1+ */
    				background: -o-linear-gradient(top, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* Opera 11.10+ */
				background: -ms-linear-gradient(top, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* IE10+ */
				background: linear-gradient(to bottom, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* W3C */
				filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#b5bdc8', endColorstr='#28343b',GradientType=0 ); /* IE6-9 */
			}

			#tbl td{
				border: 1px solid black;
				border-collapse:collapse;
				border-style:dotted;
			}

			#tbl{
				border: 1px solid black;
				border-collapse:collapse;
				border-style:dotted;
			}

 			#button4
			{

			        color:red;
			}

           
			 #links{

			       color:#777777;

			}


.pop-up211 {position:absolute; top:10px; left:50px;padding-left:360px;}
.pop-up211:target {position:static; left:0;}

.popBox211 {
  background:#ffffff;

 
  width:40%;
  min-height:60%;



   z-index:1;

  border:1px solid #3a3a3a;


  -moz-border-radius:12px;
  border-radius:12px;
  -webkit-box-shadow:2px 2px 4px #3a3a3a;
  -moz-box-shadow:2px 2px 4px #3a3a3a;
  box-shadow:2px 2px 4px #3a3a3a;
  opacity:0;
 -webkit-transition: opacity 0.5s ease-in-out;
  -moz-transition: opacity 0.5s ease-in-out;
  -o-transition: opacity 0.5s ease-in-out;
  -ms-transition: opacity 0.5s ease-in-out;
  transition: opacity 0.5s ease-in-out;  
}
:target .popBox211 {position:fixed; opacity:1;}

.popBox211:hover {box-shadow:3px 3px 6px #5a5a5a;}



.lightbox11 {display:none; text-indent:0em; background:#000; opacity:0.0; width:100%; height:100%; position:fixed; top:0; left:0; bottom:0; right:0;}
:target .lightbox11 {display:block}

.popScroll211 {position:absolute; top:9%; left:3%; right:3%; bottom:9%; overflow-y:auto; *overflow-y:scroll; padding-right:0.5em}



			


		</style>

   <body background="back_ground.jpg">

    <?php 

	include('includes/top.php');

        
      include('mis_repo2.php');
     ?>

<div id="pop211" class="pop-up211" >
  <div class="popBox211">
<div class="popScroll211" id='popScroll211' >
</div>
  </div>
 <a href="#" class="lightbox11"></a>
</div>



    <table class='report'   width='100%' border='1'>
 
   <tr>
              <th colspan='2' height='35'><b>MONTHS</b></th>
		
		<th><b>APR</b></th>
		<th><b>MAY</th>
		<th><b>JUNE</th>
		<th><b>JULY</th>
		<th><b>AUG</th>
		<th><b>SEPT</th>
		<th><b>OCT</th>
		<th><b>NOV</th>
		<th><b>DEC</th>
                <th><b>JAN</th>
		<th><b>FEB</th>
		<th><b>MAR</th>
		<th><b>TOTAL</th>
         </TR>





<?


$vr_gp=array();

print "<tr><td colspan=2><font color=brown>To Opening Stock </td>";
 //$ostock=array();
for($month=$fr; $month<$to; $month++)
{
    $k=$month;
    if($month=='13')
    {
        $k=1;
    }
    if($k=='14')
    {
        $k=2;
    }
    if($month=='15')
    {
        $k=3;
    }
    $k= sprintf("%02s", $k);

    $ops=mysqli_query($db,"select opstock from closingstock where brcode='$brcode' and month='$k'");
	$stocks=mysqli_fetch_row($ops);
	$ostock=$stocks[0];
	$cls=mysqli_query($db,"select clstock from closingstock where brcode='$brcode' and month='$k'");
	$clsrow=mysqli_fetch_row($cls);
	$cstock=$clsrow[0];
   $vr_gp[$k]+=($cstock-$ostock);

  print "<td align=right><font color=brown>".putComa($ostock)."</td>";
} 
print "<td></td></tr>";
?>
<tr><td colspan=15><font color=brown>To Purchase A/c</td></tr>
<?
$sumleft=0;
$rrr=mysqli_query($db,"select * from acgroups where headgroup ='DIRECT EXPENSE' and gname like 'PURCHASE%'") or die(mysqli_error($db));
unset($vr_total);
$vr_total = array();

print $to;

while($prows=mysqli_fetch_array($rrr))
{ //while1
 	$result=mysqli_query($db,"select * from ledger where grp='$prows[gname]' and brcode='$brcode' order by led");
 
 	
 	while($row1=mysqli_fetch_array($result))
 	{
		$hr_total =0;
		print "<tr><td colspan=2><blockquote>$row1[led]</td>";
		for($month=$fr; $month<$to; $month++)
		{
            $k=$month;
            if($month=='13')
            {
                $k=1;
            }
            if($k=='14')
            {
                $k=2;
            }
            if($month=='15')
            {
                $k=3;
            }
            if($k<10)
                 $k= sprintf("%02s", $k);
            $df=$yr."-".$k."-01"; 
            $timestamp    = strtotime($df);
            $dt  = date('Y-m-t', $timestamp);  
            $sq="select * from journal2 where partic='$row1[led]' and brcode='$brcode' and date between '$df' and '$dt' ";
   			$re2=mysqli_query($db,$sq);
  			$re3=mysqli_query($db,"select * from ledger where led='$row1[led]' and brcode='$brcode'"); 
  			$opb=mysqli_fetch_array($re3);
			$tr=0; 
			$tp=0;
			if($opb[drcr]=="Dr")
			{
				$tr+=$opb[op];
			}
			else if($opb[drcr]=="Cr")
			{
				$tp+=$opb[op];
			}
			$a1=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date between '$df' and '$dt'");
			$a1row=mysqli_fetch_row($a1);
			$tr=$a1row[0];
			$tp=$a1row[1];
			$bal=$tr-$tp;
			$tot+=$bal;
 			print"<td align=right><a href='vmledgerdt.php?cd=$row1[led]& df=".$df."&dt=".$dt."' target='blank'>".putComa($bal)."</a></td>";
			$hr_total+=$bal;
            $vr_total[$k]+=$bal;
		}//end forloop
        print "<td align='right'><a href='vmledgerdt.php?cd=$row1[led]' target='blank'>".putcoma($hr_total)."</a></td></tr>";
    }
} //end while 1

	      print "<tr><td colspan=2><b>Total:</b></td>";

             for($j=$to;$j<$fr;$j++)

		{

$k=$j;

if($j=='13')
{
$k=1;
}
if($j=='14')
{
$k=2;
}
if($j=='15')
{
$k=3;
}
                     $k= sprintf("%02s", $k);
			print "<td align='right'>".putcoma($vr_total[$k])."</td>";

                     $vr_gp[$k]-=$vr_total[$k];
		}

               print "<td align='right'><b>".putcoma(array_sum($vr_total))."</b></td></tr>";
	
$sumleft+=$tot;

	//**********************888 direct expense STOCK TRANSFER IN
print "<tr><td colspan=15><font color=brown><b>Direct Expense</b></td></tr>";



$rrr=mysqli_query($db,"select * from acgroups where headgroup ='DIRECT EXPENSE' and gname ='STOCK TRANSFER IN'") or die(mysqli_error($db));
unset($vr_total);
$vr_total = array();
while($prows=mysqli_fetch_array($rrr))
{ //while1
 	$result=mysqli_query($db,"select * from ledger where grp='$prows[gname]' and brcode='$brcode' order by led");
 
 	
 	while($row1=mysqli_fetch_array($result))
 	{
		$hr_total =0;
		print "<tr><td colspan=2><blockquote>$row1[led]</td>";
		for($month=$fr; $month<$to; $month++)
		{
            $k=$month;
            if($month=='13')
            {
                $k=1;
            }
            if($k=='14')
            {
                $k=2;
            }
            if($month=='15')
            {
                $k=3;
            }
            if($k<10)
                 $k= sprintf("%02s", $k);
            $df=$yr."-".$k."-01"; 
            $timestamp    = strtotime($df);
            $dt  = date('Y-m-t', $timestamp);  
            $sq="select * from journal2 where partic='$row1[led]' and brcode='$brcode' and date between '$df' and '$dt' ";
   			$re2=mysqli_query($db,$sq);
  			$re3=mysqli_query($db,"select * from ledger where led='$row1[led]' and brcode='$brcode'"); 
  			$opb=mysqli_fetch_array($re3);
			$tr=0; 
			$tp=0;
			if($opb[drcr]=="Dr")
			{
				$tr+=$opb[op];
			}
			else if($opb[drcr]=="Cr")
			{
				$tp+=$opb[op];
			}
			$a1=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date between '$df' and '$dt'");
			$a1row=mysqli_fetch_row($a1);
			$tr=$a1row[0];
			$tp=$a1row[1];
			$bal=$tr-$tp;
			$tot+=$bal;
 			print"<td align=right><a href='vmledgerdt.php?cd=$row1[led]& df=".$df."&dt=".$dt."' target='blank'>".putComa($bal)."</a></td>";
			$hr_total+=$bal;
            $vr_total[$k]+=$bal;
		}//end forloop
        print "<td align='right'><a href='vmledgerdt.php?cd=$row1[led]' target='blank'>".putcoma($hr_total)."</a></td></tr>";
    }
} //end while 1

	      print "<tr><td colspan=2><b>Total:</b></td>";

             for($j=$to;$j<$fr;$j++)

		{

$k=$j;

if($j=='13')
{
$k=1;
}
if($j=='14')
{
$k=2;
}
if($j=='15')
{
$k=3;
}
                     $k= sprintf("%02s", $k);
			print "<td align='right'>".putcoma($vr_total[$k])."</td>";

                     $vr_gp[$k]-=$vr_total[$k];
		}

               print "<td align='right'><b>".putcoma(array_sum($vr_total))."</b></td></tr>";
	
$sumleft+=$tot;


//*********************************************************************************************//
	//**********************888 direct expense from asset/liabl
print "<tr><td colspan=15><font color=brown><b>Direct Expense</b></td></tr>";



$rrr=mysqli_query($db,"select * from acgroups where headgroup='CURRENT ASSETS' and gname='BRANCHES' order by ghlevel") or die(mysqli_error($db));
unset($vr_total);
$vr_total = array();
while($prows=mysqli_fetch_array($rrr))
{ //while1
 	$result=mysqli_query($db,"select * from ledger where grp='$prows[gname]' and brcode='$brcode' and led='PSN AMPL STOCK TRANSFER MARGIN' order by led");
 
 	
 	while($row1=mysqli_fetch_array($result))
 	{
		$hr_total =0;
		print "<tr><td colspan=2><blockquote>$row1[led]</td>";
		for($month=$fr; $month<$to; $month++)
		{
            $k=$month;
            if($month=='13')
            {
                $k=1;
            }
            if($k=='14')
            {
                $k=2;
            }
            if($month=='15')
            {
                $k=3;
            }
            if($k<10)
                 $k= sprintf("%02s", $k);
            $df=$yr."-".$k."-01"; 
            $timestamp    = strtotime($df);
            $dt  = date('Y-m-t', $timestamp);  
            $sq="select * from journal2 where partic='$row1[led]' and brcode='$brcode' and date between '$df' and '$dt' ";
   			$re2=mysqli_query($db,$sq);
  			$re3=mysqli_query($db,"select * from ledger where led='$row1[led]' and brcode='$brcode'"); 
  			$opb=mysqli_fetch_array($re3);
			$tr=0; 
			$tp=0;
			if($opb[drcr]=="Dr")
			{
				$tr+=$opb[op];
			}
			else if($opb[drcr]=="Cr")
			{
				$tp+=$opb[op];
			}
			$a1=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date between '$df' and '$dt'");
			$a1row=mysqli_fetch_row($a1);
			$tr=$a1row[0];
			$tp=$a1row[1];
			$bal=$tr-$tp;
			$tot+=$bal;
 			print"<td align=right><a href='vmledgerdt.php?cd=$row1[led]& df=".$df."&dt=".$dt."' target='blank'>".putComa($bal)."</a></td>";
			$hr_total+=$bal;
            $vr_total[$k]+=$bal;
		}//end forloop
        print "<td align='right'><a href='vmledgerdt.php?cd=$row1[led]' target='blank'>".putcoma($hr_total)."</a></td></tr>";
    }
} //end while 1

	      print "<tr><td colspan=2><b>Total:</b></td>";

             for($j=$to;$j<$fr;$j++)

		{

$k=$j;

if($j=='13')
{
$k=1;
}
if($j=='14')
{
$k=2;
}
if($j=='15')
{
$k=3;
}
                     $k= sprintf("%02s", $k);
			print "<td align='right'>".putcoma($vr_total[$k])."</td>";

                     $vr_gp[$k]-=$vr_total[$k];
		}

               print "<td align='right'><b>".putcoma(array_sum($vr_total))."</b></td></tr>";
	
$sumleft+=$tot;


//*********************************************************************************************//




print "<tr><td colspan=15><font color=brown><b>By Sales (Spares & Lubricants)</b></td></tr>";
unset($vr_total);
$vr_total = array();
$bal=0; 
$tot=0;
$sumright=0;
$rrr=mysqli_query($db,"select * from acgroups where headgroup='DIRECT INCOME' and gname='REVENUE FROM OPERATIONS SPARE AND LUB' order by ghlevel")  or die(mysqli_error($db));

while($sarow=mysqli_fetch_array($rrr))
{ // while 1
  	$sq="select * from ledger where grp='$sarow[0]' and brcode='$brcode'";
 	$result=mysqli_query($db,$sq);
 	while($row1=mysqli_fetch_array($result))
 	{
		$hr_total=0;
              print "<tr><td colspan=2><blockquote>$row1[led]</td>";

		for($month=$fr; $month<$to; $month++)
		{
$k=$month;

if($month=='13')
{
$k=1;
}
if($k=='14')
{
$k=2;
}
if($month=='15')
{
$k=3;
}
                    if($k<10)
                             $k= sprintf("%02s", $k);

                    $df=$yr."-".$k."-01"; 
                    $timestamp    = strtotime($df);
                    $dt  = date('Y-m-t', $timestamp);               
  			$sq="select * from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'";
			$tr=0;
	       	$tp=0;
   			$re2=mysqli_query($db,$sq);
  			$re3=mysqli_query($db,"select * from ledger where led='$row1[led]' and brcode='$brcode'"); 
  			$opb=mysqli_fetch_array($re3);
			$tr=0; $tp=0;
			if($opb[drcr]=="Dr")
			{
				$tr+=$opb[op];
			}
			else if($opb[drcr]=="Cr")
			{
				$tp+=$opb[op];
			}
			$saleres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'");
			$salerow=mysqli_fetch_row($saleres);
			$tr+=$salerow[0];
			$tp+=$salerow[1];
			$bal=$tr-$tp;
	//******************************88888888888888888
		$bal*=-1;
	//******************************88888888888888888

			$tot+=$bal;

 			print "<td align=right><a href='vmledgerdt.php?cd=$row1[led]& df=".$df."& dt=".$dt."' target='blank'>".putComa($bal)."</a></td>";
			
			$hr_total+=$bal;
			$vr_total[$k]+=$bal;

		}  // end for loop
		
		print "<td align='right'><a href='vmledgerdt.php?cd=$row1[led]' target='blank'>".putcoma($hr_total)."</a></td></tr>";
	}

}// end while 1

print "<tr><td colspan=2><b>Total:</b></td>";

for($j=$fr;$j<$to;$j++)

		{
		    
		     $k=$j;

if($j=='13')
{
$k=1;
}
if($j=='14')
{
$k=2;
}
if($j=='15')
{
$k=3;
} 
                     $k= sprintf("%02s", $k);

			print "<td align='right'>".putcoma($vr_total[$k])."</td>";

			$vr_gp[$k]+=$vr_total[$k];
			$sales[$k]+=$vr_total[$k];
			
		}

     print "<td align='right'><b>".putcoma(array_sum($vr_total))."</b></td></tr>";

$sumright+=$tot;

//print "<td align=right>".putComa($tot)."</td></tr>";

//=======================================================end sales parts and lub

//*********************************************SALES WARRANTY************************************************//
print "<tr><td colspan=15><font color=brown><b>By Sales (WARRANTY SPARE & LUB)</b></td></tr>";
unset($vr_total);
$vr_total = array();
$bal=0; 
$tot=0;
$sumright=0;
$rrr=mysqli_query($db,"select * from acgroups where headgroup='DIRECT INCOME' and gname='REVENUE FROM OPERATIONS WTY SPARE AND LUB' order by ghlevel")  or die(mysqli_error($db));

while($sarow=mysqli_fetch_array($rrr))
{ // while 1
  	$sq="select * from ledger where grp='$sarow[0]' and brcode='$brcode'";
 	$result=mysqli_query($db,$sq);
 	while($row1=mysqli_fetch_array($result))
 	{
		$hr_total=0;
              print "<tr><td colspan=2><blockquote>$row1[led]</td>";

		for($month=$fr; $month<$to; $month++)
		{
$k=$month;

if($month=='13')
{
$k=1;
}
if($k=='14')
{
$k=2;
}
if($month=='15')
{
$k=3;
}
                    if($k<10)
                             $k= sprintf("%02s", $k);

                    $df=$yr."-".$k."-01"; 
                    $timestamp    = strtotime($df);
                    $dt  = date('Y-m-t', $timestamp);               
  			$sq="select * from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'";
			$tr=0;
	       	$tp=0;
   			$re2=mysqli_query($db,$sq);
  			$re3=mysqli_query($db,"select * from ledger where led='$row1[led]' and brcode='$brcode'"); 
  			$opb=mysqli_fetch_array($re3);
			$tr=0; $tp=0;
			if($opb[drcr]=="Dr")
			{
				$tr+=$opb[op];
			}
			else if($opb[drcr]=="Cr")
			{
				$tp+=$opb[op];
			}
			$saleres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'");
			$salerow=mysqli_fetch_row($saleres);
			$tr+=$salerow[0];
			$tp+=$salerow[1];
			$bal=$tr-$tp;
	//******************************88888888888888888
		$bal*=-1;
	//******************************88888888888888888

			$tot+=$bal;

 			print "<td align=right><a href='vmledgerdt.php?cd=$row1[led]& df=".$df."& dt=".$dt."' target='blank'>".putComa($bal)."</a></td>";
			
			$hr_total+=$bal;
			$vr_total[$k]+=$bal;

		}  // end for loop
		
		print "<td align='right'><a href='vmledgerdt.php?cd=$row1[led]' target='blank'>".putcoma($hr_total)."</a></td></tr>";
	}

}// end while 1

print "<tr><td colspan=2><b>Total:</b></td>";

for($j=$to;$j<$fr;$j++)

		{
		    
		     $k=$j;

if($j=='13')
{
$k=1;
}
if($j=='14')
{
$k=2;
}
if($j=='15')
{
$k=3;
} 
                     $k= sprintf("%02s", $k);

			print "<td align='right'>".putcoma($vr_total[$k])."</td>";

			$vr_gp[$k]+=$vr_total[$k];
		}

     print "<td align='right'><b>".putcoma(array_sum($vr_total))."</b></td></tr>";

$sumright+=$tot;

//print "<td align=right>".putComa($tot)."</td></tr>";


//  end wty lub sales


//****************************direct income not equal to stock transfer out*****************************************************************//


print "<tr><td colspan=15><font color=brown><font color=brown><b>By Direct Income</b></td></tr>";
	unset($vr_total);
	$vr_total = array();
$bal=0; 
$tot=0;
//$sumright=0;
$rrr=mysqli_query($db,"select * from acgroups where headgroup='Direct Income'  and gname='STOCK TRANSFER OUT' order by ghlevel")  or die(mysqli_error($db));
while($dirow=mysqli_fetch_array($rrr))
{ // while 1
 	
 	$result=mysqli_query($db,"select * from ledger where grp='$dirow[0]' and brcode='$brcode'");
 	
 	while($row1=mysqli_fetch_array($result))
 	{
		$hr_total=0;
		print "<tr><td colspan=2><blockquote>$row1[led]</td>";

		for($month=$fr; $month<$to; $month++)
		{

$k=$month;

if($month=='13')
{
$k=1;
}
if($k=='14')
{
$k=2;
}
if($month=='15')
{
$k=3;
}
                    if($k<10)
                             $k= sprintf("%02s", $k);
			$tr=0;
	       	$tp=0;
                    $df=$yr."-".$k."-01"; 
                    $timestamp    = strtotime($df);
                    $dt  = date('Y-m-t', $timestamp);
  			$sq="select * from journal2 where partic='$row1[led]'and brcode='$brcode' and date>='$df' and date<='$dt'";
   			$re2=mysqli_query($db,$sq);
  			$re3=mysqli_query($db,"select * from ledger where led='$row1[led]'"); 
  			$opb=mysqli_fetch_array($re3);
			$tr=0; $tp=0;
			if($opb[drcr]=="Dr")
			{
				$tr+=$opb[op];
			}
			else if($opb[drcr]=="Cr")
			{
				$tp+=$opb[op];
			}

			$dinres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'");
			$drinrow=mysqli_fetch_row($dinres);
			$tr+=$drinrow[0];
			$tp+=$drinrow[1];

			$bal=$tr-$tp;


		//**************************8888888
			$bal=$bal*-1;
		//**************************8888888

			$tot+=$bal;

	
 			print "<td align=right><a href='vmledgerdt.php?cd=$row1[led]& df=".$df."& dt=".$dt."' target='blank'>".putComa($bal)."</a></td>";

			$hr_total+=$bal;
			$vr_total[$k]+=$bal;


		}//end of for loop

		print "<td align='right'><a href='vmledgerdt.php?cd=$row1[led]' target='blank'>".putcoma($hr_total)."</a></td></tr>";
	}
} // end while 1

print "<tr><td colspan=2><b>Total:</b></td>";
             for($j=1;$j<13;$j++)

		{
                     $j= sprintf("%02s", $j);
			print "<td align='right'>".putcoma($vr_total[$j])."</td>";
			$vr_gp[$j]+=$vr_total[$j];
		}

    print "<td align='right'><b>".putcoma(array_sum($vr_total))."</b></td></tr>";


$sumright+=$tot; 
//$sumright=abs($sumright)+$cstock;           // CHECK


	// minus sales returns also
$sumright=$sumright+$cstock+$sreturn_value;  
         
//print "<td align=right>".putComa($tot)."</td></tr>";

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


//=======================================================closing stock================
print "<tr><td colspan=2><font color=brown><b>By Closing Stock</td>";

 for($month=$fr; $month<$to; $month++)
{
$k=$month;

if($month=='13')
{
$k=1;
}
if($k=='14')
{
$k=2;
}
if($month=='15')
{
$k=3;
}


	 $cls=mysqli_query($db,"select clstock from closingstock where brcode='$brcode' and month='$k'");
	$clsrow=mysqli_fetch_row($cls);
	$cstock=$clsrow[0]; 

print "<td align=right>".putComa($cstock)."</td>";

} 
print "<td></td></tr>";



$hr_total =0;

//-------------colse closing==========

/*************   code below for getin    Working Progress  *********************/


print "<tr><td colspan=2><font color=brown><b>By Working Progress</b></font></td>";



//---
	unset($vr_total);
	$vr_total = array();
       $hr_total=0;
for($month=$fr; $month<$to; $month++)
{
  
$k=$month;

if($month=='13')
{
$k=1;
}
if($k=='14')
{
$k=2;
}
if($month=='15')
{
$k=3;
}
  if($k<10)
             $k= sprintf("%02s", $k);

    $df=$yr."-".$k."-01"; 
    $timestamp    = strtotime($df);
    $dt  = date('Y-m-t', $timestamp); 

    	$sql=mysqli_query($db,"select wip from closingstock where brcode='$brcode' and month='$k'");
	$row=mysqli_fetch_row($sql);	
       $vr_total[$k]=$row[0];
	print "<td align='right'>".putcoma($vr_total[$k])."</td>";
       $hr_total+=$row[0];

	$vr_gp[$k]+=$row[0];
	

	
	
	
}            
 
print "<td align='right'><b>".putcoma($hr_total)."</b></td></tr>";




//print "<tr><td colspan=3 align=right><b><u>".putComa($sumright)."</td>";

//xxxxxxxxxxxxxxxxxxxxxxxxxxx//wip close

// gp start===========


$diff=array_sum($vr_gp);

//$sumright-$sumleft;             // CHECK




if($diff>=0)
{
	$trading_gross_profit=$diff;
 	print "<tr><td colspan=2><font color=green>To Gross Profit</td>";
       for($j=4; $j<16; $j++)
	{
	    
	   $j= sprintf("%02s", $j);
          print "<td align=right><font color=green><b>".putComa($vr_gp[$j])."</b></td>";

         $profit[$j]+=$vr_gp[$j];
	}
	print "<td align=right><font color=green><b>".putComa($diff)."</td></tr>";

	

	//print "<tr><td colspan=15 align=right><b><u>".putComa($sumright)."</tr>";
}
else
{
	$trading_gross_loss=$sumleft;
	 print "<tr><td colspan=2><font color=brown>By Gross Loss</td>";

       for($j=4; $j<16; $j++)
	{
	   $j= sprintf("%02s", $j);
          print "<td align=right><b><font color=green>".putComa($vr_gp[$j])."</b></td>";
          $profit[$j]+=$vr_gp[$j];
	    
	}

	 $sumright=$gross_gp;
	 print "<td align=right><font color=red><b>".putComa(abs($diff))."</td></tr>";
	 //print "<tr><td colspan=15 align=right><font color=black>".putComa($sumleft)."</td></tr>";
	 
	 
}
?>


<hr>


<?if($diff<0)
{
	$grossloss=$diff;
?>
		<tr><td colspan=14>By Gross Loss b/d</td><td align=right><?print putComa(abs($diff))?></td></tr>
<?
}


	
 	print "<tr><td colspan=2><font color=green>Margin</td>";
       for($j=4; $j<16; $j++)
	{
	    
	   $j= sprintf("%02s", $j);
	   
	    $xy=$profit[$j]/$sales[$j]*100;
	    $xy=number_format($xy,2,'.','');
	    $ay+=$xy;
	    
          print "<td align=right><font color=green><b>".putComa($xy)."</b></td>";

       
	}
	$ay=$ay/3;
	print "<td align=right><font color=green><b>".putComa($ay)."</td></tr>";

	

	


	//**********************888 direct expense others
print "<tr><td colspan=15><font color=brown><b>Direct Expense</b></td></tr>";



$rrr=mysqli_query($db,"select * from acgroups where headgroup ='DIRECT EXPENSE' and gname not like 'PURCHASE%'  and gname !='STOCK TRANSFER IN'") or die(mysqli_error($db));
unset($vr_total);
$vr_total = array();
while($prows=mysqli_fetch_array($rrr))
{ //while1
 	$result=mysqli_query($db,"select * from ledger where grp='$prows[gname]' and brcode='$brcode' order by led");
 
 	
 	while($row1=mysqli_fetch_array($result))
 	{
		$hr_total =0;
		print "<tr><td colspan=2><blockquote>$row1[led]</td>";
		for($month=$fr; $month<$to; $month++)
		{
            $k=$month;
            if($month=='13')
            {
                $k=1;
            }
            if($k=='14')
            {
                $k=2;
            }
            if($month=='15')
            {
                $k=3;
            }
            if($k<10)
                 $k= sprintf("%02s", $k);
            $df=$yr."-".$k."-01"; 
            $timestamp    = strtotime($df);
            $dt  = date('Y-m-t', $timestamp);  
            $sq="select * from journal2 where partic='$row1[led]' and brcode='$brcode' and date between '$df' and '$dt' ";
   			$re2=mysqli_query($db,$sq);
  			$re3=mysqli_query($db,"select * from ledger where led='$row1[led]' and brcode='$brcode'"); 
  			$opb=mysqli_fetch_array($re3);
			$tr=0; 
			$tp=0;
			if($opb[drcr]=="Dr")
			{
				$tr+=$opb[op];
			}
			else if($opb[drcr]=="Cr")
			{
				$tp+=$opb[op];
			}
			$a1=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date between '$df' and '$dt'");
			$a1row=mysqli_fetch_row($a1);
			$tr=$a1row[0];
			$tp=$a1row[1];
			$bal=$tr-$tp;
			$tot+=$bal;
 			print"<td align=right><a href='vmledgerdt.php?cd=$row1[led]& df=".$df."&dt=".$dt."' target='blank'>".putComa($bal)."</a></td>";
			$hr_total+=$bal;
            $vr_total[$k]+=$bal;
		}//end forloop
        print "<td align='right'><a href='vmledgerdt.php?cd=$row1[led]' target='blank'>".putcoma($hr_total)."</a></td></tr>";
    }
} //end while 1

	      print "<tr><td colspan=2><b>Total:</b></td>";

             for($j=$fr;$j<$to;$j++)

		{

$k=$j;

if($j=='13')
{
$k=1;
}
if($j=='14')
{
$k=2;
}
if($j=='15')
{
$k=3;
}
                     $k= sprintf("%02s", $k);
			print "<td align='right'>".putcoma($vr_total[$k])."</td>";

                     $vr_gp[$k]-=$vr_total[$k];
		}

               print "<td align='right'><b>".putcoma(array_sum($vr_total))."</b></td></tr>";
	
$sumleft+=$tot;

//---------------end===============




//*********************************************SALES LABOUR************************************************//
print "<tr><td colspan=15><font color=brown><b>By Sales (labour)</b></td></tr>";
unset($vr_total);
$vr_total = array();
$bal=0; 
$tot=0;
$sumright=0;
$rrr=mysqli_query($db,"select * from acgroups where headgroup='DIRECT INCOME' and gname='REVENUE FROM OPERATIONS LABOUR' order by ghlevel")  or die(mysqli_error($db));

while($sarow=mysqli_fetch_array($rrr))
{ // while 1
  	$sq="select * from ledger where grp='$sarow[0]' and brcode='$brcode'";
 	$result=mysqli_query($db,$sq);
 	while($row1=mysqli_fetch_array($result))
 	{
		$hr_total=0;
              print "<tr><td colspan=2><blockquote>$row1[led]</td>";

		for($month=$fr; $month<$to; $month++)
		{
$k=$month;

if($month=='13')
{
$k=1;
}
if($k=='14')
{
$k=2;
}
if($month=='15')
{
$k=3;
}
                    if($k<10)
                             $k= sprintf("%02s", $k);

                    $df=$yr."-".$k."-01"; 
                    $timestamp    = strtotime($df);
                    $dt  = date('Y-m-t', $timestamp);               
  			$sq="select * from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'";
			$tr=0;
	       	$tp=0;
   			$re2=mysqli_query($db,$sq);
  			$re3=mysqli_query($db,"select * from ledger where led='$row1[led]' and brcode='$brcode'"); 
  			$opb=mysqli_fetch_array($re3);
			$tr=0; $tp=0;
			if($opb[drcr]=="Dr")
			{
				$tr+=$opb[op];
			}
			else if($opb[drcr]=="Cr")
			{
				$tp+=$opb[op];
			}
			$saleres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'");
			$salerow=mysqli_fetch_row($saleres);
			$tr+=$salerow[0];
			$tp+=$salerow[1];
			$bal=$tr-$tp;
	//******************************88888888888888888
		$bal*=-1;
	//******************************88888888888888888

			$tot+=$bal;

 			print "<td align=right><a href='vmledgerdt.php?cd=$row1[led]& df=".$df."& dt=".$dt."' target='blank'>".putComa($bal)."</a></td>";
			
			$hr_total+=$bal;
			$vr_total[$k]+=$bal;

		}  // end for loop
		
		print "<td align='right'><a href='vmledgerdt.php?cd=$row1[led]' target='blank'>".putcoma($hr_total)."</a></td></tr>";
	}

}// end while 1

print "<tr><td colspan=2><b>Total:</b></td>";

for($j=$fr;$j<$to;$j++)

		{
		    
		     $k=$j;

if($j=='13')
{
$k=1;
}
if($j=='14')
{
$k=2;
}
if($j=='15')
{
$k=3;
} 
                     $k= sprintf("%02s", $k);

			print "<td align='right'>".putcoma($vr_total[$k])."</td>";

			$vr_gp[$k]+=$vr_total[$k];
		}

     print "<td align='right'><b>".putcoma(array_sum($vr_total))."</b></td></tr>";

$sumright+=$tot;

//print "<td align=right>".putComa($tot)."</td></tr>";


//  end labour sales


// start from assets/labi


//*********************************************From Liabliyt ************************************************//
print "<tr><td colspan=15><font color=brown><b>By Sales (PSN AMPL LABOUR CHARGE)</b></td></tr>";
unset($vr_total);
$vr_total = array();
$bal=0; 
$tot=0;
$sumright=0;
$rrr=mysqli_query($db,"select * from acgroups where headgroup='CURRENT ASSETS' and gname='BRANCHES' order by ghlevel")  or die(mysqli_error($db));

while($sarow=mysqli_fetch_array($rrr))
{ // while 1
 	$sq="select * from ledger where grp='$sarow[0]' and brcode='$brcode' and led='PSN AMPL LABOUR CHARGE'";
 	$result=mysqli_query($db,$sq);
 	while($row1=mysqli_fetch_array($result))
 	{
		$hr_total=0;
              print "<tr><td colspan=2><blockquote>$row1[led]</td>";

		for($month=$fr; $month<$to; $month++)
		{
$k=$month;

if($month=='13')
{
$k=1;
}
if($k=='14')
{
$k=2;
}
if($month=='15')
{
$k=3;
}
                    if($k<10)
                             $k= sprintf("%02s", $k);

                    $df=$yr."-".$k."-01"; 
                    $timestamp    = strtotime($df);
                    $dt  = date('Y-m-t', $timestamp);               
  			$sq="select * from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'";
			$tr=0;
	       	$tp=0;
   			$re2=mysqli_query($db,$sq);
  			$re3=mysqli_query($db,"select * from ledger where led='$row1[led]' and brcode='$brcode'"); 
  			$opb=mysqli_fetch_array($re3);
			$tr=0; $tp=0;
			if($opb[drcr]=="Dr")
			{
				$tr+=$opb[op];
				$tr=0;
			}
			else if($opb[drcr]=="Cr")
			{
				$tp+=$opb[op];
				$tp=0;
			}
			$saleres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'");
			$salerow=mysqli_fetch_row($saleres);
			$tr+=$salerow[0];
			$tp+=$salerow[1];
			$bal=$tr-$tp;
	//******************************88888888888888888
		$bal*=-1;
	//******************************88888888888888888

			$tot+=$bal;

 			print "<td align=right><a href='vmledgerdt.php?cd=$row1[led]& df=".$df."& dt=".$dt."' target='blank'>".putComa($bal)."</a></td>";
			
			$hr_total+=$bal;
			$vr_total[$k]+=$bal;

		}  // end for loop
		
		print "<td align='right'><a href='vmledgerdt.php?cd=$row1[led]' target='blank'>".putcoma($hr_total)."</a></td></tr>";
	}

}// end while 1

print "<tr><td colspan=2><b>Total:</b></td>";

for($j=$fr;$j<$to;$j++)

		{
		    
		     $k=$j;

if($j=='13')
{
$k=1;
}
if($j=='14')
{
$k=2;
}
if($j=='15')
{
$k=3;
} 
                     $k= sprintf("%02s", $k);

			print "<td align='right'>".putcoma($vr_total[$k])."</td>";

			$vr_gp[$k]+=$vr_total[$k];
		}

     print "<td align='right'><b>".putcoma(array_sum($vr_total))."</b></td></tr>";

$sumright+=$tot;

//print "<td align=right>".putComa($tot)."</td></tr>";


// end asset /liab

		//********************* SALES RETURNS
print "<tr><td colspan=15><font color=brown><b>Sales Returns</b></td></tr>";
	unset($vr_total);
	$vr_total = array();


$acg_pret=mysqli_query($db,"select gname from acgroups where headgroup='sales returns' and gname!='-' and brcode='$brcode'");
while($acg_pret_row=mysqli_fetch_row($acg_pret))
{
	$sq1="select * from ledger where grp='$acg_pret_row[0]' and brcode='$brcode'";
	print $sq1.'<br>';
	$pret_led=mysqli_query($db,$sq1) or die(mysqli_error($db)); 
 	 while($pret_led_row=mysqli_fetch_row($pret_led))
  	 {
		$hr_total=0;
		print "<tr><td colspan=2><blockquote>$pret_led_row[0]</td>";

		for($month=$fr; $month<$to; $month++)
		{
$k=$month;

if($month=='13')
{
$k=1;
}
if($k=='14')
{
$k=2;
}
if($month=='15')
{
$k=3;
}
                    if($k<10)
                             $k= sprintf("%02s", $month);

                    $df=$yr."-".$k."-01"; 
                    $timestamp    = strtotime($df);
                    $dt  = date('Y-m-t', $timestamp);

			$amt_ret=mysqli_query($db,"select sum(cramt) from journal2 where partic='$pret_led_row[0]' and brcode='$brcode' and date>='$df' and date<='$dt'") or die(mysqli_error($db));
     			$amt_ret_row=mysqli_fetch_row($amt_ret);
     			$sreturn_value=$amt_ret_row[0];
			$sreturn_value*=-1;

	              print "<td align=right><a href='vmledgerdt.php?cd=$pret_led_row[0]& df=".$df."& dt=".$dt."' target='blank'>".putComa($sreturn_value)."</a></td>";

			$hr_total+=$sreturn_value;
			$vr_total[$month]+=$sreturn_value;

 		}//end for loop

		print "<td align='right'><a href='vmledgerdt.php?cd=$pret_led_row[0]' target='blank'>".putcoma($hr_total)."</a></td></tr>";

 	 }
}

print "<tr><td colspan=2><b>Total:</b></td>";
             for($j=1;$j<13;$j++)

		{
		    
		    
		    
                     $j= sprintf("%02s", $j);

			print "<td align='right'>".putcoma($vr_total[$j])."</td>";

			$vr_gp[$j]+=$vr_total[$j];
		}

                 print "<td align='right'><b>".putcoma(array_sum($vr_total))."</b></td></tr>";


//print "<tr><td colspan=3 align=right>".putComa($sumright)."</td>";






//============end ledger from asset/laiblity===============
//****************************direct income not equal to stock transfer out*****************************************************************//


print "<tr><td colspan=15><font color=brown><font color=brown><b>By Direct Income</b></td></tr>";
	unset($vr_total);
	$vr_total = array();
$bal=0; 
$tot=0;
//$sumright=0;
$rrr=mysqli_query($db,"select * from acgroups where headgroup='Direct Income'  and gname!='REVENUE FROM OPERATIONS SPARE AND LUB' and gname!='REVENUE FROM OPERATIONS LABOUR' and  gname!='STOCK TRANSFER OUT' and gname!='REVENUE FROM OPERATIONS WTY SPARE AND LUB' order by ghlevel")  or die(mysqli_error($db));
while($dirow=mysqli_fetch_array($rrr))
{ // while 1
 	
 	$result=mysqli_query($db,"select * from ledger where grp='$dirow[0]' and brcode='$brcode'");
 	
 	while($row1=mysqli_fetch_array($result))
 	{
		$hr_total=0;
		print "<tr><td colspan=2><blockquote>$row1[led]</td>";

		for($month=$fr; $month<$to; $month++)
		{

$k=$month;

if($month=='13')
{
$k=1;
}
if($k=='14')
{
$k=2;
}
if($month=='15')
{
$k=3;
}
                    if($k<10)
                             $k= sprintf("%02s", $k);
			$tr=0;
	       	$tp=0;
                    $df=$yr."-".$k."-01"; 
                    $timestamp    = strtotime($df);
                    $dt  = date('Y-m-t', $timestamp);
  			$sq="select * from journal2 where partic='$row1[led]'and brcode='$brcode' and date>='$df' and date<='$dt'";
   			$re2=mysqli_query($db,$sq);
  			$re3=mysqli_query($db,"select * from ledger where led='$row1[led]'"); 
  			$opb=mysqli_fetch_array($re3);
			$tr=0; $tp=0;
			if($opb[drcr]=="Dr")
			{
				$tr+=$opb[op];
			}
			else if($opb[drcr]=="Cr")
			{
				$tp+=$opb[op];
			}

			$dinres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'");
			$drinrow=mysqli_fetch_row($dinres);
			$tr+=$drinrow[0];
			$tp+=$drinrow[1];

			$bal=$tr-$tp;


		//**************************8888888
			$bal=$bal*-1;
		//**************************8888888

			$tot+=$bal;

	
 			print "<td align=right><a href='vmledgerdt.php?cd=$row1[led]& df=".$df."& dt=".$dt."' target='blank'>".putComa($bal)."</a></td>";

			$hr_total+=$bal;
			$vr_total[$k]+=$bal;


		}//end of for loop

		print "<td align='right'><a href='vmledgerdt.php?cd=$row1[led]' target='blank'>".putcoma($hr_total)."</a></td></tr>";
	}
} // end while 1

print "<tr><td colspan=2><b>Total:</b></td>";
             for($j=1;$j<13;$j++)

		{
                     $j= sprintf("%02s", $j);
			print "<td align='right'>".putcoma($vr_total[$j])."</td>";
			$vr_gp[$j]+=$vr_total[$j];
		}

    print "<td align='right'><b>".putcoma(array_sum($vr_total))."</b></td></tr>";


$sumright+=$tot; 
//$sumright=abs($sumright)+$cstock;           // CHECK


	// minus sales returns also
$sumright=$sumright+$cstock+$sreturn_value;  
         
//print "<td align=right>".putComa($tot)."</td></tr>";

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^END ^





// gp start===========


$diff=array_sum($vr_gp);

//$sumright-$sumleft;             // CHECK




if($diff>=0)
{
	$trading_gross_profit=$diff;
 	print "<tr><td colspan=2><font color=green>To Gross Pprofit</td>";
       for($j=4; $j<16; $j++)
	{
	    
	   $j= sprintf("%02s", $j);
          print "<td align=right><font color=green><b>".putComa($vr_gp[$j])."</b></td>";

         
	}
	print "<td align=right><font color=green><b>".putComa($diff)."</td></tr>";

	

	//print "<tr><td colspan=15 align=right><b><u>".putComa($sumright)."</tr>";
}
else
{
	$trading_gross_loss=$sumleft;
	 print "<tr><td colspan=2><font color=brown>By Gross Lloss</td>";

       for($j=4; $j<16; $j++)
	{
	   $j= sprintf("%02s", $j);
          print "<td align=right><b><font color=green>".putComa($vr_gp[$j])."</b></td>";
	    
	}

	 $sumright=$gross_gp;
	 print "<td align=right><font color=red><b>".putComa(abs($diff))."</td></tr>";
	 //print "<tr><td colspan=15 align=right><font color=black>".putComa($sumleft)."</td></tr>";
}
?>


<hr>


<?if($diff<0)
{
	$grossloss=$diff;
?>
		<tr><td colspan=14>By Gross Loss b/d</td><td align=right><?print putComa(abs($diff))?></td></tr>
<?
}
?>

<tr><td colspan=15 width=100%><font color=brown><b>To Indirect Expenses</b></td></tr>
<?
$bal=0; $tot=0;
//$sumright=0;
$sumleft2=0;
	unset($vr_total);
	$vr_total = array();
	
//********************************* all other expenses are treated as indirect expenses **//
$rrr=mysqli_query($db,"select * from acgroups where headgroup='Indirect Expense' order by ghlevel")  or die(mysqli_error($db));
while($indirow=mysqli_fetch_array($rrr))
	{ // while 1

 	$result=mysqli_query($db,"select * from ledger where grp='$indirow[0]' and brcode='$brcode' order by led");
 	while($row1=mysqli_fetch_array($result))
 	{
		$hr_total=0;
		
  		$re3=mysqli_query($db,"select * from ledger where led='$row1[led]' and brcode='$brcode'"); 
  		$opb=mysqli_fetch_array($re3);

		if($opb[drcr]=="Dr")
		{
			$tr+=$opb[op];
		}
		else if($opb[drcr]=="Cr")
		{
			$tp+=$opb[op];
		}

// sum

		print "<tr><td colspan=2><blockquote>$row1[led]</td>";

		for($month=$fr; $month<$to; $month++)
		{
$k=$month;

if($month=='13')
{
$k=1;
}
if($k=='14')
{
$k=2;
}
if($month=='15')
{
$k=3;
}

                    $tr=0;
			$tp=0;
                    if($k<10)
                             $k= sprintf("%02s", $k);

                    $df=$yr."-".$k."-01"; 
                    $timestamp    = strtotime($df);
                    $dt  = date('Y-m-t', $timestamp);              
			$indexres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'");
			$indrow=mysqli_fetch_row($indexres);
			$tr+=$indrow[0];
			$tp+=$indrow[1];

			//********

			$bal=$tr-$tp;
			$tot+=$bal;
			//if($bal!=0)
 				print "<td align=right><a href='vmledgerdt.php?cd=$row1[led]& df=".$df."& dt=".$dt."' target='blank'>".putComa($bal)."</a></td>";

			$hr_total+=$bal;
			$vr_total[$k]+=$bal;
       $pl_total[$k]+=$bal;
		}

		print "<td align='right'><a href='vmledgerdt.php?cd=$row1[led]' target='blank'>".putcoma($hr_total)."</a></td></tr>";

	}
} // end while 1

 print "<tr><td colspan=2><b>Total:</b></td>";
             for($j=$fr;$j<$to;$j++)

		{
$k=$j;

if($j=='13')
{
$k=1;
}
if($j=='14')
{
$k=2;
}
if($j=='15')
{
$k=3;
}
                     $k= sprintf("%02s", $k);
			print "<td align='right'>".putcoma($vr_total[$k])."</td>";
		}

                 print "<td align='right'><b>".putcoma(array_sum($vr_total))."</b></td></tr>";
	
$sumleft2+=$tot;
//print "<tr><td colspan=2>></td><td align=right >".putComa($tot)."</td></tr>";
?>
<tr><td colspan=15 width=100%><font color=brown><b>To Indirect Expenses other</b></td></tr>
<?

// asset start
//********************************* all from asset **//
$rrr=mysqli_query($db,"select * from acgroups where headgroup='CURRENT ASSETS' and gname='BRANCHES' order by ghlevel")  or die(mysqli_error($db));
while($indirow=mysqli_fetch_array($rrr))
	{ // while 1

 	$result=mysqli_query($db,"select * from ledger where grp='$indirow[0]' and brcode='$brcode' and led='PSN AMPL HO EXPENSES' order by led");
 	while($row1=mysqli_fetch_array($result))
 	{
		$hr_total=0;
		
  		$re3=mysqli_query($db,"select * from ledger where led='$row1[led]' and brcode='$brcode'"); 
  		$opb=mysqli_fetch_array($re3);

		if($opb[drcr]=="Dr")
		{
			$tr+=$opb[op];
		}
		else if($opb[drcr]=="Cr")
		{
			$tp+=$opb[op];
		}

// sum

		print "<tr><td colspan=2><blockquote>$row1[led]</td>";

		for($month=$fr; $month<$to; $month++)
		{
$k=$month;

if($month=='13')
{
$k=1;
}
if($k=='14')
{
$k=2;
}
if($month=='15')
{
$k=3;
}

                    $tr=0;
			$tp=0;
                    if($k<10)
                             $k= sprintf("%02s", $k);

                    $df=$yr."-".$k."-01"; 
                    $timestamp    = strtotime($df);
                    $dt  = date('Y-m-t', $timestamp);              
			$indexres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'");
			$indrow=mysqli_fetch_row($indexres);
			$tr+=$indrow[0];
			$tp+=$indrow[1];

			//********

			$bal=$tr-$tp;
			$tot+=$bal;
			//if($bal!=0)
 				print "<td align=right><a href='vmledgerdt.php?cd=$row1[led]& df=".$df."& dt=".$dt."' target='blank'>".putComa($bal)."</a></td>";

			$hr_total+=$bal;
			$vr_total[$k]+=$bal;
       $pl_total[$k]+=$bal;
		}

		print "<td align='right'><a href='vmledgerdt.php?cd=$row1[led]' target='blank'>".putcoma($hr_total)."</a></td></tr>";

	}
} // end while 1

 print "<tr><td colspan=2><b>Total:</b></td>";
             for($j=$fr;$j<$to;$j++)

		{
$k=$j;

if($j=='13')
{
$k=1;
}
if($j=='14')
{
$k=2;
}
if($j=='15')
{
$k=3;
}
                     $k= sprintf("%02s", $k);
			print "<td align='right'>".putcoma($vr_total[$k])."</td>";
				$vr_gp[$k]-=$vr_total[$k];
		}

                 print "<td align='right'><b>".putcoma(array_sum($vr_total))."</b></td></tr>";
	

$sumleft2+=$tot;

//------asset end
$sumleft2=$tot+abs($grossloss);
	
	//*8888888888888
		$ind_exp_tot=$sumleft2;
	//*8888888888888

?>


<!-- *************right indirect income-->



<!--<tr><td>-->


<?
$diff=array_sum($vr_gp);
if($diff>0)


if($diff>=0)
{
	$trading_gross_profit=$diff;
 	print "<tr><td colspan=2><font color=green>To Gross Pprofittt</td>";
       for($j=4; $j<16; $j++)
	{
	    
	   $j= sprintf("%02s", $j);
          print "<td align=right><font color=green><b>".putComa($vr_gp[$j])."</b></td>";

         
	}
	print "<td align=right><font color=green><b>".putComa($diff)."</td></tr>";

	

	//print "<tr><td colspan=15 align=right><b><u>".putComa($sumright)."</tr>";
}

{
$grossprofit=$diff;
?>
<tr><td colspan=14><font color=green><b>By Gross Profitt b/d</br></td><td align=right><font color=green><?print putComa($diff)?></td></tr>
<?
$gp=$diff;
}
?>

<tr><td colspan=15><font color=brown><b>By Indirect Income</td></tr>
<?
$bal=0; $tot=0;
$sumright=0;
$sumright+=$diff;

	unset($vr_total);
	$vr_total = array();
$rrr=mysqli_query($db,"select * from acgroups where headgroup='Indirect Income' order by ghlevel")  or die(mysqli_error($db));
while($indincrow=mysqli_fetch_array($rrr))
{ // while 1
 	$result=mysqli_query($db,"select * from ledger where grp='$indincrow[0]' and brcode='$brcode' order by led");

	 while($row1=mysqli_fetch_array($result))
 	{

		$hr_total=0;

  		$re3=mysqli_query($db,"select * from ledger where led='$row1[led]'"); 
  		$opb=mysqli_fetch_array($re3);
		$tr=0; $tp=0;
		if($opb[drcr]=="Dr")
		{
			$tr+=$opb[op];
		}
		else if($opb[drcr]=="Cr")
		{
			$tp+=$opb[op];
		}

//*** sum

		print "<tr><td colspan=2><blockquote>$row1[led]</td>";

		for($month=$fr; $month<$to; $month++)
		{
$k=$month;

if($month=='13')
{
$k=1;
}
if($k=='14')
{
$k=2;
}
if($month=='15')
{
$k=3;
}
                    if($k<10)
                             $k= sprintf("%02s", $k);

                    $df=$yr."-".$k."-01"; 
                    $timestamp    = strtotime($df);
                    $dt  = date('Y-m-t', $timestamp);
		       $tr=0;
			 $tp=0;
			$indincres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'");
			$indrow=mysqli_fetch_row($indincres);
			$tr+=$indrow[0];
			$tp+=$indrow[1];


			//*****************
			$bal=$tr-$tp;

			//*******************888
			 $bal=$bal*-1;

			$tot=$tot+$bal;
			//if($bal!=0)
				 print "<td align=right><a href='vmledgerdt.php?cd=$row1[led]& df=".$df."& dt=".$dt."' target='blank'>".putComa($bal)."</a></td>";

			$hr_total+=$bal;
			 $vr_total[$k]+=$bal;
		}

		print "<td align='right'><a href='vmledgerdt.php?cd=$row1[led]' target='blank'>".putcoma($hr_total)."</a></td></tr>";

	}
} // end while 1

 print "<tr><td colspan=2><b>Total:</b></td>";
             for($j=$fr;$j<$to;$j++)
{
$k=$j;

if($j=='13')
{
$k=1;
}
if($j=='14')
{
$k=2;
}
if($j=='15')
{
$k=3;
}

		
                     $k= sprintf("%02s", $k);
			print "<td align='right'>".putcoma($vr_total[$k])."</td>";
			$vr_gp[$k]+=$vr_total[$k];
		}

                 print "<td align='right'><b>".putcoma(array_sum($vr_total))."</b></td></tr>";

 print "<tr><td colspan=2><font color=green><b>Profit/Loss:</b></td>";
             for($j=$fr;$j<$to;$j++)
{
$k=$j;

if($j=='13')
{
$k=1;
}
if($j=='14')
{
$k=2;
}
if($j=='15')
{
$k=3;
}

		
                     $k= sprintf("%02s", $k);
			print "<td align='right'><font color=green>".putcoma($vr_gp[$k])."</td>";
		}

               //  print "<td align='right'><font color=green><b>".putcoma(array_sum($vr_total)-array_sum($pl_total))."</b></td></tr>";
     print "<td align='right'><font color=green><b>".putcoma(array_sum($vr_gp))."</b></td></tr>";






$gtot=$tot+$grossprofit;

		$sumright=$sumright+$tot;
$sumright+=$grossprofit;




//print "<td align=right>".putComa($tot)."</td></tr>";
//print "<tr><td colspan=3 align=right><b><u>".putComa($gtot)."</td></tr></table>";
	//*8888888888888
	$pl_credit_tot=$tot;
	//*8888888888888



if($sumleft2>$gtot)//summright)
{

	$porloss=($sumleft2-$gtot);//+abs($diff));
     
	$gtot=$sumleft2;
//       Print "<tr><td colspan=15 align=right>".putComa($gtot)."</td></tr>";
//	print "<tr><td colspan=14><font color=darkbrown><b>Net Loss</td><td align=right><font color=darkbrown>".putComa($porloss)."</td></tr>";


}
else 
{
	
	$porloss=($gtot-$sumleft2);//+abs($diff));

	//Print "<tr><td colspan=15 align=right><b><u>".putComa($gtot)."</td></tr>";
	//print "<tr><td colspan=14><font color=green><b>Net Profit</td><td align=right><font color=green>".putComa($porloss)."</td></tr>";


}

//mysqli_query($db,"update pl set npl='$porloss'") or die(mysqli_error($db));
?>


</table>
<?
/*
print '<br>trading_g_profit='.$trading_gross_profit;
print '<br>trading_g_loss='.$trading_gross_loss;

print '<br>p&l credit='.$pl_credit_tot;
print '<br>ind exp tot='.$ind_exp_tot;

$tot1=$trading_gross_loss+$ind_exp_tot;
$tot2=$trading_gross_profit+$pl_credit_tot;

  $tot1=number_format($tot1,2,'','');
  $tot2=number_format($tot2,2,'','');


if($tot1<=$tot2)
{
print "<br>tot1=". $tot1." tot 2=".$tot2;
  $netprofit=($tot2-$tot1);
  $netprofit=number_format($netprofit,2,'','');
 print "<b>Net Profit=".$netprofit;
}
else
{
  $netloss=($tot1-$tot2);
  $netloss=number_format($netloss,2,'','');
 print "<b>Net Loss=".$netloss;
}


//print "NPL=$porloss";*/
?>                 

        


