<?php

function fi_customer_payments($batch_no, $cutoff_date_, $timeout_at, $batch_size=1){
    global $systypes_array;
    $cutoff_date = date2sql($cutoff_date_);
    $trans_type = ST_CUSTPAYMENT;
    
    $default_receivable_act = get_company_pref("debtors_act");
    if(!(strlen($default_receivable_act) > 2)){ 
            $message = $systypes_array[$trans_type]." - could not find default receivable account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
    
    $default_sales_discount_act = get_company_pref("default_sales_discount_act");
    if(!(strlen($default_sales_discount_act) > 2)){ 
            $message = $systypes_array[$trans_type]." - could not find default sales discount account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
    
    $default_prompt_payment_act = get_company_pref("default_prompt_payment_act");
    if(!(strlen($default_prompt_payment_act) > 2)){ 
            $message = $systypes_array[$trans_type]." - could not find default promt payment discount account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
            
    $result_h = get_fi_customer_payments($trans_type, $cutoff_date, $batch_size);
    while($h = db_fetch($result_h)){
            
        if($timeout_at < time()){
            return array("success"=>false, "message"=>get_fi_integration_timeout_message());
        }
        
            $tran_date = $h['tran_date'];
            $tran_date_ = sql2date($tran_date);
            $trans_no = $h['trans_no'];
            $customer_id = $h['debtor_no'];

            $currency_code = $h["curr_code"];
            $rate = $h['rate'];
            
            $cgfit_msg = can_generate_fi_transaction($trans_type, $trans_no, $tran_date);
            if($cgfit_msg['success'] !== true){ 
                return $cgfit_msg;
            }
            
//            $ss = get_company_pref('curr_default');
            if(get_company_pref('curr_default') != $currency_code){
                    $currency_code = null;
            }


            $receivable_account = $h["receivables_account"];
            if(!(strlen($receivable_account) > 2)){
                    $receivable_account = $default_receivable_act;
            } 
        
            $sales_discount_account = $h["sales_discount_account"];
            if(!(strlen($sales_discount_account) > 2)){
                    $sales_discount_account = $default_sales_discount_act;
            } 
        
            $prompt_payment_discount_account = $h["payment_discount_account"];
            if(!(strlen($prompt_payment_discount_account) > 2)){
                    $prompt_payment_discount_account = $default_prompt_payment_act;
            } 
        
            begin_global_transaction();
        
   
            $receivable_amount = $h['ov_amount'];

            if($receivable_amount == 0){
                continue;
            }

            $bank_trans = get_fi_customer_payments_bank_transaction($trans_type, $trans_no);
            
            $banl_gl_account = $bank_trans['account_code'];

            $sales_revenue = add_fi_trans_customer($batch_no, $trans_type, $trans_no, 0, $tran_date_, $banl_gl_account, 0, 0, 
                                                    $receivable_amount, $customer_id, $currency_code, $rate);
            
            $customer_receivable = add_fi_trans_customer($batch_no, $trans_type, $trans_no, 0, $tran_date_, $receivable_account, 0, 0, 
                                                    -$receivable_amount, $customer_id, $currency_code, $rate);
            
            
            $n = update_source_transaction_fi_batch_no($trans_type, $trans_no, $batch_no);
            if($n !== 1){
                cancel_transaction();
                $message = $systypes_array[$trans_type]." #".$trans_no." has been processed already";
                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"=>"");
    
}
    


?>