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

<style>
 td{font-family:verdana;font-size:14;color:black;}
 th{font-family:verdana;font-size:14;color:white;background-color:#5b5858}
 body{background-color:ivory}
</style>
<?
include("opendb.php");
include("test.php");
$r=mysqli_query($db,"select brname from branch where brcode='$brcode'");
$br_row=mysqli_fetch_row($r);
$branchname=strtoupper($br_row[0]);
?>
<head>
<script>
function sh()
{
 window.open("prnpl.php");
}
</script>
</head>

<center>
<table border=0 width=100%>
<tr><td align=center><b>
<?print $branchname?> <BR>
OPENING TRADING AND  PROFIT AND LOSS ACCOUNT
</b></td>
<td width=30><a href='javascript:sh()' title='Print'><img src='Type001.gif' border=0></a></td></tr>
</table>
<hr>
</center>
<table border=1 width=100%>
<tr><td valign=top>
<table border =1 width=100%>
<?$ops=mysqli_query($db,"select * from plopstock");
$stocks=mysqli_fetch_row($ops);
$ostock=$stocks[0];
$cstock=$stocks[1];
?>
<tr><td colspan=2><font color=brown>To Opening Stock </td><td align=right><font color=brown><?print putComa($ostock);?></td></tr>
<tr><td colspan=2><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());
while($prows=mysqli_fetch_array($rrr))
{ //while1
 $result=mysqli_query($db,"select * from ledger where brcode='$brcocde' and grp='$prows[gname]'");
 while($row1=mysqli_fetch_array($result))
 {
  $sq="select * from actrans where brcode='$brcode' and (dr='$row1[led]' or cr='$row1[led]')";
   $re2=mysqli_query($db,$sq);
  $re3=mysqli_query($db,"select * from ledger where brcode='$brcode' and 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];
}

while($r1=mysqli_fetch_array($re2))
{
if($r1[cr]==$row1[led])
{
//  $tp+=$r1[amt];
}
else if($r1[dr]==$row1[led])
{
//  $tr+=$r1[amt];
}
}
$bal=$tr-$tp;
$tot+=$bal;
if($bal!=0)
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal)."</td>";
}
} //end while 1	
$sumleft+=$tot;
print "<td align=right>".putComa($tot)."</td></tr>";
//*********************************************************************************************//
print "<tr><td colspan=2><font color=brown>To Direct Expenses</td></tr>";
$bal=0; $tot=0;
$rrr=mysqli_query($db,"select * from acgroups where headgroup='direct expense'") or die(mysqli_error());
while($dexrow=mysqli_fetch_array($rrr))
{ //while 1
 $result=mysqli_query($db,"select * from ledger where brcode='$brcode' and grp='$dexrow[0]'");
 while($row1=mysqli_fetch_array($result))
 {
  $sq="select * from actrans where brcocde='$brcode' and dr='$row1[led]' or cr='$row1[led]'";

   $re2=mysqli_query($db,$sq);
  $re3=mysqli_query($db,"select * from ledger where brcode='$brcode' and 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];
}

while($r1=mysqli_fetch_array($re2))
{
if($r1[cr]==$row1[led])
{
//  $tp+=$r1[payment];
//  $tp+=$r1[amt];
}
else if($r1[dr]==$row1[led])
{
//  $tr+=$r1[payment];
//  $tr+=$r1[amt];
}
else
{
//$tr+=$r1[payment];
//$tp+=$r1[receipt];
}
}
$bal=$tr-$tp;
$tot+=$bal;
if($bal!=0)
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal)."</td>";
}
} // end while 1
$sumleft+=$tot;
$sumleft+=$ostock;
print "<td align=right>".putComa($tot)."</td></tr>";
print "<tr><td colspan=3 align=right>".putComa($sumleft)."</td>";
?>
</table>
</td>
<td width=50% valign=top>
<table border=1 width=100%>
<?
//*********************************************************************************************//
print "<tr><td colspan=2><font color=brown>By Sales</td></tr>";
$bal=0; $tot=0;
$sumright=0;
$rrr=mysqli_query($db,"select * from acgroups where headgroup='sales' order by ghlevel")  or die(mysqli_error());
while($sarow=mysqli_fetch_array($rrr))
{ // while 1
 $result=mysqli_query($db,"select * from ledger where brcode='$brcode' and grp='$sarow[0]'");
 while($row1=mysqli_fetch_array($result))
 {
  $sq="select * from actrans where brcode='$brcode' and dr='$row1[led]' or cr='$row1[led]'";

   $re2=mysqli_query($db,$sq);
  $re3=mysqli_query($db,"select * from ledger where brcode='$brcode' and 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];
}

while($r1=mysqli_fetch_array($re2))
{
if($r1[cr]==$row1[led])
{
//  $tp+=$r1[amt];
}
else if($r1[dr]==$row1[led])
{
//  $tr+=$r1[amt];
}
}
$bal=$tr-$tp;
	//******************************88888888888888888
		$bal*=-1;
	//******************************88888888888888888

$tot+=$bal;
if($bal!=0)
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal)."</td>";
}
}// end while 1
$sumright+=$tot;
print "<td align=right>".putComa($tot)."</td></tr>";
//print "<tr><td colspan=3 align=right>".putComa($sumright)."</td>";
//*********************************************************************************************//
print "<tr><td colspan=2><font color=brown><font color=brown>By Direct Income</td></tr>";
$bal=0; 
$tot=0;
//$sumright=0;
$rrr=mysqli_query($db,"select * from acgroups where headgroup='Direct Income' order by ghlevel")  or die(mysqli_error());
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))
 {
  $sq="select * from actrans where brcode='$brcode' and dr='$row1[led]' or cr='$row1[led]'";
   $re2=mysqli_query($db,$sq);
  $re3=mysqli_query($db,"select * from ledger where brcode='$brcode' and 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];
}

while($r1=mysqli_fetch_array($re2))
{
if($r1[cr]==$row1[led])
{
//  $tp+=$r1[amt];
}
else if($r1[dr]==$row1[led])
{
//  $tr+=$r1[amt];
}
}
$bal=$tr-$tp;
		//**************************8888888
			$bal=$bal*-1;
		//**************************8888888

$tot+=$bal;
if($bal!=0)
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal)."</td>";
}
} // end while 1
$sumright+=$tot; 
//$sumright=abs($sumright)+$cstock;           // CHECK
$sumright=$sumright+$cstock;           
print "<td align=right>".putComa($tot)."</td></tr>";

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
print "<tr><td colspan=2><font color=brown>By Closing Stock</td><td align=right><font color=brown>".putComa($cstock)."</td></tr>";

print "<tr><td colspan=3 align=right><b><u>".putComa($sumright)."</td>";
print "</tr></table>";
//xxxxxxxxxxxxxxxxxxxxxxxxxxx//
//$diff=abs($sumright)-$sumleft;
$diff=$sumright-$sumleft;             // CHECK
//$grossprofit=0;$grossloss=0;
if($diff>=0)
{
 print "<tr><td><table border=1 width=100%><tr><td><font color=green>To Gross Profit</td><td align=right><font color=green>".putComa($diff)."</td></tr>";
print "<tr><td colspan=2 align=right><b><u>".putComa($sumright)."</tr></td></table>";
}
else
{
 print "<table border=1 width=100%><tr><td><font color=brown>By Gross Loss</td><td align=right><font color=red>".putComa(abs($diff))."</td></tr>";
print "<tr><td colspan=2 align=right><font color=black>".putComa($sumleft)."</tr></td></table>";
}
?>
</table></table>

<hr>
<table border=2 width=100%>
<tr><td width=50%>
<table border=1 width=100%>

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

<tr><td colspan=2><font color=brown>To Indirect Expenses</td></tr>
<?
$bal=0; $tot=0;
//$sumright=0;
$sumleft2=0;

//********************************* 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());
while($indirow=mysqli_fetch_array($rrr))
{ // while 1

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

  $sq="select * from actrans where dr='$row1[led]' or cr='$row1[led]'";

   $re2=mysqli_query($db,$sq);
  $re3=mysqli_query($db,"select * from ledger where brcode='$brcode' and 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];
}

while($r1=mysqli_fetch_array($re2))
{
if($r1[cr]==$row1[led])
{
//  $tp+=$r1[payment];
//  $tp+=$r1[amt];
}
else if($r1[dr]==$row1[led])
{
//  $tr+=$r1[payment];
//  $tr+=$r1[amt];
}
else
{
//$tr+=$r1[payment];
//$tp+=$r1[receipt];
}
}
$bal=$tr-$tp;
$tot+=$bal;
if($bal!=0)
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal)."</td>";
}
} // end while 1
//$sumright+=$tot;
$sumleft2+=$tot;
print "<td align=right>".putComa($tot)."</td></tr>";
$sumleft2=$tot+abs($grossloss);
//print "<tr><td colspan=3 align=right>X".putComa($sumleft2)."</td>";
print "</tr></table>";
?>
<td valign=top>

<!-- *************right indirect income-->
<!--<tr><td>-->
<table border=1 width=100%>
<?if($diff>0)
{
$grossprofit=$diff;
?>
<td colspan=2><font color=green>By Gross Profit b/d</td><td align=right><font color=green><?print putComa($diff)?></td></tr>
<?
$gp=$diff;
}
?>

<tr><td colspan=2><font color=brown>By Indirect Income</td></tr>
<?
$bal=0; $tot=0;
$sumright=0;
$sumright+=$diff;
$rrr=mysqli_query($db,"select * from acgroups where headgroup='Indirect Income' order by ghlevel")  or die(mysqli_error());
while($indincrow=mysqli_fetch_array($rrr))
{ // while 1
 $result=mysqli_query($db,"select * from ledger where brcode='$brcode' and grp='$indincrow[0]'");
 while($row1=mysqli_fetch_array($result))
 {

  $sq="select * from actrans where brcode='$brcode' and dr='$row1[led]' or cr='$row1[led]'";

   $re2=mysqli_query($db,$sq);
  $re3=mysqli_query($db,"select * from ledger where brcode='$brcode' and 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];
}

while($r1=mysqli_fetch_array($re2))
{
if($r1[cr]==$row1[led])
{
//  $tp+=$r1[amt];
}
else if($r1[dr]==$row1[led])
{
//  $tr+=$r1[amt];
}
}
$bal=$tr-$tp;

//*******************888
 $bal=$bal*-1;
//*****************88888888
//$tot=$tot+abs($bal);
$tot=$tot+$bal;
if($bal!=0)
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal)."</td>";

}
} // end while 1
//$gtot=$tot+$diff;
$gtot=$tot+$grossprofit;
//$sumright=abs($sumright)+$tot;
		$sumright=$sumright+$tot;
$sumright+=$grossprofit;

//$porloss1=$sumleft2-$tot;//+abs($diff));


print "<td align=right>".putComa($tot)."</td></tr>";
print "<tr><td colspan=3 align=right><b><u>".putComa($gtot)."</td></tr></table>";
//print "left".$sumleft2. " right".$gtot;
if($sumleft2>$gtot)//summright)
{
//$porloss=abs($sumleft2)-(abs($gtot));//+abs($diff));
$porloss=($sumleft2-$gtot);//+abs($diff));
print "<tr><td></td><td><table border=2 width=100%><tr><td colspan=2><font color=darkbrown><b>Net Loss</td><td align=right><font color=darkbrown>".putComa($porloss)."</td></tr>";
Print "<tr><td colspan=3 align=right>".putComa($gtot)."</td></tr>";
print "</tr></table>";
}
else 
{
//$porloss=abs($sumleft2)-(abs($gtot));//+abs($diff));
//$porloss=($sumleft2-$gtot);//+abs($diff));
$porloss=($gtot-$sumleft2);//+abs($diff));
print "<tr><td><table border=2 width=100%><tr><td colspan=2><font color=green><b>Net Profit</td><td align=right><font color=green>".putComa($porloss)."</td></tr>";
Print "<tr><td colspan=3 align=right><b><u>".putComa($gtot)."</td></tr>";
print "</tr></table>";
}

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

</table>
</table>
</tr></td>
</table>
<?
//print "NPL=$porloss";
?>
