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");
39 //--------------------------------------------------------------------------------------
41 function add_material_cost($stock_id, $qty, $date_)
44 $result = get_bom($stock_id);
45 while ($bom_item = db_fetch($result))
47 $standard_cost = get_standard_cost($bom_item['component']);
48 $m_cost += ($bom_item['quantity'] * $standard_cost);
50 //$dec = user_price_dec();
51 //price_decimal_format($m_cost, $dec);
52 $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
53 .db_escape($stock_id);
54 $result = db_query($sql);
55 $myrow = db_fetch($result);
56 $material_cost = $myrow['material_cost'];
57 //$qoh = get_qoh_on_date($stock_id, null, $date_);
58 $qoh = get_qoh_on_date($stock_id);
62 $material_cost = ($qoh * $material_cost + $qty * $m_cost) / ($qoh + $qty);
63 //$material_cost = round2($material_cost, $dec);
64 $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=$material_cost
65 WHERE stock_id=".db_escape($stock_id);
66 db_query($sql,"The cost details for the inventory item could not be updated");
69 function add_overhead_cost($stock_id, $qty, $date_, $costs)
71 //$dec = user_price_dec();
72 //price_decimal_format($costs, $dec);
75 $sql = "SELECT overhead_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
76 .db_escape($stock_id);
77 $result = db_query($sql);
78 $myrow = db_fetch($result);
79 $overhead_cost = $myrow['overhead_cost'];
80 //$qoh = get_qoh_on_date($stock_id, null, $date_);
81 $qoh = get_qoh_on_date($stock_id);
85 $overhead_cost = ($qoh * $overhead_cost + $qty * $costs) / ($qoh + $qty);
86 //$overhead_cost = round2($overhead_cost, $dec);
87 $sql = "UPDATE ".TB_PREF."stock_master SET overhead_cost=".db_escape($overhead_cost)."
88 WHERE stock_id=".db_escape($stock_id);
89 db_query($sql,"The cost details for the inventory item could not be updated");
92 function add_labour_cost($stock_id, $qty, $date_, $costs)
94 //$dec = user_price_dec();
95 //price_decimal_format($costs, $dec);
98 $sql = "SELECT labour_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
99 .db_escape($stock_id);
100 $result = db_query($sql);
101 $myrow = db_fetch($result);
102 $labour_cost = $myrow['labour_cost'];
103 //$qoh = get_qoh_on_date($stock_id, null, $date_);
104 $qoh = get_qoh_on_date($stock_id);
107 if ($qoh + $qty != 0)
108 $labour_cost = ($qoh * $labour_cost + $qty * $costs) / ($qoh + $qty);
109 //$labour_cost = round2($labour_cost, $dec);
110 $sql = "UPDATE ".TB_PREF."stock_master SET labour_cost=".db_escape($labour_cost)."
111 WHERE stock_id=".db_escape($stock_id);
112 db_query($sql,"The cost details for the inventory item could not be updated");
115 function add_issue_cost($stock_id, $qty, $date_, $costs)
119 $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
120 .db_escape($stock_id);
121 $result = db_query($sql);
122 $myrow = db_fetch($result);
123 $material_cost = $myrow['material_cost'];
124 //$dec = user_price_dec();
125 //price_decimal_format($material_cost, $dec);
126 //$qoh = get_qoh_on_date($stock_id, null, $date_);
127 $qoh = get_qoh_on_date($stock_id);
130 if ($qoh + $qty != 0)
131 $material_cost = ($qty * $costs) / ($qoh + $qty);
132 //$material_cost = round2($material_cost, $dec);
133 $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=material_cost+"
134 .db_escape($material_cost)
135 ." WHERE stock_id=".db_escape($stock_id);
136 db_query($sql,"The cost details for the inventory item could not be updated");
140 Create journal entry for WO related costs.
142 function add_wo_costs_journal($wo_id, $amount, $cost_type, $cr_acc, $db_acc, $date, $dim1=0, $dim2=0, $memo = null, $ref= null)
144 //-------- this should be done by single call to write_journal_entries() using items_cart()
146 global $Refs, $wo_cost_types;
150 $journal_id = get_next_trans_no(ST_JOURNAL);
151 if (!$ref) $ref = $Refs->get_next(ST_JOURNAL);
153 add_gl_trans_std_cost(ST_JOURNAL, $journal_id, $date, $cr_acc,
154 0, 0, $wo_cost_types[$cost_type], -$amount);
155 $is_bank_to = is_bank_account($cr_acc);
158 add_bank_trans(ST_JOURNAL, $journal_id, $is_bank_to, "",
159 $date, -$amount, PT_WORKORDER, $wo_id, get_company_currency(),
160 "Cannot insert a destination bank transaction");
163 add_gl_trans_std_cost(ST_JOURNAL, $journal_id, $date, $db_acc,
164 $dim1, $dim2, $wo_cost_types[$cost_type], $amount);
166 $Refs->save(ST_JOURNAL, $journal_id, $ref);
168 add_wo_costing($wo_id, $cost_type, ST_JOURNAL, $journal_id);
170 add_comments(ST_JOURNAL, $journal_id, $date, $memo);
171 add_audit_trail(ST_JOURNAL, $journal_id, $date);
173 commit_transaction();
176 function void_wo_costing($wo_id)
178 $res = get_wo_costing($wo_id);
180 while($row = db_fetch($res))
182 // void any related gl trans
183 void_gl_trans($row['trans_type'], $row['trans_no'], true);
186 $sql = "DELETE FROM ".TB_PREF."wo_costing WHERE workorder_id=".db_escape($wo_id);
187 db_query($sql, "could not delete work order costing");