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