<?php

function fi_supplier_transport_invoices($batch_no, $cutoff_date_, $timeout_at, $batch_size=1){

    global $systypes_array;
    $cutoff_date = date2sql($cutoff_date_);
    $trans_type = ST_SUPPLIER_TRANSPORT_INVOICES;
    
    $def_transport_expenses_account = get_company_pref('transport_exp_act');
    if(!(strlen($def_transport_expenses_account) > 2)){
            $message = $systypes_array[$trans_type]." - could not find default trasport expenses account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
    
    $def_payable_account = get_company_pref('creditors_act');
    if(!(strlen($def_payable_account) > 2)){
            $message = $systypes_array[$trans_type]." - Default payable account not found";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
    
    
    $result_h = get_fi_supplier_transport_invoices($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($h['tran_date']);
            $trans_no = $h['trans_no'];
            $supplier_id = $h['supplier_id'];
            $project_id = $h['project_id'];

            $currency_code = $h["curr_code"];
            if(get_company_pref('curr_default') == $currency_code){
                $currency_code = null;
            }
            
            $cgfit_msg = can_generate_fi_transaction($trans_type, $trans_no, $tran_date);
            if($cgfit_msg['success'] !== true){ 
                return $cgfit_msg;
            }
            
            $rate = $h["rate"];
            
        
//Inventry/expence	Debit 	100		COGS Account:
//VAT receivale 	Debit 	8		
//Supplier              Credit	108		Accounts payble Acc
        

            $payable_account = $h["payable_account"];
            if(!(strlen($payable_account) > 2)){
                $payable_account = $def_payable_account;
            }

            
//            if(!($h['total'] > 0)){
//                $message = $systypes_array[$trans_type]." #$trans_no - Total is negative.";
//                write_fi_log($batch_no, $message);
//                continue;
//            }
        
        
            begin_global_transaction();

            // Taxes
            $taxes_result = get_fi_supplier_transport_invoices_taxes($trans_type, $trans_no);    
            $taxes_total = add_fi_supplier_transport_invoice_taxes($batch_no, $trans_type, $supplier_id, $tran_date_, $taxes_result, $currency_code, $rate, $project_id);        

            // Supplier payable
            

            if($h['tax_included']){
                $invoice_taxed_total = $h['ov_amount'];
                $invoice_tax_free_total = $h['ov_amount'] - ($taxes_total/$rate);
                
                $tax_factor = ($invoice_tax_free_total ) / $invoice_taxed_total;
            }else{
                $invoice_taxed_total = $h['ov_amount'] + ($taxes_total/$rate);
                $invoice_tax_free_total = $h['ov_amount'];
                
                $tax_factor = 1;
            }
        
            $supplier_payable = add_fi_trans_supplier($batch_no, $trans_type, $trans_no, 0, $tran_date_, $payable_account, $project_id, 0, 
                                                        -$invoice_taxed_total, $supplier_id, $currency_code, $rate);

            $expenses_total = add_fi_trans_supplier($batch_no, $trans_type, $trans_no, 0, $tran_date_, $def_transport_expenses_account, $project_id, 0, 
                                                        $invoice_tax_free_total, $supplier_id, $currency_code, $rate);


            $balance = $expenses_total + $taxes_total + $supplier_payable;
            if($balance != 0){
                $m = "Non zero balance - ".$systypes_array[$trans_type]." #".$trans_no;
                add_fi_balance($batch_no, $trans_type, $trans_no, $tran_date_, -$balance, $currency_code, PT_SUPPLIER, $supplier_id, $m);
            }

            $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"=>"");
    
}
    
function add_fi_supplier_transport_invoice_taxes($batch_no, $trans_type, $supplier_id, $date_, $ub_taxes_result, $currency_code, $rate, $project_id){
    $total = 0;
    while($taxitem = db_fetch($ub_taxes_result)){
        if($taxitem['amount'] != 0){
            $total += add_fi_trans_supplier($batch_no, $trans_type, $taxitem['trans_no'], $taxitem['id'], $date_, $taxitem['purchasing_gl_code'], $project_id, 0, 
                                            $taxitem['amount'], $supplier_id, $currency_code, $rate);
            if($taxitem['suspended'] == 1){
                $total += add_fi_trans_supplier($batch_no, $trans_type, $taxitem['trans_no'], $taxitem['id'], $date_, $taxitem['sales_gl_code'], $project_id, 0, 
                                            (-$taxitem['amount']), $supplier_id, $currency_code);
            }
        }
    }
    return $total;
}

