Changes in average material cost from bom_edit to work_orders_db.
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Tue, 4 Mar 2008 15:31:32 +0000 (15:31 +0000)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Tue, 4 Mar 2008 15:31:32 +0000 (15:31 +0000)
CHANGELOG.txt
manufacturing/includes/db/work_orders_db.inc
manufacturing/includes/db/work_orders_quick_db.inc
manufacturing/manage/bom_edit.php
manufacturing/work_order_entry.php
purchasing/includes/db/grn_db.inc

index 8c997c071319872a8effae6ba28fb414d4cc90a4..d69f305d8e6404a97f566ae5841f8dc852028ab7 100644 (file)
@@ -25,6 +25,9 @@ $ -> Affected files
 $ /inventory/cost_update.php
   /purchasing/includes/db/grn_db.inc and
   /manufacturing/manage/bom_edit.php. 
+  /manufacturing/includes/db/work_orders_db.inc
+  /manufacturing/includes/db/work_orders_quick_db.inc
+  /manufacturing/work_order_entry.php
   
 21-Feb-2008 Joe Hunt
 + Added Print for documents in /admin/view_print_transaction.php (according to our new routines).
index 81aca2bdc4db0832875839c87f813d3e2a84170a..6b2064b2d7ea7fbe7cc73c0299e314fa155766a9 100644 (file)
 
 //--------------------------------------------------------------------------------------
 
-function add_work_order($wo_ref, $loc_code, $units_reqd, $stock_id, 
+function add_material_cost($stock_id, $qty, $date_)
+{
+       $m_cost = 0;
+    $result = get_bom($stock_id);
+       while ($bom_item = db_fetch($result))
+       {
+               $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = '".$bom_item['component']."'";
+               $res = db_query($sql);
+               $myrow = db_fetch($res);
+               $m_cost += ($bom_item['quantity'] * $myrow['material_cost']);
+       }
+       $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = '$stock_id'";
+       $result = db_query($sql);
+       $myrow = db_fetch($result);
+       $material_cost =  $myrow['material_cost'];
+       $qoh = get_qoh_on_date($stock_id, null, $date_);
+       if ($qoh + $qty <= 0)
+               $material_cost = 0;
+       else
+               $material_cost = ($qoh * $material_cost + $qty * $m_cost) /     ($qoh + $qty);
+       $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=$material_cost
+               WHERE stock_id='$stock_id'";
+       db_query($sql,"The cost details for the inventory item could not be updated");
+}
+
+function add_work_order($wo_ref, $loc_code, $units_reqd, $stock_id,
        $type, $date_, $required_by, $costs, $memo_)
 {
        if (!($type == wo_types::advanced()))
                return add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $costs, $memo_);
-                
+
        begin_transaction();
-       
+
+       add_material_cost($stock_id, $units_reqd, $date_);
+
        $date = date2sql($date_);
        $required = date2sql($required_by);
-       
-       $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, stock_id, 
-               type, date_, required_by) 
-       VALUES ('$wo_ref', '$loc_code', $units_reqd, '$stock_id', 
+
+       $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, stock_id,
+               type, date_, required_by)
+       VALUES ('$wo_ref', '$loc_code', $units_reqd, '$stock_id',
                $type, '$date', '$required')";
        db_query($sql, "could not add work order");
-       
+
        $woid = db_insert_id();
-       
-       add_comments(systypes::work_order(), $woid, $required_by, $memo_);      
-       
-       add_forms_for_sys_type(systypes::work_order(), $woid);  
-       
-       references::save_last($wo_ref, systypes::work_order());         
-       
-       commit_transaction();   
-       
-       return $woid;   
+
+       add_comments(systypes::work_order(), $woid, $required_by, $memo_);
+
+       add_forms_for_sys_type(systypes::work_order(), $woid);
+
+       references::save_last($wo_ref, systypes::work_order());
+
+       commit_transaction();
+
+       return $woid;
 }
 
 //--------------------------------------------------------------------------------------
 
-function update_work_order($woid, $loc_code, $units_reqd, $stock_id, 
+function update_work_order($woid, $loc_code, $units_reqd, $stock_id,
                                        $date_, $required_by, $memo_)
 {
        begin_transaction();
-               
+
+       add_material_cost($_POST['old_stk_id'], -$_POST['old_qty'], $date_);
+       add_material_cost($stock_id, $units_reqd, $date_);
+
        $date = date2sql($date_);
        $required = date2sql($required_by);
-               
-       $sql = "UPDATE ".TB_PREF."workorders SET loc_code='$loc_code', 
+
+       $sql = "UPDATE ".TB_PREF."workorders SET loc_code='$loc_code',
                units_reqd=$units_reqd, stock_id='$stock_id',
                required_by='$required',
                date_='$date'
-               WHERE id = $woid";      
+               WHERE id = $woid";
 
        db_query($sql, "could not update work order");
-       
-       update_comments(systypes::work_order(), $woid, null, $memo_);      
 
-       commit_transaction();                                   
+       update_comments(systypes::work_order(), $woid, null, $memo_);
+
+       commit_transaction();
 }
 
 function delete_work_order($woid)
 {
        begin_transaction();
-               
+
+       add_material_cost($_POST['stock_id'], -$_POST['quantity'], $_POST['date_']);
+
        // delete the work order requirements
        delete_wo_requirements($woid);
-       
+
        // delete the actual work order
        $sql = "DELETE FROM ".TB_PREF."workorders WHERE id=$woid";
        db_query($sql,"The work order could not be deleted");
-       
+
        delete_comments(systypes::work_order(), $woid);
-       
-       delete_forms_for_systype(systypes::work_order(), $woid);        
-       
-       commit_transaction();           
+
+       delete_forms_for_systype(systypes::work_order(), $woid);
+
+       commit_transaction();
 }
 
 //--------------------------------------------------------------------------------------
@@ -79,18 +111,18 @@ function get_work_order($woid, $allow_null=false)
 {
     $sql = "SELECT ".TB_PREF."workorders.*, ".TB_PREF."stock_master.description As StockItemName,
                ".TB_PREF."locations.location_name
-               FROM ".TB_PREF."workorders, ".TB_PREF."stock_master, ".TB_PREF."locations 
-               WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."workorders.stock_id 
-               AND     ".TB_PREF."locations.loc_code=".TB_PREF."workorders.loc_code 
+               FROM ".TB_PREF."workorders, ".TB_PREF."stock_master, ".TB_PREF."locations
+               WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."workorders.stock_id
+               AND     ".TB_PREF."locations.loc_code=".TB_PREF."workorders.loc_code
                AND ".TB_PREF."workorders.id=$woid
                GROUP BY ".TB_PREF."workorders.id";
-                                               
+
        $result = db_query($sql, "The work order issues could not be retrieved");
-       
+
        if (!$allow_null && db_num_rows($result) == 0)
                display_db_error("Could not find work order $workOrder", $sql);
-       
-       return db_fetch($result);       
+
+       return db_fetch($result);
 }
 
 //--------------------------------------------------------------------------------------
@@ -99,7 +131,7 @@ function work_order_has_productions($woid)
 {
        $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_manufacture WHERE workorder_id=$woid";
        $result = db_query($sql, "query work order for productions");
-    
+
        $myrow = db_fetch_row($result);
        return ($myrow[0] > 0);
 }
@@ -111,7 +143,7 @@ function work_order_has_issues($woid)
 {
        $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_issues WHERE workorder_id=$woid";
        $result = db_query($sql, "query work order for issues");
-    
+
        $myrow = db_fetch_row($result);
        return ($myrow[0] > 0);
 }
@@ -129,30 +161,30 @@ function work_order_has_payments($woid)
 
 function release_work_order($woid, $releaseDate, $memo_)
 {
-       begin_transaction();            
-       
-       $myrow = get_work_order($woid); 
+       begin_transaction();
+
+       $myrow = get_work_order($woid);
        $stock_id = $myrow["stock_id"];
-               
+
        $date = date2sql($releaseDate);
-               
-       $sql = "UPDATE ".TB_PREF."workorders SET released_date='$date', 
-               released=1 WHERE id = $woid";   
+
+       $sql = "UPDATE ".TB_PREF."workorders SET released_date='$date',
+               released=1 WHERE id = $woid";
        db_query($sql, "could not release work order");
-       
+
        // create Work Order Requirements based on the bom
        create_wo_requirements($woid, $stock_id);
-       
-       add_comments(systypes::work_order(), $woid, $releaseDate, $memo_);      
 
-       commit_transaction();   
+       add_comments(systypes::work_order(), $woid, $releaseDate, $memo_);
+
+       commit_transaction();
 }
 
 //--------------------------------------------------------------------------------------
 
 function close_work_order($woid)
 {
-       $sql = "UPDATE ".TB_PREF."workorders SET closed=1 WHERE id = $woid";    
+       $sql = "UPDATE ".TB_PREF."workorders SET closed=1 WHERE id = $woid";
        db_query($sql, "could not close work order");
 }
 
@@ -160,10 +192,10 @@ function close_work_order($woid)
 
 function work_order_is_closed($woid)
 {
-       $sql = "SELECT closed FROM ".TB_PREF."workorders WHERE id = $woid";     
+       $sql = "SELECT closed FROM ".TB_PREF."workorders WHERE id = $woid";
        $result = db_query($sql, "could not query work order");
        $row = db_fetch_row($result);
-       return ($row[0] > 0);   
+       return ($row[0] > 0);
 }
 
 //--------------------------------------------------------------------------------------
@@ -173,7 +205,7 @@ function work_order_update_finished_quantity($woid, $quantity, $force_close=0)
        $sql = "UPDATE ".TB_PREF."workorders SET units_issued = units_issued + $quantity,
                closed = ((units_issued >= units_reqd) OR $force_close)
                WHERE id = $woid";
-                               
+
        db_query($sql, "The work order issued quantity couldn't be updated");
 }
 
@@ -181,27 +213,27 @@ function work_order_update_finished_quantity($woid, $quantity, $force_close=0)
 
 function void_work_order($woid)
 {
-       $work_order = get_work_order($woid);  
-       
-       if (!($work_order["type"] == wo_types::advanced())) 
+       $work_order = get_work_order($woid);
+
+       if (!($work_order["type"] == wo_types::advanced()))
        {
-               begin_transaction();    
-               
+               begin_transaction();
+
                $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_issued=0 WHERE id = $woid";
                db_query($sql, "The work order couldn't be voided");
-               
+
                // void all related stock moves
-               void_stock_move(systypes::work_order(), $woid); 
-       
+               void_stock_move(systypes::work_order(), $woid);
+
                // void any related gl trans
                void_gl_trans(systypes::work_order(), $woid, true);
-               
+
                // clear the requirements units received
                void_wo_requirements($woid);
-               
-               commit_transaction();           
-       } 
-       else 
+
+               commit_transaction();
+       }
+       else
        {
                // void everything inside the work order : issues, productions, payments
        }
index 054eee23525c6bf2fd06b1f5c8a46245d8ff2ca8..3eecf0155c83c33c715cb6178cd692d9d1164a5b 100644 (file)
@@ -2,70 +2,72 @@
 
 //--------------------------------------------------------------------------------------
 
-function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $additional_costs, $memo_) 
+function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $additional_costs, $memo_)
 {
        begin_transaction();
-       
+
        // if unassembling, reverse the stock movements
        if ($type == wo_types::unassemble())
                $units_reqd = -$units_reqd;
-       
+
+       add_material_cost($stock_id, $units_reqd, $date_);
+
        $date = date2sql($date_);
        if (!isset($additional_costs) || ($additional_costs == ""))
                $additional_costs = 0;
-       
-       $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, units_issued, stock_id, 
-               type, additional_costs, date_, released_date, required_by, released, closed) 
-       VALUES ('$wo_ref', '$loc_code', $units_reqd, $units_reqd, '$stock_id', 
+
+       $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, units_issued, stock_id,
+               type, additional_costs, date_, released_date, required_by, released, closed)
+       VALUES ('$wo_ref', '$loc_code', $units_reqd, $units_reqd, '$stock_id',
                $type, $additional_costs, '$date', '$date', '$date', 1, 1)";
        db_query($sql, "could not add work order");
-       
+
        $woid = db_insert_id();
-       
+
        //--------------------------------------------------------------------------
 
        // create Work Order Requirements based on the bom
-       $result = get_bom($stock_id);   
-       
-       while ($bom_item = db_fetch($result)) 
+       $result = get_bom($stock_id);
+
+       while ($bom_item = db_fetch($result))
        {
-               
+
                $unit_quantity = $bom_item["quantity"];
                $item_quantity = $bom_item["quantity"] * $units_reqd;
-               
-               
+
+
                $sql = "INSERT INTO ".TB_PREF."wo_requirements (workorder_id, stock_id, workcentre, units_req, units_issued, loc_code)
-                       VALUES ($woid, " . "'" . $bom_item["component"] . "'" . ", 
-                       '". $bom_item["workcentre_added"] . "', 
+                       VALUES ($woid, " . "'" . $bom_item["component"] . "'" . ",
+                       '". $bom_item["workcentre_added"] . "',
                        $unit_quantity, $item_quantity, '" . $bom_item["loc_code"] . "')";
-                       
+
         db_query($sql, "The work order requirements could not be added");
-        
+
                // insert a -ve stock move for each item
-               add_stock_move(systypes::work_order(), $bom_item["component"], $woid, 
-                       $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, 0);        
+               add_stock_move(systypes::work_order(), $bom_item["component"], $woid,
+                       $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, 0);
        }
 
 
        // -------------------------------------------------------------------------
 
        // insert a +ve stock move for the item being manufactured
-       add_stock_move(systypes::work_order(), $stock_id, $woid,        $loc_code, $date_, 
-               $wo_ref, $units_reqd, 0);               
+       add_stock_move(systypes::work_order(), $stock_id, $woid,        $loc_code, $date_,
+               $wo_ref, $units_reqd, 0);
 
        // -------------------------------------------------------------------------
 
        work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $additional_costs);
-       
+
        // -------------------------------------------------------------------------
 
-       add_comments(systypes::work_order(), $woid, $date_, $memo_);    
-       
-       add_forms_for_sys_type(systypes::work_order(), $woid, $type);   
-       
+       add_comments(systypes::work_order(), $woid, $date_, $memo_);
+
+       add_forms_for_sys_type(systypes::work_order(), $woid, $type);
+
        references::save_last($wo_ref, systypes::work_order());
-       
-       commit_transaction();   
+
+       commit_transaction();
        return $woid;
 }
 
@@ -74,31 +76,31 @@ function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type,
 function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $additional_costs)
 {
        $result = get_bom($stock_id);
-       
+
        // credit all the components
        $total_cost = 0;
-       while ($bom_item = db_fetch($result)) 
+       while ($bom_item = db_fetch($result))
        {
-               
+
                $bom_accounts = get_stock_gl_code($bom_item["component"]);
-               
-               $bom_cost = $bom_item["standard_cost"] * $bom_item["quantity"] * $units_reqd;           
-               
+
+               $bom_cost = $bom_item["standard_cost"] * $bom_item["quantity"] * $units_reqd;
+
                add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $bom_accounts["inventory_account"], 0, 0,
                        null, -$bom_cost);
-               
+
                $total_cost += $bom_cost;
-       }       
+       }
        // credit additional costs
        $item_accounts = get_stock_gl_code($stock_id);
-       add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["assembly_account"], 
+       add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["assembly_account"],
                $item_accounts["dimension_id"], $item_accounts["dimension2_id"], null, -$additional_costs);
-       
+
        // debit total components + additional
-       $total_cost += $additional_costs;       
-       add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["inventory_account"], 
+       $total_cost += $additional_costs;
+       add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["inventory_account"],
                0, 0, null, $total_cost);
-} 
+}
 
 //--------------------------------------------------------------------------------------
 
index 181ad2076c3c5c30f488f69caf1024f9f6cfaee8..0e0d5a9a787e8bf3a4aef8bfa246cec8ed8674ab 100644 (file)
@@ -21,9 +21,6 @@ check_db_has_workcentres(_("There are no work centres defined in the system. BOM
 if (isset($_GET["NewItem"]))
 {
        $_POST['stock_id'] = $_GET["NewItem"];
-       if (isset($_GET['item']) && isset($_GET['qty']))
-               add_material_cost($_GET["NewItem"], $_GET['item'], $_GET['qty'], false);
-
 }
 if (isset($_GET['stock_id']))
 {
@@ -50,24 +47,6 @@ elseif (isset($_POST["selected_component"]))
        $selected_component = $_POST["selected_component"];
 }
 
-function add_material_cost($parent, $item, $n, $add=true)
-{
-       $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id='$parent'";
-       $result = db_query($sql);
-       $myrow = db_fetch($result);
-       $material_cost = $myrow['material_cost'];
-       $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id='$item'";
-       $result = db_query($sql);
-       $myrow = db_fetch($result);
-       $material_cost2 = $myrow['material_cost'];
-       if ($add)
-               $material_cost += ($material_cost2 * $n);
-       else
-               $material_cost -= ($material_cost2 * $n);
-       $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=$material_cost
-               WHERE stock_id='$parent'";
-       db_query($sql,"The cost details for the inventory item could not be updated");
-}
 
 //--------------------------------------------------------------------------------------------------
 
@@ -125,8 +104,8 @@ function display_bom_items($selected_parent)
         label_cell($myrow["WorkCentreDescription"]);
         label_cell($myrow["quantity"]);
         label_cell($myrow["units"]);
-        edit_link_cell(SID . "NewItem=$selected_parent&selected_component=" . $myrow["id"]."&item=".$myrow['component']."&qty=".$myrow['quantity']);
-        delete_link_cell(SID . "delete=" . $myrow["id"]. "&stock_id=" . $_POST['stock_id']."&item=".$myrow['component']."&qty=".$myrow['quantity']);
+        edit_link_cell(SID . "NewItem=$selected_parent&selected_component=" . $myrow["id"]);
+        delete_link_cell(SID . "delete=" . $myrow["id"]. "&stock_id=" . $_POST['stock_id']);
         end_row();
 
        } //END WHILE LIST LOOP
@@ -160,8 +139,6 @@ function on_submit($selected_parent, $selected_component)
                        AND id='" . $selected_component . "'";
                check_db_error("Could not update this bom component", $sql);
 
-               add_material_cost($selected_parent, $_POST['item'], $_POST['quantity'], true);
-
                db_query($sql,"could not update bom");
 
        }
@@ -189,7 +166,6 @@ function on_submit($selected_parent, $selected_component)
 
                                db_query($sql,"check failed");
 
-                               add_material_cost($selected_parent, $_POST['component'], $_POST['quantity'], true);
                                //$msg = _("A new component part has been added to the bill of material for this item.");
 
                        }
@@ -211,10 +187,9 @@ function on_submit($selected_parent, $selected_component)
 
 if (isset($_GET['delete']))
 {
-       $sql = "DELETE FROM ".TB_PREF."bom WHERE id='".$_GET['delete']."'";
-       db_query($sql,"Could not delete this bom components");
 
-       add_material_cost($_GET['stock_id'], $_GET['item'], $_GET['qty'], false);
+       $sql = "DELETE FROM ".TB_PREF."bom WHERE id='" . $_GET['delete']. "'";
+       db_query($sql,"Could not delete this bom components");
 
        display_note(_("The component item has been deleted from this bom."));
 
@@ -268,7 +243,6 @@ if (isset($_POST['stock_id']))
                $_POST['workcentre_added']  = $myrow["workcentre_added"];
                $_POST['quantity'] = $myrow["quantity"];
 
-               hidden('item', $myrow["component"]);
                hidden('selected_parent', $selected_parent);
                hidden('selected_component', $selected_component);
                label_row(_("Component:"), $myrow["component"] . " - " . $myrow["description"]);
index a8253edcb0b2b6e2e050abacdd8820a61862738c..39fd25bfe20c3bbe5ea0f85674482bed26dbbdc9 100644 (file)
@@ -27,7 +27,7 @@ check_db_has_locations(("There are no inventory locations defined in the system.
 if (isset($_GET['trans_no']))
 {
        $selected_id = $_GET['trans_no'];
-} 
+}
 elseif(isset($_POST['selected_id']))
 {
        $selected_id = $_POST['selected_id'];
@@ -35,7 +35,7 @@ elseif(isset($_POST['selected_id']))
 
 //---------------------------------------------------------------------------------------
 
-if (isset($_GET['AddedID'])) 
+if (isset($_GET['AddedID']))
 {
        $id = $_GET['AddedID'];
 
@@ -46,7 +46,7 @@ if (isset($_GET['AddedID']))
 
 //---------------------------------------------------------------------------------------
 
-if (isset($_GET['UpdatedID'])) 
+if (isset($_GET['UpdatedID']))
 {
        $id = $_GET['UpdatedID'];
 
@@ -56,7 +56,7 @@ if (isset($_GET['UpdatedID']))
 
 //---------------------------------------------------------------------------------------
 
-if (isset($_GET['DeletedID'])) 
+if (isset($_GET['DeletedID']))
 {
        $id = $_GET['DeletedID'];
 
@@ -66,7 +66,7 @@ if (isset($_GET['DeletedID']))
 
 //---------------------------------------------------------------------------------------
 
-if (isset($_GET['ClosedID'])) 
+if (isset($_GET['ClosedID']))
 {
        $id = $_GET['ClosedID'];
 
@@ -94,21 +94,21 @@ if (!isset($_POST['date_']))
        $_POST['date_'] = Today();
        if (!is_date_in_fiscalyear($_POST['date_']))
                $_POST['date_'] = end_fiscalyear();
-}              
+}
 
 function can_process()
 {
        global $selected_id;
 
-       if (!isset($selected_id)) 
+       if (!isset($selected_id))
        {
-       if (!references::is_valid($_POST['wo_ref'])) 
+       if (!references::is_valid($_POST['wo_ref']))
        {
                display_error(_("You must enter a reference."));
                return false;
        }
 
-       if (!is_new_reference($_POST['wo_ref'], systypes::work_order())) 
+       if (!is_new_reference($_POST['wo_ref'], systypes::work_order()))
        {
                display_error(_("The entered reference is already in use."));
                return false;
@@ -131,16 +131,16 @@ function can_process()
        {
                display_error( _("The date entered is in an invalid format."));
                return false;
-       } 
-       elseif (!is_date_in_fiscalyear($_POST['date_'])) 
+       }
+       elseif (!is_date_in_fiscalyear($_POST['date_']))
        {
                display_error(_("The entered date is not in fiscal year."));
                return false;
        }
        // only check bom and quantites if quick assembly
-       if (!($_POST['type'] == wo_types::advanced())) 
+       if (!($_POST['type'] == wo_types::advanced()))
        {
-        if (!has_bom($_POST['stock_id'])) 
+        if (!has_bom($_POST['stock_id']))
         {
                display_error(_("The selected item to manufacture does not have a bom."));
                return false;
@@ -160,23 +160,23 @@ function can_process()
                return false;
        }
 
-        if (!sys_prefs::allow_negative_stock()) 
+        if (!sys_prefs::allow_negative_stock())
         {
-               if ($_POST['type'] == wo_types::assemble()) 
+               if ($_POST['type'] == wo_types::assemble())
                {
                        // check bom if assembling
                 $result = get_bom($_POST['stock_id']);
 
-               while ($bom_item = db_fetch($result)) 
+               while ($bom_item = db_fetch($result))
                {
 
-                       if (has_stock_holding($bom_item["ResourceType"])) 
+                       if (has_stock_holding($bom_item["ResourceType"]))
                        {
 
                                $quantity = $bom_item["quantity"] * $_POST['quantity'];
 
                         $qoh = get_qoh_on_date($bom_item["component"], $bom_item["loc_code"], $_POST['date_']);
-                               if (-$quantity + $qoh < 0) 
+                               if (-$quantity + $qoh < 0)
                                {
                                        display_error(_("The work order cannot be processed because there is an insufficient quantity for component:") .
                                                " " . $bom_item["component"] . " - " .  $bom_item["description"] . ".  " . _("Location:") . " " . $bom_item["location_name"]);
@@ -184,36 +184,36 @@ function can_process()
                                }
                        }
                }
-               } 
-               elseif ($_POST['type'] == wo_types::unassemble()) 
+               }
+               elseif ($_POST['type'] == wo_types::unassemble())
                {
                        // if unassembling, check item to unassemble
                                $qoh = get_qoh_on_date($_POST['stock_id'], $_POST['StockLocation'], $_POST['date_']);
-                       if (-$_POST['quantity'] + $qoh < 0) 
+                       if (-$_POST['quantity'] + $qoh < 0)
                        {
                                display_error(_("The selected item cannot be unassembled because there is insufficient stock."));
                                        return false;
                        }
                }
        }
-     } 
-     else 
+     }
+     else
      {
        if (!is_date($_POST['RequDate']))
        {
                display_error( _("The date entered is in an invalid format."));
                return false;
-               } 
-               //elseif (!is_date_in_fiscalyear($_POST['RequDate'])) 
+               }
+               //elseif (!is_date_in_fiscalyear($_POST['RequDate']))
                //{
                //      display_error(_("The entered date is not in fiscal year."));
                //      return false;
                //}
-       if (isset($selected_id)) 
+       if (isset($selected_id))
        {
                $myrow = get_work_order($selected_id, true);
 
-               if ($_POST['units_issued'] > $_POST['quantity']) 
+               if ($_POST['units_issued'] > $_POST['quantity'])
                {
                        display_error(_("The quantity cannot be changed to be less than the quantity already manufactured for this order."));
                        return false;
@@ -226,7 +226,7 @@ function can_process()
 
 //-------------------------------------------------------------------------------------
 
-if (isset($_POST['ADD_ITEM']) && can_process()) 
+if (isset($_POST['ADD_ITEM']) && can_process())
 {
 
        $id = add_work_order($_POST['wo_ref'], $_POST['StockLocation'], $_POST['quantity'],
@@ -238,7 +238,7 @@ if (isset($_POST['ADD_ITEM']) && can_process())
 
 //-------------------------------------------------------------------------------------
 
-if (isset($_POST['UPDATE_ITEM']) && can_process()) 
+if (isset($_POST['UPDATE_ITEM']) && can_process())
 {
 
        update_work_order($selected_id, $_POST['StockLocation'], $_POST['quantity'],
@@ -249,22 +249,22 @@ if (isset($_POST['UPDATE_ITEM']) && can_process())
 
 //--------------------------------------------------------------------------------------
 
-if (isset($_POST['delete'])) 
+if (isset($_POST['delete']))
 {
        //the link to delete a selected record was clicked instead of the submit button
 
        $cancel_delete = false;
 
        // can't delete it there are productions or issues
-       if (work_order_has_productions($selected_id) || 
-               work_order_has_issues($selected_id)     || 
+       if (work_order_has_productions($selected_id) ||
+               work_order_has_issues($selected_id)     ||
                work_order_has_payments($selected_id))
        {
                display_error(_("This work order cannot be deleted because it has already been processed."));
                $cancel_delete = true;
        }
 
-       if ($cancel_delete == false) 
+       if ($cancel_delete == false)
        { //ie not cancelled the delete as a result of above tests
 
                // delete the actual work order
@@ -275,7 +275,7 @@ if (isset($_POST['delete']))
 
 //-------------------------------------------------------------------------------------
 
-if (isset($_POST['close'])) 
+if (isset($_POST['close']))
 {
 
        // update the closed flag in the work order
@@ -295,14 +295,14 @@ if (isset($selected_id))
 {
        $myrow = get_work_order($selected_id);
 
-       if (strlen($myrow[0]) == 0) 
+       if (strlen($myrow[0]) == 0)
        {
                echo _("The order number sent is not valid.");
                safe_exit();
        }
 
        // if it's a closed work order can't edit it
-       if ($myrow["closed"] == 1) 
+       if ($myrow["closed"] == 1)
        {
                echo "<center>";
                display_error(_("This work order is closed and cannot be edited."));
@@ -330,11 +330,13 @@ if (isset($selected_id))
        hidden('released', $_POST['released']);
        hidden('released_date', $_POST['released_date']);
        hidden('selected_id',  $selected_id);
+       hidden('old_qty', $myrow["units_reqd"]);
+       hidden('old_stk_id', $myrow["stock_id"]);
 
        label_row(_("Reference:"), $_POST['wo_ref']);
        label_row(_("Type:"), wo_types::name($_POST['type']));
-} 
-else 
+}
+else
 {
        $_POST['units_issued'] = $_POST['released'] = 0;
        ref_row(_("Reference:"), 'wo_ref', references::get_next(systypes::work_order()));
@@ -342,7 +344,7 @@ else
        wo_types_list_row(_("Type:"), 'type', $_POST['type']);
 }
 
-if ($_POST['released'] == true) 
+if ($_POST['released'] == true)
 {
        hidden('stock_id', $_POST['stock_id']);
        hidden('StockLocation', $_POST['StockLocation']);
@@ -350,8 +352,8 @@ if ($_POST['released'] == true)
 
        label_row(_("Item:"), $myrow["StockItemName"]);
        label_row(_("Destination Location:"), $myrow["location_name"]);
-} 
-else 
+}
+else
 {
        stock_manufactured_items_list_row(_("Item:"), 'stock_id', null);
 
@@ -361,7 +363,7 @@ else
 if (!isset($_POST['quantity']))
        $_POST['quantity'] = 1;
 
-if ($_POST['type'] == wo_types::advanced()) 
+if ($_POST['type'] == wo_types::advanced())
 {
     text_row_ex(_("Quantity Required:"), 'quantity', 12);
     if ($_POST['released'])
@@ -369,7 +371,7 @@ if ($_POST['type'] == wo_types::advanced())
     date_row(_("Date") . ":", 'date_');
        date_row(_("Date Required By") . ":", 'RequDate', null, sys_prefs::default_wo_required_by());
 }
-else 
+else
 {
     text_row_ex(_("Quantity:"), 'quantity', 12);
     date_row(_("Date") . ":", 'date_');
@@ -390,16 +392,16 @@ end_table(1);
 
 submit_add_or_update_center(!isset($selected_id));
 
-if (isset($selected_id)) 
+if (isset($selected_id))
 {
        echo "<br><br><table align=center><tr>";
 
-       if (isset($_POST['released'])) 
+       if (isset($_POST['released']))
        {
                submit_cells('close', _("Close This Work Order"));
        }
        submit_cells('delete', _("Delete This Work Order"));
-       
+
        echo "</tr></table>";
 }
 
index 910cf9a6f30af6e43fb2cbb91ddf7b0436cc10f3..88cab62e602184295d54b92301134e49931df150 100644 (file)
@@ -34,8 +34,11 @@ function add_grn(&$po, $date_, $reference, $location)
                        $myrow = db_fetch($result);
                        $material_cost = $myrow['material_cost'];
                        $qoh = get_qoh_on_date($order_line->stock_id, null, $date_);
-                       $material_cost = ($qoh * $material_cost + $order_line->receive_qty * $price_in_home_currency) /
-                               ($qoh + $order_line->receive_qty);
+                       if ($qoh + $order_line->receive_qty <= 0)
+                               $material_cost = 0;
+                       else
+                               $material_cost = ($qoh * $material_cost + $order_line->receive_qty * $price_in_home_currency) /
+                                       ($qoh + $order_line->receive_qty);
                        $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=$material_cost
                                WHERE stock_id='$order_line->stock_id'";
                        db_query($sql,"The cost details for the inventory item could not be updated");