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

function get_sql_for_vehicle_running_chart_inquiry($from = null, $to = null, 
            $project_id= null, $vrc_id =null,$vehicle_id = null,$supplier_id = null, $batch_no =  null)
{
        
        $sql = "SELECT 
                        vrc.id,
                        vrc.vehicle_allocation_id,
                        p.description project_description,
                        pt.description task_description,
                        vh.description vahicle_description,
                        vrc.plate_no,
                        vrc.start_date_time,
                        vrc.end_date_time,
                        vrc.meter_start,
                        vrc.meter_end,
                        vrc.cost,
                        vrc.route,
                        sup.supp_name,
                        vrc.batch_no 
                    FROM ".TB_PREF."vehicle_running_chart vrc
                            INNER JOIN ".TB_PREF."projects_master p ON
                                    p.project_id=vrc.project_id
                            INNER JOIN ".TB_PREF."project_tasks pt ON
                                    pt.task_id=vrc.task_id 
                            INNER JOIN ".TB_PREF."vehicles vh ON
                                    vh.id =vrc.vehicle_id
                            LEFT JOIN ".TB_PREF."suppliers sup ON
                                    sup.supplier_id=vrc.supplier_id
                            WHERE  1 ";
      
        if($from != null){
            $sql .= " AND vrc.start_date_time > ".db_escape(date2sql($from));
        }
        
        if($to != null){
                $sql .= " AND vrc.end_date_time < ".db_escape(date2sql($to));
        }

        if($project_id > 0){
                $sql .= " AND vrc.project_id = ".db_escape($project_id);            
        }
        
        if ($vrc_id > 0){
                $sql .= " AND vrc.id = ".$vrc_id;
        }
        
        if ($vehicle_id > 0){
                $sql .= " AND vrc.vehicle_id = ".$vehicle_id;
        }
        
        if ($supplier_id > 0)
        {
            $sql .= " AND vrc.supplier_id = ".$supplier_id;
        }
        
        if ($batch_no > 0)
        {
            $sql .= " AND vrc.batch_no = ".$batch_no;
        }

        $sql .= " ORDER BY vrc.id ";   
        
	return $sql;
}


function view_vehicle_running_chart($vrc_id)
{
        
        $sql = "SELECT 
                        vrc.*,
                        p.description project_description,
                        pt.description task_description,
                        vp.description package_description,
                        vh.description vehicle_description,
                        sup.supp_name,
                        dr.name 
                       
                    FROM ".TB_PREF."vehicle_running_chart vrc
                            LEFT JOIN ".TB_PREF."projects_master p ON
                                    p.project_id=vrc.project_id
                            LEFT JOIN ".TB_PREF."project_tasks pt ON
                                    pt.task_id=vrc.task_id 
                            LEFT JOIN ".TB_PREF."vehicles vh ON
                                    vh.id=vrc.vehicle_id
                            LEFT JOIN ".TB_PREF."suppliers sup ON
                                    vrc.supplier_id=sup.supplier_id
                            LEFT JOIN ".TB_PREF."vehicle_packages vp ON
                                    vp.id=vrc.package_id 
                            LEFT JOIN ".TB_PREF."drivers dr ON
                                    dr.id=vrc.driver_id 
                    WHERE vrc.id = ".$vrc_id;
       
        
         $result =db_query($sql,"Could not view_vehicle_running_chart");
         return db_fetch($result);
}


function view_vehicle_running_chart_batch_task_details($task_id, $vrc_batch_no)
{
        
        $sql = "SELECT 
                        vrc.*,
                        vp.description package_description,
                        vh.description vehicle_description,
                        sup.supp_name,
                        dr.name 
                       
                    FROM ".TB_PREF."vehicle_running_chart vrc  
                            LEFT JOIN ".TB_PREF."vehicles vh ON
                                    vh.id=vrc.vehicle_id
                            LEFT JOIN ".TB_PREF."suppliers sup ON
                                    vrc.supplier_id=sup.supplier_id
                            LEFT JOIN ".TB_PREF."vehicle_packages vp ON
                                    vp.id=vrc.package_id 
                            LEFT JOIN ".TB_PREF."drivers dr ON
                                    dr.id=vrc.driver_id 
                    WHERE vrc.batch_no = ".$vrc_batch_no." AND 
                            vrc.task_id = ".$task_id;
       
        
    return db_query($sql,"Could not view_vehicle_running_chart_batch_task_details");

}

function view_vehicle_running_chart_batch_projects($vrc_batch_no)
{
    $sql = "SELECT DISTINCT pm.description project_description,
                    pm.project_id,
                    vrc.task_id 
            FROM ".TB_PREF."vehicle_running_chart vrc 
                    JOIN ".TB_PREF."project_tasks pt ON
                            pt.task_id=vrc.task_id 
                    JOIN ".TB_PREF."projects_master pm ON
                            pm.project_id=pt.project_id 
            WHERE vrc.batch_no = ".$vrc_batch_no." 
            ORDER BY pm.description";
       
        
         return db_query($sql,"view_vehicle_running_chart_batch_tasks");

}


function view_vehicle_running_chart_batch_project_tasks($project_id, $vrc_batch_no)
{
    $sql = "SELECT DISTINCT pt.description task_description,
                    vrc.task_id 
            FROM ".TB_PREF."vehicle_running_chart vrc 
                    JOIN ".TB_PREF."project_tasks pt ON
                            pt.task_id=vrc.task_id 
            WHERE vrc.batch_no = ".$vrc_batch_no." 
                    AND pt.project_id = ".$project_id." 
            ORDER BY pt.description";
       
        
         return db_query($sql,"view_vehicle_running_chart_batch_project_tasks");

}

?>