<? 
session_start();
$brcode = $_SESSION['brcode'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}
include("../opendb.php");
$del = $_REQUEST['del'];
$code = $_REQUEST['code'];
$save = $_REQUEST['save'];
$m = $_REQUEST['m'];
$d = $_REQUEST['d'];
$y = $_REQUEST['y'];
$dano = $_REQUEST['dano'];
$mcode = $_REQUEST['mcode'];
$config = $_REQUEST['config'];
$supplier = $_REQUEST['supplier'];
$amt = $_REQUEST['amt'];
if($del)
{
 mysqli_query($db,"delete from purorder where code='$code' and brcode='$brcode'");
}
if($save)
{
 $orddate=$y.'-'.$m.'-'.$d;
$re=mysqli_query($db,"select max(ordno) from purorder  where brcode='$brcode'")or die(mysqli_error());
$ro=mysqli_fetch_row($re);
$on=$ro[0]+1;
$cc=mysqli_query($db,"select * from purorder where dano='$dano' and brcode='$brcode'");
if(mysqli_num_rows($cc)>0)
{
 print "<script> alert('Duplicate DA'); history.back(1); </script>";
 return;
}
 $sql="insert into purorder (ordno,ordby,orddate,dano, mcode,config, brcode,supplier,amount,purno) values('$on','$ordby','$orddate','$dano','$mcode','$config','$brcode','$supplier','$amt','-')";
 mysqli_query($db,$sql) or die(mysqli_error());
 print "<font color=green>Data has been saved </font>";
}
?>
<link type="text/css" href="../jquery/jquery-ui.min.css" rel="stylesheet" />	
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../cal/jquery-ui1.js"></script>
<script type="text/javascript" src="../jquery/jquery-1.7.min.js"></script>
<script type="text/javascript" src="../jquery/jquery-ui.min.js"></script>
<script type="text/javascript" src="../jquery/jquery.jqEasyCharCounter.min.js"></script>
<script>
var ar1=[];
function fun1()
{
 var x=document.f1.config.selectedIndex;
 if(x<=0)
   document.f1.mcode.value='';
 else
  document.f1.mcode.value=ar1[document.f1.config.selectedIndex];
}

function valid()
{
var p1=document.f1.orno.value
var dano=document.f1.dano.seletedIndex
var mcode=document.f1.mcode.seletedIndex

 submit="true";
 if(p1.length<=0)
  {submit="false"; alert('Invalid Order Number');}
 if(dano<=0)
  {submit="false"; alert('Invalid DA Number');}
 if(mcode<=0)
  {submit="false"; alert('Invalid Model Code');}

 if(submit=="false")
  return false;
}

function chkey(abc)
{
 yy=abc.value
 if(yy.indexOf(' ')>-1) 
  {
    alert('Space is not allowed');
    abc.value='';
  }
}

$(function(){

$(".partcode").live("keydown.autocomplete",function(){

$partcode = $(this);

$row = $partcode.parent().parent();

$(this).autocomplete({
		source: function( request, response ) {

		$.ajax({
		     
			url: "searchconf.php",
			dataType: "json",
			data: "key="+$partcode.val(),
			success: function( data ) {
				response( $.map( data, function( item ) {
					return {
						label: item.partcode+' | '+  item.partname,
						value: item.partcode,
						name:item.partname,
					
					}
				}));
			}
		});
	}
	
,
	minLength: 2,
	select: function( event, ui ) {

//	$row.find('.partname').val(ui.item.name);
document.getElementById("mcode").value=ui.item.name;

	document.getElementById("amt").focus();
	//document.getElementById("unitprice").value=ui.item.stock;
	//$row.find('.rate').val(ui.item.price);
	
	/*log( ui.item ?
	"Selected: " + ui.item.label :
	"Nothing selected, input was " + this.value);
	*/
	 }
	
	});
					
});
});
</script></6>
<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
th{background-color:green;color:white}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>

<CENTER>
<form name='f1' action='<?print $PHP_SELF?>' onSubmit="return valid()">
<table cellspacing=1 cellpadding=1 bgcolor=green>
<tr  bgColor='c1d7bb'><td align=center>
<table cellspacing=1 cellpadding=2>
<tr bgColor='c1d7bb'><td colspan=4 align=center bgcolor='green'>
<font color='ivory'><STRONG>PURCHASE ORDER</TD></TR>
<tr><td>Order No</td><td colspan=3>
<?
$re=mysqli_query($db,"select max(ordno) from purorder  where brcode='$brcode'")or die(mysqli_error());
$ro=mysqli_fetch_row($re);
$on=$ro[0]+1;
?>

<input type=text name='orno' readonly size=6 value='<?print $on?>'></td></tr>
<tr><td>Order by </td><td><select name='ordby'>
<option>AMPL</option>
<option>Eicher</option> 
<option>PSN</option>
</select> </td>
<td>Order Date </td><td>dd  <input type=text name=d size=2 value='<?print date("d",time())?>'>
mm  <input type=text name=m size=2 value='<?print date("m",time())?>'>
yy  <input type=text name=y size=4 value='<?print date("Y",time())?>'>
</td></tr>
<tr><td>DA Number </td><td colspan=3><input type=text name='dano' size=20 onKeyDown=chkey(this)>
Supplier <select name='supplier'>
    <?
     $i=1;
     $res=mysqli_query($db,"select * from sup_master order by supname");
     while($row=mysqli_fetch_row($res))
     {
       print "\n<option value='$row[0]'>$row[1]</option>"; 
     }
    ?>
  </select>

</td></tr>
<tr><td>Config</td><td colspan=3>
<!-- <input type=text name='model' size=10>-->
<input type=text name='config'  id='configvl' size=50 class='partcode'>


 M.Code <input type=text name='mcode' id='mcode' size=40 class='partname'></td>
</tr>
<tr><td>Amount </td><td><input type=text name='amt' id='amt' size=10 style={text-align:right}></td></tr>
<tr><td> </td><td></td></tr>
<tr><td colspan=5 align=right><input type=submit value='   SAVE   ' name='save'>
 <input type=button value='   CANCEL   ' onClick=document.location='../blank.html'> </td></tr>
</table>
</form>
</table>
<br>
Purchase orders to be cleared:
<table border=1>
<th>Order<br> No <th>Ord by <th>Ord date<th>Da No <th>M Code <th>Config <th>Amount <th>Edit
<th>Del
<?
include("../test.php");
$result=mysqli_query($db,"select * from purorder where purno='-' and brcode='$brcode'")or die(mysqli_error());
$t=0;
while($row=mysqli_fetch_array($result))
{
 print "<tr>\n\n<form action='$PHP_SELF'><input type=hidden name='code' value='$row[code]'><td>$row[1]</td><td>$row[2]</td><td>".implode('-', array_reverse(explode('-', $row[3])))."</td><td>$row[4]</td><td>$row[5]</td>";
 print"<td>$row[6]</td><td align=right>".putComa($row['amount'])."</td>";
print "<td align=center><a href='editpoform.php?code=$row[0]'><img src='../img/edit.gif' border=0 </a></td>";
print "<td>\n<input type=submit value='Del' name='del'></td></form></tr>";
 $t+=$row[amount];
}
?>
<tr><td colspan=6 align=center><font color='darkbrown'>Total </td><td align=right>
<font color='darkbrown'><?print putcoma($t)?></td><td></td></tr>
</table>

