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

function write_vehicle_packages($selected_id, $vehicle_type,$code, $description, $unit, 
                                $unit_rate, $hourly_rate, $max_hours_per_day,
                                $cost)
{
    $user = $_SESSION['wa_current_user']->user;
    $date = date('Y-m-d H:i:s');
    
    if($selected_id != '')
    {
        $sql = "UPDATE ".TB_PREF."vehicle_packages 
                SET 
                    vehicle_type_id= ".db_escape($vehicle_type).",
                    code = ".db_escape($code).",
                    description = ".db_escape($description).",
                    unit = ".db_escape($unit).",
                    unit_rate = ".db_escape($unit_rate).",
                    hourly_rate	 = ".db_escape($hourly_rate).",
                    max_hours_per_day = ".db_escape($max_hours_per_day).",
                    cost = ".db_escape($cost).",
                    updated_by = ".db_escape($user).",
                    updated_on = ".db_escape($date)."
                WHERE 
                    id = ".db_escape($selected_id);
        
        db_query($sql, "Employee Rate could not be updated");
    }
    else
    {
        $sql = "INSERT INTO ".TB_PREF."vehicle_packages
                      (vehicle_type_id,code, description, 
                      unit, unit_rate, 
                      hourly_rate, max_hours_per_day,
                      cost,
                      created_by, created_on) 
                VALUES
                        (".db_escape($vehicle_type).",".db_escape($code).",".db_escape($description).",
                         ".db_escape($unit).", ".db_escape($unit_rate).",
                         ".db_escape($hourly_rate).",".db_escape($max_hours_per_day).",
                         ".db_escape($cost).",
                         ".db_escape($user).",".db_escape($date).")";
        
        db_query($sql, "Employee Rate could not be added");
    }
    
}

function delete_vehicle_package($id)
{
	$sql="DELETE FROM ".TB_PREF."vehicle_packages 
                    WHERE 
                        id=".db_escape($id);

	db_query($sql,"Selected vehicle type could not be deleted");
}
////
function get_vehicle_packages_for_editing($id)
{
	$sql="SELECT * FROM ".TB_PREF."vehicle_packages
                WHERE 
                    id=".db_escape($id);

	$result = db_query($sql,"Selected employee rate could not be retrieved");
	return db_fetch($result);
}

function get_all_vehicle_packages() {
    $sql = "SELECT vp.* ,lv.value_name vehicle_type_name
            FROM ".TB_PREF."vehicle_packages vp
            LEFT JOIN ".TB_PREF."list_values lv ON
                lv.id = vp.vehicle_type_id"   ;
        
    return  db_query($sql, "Could not get employee rates");
}

function get_vehicle_package_cost($id)
{
    $sql="SELECT cost FROM ".TB_PREF."vehicle_packages
                WHERE 
                    id=".db_escape($id);

	$result = db_query($sql,"Could not get_vehicle_package_cost");
	$package =  db_fetch($result);
    
    $package_cost = 0;
    if(isset($package['cost'])){
        $package_cost = $package['cost'];
    }
    
    return $package_cost;
}
?>