<?
session_start();
include("../opendb.php");
include("../test.php");
if($save)
{
$ch=mysqli_query($db,"select * from partmaster where pname='$pname'");

if(mysqli_num_rows($ch)>0)
{
print "<script> alert('Duplicate Part Name'); document.location='partmaster.php'</script>";
return;
}
$code=substr($pname,0,3).substr(time(),7,3).date("s"); 
 $code=str_replace(' ','-',$code);
 
 $crdate=$y.'-'.$m.'-'.$d;
 $lcdate=$y1.'-'.$m1.'-'.$d1;

$sql="insert into partmaster values('$pcode','$pname','$bunit','$pcat','$acti','$crdate',
'$lcdate','$subgp','$minod','$snorm','$code')";
//mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error());

print "<script> document.location='partmaster.php'</script>";
return;
} // end save


//$re=mysqli_query($db,"select * from configmaster");
//$row=mysqli_fetch_array($re);
?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
th{background-color:steelblue;color:white}
</style>
<script>
function valid()
{
 
 if(document.f1.tax.value.length<=0)
 {
   alert("Invalid Tax.");
   document.f1.tax.focus();
   return false;
 }

}
</script>

<center>
<table cellspacing=1 cellpadding=1 bgcolor=black>
<tr><td align=center bgcolor='white'>
<form name='f1' action='<?print $PHP_SELF?>' method='post' onSubmit="return valid()">
<table cellspacing=1 cellpadding=3 >
<th colspan=4><font size=3>PART MASTER</th>
<tr><td>Part Code</td><td><input type=text name='pcode'  size=20></td>
    <td>Part Name</td><td><input type=text name='pname'  size=20></td></tr>
<tr><td>Base Unit </td><td><input type=text name='bunit'  size=20></td>
    <td>Part Category</td><td><input type=text name='pcat'  size=20></td></tr>
<tr><td>Activated</td><td><select name='acti'><option>A</option><option>D</option></select></td>
    <td>Created On</td><td>dd <input type=text name='d' size=4> mm <input type=text name='m' size=4> yyyy <input type=text name='y' size=5></td></tr>

<tr><td>Last Changed</td><td>dd <input type=text name='d1' size=4> mm <input type=text name='m1' size=4> yyyy <input type=text name='y1' size=5></td>
<td>Subgroup</td><td><select name='subgp'  size=1>
<option></option>
<?
$g=mysqli_query($db,"select * from subgroupmaster");
while($grow=mysqli_fetch_array($g))
{
print "<option>$grow[subname]</option>";
}
?>
</select>
</td>
    </tr>
<tr><td>Minimum Order Quantity</td><td><input type=text name='minod'  size=20></td>
    <td>Stocking Norm</td><td><input type=text name='snorm'  size=20></td></tr>

<th colspan=4><input type=submit value='Save' name='save'>
&nbsp;<input type=submit value='Search' name='sea'>
</td></tr>
</table>
</form>
</td></tr>
</table>

<script>
 function delfun(x)
 {
  if (confirm('do you want to delete '+x+' ?'))
   document.location='delpart.php?code='+x;
 }
  
</script>
<?
if($sea)
{
  if(strlen($pcode)>0)
   $sql="select * from partmaster where partcode like '%$pcode%'";
  else if(strlen(pname)>0)
   $sql="select * from partmaster where partname like '%$pname%'";
 $res=mysqli_query($db,$sql) or die(mysqli_error());
 if(mysqli_num_rows($res)<=0)
 {
  print "<script> alert('Record does not exist'); </script>";
 }
 print "<br><table border=0 cellspacing=1 cellpadding=3 bgcolor='lightsteelblue'>";
 print "<th>Part Code <th>Part Name <th>Group <th>Sub Group <th>Model <th> Re_Level <th>Active";
 print "<th>Blk Pur <th>Unit";
 while($row=mysqli_fetch_array($res))
 {
  print "<tr bgcolor='aliceblue'><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td>";
  print "<td>$row[5]</td><td>$row[6]</td><td>$row[7]</td><td>$row[7]</td>";
 }
 print "</table>";

} // end sea
?>
