Fixed page_security for various page usages.
[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($path_to_root . "/includes/db_pager.inc");
15 include($path_to_root . "/includes/session.inc");
16 include($path_to_root . "/sales/includes/sales_ui.inc");
17 include_once($path_to_root . "/reporting/includes/reporting.inc");
18
19 $page_security = 'SA_TRANSVIEW';
20
21 set_page_security( @$_POST['order_view_mode'],
22         array(  'OutstandingOnly' => 'SA_SALESDELIVERY',
23                         'InvoiceTemplates' => 'SA_SALESINVOICE'),
24         array(  'OutstandingOnly' => 'SA_SALESDELIVERY',
25                         'InvoiceTemplates' => 'SA_SALESINVOICE')
26 );
27
28 $js = "";
29 if ($use_popup_windows)
30         $js .= get_js_open_window(900, 600);
31 if ($use_date_picker)
32         $js .= get_js_date_picker();
33
34 if (get_post('type'))
35         $trans_type = $_POST['type'];
36 elseif (isset($_GET['type']) && $_GET['type'] == ST_SALESQUOTE)
37         $trans_type = ST_SALESQUOTE;
38 elseif (isset($_GET['type']) && $_GET['type'] == ST_SALESORDER)
39         $trans_type = ST_SALESORDER;
40 else
41         $page_security = 'SA_DENIED';
42
43 if ($trans_type == ST_SALESORDER)
44 {
45         if (isset($_GET['OutstandingOnly']) && ($_GET['OutstandingOnly'] == true))
46         {
47                 $_POST['order_view_mode'] = 'OutstandingOnly';
48                 $_SESSION['page_title'] = _("Search Outstanding Sales Orders");
49         }
50         elseif (isset($_GET['InvoiceTemplates']) && ($_GET['InvoiceTemplates'] == true))
51         {
52                 $_POST['order_view_mode'] = 'InvoiceTemplates';
53                 $_SESSION['page_title'] = _("Search Template for Invoicing");
54         }
55         elseif (isset($_GET['DeliveryTemplates']) && ($_GET['DeliveryTemplates'] == true))
56         {
57                 $_POST['order_view_mode'] = 'DeliveryTemplates';
58                 $_SESSION['page_title'] = _("Select Template for Delivery");
59         }
60         elseif (!isset($_POST['order_view_mode']))
61         {
62                 $_POST['order_view_mode'] = false;
63                 $_SESSION['page_title'] = _("Search All Sales Orders");
64         }
65 }
66 else
67 {
68         $_POST['order_view_mode'] = "Quotations";
69         $_SESSION['page_title'] = _("Search All Sales Quotations");
70 }
71 page($_SESSION['page_title'], false, false, "", $js);
72
73 if (isset($_GET['selected_customer']))
74 {
75         $selected_customer = $_GET['selected_customer'];
76 }
77 elseif (isset($_POST['selected_customer']))
78 {
79         $selected_customer = $_POST['selected_customer'];
80 }
81 else
82         $selected_customer = -1;
83
84 //---------------------------------------------------------------------------------------------
85
86 if (isset($_POST['SelectStockFromList']) && ($_POST['SelectStockFromList'] != "") &&
87         ($_POST['SelectStockFromList'] != ALL_TEXT))
88 {
89         $selected_stock_item = $_POST['SelectStockFromList'];
90 }
91 else
92 {
93         unset($selected_stock_item);
94 }
95 //---------------------------------------------------------------------------------------------
96 //      Query format functions
97 //
98 function check_overdue($row)
99 {
100         global $trans_type;
101         if ($trans_type == ST_SALESQUOTE)
102                 return (date1_greater_date2(Today(), sql2date($row['delivery_date'])));
103         else
104                 return ($row['type'] == 0
105                         && date1_greater_date2(Today(), sql2date($row['ord_date']))
106                         && ($row['TotDelivered'] < $row['TotQuantity']));
107 }
108
109 function view_link($dummy, $order_no)
110 {
111         global $trans_type;
112         return  get_customer_trans_view_str($trans_type, $order_no);
113 }
114
115 function prt_link($row)
116 {
117         global $trans_type;
118         return print_document_link($row['order_no'], _("Print"), true, $trans_type, ICON_PRINT);
119 }
120
121 function edit_link($row) 
122 {
123         global $trans_type;
124         $modify = ($trans_type == ST_SALESORDER ? "ModifyOrderNumber" : "ModifyQuotationNumber");
125   return pager_link( _("Edit"),
126     "/sales/sales_order_entry.php?$modify=" . $row['order_no'], ICON_EDIT);
127 }
128
129 function dispatch_link($row)
130 {
131         global $trans_type;
132         if ($trans_type == ST_SALESORDER)
133                 return pager_link( _("Dispatch"),
134                         "/sales/customer_delivery.php?OrderNumber=" .$row['order_no'], ICON_DOC);
135         else            
136                 return pager_link( _("Sales Order"),
137                         "/sales/sales_order_entry.php?OrderNumber=" .$row['order_no'], ICON_DOC);
138 }
139
140 function invoice_link($row)
141 {
142         global $trans_type;
143         if ($trans_type == ST_SALESORDER)
144                 return pager_link( _("Invoice"),
145                         "/sales/sales_order_entry.php?NewInvoice=" .$row["order_no"], ICON_DOC);
146         else
147                 return '';
148 }
149
150 function delivery_link($row)
151 {
152   return pager_link( _("Delivery"),
153         "/sales/sales_order_entry.php?NewDelivery=" .$row['order_no'], ICON_DOC);
154 }
155
156 function order_link($row)
157 {
158   return pager_link( _("Sales Order"),
159         "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
160 }
161
162 function tmpl_checkbox($row)
163 {
164         global $trans_type;
165         if ($trans_type == ST_SALESQUOTE)
166                 return '';
167         $name = "chgtpl" .$row['order_no'];
168         $value = $row['type'] ? 1:0;
169
170 // save also in hidden field for testing during 'Update'
171
172  return checkbox(null, $name, $value, true,
173         _('Set this order as a template for direct deliveries/invoices'))
174         . hidden('last['.$row['order_no'].']', $value, false);
175 }
176 //---------------------------------------------------------------------------------------------
177 // Update db record if respective checkbox value has changed.
178 //
179 function change_tpl_flag($id)
180 {
181         global  $Ajax;
182         
183         $sql = "UPDATE ".TB_PREF."sales_orders SET type = !type WHERE order_no=$id";
184
185         db_query($sql, "Can't change sales order type");
186         $Ajax->activate('orders_tbl');
187 }
188
189 $id = find_submit('_chgtpl');
190 if ($id != -1)
191         change_tpl_flag($id);
192
193 if (isset($_POST['Update']) && isset($_POST['last'])) {
194         foreach($_POST['last'] as $id => $value)
195                 if ($value != check_value('chgtpl'.$id))
196                         change_tpl_flag($id);
197 }
198
199 //---------------------------------------------------------------------------------------------
200 //      Order range form
201 //
202 if (get_post('_OrderNumber_changed')) // enable/disable selection controls
203 {
204         $disable = get_post('OrderNumber') !== '';
205
206         if ($_POST['order_view_mode']!='DeliveryTemplates' 
207                 && $_POST['order_view_mode']!='InvoiceTemplates') {
208                         $Ajax->addDisable(true, 'OrdersAfterDate', $disable);
209                         $Ajax->addDisable(true, 'OrdersToDate', $disable);
210         }
211         $Ajax->addDisable(true, 'StockLocation', $disable);
212         $Ajax->addDisable(true, '_SelectStockFromList_edit', $disable);
213         $Ajax->addDisable(true, 'SelectStockFromList', $disable);
214
215         if ($disable) {
216                 $Ajax->addFocus(true, 'OrderNumber');
217         } else
218                 $Ajax->addFocus(true, 'OrdersAfterDate');
219
220         $Ajax->activate('orders_tbl');
221 }
222
223 start_form();
224
225 start_table("class='tablestyle_noborder'");
226 start_row();
227 ref_cells(_("#:"), 'OrderNumber', '',null, '', true);
228 if ($_POST['order_view_mode'] != 'DeliveryTemplates' && $_POST['order_view_mode'] != 'InvoiceTemplates')
229 {
230         date_cells(_("from:"), 'OrdersAfterDate', '', null, -30);
231         date_cells(_("to:"), 'OrdersToDate', '', null, 1);
232 }
233 locations_list_cells(_("Location:"), 'StockLocation', null, true);
234
235 stock_items_list_cells(_("Item:"), 'SelectStockFromList', null, true);
236
237 if ($trans_type == ST_SALESQUOTE)
238         check_cells(_("Show All:"), 'show_all');
239 submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
240
241 hidden('order_view_mode', $_POST['order_view_mode']);
242 hidden('type', $trans_type);
243
244 end_row();
245
246 end_table(1);
247 end_form();
248 //---------------------------------------------------------------------------------------------
249 //      Orders inquiry table
250 //
251 $sql = "SELECT 
252                 sorder.order_no,
253                 sorder.reference,
254                 debtor.name,
255                 branch.br_name,"
256                 .($_POST['order_view_mode']=='InvoiceTemplates' 
257                         || $_POST['order_view_mode']=='DeliveryTemplates' ?
258                  "sorder.comments, " : "sorder.customer_ref, ")
259                 ."sorder.ord_date,
260                 sorder.delivery_date,
261                 sorder.deliver_to,
262                 Sum(line.unit_price*line.quantity*(1-line.discount_percent)) AS OrderValue,
263                 sorder.type,
264                 debtor.curr_code,
265                 Sum(line.qty_sent) AS TotDelivered,
266                 Sum(line.quantity) AS TotQuantity
267         FROM ".TB_PREF."sales_orders as sorder, "
268                 .TB_PREF."sales_order_details as line, "
269                 .TB_PREF."debtors_master as debtor, "
270                 .TB_PREF."cust_branch as branch
271                 WHERE sorder.order_no = line.order_no
272                 AND sorder.trans_type = line.trans_type
273                 AND sorder.trans_type = $trans_type
274                 AND sorder.debtor_no = debtor.debtor_no
275                 AND sorder.branch_code = branch.branch_code
276                 AND debtor.debtor_no = branch.debtor_no";
277
278 if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
279 {
280         // search orders with number like ...
281         $sql .= " AND sorder.order_no LIKE '%". $_POST['OrderNumber'] ."'"
282                         ." GROUP BY sorder.order_no";
283 }
284 else    // ... or select inquiry constraints
285 {
286         if ($_POST['order_view_mode']!='DeliveryTemplates' && $_POST['order_view_mode']!='InvoiceTemplates')
287         {
288                 $date_after = date2sql($_POST['OrdersAfterDate']);
289                 $date_before = date2sql($_POST['OrdersToDate']);
290
291                 $sql .=  " AND sorder.ord_date >= '$date_after'"
292                                 ." AND sorder.ord_date <= '$date_before'";
293         }
294         if ($trans_type == 32 && !check_value('show_all'))
295                 $sql .= " AND sorder.delivery_date >= '".date2sql(Today())."'";
296         if ($selected_customer != -1)
297                 $sql .= " AND sorder.debtor_no='" . $selected_customer . "'";
298
299         if (isset($selected_stock_item))
300                 $sql .= " AND line.stk_code='". $selected_stock_item ."'";
301
302         if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != ALL_TEXT)
303                 $sql .= " AND sorder.from_stk_loc = '". $_POST['StockLocation'] . "' ";
304
305         if ($_POST['order_view_mode']=='OutstandingOnly')
306                 $sql .= " AND line.qty_sent < line.quantity";
307         elseif ($_POST['order_view_mode']=='InvoiceTemplates' || $_POST['order_view_mode']=='DeliveryTemplates')
308                 $sql .= " AND sorder.type=1";
309
310         $sql .= " GROUP BY sorder.order_no,
311                                 sorder.debtor_no,
312                                 sorder.branch_code,
313                                 sorder.customer_ref,
314                                 sorder.ord_date,
315                                 sorder.deliver_to";
316 }
317
318 if ($trans_type == ST_SALESORDER)
319         $cols = array(
320                 _("Order #") => array('fun'=>'view_link'),
321                 _("Ref"),
322                 _("Customer"),
323                 _("Branch"), 
324                 _("Cust Order Ref"),
325                 _("Order Date") => 'date',
326                 _("Required By") =>array('type'=>'date', 'ord'=>''),
327                 _("Delivery To"), 
328                 _("Order Total") => array('type'=>'amount', 'ord'=>''),
329                 'Type' => 'skip',
330                 _("Currency") => array('align'=>'center')
331         );
332 else
333         $cols = array(
334                 _("Quote #") => array('fun'=>'view_link'),
335                 _("Ref"),
336                 _("Customer"),
337                 _("Branch"), 
338                 _("Cust Order Ref"),
339                 _("Quote Date") => 'date',
340                 _("Valid until") =>array('type'=>'date', 'ord'=>''),
341                 _("Delivery To"), 
342                 _("Quote Total") => array('type'=>'amount', 'ord'=>''),
343                 'Type' => 'skip',
344                 _("Currency") => array('align'=>'center')
345         );
346 if ($_POST['order_view_mode'] == 'OutstandingOnly') {
347         //array_substitute($cols, 3, 1, _("Cust Order Ref"));
348         array_append($cols, array(array('insert'=>true, 'fun'=>'dispatch_link')));
349
350 } elseif ($_POST['order_view_mode'] == 'InvoiceTemplates') {
351         array_substitute($cols, 3, 1, _("Description"));
352         array_append($cols, array( array('insert'=>true, 'fun'=>'invoice_link')));
353
354 } else if ($_POST['order_view_mode'] == 'DeliveryTemplates') {
355         array_substitute($cols, 3, 1, _("Description"));
356         array_append($cols, array(
357                         array('insert'=>true, 'fun'=>'delivery_link'))
358         );
359
360 } elseif ($trans_type == ST_SALESQUOTE) {
361          array_append($cols,array(
362                                         array('insert'=>true, 'fun'=>'edit_link'),
363                                         array('insert'=>true, 'fun'=>'order_link'),
364                                         array('insert'=>true, 'fun'=>'prt_link')));
365 } elseif ($trans_type == ST_SALESORDER) {
366          array_append($cols,array(
367                         _("Tmpl") => array('insert'=>true, 'fun'=>'tmpl_checkbox'),
368                                         array('insert'=>true, 'fun'=>'edit_link'),
369                                         array('insert'=>true, 'fun'=>'prt_link')));
370 };
371
372
373 $table =& new_db_pager('orders_tbl', $sql, $cols);
374 $table->set_marker('check_overdue', _("Marked items are overdue."));
375
376 $table->width = "80%";
377 start_form();
378
379 display_db_pager($table);
380 submit_center('Update', _("Update"), true, '', null);
381
382 end_form();
383 end_page();
384 ?>