Code cleanup.
[fa-stable.git] / manufacturing / search_work_orders.php
1 <?php
2
3 $page_security = 2;
4 $path_to_root="..";
5 include($path_to_root . "/includes/db_pager.inc");
6 include_once($path_to_root . "/includes/session.inc");
7
8 include_once($path_to_root . "/includes/date_functions.inc");
9 include_once($path_to_root . "/manufacturing/includes/manufacturing_ui.inc");
10 $js = "";
11 if ($use_popup_windows)
12         $js .= get_js_open_window(800, 500);
13 if (isset($_GET['outstanding_only']) && ($_GET['outstanding_only'] == true))
14 {
15 // curently outstanding simply means not closed
16         $outstanding_only = 1;
17         page(_("Search Outstanding Work Orders"), false, false, "", $js);
18 }
19 else
20 {
21         $outstanding_only = 0;
22         page(_("Search Work Orders"), false, false, "", $js);
23 }
24 //-----------------------------------------------------------------------------------
25 // Ajax updates
26 //
27 if (get_post('SearchOrders')) 
28 {
29         $Ajax->activate('orders_tbl');
30 } elseif (get_post('_OrderNumber_changed')) 
31 {
32         $disable = get_post('OrderNumber') !== '';
33
34         $Ajax->addDisable(true, 'StockLocation', $disable);
35         $Ajax->addDisable(true, 'OverdueOnly', $disable);
36         $Ajax->addDisable(true, 'OpenOnly', $disable);
37         $Ajax->addDisable(true, 'SelectedStockItem', $disable);
38
39         if ($disable) {
40                 set_focus('OrderNumber');
41         } else
42                 set_focus('StockLocation');
43
44         $Ajax->activate('orders_tbl');
45 }
46
47 //--------------------------------------------------------------------------------------
48
49 if (isset($_GET["stock_id"]))
50         $_POST['SelectedStockItem'] = $_GET["stock_id"];
51
52 //--------------------------------------------------------------------------------------
53
54 start_form(false, false, $_SERVER['PHP_SELF'] ."?outstanding_only=" . $outstanding_only .SID);
55
56 start_table("class='tablestyle_noborder'");
57 start_row();
58 ref_cells(_("Reference:"), 'OrderNumber', '',null, '', true);
59
60 locations_list_cells(_("at Location:"), 'StockLocation', null, true);
61
62 check_cells( _("Only Overdue:"), 'OverdueOnly', null);
63
64 if ($outstanding_only==0)
65         check_cells( _("Only Open:"), 'OpenOnly', null);
66
67 stock_manufactured_items_list_cells(_("for item:"), 'SelectedStockItem', null, true);
68
69 submit_cells('SearchOrders', _("Search"),'',_('Select documents'), true);
70 end_row();
71 end_table();
72
73 end_form();
74
75 //-----------------------------------------------------------------------------
76 function check_overdue($row)
77 {
78         return (!$row["closed"] 
79                 && date_diff(Today(), sql2date($row["required_by"]), "d") > 0);
80 }
81
82 function view_link($dummy, $order_no)
83 {
84         return get_trans_view_str(systypes::work_order(), $order_no);
85 }
86
87 function view_stock($row)
88 {
89         return view_stock_status($row["stock_id"], $row["description"], false);
90 }
91
92 function wo_type_name($dummy, $type)
93 {
94         return wo_types::name($type);
95 }
96
97 function edit_link($row)
98 {
99         return  $row['closed'] ? '<i>'._('Closed').'</i>' :
100                 pager_link(_("Edit"),
101                         "/manufacturing/work_order_entry.php?trans_no=" . $row["id"]);
102 }
103
104 function release_link($row)
105 {
106         return $row["closed"] ? '' : 
107                 ($row["released"]==0 ?
108                 pager_link(_('Release'),
109                         "/manufacturing/work_order_release.php?trans_no=" . $row["id"])
110                 : 
111                 pager_link(_('Issue'),
112                         "/manufacturing/work_order_issue.php?trans_no=" .$row["id"]));
113 }
114
115 function produce_link($row)
116 {
117         return $row["closed"] || !$row["released"] ? '' :
118                 pager_link(_('Produce'),
119                         "/manufacturing/work_order_add_finished.php?trans_no=" .$row["id"]);
120 }
121
122 function costs_link($row)
123 {
124         return $row["closed"] || !$row["released"] ? '' :
125                 pager_link(_('Costs'),
126                         "/gl/gl_bank.php?NewPayment=1&PayType=" 
127                         .payment_person_types::WorkOrder(). "&PayPerson=" .$row["id"]);
128 }
129
130 function dec_amount($row, $amount)
131 {
132         return number_format2($amount, $row['decimals']);
133 }
134
135 $sql = "SELECT
136         workorder.id,
137         workorder.wo_ref,
138         workorder.type,
139         location.location_name,
140         item.description,
141         workorder.units_reqd,
142         workorder.units_issued,
143         workorder.date_,
144         workorder.required_by,
145         workorder.released_date,
146         workorder.closed,
147         workorder.released,
148         workorder.stock_id,
149         unit.decimals
150         FROM ".TB_PREF."workorders as workorder,"
151                 .TB_PREF."stock_master as item,"
152                 .TB_PREF."item_units as unit,"
153                 .TB_PREF."locations as location
154         WHERE workorder.stock_id=item.stock_id 
155                 AND workorder.loc_code=location.loc_code
156                 AND item.units=unit.abbr";
157
158 if (check_value('OpenOnly') || $outstanding_only != 0)
159 {
160         $sql .= " AND workorder.closed=0";
161 }
162
163 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != $all_items)
164 {
165         $sql .= " AND workorder.loc_code='" . $_POST['StockLocation'] . "' ";
166 }
167
168 if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
169 {
170         $sql .= " AND workorder.wo_ref LIKE '%". $_POST['OrderNumber'] . "%'";
171 }
172
173 if (isset($_POST['SelectedStockItem']) && $_POST['SelectedStockItem'] != $all_items)
174 {
175         $sql .= " AND workorder.stock_id='". $_POST['SelectedStockItem'] . "'";
176 }
177
178 if (check_value('OverdueOnly'))
179 {
180         $Today = date2sql(Today());
181
182         $sql .= " AND workorder.required_by < '$Today' ";
183 }
184
185 $cols = array(
186         _("#") => array('fun'=>'view_link'), 
187         _("Reference"), // viewlink 2 ?
188         _("Type") => array('fun'=>'wo_type_name'),
189         _("Location"), 
190         _("Item") => array('fun'=>'view_stock'),
191         _("Required") => array('fun'=>'dec_amount', 'align'=>'right'),
192         _("Manufactured") => array('fun'=>'dec_amount', 'align'=>'right'),
193         _("Date") => 'date', 
194         _("Required By") => array('type'=>'date', 'ord'=>''),
195         array('insert'=>true, 'fun'=> 'edit_link'),
196         array('insert'=>true, 'fun'=> 'release_link'),
197         array('insert'=>true, 'fun'=> 'produce_link'),
198         array('insert'=>true, 'fun'=> 'costs_link')
199 );
200
201 $table =& new_db_pager('orders_tbl', $sql, $cols);
202 $table->set_marker('check_overdue', _("Marked orders are overdue."));
203
204 start_form();
205
206 display_db_pager($table);
207
208 end_form();
209 end_page();
210 ?>