Handle Return Stocks


Some times for many reasons, stocks returns from sites. When stock returns from sites "return stocks" need to be either add to the main stock or marked as disposed. When stock return following form handles these stocks.


returns.php file handles the return process.

<?php include("inc/page_header.php"); ?>
<?php
        if(check('Add Stocks') != 'Valid'){
                header("Location: sys_home.php?pre=error");
                ob_end_flush();
        }
        /* +++++++++++++++ Form 1 (Submit 1) Handling ++++++++++++++++++++++ */
                if(isset($_GET['submit_1'])){
                        $item_code = $_GET['item_code'];
                        $part_no = $_GET['part_no'];
                        $item_name = $_GET['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 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'];
                                        // 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'];
                        $qty = $_POST['qty'];
      $return_value = $pur_price * $qty;
                        $action = $_POST['action'];
                        $reason = $_POST['reason'];
                        $site = $_POST['site'];
                        $segment = $_POST['segment'];
                        // validate
                        if($qty == '' || $qty == 0 || $site == 0 || $segment == 0){
                                $error = 'Please enter all required details!';
                        }else{
       // validate stock issue for this item, this site and for this section of this customer
       // site = customer id
       $rq = mysql_query("SELECT SUM(SI_Qty) FROM sales, sales_items WHERE SI_Item = '$item_code' AND SI_SG_ID = '$segment' AND SI_Sale_ID = Sale_ID AND Sale_Customer = '$site'");
       $rr = mysql_fetch_assoc($rq);
       $issued_total = $rr['SUM(SI_Qty)'];
       if($qty <= $issued_total){
                                // add record to returns
                                mysql_query("INSERT INTO returns (Return_Date, Return_Item, Return_Qty, Return_Value, Return_Reason, Return_Action, Return_Site, Return_Segment) VALUES ('$today', '$item_code', '$qty', '$return_value', '$reason', '$action', '$site', '$segment')");
                                $noerror = 'Returned stock has been updated!';
                                if($action == 'add_stock'){
                                        // 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_Qty = (Stock_Qty + $qty) WHERE Stock_ID = '$stock_id'");
                                                // update the stock issue record - deduct the qty by adding a negative qty record
                                                //mysql_query("UPDATE sales_items
                                                $noerror = 'Returned 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) VALUES ('$today', '$item_code', '$pur_price', '$ret_price', '$qty')");
                                                // 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 = 'Returned Stock has been updated!';
                                        }
                                }
       }else{
        $error = $issued_total."Returned item details is not matching with any stock issued records. Please verify the customer / site, item and segment details are correct.";
       }
                        }
                }
        /* +++++++++++++++ // Form 2 Handling ++++++++++++++++++++++++++++++ */
?>
<script type="text/javascript" src="ajax/show_items_js.js"></script>
        <div class="wrapper">

                <div class="s60">
                <div class="boxtitle">Returned 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="returns.php" method="get">
                                        <table>
                                                <input type="hidden" size="8" name="item_code" value="" />
                                                <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 returned stock details.</p>
                        <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
                                <table width="100%">
                                        <!--<tr id="sh6"><td>&nbsp;Item Code:</td><td><input type="text" size="8" value="<?php echo $itemcode; ?>" disabled /></td></tr>-->
                                        <tr><td>&nbsp;Item Code:</td><td><input type="text" size="20" value="<?php echo $partno; ?>" readonly="true" /></td></tr>
                                        <tr id="sh6"><td>&nbsp;Item Name:</td><td><input type="text" size="40" value="<?php echo $itemname; ?>" readonly="true" /></td></tr>
                                        <input type="hidden" name="pur_price" size="10" value="<?php echo $purchase_price; ?>" readonly="true" />
                                        <tr id="sh6"><td>&nbsp;Normal Price Rs.:</td><td><input type="text" name="ret_price" size="10" value="<?php echo $retail_price; ?>" readonly="true" /></td></tr>
                                        <tr><td>&nbsp;Qty:</td><td><input type="text" name="qty" size="10" /> *</td></tr>
                                        <tr id="sh6"><td>&nbsp;Action:</td><td><input type="radio" name="action" value="add_stock" checked /> Add to stock <input type="radio" name="action" value="dispose" /> Dispose</td></tr>
                                        <tr><td>&nbsp;Site:</td><td><select name="site" size="1">
                                                <option value="0" selected>Please select...</option>
                                                <?php
                                                        $query = mysql_query("SELECT * FROM customers");
                                                        while($row = mysql_fetch_assoc($query)){
                                                                echo "<option value=\"{$row['Customer_ID']}\">{$row['Customer_Name']}</option>";
                                                        }
                                                ?>
                                        </select> *</td></tr>
                                        <tr><td>&nbsp;Segment:</td><td><select name="segment" size="1">
                                                <option value="0" selected>Please select...</option>
                                                <?php
                                                        $query = mysql_query("SELECT * FROM site_segments");
                                                        while($row = mysql_fetch_assoc($query)){
                                                                echo "<option value=\"{$row['SG_ID']}\">{$row['SG_Name']}</option>";
                                                        }
                                                ?>
                                        </select> *</td></tr>
                                        <tr id="sh6"><td>&nbsp;Reason:</td><td><input type="text" name="reason" size="55" /></td></tr>
                                        <tr><td>&nbsp;</td><td><input type="submit" name="submit_2" value="Update Returns" 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