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