<?php

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

    global $systypes_array;
    $cutoff_date = date2sql($cutoff_date_);
    $tran_type = ST_PETTY_CASH_FLOAT_REIMBUR;
    
    $default_cash_act = get_company_pref('default_cash_act');
    if(!(strlen($default_cash_act) > 2)){
            $message = $systypes_array[$tran_type]." - could not find default cash account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
    
    $default_float_act = get_company_pref('default_pc_float_act');
    if(!(strlen($default_float_act) > 2)){
            $message = $systypes_array[$tran_type]." - could not find default float account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
    
//    $default_expenses_act = get_company_pref('default_pc_expenses_act');
//    if(!(strlen($default_expenses_act) > 2)){
//            $message = $systypes_array[$tran_type]." - could not find default expenses account";
//            write_fi_log($batch_no, $message);
//            return;
//    }
    
    
    $result_h = get_fi_petty_cash_float_reimbursements($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'];
        
        $cgfit_msg = can_generate_fi_transaction($tran_type, $trans_no, $tran_date);
        if($cgfit_msg['success'] !== true){ 
            return $cgfit_msg;
        }
        
        begin_global_transaction();
        
        if($h['from_float_id'] > 0){
            fi_float_reimbursements_from_float($batch_no, $tran_type, $h, $default_float_act);
        }
        else{
            fi_float_reimbursements_from_bank_account($batch_no, $tran_type, $h, $default_float_act);
        }
        
        $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"=>"");
    
}


function fi_float_reimbursements_from_float($batch_no, $tran_type, $h, $default_float_account)
{
        $trans_no = $h['id'];
        $tran_date = sql2date($h['tran_date']);   
        
        if(strlen($h['float_account']) > 2){
            $float_account = $h['float_account'];
        }
        else{
            $float_account = $default_float_account;
        }
        
        if(strlen($h['from_float_account']) > 2){
            $from_float_account = $h['from_float_account'];
        }
        else{
            $from_float_account = $default_float_account;
        }

        $detail_line_id = $h['float_trans_id'];
        
        if($h['amount'] == 0){
            return;
        }

        $comment = "";
        $amount = $h['amount'];
        if($amount !== 0){
            add_fi_trans($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date, $from_float_account, 0, 0, $comment, -$amount);
            add_fi_trans($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date, $float_account, 0, 0, $comment, $amount);
        }
        
        
}

function fi_float_reimbursements_from_bank_account($batch_no, $tran_type, $h, $default_float_account)
{
    global $systypes_array;
    
    $trans_no = $h['id'];
        $tran_date = sql2date($h['tran_date']);   
        
        if(strlen($h['float_account']) > 2){
            $float_account = $h['float_account'];
        }
        else{
            $float_account = $default_float_account;
        }
        
        $bank_account = get_bank_account($h['bank_account_id']);
        $bank_account_gl_code = $bank_account['account_code'];

        $detail_line_id = $h['float_trans_id'];
        
        if($h['amount'] == 0){
            return;
        }

        $returned_total = 0;
        $comment = "";
        $amount = $h['amount'];
        if($amount !== 0){
            $bank_payment_id = $h['bank_payment_id'];
            if($bank_payment_id > 0){// Reimbursement has been done for a previously created Bank Payment
                $bank_payment = get_bank_trans_by_id($bank_payment_id); 
                $journal_res = get_journal_details_records($bank_payment['type'], $bank_payment['trans_no']);
                while ($journal_d = db_fetch($journal_res)) {
                    $jd_amount = $journal_d['amount'];
                    $jd_gl_code = $journal_d['account'];
                    $jd_detail_line_id = $journal_d['counter'];
                    if($bank_account_gl_code == $jd_gl_code){
                        continue;// Not revesing the bank account GL entry
                    }
                    if($float_account == $jd_gl_code){
                        continue;// Not revesing the float account GL entry
                    }
                    $jd_comment = "Reversing entries of ".$systypes_array[$bank_payment['type']]." #".$bank_payment['trans_no'];
                    add_fi_trans($batch_no, $tran_type, $trans_no, $jd_detail_line_id, $tran_date, $jd_gl_code, 0, 0, $jd_comment, -$jd_amount);
                    
                    $returned_total += $jd_amount;
                }
                
                if($returned_total != 0){
                    add_fi_trans($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date, $float_account, 0, 0, $comment, $returned_total);
                }
                
            }else{// Reimbursing with a new Bank Payment
                // No FI GL entries here. Those laready created when creating the reimbursement
            }
            
        }
        
}
