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


function get_performance_incentives_assigned_employee_list($salary_component_id, $from_date, $to_date)
{  
	$sql = " SELECT em.initials, em.first_name,em.middle_name,em.last_name,
                        sc.amount,sc.calculation_type,sc.salary_total_code, sc.table_id,sc.employee_code, sc.comments note,
                        lv.value_code salary_component  
            FROM ".TB_PREF."employee_master em,
                    ".TB_PREF."salary_components sc,
                    ".TB_PREF."list_values lv     
            WHERE sc.employee_code = em.employee_code AND
                  (sc.type = ".  db_escape(SCT_PERFORMANCE_RATE_INCENTIVES)." OR sc.type = ".  db_escape(SCT_PERFORMANCE_VALUE_INCENTIVES)." ) AND 
                  sc.salary_component_id = lv.id AND 
                  sc.salary_component_id = ".db_escape($salary_component_id)." AND 
                  sc.effective_date <= ".db_escape($to_date)." AND 
                  sc.end_date >= ".  db_escape($from_date)." 
            ORDER BY em.employee_code 
                    ";
	$result = db_query($sql,"Performance incentives assigned employee list could not be retrieved");	
        
        return $result;
}

function get_saved_performance_incentives_amounts($payroll_run_id, $employee_code, $salary_component_id)
{  
	$sql = "SELECT payroll_run_id, amount_achieved, comments, status_code, pid.id,
                    lv.value_name status_name 
            FROM ".TB_PREF."performance_incentives_details pid,
                    ".TB_PREF."list_values lv,
                    ".TB_PREF."list_values sc   
            WHERE pid.status_code = lv.value_code AND lv.list_code = 'ATTENDANCE_STATUS' AND 
                    pid.component = sc.value_code AND 
                    (sc.list_code = 'PERFORMANCE_RATE_INCENTIVES' OR sc.list_code = 'PERFORMANCE_VALUE_INCENTIVES') AND  
                    payroll_run_id = ".db_escape($payroll_run_id)." AND 
                    employee_code = ".db_escape($employee_code)." AND 
                    sc.id = ".db_escape($salary_component_id)." LIMIT 1 ";
    
	$result = db_query($sql,"Saved performance incentives details could not be retrieved");	
        
    return db_fetch($result);
}
//
//function get_allowances($employee_code, $salary_component_id)
//{
//    $sql="SELECT * FROM ".TB_PREF."mobile_allowances_details 
//                WHERE employee_id=".db_escape($employee_code)."AND
//                    component_id=".db_escape($employee_code)."
//                        " ;
//
//	$result = db_query($sql,"Details could not be retrieved");
//
//	return db_fetch($result);
//}

function add_performance_incentive_value($employee_code, $salary_component, $amount_achieved, $payroll_run_id, $comments)
{
    
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
    
    $sql = "INSERT INTO ".TB_PREF."performance_incentives_details 
                            (employee_code, component,  
                            amount_achieved, 
                            payroll_run_id, comments,  
                            status_code, 
                            created_by, created_on) 
			VALUES( ".db_escape($employee_code).",".db_escape($salary_component).","
                            .db_escape($amount_achieved).","
                            .db_escape($payroll_run_id).",".db_escape($comments).","
                            .db_escape(ATD_NEW).","
                            .db_escape($id_user).",".db_escape($datetime).")";

	db_query($sql,"Performance incentives detail could not be added");
    return db_insert_id();
}

function update_performance_incentive_value($row_id, $employee_code, 
                                    $amount_achieved, 
                                    $payroll_run_id, $comments, $status_code)
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
    
	$sql = "UPDATE ".TB_PREF."performance_incentives_details 
            SET
                amount_achieved = ".db_escape($amount_achieved).",
                comments = ".db_escape($comments).",
                status_code = ".db_escape($status_code).", 
                updated_by = ".db_escape($id_user).",
                updated_on = ".db_escape($datetime)."

        	WHERE employee_code = ".db_escape($employee_code)."AND 
                    payroll_run_id = ".db_escape($payroll_run_id)."AND
                    id=".db_escape($row_id) ;

	db_query($sql,"Performance incentives detail could not be updated");
        return db_num_affected_rows();
        
}

function approve_performance_incentive_detail($row_id, $employee_code, $payroll_run_id)
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
    
	$sql = "UPDATE ".TB_PREF."performance_incentives_details 
            SET status_code = ".db_escape(ATD_APPROVED).", 
                approved_by = ".db_escape($id_user).",
                approved_on = ".db_escape($datetime)."
        	WHERE payroll_run_id = ".db_escape($payroll_run_id)."AND
                employee_code = ".db_escape($employee_code)."AND 
                id=".db_escape($row_id) ;

	db_query($sql,"Performance incentive detail could not be updated");        
}


function delete_performance_incentive_value($row_id)
{
    $sql="DELETE FROM ".TB_PREF."performance_incentives_details WHERE id=".db_escape($row_id);

	db_query($sql,"Could not delete_performance_incentive_value");
}

?>