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

function write_drivers($selected_id, $name, $contact_nos, $dl_no, 
                       $nic, $employee_code, $inactive)                                 
{
    $user = $_SESSION['wa_current_user']->user;
    $date = date('Y-m-d H:i:s');
    
    if($selected_id != '')
    {
        $sql = "UPDATE ".TB_PREF."drivers 
                SET
                    name = ".db_escape($name).",
                    contact_nos = ".db_escape($contact_nos).",
                    dl_no = ".db_escape($dl_no).",
                    nic = ".db_escape($nic).",
                    employee_code = ".db_escape($employee_code).",  
                    inactive = ".db_escape($inactive).", 
                    updated_by = ".db_escape($user).",
                    updated_on = ".db_escape($date)."
                WHERE 
                    id = ".db_escape($selected_id);
        
        db_query($sql, "Drivers could not be updated");
    }
    else
    {
        $sql = "INSERT INTO ".TB_PREF."drivers
                      (name, contact_nos, dl_no, nic, 
                       employee_code, inactive, created_by, created_on) 
                VALUES
                        (".db_escape($name).",".db_escape($contact_nos).",
                         ".db_escape($dl_no).", ".db_escape($nic).",
                         ".db_escape($employee_code).",".db_escape($inactive).", "                        
                         .db_escape($user).",".db_escape($date).")";
        
        db_query($sql, "Drivers could not be added");
        return db_insert_id();
    }
    
}

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

	db_query($sql,"Selected driver could not be deleted");
}
////
function get_drivers_for_editing($id)
{
	$sql="SELECT d.id,
                     d.name,
                     d.contact_nos,
                     d.dl_no,
                     d.nic, 
                     d.inactive,
                     d.employee_code driver_code,                 
                     em.initials,
                     em.last_name,
                     em.first_name,
                     em.employee_code

            FROM ".TB_PREF."drivers d
                LEFT JOIN  ".TB_PREF."employee_master em ON            
                        d.employee_code = em.employee_code 
                        
           WHERE d.id=".db_escape($id);

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

function get_all_drivers() {
    $sql = "SELECT d.id,
                   d.name,
                   d.contact_nos,
                   d.dl_no,
                   d.nic, 
                   d.inactive,
                   d.employee_code driver_code,                 
                   em.initials,
                   em.last_name,
                   em.first_name,
                   em.employee_code

            FROM ".TB_PREF."drivers d
                LEFT JOIN  ".TB_PREF."employee_master em ON            
                        d.employee_code = em.employee_code ";
                 
                         
    return  db_query($sql, "Could not get drivers");
}

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;
}
?>