<?php


function get_worktime_deficit_deduction_value($record_type, $process_code, $employee, $payroll_run)
{
    $employee_code = $employee['employee_code'];
    $payroll_days_factor = 1;
    $applicable_days = 30;
    
    $my_row = select_salary_component($record_type, $process_code, $employee_code, $payroll_run);
    if($my_row == null){
        return 0;
    }
     
    $worktime_deficit_deduction_value = 0;
    
    $total_worktime_deficit_hours = get_total_deducted_worktime_deficit_hours( $payroll_run['id'], $employee_code, $process_code);
    
    if($total_worktime_deficit_hours > 0){  
        
        $salary_component_result = get_salary_component_calculation_result($employee_code, $payroll_run['id'], $my_row, $payroll_days_factor, "R", $total_worktime_deficit_hours);
        $worktime_deficit_deduction_value = $salary_component_result[0];
        if($salary_component_result[1] == ''){
            $comment = $total_worktime_deficit_hours." hours";
        }
        else{
            $comment = $salary_component_result[1];
        }
    } 
    else{
        $worktime_deficit_deduction_value = 0;
        $comment = '';
    }
    
    if($my_row['deduction'] == 1){
        $worktime_deficit_deduction_value = -$worktime_deficit_deduction_value;
    }
    
    insert_payroll_run_detail($payroll_run['id'], $employee_code, $process_code, $worktime_deficit_deduction_value, $comment);
    
    return array("calculated_value" => $worktime_deficit_deduction_value, "full_value" => $worktime_deficit_deduction_value);
    
}


function get_total_deducted_worktime_deficit_hours($payroll_run_id, $employee_code, $process_code)
{ 
    $approved_total_deficit_hours = 0;
    
    $worktime_deficit_summary = select_approved_worktime_deficit_summary($payroll_run_id, $employee_code, $process_code);
    if($worktime_deficit_summary){
        $approved_total_deficit_hours = $worktime_deficit_summary['deducted_time'] / 3600;
        update_calculated_worktime_deficit_summary_record_status($worktime_deficit_summary["id"], "L", $payroll_run_id);
    }
    
    return $approved_total_deficit_hours;
    
}




?>
