<?php

function fi_site_material_returns($batch_no, $cutoff_date_, $timeout_at, $batch_size=1){
    global $systypes_array;
    $cutoff_date = date2sql($cutoff_date_);
    $tran_type = ST_SITE_MATERIAL_RETURN;
    
    $default_cogs_act = get_company_pref('default_cogs_act');
    if(!(strlen($default_cogs_act) > 2)){
            $message = $systypes_array[$tran_type]." - could not find default cogs 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_site_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"];
        $from_project_id = $h["from_project_id"];
        $to_project_id = $h["to_project_id"];
        $tran_date = $h['tran_date'];
        $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_site_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['standard_cost'] == 0 || $d['qty'] == 0){
                continue;
            }

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

            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['standard_cost'] * $d['qty'];
            if($amount !== 0){
		add_fi_trans_std_cost($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $cogs_account, $from_project_id, 0, $comment, -$amount);
		add_fi_trans_std_cost($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $inventory_account, $to_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"=>"");
}

?>