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