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