Further changes in manufacturing, added unit_cost in issues and requirements
[fa-stable.git] / manufacturing / includes / db / work_order_produce_items_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 function work_order_produce($woid, $ref, $quantity, $date_, $memo_, $close_wo)
13 {
14         global $Refs;
15
16         begin_transaction();
17
18         $args = func_get_args();
19         $args = (object)array_combine(array('woid', 'ref', 'quantity', 'date_', 'memo_','close_wo'),
20                 $args);
21         $args->trans_no = 0;
22         hook_db_prewrite($args, ST_MANURECEIVE);
23
24         $product = get_work_order($woid);
25
26     $date = date2sql($date_);
27
28     $sql = "INSERT INTO ".TB_PREF."wo_manufacture (workorder_id, reference, quantity, date_)
29                 VALUES (".db_escape($woid).", ".db_escape($ref).", ".db_escape($quantity)
30                 .", '$date')";
31
32         db_query($sql,"A work order manufacture could not be added");
33
34         $id = db_insert_id();
35
36         // -------------------------------------------------------------------------
37         // insert -ve and update averaged component unit cost for BOM usage (in wo_requirements)
38         work_order_receive_costs($woid, $product["stock_id"], $quantity, $date_, $id);
39
40         // update wo quantity and close wo if requested (or finished)
41         $closed = work_order_update_finished_quantity($woid, $quantity, $date_, $close_wo);
42
43         // unit_cost is known when WO is finished, then generate +ve for all items
44         if ($closed)
45         {
46                 // 1. calculate sums of material/labour/overhead costs
47
48                 // sum collected BOM material & labour costs (no way for separate overhead here for now - needs flag in bom or stock_master)
49                 $bom = get_wo_requirements($woid);
50                 $m_cost = $l_cost = 0;
51                 while ($component = db_fetch($bom))
52                 {
53                         if (!is_service($component["mb_flag"]))
54                                 $m_cost += $component['unit_cost']*$component['units_issued'];
55                         else
56                                 $l_cost += $component['unit_cost']*$component['units_issued'];
57                 }
58
59                 // add additional material issues
60                 $issues = get_additional_issues($woid);
61                 while ($issue = db_fetch($issues))
62                 {
63                         if (!is_service($issue["mb_flag"]))
64                                 $m_cost += $issue['unit_cost']*$issue['qty_issued'];
65                         else
66                                 $l_cost += $issue['unit_cost']*$issue['qty_issued'];
67                 }
68
69                 // and additional costs
70                 $o_cost = get_gl_wo_cost($woid, WO_OVERHEAD);
71                 $l_cost += get_gl_wo_cost($woid, WO_LABOUR);
72
73                 $unit_cost = ($o_cost+$m_cost+$l_cost)/($product['units_issued']+$quantity);
74
75                 update_material_cost($product['stock_id'], $product['units_issued']+$quantity, $unit_cost, $date_);
76                 // FIXME: ?
77 //              update_labour_cost(stock_id, qty, unit_cost);
78 //              update_overheads_cost(stock_id, qty, unit_cost);
79
80                 add_stock_move(ST_WORKORDER, $product["stock_id"], $woid,
81                         $product["loc_code"], $date_, $ref, $product['units_issued']+$quantity, $unit_cost);
82         }
83
84         if ($memo_)
85                 add_comments(ST_MANURECEIVE, $id, $date_, $memo_);
86
87         $Refs->save(ST_MANURECEIVE, $id, $ref);
88         add_audit_trail(ST_MANURECEIVE, $id, $date_, _("Production."));
89
90         $args->trans_no = $id;
91         hook_db_postwrite($args, ST_MANURECEIVE);
92
93         commit_transaction();
94 }
95
96 /*
97         Process component usage: generate and post stock move, update average component cost.
98 */
99 function work_order_receive_costs($woid, $stock_id, $quantity, $date_, $rcv_no)
100 {
101         $result = get_wo_requirements($woid);
102
103         // credit all the components
104         $total_cost = 0;
105         while ($bom_item = db_fetch($result))
106         {
107
108                 $bom_cost = $bom_item["ComponentCost"] * $quantity;
109
110                 update_wo_requirement_issued($bom_item['id'], $bom_item["units_req"] * $quantity, $bom_item["ComponentCost"]);
111
112                 // insert a -ve stock move for each item
113                 add_stock_move(ST_MANURECEIVE, $bom_item["stock_id"], $rcv_no,
114                         $bom_item["loc_code"], $date_, "", -$bom_item["units_req"] * $quantity, $bom_item["ComponentCost"], 0);
115
116                 if (!is_service($bom_item["mb_flag"]))
117                         $ivaccount = $bom_item["inventory_account"];
118                 else
119                         $ivaccount = $bom_item["assembly_account"];
120
121                 $memo = $date_.": ".$bom_item["units_req"] ." * ".$bom_item["description"];
122                 $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,
123                         $memo, -$bom_cost);
124         }
125
126     add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, get_company_pref('wip_act'),
127         0, 0, $memo, -$total_cost);
128 }
129
130 //--------------------------------------------------------------------------------------------
131
132 function get_work_order_produce($id)
133 {
134         $sql = "SELECT prod.*, wo.stock_id, item.description AS StockDescription
135                         FROM ".TB_PREF."wo_manufacture prod,"
136                                 .TB_PREF."workorders wo,"
137                                 .TB_PREF."stock_master item
138                 WHERE prod.workorder_id=wo.id
139                 AND item.stock_id=wo.stock_id
140                 AND prod.id=".db_escape($id);
141     $result = db_query($sql, "The work order production could not be retrieved");
142
143     return db_fetch($result);
144 }
145
146 //--------------------------------------------------------------------------------------
147
148 function get_work_order_productions($woid)
149 {
150         $sql = "SELECT * FROM ".TB_PREF."wo_manufacture WHERE workorder_id="
151                 .db_escape($woid)." ORDER BY id";
152     return db_query($sql, "The work order issues could not be retrieved");
153 }
154
155 //--------------------------------------------------------------------------------------
156
157 function exists_work_order_produce($id)
158 {
159         $sql = "SELECT id FROM ".TB_PREF."wo_manufacture WHERE id=".db_escape($id);
160         $result = db_query($sql, "Cannot retreive a wo production");
161
162     return (db_num_rows($result) > 0);
163 }
164
165 //--------------------------------------------------------------------------------------------
166
167 function void_work_order_produce($type_no)
168 {
169         begin_transaction();
170         hook_db_prevoid(ST_MANURECEIVE, $type_no);
171
172         // Skip processing already voided entry i.e. explicitly voided
173         $void_entry = get_voided_entry(ST_MANURECEIVE, $type_no);
174         if ($void_entry)
175                 return; 
176
177         $row = get_work_order_produce($type_no);
178
179         // deduct the quantity of this production from the parent work order
180         work_order_update_finished_quantity($row["workorder_id"], -$row["quantity"]);
181
182         // void any related gl trans
183
184         $woid = $row["workorder_id"];
185         $date_ = sql2date($row["date_"]);
186                 
187         $result = get_stock_moves(ST_MANURECEIVE, $type_no);
188         while ($myrow = db_fetch($result))
189         {
190                 $issue_cost = $myrow["qty"]*$myrow["standard_cost"];
191                 $issue = get_stock_gl_code($myrow["stock_id"]);
192         $stockitem = get_item($myrow["stock_id"]);
193
194                 // Compatibility for Service Items
195                 if (!is_service($issue["mb_flag"]))
196                         $ivaccount = $issue["inventory_account"];
197                 else
198                         $ivaccount = $issue["assembly_account"];
199
200                 if ($issue_cost != 0)
201                 {
202                         add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,
203                                 $date_.": "._("Reversed the production ")." ".$stockitem["description"],
204                                 -$issue_cost);
205                 }
206         }
207         // clear the production record
208         $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".db_escape($type_no);
209         db_query($sql, "Cannot void a wo production");
210
211         // Shifted below
212         // void all related stock moves
213         void_stock_move(ST_MANURECEIVE, $type_no);
214
215         commit_transaction();
216 }
217
218