<?php


function get_rounded_off_salary($process_code, $employee_code, $payroll_run)
{

    $comment = '';
    
    $payroll_settings = select_payroll_settings($payroll_run['payroll_id']);
    
    $BF_amount = get_calculated_payroll_db_value($payroll_settings['carried_forward_value_code'], $employee_code, $payroll_run['linked_payroll_run_id']);
    //$BF_amount = select_brought_forward_amount($payroll_settings['linked_payroll_run_id'], $payroll_settings['carried_forward_value_code']);
    
    $rounding_off_reference_value = get_calculated_payroll_value($payroll_settings['rounding_off_reference_code'], $employee_code, $payroll_run['id']);
    
    $rounding_off_to_amount = $payroll_settings['rounding_off_to_amount'];
    
    $total_wih_BF = $rounding_off_reference_value + $BF_amount;
    
    if($rounding_off_to_amount > 0){
        $rounded_down_total = floor($total_wih_BF/$rounding_off_to_amount) * $rounding_off_to_amount;
    }
    else{
        $rounded_down_total = $total_wih_BF;
    }
    
    $carried_forward_amount = $total_wih_BF - $rounded_down_total;
    
    insert_payroll_run_detail($payroll_run['id'], $employee_code, $payroll_settings['brought_forward_value_code'], $BF_amount, $comment);
    insert_payroll_run_detail($payroll_run['id'], $employee_code, $payroll_settings['rounded_off_salary_code'], $rounded_down_total, $comment);
    insert_payroll_run_detail($payroll_run['id'], $employee_code, $payroll_settings['carried_forward_value_code'], $carried_forward_amount, $comment);
    
    return array("calculated_value" => $rounded_down_total, "full_value" => $rounded_down_total);
    
}


?>
