090c8c548abc3a0dc0089833631ef132b2641691
[fa-stable.git] / manufacturing / includes / db / work_order_requirements_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 get_wo_requirements($woid)
13 {
14         $sql = "SELECT req.*, item.description, item.mb_flag, loc.location_name, 
15                         center.name AS WorkCentreDescription,
16                         item.material_cost as ComponentCost,
17                         item.inventory_account,
18                         item.cogs_account
19                 FROM (".TB_PREF."wo_requirements req,"
20                         .TB_PREF."locations loc,"
21                         .TB_PREF."workcentres center)
22                         INNER JOIN ".TB_PREF."stock_master item ON req.stock_id=item.stock_id 
23                 WHERE workorder_id=".db_escape($woid)."
24                 AND loc.loc_code = req.loc_code
25                 AND center.id=workcentre";
26
27         return db_query($sql, "The work order requirements could not be retrieved");    
28 }
29
30 function get_requirements_costs($woid)
31 {
32         $sql = "SELECT SUM(units_req*unit_cost) cost
33                 FROM ".TB_PREF."wo_requirements
34                 WHERE workorder_id=".db_escape($woid);
35
36         $result = db_query($sql, "The work order requirements costs could not be retrieved");
37         $costs = db_fetch($result);
38
39         return $costs['cost'] ? $costs['cost'] : 0;
40 }
41
42 //--------------------------------------------------------------------------------------
43
44 function create_wo_requirements($woid, $stock_id)
45 {
46         $sql = "INSERT INTO ".TB_PREF."wo_requirements (workorder_id, stock_id, workcentre, units_req, loc_code)
47                 SELECT ".db_escape($woid).", component, workcentre_added, quantity, loc_code
48                 FROM ".TB_PREF."bom WHERE parent=".db_escape($stock_id);
49
50     db_query($sql, "The work order requirements could not be added");
51 }
52
53 //--------------------------------------------------------------------------------------
54
55 function delete_wo_requirements($woid)
56 {
57         $sql="DELETE FROM ".TB_PREF."wo_requirements WHERE workorder_id=".db_escape($woid);
58         db_query($sql,"The work order requirements could not be deleted");      
59 }
60
61
62 //--------------------------------------------------------------------------------------
63 /*
64         Update total component units issued and average component cost
65 */
66 function update_wo_requirement_issued($id, $quantity, $cost)
67 {
68         $sql = "UPDATE ".TB_PREF."wo_requirements SET 
69                                 unit_cost = (units_issued*unit_cost+".$quantity*$cost.")/(units_issued+".$quantity."),
70                                 units_issued = units_issued + ".db_escape($quantity)."
71                         WHERE id = ".db_escape($id);
72
73         db_query($sql, "The work requirements issued quantity couldn't be updated");
74 }
75
76 //--------------------------------------------------------------------------------------
77
78 function void_wo_requirements($woid)
79 {
80         $sql = "UPDATE ".TB_PREF."wo_requirements SET units_issued = 0
81                 WHERE workorder_id = ".db_escape($woid);
82
83         db_query($sql, "The work requirements issued quantity couldn't be voided");     
84 }
85