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


function get_sql_for_purchase_dispatch_request_inquiry($project_id, $customer_id, $from_date, $to_date)
{
    $sql = "SELECT pdr.id,
                    pm.project_name,
                    dm.name customer_name,
                    cb.br_name branch_name,
                    pdr.reference,
                    pdr.tran_date,
                    pdr.location,
                    ps.description site_name,
                    lo.location_name,
                    pdr.location

            FROM ".TB_PREF."project_dispatch_requests pdr 
                    LEFT JOIN ".TB_PREF."projects_master pm ON
                        pdr.project_id = pm.project_id 

                    LEFT JOIN ".TB_PREF."debtors_master dm ON
                        dm.debtor_no = pdr.debtor_no

                    LEFT JOIN ".TB_PREF."cust_branch cb ON
                        cb.branch_code = pdr.branch_code
                        
                    LEFT JOIN ".TB_PREF."project_sites ps ON
                        ps.id = pdr.site_id
                        
                    LEFT JOIN ".TB_PREF."locations lo ON
                        lo.loc_code = pdr.location
                                        
                 
            WHERE pdr.tran_date >=".db_escape(date2sql($from_date))." AND  pdr.tran_date <=".db_escape(date2sql($to_date));
      
   
    if($project_id > 0){
        $sql .= "AND pdr.project_id=".db_escape($project_id);
    }
    
    if($customer_id > 0){
        $sql .= "AND pdr.debtor_no=".db_escape($customer_id);
    }
    
//    if($project_id > 0){
//        $sql .= "AND pso.project_id=".db_escape($project_id);
//    }
//    
   
    return $sql;
    
}
        

function view_project_dispatch_request_header($trans_no)
{
    $sql = "SELECT pdr.id,
                    pm.project_name,
                    dm.name customer_name,
                    cb.br_name branch_name,
                    pdr.reference,
                    pdr.tran_date,
                    lo.location_name,
                    ps.description site_name

            FROM ".TB_PREF."project_dispatch_requests pdr, 
                    ".TB_PREF."projects_master pm,
                    ".TB_PREF."debtors_master dm,
                    ".TB_PREF."cust_branch cb,
                    ".TB_PREF."locations lo,
                    ".TB_PREF."project_sites ps

            WHERE pdr.project_id = pm.project_id AND
                    dm.debtor_no = pdr.debtor_no AND
                    cb.branch_code = pdr.branch_code AND   
                    lo.loc_code = pdr.location AND
                    ps.id = pdr.site_id AND
                    pdr.id=".db_escape($trans_no);         
    
    $result = db_query($sql);
    return db_fetch($result);

}


function view_project_dispatch_request_details($trans_no)
{
    $sql = "SELECT pdrd.*,
                    boqi.description boq_iem_description,  
                    pt.description budget_name
                    
            FROM ".TB_PREF."project_dispatch_request_details pdrd 
                    LEFT JOIN ".TB_PREF."project_boq_items boqi ON 
                            pdrd.item_id = boqi.id 
                            
                    LEFT JOIN ".TB_PREF."project_tasks pt ON
                            pt.task_id = pdrd.task_id
                            
            WHERE pdrd.pdr_no=".db_escape($trans_no);
    
    return db_query($sql);   
    
}

        
        
?>