Eliminated non-static method calls and other bulk fixes to fix php5 warnings
[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_, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc)
15 {
16         global $Refs;
17
18         begin_transaction();
19
20         // if unassembling, reverse the stock movements
21         if ($type == WO_UNASSEMBLY)
22                 $units_reqd = -$units_reqd;
23
24         add_material_cost($stock_id, $units_reqd, $date_);
25
26         $date = date2sql($date_);
27         if (!isset($costs) || ($costs == ""))
28                 $costs = 0;
29         add_overhead_cost($stock_id, $units_reqd, $date_, $costs);
30         if (!isset($labour) || ($labour == ""))
31                 $labour = 0;
32         add_labour_cost($stock_id, $units_reqd, $date_, $labour);
33                 
34         $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, units_issued, stock_id,
35                 type, additional_costs, date_, released_date, required_by, released, closed)
36         VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", $units_reqd, $units_reqd, '$stock_id',
37                 $type, $costs, '$date', '$date', '$date', 1, 1)";
38         db_query($sql, "could not add work order");
39
40         $woid = db_insert_id();
41
42         //--------------------------------------------------------------------------
43
44         // create Work Order Requirements based on the bom
45         $result = get_bom($stock_id);
46
47         while ($bom_item = db_fetch($result))
48         {
49
50                 $unit_quantity = $bom_item["quantity"];
51                 $item_quantity = $bom_item["quantity"] * $units_reqd;
52
53
54                 $sql = "INSERT INTO ".TB_PREF."wo_requirements (workorder_id, stock_id, workcentre, units_req, units_issued, loc_code)
55                         VALUES ($woid, " . "'" . $bom_item["component"] . "'" . ",
56                         '". $bom_item["workcentre_added"] . "',
57                         $unit_quantity, $item_quantity, '" . $bom_item["loc_code"] . "')";
58
59         db_query($sql, "The work order requirements could not be added");
60
61                 // insert a -ve stock move for each item
62                 add_stock_move(ST_WORKORDER, $bom_item["component"], $woid,
63                         $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, 0);
64         }
65
66
67         // -------------------------------------------------------------------------
68
69         // insert a +ve stock move for the item being manufactured
70         add_stock_move(ST_WORKORDER, $stock_id, $woid,  $loc_code, $date_,
71                 $wo_ref, $units_reqd, 0);
72
73         // -------------------------------------------------------------------------
74
75         work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, false, $costs, $cr_acc, $labour, $cr_lab_acc);
76
77         // -------------------------------------------------------------------------
78
79         add_comments(ST_WORKORDER, $woid, $date_, $memo_);
80
81         $Refs->save(ST_WORKORDER, $woid, $wo_ref);
82         add_audit_trail(ST_WORKORDER, $woid, $date_,_("Quick production."));
83         commit_transaction();
84         return $woid;
85 }
86
87 //--------------------------------------------------------------------------------------
88
89 function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced=false, $costs=0, $cr_acc="", $labour=0, $cr_lab_acc="")
90 {
91         global $wo_cost_types;
92         $result = get_bom($stock_id);
93
94         // credit all the components
95         $total_cost = 0;
96         while ($bom_item = db_fetch($result))
97         {
98
99                 $bom_accounts = get_stock_gl_code($bom_item["component"]);
100
101                 $bom_cost = $bom_item["ComponentCost"] * $units_reqd;
102
103                 if ($advanced)
104                 {
105                         // insert a -ve stock move for each item
106                         add_stock_move(ST_WORKORDER, $bom_item["component"], $woid,
107                                 $bom_item["loc_code"], $date_, "", -$bom_item["quantity"] * $units_reqd, 0);
108                 }
109                 $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $bom_accounts["inventory_account"], 0, 0,
110                         null, -$bom_cost);
111
112         }
113         if ($advanced)
114         {
115                 // also take the additional issues
116                 $res = get_additional_issues($woid);
117                 $wo = get_work_order($woid);
118                 $issue_total = 0;
119                 while ($item = db_fetch($res))
120                 {
121                         $standard_cost = get_standard_cost($item['stock_id']);
122                         $issue_cost = $standard_cost * $item['qty_issued'] * $units_reqd / $wo['units_reqd'];
123                         $issue = get_stock_gl_code($item['stock_id']);
124                         $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $issue["inventory_account"], 0, 0,
125                                 null, -$issue_cost);
126                         $issue_total += $issue_cost;
127                 }
128                 if ($issue_total != 0)
129                         add_issue_cost($stock_id, $units_reqd, $date_, $issue_total);
130                 $result = get_gl_wo_cost_trans($woid, WO_LABOUR);
131                 $lcost = 0;
132                 while ($row = db_fetch($result))
133                         $lcost += -$row['amount'];
134                 add_labour_cost($stock_id, $units_reqd, $date_, $lcost * $units_reqd / $wo['units_reqd']);
135                 $result = get_gl_wo_cost_trans($woid, WO_OVERHEAD);
136                 $ocost = 0;
137                 while ($row = db_fetch($result))
138                         $ocost += -$row['amount'];
139                 add_overhead_cost($stock_id, $units_reqd, $date_, $ocost * $units_reqd / $wo['units_reqd']);
140         }
141         // credit additional costs
142         $item_accounts = get_stock_gl_code($stock_id);
143         if ($costs != 0.0)
144         {
145                 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $cr_acc,
146                         0, 0, $wo_cost_types[WO_OVERHEAD], -$costs, PT_WORKORDER, WO_OVERHEAD);
147                 $is_bank_to = is_bank_account($cr_acc);
148         if ($is_bank_to)
149         {
150                 add_bank_trans(ST_WORKORDER, $woid, $is_bank_to, "",
151                         $date_, -$costs, PT_WORKORDER, WO_OVERHEAD, get_company_currency(),
152                         "Cannot insert a destination bank transaction");
153         }
154                         
155                 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["assembly_account"],
156                         $item_accounts["dimension_id"], $item_accounts["dimension2_id"], $wo_cost_types[WO_OVERHEAD], $costs, 
157                         PT_WORKORDER, WO_OVERHEAD);
158         }
159         if ($labour != 0.0)
160         {
161                 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $cr_lab_acc,
162                         0, 0, $wo_cost_types[WO_LABOUR], -$labour, PT_WORKORDER, WO_LABOUR);
163                 $is_bank_to = is_bank_account($cr_lab_acc);
164         if ($is_bank_to)
165         {
166                 add_bank_trans(ST_WORKORDER, $woid, $is_bank_to, "",
167                         $date_, -$labour, PT_WORKORDER, WO_LABOUR, get_company_currency(),
168                         "Cannot insert a destination bank transaction");
169         }
170                         
171                 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["assembly_account"],
172                         $item_accounts["dimension_id"], $item_accounts["dimension2_id"], $wo_cost_types[WO_LABOUR], $labour, 
173                         PT_WORKORDER, WO_LABOUR);
174         }
175         // debit total components $total_cost
176         add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["inventory_account"],
177                 0, 0, null, -$total_cost);
178 }
179
180 //--------------------------------------------------------------------------------------
181
182 ?>