Fixed manufacturing module voiding procedures.
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Sat, 13 Feb 2016 11:16:06 +0000 (12:16 +0100)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Sat, 13 Feb 2016 11:16:06 +0000 (12:16 +0100)
admin/db/voiding_db.inc
manufacturing/includes/db/work_order_costing_db.inc
manufacturing/includes/db/work_order_issues_db.inc
manufacturing/includes/db/work_order_produce_items_db.inc
manufacturing/includes/db/work_orders_db.inc
manufacturing/work_order_costs.php

index 8323c0d164d7c3a16880ef47801e96754f6cdf4b..c87fe17fa07bc95b82de12a8d0d4d462b292c951 100644 (file)
@@ -99,12 +99,16 @@ function void_transaction($type, $type_no, $date_, $memo_)
                case ST_MANUISSUE : // it's a work order issue
                        if (!exists_work_order_issue($type_no))
                                return _('Selected transaction does not exists.');
+                       if ($wo = check_void_wo_issue($type_no))
+                               return sprintf(_('Selected document cannot be voided because related Work Order %s is already closed.'), $wo);
                        void_work_order_issue($type_no);
                        break;
 
                case ST_MANURECEIVE : // it's a work order production
                        if (!exists_work_order_produce($type_no))
                                return _('Selected transaction does not exists.');
+                       if ($wo = check_void_wo_production($type_no))
+                               return sprintf(_('Selected document cannot be voided because related Work Order %s is already closed.'), $wo);
                        void_work_order_produce($type_no);
                        break;
 
index 845b6300e235dee8bfed143a6c614b0313c603bb..8daa7a7c61f0fcce1136231c5d04351b9c4f7154 100644 (file)
@@ -93,7 +93,7 @@ function update_material_cost($stock_id, $qty, $unit_cost, $date)
                if (!is_date_in_fiscalyear($cart->tran_date))
                        $cart->tran_date = end_fiscalyear();
 
-               $cart->reference = $Refs->get_next(ST_COSTUPDATE, null, $cart->tran_date, $date);
+               $cart->reference = $Refs->get_next(ST_COSTUPDATE, null, $cart->tran_date);
 
                $cart->memo_ = sprintf(_("COGS changed from %s to %s for %d %s of '%s'"), $avg_cost, $unit_cost, $qty_delayed, $item['units'], $stock_id);
 
@@ -111,61 +111,6 @@ function update_material_cost($stock_id, $qty, $unit_cost, $date)
        db_query($sql,"The cost details for the inventory item could not be updated");
 }
 
-function add_issue_cost($stock_id, $qty, $date_, $costs, $adj_only=false)
-{
-       if ($qty != 0)
-               $costs /= $qty;
-       $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
-               .db_escape($stock_id);
-       $result = db_query($sql);
-       $myrow = db_fetch($result);
-       $material_cost =  $myrow['material_cost'];
-       $qoh = get_qoh_on_date($stock_id);
-       if ($qoh < 0)
-               $qoh = 0;
-       if ($adj_only)
-       {
-               if ($qoh>0)
-                       $material_cost = ($qoh * $material_cost + $costs) / $qoh;
-               else // Journal Entry if QOH is 0/negative
-               {
-                       global $Refs;
-
-                       $id = get_next_trans_no(ST_JOURNAL);
-                       $ref = $Refs->get_next(ST_JOURNAL, null, $date_);
-                       add_journal(ST_JOURNAL, $id, $costs, $date_, get_company_currency(), $ref);
-                       
-                       $stock_gl_code = get_stock_gl_code($stock_id);
-                       $memo = "WO Issue settlement JV for zero/negative respository of ".$stock_id;
-                       //Reverse the inventory effect if $qoh <=0
-                       add_gl_trans_std_cost(ST_JOURNAL, $id, $date_, 
-                               $stock_gl_code["inventory_account"],
-                               $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo, 
-                               -$costs);
-                       //GL Posting to inventory adjustment account
-                       add_gl_trans_std_cost(ST_JOURNAL, $id, $date_, 
-                               $stock_gl_code["wip_account"],
-                               $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
-                               $costs);
-                               
-                       add_audit_trail(ST_JOURNAL, $id, $date_);
-                       add_comments(ST_JOURNAL, $id, $date_, $memo);
-                       $Refs->save(ST_JOURNAL, $id, $ref);     
-                       if ($qty != 0)
-                               $material_cost = $costs / $qty; 
-               }
-       }
-       else
-       {
-               if ($qoh + $qty != 0)   
-                       $material_cost = ($qoh * $material_cost + $qty * $costs) /      ($qoh + $qty);
-       }       
-       $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=material_cost+"
-               .db_escape($material_cost)
-               ." WHERE stock_id=".db_escape($stock_id);
-       db_query($sql,"The cost details for the inventory item could not be updated");
-}
-
 /*
        Create journal entry for WO related costs.
 */
index d61e3fc22ab911b4d7b257fc97240a87d441df3d..037e10cbb4fe6d5aebb170206f60be8365896b4f 100644 (file)
@@ -36,7 +36,7 @@ function add_work_order_issue($woid, $ref, $to_work_order, $items, $location, $w
 
        foreach ($items as $item)
        {
-               if ($to_work_order)
+               if ($to_work_order)     // otherwise it is material return to inventory
                        $item->quantity = -$item->quantity;
 
                $unit_cost = get_unit_cost($item->stock_id);
@@ -64,12 +64,8 @@ function add_work_order_issue($woid, $ref, $to_work_order, $items, $location, $w
 
         $total_cost += add_gl_trans_std_cost(ST_MANUISSUE, $number, $date_, $ivaccount, 0, 0,
                 $date_.": "._("Issue of")." ".$stockitem["description"], -$issue_cost);
-               $issue_total += $issue_cost;
        }
 
-       if ($issue_total != 0)  // Apply cost to QOH as adjustment only
-               add_issue_cost($wo['stock_id'], $wo['units_reqd'], $date_, $issue_total, true);
-
     $stockitem = get_item($wo['stock_id']);
 
        $wip_account = $stockitem["wip_account"];
@@ -113,7 +109,7 @@ function get_additional_issues($woid)
 
 function get_work_order_issue($issue_no)
 {
-       $sql = "SELECT DISTINCT issue.*, wo.stock_id,
+       $sql = "SELECT DISTINCT issue.*, wo.stock_id, wo.closed,
                item.description, loc.location_name, center.name AS WorkCentreName
                FROM ".TB_PREF."wo_issues issue,"
                        .TB_PREF."workorders wo,"
@@ -155,66 +151,39 @@ function exists_work_order_issue($issue_no)
 
 //--------------------------------------------------------------------------------------
 
+function check_void_wo_issue($issue_no)
+{
+       $issue = get_work_order_issue($issue_no);
+       return $issue['closed'] ? $issue['workorder_id'] : 0;
+}
+
+//--------------------------------------------------------------------------------------
+
 function void_work_order_issue($type_no)
 {
        begin_transaction();
        hook_db_prevoid(ST_MANUISSUE, $type_no);
 
-       //Chaitanya : Skip processing already voided entry i.e. explicitly voided
        $void_entry = get_voided_entry(ST_MANUISSUE, $type_no);
        if ($void_entry)
                return;
 
+       $issue = get_work_order_issue($type_no);
+
+       if (work_order_is_closed($issue['workorder_id']))
+               return;
+
+       // FIXME update issued material cost
+
        // void the actual issue items and their quantities
-       $sql = "UPDATE ".TB_PREF."wo_issue_items Set qty_issued = 0 WHERE issue_id="
-               .db_escape($type_no);
+       $sql = "UPDATE ".TB_PREF."wo_issue_items SET qty_issued = 0
+               WHERE issue_id=".db_escape($type_no);
+
        db_query($sql,"A work order issue item could not be voided");
 
-       // Reverse the gl posting
-       $issue = get_work_order_issue($type_no);
-       $manf_stock_id = $issue["stock_id"];
-       $date_ = sql2date($issue["issue_date"]);
-       $woid = $issue["workorder_id"];
-
-       $result = get_stock_moves(ST_MANUISSUE, $type_no);
-       $total_cost = 0;
-       $issue_total = 0;
-       while ($myrow = db_fetch($result))
-       {
-               $issue_cost = $myrow["qty"]*$myrow["standard_cost"];
-               $issue = get_stock_gl_code($myrow["stock_id"]);
-        $stockitem = get_item($myrow["stock_id"]);
+       void_gl_trans(ST_MANUISSUE, $type_no);
 
-               // Compatibility for Service Items
-               if (!is_service($issue["mb_flag"]))
-                       $ivaccount = $issue["inventory_account"];
-               else
-                       $ivaccount = $issue["cogs_account"];
-
-               if ($issue_cost != 0)
-               {
-                       $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,
-                               $date_.": "._("Reversed the issue of")." ".$stockitem["description"],
-                               -$issue_cost);
-                       $issue_total += $issue_cost;
-               }
-       }
-       if ($issue_total != 0)
-               // Revese cost effect on manfactured stock item as adjustment only
-               add_issue_cost($manf_stock_id, 0, $date_, $issue_total, true);
-       $issue = get_stock_gl_code($manf_stock_id);
-    $stockitem = get_item($manf_stock_id);
-       if ($total_cost != 0)
-               add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $issue["inventory_account"],
-                       0, 0, $date_.": "._("Reversed the issue to")." ".$stockitem["description"], 
-                       -$total_cost);
-
-       // Shifted below void all related stock moves
        void_stock_move(ST_MANUISSUE, $type_no);
 
        commit_transaction();
 }
-
-
-//--------------------------------------------------------------------------------------
-
index d54944511dbae8d28067af59978c46e1151ea991..44c93b864d0392bf07dc5bca3562e4e70e4caf7b 100644 (file)
@@ -39,7 +39,7 @@ function work_order_produce($woid, $ref, $quantity, $date_, $memo, $close_wo)
        work_order_production_gl($woid, $product["stock_id"], $quantity, $date_, $id);
 
        // update wo quantity and close wo if requested (or finished)
-       $closed = work_order_update_finished_quantity($woid, $quantity, $date_, $close_wo);
+       $closed = work_order_update_finished_quantity($woid, $quantity, $close_wo);
 
        // unit_cost is known when WO is finished, then generate +ve for all items
        if ($closed)
@@ -103,7 +103,7 @@ function work_order_produce($woid, $ref, $quantity, $date_, $memo, $close_wo)
 
 function get_work_order_produce($id)
 {
-       $sql = "SELECT prod.*, wo.stock_id, item.description AS StockDescription
+       $sql = "SELECT prod.*, wo.stock_id, item.description AS StockDescription, wo.closed
                        FROM ".TB_PREF."wo_manufacture prod,"
                                .TB_PREF."workorders wo,"
                                .TB_PREF."stock_master item
@@ -134,6 +134,14 @@ function exists_work_order_produce($id)
     return (db_num_rows($result) > 0);
 }
 
+//--------------------------------------------------------------------------------------
+
+function check_void_wo_production($prod_no)
+{
+       $prod = get_work_order_produce($prod_no);
+       return $prod['closed'] ? $prod['workorder_id'] : 0;
+}
+
 //--------------------------------------------------------------------------------------------
 
 function void_work_order_produce($type_no)
@@ -144,44 +152,22 @@ function void_work_order_produce($type_no)
        // Skip processing already voided entry i.e. explicitly voided
        $void_entry = get_voided_entry(ST_MANURECEIVE, $type_no);
        if ($void_entry)
-               return; 
-
-       $row = get_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"]);
+               return;
 
-       // void any related gl trans
+       $prod = get_work_order_produce($type_no);
 
-       $woid = $row["workorder_id"];
-       $date_ = sql2date($row["date_"]);
-               
-       $result = get_stock_moves(ST_MANURECEIVE, $type_no);
-       while ($myrow = db_fetch($result))
-       {
-               $issue_cost = $myrow["qty"]*$myrow["standard_cost"];
-               $issue = get_stock_gl_code($myrow["stock_id"]);
-        $stockitem = get_item($myrow["stock_id"]);
+       if (work_order_is_closed($prod['workorder_id']))
+               return;
 
-               // Compatibility for Service Items
-               if (!is_service($issue["mb_flag"]))
-                       $ivaccount = $issue["inventory_account"];
-               else
-                       $ivaccount = $issue["cogs_account"];
+       // deduct the quantity of this production from the parent work order
+       work_order_update_finished_quantity($prod["workorder_id"], -$prod["quantity"], false);
 
-               if ($issue_cost != 0)
-               {
-                       add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,
-                               $date_.": "._("Reversed the production ")." ".$stockitem["description"],
-                               -$issue_cost);
-               }
-       }
        // clear the production record
        $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".db_escape($type_no);
        db_query($sql, "Cannot void a wo production");
 
-       // Shifted below
-       // void all related stock moves
+       void_gl_trans(ST_MANURECEIVE, $type_no);
+
        void_stock_move(ST_MANURECEIVE, $type_no);
 
        commit_transaction();
index 48288367a10f6415af2ba89b87c299f3414590f9..f1f696136cf7776839d744dc864d74dfcfd515da 100644 (file)
@@ -220,7 +220,7 @@ function work_order_is_closed($woid)
        Update finished items quantity in work order, and close order either if all the order is produced,
        or on user demand. Returns calculated unit cost on close, or null otherwise.
 */
-function work_order_update_finished_quantity($woid, $quantity, $date, $force_close=0)
+function work_order_update_finished_quantity($woid, $quantity, $force_close=0)
 {
 
        $sql = "UPDATE ".TB_PREF."workorders SET units_issued = units_issued + ".db_escape($quantity)."
@@ -236,88 +236,59 @@ function work_order_update_finished_quantity($woid, $quantity, $date, $force_clo
        return db_num_affected_rows();  // returns 1 if WO has been closed
 }
 
-//--------------------------------------------------------------------------------------
+function reopen_work_order($woid)
+{
+               // FIXME: update_material_cost
+//             update_material_cost($work_order['stock_id'], -$work_order['units_issued'], ... );
+
+               void_gl_trans(ST_WORKORDER, $woid);
 
+               void_stock_move(ST_WORKORDER, $woid);
+
+               $sql = "UPDATE ".TB_PREF."workorders SET closed=0 WHERE id = "
+                       .db_escape($woid);
+               db_query($sql, "The work order couldn't be reopened");
+}
+
+//--------------------------------------------------------------------------------------
+/*
+       Void all work order related operations
+*/
 function void_work_order($woid)
 {
        begin_transaction();
        hook_db_prevoid(ST_WORKORDER, $woid);
 
        $work_order = get_work_order($woid);
-       if (!($work_order["type"] == WO_ADVANCED))
-       {
-               // restore average product costs 
-               $date = sql2date($work_order['date_']);
-               $qty = $work_order['units_reqd'];
 
-               // FIXME: update_material_cost
-
-               // close workorder
-               $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 costing records
-               void_wo_costing($woid);
+       if ($work_order['closed'])
+               reopen_work_order($woid);
 
-               // clear the requirements units received
-               void_wo_requirements($woid);
-       }
-       else
+       if ($work_order['units_issued'])
        {
-               // void everything inside the work order : issues, productions, payments
-               $date = sql2date($work_order['date_']);
+               $prods = get_work_order_productions($woid);
+               while ($prod = db_fetch($prods))
+                       void_work_order_produce($prod['id']);
+       }
 
-               // FIXME: update_material_cost
+       // close workorder
+       $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");
 
-               $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))
-               {
-                       $unit_cost = get_unit_cost($row['stock_id']);
-                       $icost = $unit_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);
-
-               $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 addtional material issues
+       $issues = get_work_order_issues($woid);
+       while($issue = db_fetch($issues))
+               void_work_order_issue($issue['issue_no']);
 
-               // void all related stock moves
-               void_stock_move(ST_WORKORDER, $woid);
+       // void all related stock moves
+       void_stock_move(ST_WORKORDER, $woid);
 
-               // void any related bank/gl trans
-               void_wo_costing($woid);
+       // void any related costing records
+       void_wo_costing($woid);
 
-               // clear the requirements units received
-               void_wo_requirements($woid);
-       }
+       // clear the requirements units received
+       void_wo_requirements($woid);
 
        commit_transaction();
 }
index ef40fc9373080b9bd08a11524f4810af744f4374..b52590f4a072094d6d3636a39e47acfd0ed29e22 100644 (file)
@@ -118,7 +118,7 @@ display_wo_details($_POST['selected_id']);
 //-------------------------------------------------------------------------------------
 
 if (!isset($_POST['ref']))
-       $_POST['ref'] = $Refs->get_next(ST_JOURNAL, null, get_post('date_'));
+       $_POST['ref'] = $Refs->get_next(ST_JOURNAL, null, Today());
 
 start_form();