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_work_order_issue($woid, $ref, $to_work_order, $items, $location, $workcentre,
20 $args = func_get_args();
21 $args = (object)array_combine(array('woid', 'ref', 'to_work_order', 'items', 'location',
22 'workcentre', 'date_', 'memo_'), $args);
24 hook_db_prewrite($args, ST_MANUISSUE);
26 $sql = "INSERT INTO ".TB_PREF."wo_issues (workorder_id, reference, issue_date, loc_code, workcentre_id)
27 VALUES (".db_escape($woid).", ".db_escape($ref).", '" .
28 date2sql($date_) . "', ".db_escape($location).", ".db_escape($workcentre).")";
29 db_query($sql,"The work order issue could not be added");
31 $number = db_insert_id();
33 $issue_total = $total_cost = 0;
35 $wo = get_work_order($woid);
37 foreach ($items as $item)
40 $item->quantity = -$item->quantity;
42 $unit_cost = get_unit_cost($item->stock_id);
43 // insert a -ve stock move for each item
44 add_stock_move(ST_MANUISSUE, $item->stock_id, $number,
45 $location, $date_, $memo_, -$item->quantity, $unit_cost);
47 $sql = "INSERT INTO ".TB_PREF."wo_issue_items (issue_id, stock_id, qty_issued, unit_cost)
48 SELECT ".db_escape($number).",".db_escape($item->stock_id).",".db_escape($item->quantity).", material_cost
49 FROM ".TB_PREF."stock_master
50 WHERE stock_id=".db_escape($item->stock_id);
52 db_query($sql,"A work order issue item could not be added");
54 $unit_cost = get_unit_cost($item->stock_id);
55 $issue_cost = $unit_cost * $item->quantity;
57 $stockitem = get_item($item->stock_id);
59 // Compatibility for Service Items
60 if (!is_service($stockitem["mb_flag"]))
61 $ivaccount = $stockitem["inventory_account"];
63 $ivaccount = $stockitem["assembly_account"];
65 $total_cost += add_gl_trans_std_cost(ST_MANUISSUE, $number, $date_, $ivaccount, 0, 0,
66 $date_.": "._("Issue of")." ".$stockitem["description"], -$issue_cost);
67 $issue_total += $issue_cost;
70 if ($issue_total != 0) // Apply cost to QOH as adjustment only
71 add_issue_cost($wo['stock_id'], $wo['units_reqd'], $date_, $issue_total, true);
73 $stockitem = get_item($wo['stock_id']);
74 $wip_account = get_company_pref('wip_act');
76 if (!$wip_account) // backward compatibility
77 $wip_account = $stockitem["inventory_account"];
79 add_gl_trans_std_cost(ST_MANUISSUE, $number, $date_, $wip_account,
80 0, 0, $date_.": "._("Issue to")." ".$stockitem["description"], -$total_cost);
83 add_comments(ST_MANUISSUE, $number, $date_, $memo_);
85 $Refs->save(ST_MANUISSUE, $number, $ref);
86 add_audit_trail(ST_MANUISSUE, $number, $date_);
88 $args->trans_no = $number;
89 hook_db_postwrite($args, ST_MANUISSUE);
94 //--------------------------------------------------------------------------------------
96 function get_work_order_issues($woid)
98 $sql = "SELECT * FROM ".TB_PREF."wo_issues WHERE workorder_id=".db_escape($woid)
99 ." ORDER BY issue_no";
100 return db_query($sql, "The work order issues could not be retrieved");
103 function get_additional_issues($woid)
105 $sql = "SELECT issue.*, item.*, stock.mb_flag
106 FROM ".TB_PREF."wo_issues issue, "
107 .TB_PREF."wo_issue_items item
108 LEFT JOIN ".TB_PREF."stock_master stock ON stock.stock_id=item.stock_id
109 WHERE issue.issue_no=item.issue_id
110 AND issue.workorder_id=".db_escape($woid)
111 ." ORDER BY item.id";
112 return db_query($sql, "The work order issues could not be retrieved");
114 //--------------------------------------------------------------------------------------
116 function get_work_order_issue($issue_no)
118 $sql = "SELECT DISTINCT issue.*, wo.stock_id,
119 item.description, loc.location_name, center.name AS WorkCentreName
120 FROM ".TB_PREF."wo_issues issue,"
121 .TB_PREF."workorders wo,"
122 .TB_PREF."stock_master item,"
123 .TB_PREF."locations loc,"
124 .TB_PREF."workcentres center
125 WHERE issue_no=".db_escape($issue_no)."
126 AND wo.id = issue.workorder_id
127 AND loc.loc_code = issue.loc_code
128 AND center.id = issue.workcentre_id
129 AND item.stock_id = wo.stock_id";
130 $result = db_query($sql, "A work order issue could not be retrieved");
132 return db_fetch($result);
135 //--------------------------------------------------------------------------------------
137 function get_work_order_issue_details($issue_no)
139 $sql = "SELECT issue.*, item.description, item.units
140 FROM ".TB_PREF."wo_issue_items issue,"
141 .TB_PREF."stock_master item
142 WHERE issue_id=".db_escape($issue_no)."
143 AND item.stock_id=issue.stock_id
145 return db_query($sql, "The work order issue items could not be retrieved");
148 //--------------------------------------------------------------------------------------
150 function exists_work_order_issue($issue_no)
152 $sql = "SELECT issue_no FROM ".TB_PREF."wo_issues WHERE issue_no=".db_escape($issue_no);
153 $result = db_query($sql, "Cannot retreive a wo issue");
155 return (db_num_rows($result) > 0);
158 //--------------------------------------------------------------------------------------
160 function void_work_order_issue($type_no)
163 hook_db_prevoid(ST_MANUISSUE, $type_no);
165 //Chaitanya : Skip processing already voided entry i.e. explicitly voided
166 $void_entry = get_voided_entry(ST_MANUISSUE, $type_no);
170 // void the actual issue items and their quantities
171 $sql = "UPDATE ".TB_PREF."wo_issue_items Set qty_issued = 0 WHERE issue_id="
172 .db_escape($type_no);
173 db_query($sql,"A work order issue item could not be voided");
175 // Reverse the gl posting
176 $issue = get_work_order_issue($type_no);
177 $manf_stock_id = $issue["stock_id"];
178 $date_ = sql2date($issue["issue_date"]);
179 $woid = $issue["workorder_id"];
181 $result = get_stock_moves(ST_MANUISSUE, $type_no);
184 while ($myrow = db_fetch($result))
186 $issue_cost = $myrow["qty"]*$myrow["standard_cost"];
187 $issue = get_stock_gl_code($myrow["stock_id"]);
188 $stockitem = get_item($myrow["stock_id"]);
190 // Compatibility for Service Items
191 if (!is_service($issue["mb_flag"]))
192 $ivaccount = $issue["inventory_account"];
194 $ivaccount = $issue["assembly_account"];
196 if ($issue_cost != 0)
198 $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,
199 $date_.": "._("Reversed the issue of")." ".$stockitem["description"],
201 $issue_total += $issue_cost;
204 if ($issue_total != 0)
205 // Revese cost effect on manfactured stock item as adjustment only
206 add_issue_cost($manf_stock_id, 0, $date_, $issue_total, true);
207 $issue = get_stock_gl_code($manf_stock_id);
208 $stockitem = get_item($manf_stock_id);
209 if ($total_cost != 0)
210 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $issue["inventory_account"],
211 0, 0, $date_.": "._("Reversed the issue to")." ".$stockitem["description"],
214 // Shifted below void all related stock moves
215 void_stock_move(ST_MANUISSUE, $type_no);
217 commit_transaction();
221 //--------------------------------------------------------------------------------------