<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");

if($show)
{
?>
<style>

	td,th,body{font-size:14;font-family:'arial narrow';letter-spacing:1}
</style>
<center>
<?
 $dfrom=$y1.'-'.$m1.'-'.$d1;
 $dto=$y2.'-'.$m2.'-'.$d2;
$brres2=mysqli_query($db,"select * from branch order by brname");
$brrow2=mysqli_fetch_array($brres2);

?>
<table>
<TR><TD COLSPAN=2 ALIGN=CENTER><b> PSN CONSTRUCTION EQUIPMENTS P LTD </b><BR>
	 <?print $brrow2[braddr].",".$brrow2[brcity].",<br><u> PH - ".$brrow2[brphone]?></u>
TIN: <?print $brrow2[tin]?>, CST: <?print $brrow2[cstno]?>&nbsp;
</td></tr>
<tr><td COLSPAN=2 ALIGN=CENTER>
<?print "<h3><br>SPARE INVOICE SUMMARY </H3>";

print " from ".datetohtml($dfrom).' to '.datetohtml($dto);
$brres=mysqli_query($db,"select * from branch order by brname");
while($brrow=mysqli_fetch_array($brres))
{
  print "<fieldset><legend><b>$brrow[brname]</b></legend>";
 $sql="select sum(aprice) aprice,sum(taxamt)txamt,sum(cessamt)cess ,sum(tot)tot from spareinvoice  where brcode='$brrow[brcode]' and entrydate>='$dfrom' and entrydate<='$dto' and paytype='Cash'";

 $cshre=mysqli_query($db,$sql) or die(mysqli_error($db));
 $cshrow=mysqli_fetch_array($cshre);
 print "<table border=1 width=100%><th width=200>Description <th colspan=4 width=400>Cash <th colspan=6 width=400>Credit";
print "<tr><th></th><th>Amount <th>Tax <th>Cess <th>Total";
print "<th>Amount <th>Tax <th>Cess <th>Total";

 print "<tr><td>Counter Sales</td><td align=right>".putComa($cshrow[0])."</td>";
 print "<td align=right>".putComa($cshrow[1])."</td>";
 print "<td align=right>$cshrow[2]</td><td align=right>".putComa($cshrow[3])."</td>";

$sql="select sum(aprice) aprice,sum(taxamt)txamt,sum(cessamt)cess ,sum(tot)tot from spareinvoice  where brcode='$brrow[brcode]' and entrydate>='$dfrom' and entrydate<='$dto' and paytype='Credit'";
 $crre=mysqli_query($db,$sql) or die(mysqli_error($db));
 $crrow=mysqli_fetch_array($crre);
print "<td align=right>".putComa($crrow[0])."</td><td align=right>".putComa($crrow[1])."</td>";
 print "<td align=right>".putComa($crrow[2])."</td>";
 print "<td align=right>".putComa($crrow[3])."</td></tr>";
//************** job card

$jre=mysqli_query($db,"select jcno from jobcard where date>='$dfrom' and date<='$dto' and brcode='$brrow[brcode]' and ptype='Cash'");
$tamt1=0;
$tamt2=0;
$tamt3=0;
$tamt4=0;

while($jr=mysqli_fetch_array($jre))
{
$sql="select sum(amt) aprice,sum(taxamt)txamt,sum(cessamt)cess ,sum(gtot)tot from jcardparts where jcno='$jr[jcno]' and brcode='$brrow[brcode]'";
 $jtotre=mysqli_query($db,$sql) or die('err'.mysqli_error($db));
 $jtot=mysqli_fetch_array($jtotre);
 $tamt1+=$jtot[0];
 $tamt2+=$jtot[1];
 $tamt3+=$jtot[2];
 $tamt4+=$jtot[3];
}

print "<tr><td>Job Card</td><td align=right>".putComa($tamt1)."</td><td align=right>".putComa($tamt2)."</td>";
 print "<td align=right>".putComa($tamt3)."</td>";
 print "<td align=right>".putComa($tamt4)."</td>";

 //********* credit

$jre1=mysqli_query($db,"select jcno from jobcard where date>='$dfrom' and date<='$dto' and brcode='$brrow[brcode]' and ptype='Credit'");
$tamt1=0;
$tamt2=0;
$tamt3=0;
$tamt4=0;

while($jr2=mysqli_fetch_array($jre1))
{
$sql="select sum(amt) aprice,sum(taxamt)txamt,sum(cessamt)cess ,sum(gtot)tot from jcardparts where jcno='$jr2[jcno]' and brcode='$brrow[brcode]'";
 $jtotre2=mysqli_query($db,$sql) or die('err'.mysqli_error($db));
 $jtot=mysqli_fetch_array($jtotre2);
 $tamt1+=$jtot[0];
 $tamt2+=$jtot[1];
 $tamt3+=$jtot[2];
 $tamt4+=$jtot[3];
}

print "<td align=right>".putComa($tamt1)."</td><td align=right>".putComa($tamt2)."</td>";
 print "<td align=right>".putComa($tamt3)."</td>";
 print "<td align=right>".putComa($tamt4)."</td></tr>";
//*********** trans out
$trre=mysqli_query($db,"select sum(amount) from sparetransout where brcode='$brrow[brcode]' and entrydate >='$dfrom' and entrydate<='$dto'");
$trrow=mysqli_fetch_array($trre);
print "<tr><td>Transfer out </td><td align=right>".putComa($trrow[0])."</td></tr>";

//*********** trans in
$trrein=mysqli_query($db,"select sum(amount) from sparetransin where brcode='$brrow[brcode]' and entrydate >='$dfrom' and entrydate<='$dto'");
$trrow=mysqli_fetch_array($trrein);
print "<tr><td>Transfer in </td><td align=right>".putComa($trrow[0])."</td></tr>";

//*********** wr trans out
$trreoutw=mysqli_query($db,"select sum(psnqty*price) from warsparetransout where brcode='$brrow[brcode]' and date >='$dfrom' and date<='$dto'")or die(mysqli_error($db));
$trrow=mysqli_fetch_array($trreoutw);
print "<tr><td>Transfer out WAR </td><td align=right>".putComa($trrow[0])."</td></tr>";

//*********** wr transin
$trreoutw=mysqli_query($db,"select sum(psnqty*amount) from sparetrinwar where brcode='$brrow[brcode]' and date >='$dfrom' and date<='$dto'")or die(mysqli_error($db));
$trrow=mysqli_fetch_array($trreoutw);
print "<tr><td>Transfer in WAR</td><td align=right>".putComa($trrow[0])."</td></tr>";


print "</table></fieldset>";

}

return;
}
?>
</td></tr>
</table>

<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
//	tr{background-color:white}
	th{background-color:steelblue;color:white}
	td{font-size:13}
.right{text-align:right};
</style>
<center>
<h3>SPARE INVOICE REPORT -2</H3>
<form action='<?Print $PHP_SELF?>' method='post'>
<table cellspacing=1 cellpadding=1 bgcolor='steelblue'>
<tr><td bgcolor='white' align=center>
<table class=noprint>
<tr><td>Date from </td><td>dd <input type=text name='d1' size=2 value='<?print date("d")?>'>
mm <input type=text name='m1' size=2 value='<?print date("m")?>'>
yy <input type=text name='y1' size=4 value='<?print date("Y")?>'></td></tr>

<tr><td align=right>to </td><td>dd <input type=text name='d2' size=2 value='<?print date("d")?>'>
mm <input type=text name='m2' size=2 value='<?print date("m")?>'>
yy <input type=text name='y2' size=4 value='<?print date("Y")?>'>
</td></tr>

<tr><td colspan=2 align=center bgcolor='lightsteelblue'><input type=submit value='Show' name='show'></td></tr>
</table>
</td></tr>
</table>
</form>

