<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); parent.document.location='ind.php' </script>";
  return;
}
include_once('opendb.php');
?>
<link rel="stylesheet" href="style.css" type="text/css" />
<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" />
<link rel="shortcut icon" href="images/logo.ico">
<style>
body
{
    margin: 0 auto;
	padding: 0;
	font-family: Arial, Verdana, Sans-Serif;
	font-size: 0.8em;
	background: #E7E7E7;
}


 body{background-color:ivory}
</style>
<?





$yr='2016';

?>
<?
include("opendb.php");
include("test.php");

 //include('accounts/monthwise_pur_stock.php');


$r=mysqli_query($db,"select brname from branch where brcode='$brcode'");
$br_row=mysqli_fetch_row($r);
$branchname=strtoupper($br_row[0]);


?>
<head>
<title>Profit & Loss </title>
<script>
function sh()
{
 window.open("prnpl.php");
}
</script>
</head>
<body>
<?php include('includes/top.php'); ?>
<center>
<table border=0 width=100%>
<tr><td align=center><b>
<?print $branchname?><BR>

TRADING AND  PROFIT AND LOSS ACCOUNT 
</b></td>
<td width=30><a href='javascript:sh()' title='Print'><img src='images/logo.gif' border=0 height=50 width=50</a></td></tr>
</table>
<hr></hr>


 <table class='report'   align='center' width='100%'  border='1' style='width:100%'>
 
        <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=4; $month<16; $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 op from $month_bal_tbl where brcode='$brcode' and month='$k'");
	 $stocks=mysqli_fetch_row($ops);
	 $ostock=$stocks[0];

	 $cls=mysqli_query($db,"select cl from $month_bal_tbl 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='purchase'") 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=4; $month<16; $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=1;$j<13;$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 PURCHASE RETURNS

               print "<tr><td colspan=15><font color=brown><b> Purchase Returns</b></td></tr>";


unset($vr_total);
$vr_total = array();
$acg_pret=mysqli_query($db,"select gname from acgroups where headgroup='purchase returns' and gname!='-'")or die(mysqli_error($db));
while($acg_pret_row=mysqli_fetch_row($acg_pret))
{
 	$sq1="select * from ledger where grp='$acg_pret_row[0]' and brcode='$brcode'"; 
 	$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=4; $month<16; $month++)
		{
                $k=$month;
                if($month=='13')
                {
                $k=1;
                }
                if($k=='14')
                {
                $k=2;
                }
                if($month=='15')
                {
                $k=3;
                }
                    if($k<10)
                             $month= sprintf("%02s", $k);

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

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

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

			$hr_total+=$preturn_value;
                     $vr_total[$month]+=$preturn_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>";


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


unset($vr_total);
$vr_total = array();

print "<tr><td colspan=15><font color=brown><b>To Direct Expenses</b></td></tr>";
$bal=0; $tot=0;
$rrr=mysqli_query($db,"select * from acgroups where headgroup='DIRECT EXPENSE'") or die(mysqli_error($db));
while($dexrow=mysqli_fetch_array($rrr))
{ //while 1
 	$sq="select * from ledger where grp='$dexrow[0]' and brcode='$brcode' order by led";
 	$result=mysqli_query($db,$sq);
 	while($row1=mysqli_fetch_array($result))
 	{
		$hr_total =0;
                print "<tr><td><blockquote>$row1[led]</td>";
		for($month=4; $month<16; $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'";
			$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];
			}
    			$ded=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[0]' and brcode='$brcode' and date>='$df' and date<='$dt'") or die(mysqli_error($db));
     			$dex_row=mysqli_fetch_row($ded);
 			$tr+=$dex_row[0];
 			$tp+=$dex_row[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;

		}//end for loop	

		print "<td align='right'><a href='vmledgerdt.php?cd=$row1[led]' 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>";


$sumleft+=$tot;
$sumleft+=$ostock;
	// less purchase returns
$sumleft+=$preturn_value;

/* print "<tr><td colspan=2></td><td align=right>".putComa($tot)."</td></tr>";
print "<tr><td colspan=3 align=right>".putComa($sumleft)."</td>";    05/03/14 */  

//*********************************************************************************************//
print "<tr><td colspan=15><font color=brown><b>By Sales</b></td></tr>";
unset($vr_total);
$vr_total = array();
$bal=0; 
$tot=0;
$sumright=0;
$rrr=mysqli_query($db,"select * from acgroups where headgroup='sales' 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=1; $month<13; $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[$month]+=$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=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;

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

		//********************* 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=4; $month<16; $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>";
//*********************************************************************************************//


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' 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]'");
 	while($row1=mysqli_fetch_array($result))
 	{
		$hr_total=0;
		print "<tr><td colspan=2><blockquote>$row1[led]</td>";

		for($month=1; $month<13; $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>";

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


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

 for($month=4; $month<16; $month++)
{
$k=$month;

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


	 $cls=mysqli_query($db,"select cl from $month_bal_tbl 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;


/*************   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=4; $month<16; $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 sum(gtot) from jcardparts where brcode='$brcode' and invno=0 and edate between '$df' and '$dt'");
	$row=mysqli_fetch_row($sql);	
       $vr_total[$j]=$row[0];
	print "<td align='right'>".putcoma($vr_total[$j])."</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//


$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=1; $j<13; $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=14><font color=brown>By Gross Loss</td>";

       for($j=1; $j<13; $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=4; $month<16; $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>Total1:</b></td>";
             for($j=4;$j<16;$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>";
$sumleft2=$tot+abs($grossloss);
	
	//*8888888888888
		$ind_exp_tot=$sumleft2;
	//*8888888888888

?>


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

<?if($diff>0)
{
$grossprofit=$diff;
?>
<tr><td colspan=14><font color=green><b>By Gross Profit 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=4; $month<16; $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>Total2:</b></td>";
             for($j=4;$j<16;$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>";

 print "<tr><td colspan=2><font color=green><b>Profit/Loss:</b></td>";
             for($j=4;$j<16;$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_total[$k]-$pl_total[$k])."</td>";
		}

                 print "<td align='right'><font color=green><b>".putcoma(array_sum($vr_total)-array_sum($pl_total))."</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";*/
?>
