<?php
/**********************************************************************
***********************************************************************/

function get_sqs_for_reopen($order_number = null, $reference = null, $customer_id = null, 
                    $data_after = null, $date_before = null)
{

    $sql = "SELECT 
                so.order_no, 
                so.reference, 
                dr.name customer_name, 
                l.location_name,

                so.ord_date, 
                dr.curr_code,
                so.total OrderValue,

                so.from_stk_loc,
                so.approval_status 
		FROM ".TB_PREF."sales_orders as so, 
                ".TB_PREF."sales_order_details as sod, 
                ".TB_PREF."debtors_master as dr, 
                ".TB_PREF."locations as l 
		WHERE so.order_no = sod.order_no
                AND so.debtor_no = dr.debtor_no
                AND l.loc_code = so.from_stk_loc 
                AND so.trans_type = ". db_escape(ST_SALESQUOTE)."
                AND so.approval_status =". db_escape(AS_APPROVED);
    
    
        if ($customer_id != null && $customer_id > 0){
                $sql .= "AND so.debtor_no=". db_escape($customer_id)." ";
        }
        
        if ($order_number != null && $order_number > 0){
            $sql .= "AND so.order_no = ".db_escape($order_number);
        }
        
        if ($reference != null && $reference != ''){
            $sql .= "AND so.reference LIKE ".db_escape('%'. $reference . '%');
        }
        
        if ($data_after != null){
            $sql .= " AND so.ord_date >= '$data_after'";
        }

        if ($date_before != null){
            $sql .= " AND so.ord_date <= '$date_before'";
        }

        $sql .= " GROUP BY so.order_no ";
        
        return db_query($sql,"Could not get_sqs_for_reopen");
	
  }	

?>