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