Merged changes form main trunk (2.2.8, 2.2.9)
[fa-stable.git] / sales / inquiry / sales_orders_view.php
index d9e993a56d527e03f75b0f793883949bf86da275..0a45cde718ed3dd4c97e58547127938ac3042a6b 100644 (file)
@@ -16,7 +16,7 @@ include($path_to_root . "/includes/session.inc");
 include($path_to_root . "/sales/includes/sales_ui.inc");
 include_once($path_to_root . "/reporting/includes/reporting.inc");
 
-$page_security = 'SA_TRANSVIEW';
+$page_security = 'SA_SALESTRANSVIEW';
 
 set_page_security( @$_POST['order_view_mode'],
        array(  'OutstandingOnly' => 'SA_SALESDELIVERY',
@@ -35,38 +35,36 @@ if (get_post('type'))
        $trans_type = $_POST['type'];
 elseif (isset($_GET['type']) && $_GET['type'] == ST_SALESQUOTE)
        $trans_type = ST_SALESQUOTE;
-elseif (isset($_GET['type']) && $_GET['type'] == ST_SALESORDER)
-       $trans_type = ST_SALESORDER;
 else
-       $page_security = 'SA_DENIED';
+       $trans_type = ST_SALESORDER;
 
 if ($trans_type == ST_SALESORDER)
 {
        if (isset($_GET['OutstandingOnly']) && ($_GET['OutstandingOnly'] == true))
        {
                $_POST['order_view_mode'] = 'OutstandingOnly';
-               $_SESSION['page_title'] = _("Search Outstanding Sales Orders");
+               $_SESSION['page_title'] = _($help_context = "Search Outstanding Sales Orders");
        }
        elseif (isset($_GET['InvoiceTemplates']) && ($_GET['InvoiceTemplates'] == true))
        {
                $_POST['order_view_mode'] = 'InvoiceTemplates';
-               $_SESSION['page_title'] = _("Search Template for Invoicing");
+               $_SESSION['page_title'] = _($help_context = "Search Template for Invoicing");
        }
        elseif (isset($_GET['DeliveryTemplates']) && ($_GET['DeliveryTemplates'] == true))
        {
                $_POST['order_view_mode'] = 'DeliveryTemplates';
-               $_SESSION['page_title'] = _("Select Template for Delivery");
+               $_SESSION['page_title'] = _($help_context = "Select Template for Delivery");
        }
        elseif (!isset($_POST['order_view_mode']))
        {
                $_POST['order_view_mode'] = false;
-               $_SESSION['page_title'] = _("Search All Sales Orders");
+               $_SESSION['page_title'] = _($help_context = "Search All Sales Orders");
        }
 }
 else
 {
        $_POST['order_view_mode'] = "Quotations";
-       $_SESSION['page_title'] = _("Search All Sales Quotations");
+       $_SESSION['page_title'] = _($help_context = "Search All Sales Quotations");
 }
 page($_SESSION['page_title'], false, false, "", $js);
 
@@ -222,11 +220,12 @@ if (get_post('_OrderNumber_changed')) // enable/disable selection controls
 
 start_form();
 
-start_table("class='tablestyle_noborder'");
+start_table(TABLESTYLE_NOBORDER);
 start_row();
 ref_cells(_("#:"), 'OrderNumber', '',null, '', true);
 if ($_POST['order_view_mode'] != 'DeliveryTemplates' && $_POST['order_view_mode'] != 'InvoiceTemplates')
 {
+       ref_cells(_("Ref"), 'OrderReference', '',null, '', true);
        date_cells(_("from:"), 'OrdersAfterDate', '', null, -30);
        date_cells(_("to:"), 'OrdersToDate', '', null, 1);
 }
@@ -244,76 +243,10 @@ hidden('type', $trans_type);
 end_row();
 
 end_table(1);
-end_form();
 //---------------------------------------------------------------------------------------------
 //     Orders inquiry table
 //
-$sql = "SELECT 
-               sorder.order_no,
-               sorder.reference,
-               debtor.name,
-               branch.br_name,"
-               .($_POST['order_view_mode']=='InvoiceTemplates' 
-                       || $_POST['order_view_mode']=='DeliveryTemplates' ?
-                "sorder.comments, " : "sorder.customer_ref, ")
-               ."sorder.ord_date,
-               sorder.delivery_date,
-               sorder.deliver_to,
-               Sum(line.unit_price*line.quantity*(1-line.discount_percent)) AS OrderValue,
-               sorder.type,
-               debtor.curr_code,
-               Sum(line.qty_sent) AS TotDelivered,
-               Sum(line.quantity) AS TotQuantity
-       FROM ".TB_PREF."sales_orders as sorder, "
-               .TB_PREF."sales_order_details as line, "
-               .TB_PREF."debtors_master as debtor, "
-               .TB_PREF."cust_branch as branch
-               WHERE sorder.order_no = line.order_no
-               AND sorder.trans_type = line.trans_type
-               AND sorder.trans_type = $trans_type
-               AND sorder.debtor_no = debtor.debtor_no
-               AND sorder.branch_code = branch.branch_code
-               AND debtor.debtor_no = branch.debtor_no";
-
-if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
-{
-       // search orders with number like ...
-       $sql .= " AND sorder.order_no LIKE '%". $_POST['OrderNumber'] ."'"
-                       ." GROUP BY sorder.order_no";
-}
-else   // ... or select inquiry constraints
-{
-       if ($_POST['order_view_mode']!='DeliveryTemplates' && $_POST['order_view_mode']!='InvoiceTemplates')
-       {
-               $date_after = date2sql($_POST['OrdersAfterDate']);
-               $date_before = date2sql($_POST['OrdersToDate']);
-
-               $sql .=  " AND sorder.ord_date >= '$date_after'"
-                               ." AND sorder.ord_date <= '$date_before'";
-       }
-       if ($trans_type == 32 && !check_value('show_all'))
-               $sql .= " AND sorder.delivery_date >= '".date2sql(Today())."'";
-       if ($selected_customer != -1)
-               $sql .= " AND sorder.debtor_no='" . $selected_customer . "'";
-
-       if (isset($selected_stock_item))
-               $sql .= " AND line.stk_code='". $selected_stock_item ."'";
-
-       if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != ALL_TEXT)
-               $sql .= " AND sorder.from_stk_loc = '". $_POST['StockLocation'] . "' ";
-
-       if ($_POST['order_view_mode']=='OutstandingOnly')
-               $sql .= " AND line.qty_sent < line.quantity";
-       elseif ($_POST['order_view_mode']=='InvoiceTemplates' || $_POST['order_view_mode']=='DeliveryTemplates')
-               $sql .= " AND sorder.type=1";
-
-       $sql .= " GROUP BY sorder.order_no,
-                               sorder.debtor_no,
-                               sorder.branch_code,
-                               sorder.customer_ref,
-                               sorder.ord_date,
-                               sorder.deliver_to";
-}
+$sql = get_sql_for_sales_orders_view($selected_customer, $trans_type);
 
 if ($trans_type == ST_SALESORDER)
        $cols = array(
@@ -374,7 +307,6 @@ $table =& new_db_pager('orders_tbl', $sql, $cols);
 $table->set_marker('check_overdue', _("Marked items are overdue."));
 
 $table->width = "80%";
-start_form();
 
 display_db_pager($table);
 submit_center('Update', _("Update"), true, '', null);