Reorganized work order costing to make corect registration of costs in journal.
[fa-stable.git] / manufacturing / includes / db / work_order_costing_db.inc
1 <?php
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
13 function add_wo_costing($workorder_id, $cost_type, $trans_type, $trans_no, $factor=1)
14 {
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).")";
19
20         db_query($sql, "could not add work order costing");
21
22 }
23
24 function get_wo_costing($workorder_id)
25 {
26         $sql="SELECT * FROM ".TB_PREF."wo_costing WHERE workorder_id=".db_escape($workorder_id);
27
28         return db_query($sql, "could not get work order costing");
29 }
30
31 function delete_wo_costing($trans_type, $trans_no)
32 {
33         $sql="DELETE FROM ".TB_PREF."wo_costing WHERE trans_type=".db_escape($trans_type)
34                 ." AND trans_no=".db_escape($trans_no);
35
36         db_query($sql, "could not delete work order costing");
37 }
38
39 //--------------------------------------------------------------------------------------
40
41 function add_material_cost($stock_id, $qty, $date_)
42 {
43         $m_cost = 0;
44     $result = get_bom($stock_id);
45         while ($bom_item = db_fetch($result))
46         {
47                 $standard_cost = get_standard_cost($bom_item['component']);
48                 $m_cost += ($bom_item['quantity'] * $standard_cost);
49         }
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);
59         if ($qoh < 0)
60                 $qoh = 0;
61         if ($qoh + $qty != 0)   
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");
67 }
68
69 function add_overhead_cost($stock_id, $qty, $date_, $costs)
70 {
71         //$dec = user_price_dec();
72         //price_decimal_format($costs, $dec); 
73         if ($qty != 0)
74                 $costs /= $qty;
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);
82         if ($qoh < 0)
83                 $qoh = 0;
84         if ($qoh + $qty != 0)   
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");
90 }
91
92 function add_labour_cost($stock_id, $qty, $date_, $costs)
93 {
94         //$dec = user_price_dec();
95         //price_decimal_format($costs, $dec); 
96         if ($qty != 0)
97                 $costs /= $qty;
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);
105         if ($qoh < 0)
106                 $qoh = 0;
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");
113 }
114
115 function add_issue_cost($stock_id, $qty, $date_, $costs)
116 {
117         if ($qty != 0)
118                 $costs /= $qty;
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);
128         if ($qoh < 0)
129                 $qoh = 0;
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");
137 }
138
139 /*
140         Create journal entry for WO related costs.
141 */
142 function add_wo_costs_journal($wo_id, $amount, $cost_type, $cr_acc, $db_acc, $date, $dim1=0, $dim2=0, $memo = null, $ref= null)
143 {
144 //-------- this should be done by single call to write_journal_entries() using items_cart()
145 //
146         global $Refs, $wo_cost_types;
147
148         begin_transaction();
149
150     $journal_id = get_next_trans_no(ST_JOURNAL);
151     if (!$ref) $ref = $Refs->get_next(ST_JOURNAL);
152
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);
156         if ($is_bank_to)
157         {
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");
161         }
162
163         add_gl_trans_std_cost(ST_JOURNAL, $journal_id, $date, $db_acc,
164                 $dim1, $dim2,  $wo_cost_types[$cost_type], $amount);
165
166         $Refs->save(ST_JOURNAL, $journal_id, $ref);
167
168         add_wo_costing($wo_id, $cost_type, ST_JOURNAL, $journal_id);
169
170         add_comments(ST_JOURNAL, $journal_id, $date, $memo);
171         add_audit_trail(ST_JOURNAL, $journal_id, $date);
172
173         commit_transaction();
174 }
175
176 function void_wo_costing($wo_id)
177 {
178         $res = get_wo_costing($wo_id);
179
180         while($row = db_fetch($res))
181         {
182                 // void any related gl trans
183                 void_gl_trans($row['trans_type'], $row['trans_no'], true);
184         }
185
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");
188 }