<?php

function fi_supplier_payments($batch_no, $cutoff_date_, $timeout_at, $batch_size=1){
    global $systypes_array;
    
    $cutoff_date = date2sql($cutoff_date_);
    $trans_type = ST_SUPPAYMENT;
   
    return array("success"=>false, "message"=>$systypes_array[$trans_type]." : GL entries created when creating the transaction");
    
    $default_payable_account = get_company_pref('creditors_act');
    if(!(strlen($default_payable_account) > 2)){
                $message = "Default payable account not found";
                write_fi_log($batch_no, $message);
                return array("success"=>false, "message"=>$message);
    }
    
    $default_payment_discount_account = get_company_pref('pyt_discount_act');
    if(!(strlen($default_payment_discount_account) > 2)){
                $message = "Default payment discounts account not found";
                write_fi_log($batch_no, $message);
                return array("success"=>false, "message"=>$message);
    }
    
    
    $result_h = get_fi_supplier_payments($trans_type, $cutoff_date, $batch_size);
    while($h = db_fetch($result_h)){
//        $supplier_payable = $taxes_total = $project_cost = $expenses_total = 0;
        
        if($timeout_at < time()){
            return array("success"=>false, "message"=>get_fi_integration_timeout_message());
        }
        
        
        $tran_date = $h['tran_date'];
        $tran_date_ = sql2date($h['tran_date']);
        $trans_no = $h['trans_no'];
        $supplier_id = $h['supplier_id'];
        $float_id = $h['float_id'];
        
        $cgfit_msg = can_generate_fi_transaction($tran_type, $trans_no, $tran_date);
        if($cgfit_msg['success'] !== true){ 
            return $cgfit_msg;
        }
        
        $currency_code = $h["curr_code"];
        if(get_company_pref('curr_default') == $currency_code){
            $currency_code = null;
        }
 
        $payable_account = $h["payable_account"];
        if(!(strlen($payable_account) > 2)){
            $payable_account = $default_payable_account;
        }
        
        $payment_discount_account = $h["payment_discount_account"];
        if(!(strlen($payment_discount_account) > 2)){
            $payment_discount_account = $default_payment_discount_account;
        }
        
        $payable_amount = $h['ov_amount'];
        if($payable_amount == 0){
            $message = $systypes_array[$trans_type]." #$trans_no - amount is zero.";
            write_fi_log($batch_no, $message);
            continue;
        }

        
        
        $bank_trans = get_fi_supplier_payments_bank_transaction($trans_type, $trans_no);
        
        $bank_gl_account = $bank_trans['account_code'];
        
        begin_global_transaction();
        
        $expenses_total = add_fi_trans_supplier($batch_no, $trans_type, $trans_no, 0, $tran_date_, $bank_gl_account, 0, 0, 
                                                    $payable_amount, $supplier_id, $currency_code);
        
        $supplier_payable = add_fi_trans_supplier($batch_no, $trans_type, $trans_no, 0, $tran_date_, $payable_account, 0, 0, 
                                                    -$payable_amount, $supplier_id, $currency_code);

        if($float_id > 0){
            
        }
        
        
        $n = update_source_transaction_fi_batch_no($trans_type, $trans_no, $batch_no);
        if($n !== 1){
            cancel_transaction();
            $message = $systypes_array[$trans_type]." - Could not process #$trans_no.";
            write_fi_log($batch_no, $message);
            continue;
        }        
        
        
        add_fi_batch_transaction($batch_no, $trans_type, $trans_no, $tran_date);
    
        commit_transaction();
    }
    
    return array("success"=>true, "message"=>"");
    
}
    


?>