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


function get_employee_salary_totals($payroll_run_id, $employee_code)
{
	$sql = "SELECT p.id, p.component, p.value 
            FROM ".TB_PREF."payroll_run_details p, 
                    ".TB_PREF."lists l, 
                    ".TB_PREF."list_values lv  
            WHERE p.component = lv.value_code AND 
                    lv.list_code = l.list_code AND 
                    l.list_code = 'SALARY_TOTALS' AND 
                    payroll_run_id = ".db_escape($payroll_run_id)." AND 
                    employee_code = ".db_escape($employee_code);
	
	$result = db_query($sql,"Could not retrieve employee salary totals");	
        
    return $result;		
}



function get_all_salary_components_sum($payroll_run_id, $employee_code)
{
	$sql = "SELECT SUM(p.value)  total  
            FROM ".TB_PREF."payroll_run_details p, 
                    ".TB_PREF."lists l, 
                    ".TB_PREF."list_values lv  
            WHERE p.component = lv.value_code AND 
                    lv.list_code <> l.list_code AND 
                    l.list_code = 'SALARY_TOTALS' AND 
                    payroll_run_id = ".db_escape($payroll_run_id)." AND 
                    employee_code = ".db_escape($employee_code);
	
	$result = db_query($sql,"Could not retrieve employee all salary component sum");	
        
    return db_fetch($result);		
}

function get_employee_salary_components($payroll_run_id, $employee_code, $from_record = 0, $to_record = 0)
{
	$sql = "SELECT id, component, value 
            FROM ".TB_PREF."payroll_run_details 
            WHERE payroll_run_id = ".db_escape($payroll_run_id)." AND 
                    employee_code = ".db_escape($employee_code)." ";
    
    if($from_record > 0){
        $sql .= "AND id > ".$from_record." ";
    }
    
    if($to_record > 0){
        $sql .= "AND id < ".$to_record." ";
    }
	
	$result = db_query($sql,"Could not retrieve employee salary components");	
        
    return $result;		
}


function get_employee_salary_components_sum($payroll_run_id, $employee_code, $from_record = 0, $to_record = 0)
{
	$sql = "SELECT SUM(value) total 
            FROM ".TB_PREF."payroll_run_details 
            WHERE payroll_run_id = ".db_escape($payroll_run_id)." AND 
                    employee_code = ".db_escape($employee_code)." ";
    
    if($from_record > 0){
        $sql .= "AND id > ".$from_record." ";
    }
    
    if($to_record > 0){
        $sql .= "AND id < ".$to_record." ";
    }
	
	$result = db_query($sql,"Could not retrieve employee salary components");	
        
    $sum = db_fetch($result);
    if($sum){
        return $sum['total'];
    }
    else{
        return 0;
    }
}

?>