<?php

function fi_stock_item_cost_update($batch_no, $cutoff_date_, $tran_type, $timeout_at, $batch_size=1){
    global $systypes_array;
    
    $cutoff_date = date2sql($cutoff_date_);
    
    $def_adjustment_account = get_company_pref('default_adj_act');
    if(!(strlen($def_adjustment_account) > 2)){
            $message = $systypes_array[$tran_type]." - could not find default adjustments 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);
    }
    
    begin_global_transaction();

    $result_d = get_fi_stock_item_cost_updates($cutoff_date, $batch_size);

    $error = 0;

    while($d = db_fetch($result_d)){
        
        if($timeout_at < time()){
            return array("success"=>false, "message"=>get_fi_integration_timeout_message());
        }
            
        $trans_no = $d['trans_no'];

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

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

        $total_quantity = $d['total_quantity'];
        $previous_material_cost = $d['previous_material_cost'];
        $new_material_cost = $d['new_material_cost'];

        $comment = $d['stock_id']." - ".$d['description'];
        $amount = $total_quantity * ($new_material_cost - $previous_material_cost);
        
        $tran_date = $d['trans_date'];
        $tran_date_ = sql2date($tran_date);   
        
        $detail_line_id = 0;
        
        
        $cgfit_msg = can_generate_fi_transaction($tran_type, $trans_no, $tran_date);
        if($cgfit_msg['success'] !== true){ 
            return $cgfit_msg;
        }
        
        
        if(abs($amount) > FLOAT_COMP_DELTA){
            add_fi_trans_std_cost($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $adjustment_account, 0, 0, $comment, -$amount);
            add_fi_trans_std_cost($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $inventory_account, 0, 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, $d['trans_date']);

        commit_transaction();
        
    }
    
    return array("success"=>true, "message"=>"");
}

?>