<?php
/**********************************************************************
***********************************************************************/
function add_salary_component($payroll_id, $type, $employee_code, 
                        $salary_component_id, $deduction,
                        $calculation_type, $amount, 
                        $salary_total_code, $table_id, 
                        $effective_date, $end_date,$full_amount, 
                        $nopay_deduct, $comments, 
                        $inactive)
{
	$created_by = $_SESSION['wa_current_user']->user;
	$created_on = date('Y-m-d H:i:s');
        
        $sql = "INSERT INTO ".TB_PREF."salary_components (
                    payroll_id, type, employee_code, 
                    salary_component_id, deduction,
                    calculation_type,amount, 
                    salary_total_code, table_id, 
                    effective_date,end_date,full_amount, 
                    nopay_deduct, comments, 
                    inactive, 
                    created_by, created_on) 
		VALUES ("
                    .db_escape($payroll_id).",".db_escape($type).",".db_escape($employee_code).", "
                    .db_escape($salary_component_id).", ".db_escape($deduction).", "
                    .db_escape($calculation_type).", ".db_escape($amount).", "
                    .db_escape($salary_total_code).", ".db_escape($table_id).", "
                    .db_escape(date2sql($effective_date)).", ".db_escape(date2sql($end_date)).", "
                    .db_escape($full_amount).", "
                    .db_escape($nopay_deduct).",".db_escape($comments).", "
                    .db_escape($inactive).", "
                    .db_escape($created_by).", ".db_escape($created_on).")";
	
	db_query($sql,"Salary component record could not be added");		
}

function update_salary_component($selected_id, $payroll_id, $deduction,
                        $calculation_type, $amount, 
                        $salary_total_code, $table_id, 
                        $effective_date, $end_date,$full_amount, 
                        $nopay_deduct, $comments, 
                        $inactive,
                        $type, $employee_code, 
                        $salary_component_id)
{
	                
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
     	
    $sql = "UPDATE ".TB_PREF."salary_components "
                . "SET "
                    . "payroll_id=".db_escape($payroll_id).", "
                    . "deduction=".db_escape($deduction).", "
                    . "calculation_type=".db_escape($calculation_type).", "
                    . "amount=".db_escape($amount).","
                    . "salary_total_code=".db_escape($salary_total_code).", "
                    . "table_id=".db_escape($table_id).", "
                    . "effective_date=".db_escape(date2sql($effective_date)).","
                    . "end_date=".db_escape(date2sql($end_date)).","
                    . "full_amount=".db_escape($full_amount).","
                    . "nopay_deduct=".db_escape($nopay_deduct).","
                    . "comments=".db_escape($comments).","
                    . "inactive=".db_escape($inactive).","
                    . "updated_by=".db_escape($updated_by).","
                    . "updated_on=".db_escape($updated_on)
                . "WHERE "
                    . "id=".db_escape($selected_id)." AND "
                    . "type=".db_escape($type)." AND "
                    . "employee_code=".db_escape($employee_code)." AND "
                    . "salary_component_id=".db_escape($salary_component_id);
	
	db_query($sql,"Salary component could not be updated");		
}

function get_salary_components($payroll_id, $employee_code, $salary_components_types = null)
{
	$sql = "SELECT sc.*, 
                    scn.value_name salary_component_name,
                    ct.value_name calculation_type_name,
                    IFNULL((SELECT stn.value_name 
                            FROM ".TB_PREF."list_values stn 
                            WHERE stn.value_code = sc.salary_total_code AND 
                                    stn.list_code = 'SALARY_TOTALS' ),'') salary_total_name,  
                    IFNULL((SELECT tn.value_name 
                            FROM ".TB_PREF."list_values tn 
                            WHERE tn.id = sc.table_id),'') table_name  
            FROM "
                    .TB_PREF."salary_components  sc,"
                    .TB_PREF."list_values  scn,"
                    .TB_PREF."list_values  ct  
            WHERE 
                    sc.salary_component_id = scn.id";
        
            if($salary_components_types != null){
                        $sql .= " AND employee_code=".db_escape($employee_code);
            }
            
            if($payroll_id > 0){
                $sql .= " AND payroll_id=".db_escape($payroll_id);
            }
     
            $sql .= " AND type IN(".$salary_components_types.") AND 
                        (sc.calculation_type = ct.value_code AND ct.list_code = 'SALARY_CAL_TYPES')  
                        ORDER BY effective_date DESC  ";
                
	return db_query($sql,"Salary components could not be retreived");
}


function get_existing_salary_components($payroll_id, $employee_code, $salary_components_type, $salary_component_id)
{
	$sql = "SELECT sc.*
            FROM "
                    .TB_PREF."salary_components  sc  
            WHERE 
                    sc.salary_component_id = ".db_escape($salary_component_id);  
        
            if($payroll_id > 0){
                $sql .= " AND payroll_id=".db_escape($payroll_id);
            }
            else{
                $sql .= " AND employee_code=".db_escape($employee_code);
            }
     
            $sql .= " AND type = ".db_escape($salary_components_type)."  
                             
            ORDER BY effective_date  ";
                
	return db_query($sql,"Salary components could not be retreived");
}


function get_salary_component_detail($selected_id)
{
	$sql = "SELECT *    
		FROM "
                    .TB_PREF."salary_components  
                WHERE 
                    id = ".db_escape($selected_id);	
	
	$result = db_query($sql,"Salary component detail could not be retreived");
        return db_fetch($result);
}

function delete_salary_component($selected_id)
{
	$sql = "DELETE FROM ".TB_PREF."salary_components  
                WHERE id = ".db_escape($selected_id);	
	
	db_query($sql,"Salary component could not be deleted");
}

?>