fixed underline in db pager for sortable columns.
[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         add_material_cost($stock_id, $units_reqd, $date_);
14
15         $date = date2sql($date_);
16         if (!isset($additional_costs) || ($additional_costs == ""))
17                 $additional_costs = 0;
18
19         $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, units_issued, stock_id,
20                 type, additional_costs, date_, released_date, required_by, released, closed)
21         VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", $units_reqd, $units_reqd, '$stock_id',
22                 $type, $additional_costs, '$date', '$date', '$date', 1, 1)";
23         db_query($sql, "could not add work order");
24
25         $woid = db_insert_id();
26
27         //--------------------------------------------------------------------------
28
29         // create Work Order Requirements based on the bom
30         $result = get_bom($stock_id);
31
32         while ($bom_item = db_fetch($result))
33         {
34
35                 $unit_quantity = $bom_item["quantity"];
36                 $item_quantity = $bom_item["quantity"] * $units_reqd;
37
38
39                 $sql = "INSERT INTO ".TB_PREF."wo_requirements (workorder_id, stock_id, workcentre, units_req, units_issued, loc_code)
40                         VALUES ($woid, " . "'" . $bom_item["component"] . "'" . ",
41                         '". $bom_item["workcentre_added"] . "',
42                         $unit_quantity, $item_quantity, '" . $bom_item["loc_code"] . "')";
43
44         db_query($sql, "The work order requirements could not be added");
45
46                 // insert a -ve stock move for each item
47                 add_stock_move(systypes::work_order(), $bom_item["component"], $woid,
48                         $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, 0);
49         }
50
51
52         // -------------------------------------------------------------------------
53
54         // insert a +ve stock move for the item being manufactured
55         add_stock_move(systypes::work_order(), $stock_id, $woid,        $loc_code, $date_,
56                 $wo_ref, $units_reqd, 0);
57
58         // -------------------------------------------------------------------------
59
60         work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $additional_costs);
61
62         // -------------------------------------------------------------------------
63
64         add_comments(systypes::work_order(), $woid, $date_, $memo_);
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, $advanced=false)
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                 if ($advanced)
88                 {
89                         // insert a -ve stock move for each item
90                         add_stock_move(systypes::work_order(), $bom_item["component"], $woid,
91                                 $bom_item["loc_code"], $date_, "", -$bom_item["quantity"] * $units_reqd, 0);
92                 }
93                 add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $bom_accounts["inventory_account"], 0, 0,
94                         null, -$bom_cost);
95
96                 $total_cost += $bom_cost;
97         }
98         if ($advanced)
99         {
100                 // also take the additional issues
101                 $res = get_additional_issues($woid);
102                 while ($item = db_fetch($res))
103                 {
104                         $standard_cost = get_standard_cost($item['stock_id']);
105                         $issue_cost = $standard_cost * $item['qty_issued'];
106                         $issue = get_stock_gl_code($item['stock_id']);
107                         add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $issue["inventory_account"], 0, 0,
108                                 null, -$issue_cost);
109                         $total_cost += $issue_cost;
110                 }
111         }
112         // credit additional costs
113         $item_accounts = get_stock_gl_code($stock_id);
114         add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["assembly_account"],
115                 $item_accounts["dimension_id"], $item_accounts["dimension2_id"], null, -$additional_costs);
116
117         // debit total components + additional
118         $total_cost += $additional_costs;
119         add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["inventory_account"],
120                 0, 0, null, $total_cost);
121 }
122
123 //--------------------------------------------------------------------------------------
124
125 ?>