<?php

function get_nopay_deductions_total(&$salary, $process_code, $employee_code, $payroll_run)
{
    $nopay_rate = 0;
    $nopay_deductions_total = 0;
    $comment = '';
    
    $employment_type = get_applicable_employment_type($employee_code, $payroll_run['period_start_date'], $payroll_run['period_end_date']);
    if(!$employment_type){
        $comment = "No applicable employment type found for this employee\r\n";
    }
    else{
        $nopay_reference = select_nopay_refercnce_details($payroll_run['payroll_id'], $employment_type['employment_type_id']);
        if(!$nopay_reference){
            $comment = "No nopay reference defined for employment type id [".$employment_type['employment_type_id']."] for this payroll\r\n";
        }
        else{
            $list_value = get_list_value($nopay_reference['nopay_reference_value_id']);

            $nopay_reference_code = $list_value['value_code'];

            $nopay_reference_value = get_calculated_payroll_value($salary, $nopay_reference_code, $employee_code, $payroll_run['id'], true);
            $nopay_rate = $nopay_reference_value / $nopay_reference['days_factor'];
        }
    }
    
    $result = select_nopay_leaves($employee_code, $payroll_run['period_end_date']);
    while ($myrow = db_fetch($result)){
        $nopay_leave_deduction = get_nopay_leave_deduction($myrow['id'], $process_code, $employee_code, $payroll_run, $nopay_rate);
        $nopay_deductions_total += $nopay_leave_deduction;
        $comment .= sql2date($myrow["from_date"])." - ".sql2date($myrow["to_date"])." - ".$myrow["nopay_days"]." - ".number_format($nopay_leave_deduction,2)."\r\n";
    }
    
    $nopay_deductions_total *= -1; 
    
    if(db_num_rows($result) > 0){
        insert_payroll_run_detail($payroll_run['id'], $employee_code, $process_code, $nopay_deductions_total, $comment);
    }
    
    return array("calculated_value" => $nopay_deductions_total, "full_value" => $nopay_deductions_total);
    
}

function get_nopay_leave_deduction($leave_id, $process_code, $employee_code, $payroll_run, $nopay_rate)
{
    $nopay_leave_deduction = 0;
    
    $myrow = select_nopay_leave_record($leave_id);
    if($myrow == null){
        $nopay_leave_deduction = 0;
    }
    else{
        $nopay_days = $myrow['nopay_days'];

        $nopay_leave_deduction = $nopay_days * $nopay_rate;

    }
    
    update_nopay_leave_record_status($leave_id, $employee_code, "L", $payroll_run['id'], $nopay_leave_deduction);
    
    return $nopay_leave_deduction;
}

?>
