<link href="../css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<html>
<body >

<?
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($save)
{
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
$tbl='suptmp'.$brcode.$user;
$re=mysqli_query($db,"select * from $tbl");
$dt=date("Y-m-d");
$pres=mysqli_query($db,"select max(orderno) from purorder_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))
{
  	$disres=mysqli_query($db,"select ndpcost from spareprmast where partcode='$ro[0]' order by effectfrom desc");
   	$disrow=mysqli_fetch_array($disres);
	print $disrow[ndpcost];
	$sql="insert into purorder_spare (partcode,partname,qty,date,orderto,brcode,orderno,rate) values('$ro[0]','".addslashes($ro[1])."','$ro[2]','$dt','$to','$brcode','$orderno','$ro[4]')";
	mysqli_query($db,$sql) or die(mysqli_error());
}
log_entry($brcode,$user,$REMOTE_ADDR,"Purchase Order Saved:$orderno,branch:$brcode");
//mysqli_query($db,"delete from $tbl");
mysqli_query($db,"drop table $tbl");
print "<b>Purchase order ".$orderno." has been saved</B>";
}
?>
<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?>' onSubmit='return valid()'>
<table border=1 width=100%>
<br>
<tr><td><center>Order To : 
<?
if ($brcode == 'PSN672' ||$brcode == 'PSN880')
{
$brres=mysqli_query($db,"select code,supname from sup_master where branch ='$brcode' order by supname");
}
else
{
$brres=mysqli_query($db,"select brcode,brname from productiondb.branch where brcode!='$brcode' and brtype!='offline' order by brname");
}
print "<select name='to'>";
 while($brrow=mysqli_fetch_array($brres))
 {
   print "<option value='$brrow[0]'>$brrow[1]</option>";
 }


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

<br>
<?
$tbl='suptmp'.$brcode.$user;
//mysqli_query($db,"drop table $tbl");


$sql="create table $tbl (partcode varchar(100),prtname varchar(200), qty double(14,2), brcode varchar(100),rate double(10,2))";

//$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;
  	}
	$d=mysqli_query($db,"select ndpcost from spareprmast where partcode='$partcode' order by effectfrom desc");
  	$di=mysqli_fetch_array($d);
 	$sql="insert into $tbl values('$partcode','$prtname','$qty', '$brcode','$di[0]')";
 	mysqli_query($db,$sql);
}

$res=mysqli_query($db,"select * from $tbl");
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))
   {
$sub=$row[2]*$row[4];
$tot+=$sub;
$qt+=$row[2];
    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><td>$row[4]</td><td>$sub</td>";
    print "<td><input type=submit value='Del' name='Del'></td></tr></form>";
  }
 print "<tr><td></td><td></td><td></td><td>$qt</td><td></td><td>$tot</td></tr>";

 print "</table>";

}

if($Del)
{
 mysqli_query($db,"delete from $tbl where partcode='$partcode'")or die('err'.mysqli_error());
}
?>
<script>
 function valid()
 {
  if(confirm('Do you want to save?'))
   return true;
  else
    return false;
 }
</script>
