<style>
 td{font-family:'arial narrow';font-size:14;color:black;}
 th{font-family:'arial narrow';font-size:14;color:white;background-color:#5b5858}
 body{background-color:white}
</style>
<?
include("opendb.php");
include("test.php");

?>
<head>
<script>
function sh()
{
 window.open("prnpldw.php?df=<?print $df?>&dtt=<?print $dtt?>");
}
</script>
</head>

<center>
<table border=0 width=100%>
<tr><td align=center><b>
ALPHONSA HOSPITAL, PULLURAMPARA<BR>
TRADING AND  PROFIT AND LOSS ACCOUNT FROM <?print datetohtml($df)." to ".datetohtml($dtt)?>
</b></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><b>Opening Stock </td><td align=right><b><?print putComa($ostock);?></td></tr>
<tr><td colspan=2><font color=black><b>To Purchase A/c</td></tr>
<?
$sumleft=0;
 $result=mysqli_query($db,"select * from ledger where grp='Purchase Accounts'");
 while($row1=mysqli_fetch_array($result))
 {
/*  $sq="select * from actrans where ledger='$row1[led]' or dr='$row1[led]' or cr='$row1[led]' or mode='$row1[led]'";*/

 $sq="select * from actrans where dt>='$df' and dt<='$dtt' and(dr='$row1[led]' or cr='$row1[led]')";

   $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];
}

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;
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal)."</td>";
}
$sumleft+=$tot;
print "<td align=right>".putComa($tot)."</td></tr>";
//*********************************************************************************************//
print "<tr><td colspan=2><font color=black><b>To Direct Expenses</td></tr>";
$bal=0; $tot=0;
 $result=mysqli_query($db,"select * from ledger where grp='Direct Expenses'");
 while($row1=mysqli_fetch_array($result))
 {
/*  $sq="select * from actrans where ledger='$row1[led]' or dr='$row1[led]' or cr='$row1[led]' or mode='$row1[led]'";*/

 $sq="select * from actrans where dt>='$df' and dt<='$dtt' and(dr='$row1[led]' or cr='$row1[led]')";

   $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];
}

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;
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal)."</td>";
}
$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=black><b>By Sales</td></tr>";
$bal=0; $tot=0;
$sumright=0;
 $result=mysqli_query($db,"select * from ledger where grp='Sales Accounts'");
 while($row1=mysqli_fetch_array($result))
 {
/*  $sq="select * from actrans where ledger='$row1[led]' or dr='$row1[led]' or cr='$row1[led]' or mode='$row1[led]'";*/

 $sq="select * from actrans where dt>='$df' and dt<='$dtt' and(dr='$row1[led]' or cr='$row1[led]')";

   $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];
}

while($r1=mysqli_fetch_array($re2))
{
if($r1[cr]==$row1[led])
{
  $tp+=$r1[amt];
}
else if($r1[dr]==$row1[led])
{
  $tr+=$r1[amt];
}
/*else
{
$tr+=$r1[payment];
$tp+=$r1[receipt];
}*/
}
$bal=$tr-$tp;
$tot+=$bal;
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal*-1)."</td>";
}
$sumright+=$tot;
print "<td align=right>".putComa($tot*=-1)."</td></tr>";
print "<tr><td colspan=3 align=right>".putComa($sumright*=-1)."</td>";
//*********************************************************************************************//
print "<tr><td colspan=2><font color=black><b>By Direct Income</td></tr>";
$bal=0; 
$tot=0;
//$sumright=0;
 $result=mysqli_query($db,"select * from ledger where grp='Direct Income'");
 while($row1=mysqli_fetch_array($result))
 {
/*  $sq="select * from actrans where ledger='$row1[led]' or dr='$row1[led]' or cr='$row1[led]' or mode='$row1[led]'";
*/

 $sq="select * from actrans where dt>='$df' and dt<='$dtt' and(dr='$row1[led]' or cr='$row1[led]')";

   $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];
}

while($r1=mysqli_fetch_array($re2))
{
if($r1[cr]==$row1[led])
{
  $tp+=$r1[amt];
}
else if($r1[dr]==$row1[led])
{
  $tr+=$r1[amt];
}
else
{
//$tr+=$r1[amt];
//$tp+=$r1[amt];
}
}
$bal=$tr-$tp;
$tot+=$bal;
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal*-1)."</td>";
}
$sumright+=$tot; 
$sumright=abs($sumright)+$cstock;
print "<td align=right>".putComa($tot*-1)."</td></tr>";

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

print "<tr><td colspan=3 align=right><b>".putComa($sumright)."</td>";
print "</tr></table>";
//xxxxxxxxxxxxxxxxxxxxxxxxxxx//
$diff=abs($sumright)-$sumleft;
//$grossprofit=0;$grossloss=0;
if($diff>0)
{
 print "<tr><td><table border=1 width=100%><tr><td><font color=black><b>To Gross Profit</td><td align=right><font color=black><b>".putComa($diff)."</td></tr>";
print "<tr><td colspan=2 align=right><b>".putComa($sumright)."</tr></td></table>";
}
else
{
 print "<table border=1 width=100%><tr><td><font color=black><b>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><b>".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=black><b>To Indirect Expenses</td></tr>
<?
$bal=0; $tot=0;
//$sumright=0;
$sumleft2=0;
 $result=mysqli_query($db,"select * from ledger where grp='Indirect Expenses'");
 while($row1=mysqli_fetch_array($result))
 {
/*  $sq="select * from actrans where ledger='$row1[led]' or dr='$row1[led]' or cr='$row1[led]' or mode='$row1[led]'";*/

 $sq="select * from actrans where dt>='$df' and dt<='$dtt' and(dr='$row1[led]' or cr='$row1[led]')";

   $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];
}

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;
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal)."</td>";
}
//$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=black><b>By Gross Profit b/d</td><td align=right><font color=black><b><?print putComa($diff)?></td></tr>
<?
$gp=$diff;
}
?>

<tr><td colspan=2><font color=black><b>By Indirect Income</td></tr>
<?
$bal=0; $tot=0;
$sumright=0;
$sumright+=$diff;
 $result=mysqli_query($db,"select * from ledger where grp='Indirect Income'");
 while($row1=mysqli_fetch_array($result))
 {
/*
  $sq="select * from actrans where ledger='$row1[led]' or dr='$row1[led]' or cr='$row1[led]' or mode='$row1[led]'";
*/

 $sq="select * from actrans where dt>='$df' and dt<='$dtt' and(dr='$row1[led]' or cr='$row1[led]')";


   $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];
}

while($r1=mysqli_fetch_array($re2))
{
if($r1[cr]==$row1[led])
{
  $tp+=$r1[amt];
}
else if($r1[dr]==$row1[led])
{
  $tr+=$r1[amt];
}
/*else
{
$tr+=$r1[payment];
$tp+=$r1[receipt];
}*/
}
$bal=$tr-$tp;
$tot=$tot+abs($bal);
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal*-1)."</td>";

}
//$gtot=$tot+$diff;
$gtot=$tot+$grossprofit;
$sumright=abs($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>".putComa($gtot)."</td></tr></table>";
//print "left".$sumleft2. " right".$gtot;
if($sumleft2>$gtot)//summright)
{
$porloss=abs($sumleft2)-(abs($gtot));//+abs($diff));
print "<tr><td></td><td><table border=2 width=100%><tr><td colspan=2><font color=black><b>Net Loss</td><td align=right><font color=black><b>".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));
print "<tr><td><table border=2 width=100%><tr><td colspan=2><font color=black><b>Net Profit</td><td align=right><font color=black><b>".putComa($porloss*-1)."</td></tr>";
Print "<tr><td colspan=3 align=right><b>".putComa($gtot)."</td></tr>";
print "</tr></table>";
}

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

</table>
</table>
</tr></td>
</table>
