Merging version 2.1 RC to main trunk.
[fa-stable.git] / purchasing / inquiry / po_search.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 2;
13 $path_to_root="../..";
14 include($path_to_root . "/includes/db_pager.inc");
15 include($path_to_root . "/includes/session.inc");
16
17 include($path_to_root . "/purchasing/includes/purchasing_ui.inc");
18 include_once($path_to_root . "/reporting/includes/reporting.inc");
19
20 $js = "";
21 if ($use_popup_windows)
22         $js .= get_js_open_window(900, 500);
23 if ($use_date_picker)
24         $js .= get_js_date_picker();
25 page(_("Search Outstanding Purchase Orders"), false, false, "", $js);
26
27 if (isset($_GET['order_number']))
28 {
29         $_POST['order_number'] = $_GET['order_number'];
30 }
31 //-----------------------------------------------------------------------------------
32 // Ajax updates
33 //
34 if (get_post('SearchOrders')) 
35 {
36         $Ajax->activate('orders_tbl');
37 } elseif (get_post('_order_number_changed')) 
38 {
39         $disable = get_post('order_number') !== '';
40
41         $Ajax->addDisable(true, 'OrdersAfterDate', $disable);
42         $Ajax->addDisable(true, 'OrdersToDate', $disable);
43         $Ajax->addDisable(true, 'StockLocation', $disable);
44         $Ajax->addDisable(true, '_SelectStockFromList_edit', $disable);
45         $Ajax->addDisable(true, 'SelectStockFromList', $disable);
46
47         if ($disable) {
48                 $Ajax->addFocus(true, 'order_number');
49         } else
50                 $Ajax->addFocus(true, 'OrdersAfterDate');
51
52         $Ajax->activate('orders_tbl');
53 }
54
55
56 //---------------------------------------------------------------------------------------------
57
58 start_form(false, true);
59
60 start_table("class='tablestyle_noborder'");
61 start_row();
62 ref_cells(_("#:"), 'order_number', '',null, '', true);
63
64 date_cells(_("from:"), 'OrdersAfterDate', '', null, -30);
65 date_cells(_("to:"), 'OrdersToDate');
66
67 locations_list_cells(_("Location:"), 'StockLocation', null, true);
68
69 stock_items_list_cells(_("Item:"), 'SelectStockFromList', null, true);
70
71 submit_cells('SearchOrders', _("Search"),'',_('Select documents'), true);
72 end_row();
73 end_table();
74 end_form();
75 //---------------------------------------------------------------------------------------------
76 function trans_view($trans)
77 {
78         return get_trans_view_str(systypes::po(), $trans["order_no"]);
79 }
80
81 function edit_link($row) 
82 {
83   return pager_link( _("Edit"),
84         "/purchasing/po_entry_items.php?" . SID 
85         . "ModifyOrderNumber=" . $row["order_no"], ICON_EDIT);
86 }
87
88 function prt_link($row)
89 {
90         return print_document_link($row['order_no'], _("Print"), true, 18, ICON_PRINT);
91 }
92
93 function receive_link($row) 
94 {
95   return pager_link( _("Receive"),
96         "/purchasing/po_receive_items.php?" . SID 
97         . "PONumber=" . $row["order_no"], ICON_RECEIVE);
98 }
99
100 function check_overdue($row)
101 {
102         return $row['OverDue']==1;
103 }
104 //---------------------------------------------------------------------------------------------
105
106 if (isset($_POST['order_number']) && ($_POST['order_number'] != ""))
107 {
108         $order_number = $_POST['order_number'];
109 }
110
111 if (isset($_POST['SelectStockFromList']) && ($_POST['SelectStockFromList'] != "") &&
112         ($_POST['SelectStockFromList'] != $all_items))
113 {
114         $selected_stock_item = $_POST['SelectStockFromList'];
115 }
116 else
117 {
118         unset($selected_stock_item);
119 }
120
121 //figure out the sql required from the inputs available
122 $sql = "SELECT 
123         porder.order_no, 
124         porder.reference,
125         supplier.supp_name, 
126         location.location_name,
127         porder.requisition_no, 
128         porder.ord_date,
129         supplier.curr_code,
130         Sum(line.unit_price*line.quantity_ordered) AS OrderValue,
131         Sum(line.delivery_date < '". date2sql(Today()) ."'
132         AND (line.quantity_ordered > line.quantity_received)) As OverDue
133         FROM "
134                 .TB_PREF."purch_orders as porder, "
135                 .TB_PREF."purch_order_details as line, "
136                 .TB_PREF."suppliers as supplier, "
137                 .TB_PREF."locations as location
138         WHERE porder.order_no = line.order_no
139         AND porder.supplier_id = supplier.supplier_id
140         AND location.loc_code = porder.into_stock_location
141         AND (line.quantity_ordered > line.quantity_received) ";
142
143 if (isset($order_number) && $order_number != "")
144 {
145         $sql .= "AND porder.reference LIKE '%". $order_number . "%'";
146 }
147 else
148 {
149         $data_after = date2sql($_POST['OrdersAfterDate']);
150         $data_before = date2sql($_POST['OrdersToDate']);
151
152         $sql .= "  AND porder.ord_date >= '$data_after'";
153         $sql .= "  AND porder.ord_date <= '$data_before'";
154
155         if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != $all_items)
156         {
157                 $sql .= " AND porder.into_stock_location = '". $_POST['StockLocation'] . "' ";
158         }
159
160         if (isset($selected_stock_item))
161         {
162                 $sql .= " AND line.item_code='". $selected_stock_item ."' ";
163         }
164 } //end not order number selected
165
166 $sql .= " GROUP BY porder.order_no";
167
168 $result = db_query($sql,"No orders were returned");
169
170 /*show a table of the orders returned by the sql */
171 $cols = array(
172                 _("#") => array('fun'=>'trans_view', 'ord'=>''), 
173                 _("Reference"), 
174                 _("Supplier") => array('ord'=>''),
175                 _("Location"),
176                 _("Supplier's Reference"), 
177                 _("Order Date") => array('name'=>'ord_date', 'type'=>'date', 'ord'=>'desc'),
178                 _("Currency") => array('align'=>'center'), 
179                 _("Order Total") => 'amount',
180                 array('insert'=>true, 'fun'=>'edit_link'),
181                 array('insert'=>true, 'fun'=>'prt_link'),
182                 array('insert'=>true, 'fun'=>'receive_link')
183 );
184
185 if (get_post('StockLocation') != $all_items) {
186         $cols[_("Location")] = 'skip';
187 }
188
189 $table =& new_db_pager('orders_tbl', $sql, $cols);
190 $table->set_marker('check_overdue', _("Marked orders have overdue items."));
191
192 if (get_post('SearchOrders')) {
193         $table->set_sql($sql);
194         $table->set_columns($cols);
195 }
196 $table->width = "80%";
197 start_form();
198
199 display_db_pager($table);
200
201 end_form();
202 end_page();
203 ?>