<?php

function fi_i_owe_you_batch_expenses($batch_no, $cutoff_date_, $timeout_at, $batch_size=1){
   global $systypes_array;
    $cutoff_date = date2sql($cutoff_date_);
    $tran_type = ST_I_OWE_YOU_BATCH;
    
    $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 array("success"=>false, "message"=>$message);
    }
    
    $default_emp_pc_advances_act = get_sys_prefs_parameter_value('emp_pc_advances_act');
    if(!(strlen($default_emp_pc_advances_act) > 2)){
            $message = $systypes_array[$tran_type]." - could not find employee petty cash advances account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
    
    $default_coc_account = get_company_pref('default_coc_act');
    if(!(strlen($default_coc_account) > 2)){
            $message = $systypes_array[$tran_type]." - could not find default COC account";
            write_fi_log($batch_no, $message);
            return;
    }
    
    
    $result_h = get_fi_i_owe_you_expense_batches($cutoff_date, $batch_size);
    while($h = db_fetch($result_h)){
        
        if($timeout_at < time()){
            return array("success"=>false, "message"=>get_fi_integration_timeout_message());
        }
        
        $detail_line_id = 0;
        if($h['amount'] == 0){
            continue;
        }
        
        $amount = $h['amount'];
        
        $trans_no = $h['batch_no'];
        $project_id = $h['project_id'];
        $project_type_id = $h['project_type_id'];
        $tran_date = $h['tran_date'];
        $tran_date_ = sql2date($tran_date);
        $float_employee_advances_account = $h['float_employee_advances_account'];
        $project_employee_advances_account = $h['project_employee_advances_account'];
        
        if($project_employee_advances_account > 2){
            $emp_pc_advances_act = $project_employee_advances_account;              
        }else{
            if($float_employee_advances_account > 2){
                $emp_pc_advances_act = $float_employee_advances_account;              
            }else{
                $emp_pc_advances_act = $default_emp_pc_advances_act;
            }
        }        
        
        $cgfit_msg = can_generate_fi_transaction($tran_type, $trans_no, $tran_date);
        if($cgfit_msg['success'] !== true){ 
            return $cgfit_msg;
        }
                
        begin_global_transaction();
        
        $detail_expenses_total = 0;

        $result_d = get_fi_i_owe_you_batch_expense_details($trans_no);
        while($d = db_fetch($result_d)){
//            $tran_date = sql2date($h['tran_date']);   
            
            $detail_expense_account = $default_expenses_act;
            $detail_coc_account = $default_coc_account;
            
            if(strlen($d['debit_account']) > 2){
                $detail_expense_account = $d['debit_account'];              
            }  
            
            if(strlen($d['coc_account']) > 2){
                $detail_coc_account = $d['coc_account'];              
            }  
            
            set_fi_applicable_expense_account($project_type_id, $detail_expense_account, $detail_coc_account);
            
            if(strlen($d['credit_account']) > 2){
                $detail_credit_account =$d['credit_account'];
            }
            

            $detail_line_id = 0;
            $detail_amount = $d['approved_amount'];

            $comment = "";
            
            if(abs($detail_amount) > 0){
                $detail_expenses_total += add_fi_trans($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $detail_expense_account, $project_id, 0, $comment, $detail_amount);
            }
        }
        
                
        $comment = "";
        add_fi_trans($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $emp_pc_advances_act, $project_id, 0, $comment, -$amount);
        
//        $balance = $amount - $detail_expenses_total;
//        
//            add_fi_trans($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $default_expenses_act, 0, 0, $comment, $amount);
//        }
//        if(abs($balance) > 0.00001){
//            $m = "Non zero balance - ".$systypes_array[$tran_type]." #".$trans_no;
//            add_fi_balance($batch_no, $tran_type, $trans_no, $tran_date_, -$balance, null, null, null, $m);
//        }

        $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_bank_account($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;
//        }
//        
//        $bank_account_gl_code = $h['bank_account_gl_code'];
//
//        $detail_line_id = 0;
//        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, $bank_account_gl_code, 0, 0, $comment, -$amount);
//            add_fi_trans($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date, $float_account, 0, 0, $comment, $amount);
//        }
//        
//}
