<?session_start();
include("opendmsdb.php");
include("../test.php");
$dres=mysqli_query($db,"select date_format(upddate,'%d %M %Y') from dailystock ")or die(mysqli_error());
$drow=mysqli_fetch_row($dres);
?>
<link rel="stylesheet" href="style.css" type="text/css" />
<style>
body,td{font-family:'arial narrow';letter-spacing:1;font-size:.9em}
th{background-color:c1d799;font-size:.8em}
.mnu{margin-left:5}
</style>
<center>
<TABLE BGCOLOR='BROWN' WIDTH=100%>
	<TR><TD ALIGN=CENTER><h2><FONT COLOR=IVORY>DMS - DAILY STOCK REPORT</h2></TD></TR>
</TABLE><BR>
<table border=0 width=70%>
<tr><td valign=top align=center>
<!--
<table border=1>
<th colspan=7>SUMMARY </th>
<tr>
<th>Branch <th>Item Group <th>Qty <th>Amount
<?
$br=mysqli_query($db,"select distinct a.brcode,b.brname,date_format(a.upddate,'%d %M %Y %r') from dailystock a, branch b where a.brcode=b.brcode") or die(mysqli_error());
while($brrow=mysqli_fetch_array($br))
{
 $sq="select itype,sum(balqty),sum(tot) from dailystock where brcode='$brrow[0]' group by itype order by itype";
  $res=mysqli_query($db,$sq) or die('err'.mysqli_error());
$t1=$t2=0;
  print "<tr><td rowspan=4 align=center><b>$brrow[1]</B><br>As on ".$brrow[2]."</td>";
   while($row=mysqli_fetch_array($res))
    {
      print "<tr><td>$row[0]</td><td align=right>$row[1]</td><td align=right>$row[2]</td>";
      $t1+=$row[1];
      $t2+=$row[2];
    }
  print "<tr><td align=center><b>Total</td><td>&nbsp;</td><td align=right>".$t1."</td><td align=right>".$t2."</td></tr>";
}
?>
</table> -->
</td>
<td valign=top align=center>
<form action='<?print $PHP_SELF?>'>
  <table border=1 width=200>
	<tr><td align=center bgcolor='lightsteelblue' colspan=2><b>SEARCH</td></tr>
	<tr><td>Branch</td><td><select name='brch'>
<?
$br=mysqli_query($db,"select distinct a.brcode,b.brname,date_format(a.upddate,'%d %M %Y') from dailystock a, branch b where a.brcode=b.brcode") or die(mysqli_error());
while($brrow=mysqli_fetch_array($br))
{
 print "<option value='$brrow[0]'>$brrow[1]</option>";
}
?>
      <option>All</option></select></td></tr>
	<tr><td>Item</td><td><select name='itype'>
<?
$re=mysqli_query($db,"select distinct itype from dailystock");
while($irow=mysqli_fetch_row($re))
{
 print "<option>$irow[0]</option>";
}
?>

</select></td></tr>
	<tr><td align=center bgcolor='lightsteelblue' colspan=2><input type=submit value='Show' name='shall'></td></tr>

  </table>
 </form>
</td></tr>
</table>
<?
$gtt=0;
$divres=mysqli_query($db,"select distinct divis from branch order by divis");
while($divirow=mysqli_fetch_array($divres))
{
$gt=0;

?>
<table border=1 width=60%>
<th colspan=7>SUMMARY </th>
<tr>
<th>Branch <th>Oil (Rs.) <th>Other (Rs.) <th>Spare Parts (Rs.)<th>Total (Rs.)
<?

$br=mysqli_query($db,"select distinct a.brcode,b.brname,date_format(a.upddate,'%d %M %Y %r') from dailystock a, branch b where a.brcode=b.brcode and b.divis='$divirow[0]'") or die(mysqli_error());
while($brrow=mysqli_fetch_array($br))
{
$t1=$t2=0;
 $sq="select distinct itype from dailystock order by itype";
  $res=mysqli_query($db,$sq) or die('err'.mysqli_error());

  print "<tr valign=top><td align=center><b>$brrow[1]</B><br>As on ".$brrow[2]."</td>";

   while($row2=mysqli_fetch_array($res))
    {
      $sql="select sum(tot) from dailystock where brcode='$brrow[0]' and itype='$row2[0]'";
      $res2=mysqli_query($db,$sql)or die(mysqli_error());
	if(mysqli_num_rows($res2)<=0)
          print "<td>N/A</td>";
      while($row=mysqli_fetch_array($res2))
        {
           print "<td align=right>$row[0]</td>";
           $t+=$row[0];
		$gt+=$t;
		$ttt+=$t;
		$t=0;
         }
    }
       print "<td align=right>$ttt</td>";$ttt=0;

} // end while
$gtt+=$gt;
?>
<tr><td colspan=4 align=center>Total</td><td align=right><?print putComa($gt)?></td></tr>
<?
}
print "<tr><td colspan=4 align=center>Grant Total</td><td align=right>". putComa($gtt)."</td></tr></table>";



if($shall)
{
if($brch=='All')
{
$sql1="select distinct a.brcode,b.brname,date_format(a.upddate,'%d %M %Y') from dailystock a, branch b where a.brcode=b.brcode group by a.brcode";
//$sql2="select itype from dailystock where itype='$itype'";
}
else
{
$sql1="select distinct a.brcode,b.brname,date_format(a.upddate,'%d %M %Y') from dailystock a, branch b where a.brcode='$brch' and a.brcode=b.brcode group by a.brcode";
//$sql2="select itype from dailystock where itype='$itype'";
$sql3="select * from dailystock where itype='$itype' and brcode='$brch' order by no ";
}

$br=mysqli_query($db,$sql1) or die(mysqli_error());
while($brrow=mysqli_fetch_array($br))
{
print "<h3>$itype Stock at $brrow[1]  </h3> Last updated on $brrow[2]";
?>

<table cellspacing=1 cellpadding=1 bgcolor=gray width=100%>
<th width=10>No <th width=20>Location <th width=90>Part Code <th>Part Name <th>Unit <th>ROL <th>Opening Bal <th>INW Qty
<th>OUT Qty <th>Bal Qty <th>NDP Rate <th>Total 
<?
$i=1;
$tb=0;
$tinw=$toutw=$tbal=$tndp=0;
$gt=0;
if($brch=='All')
 $sql3="select * from dailystock where itype='$itype' and brcode='$brrow[0]' ";
$result=mysqli_query($db,$sql3)or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
 print "<tr BGCOLOR=WHITE><td align=center>".$i++."</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td>";
 print "<td align=center>$row[5]</td><td align=center>$row[6]</td><td align=right>$row[7]</td>";
 print "<td align=right>$row[8]</td><td align=right>$row[9]</td>";
 print "<td align=right>$row[10]</td><td align=right>$row[11]</td><td align=right>$row[12]</td>";
 print "</tr>";
 $tb+=$row[opbal];
 $tinw+=$row[inwqty];
 $toutw+=$row[outwqty];
 $tbal+=$row[balqty];
 $tndp+=$row[ndprate];
 $gt+=$row[tot];
}
 print "\n<tr bgcolor=c1d799><td colspan=6 align=center><b>Total</td><td align=right>".putComa($tb)."</td>";
print "<td align=right>".putComa($tinw)."</td><td align=right>".putComa($toutw)."</td>";
print "<td align=right>".putComa($tbal)."</td><td align=right>".putComa($tndp)."</td>";
print "<td align=right>".putComa($gt)."</td>";

print "\n</table>";
}

}
?>

