<?
session_start();
include("../opendb.php");
include("../test.php");
$purre=mysqli_query($db,"select max(tono) from purorder");
$purrow=mysqli_fetch_row($purre);
if($purrow[0]<=0)
 $tono=1;
else
$tono=($purrow[0]+1);

if($sv)
{
 $orddate=$y.'-'.$m.'-'.$d;
 $chqdate=$y1.'-'.$m1.'-'.$d1;
 $code=substr(time(),7,3).date(s);

 $refres=mysqli_query($db,"select max(psnrefno) from purorder where model='$model'");
 $refrow=mysqli_fetch_row($refres);
 if($refrow[0]<=0)
  $psnrefno=1;
 else
  $psnrefno+=1;

 $sql="insert into purorder (code, tono, psnrefno, model,config,orddate , qty,advance ,cheque,chqdate,bank,terms,billto, trans, brcode,unitprice ,note,type)values('$code','$tono','$psnrefno', '$model','$config','$orddate', '$qty', '$advance', '$cheque', '$chqdate', '$bank','$terms' , '$billto','$trans', '$brcode','$unitprice','$note','$type')";
 mysqli_query($db,$sql) or die('error:'.mysqli_error());

 if(strlen($billto)>0)
  {
    print "<script> window.open('purorderprint1.php?tono=$tono')</script>";
  }
  else
    print "<script> window.open('purorderprint2.php?tono=$tono') </script>";
 print "<script> document.location='pur_order.php'; </script>";
}

?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
tr{background-color:white}
th{background-color:steelblue;color:white}
</style>

<script>
function valid()
{
 if(document.f1.unitprice.value.length<=0)
 { alert ('Invalid Unitprice'); 
	document.f1.unitprice.focus();
	return false;
 }

}
</script>
<center>
<form name='f1' action='<?print $PHP_SELF?>' onSubmit="return valid()" method='post'> 
<table cellspacing=1 cellpadding=5 bgcolor=steelblue>
<th colspan=12 align=left><font color=white size=3><b>P U R C H A S E &nbsp; O R D E R</th>
<tr><td width=190>Telcon Order No </td><td width=150><input type=text name='tono' size=5 value='<?print $tono?>'></td>
    <td width=100>PSN Ref. NO. </td><td>[Auto Gen]</td>
</tr>
<tr><td width=150>Model </td><td width=150><select name='model'>
<select name='model'>
<?
$re=mysqli_query($db,"select * from configmaster order by model");
while($row=mysqli_fetch_array($re))
{
 print "<option>$row[1]</option>";
}
?>
</select> </td>
<td>Type </td><td><input type=text name='type'></td></tr>
<tr>
<td width=150> Configuration</td><td colspan=4 width=150>
<select name='config'>
<?
$re=mysqli_query($db,"select * from configmaster order by model");
while($row=mysqli_fetch_array($re))
{
 print "<option>$row[2]</option>";
}
?>
</select> </td></tr>
<tr><td width=150>Date </td><td width=150>dd <input type=text name='d' size=2 value='<?print date('d')?>'>
	mm <input type=text name='m' size=2 value='<?print date('m')?>'>
	yy <input type=text name='y' size=4 value='<?print date('Y')?>'>
	</td>
	<td>Qty </td><td><input type=text name=qty size=1 value='1'>
	 <font color=red>Unit Price</font><input type=text name='unitprice' size=13>
	</td>
</tr>
<tr height=1><td colspan=4 bgcolor='ivory'></td></tr>
<tr><td width=150>Advance Amt</td><td width=150><input type=text name='advance' size=12></td>
	<td>Cheque NO </td><td><input type=text name='cheque' size=22></td>
</tr>

<tr><td width=150>Cheque Date </td><td width=250>dd <input type=text name='d1' size=2 value='<?print date('d')?>'>
	mm <input type=text name='m1' size=2 value='<?print date('m')?>'>
	yy <input type=text name='y1' size=4 value='<?print date('Y')?>'>
	</td>
	<td>Bank</td><td><select name='bank'>
	<option></option>
	<option>ICICI Bank</option>
	<option>Federal Bank</option>
	</select>
	</td>
</tr>

<tr><td width=150>Taxes and Duties </td><td  colspan=3>
<select name='terms'  style="width:580">
<?
$re=mysqli_query($db,"select * from termsmaster order by 1");
while($row=mysqli_fetch_array($re))
{
 print "<option>$row[1]</option>";
}
?>
</select> </td></tr>

<tr height=1><td colspan=4 bgcolor='ivory'></td></tr>
<tr><td width=150> <font color=red> Bill To </td><td><select name='billto'>
<option></option>
<?
$re1=mysqli_query($db,"select * from branch where brstate !='Kerala' order by 1")or die(mysqli_error());
while($row=mysqli_fetch_array($re1))
{
 print "<option value='$row[0]'>$row[1]</option>";
}
?>
</select> </td>
<td><font color=red>Transport </td><td>
<select name='trans'>
<option></option>
<?
$re2=mysqli_query($db,"select * from transpmaster order by 1")or die(mysqli_error());
while($row=mysqli_fetch_array($re2))
{
 print "<option value='$row[0]'>$row[2]</option>";
}
?>
</select> </td>

</tr>
<tr><td width=150>Payment Terms</td><td colspan=5><textarea rows=5 cols=80 name='note'></textarea></td></tr>
<tr height=1><td colspan=4 bgcolor='steelblue'>
<input type='submit' value='   Save   ' name='sv'> <input type=button value='Cancel' onClick=document.location='../blank.html'>
</td></tr>
</table>
</form>

<?
$result=mysqli_query($db,"select * from purorder where pinvoice='-' order by orddate");
if(mysqli_num_rows($result)>0)
{
 $i=1;
 print "<table border=0 cellspacing=1 cellpadding=1 bgcolor='silver' width=100%><th>No. <th> Tel No <th>PSN NO <th>Model <th>Config<th>Date <th>Bill to ";
 print "<th>Transp<th>Edit<th>Del";

while($row=mysqli_fetch_array($result))
{
  print "\n<tr><form action='edordelpurorder.php'><input type=hidden name='tono' value='$row[tono]'>";
  print "<td>".$i++."</td><td align=center>$row[tono]</td>";
  print "<td align=center>$row[psnrefno]</td><td>$row[model]</td>";
  $brres=mysqli_query($db,"select brname from branch where brcode='$row[billto]'");
   $brrow=mysqli_fetch_row($brres);
 print "<td>$row[config]</td><td align=center>".datetohtml($row[orddate])."</td><td>$brrow[0]</td>";
  $trres=mysqli_query($db,"select trname from transpmaster where code='$row[trans]'")or die(mysqli_error());
   $trrow=mysqli_fetch_row($trres);
 print "<td>$trrow[0]</td>\n<td align=center><input type=submit value='Edit' name='ed'></td>";
 print "<td align=center><input type=submit value='Del' name='del'></form></tr>";
}
}
?>
</table>

