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
17 begin_transaction(__FUNCTION__, func_get_args());
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, $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['wip_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 // first update material cost
85 update_material_cost($product['stock_id'], $product['units_issued']+$quantity, $unit_cost, $date_);
87 add_stock_move(ST_WORKORDER, $product["stock_id"], $woid,
88 $product["loc_code"], $date_, $ref, $product['units_issued']+$quantity, $unit_cost);
92 add_comments(ST_MANURECEIVE, $id, $date_, $memo);
94 $Refs->save(ST_MANURECEIVE, $id, $ref);
95 add_audit_trail(ST_MANURECEIVE, $id, $date_, _("Production."));
97 $args->trans_no = $id;
98 hook_db_postwrite($args, ST_MANURECEIVE);
100 commit_transaction();
103 //--------------------------------------------------------------------------------------------
105 function get_work_order_produce($id)
107 $sql = "SELECT prod.*, wo.stock_id, item.description AS StockDescription, wo.closed
108 FROM ".TB_PREF."wo_manufacture prod,"
109 .TB_PREF."workorders wo,"
110 .TB_PREF."stock_master item
111 WHERE prod.workorder_id=wo.id
112 AND item.stock_id=wo.stock_id
113 AND prod.id=".db_escape($id);
114 $result = db_query($sql, "The work order production could not be retrieved");
116 return db_fetch($result);
119 //--------------------------------------------------------------------------------------
121 function get_work_order_productions($woid)
123 $sql = "SELECT * FROM ".TB_PREF."wo_manufacture WHERE workorder_id="
124 .db_escape($woid)." ORDER BY id";
125 return db_query($sql, "The work order issues could not be retrieved");
128 //--------------------------------------------------------------------------------------
130 function exists_work_order_produce($id)
132 $sql = "SELECT id FROM ".TB_PREF."wo_manufacture WHERE id=".db_escape($id);
133 $result = db_query($sql, "Cannot retreive a wo production");
135 return (db_num_rows($result) > 0);
138 //--------------------------------------------------------------------------------------
140 function check_void_wo_production($prod_no)
142 $prod = get_work_order_produce($prod_no);
143 return $prod['closed'] ? $prod['workorder_id'] : 0;
146 //--------------------------------------------------------------------------------------------
148 function void_work_order_produce($type_no)
150 begin_transaction(__FUNCTION__, func_get_args());
151 hook_db_prevoid(ST_MANURECEIVE, $type_no);
153 // Skip processing already voided entry i.e. explicitly voided
154 $void_entry = get_voided_entry(ST_MANURECEIVE, $type_no);
158 $prod = get_work_order_produce($type_no);
160 if (work_order_is_closed($prod['workorder_id']))
163 // deduct the quantity of this production from the parent work order
164 work_order_update_finished_quantity($prod["workorder_id"], -$prod["quantity"], false);
166 // clear the production record
167 $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".db_escape($type_no);
168 db_query($sql, "Cannot void a wo production");
170 void_gl_trans(ST_MANURECEIVE, $type_no);
172 void_stock_move(ST_MANURECEIVE, $type_no);
174 commit_transaction();