Removed obsolete stock_master.last_cost field.
[fa-stable.git] / manufacturing / includes / db / work_orders_quick_db.inc
index 940145af6a5d64802bcbd0cfc7ede6e8f8ee27dd..14570f18e36d0df2542b9b34467722d321b1a0b6 100644 (file)
@@ -1,25 +1,49 @@
 <?php
-
-//--------------------------------------------------------------------------------------
-
-function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $additional_costs, $memo_)
+/**********************************************************************
+    Copyright (C) FrontAccounting, LLC.
+       Released under the terms of the GNU General Public License, GPL, 
+       as published by the Free Software Foundation, either version 3 
+       of the License, or (at your option) any later version.
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
+    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
+***********************************************************************/
+/*
+       Quick work order entry, $type is either WO_ASSEMBLY, or WO_UNASSEMBLY
+       All changes in material and manufactured item stock are made during work order entry.
+*/
+function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, 
+       $date_, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc)
 {
+       global $Refs;
+
        begin_transaction();
+       $args = func_get_args();
+       $args = (object)array_combine(array('wo_ref', 'loc_code', 'units_reqd', 'stock_id',
+               'type', 'date_', 'memo_', 'costs', 'cr_acc', 'labour', 'cr_lab_acc'), $args);
+       $args->woid = 0;
+       hook_db_prewrite($args, ST_WORKORDER);
 
        // if unassembling, reverse the stock movements
-       if ($type == wo_types::unassemble())
+       if ($type == WO_UNASSEMBLY)
                $units_reqd = -$units_reqd;
 
        add_material_cost($stock_id, $units_reqd, $date_);
 
        $date = date2sql($date_);
-       if (!isset($additional_costs) || ($additional_costs == ""))
-               $additional_costs = 0;
+       if (!isset($costs) || ($costs == ""))
+               $costs = 0;
+       add_overhead_cost($stock_id, $units_reqd, $date_, $costs);
+       if (!isset($labour) || ($labour == ""))
+               $labour = 0;
+       add_labour_cost($stock_id, $units_reqd, $date_, $labour);
 
        $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)";
+       VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", ".db_escape($units_reqd)
+       .", ".db_escape($units_reqd).", ".db_escape($stock_id).",
+               ".db_escape($type).", ".db_escape($costs).", '$date', '$date', '$date', 1, 1)";
        db_query($sql, "could not add work order");
 
        $woid = db_insert_id();
@@ -44,26 +68,44 @@ function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type,
         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);
+               $UnitCost = get_standard_cost($bom_item["component"]);
+               add_stock_move(ST_WORKORDER, $bom_item["component"], $woid,
+                       $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, $UnitCost, $UnitCost);
+       }
+
+       // -------------------------------------------------------------------------
+       //Negative Stock Handling
+       $qoh = get_qoh_on_date($stock_id);
+       $cost_adjust = false;
+       if ($qoh < 0)
+       {
+               if ($qoh + $units_reqd >= 0)
+                       $cost_adjust = true;
        }
 
+       //Negative Stock Handling First; Prior to add_stock_move
+       if ($cost_adjust)
+               adjust_deliveries($stock_id, get_standard_cost($stock_id), $date_);
 
        // -------------------------------------------------------------------------
 
        // insert a +ve stock move for the item being manufactured
-       add_stock_move(systypes::work_order(), $stock_id, $woid,        $loc_code, $date_,
+       add_stock_move(ST_WORKORDER, $stock_id, $woid,  $loc_code, $date_,
                $wo_ref, $units_reqd, 0);
 
        // -------------------------------------------------------------------------
 
-       work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $additional_costs);
+       work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $costs, $cr_acc, $labour, $cr_lab_acc);
 
        // -------------------------------------------------------------------------
 
-       add_comments(systypes::work_order(), $woid, $date_, $memo_);
+       add_comments(ST_WORKORDER, $woid, $date_, $memo_);
 
-       references::save_last($wo_ref, systypes::work_order());
+       $Refs->save(ST_WORKORDER, $woid, $wo_ref);
+       add_audit_trail(ST_WORKORDER, $woid, $date_,_("Quick production."));
+
+       $args->woid = $woid;
+       hook_db_postwrite($args, ST_WORKORDER);
 
        commit_transaction();
        return $woid;
@@ -71,7 +113,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_, $additional_costs)
+function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $costs, $cr_acc, $labour, $cr_lab_acc)
 {
        $result = get_bom($stock_id);
 
@@ -82,24 +124,39 @@ function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $addition
 
                $bom_accounts = get_stock_gl_code($bom_item["component"]);
 
-               $bom_cost = $bom_item["standard_cost"] * $bom_item["quantity"] * $units_reqd;
+               $bom_cost = $bom_item["ComponentCost"] * $units_reqd; // ??  * $bom_item["quantity"]
 
-               add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $bom_accounts["inventory_account"], 0, 0,
-                       null, -$bom_cost);
+               $memo = $bom_item["quantity"] ." * ".$bom_item["description"];
 
-               $total_cost += $bom_cost;
+               if (!is_service($bom_accounts["mb_flag"]))
+                       $ivaccount = $bom_accounts["inventory_account"];
+               else
+                       $ivaccount = $bom_accounts["assembly_account"];
+               $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,
+                       $memo, -$bom_cost);
        }
+
+       $stockitem = get_item($stock_id);
        // 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"],
-               $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"],
-               0, 0, null, $total_cost);
-}
+       if ($costs != 0.0)
+       {
+               add_wo_costs_journal($woid, $costs, WO_OVERHEAD, $cr_acc, $stockitem["assembly_account"],
+                       $date_, $stockitem["dimension_id"], $stockitem["dimension2_id"]);
+       }
 
-//--------------------------------------------------------------------------------------
+       if ($labour != 0.0)
+       {
+               add_wo_costs_journal($woid, $labour, WO_LABOUR, $cr_lab_acc, $stockitem["assembly_account"],
+                       $date_, $stockitem["dimension_id"], $stockitem["dimension2_id"]);
+       }
+
+       // debit total components $total_cost
+       $memo = _("Produced")." ".$units_reqd. " * ".$stockitem["description"];
+
+    add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $stockitem["inventory_account"],
+        0, 0, $memo, -$total_cost);
+
+       $UnitWOCost = ($total_cost + $costs + $labour)/$units_reqd;
+       update_stock_move(ST_WORKORDER, $woid, $stock_id, $UnitWOCost); 
+}
 
-?>
\ No newline at end of file