<?
session_start();
$brcode = $_SESSION['brcode'];
$user = $_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}

include("../opendb.php");
include("../test.php");

if(isset($_POST['save']))
{

$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);
       $to = $_REQUEST['to'];

       while($ro=mysqli_fetch_array($re))
       {
             $sql="insert into purorder_spare (partcode,partname,qty,date,orderto,brcode,orderno) values('$ro[0]','$ro[1]','$ro[2]','$dt','$to','$brcode','$orderno')";
//echo $sql;
             mysqli_query($db,$sql) or die(mysqli_error($db));
        }
log_entry($brcode,$user,$REMOTE_ADDR,"Purchase Order Saved:$orderno,branch:$brcode");

mysqli_query($db,"delete from $tbl");
print "<b>Purchase order ".$orderno." has been saved</B>";
}
?>
<title>PSN ZF</title>
    <script type="text/javascript" src="../scripts/jquery-latest.js"></script>
	<link type="text/css" href="../includes/style.css" rel="stylesheet" />
<link href="../css/form.css" type="text/css" rel="stylesheet" media="screen"  />
    <script type="text/javascript">
        $(function() {
          if ($.browser.msie && $.browser.version.substr(0,1)<7)
          {
			$('li').has('ul').mouseover(function(){
				$(this).children('ul').show();
				}).mouseout(function(){
				$(this).children('ul').hide();
				})
          }
        });        
    </script>
<body background="../back_ground.jpg">
<?php include('logo1.php'); ?>
<?php include('top.php'); ?>

<link rel="stylesheet" href="../style.css" type="text/css" />
<style>

#tbl	{
      margin-top:70px;
    	}

.cont
	{




	background:url(images/yellow_back2.jpg) repeat;
  	width: 100%;position: absolute ;
	height:100% auto;
    	/* Optional - just for DIV)*/
    	//border: solid 1px #000000;
	-moz-box-shadow: 0px 5px 5px #888;
	-webkit-box-shadow: 0px 5px 5px #888;
	box-shadow: 0px 5px 10px #888;
    
	}
body
	{
	background: url(images/yellow_back2.jpg) repeat;
	}
#txt
	 {
	font-size:13px;
	}

	#popup td 
	{
	border-bottom:1px solid #CCCCCC;border-left:1px solid #CCCCCC;
	}
.del 	{
    	border: none;
   	background: url('img/delete.png') no-repeat ;
	padding: 0px 25px 20px 0;
    
	}
.edit {
   	 border: none;
    	background: url('img/edit.gif') no-repeat ;
	padding: 0px 25px 12px 0;
    
	}
.tb
	{
	-moz-box-shadow:    inset 1px 10px 10px grey;
  	 -webkit-box-shadow: inset 1px 10px 10px grey;
   	box-shadow:         inset 1px 10px 10px grey;
	}
.tb1
	{
	-moz-box-shadow:    inset 1px 10px 10px #565155;
   	-webkit-box-shadow: inset 1px 10px 10px #565155;
  	 box-shadow:         inset 1px 10px 10px #565155;
	width:100%;
	height:50px;
	font-size:11px;
	background:#2C2C30;
	}
  .tb1 a
	 {
  	text-decoration: none;
  	color:black;
	font-family: Arial, Helvetica;
    	}
</style>
<style>
	tr{background-color:white}
	th{background-color:steelblue;color:white}
</style>
<center>
<form action='<?print $PHP_SELF?>' method='POST' onSubmit='return valid()'>
<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='suptmp'.$brcode.$user;
$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(isset($_POST['add']))
{
$partcode = $_REQUEST['partcode'];
  $res1=mysqli_query($db,"select * from $tbl where partcode='$partcode'");
  if(mysqli_num_rows($res1)>0)
  {
    print "<script> alert('Duplicate Entry')</script>";
    //return;
  }
  
  $prtname = $_REQUEST['prtname'];
  $qty = $_REQUEST['qty'];
$res2=mysqli_query($db,"select * from partmaster where partcode='$partcode'");
$r=mysqli_fetch_array($res2);
$prtname=$r[partname];

 $sql="insert into $tbl values('$partcode','$prtname','$qty', '$brcode')";
 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))
  {
    print "\n<form action='$PHP_SELF' method='POST'> <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(isset($_POST[Del]))
{
$partcode=$_REQUEST['partcode'];
 mysqli_query($db,"delete from $tbl where partcode='$partcode'")or die('err'.mysqli_error($db));
}
?>
<script>
 function valid()
 {
  if(confirm('Do you want to save?'))
   return true;
  else
    return false;
 }
</script>
