Fixed a non esisting voiding of Work Order Advanced and a couple of bugs in the same...
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Sun, 28 Feb 2010 15:00:24 +0000 (15:00 +0000)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Sun, 28 Feb 2010 15:00:24 +0000 (15:00 +0000)
Wiki will be updated on how does the Work Order work

CHANGELOG.txt
manufacturing/includes/db/work_order_produce_items_db.inc
manufacturing/includes/db/work_order_requirements_db.inc
manufacturing/includes/db/work_orders_db.inc
manufacturing/includes/db/work_orders_quick_db.inc
purchasing/includes/db/grn_db.inc

index 06873305ba5f189500a24ce7574144fd820ce192..ea70728d2c8806a0f67ee2c79c111f731afbeeff 100644 (file)
@@ -19,7 +19,16 @@ Legend:
 ! -> Note
 $ -> Affected files
 
-
+28-Feb-2010 Joe Hunt
+# Fixed a non esisting voiding of Work Order Advanced and a couple of
+  bugs in the same operation. And a fix according to Vramak on the forum.
+  Wiki will be updated on how does the Work Order work
+$ /manufacturing/includes/db/work_orders_db.inc
+  /manufacturing/includes/db/work_orders_quick_db.inc
+  /manufacturing/includes/db/work_order_produce_items_db.inc
+  /manufacturing/includes/db/work_order_requirements_db.inc
+  /purchasing/includes/db/grn_db.inc
+  
 26-Feb-2010 Janusz Dobrowolski
 # [0000209],[0000210] Improper quantity/price handling on supplier change.
 $ /purchasing/includes/ui/po_ui.inc
@@ -55,7 +64,7 @@ $ /includes/access_levels.inc
   and no exchange rates there before.
 $ /gl/manage/exchange_rates.php
 # Bug in exchange variation calculation in certain situations. Again.
-$ /gl/includes/gl_db_banking.inc
+$ /gl/includes/db/gl_db_banking.inc
  
 21-Feb-2010 Janusz Dobrowolski
 # Fixed check for language session var.
index 428508112d75d9428b950d7366b5a6886b4e1b78..b9fd16f0e9198ad61d94b74a281e21b4aebc84b8 100644 (file)
@@ -42,7 +42,7 @@ function work_order_produce($woid, $ref, $quantity, $date_, $memo_, $close_wo)
 
        // -------------------------------------------------------------------------
 
-       work_order_quick_costs($woid, $details["stock_id"], $quantity, $date_, true);
+       work_order_quick_costs($woid, $details["stock_id"], $quantity, $date_, $id);
 
        // -------------------------------------------------------------------------
        // insert a +ve stock move for the item being manufactured
@@ -107,7 +107,7 @@ function void_work_order_produce($type_no)
        // deduct the quantity of this production from the parent work order
        work_order_update_finished_quantity($row["workorder_id"], -$row["quantity"]);
 
-       work_order_quick_costs($row['workorder_id'], $row['stock_id'], -$row['quantity'], sql2date($row['date_']), true);
+       work_order_quick_costs($row['workorder_id'], $row['stock_id'], -$row['quantity'], sql2date($row['date_']), $type_no);
 
        // clear the production record
        $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".db_escape($type_no);
index ba53b5a2b2e69192a086342608545835560790c8..a722c1ed96465a5b4fbd5e2e13111d48b392e1c5 100644 (file)
@@ -57,10 +57,10 @@ function delete_wo_requirements($woid)
 
 //--------------------------------------------------------------------------------------
 
-function update_wo_requirement_issued($woReqID, $quantity)
+function update_wo_requirement_issued($woid, $stock_id, $quantity)
 {
        $sql = "UPDATE ".TB_PREF."wo_requirements SET units_issued = units_issued + ".db_escape($quantity)."
-               WHERE id = ".db_escape($woReqID);
+               WHERE workorder_id = ".db_escape($woid)." AND stock_id = ".db_escape($stock_id);
                                
        db_query($sql, "The work requirements issued quantity couldn't be updated");
 }
index 24754e4b1867611190464207f38983b97973f51a..b53012e1832bb55302d5c9b43b7ad7bf082e367f 100644 (file)
@@ -28,14 +28,10 @@ function add_material_cost($stock_id, $qty, $date_)
        $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
-       */
        if ($qoh < 0)
                $qoh = 0;
-       $material_cost = ($qoh * $material_cost + $qty * $m_cost) /     ($qoh + $qty);
+       if ($qoh + $qty != 0)   
+               $material_cost = ($qoh * $material_cost + $qty * $m_cost) /     ($qoh + $qty);
        $material_cost = round2($material_cost, $dec);  
        $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=$material_cost
                WHERE stock_id=".db_escape($stock_id);
@@ -54,14 +50,10 @@ function add_overhead_cost($stock_id, $qty, $date_, $costs)
        $myrow = db_fetch($result);
        $overhead_cost =  $myrow['overhead_cost'];
        $qoh = get_qoh_on_date($stock_id, null, $date_);
-       /*
-       if ($qoh + $qty <= 0)
-               $overhead_cost = 0;
-       else
-       */
        if ($qoh < 0)
                $qoh = 0;
-       $overhead_cost = ($qoh * $overhead_cost + $qty * $costs) /      ($qoh + $qty);
+       if ($qoh + $qty != 0)   
+               $overhead_cost = ($qoh * $overhead_cost + $qty * $costs) /      ($qoh + $qty);
        $overhead_cost = round2($overhead_cost, $dec);  
        $sql = "UPDATE ".TB_PREF."stock_master SET overhead_cost=".db_escape($overhead_cost)."
                WHERE stock_id=".db_escape($stock_id);
@@ -80,14 +72,10 @@ function add_labour_cost($stock_id, $qty, $date_, $costs)
        $myrow = db_fetch($result);
        $labour_cost =  $myrow['labour_cost'];
        $qoh = get_qoh_on_date($stock_id, null, $date_);
-       /*
-       if ($qoh + $qty <= 0)
-               $labour_cost = 0;
-       else
-       */
        if ($qoh < 0)
                $qoh = 0;
-       $labour_cost = ($qoh * $labour_cost + $qty * $costs) /  ($qoh + $qty);
+       if ($qoh + $qty != 0)   
+               $labour_cost = ($qoh * $labour_cost + $qty * $costs) /  ($qoh + $qty);
        $labour_cost = round2($labour_cost, $dec);      
        $sql = "UPDATE ".TB_PREF."stock_master SET labour_cost=".db_escape($labour_cost)."
                WHERE stock_id=".db_escape($stock_id);
@@ -104,16 +92,12 @@ function add_issue_cost($stock_id, $qty, $date_, $costs)
        $myrow = db_fetch($result);
        $material_cost =  $myrow['material_cost'];
        $dec = user_price_dec();
-       $material_cost = price_decimal_format($material_cost, $dec); 
+       price_decimal_format($material_cost, $dec); 
        $qoh = get_qoh_on_date($stock_id, null, $date_);
-       /*
-       if ($qoh + $qty  <= 0)
-               $material_cost = 0;
-       else
-       */
        if ($qoh < 0)
                $qoh = 0;
-       $material_cost = ($qty * $costs) /      ($qoh + $qty);
+       if ($qoh + $qty != 0)   
+               $material_cost = ($qty * $costs) /      ($qoh + $qty);
        $material_cost = round2($material_cost, $dec);  
        $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=material_cost+"
                .db_escape($material_cost)
@@ -310,13 +294,22 @@ function work_order_update_finished_quantity($woid, $quantity, $force_close=0)
 
 function void_work_order($woid)
 {
-       $work_order = get_work_order($woid);
+       begin_transaction();
 
+       $work_order = get_work_order($woid);
        if (!($work_order["type"] == WO_ADVANCED))
        {
-               begin_transaction();
-
-               $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_issued=0 WHERE id = "
+               $date = sql2date($work_order['date_']);
+               $qty = $work_order['units_reqd'];
+               add_material_cost($work_order['stock_id'], -$qty, $date); // remove avg. cost for qty
+               $cost = get_gl_wo_cost($woid, WO_LABOUR); // get the labour cost and reduce avg cost
+               if ($cost != 0)
+                       add_labour_cost($work_order['stock_id'], -$qty, $date, $cost);
+               $cost = get_gl_wo_cost($woid, WO_OVERHEAD); // get the overhead cost and reduce avg cost
+               if ($cost != 0)
+                       add_overhead_cost($work_order['stock_id'], -$qty, $date, $cost);
+
+               $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
                        .db_escape($woid);
                db_query($sql, "The work order couldn't be voided");
 
@@ -328,15 +321,74 @@ function void_work_order($woid)
 
                // clear the requirements units received
                void_wo_requirements($woid);
-
-               commit_transaction();
        }
        else
        {
                // void everything inside the work order : issues, productions, payments
+               $date = sql2date($work_order['date_']);
+               add_material_cost($work_order['stock_id'], -$work_order['units_reqd'], $date); // remove avg. cost for qty
+               $result = get_work_order_productions($woid); // check the produced quantity
+               $qty = 0;
+               while ($row = db_fetch($result))
+               {
+                       $qty += $row['quantity'];
+                       // clear the production record
+                       $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".$$row['id'];
+                       db_query($sql, "Cannot void a wo production");
+                       
+                       void_stock_move(ST_MANURECEIVE, $row['id']); // and void the stock moves; 
+               }
+               $result = get_additional_issues($woid); // check the issued quantities
+               $cost = 0;
+               $issue_no = 0;
+               while ($row = db_fetch($result))
+               {
+                       $std_cost = get_standard_cost($row['stock_id']);
+                       $icost = $std_cost * $row['qty_issued'];
+                       $cost += $icost;
+                       if ($issue_no == 0)
+                               $issue_no = $row['issue_no'];
+                       // void the actual issue items and their quantities
+                       $sql = "UPDATE ".TB_PREF."wo_issue_items SET qty_issued = 0 WHERE issue_id="
+                               .db_escape($row['id']);
+                       db_query($sql,"A work order issue item could not be voided");
+               }       
+               if ($issue_no != 0)
+                       void_stock_move(ST_MANUISSUE, $issue_no); // and void the stock moves 
+               if ($cost != 0)
+                       add_issue_cost($work_order['stock_id'], -$qty, $date, $cost);
+
+               $cost = get_gl_wo_cost($woid, WO_LABOUR); // get the labour cost and reduce avg cost
+               if ($cost != 0)
+                       add_labour_cost($work_order['stock_id'], -$qty, $date, $cost);
+               $cost = get_gl_wo_cost($woid, WO_OVERHEAD); // get the overhead cost and reduce avg cost
+               if ($cost != 0)
+                       add_overhead_cost($work_order['stock_id'], -$qty, $date, $cost);
+               
+               $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
+                       .db_escape($woid);
+               db_query($sql, "The work order couldn't be voided");
+
+               // void all related stock moves
+               void_stock_move(ST_WORKORDER, $woid);
+
+               // void any related gl trans
+               void_gl_trans(ST_WORKORDER, $woid, true);
+
+               // clear the requirements units received
+               void_wo_requirements($woid);
        }
+       commit_transaction();
 }
 
 //--------------------------------------------------------------------------------------
+function get_gl_wo_cost($woid, $cost_type)
+{
+       $cost = 0;
+       $result = get_gl_wo_cost_trans($woid, $cost_type);
+       while ($row = db_fetch($result))
+               $cost += -$row['amount'];
+       return $cost;   
+}
 
 ?>
\ No newline at end of file
index 1b254f753c2bb3bf316fce28c9b5994184b02356..92b984dde11d948cc5e266d7aa4c22727f863993 100644 (file)
@@ -73,7 +73,7 @@ function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type,
 
        // -------------------------------------------------------------------------
 
-       work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, false, $costs, $cr_acc, $labour, $cr_lab_acc);
+       work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, 0, $costs, $cr_acc, $labour, $cr_lab_acc);
 
        // -------------------------------------------------------------------------
 
@@ -87,7 +87,7 @@ 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_, $advanced=false, $costs=0, $cr_acc="", $labour=0, $cr_lab_acc="")
+function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced=0, $costs=0, $cr_acc="", $labour=0, $cr_lab_acc="")
 {
        global $wo_cost_types;
        $result = get_bom($stock_id);
@@ -103,8 +103,9 @@ function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced
 
                if ($advanced)
                {
+                       update_wo_requirement_issued($woid, $bom_item['component'], $bom_item["quantity"] * $units_reqd);                       
                        // insert a -ve stock move for each item
-                       add_stock_move(ST_WORKORDER, $bom_item["component"], $woid,
+                       add_stock_move(ST_MANURECEIVE, $bom_item["component"], $advanced,
                                $bom_item["loc_code"], $date_, "", -$bom_item["quantity"] * $units_reqd, 0);
                }
                $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $bom_accounts["inventory_account"], 0, 0,
@@ -128,15 +129,9 @@ function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced
                }
                if ($issue_total != 0)
                        add_issue_cost($stock_id, $units_reqd, $date_, $issue_total);
-               $result = get_gl_wo_cost_trans($woid, WO_LABOUR);
-               $lcost = 0;
-               while ($row = db_fetch($result))
-                       $lcost += -$row['amount'];
+               $lcost = get_gl_wo_cost($woid, WO_LABOUR);
                add_labour_cost($stock_id, $units_reqd, $date_, $lcost * $units_reqd / $wo['units_reqd']);
-               $result = get_gl_wo_cost_trans($woid, WO_OVERHEAD);
-               $ocost = 0;
-               while ($row = db_fetch($result))
-                       $ocost += -$row['amount'];
+               $ocost = get_gl_wo_cost($woid, WO_OVERHEAD);
                add_overhead_cost($stock_id, $units_reqd, $date_, $ocost * $units_reqd / $wo['units_reqd']);
        }
        // credit additional costs
index 59973aacf0990a8192bf833adb4b45b1c2c9c37c..5d91b697a93b9900764b1359b291ab94d3e09cd1 100644 (file)
@@ -24,7 +24,6 @@ function update_average_material_cost($supplier, $stock_id, $price, $qty, $date,
        {
                $ex_rate = get_exchange_rate_to_home_currency($currency, $date);
                $price_in_home_currency = $price / $ex_rate;
-               //$price_in_home_currency = to_home_currency($price, $currency, $date);
        }       
        else
                $price_in_home_currency = $price;
@@ -43,17 +42,8 @@ function update_average_material_cost($supplier, $stock_id, $price, $qty, $date,
        if ($adj_only)
        {
                if ($qoh > 0)
-               /*
-               if ($qoh <= 0)
-                       $material_cost = 0;
-               else
-               */
                        $material_cost = ($qoh * $material_cost + $qty * $price_in_home_currency) /     $qoh;
        }
-       /*
-       elseif ($qoh + $qty <= 0)
-               $material_cost = 0;
-       */      
        else
        {
                if ($qoh < 0)
@@ -61,8 +51,9 @@ function update_average_material_cost($supplier, $stock_id, $price, $qty, $date,
                        if ($qoh + $qty > 0)
                                $cost_adjust = true;
                        $qoh = 0;
-               }       
-               $material_cost = ($qoh * $material_cost + $qty * $price_in_home_currency) /     ($qoh + $qty);
+               }
+               if ($qoh + $qty != 0)
+                       $material_cost = ($qoh * $material_cost + $qty * $price_in_home_currency) /     ($qoh + $qty);
        }       
        $material_cost = round2($material_cost, $dec);
        if ($cost_adjust) // new 2010-02-10