<?php

function cp_get_open_fi_batches($trans_type)
{
    $sql = "SELECT fib.*    
            FROM ".TB_PREF."fi_batches fib    
            WHERE fib.trans_type = ".db_escape($trans_type)."  
                AND fib.gl_batch_no = 0   
            ORDER BY id 
            LIMIT 1000 ";
    
    return db_query($sql, "cp_get_open_fi_batches"); 
}


function cp_delete_fi_batches($path_to_root)
{
    include_once($path_to_root . "/gl/includes/ui/delete_gl_batch_ui.inc");
    include_once($path_to_root . "/gl/includes/ui/delete_fi_batch_ui.inc");
    include_once($path_to_root . "/gl/includes/fi/db/generate_fi_accounting_entries_db.inc"); 
    include_once($path_to_root . "/includes/db/group_companies_db.inc");
    
    global $systypes_array;
    
    $log_filename = "cp_delete_fi_batches";

    $trans_type = $_POST["filterType"];
    
    if(!($trans_type > 0)){ 
        $message = "No Transaction Type selected";
        write_custom_program_log($log_filename, $message);
        display_error($message);
        return;
    }
    
    $result = cp_get_open_fi_batches($trans_type);
    
    $n = db_num_rows($result);
    $message = $n." records selected";
    write_custom_program_log($log_filename, $message);
        
    if(!($n > 0)){        
        return;
    }
        
    $c = 0;
    while ($fi_batch = db_fetch($result)) {
        $fi_batch_no = $fi_batch['id'];

        $row_state = $fi_batch['row_state'];

        $cdfib_msg = can_delete_fi_batch($fi_batch);
        if($cdfib_msg['success'] !== true){
            display_error($cdfib_msg['message']);
            return;
        }

        begin_transaction();

        delete_fi_accounting_entries($fi_batch_no);
        
        reopen_fi_batch_source_transactions($fi_batch_no);

        reopen_all_group_company_source_transactions($fi_batch_no);

        delete_fi_batch_transactions($fi_batch_no);

        $count = get_fi_batch_processed_entries_count($fi_batch_no);
        if($count > 0){
            $message = ("This FI batch is being processed. It can not be deleted.");
            display_error_cancel($message);
            return;  
        }

        $n = delete_fi_batch_header_record($fi_batch_no, $row_state);
        if($n != 1){
            $message = ("This FI batch has been updated by another user.");
            display_error_cancel($message);
            return;  
        }

        commit_transaction();
        
        
        $message = $systypes_array[ST_FI_BATCH]." ".$fi_batch_no." deleted";
        write_custom_program_log($log_filename, $message);
        
        $c++;  
    }
    
    $message = $c." ".$systypes_array[ST_FI_BATCH]."es ".$fi_batch_no." deleted";
    write_custom_program_log($log_filename, $message);
        
    
}

function cp_display_delete_fi_batches_custom_edit_controls($path_to_root)
{
    start_table(TABLESTYLE); 
    //custom_values_list_row("Script", 'custom_script', $values_array, null, "");
    journal_types_list_cells(_("Type:"), "filterType", null, true);
    
    $rec_count = 0;
    
    $trans_type = $_POST["filterType"];
    
    if(isset($trans_type) && $trans_type > 0){
        $rec_count = cp_get_fi_batch_count($trans_type);
    }
    
    start_row();
    label_cell("Records");
    label_cell($rec_count, "align=center");
    end_row();
    
    end_table();
}


function cp_get_fi_batch_count($filterType)
{
    
        $sql = "SELECT COUNT(gb.id) rec_count  
            FROM ".TB_PREF."fi_batches gb  
            WHERE gb.trans_type=".db_escape($filterType)." 
                AND gb.gl_batch_no = 0 ";    
        
    $result = db_query($sql, "cp_get_gl_batch_count"); 
    
    return db_result_value($result, "rec_count");
    
}