<head>
<script>
function sh2()
{
 window.open("prnopbsheet.php");
}
</script>
<style>
td{font-family:'arial narrow';font-size:14}
body{background-color:white}
</style>
</head>

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

<center>
<table border=0 width=100%>
<tr><td align=center><b>
ALPHONSA HOSPITAL, PULLURAMPARA<BR>
OPENING TRADING AND  PROFIT AND LOSS ACCOUNT
</b></td>
</table>
<hr>
</center>
<table border=0 width=100%>
<tr><td valign=top>
<table border =0 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=black>To Opening Stock </td><td align=right><font color=black><?print putComa($ostock);?></td></tr>
<tr><td colspan=2><font color=black>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 grp='$prows[gname]'");
 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 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=black>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 grp='$dexrow[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 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=0 width=100%>
<?
//*********************************************************************************************//
print "<tr><td colspan=2><font color=black>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 grp='$sarow[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 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=black><font color=black>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 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;
		//**************************8888888
			$bal=$ba*-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=black>By Closing Stock</td><td align=right><font color=black>".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=0 width=100%><tr><td><font color=black>To Gross Profit</td><td align=right><font color=black>".putComa($diff)."</td></tr>";
print "<tr><td colspan=2 align=right><b><u>".putComa($sumright)."</tr></td></table>";
}
else
{
 print "<table border=0 width=100%><tr><td><font color=black>By Gross Loss</td><td align=right><font color=black>".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=0 width=100%>
<tr><td width=50%>
<table border=0 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>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 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 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=0 width=100%>
<?if($diff>0)
{
$grossprofit=$diff;
?>
<td colspan=2><font color=black>By Gross Profit b/d</td><td align=right><font color=black><?print putComa($diff)?></td></tr>
<?
$gp=$diff;
}
?>

<tr><td colspan=2><font color=black>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 grp='$indincrow[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 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=0 width=100%><tr><td colspan=2><font color=darkblack><b>Net Loss</td><td align=right><font color=darkblack>".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=0 width=100%><tr><td colspan=2><font color=black><b>Net Profit</td><td align=right><font color=black>".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";
?>









<!-- ****************************B sheet starts here ******************** -->
<br>
<hr>
<table border=0 WIDTH=100%>
<tr><td colspan=2><CENTER> <B>ALPHONSA HOSPITAL, PULLURAMPARA<BR>OPENING BALANCE SHEET 
</TD></TR>
</table>
<table border=0>
<tr bgcolor=#5b5858><td><CENTER><font color=white><b>LIABILITIES</td><td colspan=2><b><font color=white><CENTER>ASSETS</td></tr>
<tr><td width=50% valign=top >
 <table border=0>
<?
$sql="select distinct headgroup from acgroups where nature='liabilities' order by ghlevel";
$result1=mysqli_query($db,$sql) or die(mysqli_error());
while($rrrr=mysqli_fetch_row($result1))
{ //while 1
$sql="select * from acgroups where headgroup='$rrrr[0]' order by level";
//print "<br>".$sql;
$result=mysqli_query($db,$sql) or die(mysqli_error());
 print "<tr><td colspan=3> <b><font color=black>$rrrr[0]</td></tr>";

while($row=mysqli_fetch_array($result))
{
 $tot=0;
 print "<tr><td colspan=3><font color=black>$row[0]</td></tr>";
 $re=mysqli_query($db,"select * from ledger where grp='$row[gname]'");
 while($row1=mysqli_fetch_array($re))
 {
 $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 led='$row1[led]'"); 
  $opb=mysqli_fetch_array($re3);
$tr=0; $tp=0;
if($opb[drcr]=="Dr")
{
$tr+=$opb[op];
// print "<br>$row1[led], $opb[drcr], tr=$tr, tp=$tp";
}
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;

//*************************************************************************************
	$bal=number_format($bal,2,'','');
//*************************************************************************************
 if($bal!=0)
  print "<tr><td width=3000> $row1[led]</td><td align=right>".putComa($bal*-1)."</td>";
 }
//*************************************************************************************
	$tot=number_format($tot,2,'','');
//*************************************************************************************
if($tot!=0)
 print "<td align=right>".putComa($tot*-1)."</td></tr>";
$tot2+=$tot;
}
} // end while 1
$plval=mysqli_query($db,"select * from pl") or die(mysqli_error());
$pval=mysqli_fetch_array($plval);
// print "<tr><td colspan=2><font color=black><b> Profit & loss a/c</tr></td>";
// print "<tr><td> Opening Balance(P&L ob)</td><td align=right>$pval[opb]</td></tr>";
// print "<tr><td> Current Period</td><td align=right>".putComa($pval[npl]*-1)."</td><td align=right>".putComa($pval[npl]*-1)."</td></tr>";


	//***********88888888888
		$porloss=$porloss*-1;
	//***********88888888888

 print "<tr><td> Current Period</td><td align=right>".putComa($porloss*-1)."</td><td align=right>".putComa($porloss*-1)."</td></tr>";


//$tot2+=$pval[npl];
$tot2+=$porloss;
//$tot2+=
print "<tr><td colspan=3 align=right>".putComa($tot2*-1)."</td></tr>";
$totleft=$tot2;
?>
</table>
 </td>
<td valign=top>
 <table border=0>
<?
$tp=0;$tr=0;$bal=0;$tot=0;$tot2=0;

 $ops=mysqli_query($db,"select * from plopstock") or die(mysqli_error());
 $clstock=mysqli_fetch_row($ops);$clstk=$clstock[1];

			//******************* ASSETS *********************//

$sql="select distinct headgroup from acgroups where nature='assets' order by ghlevel";
$result=mysqli_query($db,$sql) or die(mysqli_error());
while($rrrr=mysqli_fetch_row($result))
{ //while 1
$sql="select * from acgroups where headgroup='$rrrr[0]' order by level"; 
 print "<tr><td colspan=3><font color=black><b>".strtoupper($rrrr[0])."</td></tr>";
$rrs=mysqli_query($db,$sql) or die(mysqli_error());
while($row=mysqli_fetch_array($rrs))
{
$tot=0;$optr=0;$optp=0;
 $re=mysqli_query($db,"select * from ledger where grp='$row[gname]'"); //'$row[gname]'");
 print "<tr><td colspan=2><font color=black colspan=2>$row[0]</td></tr>";
 while($row1=mysqli_fetch_array($re))
 {
  $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 led='$row1[led]'"); 
  $opb=mysqli_fetch_array($re3);
$tr=0; $tp=0;
if($opb[drcr]=="Dr")
{
$optr+=$opb[op];
}
else if($opb[drcr]=="Cr")
{
$optr-=$opb[op];
//$optp+=$opb[op];
}
//print "<br>$row1[led] optr=$optr optp=$optp";

while($r1=mysqli_fetch_array($re2))
{
if($r1[cr]==$row1[led])
{
//  $tp+=$r1[amt];
}
else if($r1[dr]==$row1[led])
{
//  $tr+=$r1[amt];
}
}
//print "<br>tp=$tp , tr=$tr";
$bal=$optr+$tr-$tp;
$optr=0;$tr=0;
//$bal=$opb-$tp;
$tot+=$bal;
//$tot2+=$clstk;
//*************************************************************************************
		$bal=number_format($bal,2,'','');
//*************************************************************************************
 if($bal!=0)
  print "<tr><td width=3000>$row1[led]</td><td align=right>".putComa($bal)."</td>";
 }

//*************************************************************************************
$tot=number_format($tot,2,'','');
//*************************************************************************************
 if($tot!=0)
 print "<td align=right>".putComa($tot)."</td></tr>";
$tot2+=$tot;
}
}// end while 1
$tot2+=$clstk;
print "<tr><td colspan=2>Closing stock</td><td align=right>".putComa($clstock[1])."</td></tr>";
print "<tr><td colspan=3 align=right>".putComa($tot2)."</td></tr>";
$totright=$tot2;//+$clstk; 

//$bal=abs($totleft)-abs($totright);
$bal=($totleft+$totright);
$bal=number_format($bal,2,'','');
?>
 </table>
</tr></td>
</table>
<?
print "<font face=verdana><br>Difference:".putComa($bal);
?>

