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 //--------------------------------------------------------------------------------------
14 function add_material_cost($stock_id, $qty, $date_)
17 $result = get_bom($stock_id);
18 while ($bom_item = db_fetch($result))
20 $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = '".$bom_item['component']."'";
21 $res = db_query($sql);
22 $myrow = db_fetch($res);
23 $m_cost += ($bom_item['quantity'] * $myrow['material_cost']);
25 $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = '$stock_id'";
26 $result = db_query($sql);
27 $myrow = db_fetch($result);
28 $material_cost = $myrow['material_cost'];
29 $qoh = get_qoh_on_date($stock_id, null, $date_);
33 $material_cost = ($qoh * $material_cost + $qty * $m_cost) / ($qoh + $qty);
34 $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=$material_cost
35 WHERE stock_id='$stock_id'";
36 db_query($sql,"The cost details for the inventory item could not be updated");
39 function add_work_order($wo_ref, $loc_code, $units_reqd, $stock_id,
40 $type, $date_, $required_by, $costs, $memo_)
42 if (!($type == wo_types::advanced()))
43 return add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $costs, $memo_);
47 add_material_cost($stock_id, $units_reqd, $date_);
49 $date = date2sql($date_);
50 $required = date2sql($required_by);
52 $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, stock_id,
53 type, date_, required_by)
54 VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", $units_reqd, '$stock_id',
55 $type, '$date', '$required')";
56 db_query($sql, "could not add work order");
58 $woid = db_insert_id();
60 add_comments(systypes::work_order(), $woid, $required_by, $memo_);
62 references::save_last($wo_ref, systypes::work_order());
69 //--------------------------------------------------------------------------------------
71 function update_work_order($woid, $loc_code, $units_reqd, $stock_id,
72 $date_, $required_by, $memo_)
76 add_material_cost($_POST['old_stk_id'], -$_POST['old_qty'], $date_);
77 add_material_cost($stock_id, $units_reqd, $date_);
79 $date = date2sql($date_);
80 $required = date2sql($required_by);
82 $sql = "UPDATE ".TB_PREF."workorders SET loc_code=".db_escape($loc_code).",
83 units_reqd=$units_reqd, stock_id='$stock_id',
84 required_by='$required',
88 db_query($sql, "could not update work order");
90 update_comments(systypes::work_order(), $woid, null, $memo_);
95 function delete_work_order($woid)
99 add_material_cost($_POST['stock_id'], -$_POST['quantity'], $_POST['date_']);
101 // delete the work order requirements
102 delete_wo_requirements($woid);
104 // delete the actual work order
105 $sql = "DELETE FROM ".TB_PREF."workorders WHERE id=$woid";
106 db_query($sql,"The work order could not be deleted");
108 delete_comments(systypes::work_order(), $woid);
110 commit_transaction();
113 //--------------------------------------------------------------------------------------
115 function get_work_order($woid, $allow_null=false)
117 $sql = "SELECT ".TB_PREF."workorders.*, ".TB_PREF."stock_master.description As StockItemName,
118 ".TB_PREF."locations.location_name
119 FROM ".TB_PREF."workorders, ".TB_PREF."stock_master, ".TB_PREF."locations
120 WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."workorders.stock_id
121 AND ".TB_PREF."locations.loc_code=".TB_PREF."workorders.loc_code
122 AND ".TB_PREF."workorders.id=$woid
123 GROUP BY ".TB_PREF."workorders.id";
125 $result = db_query($sql, "The work order issues could not be retrieved");
127 if (!$allow_null && db_num_rows($result) == 0)
128 display_db_error("Could not find work order $woid", $sql);
130 return db_fetch($result);
133 //--------------------------------------------------------------------------------------
135 function work_order_has_productions($woid)
137 $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_manufacture WHERE workorder_id=$woid";
138 $result = db_query($sql, "query work order for productions");
140 $myrow = db_fetch_row($result);
141 return ($myrow[0] > 0);
145 //--------------------------------------------------------------------------------------
147 function work_order_has_issues($woid)
149 $sql = "SELECT COUNT(*) FROM ".TB_PREF."wo_issues WHERE workorder_id=$woid";
150 $result = db_query($sql, "query work order for issues");
152 $myrow = db_fetch_row($result);
153 return ($myrow[0] > 0);
156 //--------------------------------------------------------------------------------------
158 function work_order_has_payments($woid)
160 $result = get_bank_trans(null, null, payment_person_types::WorkOrder(), $woid);
162 return (db_num_rows($result) != 0);
165 //--------------------------------------------------------------------------------------
167 function release_work_order($woid, $releaseDate, $memo_)
171 $myrow = get_work_order($woid);
172 $stock_id = $myrow["stock_id"];
174 $date = date2sql($releaseDate);
176 $sql = "UPDATE ".TB_PREF."workorders SET released_date='$date',
177 released=1 WHERE id = $woid";
178 db_query($sql, "could not release work order");
180 // create Work Order Requirements based on the bom
181 create_wo_requirements($woid, $stock_id);
183 add_comments(systypes::work_order(), $woid, $releaseDate, $memo_);
185 commit_transaction();
188 //--------------------------------------------------------------------------------------
190 function close_work_order($woid)
192 $sql = "UPDATE ".TB_PREF."workorders SET closed=1 WHERE id = $woid";
193 db_query($sql, "could not close work order");
196 //--------------------------------------------------------------------------------------
198 function work_order_is_closed($woid)
200 $sql = "SELECT closed FROM ".TB_PREF."workorders WHERE id = $woid";
201 $result = db_query($sql, "could not query work order");
202 $row = db_fetch_row($result);
203 return ($row[0] > 0);
206 //--------------------------------------------------------------------------------------
208 function work_order_update_finished_quantity($woid, $quantity, $force_close=0)
210 $sql = "UPDATE ".TB_PREF."workorders SET units_issued = units_issued + $quantity,
211 closed = ((units_issued >= units_reqd) OR $force_close)
214 db_query($sql, "The work order issued quantity couldn't be updated");
217 //--------------------------------------------------------------------------------------
219 function void_work_order($woid)
221 $work_order = get_work_order($woid);
223 if (!($work_order["type"] == wo_types::advanced()))
227 $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_issued=0 WHERE id = $woid";
228 db_query($sql, "The work order couldn't be voided");
230 // void all related stock moves
231 void_stock_move(systypes::work_order(), $woid);
233 // void any related gl trans
234 void_gl_trans(systypes::work_order(), $woid, true);
236 // clear the requirements units received
237 void_wo_requirements($woid);
239 commit_transaction();
243 // void everything inside the work order : issues, productions, payments
247 //--------------------------------------------------------------------------------------