<?php

function cp_get_open_gl_batches($trans_type)
{
    $sql = "SELECT glb.*    
            FROM ".TB_PREF."gl_batches glb    
            WHERE glb.gl_batch_type = ".db_escape($trans_type)."  
                AND glb.deleted_by = 0   
            ORDER BY id DESC 
            LIMIT 1000 ";
    
    return db_query($sql, "cp_get_open_fi_batches"); 
}

function cp_delete_gl_batch($gl_batch_no)
{
    global $systypes_array; 
    
    $gl_batch = get_gl_batch_record($gl_batch_no);
    $gl_batch_trans_type = $gl_batch["gl_batch_type"];

    $result1 = get_gl_trans($gl_batch_trans_type, $gl_batch_no);
    $gl_recs = db_num_rows($result1);
    if($gl_recs == 0){
        $message = ("No GL entries in the selected GL batch.");
        return array("success"=>false, "message"=>$message); 
    }
    
    $deleted_gl_recs = void_gl_trans($gl_batch_trans_type, $gl_batch_no);
    if($gl_recs != $deleted_gl_recs){
        $message = ("GL entries count and deleted GL entries count do not match. Please contact system administrator.");
        return array("success"=>false, "message"=>$message); 
    }

    $result2 = view_gl_batch_details($gl_batch_no);
    $fi_recs = db_num_rows($result2);
    $reopened_fi_recs = 0;
    while ($fi_batch = db_fetch($result2)) {
        $fi_batch_no = $fi_batch['id'];
        $ndp = get_fi_batch_processed_entries_count($fi_batch_no);
        $ndr = reopen_fi_batch_details($fi_batch_no);
        if($ndp != $ndr){
            $message = ($systypes_array[ST_FI_BATCH]." #".$fi_batch_no." : Processed entries count and reopened entries count do not match. Please contact system administrator.");
            return array("success"=>false, "message"=>$message);  
        }
        
        $m = get_fi_batch_processed_entries_count($fi_batch_no); 
        if($m != 0){
            $message = ($systypes_array[ST_FI_BATCH]." #".$fi_batch_no." - There are $m records remaining with gl_counter > 0");
            return array("success"=>false, "message"=>$message);  
        }
        
        $n = reopen_fi_batch_header_record($fi_batch_no, $fi_batch['row_state']);
        if($n != 1){
            $message = ("Another user has updated ".$systypes_array[ST_FI_BATCH]." #".$fi_batch_no);
            return array("success"=>false, "message"=>$message);  
        }
        $reopened_fi_recs++;
    }

    if($fi_recs != $reopened_fi_recs){
        $message = ("FI Batches count and reopened FI Batches count do not match. Please contact system administrator.");
        return array("success"=>false, "message"=>$message);  
    }

    $n2 = update_gl_batch_header_deleted($gl_batch_no);
    if($n2 != 1){
        $message = ("Another user has updated ".$systypes_array[$gl_batch_trans_type]." #".$gl_batch_no);
        return array("success"=>false, "message"=>$message);  
    }
    
    add_audit_trail($gl_batch_trans_type, $gl_batch_no, Today(), 'Deleted');

    $message = $systypes_array[$gl_batch_trans_type]." #".$gl_batch_no." deleted";
    return array("success"=>true, "message"=>$message); 

    
}


function cp_delete_gl_batches($path_to_root)
{
    include_once($path_to_root . "/gl/includes/db/gl_db_trans.inc");
    include_once($path_to_root . "/gl/includes/db/financial_integration_inquiry_db.inc");
    include_once($path_to_root . "/gl/includes/fi/db/generate_fi_accounting_entries_db.inc");
    include_once($path_to_root . "/gl/includes/fi/db/post_fi_accounting_entries_db.inc");
    include_once($path_to_root . "/gl/includes/ui/delete_gl_batch_ui.inc");
    
    global $systypes_array;
    
    $log_filename = "cp_delete_gl_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_gl_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 ($gl_batch = db_fetch($result)) {
        $gl_batch_no = $gl_batch['id'];

        $cdglb_msg = can_delete_gl_batch($gl_batch_no);
        if($cdglb_msg['success'] !== true){ 
            $message = $cdglb_msg['message'];
            write_custom_program_log($log_filename, $message);
            display_error($message);
            return;
        }

        begin_transaction();

        $dglb_msg = cp_delete_gl_batch($gl_batch_no); 
        if($dglb_msg["success"] !== true){
            $message = $dglb_msg["message"];
            write_custom_program_log($log_filename, $message);
            display_error_cancel($message);
            return;  
        }

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

function cp_display_delete_gl_batches_custom_edit_controls($path_to_root)
{
    global $systypes_array;
    
    start_table(TABLESTYLE); 
    //custom_values_list_row("Script", 'custom_script', $values_array, null, "");
//    journal_types_list_cells(_("Type:"), "filterType");
    $values_array[ST_GL_BATCH] = $systypes_array[ST_GL_BATCH];  
    foreach ($systypes_array as $key => $value) {
        if($key >= 1000){
            $values_array[$key] = $value;   
        }    
    }
    
    asort($values_array);

    start_row();
        custom_values_list_cells(_("Type:"), "filterType", $values_array, null, '', true);
    end_row();
    
    $rec_count = 0;
    if(isset($_POST['filterType']) && $_POST['filterType'] > 0){
        $rec_count = cp_get_gl_batch_count($_POST['filterType'], $deleted=false);
    }
    
    start_row();
    label_cell("Records");
    label_cell($rec_count, "align=center");
    end_row();
    
    end_table();
}


function cp_get_gl_batch_count($filterType, $deleted=false)
{
    
    $sql = "SELECT COUNT(gb.id) rec_count  
            FROM ".TB_PREF."gl_batches gb  
            WHERE gb.gl_batch_type=".db_escape($filterType);
    
    if ($deleted == true) {
        $sql .= " AND gb.deleted_by = 1 ";
    }else{        
        $sql .= " AND gb.deleted_by = 0 ";
    }
        
    $result = db_query($sql, "cp_get_gl_batch_count"); 
    
    return db_result_value($result, "rec_count");
    
}