<?php

include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/ui/items_cart.inc");

function line_start_focus() {
  global $Ajax;

  $Ajax->activate('items_table');
  set_focus('_stock_id_edit');
}

function handle_new_order() {
    if (isset($_SESSION['mt_recp_items'])) {
        $_SESSION['mt_recp_items']->clear_items();
        unset ($_SESSION['mt_recp_items']);
    }

    $_SESSION['mt_recp_items'] = new items_cart(ST_LOCTRANSFER);
        
        populate_cart_from_database();
}

function populate_cart_from_database() {
    $myrow = get_material_requisition_transfer($_GET['mrt_no']);
        
        $mrt_cart = $_SESSION['mt_recp_items'];

        $mrt_cart->order_id = $myrow["mrt_no"];
        $mrt_cart->reference = $myrow["reference"];
        $mrt_cart->issuing_location  = $myrow["issuing_location"];
        $mrt_cart->issuing_location_name  = $myrow["issuing_location_name"];
        $mrt_cart->issuing_project_id = $myrow["project_id"];
        $mrt_cart->project_description = $myrow["project_description"];
        $mrt_cart->receiving_location  = $myrow["receiving_location"];
        $mrt_cart->receiving_location_name  = $myrow["receiving_location_name"];
        
        $rqt_row = get_material_transfer_requisition($myrow["source_mtr_no"]);
            $mrt_cart->project_id = $rqt_row['project_id'];
            $mrt_cart->movement_type_name = $rqt_row["movement_type_name"];
            $mrt_cart->linked_field_type = $rqt_row["linked_field_type"];
            $mrt_cart->linked_field_id  = $rqt_row["linked_field_id"];
            $mrt_cart->requestor_name  = $rqt_row["requestor_name"];
        
        $mrt_cart->memo_ = $myrow["comments"];
        $mrt_cart->required_date  = $myrow["created_on"];
        $mrt_cart->created_on = $myrow["created_on"];
        $mrt_cart->approval_status = $myrow["approval_status"];
  
  
        $mrt_details = get_mrt_details($mrt_cart->order_id);

            $element_no = 0;
            while ($mrtd_row = db_fetch($mrt_details)) {
                
                $mrt_line = new line_item($mrtd_row["stock_id"], 0, $standard_cost=null, 
                        $mrtd_row["description"], null, null, null, $mrtd_row["id"], $mrtd_row["issued_qty"], $mrtd_row["qty"], 0, 0, null, 0,
                        null, null, null, null, null, $mrtd_row["tran_unit"], $mrtd_row["tran_qty"], $mrtd_row["convertion_factor"]);


                $mrt_line->row_id = $mrtd_row["id"];
                $mrt_line->stock_control_method = $mrtd_row["stock_control_method"];
                $mrt_line->project_site_id = $mrtd_row["project_site_id"];
                $mrt_line->line_ref = $mrt_line->row_id;
                $mrt_line->issued_qty = $mrtd_row["issued_qty"];
                $mrt_line->requested_unit_quantity = $mrtd_row["tran_qty"];
                
//                $mtr_line_row = get_mtr_deatails_record_by_id($mrtd_row["ref_line_id"]);
//                $task_row = get_task_record($mtr_line_row["task_id"]);
//                $mrt_line->task_id = $task_row["task_id"];
//                $mrt_line->task_name = $task_row["description"];
                $mrt_line->task_id = 0;
                $mrt_line->task_name = '';
                
                $mtr_header = get_mtr_edit_info($myrow["source_mtr_no"]);
                $mrt_cart->row_state = $mtr_header["row_state"];
                
                $_POST[$mrt_line->row_id.'_tran_unit'] = $mrtd_row["tran_unit"];

                $mrt_cart->line_items[$element_no] = $mrt_line;
                
                $element_no++;
            }
}

function can_process() {
    global $Refs;
    
    if ($_SESSION['mt_recp_items']->approval_status == AS_COMPLETED || $_SESSION['mt_recp_items']->approval_status == AS_CLOSED) {
        display_error("Approval status is not allowed to further updates.");
        return false;
    }

//    if (! (isset($_SESSION['mt_recp_items']->issuing_project_id) && $_SESSION['mt_recp_items']->issuing_project_id > 0)) {
//        display_error(_("Issuing project not found."));
//        set_focus('');
//        return false;
//    } 
    
    if (! (isset($_SESSION['mt_recp_items']->project_id) && $_SESSION['mt_recp_items']->project_id > 0)) {
        display_error(_("Requesting project not found."));
        set_focus('');
        return false;
    } 
    
    if (! (strlen($_POST['company_code']) > 0)) {
        display_error('Please select a company.');
        set_focus('company_code');
        return false;
    }
    
    if ($_POST['employee_code'] == 0) {
        display_error('Please select an employee.');
        set_focus('employee_code');
        return false;
    }
    
    if (! is_date($_POST['tran_date'])) {
        display_error(_("Entered date is not valid."));
        set_focus('tran_date');
        return false;
    }
        
    $total_qty = 0;
        
	foreach ($_SESSION['mt_recp_items']->line_items as $line_item) {
            if (isset($_POST[$line_item->row_id."_check"]) && $_POST[$line_item->row_id."_check"] == 1) {
                if (! check_num($line_item->row_id.'_tran_qty', 0.0001))
                {
                    display_error(_("Line Id ".$line_item->row_id.": The quantity entered for item [".$line_item->stock_id."] is negative or invalid."));
                    set_focus($line_item->row_id."_tran_qty");
                    return false;
                }
                $total_qty += $_POST[$line_item->row_id."_tran_qty"];
            }
        }
        
        if ($total_qty == 0) { 
            display_error(_("Please check at least one item before saving."));
            return false;
        }
        
        foreach ($_SESSION['mt_recp_items']->line_items as $line_item) {
            if (isset($_POST[$line_item->row_id."_check"]) && $_POST[$line_item->row_id."_check"] == 1) {
                $stock_id = $line_item->stock_id;
                $alternate_unit = $_POST[$line_item->row_id."_tran_unit"];
                $convesion_factor = 1;
                get_convertion_factor2($stock_id, $alternate_unit, $convesion_factor);

//                $requested_tran_unit_qty = $line_item->requested_qty / $convesion_factor;
                $requested_tran_unit_qty = $line_item->qty / $convesion_factor; // total allowed issuing qty 
                $issued_tran_unit_qty = $line_item->issued_qty / $convesion_factor; // already issuing qty 
                $this_issue_tran_unit_qty = input_num($line_item->row_id."_tran_qty"); 
                
                if($requested_tran_unit_qty < $issued_tran_unit_qty + $this_issue_tran_unit_qty) {
                    $allowed_maximum_tran_unit_qty = $requested_tran_unit_qty - $issued_tran_unit_qty;
                    $msg = "Line Id ".$line_item->row_id.": Maximum allowed transderring quantity is ". number_format($allowed_maximum_tran_unit_qty, 2).'.';
                    display_error($msg);
                    set_focus($line_item->row_id."_qty");
                    return false;
                }
            }
        }
        
	if (! $Refs->is_valid($_POST['mrt_reference'])) {
            display_error( _("You must enter a reference."));
            set_focus('mio_reference');
            return false;
	}

    $ref_msg = reference_exists($_POST['mrt_reference'], ST_MATERIAL_TRANSFER_RECEIPT);
        if ($ref_msg) {
            display_error( $ref_msg);
            set_focus('mrt_reference');
            return false;
        }
    
    $vtqd_msg = validate_transaction_quantity_details($_SESSION['mt_recp_items']);
        if ($vtqd_msg['success'] !== true) {
            display_error($vtqd_msg['message']);
            return false;
        }   
        
	return true;
}

function add_lot_details() {
    if (! isset($_SESSION['lot_info#'.ST_MATERIAL_TRANSFER_RECEIPT])) {
        return;
    }
    
    $lot_info_array =  $_SESSION['lot_info#'.ST_MATERIAL_TRANSFER_RECEIPT];

    foreach ($_SESSION['mt_recp_items']->line_items as $line_item) {
       	if ($line_item->stock_control_method != SKCM_NORMAL) {
            $line_item->lot_details = $lot_info_array[$line_item->stock_id] ;
        } 
    }    
}

function update_cart() {
    foreach ($_SESSION['mt_recp_items']->line_items as $line_item) {
        $line_item->tran_qty = $_POST[$line_item->row_id."_tran_qty"];
        $line_item->tran_unit = $_POST[$line_item->row_id."_tran_unit"];
        
        $alternate_unit = get_convertion_factor($_POST[$line_item->row_id."_tran_unit"], $line_item->stock_id);
        if ($alternate_unit == false) {
            $issue_convertion_factor = 1;
        } else {
            $issue_convertion_factor = $alternate_unit['convertion_factor'];
        }
        $line_item->convertion_factor = $issue_convertion_factor;
        
        $item_info = get_stock_item_edit_info($line_item->stock_id);

        $lot_details = get_selected_lot_serial_info('guid', $line_item);         
        $line_item->lot_details = $lot_details; 
        
        $line_item->high_value_item = $item_info["high_value_item"];
        $line_item->sample_item = $item_info["sample_item"];
        $line_item->reference_cost = $item_info["reference_cost"];
        $line_item->stock_control_method = $item_info["stock_control_method"];

        
        if ($_POST[$line_item->row_id."_project_site_id"] > 0) {
            $line_item->project_site_id = $_POST[$line_item->row_id."_project_site_id"];
        } else {
            $line_item->project_site_id = 0;
        }
    }
}

function end_processing($trans_no) {
    $_SESSION['mt_recp_items']->clear_items();
    unset($_SESSION['mt_recp_items']);         
        
    meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
}

function close_material_transfer_requisition($mrt_no){
    $cmrt_msg = close_mrt($mrt_no);
        if ($cmrt_msg['success'] !== true) {
            display_error($cmrt_msg['message']);
        } else {
            set_session_variable('mrt_messages', "Material requisition ".$mrt_no." has been closed");
            end_processing($mrt_no); 
        }
}

function create_material_transfer_receipt($close_mrt=false) {
    update_cart();
    
    if(!can_process()){
        return;
    }
    
    $source_mrt_no = $_SESSION['mt_recp_items']->order_id;
    
        $amrt_msg = add_material_transfer_receipt($_SESSION['mt_recp_items']->line_items, $_POST['mrt_reference'],
                                                  $_SESSION['mt_recp_items']->issuing_location, $_SESSION['mt_recp_items']->receiving_location, 
                                                  $_SESSION['mt_recp_items']->project_id, $_POST['memo_'], $_SESSION['mt_recp_items']->order_id, 
                                                  $_SESSION['mt_recp_items']->row_state, $_POST['company_code'], $_POST['employee_code'],
                                                  $_POST['tran_date']);
        
            if ($amrt_msg['success'] !== true) {
                display_error($amrt_msg['message']);
                return;
            }
        
            $can_close_mrt =  false;
            $closing_status = AS_RECEIVED;

            if (check_all_mrt_lines_issued($source_mrt_no) === true) {
                $can_close_mrt = true;
                $closing_status = AS_COMPLETED;
            } else {
                if ($close_mrt) {
                    $can_close_mrt = true;
                    $closing_status = AS_CLOSED;
                }        
            }    

            if ($can_close_mrt) {
                $cmr_msg = close_mrt($source_mrt_no, $closing_status);
                    if ($cmr_msg['success'] !== true) {
                        display_error($cmr_msg['message']);
                        return;
                    }
            }
            
            commit_transaction();
        
                unset($_SESSION['mt_recp_items']);
                meta_forward($_SERVER['PHP_SELF'], "AddedID=".$amrt_msg['tran_no']);
}

function close_material_requisition_transfer($mrt_no) {
    if (! can_close_material_requisition_transfer()) {
        return;
    }
    
    begin_transaction();
    
    $cmr_msg = close_mrt($mrt_no, AS_CLOSED);
        if($cmr_msg['success'] !== true){
            display_error($cmr_msg['message']);
        }
        else{
            set_session_variable('im_messages', "Material requisition transfer ".$mrt_no." has been closed");
            end_processing($mrt_no); 
        }
}

function can_close_material_requisition_transfer() {
    if (! ($_SESSION['im_items']->approval_status == AS_NEW || $_SESSION['im_items']->approval_status == AS_ISSUED)) {
        display_error("Status of this Material Requisition Transfer does not allow closing it.");
        return false;
    }
        
    return true;
}

function check_item_data() {
    if (! check_num('qty',0)) {
        display_error(_("The quantity entered is negative or invalid."));
        set_focus('qty');
        return false;
    }
        
    if (input_num('qty') == 0) {
        display_error(_("The quantity entered has to be greater than zero."));
        set_focus('qty');
        return false;
    }
   	return true;
}

function can_delete_item($id) {
    if ($_SESSION['mt_recp_items']->line_items[$id]->issued_qty > 0) {
        display_error("This item has some quantity issued. Can not delete this line.");
        return false;
    }
    
    return true;
}

function handle_update_item() {
    if ($_POST['UpdateItem'] != "" && check_item_data()) {
        $id = $_POST['LineNo'];
    	$_SESSION['mt_recp_items']->update_cart_item($id, input_num('qty'), input_num('std_cost'));
    }
    
    line_start_focus();
}

function handle_delete_item($id) {
    if (can_delete_item($id)) {
        $_SESSION['mt_recp_items']->remove_from_cart($id);
        line_start_focus();
    }
}

function handle_new_item() {
    if (!check_item_data())
            return;

    add_mtro_to_cart($_SESSION['mt_recp_items'], $_POST['stock_id'],input_num('qty'), $_POST['required_date'], $_POST['line_commnet']);
	line_start_focus();
}

function add_mtro_to_cart(&$order, $new_item, $new_item_qty, $required_date, $line_comment) {
    if ($order->find_cart_item($new_item)) {
        display_error(_("For Part :") . $new_item . " " . "This item is already on this material transfer receipt. 
                        You can change the quantity requested of the existing line if necessary.");
    } else {
        $order->add_to_cart(count($order->line_items), $new_item, $new_item_qty, null, null, $required_date, $line_comment);
    }
}

function display_material_transfer_receipt_header(&$order) {
    global $Refs;
    
    if(isset($order->order_id) && $order->order_id > 0){ // 3rd step project
        $project_id = $order->project_id;
        $user_id = $_SESSION['wa_current_user']->user;
        $cap = can_access_project($project_id, $user_id);
            if($cap['success'] !== true) 
            {
                display_error($cap["message"]);
                set_focus('project_id');
                display_footer_exit();
                exit;
            } 
    }      

    start_outer_table(TABLESTYLE2, "width=80%");

        table_section(1);
            label_row(_("Issuing Location:"), $order->issuing_location_name, "class='tableheader2'");
            label_row(_("Receiving Location:"), $order->receiving_location_name, "class='tableheader2'");
            $requesting_project = get_project($order->project_id);
            label_row('Requesting Project:', $requesting_project['description']);
//            label_row(_("Requsting Project:"), $order->project_description, "class='tableheader2'");

        table_section(2, "35%");
            label_row(_("Transfer No:"), get_inventory_trans_view_str(ST_REQTRANSFER, $order->order_id), "class='tableheader2'");
            label_row(_("Transfer Reference:"), $order->reference, "class='tableheader2'");
            label_row(_("Transfer Date:"), sql2date($order->required_date), "class='tableheader2'");

        table_section(3, "35%");
            label_row(_("Movement Type:"), $order->movement_type_name, "class='tableheader2'");
            $linked_field_values = get_movement_type_linked_field_values($order->linked_field_type, $order->linked_field_id);
            if(strlen($linked_field_values["field_name"]) > 0){
                label_row($linked_field_values["field_name"].':', $linked_field_values['value'], "class='tableheader2'");
            }

            label_row(_("Requestor:"), $order->requestor_name, "class='tableheader2'");

    end_outer_table(1);

    if (strlen($order->memo_) > 0) {
        start_table(TABLESTYLE2);
            label_row(_("MRT Comment:"), nl2br($order->memo_), "class='tableheader2'");
        end_table(1);
    }

    start_outer_table(TABLESTYLE2, "width=60%"); // outer table
    
        table_section(1, "50%");
            locations_list_row(_("Issuing Location:"), 'issuing_location', $order->issuing_location, false, false);
            locations_list_row(_("Receiving Location:"), 'receiving_location', $order->receiving_location, false, false);
            ref_row(_("Reference:"), 'mrt_reference', '', $Refs->get_next(ST_MATERIAL_TRANSFER_RECEIPT));
            
        table_section(2, "50%");
            label_row(_("Company:"), group_companies_list('company_code', null, 'Select a company', true));
            label_row(_("Employee:"), employees_simple_list('employee_code', null, 'Select an employee', true));
            date_row('Date:', 'tran_date');
    
    end_outer_table(2); // outer table
}

function display_material_transfer_receipt_items($title, &$order) {
    display_heading($title);
    div_start('items_table');
	start_table(TABLESTYLE, "width=100%");
	$th = array(_("Line Id"), _("Item Code"), _("Item Description"), _("Transferred Qty"), _("Transferred Unit"), _("Received Qty"), 
                    _("Receiving Unit"), _("Receiving Unit Qty"), _("Job"));
    
	if ( count($order->line_items)) $th[] = '';

	table_header($th);
	$total = $k = 0; 

	$id = find_submit('Edit');
            foreach ($order->line_items as $line_no=>$stock_item) {

		$total += ($stock_item->standard_cost * ($stock_item->quantity * $stock_item->convertion_factor));

		if ($id != $line_no) {
                    alt_table_row_color($k);

                    label_cell($stock_item->row_id, 'align=center');
                    view_stock_status_cell($stock_item->stock_id);
                    label_cell($stock_item->item_description);
                    
                    $dec = get_qty_dec($stock_item->stock_id);
                    qty_cell($stock_item->requested_unit_quantity, false, 2);
                    label_cell($stock_item->alternate_unit_name);
                    $tran_unit_issued_qty = $stock_item->issued_qty / $stock_item->convertion_factor;
                    qty_cell($tran_unit_issued_qty, false, $dec);
                    
                    if (get_post($stock_item->row_id.'_tran_unit') != '') {
                        $current_tran_unit = get_post($stock_item->row_id.'_tran_unit');
                    } else {
                        $current_tran_unit = $stock_item->tran_unit;
                    }
                    
                    
                    $alternate_unit = get_alternate_unit_info($stock_item->stock_id, $current_tran_unit);
                        if ($alternate_unit) {
                            $alternate_unit_convertion_factor = $alternate_unit['convertion_factor'];
                        } else {
                            $alternate_unit_convertion_factor = 1;
                        }
                    
                    if (list_updated($stock_item->row_id.'_tran_unit') || !isset($_POST[$stock_item->row_id."_tran_qty"] )) {
                        $base_unit_req_qty = $stock_item->tran_qty * $stock_item->convertion_factor;
                        $suggested_issu_qty = ( $base_unit_req_qty - $stock_item->issued_qty) / $alternate_unit_convertion_factor;
                        if ($suggested_issu_qty < 0) {
                            $suggested_issu_qty = 0;
                        }
                        $_POST[$stock_item->row_id."_tran_qty"] = $suggested_issu_qty;
                    } else {
                        $suggested_issu_qty = $_POST[$stock_item->row_id."_tran_qty"];
                    }
                    
                    units_list_cells(null, $stock_item->row_id.'_tran_unit', $stock_item->stock_id, null, false, true);
                    $unit_of_parent = get_post($stock_item->row_id.'_tran_unit');

                    $item_info = get_item_edit_info($stock_item->stock_id);
                    $_POST['stock_control_method'] = $item_info["stock_control_method"];
                    
                    display_quantity_cells(get_post('guid'), $order->trans_type, $stock_item->line_ref, $stock_item->stock_id, 
                                            $item_info["stock_control_method"], $stock_item->row_id.'_', $suggested_issu_qty, $unit_of_parent, 
                                            $dec = 2, "items_table");
                    
//                    label_cell($stock_item->task_name);
                    
                    project_sites_list_cells(null, $stock_item->row_id.'_project_site_id', $order->project_id, $stock_item->project_site_id,'');
                    
                    check_cells('', $stock_item->row_id.'_check', null, false, false, 'align=center');
                    end_row();
                    
		}
	}

    end_table();
	div_end();
}

function mt_receipt_options_controls() {
    echo "<br>";
    start_table();
	  textarea_row(_("Comment:"), 'memo_', null, 50, 3);
    end_table(1);
}

function view_material_transfer_receipt_header($mrt_no) {
    $myrow = get_material_requisition_transfer($mrt_no);
    $mrt_head = get_material_requisition_transfer($myrow['source_mtr_no']);
    $project = get_project_record($mrt_head['project_id']);
    
    start_outer_table(TABLESTYLE2, "width=60%");

        table_section(1, '50%');
            label_row(_("Requesting Location:"), $myrow['receiving_location_name'], "class='tableheader2'");
            label_row(_("Requesting Project:"), $myrow['project_description'], "class='tableheader2'");
            label_row(_("Issuing Location:"), $myrow['issuing_location_name'], "class='tableheader2'");
//            label_row(_("Issuing Project:"), $project['description'], "class='tableheader2'");

        table_section(2, "50%");
            label_row(_("Transfer No:"), get_inventory_trans_view_str(ST_REQTRANSFER, $myrow['source_mtr_no']), "class='tableheader2'");
            label_row(_("Reference:"), $myrow['reference'], "class='tableheader2'");
            label_row(_("Date:"), sql2date($myrow['tran_date']), "class='tableheader2'");
            
            global $inventory_movement_type_linked_fields;
            
            $mtr_header = get_material_transfer_requisition($mrt_head['source_mtr_no']);
            label_row(_("Movement Type:"), $mtr_header['movement_type_name'], "class='tableheader2'");

            if ($mtr_header['linked_field_type'] != IMLF_NOT_LINKED) {
                $link_field = get_movement_type_linked_field_values($mtr_header['linked_field_type'], $mtr_header['linked_field_id']);
                label_row(_($inventory_movement_type_linked_fields[$mtr_header['linked_field_type']].":"), $link_field['value'], "class='tableheader2'");
            }

    end_outer_table(2);
    
    if (strlen($myrow['comments']) > 0) {
        start_table(TABLESTYLE2);
            label_row(_("Comment:"), nl2br($myrow['comments']), "class='tableheader2'");
        end_table(3);
    }
}

function view_material_transfer_receipt_details($mt_rec_no) {
    $result = get_material_transfer_receipt_details($mt_rec_no);

   	start_table(TABLESTYLE, "width=80%");

            $th = array(_("Line Id"), _("MRT Line"), _("Itemcode"), _("Item Description"), _("Tran Qty"), _("Tran Unit"), _("Job"));
            table_header($th);

            while ($myrow = db_fetch($result)){
                start_row();

                    label_cell($myrow["id"], 'align=center');
                    label_cell($myrow["ref_line_id"], 'align=center');
                    label_cell($myrow["stock_id"]);
                    label_cell($myrow["description"]);
                    $dec = get_qty_dec($myrow["stock_id"]);
                        qty_cell($myrow["tran_qty"], false, $dec);
                    $alternate_unit = get_unit_name($myrow['tran_unit']);
                        label_cell($alternate_unit['name']);
//                    label_cell($myrow["task_description"]);
                    label_cell($myrow["project_site_description"]);

                end_row();
            }

	end_table(2);
}

function view_material_transfers_receipt_headers($trans_no, $mt_requst=true, $show_data_has=false) {
    if ($mt_requst) {
        $result = get_material_transfers_receipt_headers_by_mt_req_no($trans_no);
    } else {
        $result = get_material_transfers_receipt_headers_by_mrt_no($trans_no);
    }
    
    if ($show_data_has === true) {
        $db_num_rows = db_num_rows($result);
            if ($db_num_rows == 0) {
                return;
            }
    }

        display_heading2(_(text_bold("Material Transfer Receipts")));
        
        start_table(TABLESTYLE, "width=70%");

            $th = array(_("#"), _("Ref Line"), _("Reference"), _("Issuing Location"), _("Receiving Location"), _("Received By"), _("Received On"));
            table_header($th);

            $k = 0;
                while ($mts = db_fetch($result)) {
                    alt_table_row_color($k);
                        label_cell(get_trans_view_str(ST_MATERIAL_TRANSFER_RECEIPT, $mts['mrt_no']), 'align=center');
                        label_cell($mts['source_mtr_no'], 'align=center');
                        label_cell($mts['reference']);
                        label_cell($mts['issuing_location_name']);
                        label_cell($mts['receiving_location_name']);
                        label_cell($mts['user_name']);
                        label_cell(sql2date($mts['tran_date']));
                    end_row();
                }

        end_table(2);
}

function close_mrt($mrt_no, $closing_status=AS_COMPLETED) {
    $mrh = get_mrt_header_record($mrt_no);
    
            $mrt_close_msg = direct_update_document_status(ST_REQTRANSFER, $mrt_no, $closing_status, $mrh['row_state']);     

                if ($mrt_close_msg['success'] !== true ) {
                    cancel_transaction();

                    return array("success" => false, "message" => $mrt_close_msg['message']);
                }
    
        return array("success" => true, "message" => "");
}

?>