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

function get_sql_for_import_po_inquiry()
{
    $home_curr = get_company_pref('curr_default');

    $sql = "SELECT 
                porder.order_no, 
                porder.reference,
                supplier.supp_name, 
                porder.curr_code, 
                projects.description project_description,
                location.location_name,
                porder.requisition_no, 
                porder.ord_date, 
                
                porder.total + porder.taxes AS OrderValue,
                porder.approval_status 
		FROM ".TB_PREF."purch_orders as porder, "
                .TB_PREF."suppliers as supplier, "
                .TB_PREF."locations as location, "
                .TB_PREF."projects_master as projects 
	WHERE porder.supplier_id = supplier.supplier_id
                AND location.loc_code = porder.into_stock_location 
                AND porder.project_id = projects.project_id ";

                $data_after = date2sql($_POST['OrdersAfterDate']);
		$date_before = date2sql($_POST['OrdersToDate']);

		$sql .= " AND porder.ord_date >= ".db_escape($data_after)." ";
		$sql .= " AND porder.ord_date <= ".db_escape($date_before)." ";
                $sql .= " AND porder.curr_code <> ". db_escape($home_curr)." ";
                $sql .= " AND porder.curr_code <> '' ";
        
        if (isset($_POST['supplier_id']) && $_POST['supplier_id'] > 0){
            $sql .= "AND porder.supplier_id=". db_escape($_POST['supplier_id'])." ";
        }

        if (isset($_POST['order_number']) && $_POST['order_number'] > 0){
            $sql .= "AND porder.order_no=". db_escape($_POST['order_number'])." ";
        }

        if (isset($_POST['reference']) && $_POST['reference'] > 0){
            $sql .= "AND porder.reference LIKE ".db_escape('%'. $_POST['reference'] . '%');
        }
    
		if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != ALL_TEXT){
			$sql .= " AND porder.into_stock_location = ".db_escape($_POST['StockLocation']);
		}
        
        if (isset($_POST['project_id']) && $_POST['project_id'] > 0){
            $sql .= "AND porder.project_id=". db_escape($_POST['project_id'])." ";
        }

	$sql .= " GROUP BY porder.order_no";
    
	return $sql;
}	

//----------------------------------------------------------------------------------------



//----------------------------------------------------------------------------------------




?>