<?php


function get_employee_allawance_usage_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;
    }
    
    //-----------------------------
    
    $allawance_spent_value = get_approved_employee_allawance_usage_value($payroll_run['id'], $employee_code, $process_code);

    $salary_component_result = get_salary_component_calculation_result($employee_code, $payroll_run['id'], $my_row, $payroll_days_factor, "S", $allawance_usage_value);
    $salary_component_value = $salary_component_result[0];
    
        
    if($my_row['calculation_type'] == "V" && $my_row['full_amount']){
        $salary_component_value_full = $my_row['amount'];
    }
    else{
        $salary_component_value_full = $salary_component_value;
    }
    
    $allawance_over_used_value_full = $allawance_spent_value - $salary_component_value_full;
    $allawance_over_used_value = $allawance_spent_value - $salary_component_value;
    
    $comment = $salary_component_result[1];
        
    if($allawance_over_used_value_full < 0){
        $allawance_over_used_value_full = 0;
    } 
    if($allawance_over_used_value < 0){
        $allawance_over_used_value = 0;
    } 
    
    if($my_row['deduction'] == 1){
        $allawance_over_used_value_full = -$allawance_over_used_value_full;
        $allawance_over_used_value = -$allawance_over_used_value;
    }

    if($my_row['calculation_type'] == "V" && $my_row['full_amount']){
        $salary_value = $allawance_over_used_value_full;
    }
    else{
        $salary_value = $allawance_over_used_value;
    }
    
    insert_payroll_run_detail($payroll_run['id'], $employee_code, $process_code, $salary_value, $comment);
    
    return array("calculated_value" => $allawance_over_used_value, "full_value" => $allawance_over_used_value_full);
    
}

function get_approved_employee_allawance_usage_value($payroll_run_id, $employee_code, $process_code)
{ 
    $allawance_usage = select_approved_employee_allawance_usage_records($payroll_run_id, $employee_code, $process_code);
    if(!$allawance_usage){
        $allawance_usage_value = 0;
    }
    else{
        $allawance_usage_value = $allawance_usage['spent_amount'];
        update_approved_employee_allawance_usage_record_status($allawance_usage['id'], "L", $payroll_run_id);
    }
    
    return $allawance_usage_value;
}



?>
