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 = "../..";
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");
19 $page_security = 'SA_TRANSVIEW';
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')
29 if ($use_popup_windows)
30 $js .= get_js_open_window(900, 600);
32 $js .= get_js_date_picker();
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;
41 $page_security = 'SA_DENIED';
43 if ($trans_type == ST_SALESORDER)
45 if (isset($_GET['OutstandingOnly']) && ($_GET['OutstandingOnly'] == true))
47 $_POST['order_view_mode'] = 'OutstandingOnly';
48 $_SESSION['page_title'] = _("Search Outstanding Sales Orders");
50 elseif (isset($_GET['InvoiceTemplates']) && ($_GET['InvoiceTemplates'] == true))
52 $_POST['order_view_mode'] = 'InvoiceTemplates';
53 $_SESSION['page_title'] = _("Search Template for Invoicing");
55 elseif (isset($_GET['DeliveryTemplates']) && ($_GET['DeliveryTemplates'] == true))
57 $_POST['order_view_mode'] = 'DeliveryTemplates';
58 $_SESSION['page_title'] = _("Select Template for Delivery");
60 elseif (!isset($_POST['order_view_mode']))
62 $_POST['order_view_mode'] = false;
63 $_SESSION['page_title'] = _("Search All Sales Orders");
68 $_POST['order_view_mode'] = "Quotations";
69 $_SESSION['page_title'] = _("Search All Sales Quotations");
71 page($_SESSION['page_title'], false, false, "", $js);
73 if (isset($_GET['selected_customer']))
75 $selected_customer = $_GET['selected_customer'];
77 elseif (isset($_POST['selected_customer']))
79 $selected_customer = $_POST['selected_customer'];
82 $selected_customer = -1;
84 //---------------------------------------------------------------------------------------------
86 if (isset($_POST['SelectStockFromList']) && ($_POST['SelectStockFromList'] != "") &&
87 ($_POST['SelectStockFromList'] != ALL_TEXT))
89 $selected_stock_item = $_POST['SelectStockFromList'];
93 unset($selected_stock_item);
95 //---------------------------------------------------------------------------------------------
96 // Query format functions
98 function check_overdue($row)
101 if ($trans_type == ST_SALESQUOTE)
102 return (date1_greater_date2(Today(), sql2date($row['delivery_date'])));
104 return ($row['type'] == 0
105 && date1_greater_date2(Today(), sql2date($row['ord_date']))
106 && ($row['TotDelivered'] < $row['TotQuantity']));
109 function view_link($dummy, $order_no)
112 return get_customer_trans_view_str($trans_type, $order_no);
115 function prt_link($row)
118 return print_document_link($row['order_no'], _("Print"), true, $trans_type, ICON_PRINT);
121 function edit_link($row)
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);
129 function dispatch_link($row)
132 if ($trans_type == ST_SALESORDER)
133 return pager_link( _("Dispatch"),
134 "/sales/customer_delivery.php?OrderNumber=" .$row['order_no'], ICON_DOC);
136 return pager_link( _("Sales Order"),
137 "/sales/sales_order_entry.php?OrderNumber=" .$row['order_no'], ICON_DOC);
140 function invoice_link($row)
143 if ($trans_type == ST_SALESORDER)
144 return pager_link( _("Invoice"),
145 "/sales/sales_order_entry.php?NewInvoice=" .$row["order_no"], ICON_DOC);
150 function delivery_link($row)
152 return pager_link( _("Delivery"),
153 "/sales/sales_order_entry.php?NewDelivery=" .$row['order_no'], ICON_DOC);
156 function order_link($row)
158 return pager_link( _("Sales Order"),
159 "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
162 function tmpl_checkbox($row)
165 if ($trans_type == ST_SALESQUOTE)
167 $name = "chgtpl" .$row['order_no'];
168 $value = $row['type'] ? 1:0;
170 // save also in hidden field for testing during 'Update'
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);
176 //---------------------------------------------------------------------------------------------
177 // Update db record if respective checkbox value has changed.
179 function change_tpl_flag($id)
183 $sql = "UPDATE ".TB_PREF."sales_orders SET type = !type WHERE order_no=$id";
185 db_query($sql, "Can't change sales order type");
186 $Ajax->activate('orders_tbl');
189 $id = find_submit('_chgtpl');
191 change_tpl_flag($id);
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);
199 //---------------------------------------------------------------------------------------------
202 if (get_post('_OrderNumber_changed')) // enable/disable selection controls
204 $disable = get_post('OrderNumber') !== '';
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);
211 $Ajax->addDisable(true, 'StockLocation', $disable);
212 $Ajax->addDisable(true, '_SelectStockFromList_edit', $disable);
213 $Ajax->addDisable(true, 'SelectStockFromList', $disable);
216 $Ajax->addFocus(true, 'OrderNumber');
218 $Ajax->addFocus(true, 'OrdersAfterDate');
220 $Ajax->activate('orders_tbl');
225 start_table("class='tablestyle_noborder'");
227 ref_cells(_("#:"), 'OrderNumber', '',null, '', true);
228 if ($_POST['order_view_mode'] != 'DeliveryTemplates' && $_POST['order_view_mode'] != 'InvoiceTemplates')
230 date_cells(_("from:"), 'OrdersAfterDate', '', null, -30);
231 date_cells(_("to:"), 'OrdersToDate', '', null, 1);
233 locations_list_cells(_("Location:"), 'StockLocation', null, true);
235 stock_items_list_cells(_("Item:"), 'SelectStockFromList', null, true);
237 if ($trans_type == ST_SALESQUOTE)
238 check_cells(_("Show All:"), 'show_all');
239 submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
241 hidden('order_view_mode', $_POST['order_view_mode']);
242 hidden('type', $trans_type);
248 //---------------------------------------------------------------------------------------------
249 // Orders inquiry table
256 .($_POST['order_view_mode']=='InvoiceTemplates'
257 || $_POST['order_view_mode']=='DeliveryTemplates' ?
258 "sorder.comments, " : "sorder.customer_ref, ")
260 sorder.delivery_date,
262 Sum(line.unit_price*line.quantity*(1-line.discount_percent)) AS OrderValue,
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";
278 if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
280 // search orders with number like ...
281 $sql .= " AND sorder.order_no LIKE '%". $_POST['OrderNumber'] ."'"
282 ." GROUP BY sorder.order_no";
284 else // ... or select inquiry constraints
286 if ($_POST['order_view_mode']!='DeliveryTemplates' && $_POST['order_view_mode']!='InvoiceTemplates')
288 $date_after = date2sql($_POST['OrdersAfterDate']);
289 $date_before = date2sql($_POST['OrdersToDate']);
291 $sql .= " AND sorder.ord_date >= '$date_after'"
292 ." AND sorder.ord_date <= '$date_before'";
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 . "'";
299 if (isset($selected_stock_item))
300 $sql .= " AND line.stk_code='". $selected_stock_item ."'";
302 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != ALL_TEXT)
303 $sql .= " AND sorder.from_stk_loc = '". $_POST['StockLocation'] . "' ";
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";
310 $sql .= " GROUP BY sorder.order_no,
318 if ($trans_type == ST_SALESORDER)
320 _("Order #") => array('fun'=>'view_link'),
325 _("Order Date") => 'date',
326 _("Required By") =>array('type'=>'date', 'ord'=>''),
328 _("Order Total") => array('type'=>'amount', 'ord'=>''),
330 _("Currency") => array('align'=>'center')
334 _("Quote #") => array('fun'=>'view_link'),
339 _("Quote Date") => 'date',
340 _("Valid until") =>array('type'=>'date', 'ord'=>''),
342 _("Quote Total") => array('type'=>'amount', 'ord'=>''),
344 _("Currency") => array('align'=>'center')
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')));
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')));
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'))
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')));
373 $table =& new_db_pager('orders_tbl', $sql, $cols);
374 $table->set_marker('check_overdue', _("Marked items are overdue."));
376 $table->width = "80%";
379 display_db_pager($table);
380 submit_center('Update', _("Update"), true, '', null);