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

function add_salary_addition($employee_code, $type_id, 
                            $salary_addition, 
                            $due_date, $comments, $status_code)
{
        $created_by = $_SESSION['wa_current_user']->user;
        $created_on = date('Y-m-d H:i:s');
        
        
	$sql = "INSERT INTO ".TB_PREF."salary_additions (
                    employee_code, type_id, 
                    salary_addition,  
                    due_date, comments, 
                    status_code,
                    created_by, created_on) 
                VALUES ("
                    .db_escape($employee_code).", ".db_escape($type_id).", "
                    .db_escape($salary_addition).", "
                    .db_escape(date2sql($due_date)). ", ".db_escape($comments).", "
                    .db_escape($status_code).", "
                    .db_escape($created_by).", ".db_escape($created_on).")";
        
        db_query($sql,"Salary addition could not be added");
        
        return db_insert_id();

}


function update_salary_addition($rec_id, $employee_code, 
                            $type_id, $salary_addition, 
                            $due_date, $comments, $status_code)
{

         $updated_by = $_SESSION['wa_current_user']->user;
         $updated_on = date('Y-m-d H:i:s');

        $sql = "UPDATE ".TB_PREF."salary_additions 
                SET 
                    salary_addition=".db_escape($salary_addition).",
                    type_id=".db_escape($type_id).",
                    due_date =".db_escape(date2sql($due_date)).",
                    comments =".db_escape($comments).",
                    status_code = ".db_escape($status_code).",
                    updated_by=".db_escape($updated_by).",
                    updated_on=".db_escape($updated_on)." 
                WHERE id=".db_escape($rec_id)." AND 
                    employee_code =".db_escape($employee_code);

        db_query($sql,"Salary addition could not be updated");
        
        return db_num_affected_rows();
}

function delete_salary_addition($rec_id, $employee_code)
{
        $sql = "DELETE FROM ".TB_PREF."salary_additions  
                WHERE id=".db_escape($rec_id)." AND 
                    employee_code =".db_escape($employee_code);

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

function get_salary_addition_details($selected_id)
{
    $sql = "SELECT s.*  
            FROM " 
                .TB_PREF."salary_additions s 
            WHERE s.id = ".db_escape($selected_id);
  

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


function get_salary_additions($employee_code)
{
    $sql = "SELECT s.*, 
                st.value_name status_name,
                ty.value_name type_name
            FROM " 
                .TB_PREF."salary_additions s, "
                .TB_PREF."list_values st, "
                .TB_PREF."list_values ty      
            WHERE s.employee_code = ".db_escape($employee_code)." AND 
                    s.status_code = st.value_code AND st.list_code = 'RECORD_STATUS' AND 
                    s.type_id = ty.id  
            ORDER BY due_date";
  

    return db_query($sql, "Salary additions could not be retrieved");
}

function get_salary_addition_existing_record($employee_code, $type_id, $salary_addition, $due_date)
{
    $sql = "SELECT *  
            FROM ".TB_PREF."salary_additions  
            WHERE employee_code = ".db_escape($employee_code)." 
                    AND type_id = ".db_escape($type_id)." 
                    AND salary_addition = ".db_escape($salary_addition)." 
                    AND due_date = ".db_escape(date2sql($due_date));  

    $result = db_query($sql, "Could not get_salary_addition_existing_record");
    
    return db_fetch($result);
}

?>