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

function add_vehicle($asset_id, $description, $hired, $plate_number ,$chassis_no,$owner_details,$model_no,$engine_no,$vehicle_type,$purchased_status,$cylinder_capacity,
                     $fuel_type, $manufacturer,$country_of_origin,$year_manuf,$inactive) {
        $created_by = $_SESSION['wa_current_user']->user;
        $created_on = date('Y-m-d H:i:s');        
        
	$sql = "INSERT INTO ".TB_PREF."vehicles 
                          (id, description, hired, plate_number, 
                          chassis_no,owner_details, 
                          model_no,engine_no,
                          vehicle_type, purchased_status,
                          cylinder_capacity,fuel_type,
                          manufacturer,country_of_origin,
                          year_of_manufacture,
                          inactive, asset_id,
                          created_by, created_on) 
                VALUES ("
                    .db_escape($asset_id).", "
                    .db_escape($description).", "
                    .db_escape($hired).", "
                    .db_escape($plate_number).", "
                    .db_escape($chassis_no).", "
                    .db_escape($owner_details).", "
                    .db_escape($model_no).", "
                    .db_escape($engine_no).", "
                    .db_escape($vehicle_type).", "
                    .db_escape($purchased_status).", "
                    .db_escape($cylinder_capacity).", "
                    .db_escape($fuel_type).", "
                    .db_escape($manufacturer).", "
                    .db_escape($country_of_origin).", "
                    .db_escape($year_manuf).", "
                    .db_escape($inactive).", "
                    .db_escape($asset_id).", "
                    .db_escape($created_by).", ".db_escape($created_on).")";
        
        db_query($sql,"Vehicle could not be added");        
        return db_num_affected_rows();
}

function update_vehicle($asset_id, $description, $hired, $plate_number ,$chassis_no,$owner_details,$model_no,$engine_no,$vehicle_type,$purchased_status,$cylinder_capacity,
                        $fuel_type, $manufacturer,$country_of_origin,$year_manuf,$inactive)       
{
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');

        $sql = "UPDATE ".TB_PREF."vehicles 
                SET                     
                    description=".db_escape($description).",
                    hired=".db_escape($hired).",  
                    plate_number=".db_escape($plate_number).",  
                    chassis_no =".db_escape($chassis_no).", 
                    owner_details=".db_escape($owner_details).", 
                    model_no=".db_escape($model_no).", 
                    engine_no =".db_escape($engine_no).", 
                    vehicle_type =".db_escape($vehicle_type).", 
                    purchased_status=".db_escape($purchased_status).", 
                    cylinder_capacity =".db_escape($cylinder_capacity).", 
                    fuel_type =".db_escape($fuel_type).", 
                    manufacturer =".db_escape($manufacturer).", 
                    country_of_origin =".db_escape($country_of_origin).", 
                    year_of_manufacture=".db_escape($year_manuf).", 
                    inactive=".db_escape($inactive).", 
                    asset_id=".db_escape($asset_id).", 
                    updated_by=".db_escape($updated_by).",
                    updated_on=".db_escape($updated_on)." 
                WHERE id = ".db_escape($asset_id);

        db_query($sql,"Selected vehicle could not be updated");
        return db_num_affected_rows();
}

function delete_vehicle($selected_id)
{
    return;
        $sql = "DELETE FROM ".TB_PREF."vehicles 
                WHERE id=".db_escape($selected_id)."  ";                    

        db_query($sql," Vehicle could not be deleted");        
        return db_num_affected_rows();
}

function get_vehicles()
{
    $sql = "SELECT v.*,vt.value_name vehicle_type_name,
                   ps.value_name purchased_status_name, 
                   ft.value_name fuel_type_name,
                   mf.value_name manufacturer_name,
                   co.value_name country_of_origin,
                   yr.value_name year_manuf 
            FROM  ".TB_PREF."vehicles v
            LEFT JOIN ".TB_PREF."list_values vt ON
                vt.id = v.vehicle_type
            LEFT JOIN ".TB_PREF."list_values ps ON
                ps.id = v.purchased_status   
            LEFT JOIN ".TB_PREF."list_values ft ON
                ft.id = v.fuel_type 
            LEFT JOIN ".TB_PREF."list_values mf ON
                mf.id = v.manufacturer 
            LEFT JOIN ".TB_PREF."list_values co ON
                co.id = v.country_of_origin
            LEFT JOIN ".TB_PREF."list_values yr ON
                yr.id = v.year_of_manufacture 
            WHERE v.inactive = 0";
               
    return db_query($sql, "Could not get vehicles");
}

function get_vehicle($id)
{
    $sql = "SELECT *                                  
            FROM  ".TB_PREF."vehicles                  
            WHERE id = ".db_escape($id);
               
    $result = db_query($sql, "Could not get vehicle");
    
    return db_fetch($result);
}

function get_vehicle_for_edit($selected_id)
{
    $sql = "SELECT *
            FROM ".TB_PREF."vehicles
            WHERE id=".db_escape($selected_id);
            
    $result = db_query($sql, "Could not get vehicle requests for edit");
    return db_fetch($result);
}

function check_selected_vehicle_already_in_system($asset_id)
{
    $sql = "SELECT *
            FROM ".TB_PREF."vehicles
            WHERE id = ".db_escape($asset_id);
            
    $result = db_query($sql, "check_selected_vehicle_already_in_system");
    if(db_num_rows($result) > 0){
        $vehicle = db_fetch($result);
        $vehicle_formatted_name = get_vehicle_formatted_name($vehicle["id"]);
        return array("success"=>false, "message" => "Selected vehicle is already in the system as ".$vehicle_formatted_name);
    }else{
        return array("success"=>true, "message" => "");
    }
}

function get_vehicle_with_asset($asset_id) {
    $sql = "SELECT a.asset_id parent_asset_id, a.type parent_type, a.description asset_description, v.*  
            FROM ".TB_PREF."asset_master a 
                LEFT JOIN ".TB_PREF."vehicles v  
                    ON a.asset_id = v.id  
            WHERE a.asset_id = ".db_escape($asset_id);
            
        $result = db_query($sql, "get_vehicle_with_asset");
        return db_fetch($result);
}

function update_vehicle_description($asset_id, $description) {        
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');

        $sql = "UPDATE ".TB_PREF."vehicles 
                SET description = ".db_escape($description).",
                    updated_by = ".db_escape($updated_by).",
                    updated_on = ".db_escape($updated_on)." 
                WHERE id = ".db_escape($asset_id);

        db_query($sql, "update_vehicle_description");
        return db_num_affected_rows();
}

?>