<?php

function get_loan_installments_total($process_code, $employee_code, $payroll_run)
{
    if($employee_code == '0169'){
        $ee =5;
    }
    remove_payroll_employee_loan_installments($employee_code, $payroll_run['id']);
    $loan_installment_total = 0;
    $interest_amount = 0;
    $comment = "";
    
    $result = select_active_loans($employee_code, $payroll_run);
    while ($myrow = db_fetch($result)){
        $loan_installment = calculate_loan_installment($myrow['id'], $employee_code, $payroll_run);
        if($loan_installment > 0){
            delete_previously_calculated_loan_installments($payroll_run['id'], $myrow['id']);
            add_loan_installment($myrow['id'],$loan_installment, $interest_amount, $payroll_run['id'], "", "L");
            $comment .= $myrow['loan_description']." (".number_format($myrow['loan_amount'],2).") - ".number_format($loan_installment,2)."\r\n";
            $loan_installment_total += $loan_installment;
        }
    }
    
    $loan_installment_total = - $loan_installment_total;
    
    if($loan_installment_total < 0){
        insert_payroll_run_detail($payroll_run['id'], $employee_code, $process_code, $loan_installment_total, $comment);
    }
    
    return array("calculated_value" => $loan_installment_total, "full_value" => $loan_installment_total);
    
}


function remove_payroll_employee_loan_installments($employee_code, $payroll_run_id)
{
    $result = select_employee_loans($employee_code);
    while ($myrow = db_fetch($result)){
        delete_payroll_loan_installments($myrow['id'], $payroll_run_id);
    }
}


function calculate_loan_installment($loan_id, $employee_code, $payroll_run)
{
    $loan_installment = 0;
    
    $myrow = select_loan($loan_id);
    if($myrow == null){
        $loan_installment = 0;
    }
    else{
        $outstanding_amount = $myrow['loan_amount'] - $myrow['total_paid'];
        
        $payroll_period_start_date = date_create($payroll_run['period_start_date']);
        $payroll_period_end_date = date_create($payroll_run['period_end_date']);
        
        $loan_start_date = date_create($myrow['start_date']);
        
        $loan_record_installment_amount = (double)$myrow['installment_amount'];
        
        $payroll_days_s = date_diff($payroll_period_end_date, $payroll_period_start_date);
        $payroll_days = $payroll_days_s->format('%a');
        
        $payroll_days += 1;
        
        if($payroll_days >= 28){
            $payroll_period_installment_amount = $loan_record_installment_amount;
        }
        else{
//            $payroll_period_installment_amount = $loan_record_installment_amount * $payroll_days/30;
            $payroll_period_installment_amount = $loan_record_installment_amount;
        }
        
        if($myrow['start_date'] <= $payroll_run['period_start_date']){    
            if($outstanding_amount >= $payroll_period_installment_amount){
                $loan_installment = $payroll_period_installment_amount;
            }
            else{
                $loan_installment = $outstanding_amount;
            }
        }
        elseif($myrow['start_date'] <= $payroll_run['period_end_date']){ 
            $loan_days_s = date_diff($payroll_period_end_date, $loan_start_date);
            $loan_days = $loan_days_s->format('%a');
            
            $loan_days += 1;
            
            if($payroll_days == $loan_days){
                $loan_installment = $payroll_period_installment_amount;
            }
            else{
                $loan_installment = $payroll_period_installment_amount;
//                $loan_installment = $payroll_period_installment_amount * $loan_days/$payroll_days;
            }
        }
    }
    
    //update_loan_record_status($loan_id, $employee_code, "L", $payroll_run['id'], $loan_installment);
    
    return $loan_installment;
    
}

?>
