<?php

function fi_goods_received_notes($batch_no, $cutoff_date_, $timeout_at, $batch_size=1){
    global $systypes_array;
    $cutoff_date = date2sql($cutoff_date_);
    $tran_type = ST_SUPPRECEIVE;
    
    
    $clearing_act = get_company_pref('grn_clearing_act');
    if(!(strlen($clearing_act) > 2)){
            $message = $systypes_array[$tran_type]." - could not find grn clearing 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_goods_received_notes($cutoff_date, $batch_size);
    while($h = db_fetch($result_mr)){        
        $total = 0;
        
        if($timeout_at < time()){
            return array("success"=>false, "message"=>get_fi_integration_timeout_message());
        }
        
        $trans_no = $h["trans_no"];
        $tran_date = $h['tran_date'];
        $tran_date_ = sql2date($tran_date);
        $supplier_id = $h["supplier_id"];
        
        $cgfit_msg = can_generate_fi_transaction($tran_type, $trans_no, $tran_date);
        if($cgfit_msg['success'] !== true){ 
            return $cgfit_msg;
        }
        
        $result_mrd = get_fi_goods_received_note_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)){
            //Debit     $amount1 + $amount2 + $amount3 + ...     inventory_account:
            //Credit    $total       clearing_act
            
            $detail_line_id = $d['detail_line_id'];
            if($d['unit_cost'] == 0 || $d['qty_recd'] == 0){
                continue;
            }

            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_recd'];
            if($amount !== 0){
                $total += add_fi_trans_supplier($batch_no, $tran_type, $trans_no, 0, $tran_date_, $inventory_account, 0, 0, 
                                                $amount, $supplier_id, null, 0, $comment);
            }
        }
        
        add_fi_trans_supplier($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $clearing_act, 0, 0, 
                                        -$total, $supplier_id, null, 0);
        
        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]." - 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"=>"");
    
}

?>