44c93b864d0392bf07dc5bca3562e4e70e4caf7b
[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, $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['wip_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, wo.closed
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 check_void_wo_production($prod_no)
140 {
141         $prod = get_work_order_produce($prod_no);
142         return $prod['closed'] ? $prod['workorder_id'] : 0;
143 }
144
145 //--------------------------------------------------------------------------------------------
146
147 function void_work_order_produce($type_no)
148 {
149         begin_transaction();
150         hook_db_prevoid(ST_MANURECEIVE, $type_no);
151
152         // Skip processing already voided entry i.e. explicitly voided
153         $void_entry = get_voided_entry(ST_MANURECEIVE, $type_no);
154         if ($void_entry)
155                 return;
156
157         $prod = get_work_order_produce($type_no);
158
159         if (work_order_is_closed($prod['workorder_id']))
160                 return;
161
162         // deduct the quantity of this production from the parent work order
163         work_order_update_finished_quantity($prod["workorder_id"], -$prod["quantity"], false);
164
165         // clear the production record
166         $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".db_escape($type_no);
167         db_query($sql, "Cannot void a wo production");
168
169         void_gl_trans(ST_MANURECEIVE, $type_no);
170
171         void_stock_move(ST_MANURECEIVE, $type_no);
172
173         commit_transaction();
174 }
175
176