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 ***********************************************************************/
12 function work_order_produce($woid, $ref, $quantity, $date_, $memo, $close_wo)
16 // FIXME: support for WO_UNASSEMBLY case
19 $args = func_get_args();
20 $args = (object)array_combine(array('woid', 'ref', 'quantity', 'date_', 'memo','close_wo'),
23 hook_db_prewrite($args, ST_MANURECEIVE);
25 $product = get_work_order($woid);
27 $date = date2sql($date_);
29 $sql = "INSERT INTO ".TB_PREF."wo_manufacture (workorder_id, reference, quantity, date_)
30 VALUES (".db_escape($woid).", ".db_escape($ref).", ".db_escape($quantity)
33 db_query($sql,"A work order manufacture could not be added");
37 // -------------------------------------------------------------------------
38 // insert -ve and update averaged component unit cost for BOM usage (in wo_requirements)
39 work_order_production_gl($woid, $product["stock_id"], $quantity, $date_, $id);
41 // update wo quantity and close wo if requested (or finished)
42 $closed = work_order_update_finished_quantity($woid, $quantity, $date_, $close_wo);
44 // unit_cost is known when WO is finished, then generate +ve for all items
47 // 1. calculate sums of material/labour/overhead costs
49 // sum collected BOM material & labour costs (no way for separate overhead here for now - needs flag in bom or stock_master)
50 $bom = get_wo_requirements($woid);
51 $m_cost = $l_cost = 0;
52 while ($component = db_fetch($bom))
54 if (!is_service($component["mb_flag"]))
55 $m_cost += $component['unit_cost']*$component['units_issued'];
57 $l_cost += $component['unit_cost']*$component['units_issued'];
60 // add additional material issues
61 $issues = get_additional_issues($woid);
62 while ($issue = db_fetch($issues))
64 if (!is_service($issue["mb_flag"]))
65 $m_cost += $issue['unit_cost']*$issue['qty_issued'];
67 $l_cost += $issue['unit_cost']*$issue['qty_issued'];
70 // and additional costs
71 $o_cost = get_gl_wo_cost($woid, WO_OVERHEAD);
72 $l_cost += get_gl_wo_cost($woid, WO_LABOUR);
74 $total_cost = $o_cost + $m_cost + $l_cost;
76 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $product['assembly_account'],
77 0, 0, $memo, -$total_cost);
79 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $product['inventory_account'],
80 0, 0, $memo, $total_cost);
82 $unit_cost = $total_cost/($product['units_issued']+$quantity);
84 add_stock_move(ST_WORKORDER, $product["stock_id"], $woid,
85 $product["loc_code"], $date_, $ref, $product['units_issued']+$quantity, $unit_cost);
87 update_material_cost($product['stock_id'], $product['units_issued']+$quantity, $unit_cost, $date_);
91 add_comments(ST_MANURECEIVE, $id, $date_, $memo);
93 $Refs->save(ST_MANURECEIVE, $id, $ref);
94 add_audit_trail(ST_MANURECEIVE, $id, $date_, _("Production."));
96 $args->trans_no = $id;
97 hook_db_postwrite($args, ST_MANURECEIVE);
102 //--------------------------------------------------------------------------------------------
104 function get_work_order_produce($id)
106 $sql = "SELECT prod.*, wo.stock_id, item.description AS StockDescription
107 FROM ".TB_PREF."wo_manufacture prod,"
108 .TB_PREF."workorders wo,"
109 .TB_PREF."stock_master item
110 WHERE prod.workorder_id=wo.id
111 AND item.stock_id=wo.stock_id
112 AND prod.id=".db_escape($id);
113 $result = db_query($sql, "The work order production could not be retrieved");
115 return db_fetch($result);
118 //--------------------------------------------------------------------------------------
120 function get_work_order_productions($woid)
122 $sql = "SELECT * FROM ".TB_PREF."wo_manufacture WHERE workorder_id="
123 .db_escape($woid)." ORDER BY id";
124 return db_query($sql, "The work order issues could not be retrieved");
127 //--------------------------------------------------------------------------------------
129 function exists_work_order_produce($id)
131 $sql = "SELECT id FROM ".TB_PREF."wo_manufacture WHERE id=".db_escape($id);
132 $result = db_query($sql, "Cannot retreive a wo production");
134 return (db_num_rows($result) > 0);
137 //--------------------------------------------------------------------------------------------
139 function void_work_order_produce($type_no)
142 hook_db_prevoid(ST_MANURECEIVE, $type_no);
144 // Skip processing already voided entry i.e. explicitly voided
145 $void_entry = get_voided_entry(ST_MANURECEIVE, $type_no);
149 $row = get_work_order_produce($type_no);
151 // deduct the quantity of this production from the parent work order
152 work_order_update_finished_quantity($row["workorder_id"], -$row["quantity"]);
154 // void any related gl trans
156 $woid = $row["workorder_id"];
157 $date_ = sql2date($row["date_"]);
159 $result = get_stock_moves(ST_MANURECEIVE, $type_no);
160 while ($myrow = db_fetch($result))
162 $issue_cost = $myrow["qty"]*$myrow["standard_cost"];
163 $issue = get_stock_gl_code($myrow["stock_id"]);
164 $stockitem = get_item($myrow["stock_id"]);
166 // Compatibility for Service Items
167 if (!is_service($issue["mb_flag"]))
168 $ivaccount = $issue["inventory_account"];
170 $ivaccount = $issue["cogs_account"];
172 if ($issue_cost != 0)
174 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,
175 $date_.": "._("Reversed the production ")." ".$stockitem["description"],
179 // clear the production record
180 $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".db_escape($type_no);
181 db_query($sql, "Cannot void a wo production");
184 // void all related stock moves
185 void_stock_move(ST_MANURECEIVE, $type_no);
187 commit_transaction();