When retrieving work centres id's there was a database error. Fixed by casting string...
[fa-stable.git] / manufacturing / includes / db / work_orders_quick_db.inc
1 <?php
2
3 //--------------------------------------------------------------------------------------
4
5 function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $additional_costs, $memo_) 
6 {
7         begin_transaction();
8         
9         // if unassembling, reverse the stock movements
10         if ($type == wo_types::unassemble())
11                 $units_reqd = -$units_reqd;
12         
13         $date = date2sql($date_);
14         if (!isset($additional_costs) || ($additional_costs == ""))
15                 $additional_costs = 0;
16         
17         $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, units_issued, stock_id, 
18                 type, additional_costs, date_, released_date, required_by, released, closed) 
19         VALUES ('$wo_ref', '$loc_code', $units_reqd, $units_reqd, '$stock_id', 
20                 $type, $additional_costs, '$date', '$date', '$date', 1, 1)";
21         db_query($sql, "could not add work order");
22         
23         $woid = db_insert_id();
24         
25         //--------------------------------------------------------------------------
26
27         // create Work Order Requirements based on the bom
28         $result = get_bom($stock_id);   
29         
30         while ($bom_item = db_fetch($result)) 
31         {
32                 
33                 $unit_quantity = $bom_item["quantity"];
34                 $item_quantity = $bom_item["quantity"] * $units_reqd;
35                 
36                 
37                 $sql = "INSERT INTO ".TB_PREF."wo_requirements (workorder_id, stock_id, workcentre, units_req, units_issued, loc_code)
38                         VALUES ($woid, " . "'" . $bom_item["component"] . "'" . ", 
39                         '". $bom_item["workcentre_added"] . "', 
40                         $unit_quantity, $item_quantity, '" . $bom_item["loc_code"] . "')";
41                         
42         db_query($sql, "The work order requirements could not be added");
43         
44                 // insert a -ve stock move for each item
45                 add_stock_move(systypes::work_order(), $bom_item["component"], $woid, 
46                         $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, 0);        
47         }
48
49
50         // -------------------------------------------------------------------------
51
52         // insert a +ve stock move for the item being manufactured
53         add_stock_move(systypes::work_order(), $stock_id, $woid,        $loc_code, $date_, 
54                 $wo_ref, $units_reqd, 0);               
55
56         // -------------------------------------------------------------------------
57
58         work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $additional_costs);
59         
60         // -------------------------------------------------------------------------
61
62         add_comments(systypes::work_order(), $woid, $date_, $memo_);    
63         
64         add_forms_for_sys_type(systypes::work_order(), $woid, $type);   
65         
66         references::save_last($wo_ref, systypes::work_order());
67         
68         commit_transaction();   
69         return $woid;
70 }
71
72 //--------------------------------------------------------------------------------------
73
74 function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $additional_costs)
75 {
76         $result = get_bom($stock_id);
77         
78         // credit all the components
79         $total_cost = 0;
80         while ($bom_item = db_fetch($result)) 
81         {
82                 
83                 $bom_accounts = get_stock_gl_code($bom_item["component"]);
84                 
85                 $bom_cost = $bom_item["standard_cost"] * $bom_item["quantity"] * $units_reqd;           
86                 
87                 add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $bom_accounts["inventory_account"], 0, 0,
88                         null, -$bom_cost);
89                 
90                 $total_cost += $bom_cost;
91         }       
92         // credit additional costs
93         $item_accounts = get_stock_gl_code($stock_id);
94         add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["assembly_account"], 
95                 $item_accounts["dimension_id"], $item_accounts["dimension2_id"], null, -$additional_costs);
96         
97         // debit total components + additional
98         $total_cost += $additional_costs;       
99         add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["inventory_account"], 
100                 0, 0, null, $total_cost);
101
102
103 //--------------------------------------------------------------------------------------
104
105 ?>