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