-$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'] != reserved_words::get_all())
- $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";
-}