<?
session_start();
include("../opendb.php");
if($save)
{
$tbl='declaretmp'.$brcode;
$re=mysqli_query($db,"select * from $tbl");
$dt=date("Y-m-d");
$pres=mysqli_query($db,"select max(orderno) from declare_spare where brcode='$brcode'");
$prow=mysqli_fetch_row($pres);
if($prow[0]<=0)
 $orderno=1;
else
 $orderno=($prow[0]+1);

while($ro=mysqli_fetch_array($re))
{
 $sql="insert into declare_spare (partcode,partname,qty,date,orderto,brcode,orderno) values('$ro[0]','$ro[1]','$ro[2]','$dt','$to','$brcode','$orderno')";
 mysqli_query($db,$sql) or die(mysqli_error($db));
}
mysqli_query($db,"delete from $tbl");
}
?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
	tr{background-color:white}
	th{background-color:steelblue;color:white}
</style>
<center>
<form action='<?print $PHP_SELF?>'>
<table border=1 width=100%>
<tr><td><center>Order To : 
<?
$brres=mysqli_query($db,"select brcode,brname from branch where brcode!='$brcode' order by brname");
print "<select name='to'>";
 while($brrow=mysqli_fetch_array($brres))
 {
   print "<option value='$brrow[0]'>$brrow[1]</option>";
 }
$brres1=mysqli_query($db,"select distinct code,supname from sup_master order by supname");
 while($brrow1=mysqli_fetch_array($brres1))
 {
   print "<option value='$brrow1[0]'>$brrow1[1]</option>";
 }

print "</select>";
?>
&nbsp;
<input type=submit value='Save & Print' name='save'>
</td></tr></table>
</form>

<br>
<?
$tbl='declaretmp'.$brcode;
$sql="create table $tbl (partcode varchar(100),prtname varchar(200), qty double(14,2), brcode varchar(100))";

//$sql="create table $tbl (partcode varchar(100),prtname varchar(200), qty double(14,2), price double(14,2), discount double(14,2), hand double(14,2), cst double(14,2), tot double(14,2), brcode varchar(100))";
mysqli_query($db,$sql);


if($add)
{
  $res1=mysqli_query($db,"select * from $tbl where partcode='$partcode'");
  if(mysqli_num_rows($res1)>0)
  {
    print "<script> alert('Duplicate Entry')</script>";
    return;
  }

 $sql="insert into $tbl values('$partcode','$prtname','$qty', '$brcode')";
 mysqli_query($db,$sql);
}

$res=mysqli_query($db,"select * from $tbl order by partcode");
if(mysqli_num_rows($res)>0)
{
  $i=1;
  print "<table border=1 cellpadding=3> <th>No <th>Item Code <th>Item Name <th>Qty <th> Del";
  while($row=mysqli_fetch_array($res))
  {
    print "\n<form action='$PHP_SELF'> <input type=hidden name='partcode' value='$row[0]'><tr><td>";
    print $i++."</td><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td>";
    print "<td><input type=submit value='Del' name='Del'></td></tr></form>";
  }
 print "</table>";
}

if($Del)
{
 mysqli_query($db,"delete from $tbl where partcode='$partcode'")or die('err'.mysqli_error($db));
}
?>
