Unable to void Work Order. Fixed by @kvvaradha.
[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                 // first update material cost
85                 update_material_cost($product['stock_id'], $product['units_issued']+$quantity, $unit_cost, $date_);
86
87                 add_stock_move(ST_WORKORDER, $product["stock_id"], $woid,
88                         $product["loc_code"], $date_, $ref, $product['units_issued']+$quantity, $unit_cost);
89         }
90
91         if ($memo)
92                 add_comments(ST_MANURECEIVE, $id, $date_, $memo);
93
94         $Refs->save(ST_MANURECEIVE, $id, $ref);
95         add_audit_trail(ST_MANURECEIVE, $id, $date_, _("Production."));
96
97         $args->trans_no = $id;
98         hook_db_postwrite($args, ST_MANURECEIVE);
99
100         commit_transaction();
101 }
102
103 //--------------------------------------------------------------------------------------------
104
105 function get_work_order_produce($id)
106 {
107         $sql = "SELECT prod.*, wo.stock_id, item.description AS StockDescription, wo.closed
108                         FROM ".TB_PREF."wo_manufacture prod,"
109                                 .TB_PREF."workorders wo,"
110                                 .TB_PREF."stock_master item
111                 WHERE prod.workorder_id=wo.id
112                 AND item.stock_id=wo.stock_id
113                 AND prod.id=".db_escape($id);
114     $result = db_query($sql, "The work order production could not be retrieved");
115
116     return db_fetch($result);
117 }
118
119 //--------------------------------------------------------------------------------------
120
121 function get_work_order_productions($woid)
122 {
123         $sql = "SELECT * FROM ".TB_PREF."wo_manufacture WHERE workorder_id="
124                 .db_escape($woid)." ORDER BY id";
125     return db_query($sql, "The work order issues could not be retrieved");
126 }
127
128 //--------------------------------------------------------------------------------------
129
130 function exists_work_order_produce($id)
131 {
132         $sql = "SELECT id FROM ".TB_PREF."wo_manufacture WHERE id=".db_escape($id);
133         $result = db_query($sql, "Cannot retreive a wo production");
134
135     return (db_num_rows($result) > 0);
136 }
137
138 //--------------------------------------------------------------------------------------
139
140 function check_void_wo_production($prod_no)
141 {
142         $prod = get_work_order_produce($prod_no);
143         return $prod['closed'] ? $prod['workorder_id'] : 0;
144 }
145
146 //--------------------------------------------------------------------------------------------
147
148 function void_work_order_produce($type_no)
149 {
150         begin_transaction();
151         hook_db_prevoid(ST_MANURECEIVE, $type_no);
152
153         // Skip processing already voided entry i.e. explicitly voided
154         $void_entry = get_voided_entry(ST_MANURECEIVE, $type_no);
155         if ($void_entry)
156                 return;
157
158         $prod = get_work_order_produce($type_no);
159
160         if (work_order_is_closed($prod['workorder_id']))
161                 return;
162
163         // deduct the quantity of this production from the parent work order
164         work_order_update_finished_quantity($prod["workorder_id"], -$prod["quantity"]);
165
166         // clear the production record
167         $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".db_escape($type_no);
168         db_query($sql, "Cannot void a wo production");
169
170         void_gl_trans(ST_MANURECEIVE, $type_no);
171
172         void_stock_move(ST_MANURECEIVE, $type_no);
173
174         commit_transaction();
175 }
176
177