0003761: Reorder email is not sent for items used work order. Fixed
[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 $loc_notification, $path_to_root, $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         if ($loc_notification == 1)
52         {
53                 include_once($path_to_root . "/sales/includes/cart_class.inc");
54                 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
55                 $st_ids = array();
56                 $st_names = array();
57                 $st_num = array();
58                 $st_reorder = array();
59         }
60         // create Work Order Requirements based on the bom
61         $result = get_bom($stock_id);
62
63         while ($bom_item = db_fetch($result))
64         {
65
66                 $unit_quantity = $bom_item["quantity"];
67                 $item_quantity = $bom_item["quantity"] * $units_reqd;
68
69                 if ($loc_notification == 1 && is_inventory_item($bom_item["component"]))
70                 {
71                         
72                         $line = new line_details($bom_item['component'], $item_quantity, 0, 0, 0, 0, $bom_item['description']);
73                         $loc = calculate_reorder_level($bom_item["loc_code"], $line, $st_ids, $st_names, $st_num, $st_reorder); 
74                 }
75                 $sql = "INSERT INTO ".TB_PREF."wo_requirements (workorder_id, stock_id, workcentre, units_req, units_issued, loc_code)
76                         VALUES ($woid, " . "'" . $bom_item["component"] . "'" . ",
77                         '". $bom_item["workcentre_added"] . "',
78                         $unit_quantity, $item_quantity, '" . $bom_item["loc_code"] . "')";
79
80         db_query($sql, "The work order requirements could not be added");
81
82                 // insert a -ve stock move for each item
83                 // Chaitanya: Updated to record price and cost as std_cost for work order issued item
84                 $UnitCost = get_standard_cost($bom_item["component"]);
85                 add_stock_move(ST_WORKORDER, $bom_item["component"], $woid,
86                         $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, $UnitCost, 0, 1, $UnitCost);
87         }
88         
89         // -------------------------------------------------------------------------
90         //Negative Stock Handling
91         $qoh = get_qoh_on_date($stock_id);
92         $cost_adjust = false;
93         if ($qoh < 0)
94         {
95                 if ($qoh + $units_reqd >= 0)
96                         $cost_adjust = true;
97         }
98
99         // -------------------------------------------------------------------------
100         
101         //Negative Stock Handling First; Prior to add_stock_move
102         if ($cost_adjust)
103                 adjust_deliveries($stock_id, get_standard_cost($stock_id), $date_);
104                 
105         // -------------------------------------------------------------------------    
106
107         // insert a +ve stock move for the item being manufactured
108         add_stock_move(ST_WORKORDER, $stock_id, $woid,  $loc_code, $date_,
109                 $wo_ref, $units_reqd, 0);
110
111         // -------------------------------------------------------------------------
112
113         work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, 0, $costs, $cr_acc, $labour, $cr_lab_acc);
114                 
115         //--------------------------------------------------------------------------
116
117         add_comments(ST_WORKORDER, $woid, $date_, $memo_);
118
119         $Refs->save(ST_WORKORDER, $woid, $wo_ref);
120         add_audit_trail(ST_WORKORDER, $woid, $date_,_("Quick production."));
121
122         $args->woid = $woid;
123         hook_db_postwrite($args, ST_WORKORDER);
124         commit_transaction();
125         if ($loc_notification == 1 && count($st_ids) > 0)
126                 send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
127         
128         return $woid;
129 }
130
131 //--------------------------------------------------------------------------------------
132
133 function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced=0, $costs=0, $cr_acc="", $labour=0, $cr_lab_acc="")
134 {
135         global $loc_notification, $path_to_root, $wo_cost_types;
136         $result = get_bom($stock_id);
137
138         if ($loc_notification == 1 && $advanced)
139         {
140                 include_once($path_to_root . "/sales/includes/cart_class.inc");
141                 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
142                 $st_ids = array();
143                 $st_names = array();
144                 $st_num = array();
145                 $st_reorder = array();
146         }
147
148         // credit all the components
149         $total_cost = 0;
150         //Chaitanya : capture the exact i.e. non-rounded cost to avoid rounding error amplification
151         $total_material_cost = 0;
152         while ($bom_item = db_fetch($result))
153         {
154                 $bom_accounts = get_stock_gl_code($bom_item["component"]);
155
156                 $bom_cost = $bom_item["ComponentCost"] * $units_reqd;
157                 
158                 $memo = $bom_item["quantity"] ." * ".$bom_item["description"];
159                 if ($advanced)
160                 {
161                         if ($loc_notification == 1 && is_inventory_item($bom_item['component']))
162                         {
163                                 $line = new line_details($bom_item['component'], $bom_item["quantity"] * $units_reqd, 0, 0, 0, 0, $bom_item['description']);
164                                 $loc = calculate_reorder_level($bom_item["loc_code"], $line, $st_ids, $st_names, $st_num, $st_reorder); 
165                         }       
166                         $memo = $date_.": ".$memo; 
167                         update_wo_requirement_issued($woid, $bom_item['component'], $bom_item["quantity"] * $units_reqd);                       
168                         // insert a -ve stock move for each item
169                         // Chaitanya: Updated to record price and cost as std_cost for work order issued item
170                         $UnitCost = get_standard_cost($bom_item["component"]);                  
171                         add_stock_move(ST_MANURECEIVE, $bom_item["component"], $advanced,
172                                 $bom_item["loc_code"], $date_, "", -$bom_item["quantity"] * $units_reqd, $UnitCost, 0, 1, $UnitCost);
173                 }
174                 
175                 //Chaitanya : Compatibility for Service Items
176                 if (!is_service($bom_accounts["mb_flag"]))
177                         $ivaccount = $bom_accounts["inventory_account"];
178                 else
179                         $ivaccount = $bom_accounts["cogs_account"];
180                 $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,
181                         $memo, -$bom_cost);
182                 $total_material_cost += $bom_cost;
183         }
184         if ($advanced)
185         {
186                 //Chaitanya: Material cost added at time of production as per BOM at the time and in adjustment mode.
187                 add_material_cost($stock_id, $units_reqd, $date_, true, $woid); 
188         
189                 // $wo = get_work_order($woid);
190                 // also take the additional issues
191                 // moved to work_order_issues_db.inc
192                 /*
193                 $res = get_additional_issues($woid);
194                 $issue_total = 0;
195                 while ($item = db_fetch($res))
196                 {
197                         $standard_cost = get_standard_cost($item['stock_id']);
198                         $issue_cost = $standard_cost * $item['qty_issued'] * $units_reqd / $wo['units_reqd'];
199                         $issue = get_stock_gl_code($item['stock_id']);
200             $stockitem = get_item($item['stock_id']);
201             $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $issue["inventory_account"], 0, 0,
202                 $date_.": "._("Issue of")." ".$stockitem["description"], -$issue_cost);                 
203                         $issue_total += $issue_cost;
204                 }
205                 if ($issue_total != 0)
206                         add_issue_cost($stock_id, $units_reqd, $date_, $issue_total);
207                 */      
208                 
209                 // Logic moved to work_order_costs.php
210                 /*
211                 $lcost = get_gl_wo_cost($woid, WO_LABOUR);
212                 add_labour_cost($stock_id, $units_reqd, $date_, $lcost * $units_reqd / $wo['units_reqd']);
213                 $ocost = get_gl_wo_cost($woid, WO_OVERHEAD);
214                 add_overhead_cost($stock_id, $units_reqd, $date_, $ocost * $units_reqd / $wo['units_reqd']); */
215         }
216         // credit additional costs
217         $item_accounts = get_stock_gl_code($stock_id);
218         if ($costs != 0.0)
219         {
220                 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $cr_acc,
221                         0, 0, $wo_cost_types[WO_OVERHEAD], -$costs, PT_WORKORDER, WO_OVERHEAD);
222                 $is_bank_to = is_bank_account($cr_acc);
223         if ($is_bank_to)
224         {
225                 add_bank_trans(ST_WORKORDER, $woid, $is_bank_to, "",
226                         $date_, -$costs, PT_WORKORDER, WO_OVERHEAD, get_company_currency(),
227                         "Cannot insert a destination bank transaction");
228         }
229                         
230                 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["assembly_account"],
231                         $item_accounts["dimension_id"], $item_accounts["dimension2_id"], $wo_cost_types[WO_OVERHEAD], $costs, 
232                         PT_WORKORDER, WO_OVERHEAD);
233         }
234         if ($labour != 0.0)
235         {
236                 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $cr_lab_acc,
237                         0, 0, $wo_cost_types[WO_LABOUR], -$labour, PT_WORKORDER, WO_LABOUR);
238                 $is_bank_to = is_bank_account($cr_lab_acc);
239         if ($is_bank_to)
240         {
241                 add_bank_trans(ST_WORKORDER, $woid, $is_bank_to, "",
242                         $date_, -$labour, PT_WORKORDER, WO_LABOUR, get_company_currency(),
243                         "Cannot insert a destination bank transaction");
244         }
245                         
246                 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["assembly_account"],
247                         $item_accounts["dimension_id"], $item_accounts["dimension2_id"], $wo_cost_types[WO_LABOUR], $labour, 
248                         PT_WORKORDER, WO_LABOUR);
249         }
250         // debit total components $total_cost
251         $stockitem = get_item($stock_id);
252         $memo = _("Produced")." ".$units_reqd. " * ".$stockitem["description"];
253         if ($advanced)
254         {
255                 if ($loc_notification == 1 && count($st_ids) > 0)
256                         send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
257                 $memo = $date_.": ".$memo;
258         }       
259     add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["inventory_account"],
260         0, 0, $memo, -$total_cost);     
261                 
262         //Chaitanya : Update cost of the manufactured item in stock moves table
263         if (!$advanced)
264         {
265                 $UnitWOCost = ($total_material_cost + $costs + $labour)/$units_reqd;
266                 update_stock_move(ST_WORKORDER, $woid, $stock_id, $UnitWOCost); 
267         }
268 }
269
270 //--------------------------------------------------------------------------------------
271
272 ?>