Moving bugfixes from 2.3 to 2.4 and updated install languages for Denmark and Sweden.
[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         $args = func_get_args();
18         $args = (object)array_combine(array('woid', 'ref', 'quantity', 'date_', 'memo_','close_wo'),
19                 $args);
20         $args->trans_no = 0;
21         hook_db_prewrite($args, ST_MANURECEIVE);
22
23         $details = get_work_order($woid);
24
25     if (strlen($details[0]) == 0)
26     {
27         echo _("The order number sent is not valid.");
28         exit;
29     }
30
31         if (work_order_is_closed($woid))
32         {
33                 display_error("UNEXPECTED : Producing Items for a closed Work Order");
34                 cancel_transaction();
35                 exit;
36         }
37
38     $date = date2sql($date_);
39
40     $sql = "INSERT INTO ".TB_PREF."wo_manufacture (workorder_id, reference, quantity, date_)
41                 VALUES (".db_escape($woid).", ".db_escape($ref).", ".db_escape($quantity)
42                 .", '$date')";
43
44         db_query($sql,"A work order manufacture could not be added");
45
46         $id = db_insert_id();
47
48         // -------------------------------------------------------------------------
49
50         work_order_quick_costs($woid, $details["stock_id"], $quantity, $date_, $id);
51         // -------------------------------------------------------------------------
52
53         // Stamp BOM cost to finished item
54         $m_cost = 0;
55     $result = get_bom($details["stock_id"]);
56         while ($bom_item = db_fetch($result))
57         {
58                 $standard_cost = get_standard_cost($bom_item['component']);
59                 $m_cost += ($bom_item['quantity'] * $standard_cost);
60         }
61         // new 2015.10.15       
62         // additilnal costs.
63         if (work_order_has_issues($woid))
64         {
65                 $res = get_additional_issues($woid);
66                 while ($issue = db_fetch($res))
67                 {
68                         $standard_cost = get_standard_cost($issue['stock_id']);
69                         $m_cost += ($issue['qty_issued'] * $standard_cost) / $quantity;
70                 }
71         }
72         $m_cost += (get_gl_wo_cost($woid, WO_LABOUR) / $quantity);
73         $m_cost += (get_gl_wo_cost($woid, WO_OVERHEAD) / $quantity);
74
75         // insert a +ve stock move for the item being manufactured
76         // negative means "unproduce" or unassemble
77         add_stock_move(ST_MANURECEIVE, $details["stock_id"], $id,
78                 $details["loc_code"], $date_, $ref, $quantity, $m_cost);
79         // update wo quantity and close wo if requested
80         work_order_update_finished_quantity($woid, $quantity, $close_wo);
81
82         if ($memo_)
83                 add_comments(ST_MANURECEIVE, $id, $date_, $memo_);
84
85         $Refs->save(ST_MANURECEIVE, $id, $ref);
86         add_audit_trail(ST_MANURECEIVE, $id, $date_, _("Production."));
87
88         $args->trans_no = $id;
89         hook_db_postwrite($args, ST_MANURECEIVE);
90         commit_transaction();
91 }
92
93 //--------------------------------------------------------------------------------------------
94
95 function get_work_order_produce($id)
96 {
97         $sql = "SELECT prod.*, wo.stock_id, item.description AS StockDescription
98                         FROM ".TB_PREF."wo_manufacture prod,"
99                                 .TB_PREF."workorders wo,"
100                                 .TB_PREF."stock_master item
101                 WHERE prod.workorder_id=wo.id
102                 AND item.stock_id=wo.stock_id
103                 AND prod.id=".db_escape($id);
104     $result = db_query($sql, "The work order production could not be retrieved");
105
106     return db_fetch($result);
107 }
108
109 //--------------------------------------------------------------------------------------
110
111 function get_work_order_productions($woid)
112 {
113         $sql = "SELECT * FROM ".TB_PREF."wo_manufacture WHERE workorder_id="
114                 .db_escape($woid)." ORDER BY id";
115     return db_query($sql, "The work order issues could not be retrieved");
116 }
117
118 //--------------------------------------------------------------------------------------
119
120 function exists_work_order_produce($id)
121 {
122         $sql = "SELECT id FROM ".TB_PREF."wo_manufacture WHERE id=".db_escape($id);
123         $result = db_query($sql, "Cannot retreive a wo production");
124
125     return (db_num_rows($result) > 0);
126 }
127
128 //--------------------------------------------------------------------------------------------
129
130 function void_work_order_produce($type_no)
131 {
132         begin_transaction();
133         hook_db_prevoid(ST_MANURECEIVE, $type_no);
134
135         // Skip processing already voided entry i.e. explicitly voided
136         $void_entry = get_voided_entry(ST_MANURECEIVE, $type_no);
137         if ($void_entry)
138                 return; 
139
140         $row = get_work_order_produce($type_no);
141
142         // deduct the quantity of this production from the parent work order
143         work_order_update_finished_quantity($row["workorder_id"], -$row["quantity"]);
144
145         // void any related gl trans
146
147         $woid = $row["workorder_id"];
148         $date_ = sql2date($row["date_"]);
149                 
150         $result = get_stock_moves(ST_MANURECEIVE, $type_no);
151         while ($myrow = db_fetch($result))
152         {
153                 $issue_cost = $myrow["qty"]*$myrow["standard_cost"];
154                 $issue = get_stock_gl_code($myrow["stock_id"]);
155         $stockitem = get_item($myrow["stock_id"]);
156
157                 // Compatibility for Service Items
158                 if (!is_service($issue["mb_flag"]))
159                         $ivaccount = $issue["inventory_account"];
160                 else
161                         $ivaccount = $issue["assembly_account"];
162
163                 if ($issue_cost != 0)
164                 {
165                         add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,
166                                 $date_.": "._("Reversed the production ")." ".$stockitem["description"],
167                                 -$issue_cost);
168                 }
169         }
170         // clear the production record
171         $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".db_escape($type_no);
172         db_query($sql, "Cannot void a wo production");
173
174         // Shifted below
175         // void all related stock moves
176         void_stock_move(ST_MANURECEIVE, $type_no);
177
178         commit_transaction();
179 }
180
181