<?php


function get_performance_incentive_value($record_type, $process_code, $employee, $payroll_run, $nopay_days, $nopay_reference_days_factor = 30)
{ 
    $employee_code = $employee['employee_code'];
    $payroll_days_factor = 1;
    
    $my_row = select_salary_component($record_type, $process_code, $employee_code, $payroll_run);
    if($my_row == null){
        return 0;
    }
 
    //---------------------------
    $applicable_start_date = $applicable_end_date = '';
    $applicable_days = get_payroll_calculation_applicable_values($payroll_run, $employee, 
                                                $my_row, $nopay_days, 
                                                $applicable_start_date, $applicable_end_date, $payroll_days_factor,
                                                $nopay_reference_days_factor);
    if($applicable_days == 0){
        return 0;
    }
    //-----------------------------
 
    $performance_incentive_value = get_approved_performance_incentive_value($payroll_run['id'], $employee_code, $my_row['process_code']);
    
    if($record_type == SCT_PERFORMANCE_RATE_INCENTIVES){    
        // Here the rate is calculated. So ignore applicable date period.
        $payroll_days_factor = 1;
        $salary_component_result = get_salary_component_calculation_result($employee_code, $payroll_run['id'], $my_row, 
                                                                            $payroll_days_factor, "R", $performance_incentive_value,
                                                                            true);
        if($my_row['calculation_type'] == "T"){
            // For table type calculations use standard way of rate calculation in the table for accumulated values 
            $performance_incentive_amount = $salary_component_result[0];
        }
        else{
            // For all other types consider the value defined in the salary component as rate 
            $performance_incentive_amount = $performance_incentive_value * $salary_component_result[0];
        }
    
        $comment = $salary_component_result[1];
        
        if($performance_incentive_amount < 0){
            $performance_incentive_amount = 0;
        }
        
        $performance_incentive_amount_full = $salary_value = $performance_incentive_amount;
        
    }
    elseif($record_type == SCT_PERFORMANCE_VALUE_INCENTIVES){
        $salary_component_result = get_salary_component_calculation_result($employee_code, $payroll_run['id'], $my_row, 
                                                                            $payroll_days_factor, "V", $performance_incentive_value);
        $performance_incentive_amount = $salary_component_result[0];
        $comment = $salary_component_result[1];
        
        if($my_row['calculation_type'] == "V" && $my_row['full_amount']){
            $performance_incentive_amount_full = $my_row['amount'];
        }
        else{
            $performance_incentive_amount_full = $performance_incentive_amount;
        }
        
        if($performance_incentive_amount_full < 0){
            $performance_incentive_amount_full = 0;
        }
        if($performance_incentive_amount < 0){
            $performance_incentive_amount = 0;
        }
   
    }
    else{
        $performance_incentive_amount = 0;
        $performance_incentive_amount_full = 0;
        $salary_value = 0;
    }
    
    if($my_row['calculation_type'] == "V" && $my_row['full_amount']){
        $salary_value = $performance_incentive_amount_full;
    }
    else{
        $salary_value = $performance_incentive_amount;
    }        
        
    insert_payroll_run_detail($payroll_run['id'], $employee_code, $process_code, $salary_value, $comment);
    
    return array("calculated_value" => $performance_incentive_amount, "full_value" => $performance_incentive_amount_full);
}

function get_approved_performance_incentive_value($payroll_run_id, $employee_code, $process_code)
{ 
    $approved_performance_incentive_value = select_approved_performance_incentive_value($payroll_run_id, $employee_code, $process_code);
    if(!$approved_performance_incentive_value){
        $amount_achieved = 0;
    }
    else{
        $amount_achieved = $approved_performance_incentive_value['amount_achieved'];
        update_calculated_performance_incentive_record_status($approved_performance_incentive_value['id'], "L", $payroll_run_id);
    }
    
    return $amount_achieved;
}





?>
