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)
17 $args = func_get_args();
18 $args = (object)array_combine(array('woid', 'ref', 'quantity', 'date_', 'memo_','close_wo'),
21 hook_db_prewrite($args, ST_MANURECEIVE);
23 $details = get_work_order($woid);
25 if (strlen($details[0]) == 0)
27 echo _("The order number sent is not valid.");
31 if (work_order_is_closed($woid))
33 display_error("UNEXPECTED : Producing Items for a closed Work Order");
38 $date = date2sql($date_);
40 $sql = "INSERT INTO ".TB_PREF."wo_manufacture (workorder_id, reference, quantity, date_)
41 VALUES (".db_escape($woid).", ".db_escape($ref).", ".db_escape($quantity)
44 db_query($sql,"A work order manufacture could not be added");
48 // -------------------------------------------------------------------------
50 work_order_quick_costs($woid, $details["stock_id"], $quantity, $date_, $id);
52 // -------------------------------------------------------------------------
54 // Chaitanya: stamp BOM cost to finished item
56 $result = get_bom($details["stock_id"]);
57 while ($bom_item = db_fetch($result))
59 $standard_cost = get_standard_cost($bom_item['component']);
60 $m_cost += ($bom_item['quantity'] * $standard_cost);
62 // new Joe Hunt 2015.10.15
64 if (work_order_has_issues($woid))
66 $res = get_additional_issues($woid);
67 while ($issue = db_fetch($res))
69 $standard_cost = get_standard_cost($issue['stock_id']);
70 $m_cost += ($issue['qty_issued'] * $standard_cost) / $quantity;
73 $m_cost += (get_gl_wo_cost($woid, WO_LABOUR) / $quantity);
74 $m_cost += (get_gl_wo_cost($woid, WO_OVERHEAD) / $quantity);
76 // insert a +ve stock move for the item being manufactured
77 // negative means "unproduce" or unassemble
78 add_stock_move(ST_MANURECEIVE, $details["stock_id"], $id,
79 $details["loc_code"], $date_, $ref, $quantity, $m_cost);
80 // update wo quantity and close wo if requested
81 work_order_update_finished_quantity($woid, $quantity, $close_wo);
84 add_comments(ST_MANURECEIVE, $id, $date_, $memo_);
86 $Refs->save(ST_MANURECEIVE, $id, $ref);
87 add_audit_trail(ST_MANURECEIVE, $id, $date_, _("Production."));
89 $args->trans_no = $id;
90 hook_db_postwrite($args, ST_MANURECEIVE);
94 //--------------------------------------------------------------------------------------------
96 function get_work_order_produce($id)
98 $sql = "SELECT ".TB_PREF."wo_manufacture.*,".TB_PREF."workorders.stock_id, "
99 .TB_PREF."stock_master.description AS StockDescription
100 FROM ".TB_PREF."wo_manufacture, ".TB_PREF."workorders, ".TB_PREF."stock_master
101 WHERE ".TB_PREF."wo_manufacture.workorder_id=".TB_PREF."workorders.id
102 AND ".TB_PREF."stock_master.stock_id=".TB_PREF."workorders.stock_id
103 AND ".TB_PREF."wo_manufacture.id=".db_escape($id);
104 $result = db_query($sql, "The work order production could not be retrieved");
106 return db_fetch($result);
109 //--------------------------------------------------------------------------------------
111 function get_work_order_productions($woid)
113 $sql = "SELECT * FROM ".TB_PREF."wo_manufacture WHERE workorder_id="
114 .db_escape($woid)." ORDER BY id";
115 return db_query($sql, "The work order issues could not be retrieved");
118 //--------------------------------------------------------------------------------------
120 function exists_work_order_produce($id)
122 $sql = "SELECT id FROM ".TB_PREF."wo_manufacture WHERE id=".db_escape($id);
123 $result = db_query($sql, "Cannot retreive a wo production");
125 return (db_num_rows($result) > 0);
128 //--------------------------------------------------------------------------------------------
130 function void_work_order_produce($type_no)
133 hook_db_prevoid(ST_MANURECEIVE, $type_no);
135 //Chaitanya : Skip processing already voided entry i.e. explicitly voided
136 $void_entry = get_voided_entry(ST_MANURECEIVE, $type_no);
140 $row = get_work_order_produce($type_no);
142 // deduct the quantity of this production from the parent work order
143 work_order_update_finished_quantity($row["workorder_id"], -$row["quantity"]);
145 //Chaitanya : skipped this step as BOM may have got changed
146 //work_order_quick_costs($row['workorder_id'], $row['stock_id'], -$row['quantity'], sql2date($row['date_']), $type_no);
148 // void any related gl trans
149 //Chaitanya : Nothing happens due to next statement as all gl postings are done against WO
150 //void_gl_trans(ST_MANURECEIVE, $type_no, true);
152 $woid = $row["workorder_id"];
153 $date_ = sql2date($row["date_"]);
155 $result = get_stock_moves(ST_MANURECEIVE, $type_no);
156 while ($myrow = db_fetch($result))
158 $issue_cost = $myrow["qty"]*$myrow["standard_cost"];
159 $issue = get_stock_gl_code($myrow["stock_id"]);
160 $stockitem = get_item($myrow["stock_id"]);
162 //Chaitanya : Compatibility for Service Items
163 if (!is_service($issue["mb_flag"]))
164 $ivaccount = $issue["inventory_account"];
166 $ivaccount = $issue["cogs_account"];
168 if ($issue_cost != 0)
170 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,
171 $date_.": "._("Reversed the production ")." ".$stockitem["description"],
176 // clear the production record
177 $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".db_escape($type_no);
178 db_query($sql, "Cannot void a wo production");
180 //Chaitanya : Shifted below
181 // void all related stock moves
182 void_stock_move(ST_MANURECEIVE, $type_no);
184 commit_transaction();