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 ***********************************************************************/
13 function add_wo_costing($workorder_id, $cost_type, $trans_type, $trans_no, $factor=1)
15 $sql = "INSERT INTO ".TB_PREF."wo_costing (workorder_id, cost_type, trans_type, trans_no, factor)
16 VALUES (".db_escape($workorder_id).","
17 .db_escape($cost_type).",".db_escape($trans_type).","
18 .db_escape($trans_no).",".db_escape($factor).")";
20 db_query($sql, "could not add work order costing");
24 function get_wo_costing($workorder_id)
26 $sql="SELECT * FROM ".TB_PREF."wo_costing WHERE workorder_id=".db_escape($workorder_id);
28 return db_query($sql, "could not get work order costing");
31 function delete_wo_costing($trans_type, $trans_no)
33 $sql="DELETE FROM ".TB_PREF."wo_costing WHERE trans_type=".db_escape($trans_type)
34 ." AND trans_no=".db_escape($trans_no);
36 db_query($sql, "could not delete work order costing");
38 //--------------------------------------------------------------------------------------
40 function add_material_cost($stock_id, $qty, $date_, $advanced=false, $woid=0)
43 $result = get_bom($stock_id);
44 while ($bom_item = db_fetch($result))
46 $standard_cost = get_standard_cost($bom_item['component']);
47 $m_cost += ($bom_item['quantity'] * $standard_cost);
51 if ($woid != 0 && work_order_has_issues($woid))
53 $res = get_additional_issues($woid);
54 while ($issue = db_fetch($res))
56 $standard_cost = get_standard_cost($issue['stock_id']);
57 $i_cost += ($issue['qty_issued'] * $standard_cost) / $qty;
60 $sql = "SELECT material_cost, labour_cost, overhead_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
61 .db_escape($stock_id);
62 $result = db_query($sql);
63 $myrow = db_fetch($result);
64 $material_cost = $myrow['material_cost'];
69 //reduce overhead_cost and labour_cost from price as those will remain as is
70 $m_cost = $m_cost - $myrow['labour_cost'] - $myrow['overhead_cost'];
73 $qoh = get_qoh_on_date($stock_id);
84 $material_cost = $m_cost;
86 $material_cost = ($qoh * $material_cost + $qty * $m_cost) / ($qoh + $qty);
89 if ($advanced && $cost_adjust)
90 adjust_deliveries($stock_id, $bom_cost, $date_);
92 $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=".db_escape($material_cost)."
93 WHERE stock_id=".db_escape($stock_id);
94 db_query($sql,"The cost details for the inventory item could not be updated");
98 Updates average material cost for item.
100 function update_material_cost($stock_id, $qty, $unit_cost, $date)
104 $product = get_item($stock_id);
105 $avg_cost = $product['material_cost'];
107 $qoh = get_qoh_on_date($stock_id, null, $date);
109 // unit_cost*qty is already posted on inventory GL
114 $qty_done = max(-$qoh, $qty);
119 $qty_done = max($qoh+$qty, 0);
123 $qty_delayed = $qty-$qty_done;
127 $qty_delayed = min(-$qoh, $qty);
131 $qty_new = $qty - $qty_delayed;
133 if ($qty_delayed > 0 && ($unit_cost != $avg_cost))
135 // post $qty_delayed*($avg_cost-$unit_cost)
136 $diff = $qty_delayed*($unit_cost-$avg_cost);
137 $stock_gl_code = get_item($stock_id);
139 $dec = user_price_dec();
140 $old_cost = -round2($avg_cost, $dec);
141 $new_cost = -round2($unit_cost, $dec);
143 $cart = new items_cart(ST_COSTUPDATE);
144 $cart->tran_date = $cart->doc_date = $cart->event_date = $date;
146 if (!is_date_in_fiscalyear($cart->tran_date))
147 $cart->tran_date = end_fiscalyear();
149 $cart->reference = $Refs->get_next(ST_COSTUPDATE, null, $cart->tran_date, $date);
151 $cart->memo_ = sprintf(_("COGS changed from %s to %s for %d %s of '%s'"), $avg_cost, $unit_cost, $qty_delayed, $item['units'], $stock_id);
153 $cart->add_gl_item($item["cogs_account"], $item["dimension_id"], $item["dimension2_id"], -$diff);
154 $cart->add_gl_item($item["inventory_account"], 0, 0, $diff);
156 write_journal_entries($cart);
159 if ($qty > 0 && ($qoh != -$qty))
160 $avg_cost = ($avg_cost*($qoh+$qty_delayed)+$unit_cost*$qty_new)/($qoh+$qty);
162 $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=".db_escape($avg_cost)."
163 WHERE stock_id=".db_escape($stock_id);
164 db_query($sql,"The cost details for the inventory item could not be updated");
167 function add_overhead_cost($stock_id, $qty, $date_, $costs, $adj_only=false)
171 $sql = "SELECT overhead_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
172 .db_escape($stock_id);
173 $result = db_query($sql);
174 $myrow = db_fetch($result);
175 $overhead_cost = $myrow['overhead_cost'];
176 $qoh = get_qoh_on_date($stock_id);
183 if ($qoh + $qty != 0)
184 $overhead_cost = ($qoh * $overhead_cost + $qty * $costs) / ($qoh + $qty);
186 $overhead_cost = ($qoh * $overhead_cost + $costs) / $qoh;
188 else // Journal Entry if QOH is 0/negative
192 $id = get_next_trans_no(ST_JOURNAL);
193 $ref = $Refs->get_next(ST_JOURNAL, null, $date_);
195 $stock_gl_code = get_stock_gl_code($stock_id);
196 add_journal(ST_JOURNAL, $id, $costs, $date_, get_company_currency(), $ref);
197 $memo = "WO Overhead cost settlement JV for zero/negative respository of ".$stock_id;
198 //Reverse the inventory effect if $qoh <=0
199 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
200 $stock_gl_code["inventory_account"],
201 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
203 //GL Posting to inventory adjustment account
204 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
205 $stock_gl_code["assembly_account"],
206 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
209 add_audit_trail(ST_JOURNAL, $id, $date_);
210 add_comments(ST_JOURNAL, $id, $date_, $memo);
211 $Refs->save(ST_JOURNAL, $id, $ref);
213 $overhead_cost = ($qoh * $overhead_cost + $costs) / $qty;
218 if ($qoh + $qty != 0)
219 $overhead_cost = ($qoh * $overhead_cost + $qty * $costs) / ($qoh + $qty);
221 $sql = "UPDATE ".TB_PREF."stock_master SET overhead_cost=".db_escape($overhead_cost)."
222 WHERE stock_id=".db_escape($stock_id);
223 db_query($sql,"The cost details for the inventory item could not be updated");
226 function add_labour_cost($stock_id, $qty, $date_, $costs, $adj_only=false)
230 $sql = "SELECT labour_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
231 .db_escape($stock_id);
232 $result = db_query($sql);
233 $myrow = db_fetch($result);
234 $labour_cost = $myrow['labour_cost'];
235 $qoh = get_qoh_on_date($stock_id);
242 if ($qoh + $qty != 0)
243 $labour_cost = ($qoh * $labour_cost + $qty * $costs) / ($qoh + $qty);
245 $labour_cost = ($qoh * $labour_cost + $costs) / $qoh;
247 else // Journal Entry if QOH is 0/negative
251 $id = get_next_trans_no(ST_JOURNAL);
252 $ref = $Refs->get_next(ST_JOURNAL, null, $date_);
253 add_journal(ST_JOURNAL, $id, $costs, $date_, get_company_currency(), $ref);
255 $stock_gl_code = get_stock_gl_code($stock_id);
256 $memo = "WO labour cost settlement JV for zero/negative respository of ".$stock_id;
257 //Reverse the inventory effect if $qoh <=0
258 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
259 $stock_gl_code["inventory_account"],
260 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
262 //GL Posting to inventory adjustment account
263 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
264 $stock_gl_code["assembly_account"],
265 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
268 add_audit_trail(ST_JOURNAL, $id, $date_);
269 add_comments(ST_JOURNAL, $id, $date_, $memo);
270 $Refs->save(ST_JOURNAL, $id, $ref);
272 $labour_cost = ($qoh * $labour_cost + $costs) / $qty;
277 if ($qoh + $qty != 0)
278 $labour_cost = ($qoh * $labour_cost + $qty * $costs) / ($qoh + $qty);
280 $sql = "UPDATE ".TB_PREF."stock_master SET labour_cost=".db_escape($labour_cost)."
281 WHERE stock_id=".db_escape($stock_id);
282 db_query($sql,"The cost details for the inventory item could not be updated");
285 function add_issue_cost($stock_id, $qty, $date_, $costs, $adj_only=false)
289 $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
290 .db_escape($stock_id);
291 $result = db_query($sql);
292 $myrow = db_fetch($result);
293 $material_cost = $myrow['material_cost'];
294 $qoh = get_qoh_on_date($stock_id);
300 $material_cost = ($qoh * $material_cost + $costs) / $qoh;
301 else // Journal Entry if QOH is 0/negative
305 $id = get_next_trans_no(ST_JOURNAL);
306 $ref = $Refs->get_next(ST_JOURNAL, null, $date_);
307 add_journal(ST_JOURNAL, $id, $costs, $date_, get_company_currency(), $ref);
309 $stock_gl_code = get_stock_gl_code($stock_id);
310 $memo = "WO Issue settlement JV for zero/negative respository of ".$stock_id;
311 //Reverse the inventory effect if $qoh <=0
312 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
313 $stock_gl_code["inventory_account"],
314 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
316 //GL Posting to inventory adjustment account
317 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
318 $stock_gl_code["assembly_account"],
319 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
322 add_audit_trail(ST_JOURNAL, $id, $date_);
323 add_comments(ST_JOURNAL, $id, $date_, $memo);
324 $Refs->save(ST_JOURNAL, $id, $ref);
326 $material_cost = $costs / $qty;
331 if ($qoh + $qty != 0)
332 $material_cost = ($qoh * $material_cost + $qty * $costs) / ($qoh + $qty);
334 $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=material_cost+"
335 .db_escape($material_cost)
336 ." WHERE stock_id=".db_escape($stock_id);
337 db_query($sql,"The cost details for the inventory item could not be updated");
341 Create journal entry for WO related costs.
343 function add_wo_costs_journal($wo_id, $amount, $cost_type, $cr_acc, $db_acc, $date, $dim1=0, $dim2=0, $memo = null, $ref= null)
345 //-------- this should be done by single call to write_journal_entries() using items_cart()
347 global $Refs, $wo_cost_types;
351 $journal_id = get_next_trans_no(ST_JOURNAL);
353 if (!$ref) $ref = $Refs->get_next(ST_JOURNAL, null, $date);
355 add_gl_trans_std_cost(ST_JOURNAL, $journal_id, $date, $cr_acc,
356 0, 0, $wo_cost_types[$cost_type], -$amount);
357 $is_bank_to = is_bank_account($cr_acc);
360 add_bank_trans(ST_JOURNAL, $journal_id, $is_bank_to, "",
361 $date, -$amount, PT_WORKORDER, $wo_id, get_company_currency(),
362 "Cannot insert a destination bank transaction");
364 add_journal(ST_JOURNAL, $journal_id, $amount, $date, get_company_currency(), $ref);
366 add_gl_trans_std_cost(ST_JOURNAL, $journal_id, $date, $db_acc,
367 $dim1, $dim2, $wo_cost_types[$cost_type], $amount);
369 $Refs->save(ST_JOURNAL, $journal_id, $ref);
371 add_wo_costing($wo_id, $cost_type, ST_JOURNAL, $journal_id);
373 add_comments(ST_JOURNAL, $journal_id, $date, $memo);
374 add_audit_trail(ST_JOURNAL, $journal_id, $date);
376 commit_transaction();
379 function void_wo_costing($wo_id)
381 $res = get_wo_costing($wo_id);
383 while($row = db_fetch($res))
385 // void any related gl trans
386 void_gl_trans($row['trans_type'], $row['trans_no'], true);
389 $sql = "DELETE FROM ".TB_PREF."wo_costing WHERE workorder_id=".db_escape($wo_id);
390 db_query($sql, "could not delete work order costing");