References saved in refs table for all documents for easy access.
[fa-stable.git] / manufacturing / includes / db / work_orders_quick_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 //--------------------------------------------------------------------------------------
13
14 function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $additional_costs, $memo_)
15 {
16         begin_transaction();
17
18         // if unassembling, reverse the stock movements
19         if ($type == wo_types::unassemble())
20                 $units_reqd = -$units_reqd;
21
22         add_material_cost($stock_id, $units_reqd, $date_);
23
24         $date = date2sql($date_);
25         if (!isset($additional_costs) || ($additional_costs == ""))
26                 $additional_costs = 0;
27
28         $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, units_issued, stock_id,
29                 type, additional_costs, date_, released_date, required_by, released, closed)
30         VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", $units_reqd, $units_reqd, '$stock_id',
31                 $type, $additional_costs, '$date', '$date', '$date', 1, 1)";
32         db_query($sql, "could not add work order");
33
34         $woid = db_insert_id();
35
36         //--------------------------------------------------------------------------
37
38         // create Work Order Requirements based on the bom
39         $result = get_bom($stock_id);
40
41         while ($bom_item = db_fetch($result))
42         {
43
44                 $unit_quantity = $bom_item["quantity"];
45                 $item_quantity = $bom_item["quantity"] * $units_reqd;
46
47
48                 $sql = "INSERT INTO ".TB_PREF."wo_requirements (workorder_id, stock_id, workcentre, units_req, units_issued, loc_code)
49                         VALUES ($woid, " . "'" . $bom_item["component"] . "'" . ",
50                         '". $bom_item["workcentre_added"] . "',
51                         $unit_quantity, $item_quantity, '" . $bom_item["loc_code"] . "')";
52
53         db_query($sql, "The work order requirements could not be added");
54
55                 // insert a -ve stock move for each item
56                 add_stock_move(systypes::work_order(), $bom_item["component"], $woid,
57                         $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, 0);
58         }
59
60
61         // -------------------------------------------------------------------------
62
63         // insert a +ve stock move for the item being manufactured
64         add_stock_move(systypes::work_order(), $stock_id, $woid,        $loc_code, $date_,
65                 $wo_ref, $units_reqd, 0);
66
67         // -------------------------------------------------------------------------
68
69         work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $additional_costs);
70
71         // -------------------------------------------------------------------------
72
73         add_comments(systypes::work_order(), $woid, $date_, $memo_);
74
75         references::save(systypes::work_order(), $woid, $wo_ref);
76         add_audit_trail(systypes::work_order(), $woid, $date_,_("Quick production."));
77         commit_transaction();
78         return $woid;
79 }
80
81 //--------------------------------------------------------------------------------------
82
83 function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $additional_costs, $advanced=false)
84 {
85         $result = get_bom($stock_id);
86
87         // credit all the components
88         $total_cost = 0;
89         while ($bom_item = db_fetch($result))
90         {
91
92                 $bom_accounts = get_stock_gl_code($bom_item["component"]);
93
94                 $bom_cost = $bom_item["standard_cost"] * $bom_item["quantity"] * $units_reqd;
95
96                 if ($advanced)
97                 {
98                         // insert a -ve stock move for each item
99                         add_stock_move(systypes::work_order(), $bom_item["component"], $woid,
100                                 $bom_item["loc_code"], $date_, "", -$bom_item["quantity"] * $units_reqd, 0);
101                 }
102                 add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $bom_accounts["inventory_account"], 0, 0,
103                         null, -$bom_cost);
104
105                 $total_cost += $bom_cost;
106         }
107         if ($advanced)
108         {
109                 // also take the additional issues
110                 $res = get_additional_issues($woid);
111                 while ($item = db_fetch($res))
112                 {
113                         $standard_cost = get_standard_cost($item['stock_id']);
114                         $issue_cost = $standard_cost * $item['qty_issued'];
115                         $issue = get_stock_gl_code($item['stock_id']);
116                         add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $issue["inventory_account"], 0, 0,
117                                 null, -$issue_cost);
118                         $total_cost += $issue_cost;
119                 }
120         }
121         // credit additional costs
122         $item_accounts = get_stock_gl_code($stock_id);
123         add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["assembly_account"],
124                 $item_accounts["dimension_id"], $item_accounts["dimension2_id"], null, -$additional_costs);
125
126         // debit total components + additional
127         $total_cost += $additional_costs;
128         add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["inventory_account"],
129                 0, 0, null, $total_cost);
130 }
131
132 //--------------------------------------------------------------------------------------
133
134 ?>