Cleanup comments. Small bugfixes. Third run
[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, 
15         $date_, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc)
16 {
17         global $Refs;
18
19         begin_transaction();
20         $args = func_get_args();
21         $args = (object)array_combine(array('wo_ref', 'loc_code', 'units_reqd', 'stock_id',
22                 'type', 'date_', 'memo_', 'costs', 'cr_acc', 'labour', 'cr_lab_acc'), $args);
23         $args->woid = 0;
24         hook_db_prewrite($args, ST_WORKORDER);
25
26         // if unassembling, reverse the stock movements
27         if ($type == WO_UNASSEMBLY)
28                 $units_reqd = -$units_reqd;
29
30         add_material_cost($stock_id, $units_reqd, $date_);
31
32         $date = date2sql($date_);
33         if (!isset($costs) || ($costs == ""))
34                 $costs = 0;
35         add_overhead_cost($stock_id, $units_reqd, $date_, $costs);
36         if (!isset($labour) || ($labour == ""))
37                 $labour = 0;
38         add_labour_cost($stock_id, $units_reqd, $date_, $labour);
39
40         $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, units_issued, stock_id,
41                 type, additional_costs, date_, released_date, required_by, released, closed)
42         VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", ".db_escape($units_reqd)
43         .", ".db_escape($units_reqd).", ".db_escape($stock_id).",
44                 ".db_escape($type).", ".db_escape($costs).", '$date', '$date', '$date', 1, 1)";
45         db_query($sql, "could not add work order");
46
47         $woid = db_insert_id();
48
49         //--------------------------------------------------------------------------
50
51         // create Work Order Requirements based on the bom
52         $result = get_bom($stock_id);
53
54         while ($bom_item = db_fetch($result))
55         {
56
57                 $unit_quantity = $bom_item["quantity"];
58                 $item_quantity = $bom_item["quantity"] * $units_reqd;
59
60
61                 $sql = "INSERT INTO ".TB_PREF."wo_requirements (workorder_id, stock_id, workcentre, units_req, units_issued, loc_code)
62                         VALUES ($woid, " . "'" . $bom_item["component"] . "'" . ",
63                         '". $bom_item["workcentre_added"] . "',
64                         $unit_quantity, $item_quantity, '" . $bom_item["loc_code"] . "')";
65
66         db_query($sql, "The work order requirements could not be added");
67
68                 // insert a -ve stock move for each item
69                 $UnitCost = get_standard_cost($bom_item["component"]);
70                 add_stock_move(ST_WORKORDER, $bom_item["component"], $woid,
71                         $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, $UnitCost, $UnitCost);
72         }
73
74         // -------------------------------------------------------------------------
75         //Negative Stock Handling
76         $qoh = get_qoh_on_date($stock_id);
77         $cost_adjust = false;
78         if ($qoh < 0)
79         {
80                 if ($qoh + $units_reqd >= 0)
81                         $cost_adjust = true;
82         }
83
84
85         //Negative Stock Handling First; Prior to add_stock_move
86         if ($cost_adjust)
87                 adjust_deliveries($stock_id, get_standard_cost($stock_id), $date_);
88
89         // -------------------------------------------------------------------------
90
91         // insert a +ve stock move for the item being manufactured
92         add_stock_move(ST_WORKORDER, $stock_id, $woid,  $loc_code, $date_,
93                 $wo_ref, $units_reqd, 0);
94
95         // -------------------------------------------------------------------------
96
97         work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, 0, $costs, $cr_acc, $labour, $cr_lab_acc);
98
99         // -------------------------------------------------------------------------
100
101         add_comments(ST_WORKORDER, $woid, $date_, $memo_);
102
103         $Refs->save(ST_WORKORDER, $woid, $wo_ref);
104         add_audit_trail(ST_WORKORDER, $woid, $date_,_("Quick production."));
105
106         $args->woid = $woid;
107         hook_db_postwrite($args, ST_WORKORDER);
108         commit_transaction();
109         return $woid;
110 }
111
112 //--------------------------------------------------------------------------------------
113
114 function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced=0, $costs=0, $cr_acc="", $labour=0, $cr_lab_acc="")
115 {
116         $result = get_bom($stock_id);
117
118         // credit all the components
119         $total_cost = 0;
120         while ($bom_item = db_fetch($result))
121         {
122
123                 $bom_accounts = get_stock_gl_code($bom_item["component"]);
124
125                 $bom_cost = $bom_item["ComponentCost"] * $units_reqd;
126
127                 $memo = $bom_item["quantity"] ." * ".$bom_item["description"];
128                 if ($advanced)
129                 {
130                         $memo = $date_.": ".$memo; 
131                         update_wo_requirement_issued($woid, $bom_item['component'], $bom_item["quantity"] * $units_reqd);                       
132                         // insert a -ve stock move for each item
133                         add_stock_move(ST_MANURECEIVE, $bom_item["component"], $advanced,
134                                 $bom_item["loc_code"], $date_, "", -$bom_item["quantity"] * $units_reqd, 0);
135                 }
136                 $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $bom_accounts["inventory_account"], 0, 0,
137                         $memo, -$bom_cost);
138
139         }
140         $item_accounts = get_stock_gl_code($stock_id);
141         if ($advanced)
142         {
143                 $wo = get_work_order($woid);
144                 $lcost = get_gl_wo_cost($woid, WO_LABOUR);
145                 add_labour_cost($stock_id, $units_reqd, $date_, $lcost * $units_reqd / $wo['units_reqd']);
146                 $ocost = get_gl_wo_cost($woid, WO_OVERHEAD);
147                 add_overhead_cost($stock_id, $units_reqd, $date_, $ocost * $units_reqd / $wo['units_reqd']);
148
149         } else { // only for quick
150                 // credit additional costs
151
152                 if ($costs != 0.0)
153                 {
154                         add_wo_costs_journal($woid, $costs, WO_OVERHEAD, $cr_acc, $item_accounts["assembly_account"],
155                                 $date_, $item_accounts["dimension_id"], $item_accounts["dimension2_id"]);
156                 }
157                 if ($labour != 0.0) // only for quick
158                 {
159                         add_wo_costs_journal($woid, $labour, WO_LABOUR, $cr_lab_acc, $item_accounts["assembly_account"],
160                                 $date_, $item_accounts["dimension_id"], $item_accounts["dimension2_id"]);
161                 }
162         }
163         // debit total components $total_cost
164         $stockitem = get_item($stock_id);
165         $memo = _("Produced")." ".$units_reqd. " * ".$stockitem["description"];
166         if ($advanced)
167                 $memo = $date_.": ".$memo;
168     add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["inventory_account"],
169         0, 0, $memo, -$total_cost);
170 }
171