<?
session_start();
//include_once('includes/top.php');
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
?>
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Partmaster</title>

<script type="text/javascript" src="js/jquery.js"></script>
<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<link type="text/css" href="css/form.css" rel="stylesheet" />


<script type="text/javascript">
$(document).ready(function(){

    $("select.grp").change(function(){

        var selectedGrp = $(".grp option:selected").val();

        $.ajax({

            type: "POST",

            url: "selectsubgrp.php",

            data: { grp : selectedGrp }

        }).done(function(data){

            $("#response").html(data);

        });

    });

});


</script>

<!-- js code below for active enter with textfield-->


<script type="text/javascript">

/***********************************************
* Disable "Enter" key in Form script
* This notice must stay intact for use
***********************************************/

function handleEnter (field, event) {
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13) {
      var i;
      for (i = 0; i < field.form.elements.length; i++)
        if (field == field.form.elements[i])
          break;
      i = (i + 1) % field.form.elements.length;
      field.form.elements[i].focus();
      return false;
    }
    else
    return true;
  }








</script>

<style>
#txt{
font-family: Arial, Helvetica;
background-color: #F2F2F2;
}
table.form{
font-weight: normal;border:1px;
}
</style>
<style>
.cont{
      background-color:#fff;
      //background:url(images/bg-inner.jpg) #e9e0c3 repeat-x;
      width: 100%;position: absolute ;
      height:99%;
      /* 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;

      }


#pg{
       text-decoration:none;
}
#pg button{
       background-color:#3b5998;
}

</style>



<body bgcolor="#A9F5A9">
<div class='cont' style="background-color:#E0F8E0" >



<?
include('includes/top.php');

?>
<font style="font-family: 'Trebuchet MS',Verdana,Helvetica,sans-serif; font-size:15px; color:white;">  &nbsp;&nbsp;Master >> Item Master</font>
<?
if(isset($_POST['save']))
{
  $dt=date("Y-m-d");
  $pcode=$_REQUEST['pcode'];
        $pcode=trim($pcode);
  $pname=$_REQUEST['pname'];
  $grp=$_REQUEST['grp'];
  $subgp=$_REQUEST['subgrp'];
  $acti=$_REQUEST['acti'];
  $bunit=$_REQUEST['bunit'];
  $rebtbl=$_REQUEST['rebtbl'];
  $rbtvl=$_REQUEST['rbtvl'];
  $reduction=$_REQUEST['reduction'];
  $prate=$_REQUEST['prate'];
  $srate=$_REQUEST['srate'];
  $opening=$_REQUEST['opening'];
  $location=$_REQUEST['location'];
  $hsn=$_REQUEST['hsn'];
  $taxp=$_REQUEST['taxp'];
  $qry=mysqli_query($db,"select * from partmaster where partcode='$pcode'");
   if(mysqli_num_rows($qry)>0)
     {
         print "<script> alert('Could not save: Duplicate Part code'); document.location='partmaster.php'</script>";
     }
  else
    {
      $sql1="INSERT INTO partmaster(partcode,partname,group1,unit,hsncode,tax_sales) VALUES ('$pcode','$pname','$grp','$bunit','$hsn','$taxp')";
      mysqli_query($db,$sql1) or die('Insert Err:'.mysqli_error($db));
      $sql2="insert into spareprmast(partcode,effectfrom,effectto,ndpcost,lstprice,brcode) values('$pcode','$dt','2099-12-31','$prate','$srate','$brcode')";

      mysqli_query($db,$sql2) or die('Insert Err1:'.mysqli_error($db));
      if($opening>0)
        {
          mysqli_query($db,"insert into sparestock(itemcode,itemname,opstk,curstk,brcode,location,item_grp,item_subgrp,item_unit,item_active) values('$pcode','$pname','$opening','$opening','$brcode','$location','$grp','$subgp','$acti','$bunit') ");
        }

print "<script> document.location='partmaster.php'</script>";

return;
}
} // end save


?>
<style>
th{background-color:steelblue;color:white}
</style>
<script>
function valid()
{

 if(document.f1.pcode.value.length<=0)
 {
   alert("Invalid Itemcode.");
   document.f1.pcode.focus();
   return false;
 }

if(document.f1.pname.value.length<=0)
 {
   alert("Invalid ItemName.");
   document.f1.pname.focus();
   return false;
 }
/*if(document.f1.grp.value=='--select--')
  {
    alert("Invalid Group.");
    document.f1.grp.focus();
    return false;

  }
if(document.f1.subgrp.value=='--select--')
  {
    alert("Invalid SubGroup.");
    document.f1.subgrp.focus();
    return false;

  }
*/
if(document.f1.srate.value.length<=0)
 {
   alert("Invalid SaleRate.");
   document.f1.srate.focus();
   return false;
 }
if(document.f1.prate.value.length<=0)
 {
   alert("Invalid Purchase Rate.");
   document.f1.prate.focus();
   return false;
 }



}
</script>
<script type="text/javascript">

/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function handleEnter (field, event) {
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13) {
      var i;
      for (i = 0; i < field.form.elements.length; i++)
        if (field == field.form.elements[i])
          break;
      i = (i + 1) % field.form.elements.length;
      field.form.elements[i].focus();
      return false;
    }
    else
    return true;
  }


</script>
<?
$xx=$_GET['id']+1;
?>
<body>
<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 class='form'>
<th colspan=4><font size=3>ITEM MASTER</th>
<tr><td>Item Code</td><td><input type=text name='pcode'  size=20 ></td>
<td>Item Name</td><td><input type=text name='pname'  size=20 ></td></tr>
<tr>
    <tr><td id="dptd"></td><tr>
<td>Group</td><td><select name='grp'  >
<option>SPARE PARTS</option>
<option>LUBRICANT</option>
<option>USED OIL</option>
<option>BARREL</option>
<option>SCRAP</option>
<option>ASSET</option>
<option>OTHERS</option>
<option>VEHICLE</option>
<option>LABOUR</option>
</select>
</td>

<td>HSN/SAC</td><td id="response"><input type= text name='hsn' >
</td>
</tr>

<tr><td>Unit</td><td><select name='bunit' >
<option>Nos</option>
<option>Ltr</option>
<option>Others</option>
</select></td>

<td>Location</td><td>
<input type=text id='location' name='location' placeholder="Enter location" size=20>
</td>
</tr>
<tr><td>Opening</td><td><input type=text id='opening' value='0' name='opening' size=20></td>
<td>Tax</td><td><select name='taxp'  >
<option>18.00</option>
<option>12.00</option>
<option>5.00</option>

</select>
</td>
</tr>
<tr><td>PurchaseRate</td><td><input type=text name='prate'  size=10></td><td>SaleRate</td><td><input type=text name='srate'  size=10 onKeyPress="return handleEnter(this, event)"></td></tr>


<th colspan=4><input type=submit value='Save' name='save'>
    <div id="strtxt"></div>
</table>
</form>
</td></tr>
</table>
<?
$sql="select * from partmaster ";
 $res=mysqli_query($db,$sql) or die(mysqli_error($db));
 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>HSN/SAC <th>Tax % <th>NDP<th> LIST<th>Action";
 //print "<th>Blk Pur <th>Unit";
 while($row=mysqli_fetch_array($res))
 {
     $sqlpr="select * from spareprmast where partcode='$row[0]' order by effectfrom desc limit 1";
 $respr=mysqli_query($db,$sqlpr) or die(mysqli_error($db));
$rowpr=mysqli_fetch_row($respr);
print"<form name='form1' id='form1' method='POST' Action='editpart.php?code= $row[0]'>";
  print "<tr bgcolor='aliceblue'><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[23]</td><td>$row[9]</td><td>$rowpr[3]</td><td>$rowpr[4]</td><td align=center><input type=submit value='Edit' name='ed' ></td></tr>";
  print"</form>";
  //print "<td>$row[5]</td><td>$row[6]</td><td>$row[7]</td><td>$row[7]</td>";
 }
 print "</table>";
?>
<script type="text/javascript">
function showrebtvl(str)
{


  if(str=='Yes')
  {

   document.getElementById("rbtvl").style.visibility = "visible";



  }
    else
  {

  document.getElementById("rbtvl").style.visibility ="hidden";



  }


}
</script>
<script type="text/javascript">
function chkthis(str)
{
if(document.f1.pcode.value!='')
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("strtxt").innerHTML=xmlhttp.responseText;

  //document.f1.batta.value=document.getElementById("bat1").value;

  var dupvl=document.getElementById("dupitm").value;
  if(dupvl==1)
      {
          alert('ItemCode already exists!');
          document.f1.pcode.focus();
          document.f1.pcode.value='';
            return false;
      }

    }
  }

xmlhttp.open("GET","chkitm.php?itcd="+str,true);
xmlhttp.send();

}

}
</script>