Add Stocks / Purchasings


To add stocks to the system, go to Stocks >> Add Stocks. First you need to find the product you need to add stocks to the system. You can directly enter the item code and search for the item or you can type the name of the product, while typing drop down list of matching products shows and from there you can select the desired item. To show the product suggestions, we used ajax functions. These ajax functions are in the files of "ajax" folder. There are two files handling product suggestions process;


  1. show_items.php file
  2. show_items_js.js file
This is the source code of show_items.php file that handles ajax request.

<table width="96%" cellpadding="2px" cellspacing="2px">
<?php
 include("../inc/db_connect.php");
 $str = $_GET['str'];
 if($str != ""){
  $q = mysql_query("SELECT Item_Name FROM items WHERE Item_Name LIKE '$str%'");
  $n=1;
  while($r = mysql_fetch_assoc($q)){
   echo "<tr><td id=\"sh{$n}\"><a href=\"javascript:selectItem('{$r['Item_Name']}')\" onclick=\"setFocus('submit_1')\">{$r['Item_Name']}</a></td></tr>";
   if($n==1){
    $n=2;
   }else{
    $n=1;
   }
  }
 }
 mysql_close($conn);
?>
</table>

show_items_js is the javascript that calls to the server php file.

// JavaScript Document
function showItems(str){
 // creating XMLHttpRequest Object
 if (window.XMLHttpRequest){
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
 }else{
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
  // When the reposponse from server is ready
  xmlhttp.onreadystatechange=function(){
   if (xmlhttp.readyState==4 && xmlhttp.status==200){
    document.getElementById("item_list").innerHTML=xmlhttp.responseText;
   }
  }
   // Sending request to the server
   xmlhttp.open("GET","ajax/show_items.php?str="+str,true);
   xmlhttp.send();
}
function showItemList(){
 document.getElementById("item_list").style.visibility="visible";
}
function hideItemList(){
 var t = setTimeout("document.getElementById('item_list').style.visibility='hidden'",500);
}
function selectItem(str){
 document.getElementById("item_name").value=str;
}
function setFocus(str){
 document.getElementById(str).focus();
}

These are the old but original methods of ajax calls. But things are now more easier with using of JQUERY library. Since we are not using JQuery with this project and for educational purpose, this script explain all about how ajax works.

Following the source code for add_stocks.php file:

<?php include("inc/page_header.php"); ?>
  <script type="text/javascript">
   function getDisprice(dis){
    var normal_price = document.getElementById("ret_price").value;
    var discounted_price = (normal_price/100)*(100-dis);
    document.getElementById("pur_price").value = discounted_price.toFixed(2);
   }
  </script>

<?php
 if(check('Add Stocks') != 'Valid'){
  header("Location: sys_home.php?pre=error");
  ob_end_flush();
 }
 /* +++++++++++++++ Form 1 (Submit 1) Handling ++++++++++++++++++++++ */
  if(isset($_POST['submit_1'])){
   $item_code = ''; //$_POST['item_code'];
   $part_no = $_POST['part_no'];
   $item_name = $_POST['item_name'];
   if($item_code == '' && $part_no == '' && $item_name == ''){
    $error = 'Please enter Item Code OR Part No OR Item Name to start!';
   }else{
    if($item_code != ''){
     // search by Item Code
     $q = mysql_query("SELECT * FROM items WHERE Item_Code = '$item_code'");
    }else if($part_no != ''){
     // search by Part No
     $q = mysql_query("SELECT * FROM items WHERE Item_Part_No = '$part_no'");
    }else{
     // search by Item Name
     $q = mysql_query("SELECT * FROM items WHERE Item_Name = '$item_name'");
    }
    // check item found in the inventory
    if(mysql_num_rows($q) >0){
     // item found and set variables for item code, part no and item name
     $r = mysql_fetch_assoc($q);
     $itemcode = $r['Item_Code'];
     $partno = $r['Item_Part_No'];
     $itemname = $r['Item_Name'];
     // get the current stock details
     $q = mysql_query("SELECT SUM(Stock_Qty), Stock_Purchase_Price, Stock_Retail_Price, Stock_Discount FROM stocks WHERE Stock_Item = '$itemcode' ORDER BY Stock_ID DESC");
     $r = mysql_fetch_assoc($q);
     $current_stock = $r['SUM(Stock_Qty)'];
     $purchase_price = $r['Stock_Purchase_Price'];
     $retail_price = $r['Stock_Retail_Price'];
     $stock_discount = $r['Stock_Discount'];
     // variable to hide form 1 and show form 2
     $form1 = true;
    }else{
     // item not found and create error message
     $error = 'Item not found in the inventory!';
    }
   }
  }
 /* +++++++++++++++ // Form 1 Handling ++++++++++++++++++++++++++++++ */
 /* +++++++++++++++  Form 2 Handling ++++++++++++++++++++++++++++++ */
  if(isset($_POST['submit_2'])){
   $item_code = $_POST['item_code'];
   $part_no = $_POST['part_no'];
   $item_name = $_POST['item_name'];
   $pur_price = $_POST['pur_price'];
   $ret_price = $_POST['ret_price'];
   $stock_discount = $_POST['discount'];
   $qty = $_POST['qty'];
   // first check for existing record for the same purchase price
   $q = mysql_query("SELECT * FROM stocks WHERE Stock_Item = '$item_code' AND Stock_Purchase_Price = '$pur_price'");
   if(mysql_num_rows($q) > 0){
    // update the existing stock record
    // get the curent Stock ID
    $r = mysql_fetch_assoc($q);
    $stock_id = $r['Stock_ID'];
    mysql_query("UPDATE stocks SET Stock_Date = '$today', Stock_Purchase_Price = '$pur_price', Stock_Qty = (Stock_Qty + $qty), Stock_Discount = '$stock_discount' WHERE Stock_ID = '$stock_id'");
    // now update the retail price of all stock records for the current item
    mysql_query("UPDATE stocks SET Stock_Retail_Price = '$ret_price' WHERE Stock_Item = '$item_code'");
    $noerror = 'Stock has been updated!';
   }else{
    // add a new stock record for the current purchase price
    mysql_query("INSERT INTO stocks (Stock_Date, Stock_Item, Stock_Purchase_Price, Stock_Retail_Price, Stock_Qty, Stock_Discount) VALUES ('$today', '$item_code', '$pur_price', '$ret_price', '$qty', '$stock_discount')");
    // now update the retail price of all stock records for the current item
    mysql_query("UPDATE stocks SET Stock_Retail_Price = '$ret_price' WHERE Stock_Item = '$item_code'");
    $noerror = 'Stock has been updated2!';
   }
  }
 /* +++++++++++++++ // Form 2 Handling ++++++++++++++++++++++++++++++ */
?>
<script type="text/javascript" src="ajax/show_items_js.js"></script>
 <div class="wrapper">

  <div class="s60">
  <div class="boxtitle">Add Stocks</div>
  <?php if(!isset($form1)){ // show form 1 ?>
  <p>Please enter <u>Item Code</u> <strong>OR</strong> <u>Item Name</u> and hit the enter key!</p>
   <div style="border:2px solid #6484F0; width:90%; margin:10px auto 10px auto; padding:5px; ">
    <form action="add_stocks.php" method="post">
     <table>
      <tr><td>Item Code:</td><td><input type="text" size="20" name="part_no" /></td></tr>
      <tr><td>Item Name:</td><td><input id="item_name" type="text" size="40" name="item_name" onKeyUp="showItems(this.value)" onFocus="showItemList()" onBlur="hideItemList()" /> <input id="submit_1" type="submit" name="submit_1" class="btn" value="&raquo;" /><br />
       <div id="item_list" style="position:absolute; z-index:2; width:264px; background-color:#FFFFCC; border:1px solid #999999; overflow:scroll; min-height:1px; visibility:hidden; "></div>
      </td></tr>
     </table>
    </form>
   </div>
  <?php }else{ // show form 2 ?>
   <p>Please enter new stock details.</p>
   <form action="add_stocks.php" method="post">
    <table width="100%">
     <tr><td>&nbsp;Item Code:</td><td><input type="text" size="20" value="<?php echo $partno; ?>" disabled /></td></tr>
     <tr id="sh6"><td>&nbsp;Item Name:</td><td><input type="text" size="40" value="<?php echo $itemname; ?>" disabled /></td></tr>
     <tr><td>&nbsp;Current Stock:</td><td><input type="text" size="10" value="<?php echo $current_stock; ?>" disabled /></td></tr>
     <tr><td>&nbsp;Normal Price Rs.:</td><td><input type="text" id = "ret_price" name="ret_price" size="10" value="<?php echo $retail_price; ?>" /> *</td></tr>
     <tr id="sh6"><td>&nbsp;Discount %:</td><td><input type="text" id="discount" onKeyup="getDisprice(this.value)" name="discount" size="10" value="<?php echo $stock_discount; ?>" /></td></tr>
     <tr id="sh6"><td>&nbsp;Discounted Price Rs.:</td><td><input type="text" id = "pur_price" name="pur_price" size="10" value="<?php echo $purchase_price; ?>" /> *</td></tr>     
     <tr id="sh6"><td>&nbsp;Qty:</td><td><input type="text" name="qty" size="10" /> *</td></tr>
     <tr><td>&nbsp;</td><td><input type="submit" name="submit_2" value="Add to Stock" class="btn" /></td></tr>
    </table>
    <input type="hidden" name="item_code" value="<?php echo $itemcode; ?>" />
    <input type="hidden" name="part_no" value="<?php echo $partno; ?>" />
    <input type="hidden" name="item_name" value="<?php echo $itemname; ?>" />
   </form>
  <?php } // end of form visibility handle ?>
  </div>
  <div class="s40">
   <p><strong>Add New Stock<br>
   </strong>Use this form to add new stocks to the system.</p>
   <?php
    if(isset($error)){ echo "<div class='errordiv'>{$error}</div>"; }
    if(isset($noerror)){ echo "<div class='noerrordiv'>{$noerror}</div>"; }
   ?>
  </div>
  
 </div>
 
<?php include("inc/page_footer.php"); ?>

Comments