Cleanup in db_pager get_sql and testing it.
[fa-stable.git] / sales / inquiry / sales_orders_view.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 $path_to_root = "../..";
13
14 include_once($path_to_root . "/includes/db_pager.inc");
15 include_once($path_to_root . "/includes/session.inc");
16 include_once($path_to_root . "/sales/includes/sales_ui.inc");
17 include_once($path_to_root . "/reporting/includes/reporting.inc");
18
19 $page_security = 'SA_SALESTRANSVIEW';
20
21 set_page_security( @$_POST['order_view_mode'],
22         array(  'OutstandingOnly' => 'SA_SALESDELIVERY',
23                         'InvoiceTemplates' => 'SA_SALESINVOICE',
24                         'DeliveryTemplates' => 'SA_SALESDELIVERY',
25                         'PrepaidOrders' => 'SA_SALESINVOICE'),
26         array(  'OutstandingOnly' => 'SA_SALESDELIVERY',
27                         'InvoiceTemplates' => 'SA_SALESINVOICE',
28                         'DeliveryTemplates' => 'SA_SALESDELIVERY',
29                         'PrepaidOrders' => 'SA_SALESINVOICE')
30 );
31
32 if (get_post('type'))
33         $trans_type = $_POST['type'];
34 elseif (isset($_GET['type']) && $_GET['type'] == ST_SALESQUOTE)
35         $trans_type = ST_SALESQUOTE;
36 else
37         $trans_type = ST_SALESORDER;
38
39 if ($trans_type == ST_SALESORDER)
40 {
41         if (isset($_GET['OutstandingOnly']) && ($_GET['OutstandingOnly'] == true))
42         {
43                 $_POST['order_view_mode'] = 'OutstandingOnly';
44                 $_SESSION['page_title'] = _($help_context = "Search Outstanding Sales Orders");
45         }
46         elseif (isset($_GET['InvoiceTemplates']) && ($_GET['InvoiceTemplates'] == true))
47         {
48                 $_POST['order_view_mode'] = 'InvoiceTemplates';
49                 $_SESSION['page_title'] = _($help_context = "Search Template for Invoicing");
50         }
51         elseif (isset($_GET['DeliveryTemplates']) && ($_GET['DeliveryTemplates'] == true))
52         {
53                 $_POST['order_view_mode'] = 'DeliveryTemplates';
54                 $_SESSION['page_title'] = _($help_context = "Select Template for Delivery");
55         }
56         elseif (isset($_GET['PrepaidOrders']) && ($_GET['PrepaidOrders'] == true))
57         {
58                 $_POST['order_view_mode'] = 'PrepaidOrders';
59                 $_SESSION['page_title'] = _($help_context = "Invoicing Prepayment Orders");
60         }
61         elseif (!isset($_POST['order_view_mode']))
62         {
63                 $_POST['order_view_mode'] = false;
64                 $_SESSION['page_title'] = _($help_context = "Search All Sales Orders");
65         }
66 }
67 else
68 {
69         $_POST['order_view_mode'] = "Quotations";
70         $_SESSION['page_title'] = _($help_context = "Search All Sales Quotations");
71 }
72
73 $js = "";
74 if ($use_popup_windows)
75         $js .= get_js_open_window(900, 600);
76 if (user_use_date_picker())
77         $js .= get_js_date_picker();
78 page($_SESSION['page_title'], false, false, "", $js);
79 //---------------------------------------------------------------------------------------------
80 //      Query format functions
81 //
82 function check_overdue($row)
83 {
84         global $trans_type;
85         if ($trans_type == ST_SALESQUOTE)
86                 return (date1_greater_date2(Today(), sql2date($row['delivery_date'])));
87         else
88                 return ($row['type'] == 0
89                         && date1_greater_date2(Today(), sql2date($row['delivery_date']))
90                         && ($row['TotDelivered'] < $row['TotQuantity']));
91 }
92
93 function view_link($dummy, $order_no)
94 {
95         global $trans_type;
96         return  get_customer_trans_view_str($trans_type, $order_no);
97 }
98
99 function prt_link($row)
100 {
101         global $trans_type;
102         return print_document_link($row['order_no'], _("Print"), true, $trans_type, ICON_PRINT);
103 }
104
105 function edit_link($row) 
106 {
107         global $page_nested;
108
109         if ($page_nested)
110                 return '';
111         global $trans_type;
112         $modify = ($trans_type == ST_SALESORDER ? "ModifyOrderNumber" : "ModifyQuotationNumber");
113   return pager_link( _("Edit"),
114     "/sales/sales_order_entry.php?$modify=" . $row['order_no'], ICON_EDIT);
115 }
116
117 function dispatch_link($row)
118 {
119         global $trans_type;
120
121         if ($row['ord_payments'] + $row['inv_payments'] < $row['prep_amount'])
122                 return '';
123
124         if ($trans_type == ST_SALESORDER)
125                 return pager_link( _("Dispatch"),
126                         "/sales/customer_delivery.php?OrderNumber=" .$row['order_no'], ICON_DOC);
127         else
128                 return pager_link( _("Sales Order"),
129                         "/sales/sales_order_entry.php?OrderNumber=" .$row['order_no'], ICON_DOC);
130 }
131
132 function invoice_link($row)
133 {
134         global $trans_type;
135         if ($trans_type == ST_SALESORDER)
136                 return pager_link( _("Invoice"),
137                         "/sales/sales_order_entry.php?NewInvoice=" .$row["order_no"], ICON_DOC);
138         else
139                 return '';
140 }
141
142 function delivery_link($row)
143 {
144   return pager_link( _("Delivery"),
145         "/sales/sales_order_entry.php?NewDelivery=" .$row['order_no'], ICON_DOC);
146 }
147
148 function order_link($row)
149 {
150   return pager_link( _("Sales Order"),
151         "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
152 }
153
154 function tmpl_checkbox($row)
155 {
156         global $trans_type, $page_nested;
157         if ($trans_type == ST_SALESQUOTE)
158                 return '';
159
160         if ($page_nested)
161                 return '';
162         $name = "chgtpl" .$row['order_no'];
163         $value = $row['type'] ? 1:0;
164
165 // save also in hidden field for testing during 'Update'
166
167  return checkbox(null, $name, $value, true,
168         _('Set this order as a template for direct deliveries/invoices'))
169         . hidden('last['.$row['order_no'].']', $value, false);
170 }
171
172 function invoice_prep_link($row)
173 {
174         // invoicing should be available only for partially allocated orders
175         return 
176                 $row['inv_payments'] < $row['total'] ?
177                 pager_link($row['ord_payments']  ? _("Prepayment Invoice") : _("Final Invoice"),
178                 "/sales/customer_invoice.php?InvoicePrepayments=" .$row['order_no'], ICON_DOC) : '';
179 }
180
181 $id = find_submit('_chgtpl');
182 if ($id != -1)
183 {
184         sales_order_set_template($id, check_value('chgtpl'.$id));
185         $Ajax->activate('orders_tbl');
186 }
187
188 if (isset($_POST['Update']) && isset($_POST['last'])) {
189         foreach($_POST['last'] as $id => $value)
190                 if ($value != check_value('chgtpl'.$id))
191                         sales_order_set_template($id, !check_value('chgtpl'.$id));
192 }
193
194 $show_dates = !in_array($_POST['order_view_mode'], array('OutstandingOnly', 'InvoiceTemplates', 'DeliveryTemplates'));
195 //---------------------------------------------------------------------------------------------
196 //      Order range form
197 //
198 if (get_post('_OrderNumber_changed') || get_post('_OrderReference_changed')) // enable/disable selection controls
199 {
200         $disable = get_post('OrderNumber') !== '' || get_post('OrderReference') !== '';
201
202         if ($show_dates) {
203                         $Ajax->addDisable(true, 'OrdersAfterDate', $disable);
204                         $Ajax->addDisable(true, 'OrdersToDate', $disable);
205         }
206
207         $Ajax->activate('orders_tbl');
208 }
209
210 start_form();
211
212 start_table(TABLESTYLE_NOBORDER);
213 start_row();
214 ref_cells(_("#:"), 'OrderNumber', '',null, '', true);
215 ref_cells(_("Ref"), 'OrderReference', '',null, '', true);
216 if ($show_dates)
217 {
218         date_cells(_("from:"), 'OrdersAfterDate', '', null, -$_SESSION["wa_current_user"]->prefs->transaction_days());
219         date_cells(_("to:"), 'OrdersToDate', '', null, 1);
220 }
221 locations_list_cells(_("Location:"), 'StockLocation', null, true, true);
222
223 if($show_dates) {
224         end_row();
225         end_table();
226
227         start_table(TABLESTYLE_NOBORDER);
228         start_row();
229 }
230 stock_items_list_cells(_("Item:"), 'SelectStockFromList', null, true, true);
231
232 if (!$page_nested)
233         customer_list_cells(_("Select a customer: "), 'customer_id', null, true, true);
234 if ($trans_type == ST_SALESQUOTE)
235         check_cells(_("Show All:"), 'show_all');
236
237 submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
238 hidden('order_view_mode', $_POST['order_view_mode']);
239 hidden('type', $trans_type);
240
241 end_row();
242
243 end_table(1);
244 //---------------------------------------------------------------------------------------------
245 //      Orders inquiry table
246 //
247 $sql = get_sql_for_sales_orders_view($trans_type, get_post('OrderNumber'), get_post('order_view_mode'),
248         get_post('SelectStockFromList'), get_post('OrdersAfterDate'), get_post('OrdersToDate'), get_post('OrderReference'), get_post('StockLocation'),
249         get_post('customer_id'));
250
251 if ($trans_type == ST_SALESORDER)
252         $cols = array(
253                 _("Order #") => array('fun'=>'view_link'),
254                 _("Ref") => array('type' => 'sorder.reference', 'ord' => '') ,
255                 _("Customer") => array('type' => 'debtor.name' , 'ord' => '') ,
256                 _("Branch"), 
257                 _("Cust Order Ref"),
258                 _("Order Date") => array('type' =>  'date', 'ord' => ''),
259                 _("Required By") =>array('type'=>'date', 'ord'=>''),
260                 _("Delivery To"), 
261                 _("Order Total") => array('type'=>'amount', 'ord'=>''),
262                 'Type' => 'skip',
263                 _("Currency") => array('align'=>'center')
264         );
265 else
266         $cols = array(
267                 _("Quote #") => array('fun'=>'view_link'),
268                 _("Ref"),
269                 _("Customer"),
270                 _("Branch"), 
271                 _("Cust Order Ref"),
272                 _("Quote Date") => 'date',
273                 _("Valid until") =>array('type'=>'date', 'ord'=>''),
274                 _("Delivery To"), 
275                 _("Quote Total") => array('type'=>'amount', 'ord'=>''),
276                 'Type' => 'skip',
277                 _("Currency") => array('align'=>'center')
278         );
279 if ($_POST['order_view_mode'] == 'OutstandingOnly') {
280         //array_substitute($cols, 3, 1, _("Cust Order Ref"));
281         array_append($cols, array(
282                 array('insert'=>true, 'fun'=>'dispatch_link'),
283                 array('insert'=>true, 'fun'=>'edit_link')));
284
285 } elseif ($_POST['order_view_mode'] == 'InvoiceTemplates') {
286         array_substitute($cols, 4, 1, _("Description"));
287         array_append($cols, array( array('insert'=>true, 'fun'=>'invoice_link')));
288
289 } else if ($_POST['order_view_mode'] == 'DeliveryTemplates') {
290         array_substitute($cols, 4, 1, _("Description"));
291         array_append($cols, array(
292                         array('insert'=>true, 'fun'=>'delivery_link'))
293         );
294 } else if ($_POST['order_view_mode'] == 'PrepaidOrders') {
295         array_append($cols, array(
296                         array('insert'=>true, 'fun'=>'invoice_prep_link'))
297         );
298
299 } elseif ($trans_type == ST_SALESQUOTE) {
300          array_append($cols,array(
301                                         array('insert'=>true, 'fun'=>'edit_link'),
302                                         array('insert'=>true, 'fun'=>'order_link'),
303                                         array('insert'=>true, 'fun'=>'prt_link')));
304 } elseif ($trans_type == ST_SALESORDER) {
305          array_append($cols,array(
306                         _("Tmpl") => array('insert'=>true, 'fun'=>'tmpl_checkbox'),
307                                         array('insert'=>true, 'fun'=>'edit_link'),
308                                         array('insert'=>true, 'fun'=>'prt_link')));
309 };
310
311
312 $table =& new_db_pager('orders_tbl', $sql, $cols);
313 $table->set_marker('check_overdue', _("Marked items are overdue."));
314
315 $table->width = "80%";
316
317 display_db_pager($table);
318 submit_center('Update', _("Update"), true, '', null);
319
320 end_form();
321 end_page();