<link href="../css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<html>
<body >
<?
session_start();
include("../opendb.php");
include_once("../test.php");
$brcode = $_SESSION['brcode'];
$user = $_SESSION['user'];
$dd = $_REQUEST['dd'];
$thirty=$_REQUEST['thirty2'];
$sixty=$_REQUEST['sixty2'];
$ninty=$_REQUEST['ninty2'];

$ker=mysqli_query($db,"select brstate from branch where brcode='$brcode'");
$kar=mysqli_fetch_array($ker);
$tbl="stockagingbr".$brcode.$user;
$tbl2="stockrepvalbr".$brcode.$user;
$tbl_pur="purchasedetbr".$brcode.$user;
print "<center> <font color='red'><b>BRANCH AGEING-".getbrname($brcode)."</b> </font><br><font size=7><b><i><font color=#23D21D>Stock</font></I> </b><font size=6>Aging as on ".datetohtml($dd)."' ";
?>
<table border=1>
<th>PartCode <th>PartName<th>Qty<=<?print $thirty?><th>Price<=<?print $thirty?> <th>Qty><?print $thirty?> & <=<?print $sixty?><th>Price><?print $thirty?> & <=<?print $sixty?><th>Qty><?print $sixty?> & <=<?print $ninty?><th>Price><?print $sixty?> & <=<?print $ninty?><th>Qty><?print $ninty?><th>Price><?print $ninty?><th>Total Qty<th>Net
<?
mysqli_query($db,"update $tbl2 set entrydate='1901-01-01' where entrydate='0000-00-00'");
$next_year = mktime(0,0,0,date("m"),date("d"),date("Y")+1);
$re2=mysqli_query($db,"select distinct(partcode) from $tbl2  order by partcode");
while($row=mysqli_fetch_array($re2))
{
       $e2=mysqli_query($db,"select sum(supqty),partname from $tbl2  where partcode='$row[0]' group by partcode");
       $rrow=mysqli_fetch_array($e2);
       print "<tr><td>$row[partcode]</td><td>$rrow[partname]</td>";
       $ree2=mysqli_query($db,"select sum(supqty) supqty,entrydate,sum(price*supqty)/sum(supqty) price from $tbl2 where to_days(curdate())-to_days(entrydate)<=$thirty and partcode='$row[partcode]' group by partcode order by partcode");
       $rowtot=0;
       $row1=mysqli_fetch_array($ree2);
       if(mysqli_num_rows($ree2)>0)
       {
          print "<td>$row1[supqty]</td>";
          print "<td align=right>".getlstprice($row[partcode])."</td>";
          $qty30=$row1[supqty];
          $amt30=getlstprice($row[partcode])*$qty30;
          $amt30=round($amt30,2);
          $amt301+=$amt30;
          $rowtot+=$amt30;
       }
       else
       {
           print "<td>0</td>";
           print "<td>0</td>";
       }
       $ree2=mysqli_query($db,"select sum(supqty) supqty,entrydate,sum(price*supqty)/sum(supqty) price from $tbl2 where to_days(curdate())-to_days(entrydate)>$thirty and  to_days(curdate())-to_days(entrydate)<=$sixty and partcode='$row[partcode]' group by partcode order by partcode");
       $row1=mysqli_fetch_array($ree2)  ;
       if(mysqli_num_rows($ree2)>0)
       {
           print "<td>$row1[supqty]</td>";
           print "<td align=right>".getlstprice($row[partcode])."</td>";
           $qty60=$row1[supqty];
           $amt60=getlstprice($row[partcode])*$qty60;
           $amt60=round($amt60,2);
           $amt601+=$amt60;
           $rowtot+=$amt60;
       }
       else
       {
           print "<td>0</td>";
           print "<td>0</td>";
       }
       $ree2=mysqli_query($db,"select sum(supqty) supqty,entrydate,sum(price*supqty)/sum(supqty) price from $tbl2 where to_days(curdate())-to_days(entrydate)>$sixty and  to_days(curdate())-to_days(entrydate)<=$ninty and partcode='$row[partcode]' group by partcode order by partcode");
       $row1=mysqli_fetch_array($ree2)  ;
       if(mysqli_num_rows($ree2)>0)
       {
          print "<td>$row1[supqty]</td>";
          print "<td align=right>".getlstprice($row[partcode])."</td>";
          $qty90=$row1[supqty];
          $amt90=getlstprice($row[partcode])*$qty90;
          $amt90=round($amt90,2);
          $amt901+=$amt90;
          $rowtot+=$amt90;
       }
       else
       {
           print "<td>0</td>";
           print "<td>0</td>";
       }

       $ree2=mysqli_query($db,"select sum(supqty) supqty,entrydate,sum(price*supqty)/sum(supqty) price from $tbl2 where to_days(curdate())-to_days(entrydate)>$ninty  and partcode='$row[partcode]' group by partcode order by partcode");
       $row1=mysqli_fetch_array($ree2);

       if(mysqli_num_rows($ree2)>0)
       {
           print "<td>$row1[supqty]</td>";
           //print "<td align=right>".getlstprice($row[partcode])."XX</td>";
           $qty100=$row1[supqty];

//           $amt100=getlstprice($row[partcode])*$qty100;

           $amt100=round($amt100,2);
           $amt1001+=$amt100;
           $rowtot+=$amt100;

       }
       else
       {

           print "<td>0</td>";
           print "<td>0</td>";
       }

       print "<td>$rrow[0]</td><td align=right>$rowtot</td></tr>";
       $tot+=$rrow[0];
       $net=$amt301+$amt601+$amt901+$amt1001;
}
print "<tr><td colspan=2><center><b>Total</b></td><td></td><td align=right><b>$amt301</td>";
print "<td></td><td align=right><b>$amt601</td>";
print "<td></td><td align=right><b>$amt901</td>";
print "<td></td><td align=right><b>$amt1001</td>";
print "<td align=right><b>".putComa($tot)."</td><td align=right><b>".putComa($net)."</td></tr>";

function getlstprice($code)
{
 $rew11=mysqli_query($db,"select lstprice from spareprmast where partcode='$code' ");
 $rerow11=mysqli_fetch_row($rew11);
  return $rerow11[0];
}
?>
</table>
