Moving 2.0 development version to main trunk.
[fa-stable.git] / purchasing / inquiry / po_search.php
1 <?php
2
3 $page_security = 2;
4 $path_to_root="../..";
5 include($path_to_root . "/includes/session.inc");
6
7 include($path_to_root . "/purchasing/includes/purchasing_ui.inc");
8 include_once($path_to_root . "/reporting/includes/reporting.inc");
9
10 $js = "";
11 if ($use_popup_windows)
12         $js .= get_js_open_window(900, 500);
13 if ($use_date_picker)
14         $js .= get_js_date_picker();
15 page(_("Search Outstanding Purchase Orders"), false, false, "", $js);
16
17 if (isset($_GET['order_number']))
18 {
19         $_POST['order_number'] = $_GET['order_number'];
20 }
21 //-----------------------------------------------------------------------------------
22 // Ajax updates
23 //
24 if (get_post('SearchOrders')) 
25 {
26         $Ajax->activate('orders_tbl');
27 } elseif (get_post('_order_number_changed')) 
28 {
29         $disable = get_post('order_number') !== '';
30
31         $Ajax->addDisable(true, 'OrdersAfterDate', $disable);
32         $Ajax->addDisable(true, 'OrdersToDate', $disable);
33         $Ajax->addDisable(true, 'StockLocation', $disable);
34         $Ajax->addDisable(true, '_SelectStockFromList_edit', $disable);
35         $Ajax->addDisable(true, 'SelectStockFromList', $disable);
36
37         if ($disable) {
38                 $Ajax->addFocus(true, 'order_number');
39         } else
40                 $Ajax->addFocus(true, 'OrdersAfterDate');
41
42         $Ajax->activate('orders_tbl');
43 }
44
45
46 //---------------------------------------------------------------------------------------------
47
48 start_form(false, true);
49
50 start_table("class='tablestyle_noborder'");
51 start_row();
52 ref_cells(_("#:"), 'order_number', '',null, '', true);
53
54 date_cells(_("from:"), 'OrdersAfterDate', '', null, -30);
55 date_cells(_("to:"), 'OrdersToDate');
56
57 locations_list_cells(_("Location:"), 'StockLocation', null, true);
58
59 stock_items_list_cells(_("Item:"), 'SelectStockFromList', null, true);
60
61 submit_cells('SearchOrders', _("Search"),'',_('Select documents'), true);
62 end_row();
63 end_table();
64
65 end_form();
66
67 //---------------------------------------------------------------------------------------------
68
69 if (isset($_POST['order_number']) && ($_POST['order_number'] != ""))
70 {
71         $order_number = $_POST['order_number'];
72 }
73
74 if (isset($_POST['SelectStockFromList']) && ($_POST['SelectStockFromList'] != "") &&
75         ($_POST['SelectStockFromList'] != $all_items))
76 {
77         $selected_stock_item = $_POST['SelectStockFromList'];
78 }
79 else
80 {
81         unset($selected_stock_item);
82 }
83
84 $today = date2sql(Today());
85
86 //figure out the sql required from the inputs available
87 $sql = "SELECT ".TB_PREF."purch_orders.order_no, ".TB_PREF."suppliers.supp_name, ".TB_PREF."purch_orders.into_stock_location,
88         ".TB_PREF."purch_orders.ord_date, ".TB_PREF."purch_orders.requisition_no, ".TB_PREF."purch_orders.reference,
89         ".TB_PREF."suppliers.curr_code, ".TB_PREF."locations.location_name,
90         Sum(".TB_PREF."purch_order_details.unit_price*".TB_PREF."purch_order_details.quantity_ordered) AS OrderValue,
91         Sum(".TB_PREF."purch_order_details.delivery_date < '" . $today. "'
92         AND (".TB_PREF."purch_order_details.quantity_ordered > ".TB_PREF."purch_order_details.quantity_received)) As OverDue
93         FROM ".TB_PREF."purch_orders, ".TB_PREF."purch_order_details, ".TB_PREF."suppliers, ".TB_PREF."locations
94         WHERE ".TB_PREF."purch_orders.order_no = ".TB_PREF."purch_order_details.order_no
95         AND ".TB_PREF."purch_orders.supplier_id = ".TB_PREF."suppliers.supplier_id
96         AND ".TB_PREF."locations.loc_code = ".TB_PREF."purch_orders.into_stock_location
97         AND (".TB_PREF."purch_order_details.quantity_ordered > ".TB_PREF."purch_order_details.quantity_received) ";
98
99 if (isset($order_number) && $order_number != "")
100 {
101         $sql .= "AND ".TB_PREF."purch_orders.reference LIKE '%". $order_number . "%'";
102 }
103 else
104 {
105
106         $data_after = date2sql($_POST['OrdersAfterDate']);
107         $data_before = date2sql($_POST['OrdersToDate']);
108
109         $sql .= "  AND ".TB_PREF."purch_orders.ord_date >= '$data_after'";
110         $sql .= "  AND ".TB_PREF."purch_orders.ord_date <= '$data_before'";
111
112         if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != $all_items)
113         {
114                 $sql .= " AND ".TB_PREF."purch_orders.into_stock_location = '". $_POST['StockLocation'] . "' ";
115         }
116
117         if (isset($selected_stock_item))
118         {
119                 $sql .= " AND ".TB_PREF."purch_order_details.item_code='". $selected_stock_item ."' ";
120         }
121 } //end not order number selected
122
123 $sql .= " GROUP BY ".TB_PREF."purch_orders.order_no";
124
125 $result = db_query($sql,"No orders were returned");
126
127 print_hidden_script(18);
128
129 /*show a table of the orders returned by the sql */
130
131 div_start('orders_tbl');
132 start_table("$table_style colspan=7 width=80%");
133
134 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] == $all_items)
135         $th = array(_("#"), _("Reference"), _("Supplier"), _("Location"),
136                 _("Supplier's Reference"), _("Order Date"), _("Currency"), _("Order Total"),"","","");
137 else
138         $th = array(_("#"), _("Reference"), _("Supplier"),
139                 _("Supplier's Reference"), _("Order Date"), _("Currency"), _("Order Total"),"","","");
140
141 table_header($th);
142
143 $j = 1;
144 $k = 0; //row colour counter
145 $overdue_items = false;
146 while ($myrow = db_fetch($result))
147 {
148
149         if ($myrow["OverDue"] == 1)
150         {
151                  start_row("class='overduebg'");
152                  $overdue_items = true;
153         }
154         else
155         {
156                 alt_table_row_color($k);
157         }
158
159         $modify = "$path_to_root/purchasing/po_entry_items.php?" . SID . "ModifyOrderNumber=" . $myrow["order_no"];
160         $receive = "$path_to_root/purchasing/po_receive_items.php?" . SID . "PONumber=" . $myrow["order_no"];
161
162         $date = sql2date($myrow["ord_date"]);
163
164         label_cell(get_trans_view_str(systypes::po(), $myrow["order_no"]));
165         label_cell($myrow["reference"]);
166         label_cell($myrow["supp_name"]);
167         if (isset($_POST['StockLocation']) && $_POST['StockLocation'] == $all_items)
168                 label_cell($myrow["location_name"]);
169         label_cell($myrow["requisition_no"]);
170         label_cell($date);
171         label_cell($myrow["curr_code"]);
172         amount_cell($myrow["OrderValue"]);
173         label_cell("<a href=$modify>" . _("Edit") . "</a>");
174         label_cell(print_document_link($myrow['order_no'], _("Print")));
175         label_cell("<a href=$receive>" . _("Receive") . "</a>");
176         end_row();
177
178         $j++;
179         If ($j == 12)
180         {
181                 $j = 1;
182                 table_header($th);
183         }
184 //end of page full new headings if
185 }
186 //end of while loop
187
188 end_table();
189
190 if ($overdue_items)
191         display_note(_("Marked orders have overdue items."), 0, 1, "class='overduefg'");
192 div_end();
193 end_page();
194 ?>