<?php
/**********************************************************************
***********************************************************************/


function process_delete_gl_batch($gl_batch_no)
{
    global $systypes_array;
    
    $cdglb_msg = can_delete_gl_batch($gl_batch_no);
    if($cdglb_msg['success'] !== true){
        display_error($cdglb_msg['message']);
        return;
    }
    
    begin_transaction();
    
//    $result1 = get_gl_trans(ST_GL_BATCH, $gl_batch_no);
//    $gl_recs = db_num_rows($result1);
//    if($gl_recs == 0){
//        display_error_cancel("No GL entries in the selected GL batch.");
//        return;
//    }
    
//    $deleted_gl_recs = void_gl_trans(ST_GL_BATCH, $gl_batch_no);
//    if($gl_recs != $deleted_gl_recs){
//        display_error_cancel("GL entries count and deleted GL entries count do not match. Please contact system administrator.");
//        return;
//    }

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

    if($fi_recs != $reopened_fi_recs){
        display_error_cancel("FI Batches count and reopened FI Batches count do not match. Please contact system administrator.");
        return;
    }

    $n2 = update_gl_batch_header_deleted($gl_batch_no);
    if($n2 != 1){
        display_error_cancel("Another user has updated ".$systypes_array[ST_GL_BATCH]." #".$gl_batch_no);
            return;        
    }
    
//    add_audit_trail(ST_GL_BATCH, $gl_batch_no, Today(), 'Deleted');

//    cancel_transaction();
    commit_transaction();

    
    meta_forward($_SERVER['PHP_SELF'], "did=$gl_batch_no");

    
}

function void_fi_batch_gl_trans($fi_batch_no)
{
    $res_fi_trans_h = get_fi_batch_transactions($fi_batch_no);   
    while($fi_trans_h = db_fetch($res_fi_trans_h)){            
        $type = $fi_trans_h["type"];
        $type_no = $fi_trans_h["type_no"];

        void_gl_trans($type, $type_no);
        
        add_audit_trail($type, $type_no, Today(), 'Deleted');
    }
}


function can_delete_gl_batch($batch_no)
{
    $gl_batch = view_gl_batch_header($batch_no);
    if($gl_batch['deleted_by'] != 0){
        $message = ("This GL batch has been deleted already.");
        return array("success" => false, "message" => $message);    
    }
    
    return array("success" => true, "message" => '');    
    
}
