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