<?php

function fi_stock_counts($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);
    }
    
    
    $result_h = get_fi_stock_counts($cutoff_date, $tran_type, $batch_size);
    while($h = db_fetch($result_h)){
            
            if($timeout_at < time()){
                return array("success"=>false, "message"=>get_fi_integration_timeout_message());
            }

            $trans_no = $h['stock_count_no'];
            $tran_date = $h['tran_date'];
            $tran_date_ = sql2date($tran_date);
            $project_id = $h['project_id'];
            
            $cgfit_msg = can_generate_fi_transaction($tran_type, $trans_no, $tran_date);
            if($cgfit_msg['success'] !== true){ 
                return $cgfit_msg;
            }

            
//            $stock_gl_codes = get_stock_gl_code($stock_id);
//            add_gl_trans_std_cost($tran_type, $adj_no, $date_,
//                            $stock_gl_codes['adjustment_account'], $stock_gl_codes['dimension_id'], $stock_gl_codes['dimension2_id'], $memo_, ($standard_cost * -($quantity)));
//
//            add_gl_trans_std_cost($tran_type, $adj_no, $date_, $stock_gl_codes['inventory_account'], 0, 0, $memo_, ($standard_cost * $quantity));

            begin_global_transaction();
        
            $result_d = get_fi_stock_count_details($tran_type, $trans_no);
            if(!(db_num_rows($result_d) > 0)){
                write_fi_log($batch_no, $systypes_array[$tran_type]." #$trans_no - No Details found.");
            }

            $error = 0;
        
            while($d = db_fetch($result_d)){
                    $detail_line_id = $d['detail_line_id'];
                    if($d['qty'] == 0 || $d['standard_cost'] == 0){
                        continue;
                    }

                    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;
                    }


                    $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_, $adjustment_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"=>"");
}

?>