2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
13 Quick work order entry, $type is either WO_ASSEMBLY, or WO_UNASSEMBLY
14 All changes in material and manufactured item stock are made during work order entry.
16 function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type,
17 $date_, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc)
22 $args = func_get_args();
23 $args = (object)array_combine(array('wo_ref', 'loc_code', 'units_reqd', 'stock_id',
24 'type', 'date_', 'memo_', 'costs', 'cr_acc', 'labour', 'cr_lab_acc'), $args);
26 hook_db_prewrite($args, ST_WORKORDER);
28 // if unassembling, reverse the stock movements
29 if ($type == WO_UNASSEMBLY)
30 $units_reqd = -$units_reqd;
32 add_material_cost($stock_id, $units_reqd, $date_);
34 $date = date2sql($date_);
35 if (!isset($costs) || ($costs == ""))
37 add_overhead_cost($stock_id, $units_reqd, $date_, $costs);
38 if (!isset($labour) || ($labour == ""))
40 add_labour_cost($stock_id, $units_reqd, $date_, $labour);
42 $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, units_issued, stock_id,
43 type, additional_costs, date_, released_date, required_by, released, closed)
44 VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", ".db_escape($units_reqd)
45 .", ".db_escape($units_reqd).", ".db_escape($stock_id).",
46 ".db_escape($type).", ".db_escape($costs).", '$date', '$date', '$date', 1, 1)";
47 db_query($sql, "could not add work order");
49 $woid = db_insert_id();
51 //--------------------------------------------------------------------------
53 // create Work Order Requirements based on the bom
54 $result = get_bom($stock_id);
56 while ($bom_item = db_fetch($result))
59 $unit_quantity = $bom_item["quantity"];
60 $item_quantity = $bom_item["quantity"] * $units_reqd;
63 $sql = "INSERT INTO ".TB_PREF."wo_requirements (workorder_id, stock_id, workcentre, units_req, units_issued, loc_code)
64 VALUES ($woid, " . "'" . $bom_item["component"] . "'" . ",
65 '". $bom_item["workcentre_added"] . "',
66 $unit_quantity, $item_quantity, '" . $bom_item["loc_code"] . "')";
68 db_query($sql, "The work order requirements could not be added");
70 // insert a -ve stock move for each item
71 $UnitCost = get_standard_cost($bom_item["component"]);
72 add_stock_move(ST_WORKORDER, $bom_item["component"], $woid,
73 $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, $UnitCost, $UnitCost);
76 // -------------------------------------------------------------------------
77 //Negative Stock Handling
78 $qoh = get_qoh_on_date($stock_id);
82 if ($qoh + $units_reqd >= 0)
86 //Negative Stock Handling First; Prior to add_stock_move
88 adjust_deliveries($stock_id, get_standard_cost($stock_id), $date_);
90 // -------------------------------------------------------------------------
92 // insert a +ve stock move for the item being manufactured
93 add_stock_move(ST_WORKORDER, $stock_id, $woid, $loc_code, $date_,
94 $wo_ref, $units_reqd, 0);
96 // -------------------------------------------------------------------------
98 work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $costs, $cr_acc, $labour, $cr_lab_acc);
100 // -------------------------------------------------------------------------
102 add_comments(ST_WORKORDER, $woid, $date_, $memo_);
104 $Refs->save(ST_WORKORDER, $woid, $wo_ref);
105 add_audit_trail(ST_WORKORDER, $woid, $date_,_("Quick production."));
108 hook_db_postwrite($args, ST_WORKORDER);
110 commit_transaction();
114 //--------------------------------------------------------------------------------------
116 function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $costs, $cr_acc, $labour, $cr_lab_acc)
118 $result = get_bom($stock_id);
120 // credit all the components
122 while ($bom_item = db_fetch($result))
125 $bom_accounts = get_stock_gl_code($bom_item["component"]);
127 $bom_cost = $bom_item["ComponentCost"] * $units_reqd; // ?? * $bom_item["quantity"]
129 $memo = $bom_item["quantity"] ." * ".$bom_item["description"];
131 if (!is_service($bom_accounts["mb_flag"]))
132 $ivaccount = $bom_accounts["inventory_account"];
134 $ivaccount = $bom_accounts["assembly_account"];
135 $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,
139 $stockitem = get_item($stock_id);
140 // credit additional costs
143 add_wo_costs_journal($woid, $costs, WO_OVERHEAD, $cr_acc, $stockitem["assembly_account"],
144 $date_, $stockitem["dimension_id"], $stockitem["dimension2_id"]);
149 add_wo_costs_journal($woid, $labour, WO_LABOUR, $cr_lab_acc, $stockitem["assembly_account"],
150 $date_, $stockitem["dimension_id"], $stockitem["dimension2_id"]);
153 // debit total components $total_cost
154 $memo = _("Produced")." ".$units_reqd. " * ".$stockitem["description"];
156 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $stockitem["inventory_account"],
157 0, 0, $memo, -$total_cost);
159 $UnitWOCost = ($total_cost + $costs + $labour)/$units_reqd;
160 update_stock_move(ST_WORKORDER, $woid, $stock_id, $UnitWOCost);