<?php

function fi_company_loan_repayments($batch_no, $cutoff_date_, $timeout_at, $batch_size=1){
    global $systypes_array;
    $cutoff_date = date2sql($cutoff_date_);
    $tran_type = ST_COMP_LOAN_REPAYMENT;
    
    $default_loans_act = get_company_pref('default_fn_loans_act');
    if(!(strlen($default_loans_act) > 2)){
            $message = $systypes_array[$tran_type]." - could not find default loans account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
    
    $default_loan_interests_act = get_company_pref('default_fn_loan_interests_act');
    if(!(strlen($default_loan_interests_act) > 2)){
            $message = $systypes_array[$tran_type]." - could not find default loan interests account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
    
    $default_loan_late_interests_act = get_company_pref('default_fn_loan_late_interests_act');
    if(!(strlen($default_loan_late_interests_act) > 2)){
            $message = $systypes_array[$tran_type]." - could not find default loan late payment interests account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
    
    
//Capital       - debit loan liability    |   credit bank account
//Interest      - debit loan interest    |   credit bank account
//Late Interest - debit loan late interest    |   credit bank account

    
    $result_h = get_fi_company_loan_repayments($cutoff_date, $batch_size);
    while($h = db_fetch($result_h)){
        
        if($timeout_at < time()){
            return array("success"=>false, "message"=>get_fi_integration_timeout_message());
        }
        
        $trans_no = $h['id'];
        $tran_date = $h['tran_date'];   
        $tran_date_ = sql2date($tran_date);   
        
        $cgfit_msg = can_generate_fi_transaction($tran_type, $trans_no, $tran_date);
        if($cgfit_msg['success'] !== true){ 
            return $cgfit_msg;
        }
        

        $bank_gl_account = $h['account_code'];


        begin_global_transaction();
        
        if($h['payment_amount'] != 0){
            $amount = $h['payment_amount'];
            $payment_type = "Capital";
            
            if(strlen($h['loan_account']) > 2){
                $debit_account = $h['loan_account'];
            }
            else{
                $debit_account = $default_loans_act;
            }
        }
        elseif($h['payment_interest_amount'] != 0){
            $amount = $h['payment_interest_amount'];
            $payment_type = "Interest";
            
            if(strlen($h['interest_account']) > 2){
                $debit_account = $h['interest_account'];
            }
            else{
                $debit_account = $default_loan_interests_act;
            }
        }
        elseif($h['late_interest_amount'] != 0){
            $amount = $h['late_interest_amount'];
            $payment_type = "Late Interest";
            
            if(strlen($h['late_interest_account']) > 2){
                $debit_account = $h['late_interest_account'];
            }
            else{
                $debit_account = $default_loan_late_interests_act;
            }
        }

        $detail_line_id = 0;
        if($h['payment_amount'] == 0 && $h['payment_interest_amount'] == 0 && $h['late_interest_amount'] == 0){
            continue;
        }

        $comment = "";

        add_fi_trans($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $bank_gl_account, 0, 0, $comment, -$amount);
        add_fi_trans($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $debit_account, 0, 0, $comment, $amount);
        
        
        $n = update_source_transaction_fi_batch_no($tran_type, $trans_no, $batch_no);
        if($n !== 1){
            cancel_transaction();
            $message = $systypes_array[$tran_type]." - Could not process #$trans_no.";
            write_fi_log($batch_no, $message);
            continue;
        }
        
        add_fi_batch_transaction($batch_no, $tran_type, $trans_no, $tran_date);
        
        commit_transaction();
    }
    
    return array("success"=>true, "message"=>"");
    
}

?>