<?php

function fi_material_returns($batch_no, $cutoff_date_, $timeout_at, $batch_size=1){
    global $systypes_array;
    $cutoff_date = date2sql($cutoff_date_);
    $tran_type = ST_MATERIAL_RETURN;
    
    $default_coc_act = get_company_pref('default_coc_act');
    if(!(strlen($default_coc_act) > 2)){
            $message = $systypes_array[$tran_type]." - could not find default coc account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
    
    $default_cost_act = get_company_pref('default_cost_act');
    if(!(strlen($default_cost_act) > 2)){
            $message = $systypes_array[$tran_type]." - could not find default cost account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
    
    $def_inventory_account = get_company_pref('default_inventory_act');
    if(!(strlen($def_inventory_account) > 2)){
            $message = $systypes_array[$tran_type]." - could not find default inventory account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
    
    $result_mr = get_fi_material_returns($cutoff_date, $batch_size);
    while($h = db_fetch($result_mr)){
        
        if($timeout_at < time()){
            return array("success"=>false, "message"=>get_fi_integration_timeout_message());
        }
        
        $trans_no = $h["trans_no"];
        $tran_date = $h['tran_date'];        
        $project_type_id = $h['project_type_id'];
        $project_id = $h['project_id'];
        $tran_date_ = sql2date($tran_date);
        
        $cgfit_msg = can_generate_fi_transaction($tran_type, $trans_no, $tran_date);
        if($cgfit_msg['success'] !== true){ 
            return $cgfit_msg;
        }
        
        $result_mrd = get_fi_material_return_details($trans_no);
        begin_global_transaction();
        
        if(!(db_num_rows($result_mrd) > 0)){
            write_fi_log($batch_no, $systypes_array[$tran_type]." #$trans_no - No Details found.");
        }
        $error = 0;
                
        while($d = db_fetch($result_mrd)){
            $detail_line_id = $d['detail_line_id'];
            if($d['unit_cost'] == 0 || $d['qty_returned'] == 0){
                continue;
            }

            if(strlen($d['cost_account']) > 2){
                $cost_account = $d['cost_account'];
            }
            else{
                $cost_account = $default_cost_act;
            }

            if(strlen($d['coc_account']) > 2){
                $coc_account = $d['coc_account'];
            }
            else{
                $coc_account = $default_coc_act;
            }

            set_fi_applicable_expense_account($project_type_id, $cost_account, $coc_account);

            if(strlen($d['inventory_account']) > 2){
                $inventory_account = $d['inventory_account'];
            }
            else{
                $inventory_account = $def_inventory_account;
            }

            $comment = "Line Id $detail_line_id : ".$d['stock_id']." - ".$d['description'];

            $amount = $d['unit_cost'] * $d['qty_returned'];
            if($amount !== 0){
		add_fi_trans_std_cost($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $cost_account, $project_id, 0, $comment, -$amount);
		add_fi_trans_std_cost($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $inventory_account, $project_id, 0, $comment, $amount);
            }
        }
        
        if($error > 0){ // if fails inner loop then continue with outer loop
            cancel_transaction();
            continue;
        }

        $n = update_source_transaction_fi_batch_no($tran_type, $trans_no, $batch_no);
        if($n !== 1){
            cancel_transaction();
            $message = $systypes_array[$tran_type]." #".$trans_no." has been processed already";
            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"=>"");
    
}

?>