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

$page_security = 'SA_BUDGET_TO_BUDGET_TRANSFER';
if (!@$_GET['popup'])
    $path_to_root = "..";
else	
    $path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/inventory/includes/ui/budget_to_budget_transfers_ui.inc");
include_once($path_to_root . "/inventory/includes/db/budget_to_budget_transfers_db.inc");
include_once($path_to_root . "/inventory/includes/db/items_db.inc"); 
include_once($path_to_root . "/inventory/includes/db/items_units_db.inc"); 
include_once($path_to_root . "/admin/db/transactions_db.inc"); 
include_once($path_to_root . "/projects/includes/db/projects_db.inc"); 
include_once($path_to_root . "/includes/project_cost_functions.inc"); 
include_once($path_to_root . "/includes/db/inventory_db.inc"); 
include_once($path_to_root . "/inventory/includes/db/lot_allocations_db.inc");  
include_once($path_to_root . "/inventory/includes/ui/lot_ui.inc");  
include_once($path_to_root . "/inventory/includes/db/lots_db.inc");   
include_once($path_to_root . "/services/includes/db/job_orders_db.inc"); 
include_once($path_to_root . "/inventory/includes/db/lot_allocations_db.inc");
include_once($path_to_root . "/includes/approval_functions.inc"); 

$js = '';
$js .= get_js_open_window(900, 500);
$js .= get_js_date_picker();

global $systypes_array;

if (isset($_GET['ProcessedID'])) {
    page(_($help_context = $systypes_array[ST_BUDGET_TO_BUDGET_TRANSFER]), false, false, "", $js);
    
    $trans_no = $_GET['ProcessedID'];
    $trans_type = $_GET['type'];
    $msg = get_session_variable('btbtr_messages');
    clear_session_variable('btbtr_messages');

    $type_name = $systypes_array[$trans_type];

    display_notification_centered($msg);
    display_note(get_trans_view_str($trans_type, $trans_no, _("&View this $type_name #$trans_no.")), 0, 1);
    display_footer_exit();
}

if ((isset($_GET['bbt_no']) && is_numeric($_GET['bbt_no'])) || (isset($_POST['bbt_no']) && is_numeric($_POST['bbt_no']))) {
    $_POST['bbt_no'] = isset($_GET['bbt_no']) ? $_GET['bbt_no'] : $_POST['bbt_no'];
    
    $header = get_budget_to_budget_transfer_header_record($_POST['bbt_no']);
        if (is_array($header)) {
            if ($header['type'] != ST_BUDGET_TO_BUDGET_TRANSFER) {
                page(_($help_context = ''), false, false, "", $js);
                display_error(("Invalid transaction type."));
                display_footer_exit();
            } 
        } else {
            page(_($help_context = ''), false, false, "", $js);
            display_error(("Could not find ".$systypes_array[ST_BUDGET_TO_BUDGET_TRANSFER]." record #".$_POST['bbt_no']));
            display_footer_exit();
        }
        
    $page_title = _('Modify '.$systypes_array[ST_BUDGET_TO_BUDGET_TRANSFER]." #".$header['trans_no']);
    
} else {
    $_POST['bbt_no'] = 0;
    $page_title = _('New '.$systypes_array[ST_BUDGET_TO_BUDGET_TRANSFER]);
}

page(_($help_context = $page_title), false, false, "", $js);

simple_page_mode(true);

if (isset($_POST['Save_BBT'])) {
    $msg = save_budget_to_budget_transfer($_POST['bbt_no']);
        if ($msg['success'] !== true) {
            if (isset($msg["focus_to"])) {
                set_focus($msg["focus_to"]);            
            }
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            $Mode = 'RESET';
        }
}

if (isset($_POST['Process_BBT'])) {
    $msg = save_budget_to_budget_transfer($_POST['bbt_no'], true);
        if ($msg['success'] !== true) {
            if (isset($msg["focus_to"])) {
                set_focus($msg["focus_to"]);            
            }
            display_error($msg['message']);
        } 
}

if (isset($_POST['Add_BBT']) || isset($_POST['Update_BBT'])) {        
    $msg = save_budget_to_budget_transfer_header_and_line($_POST['bbt_no'], $selected_id);
        if ($msg['success'] !== true) {
            if (isset($msg["focus_to"])) {
                set_focus($msg["focus_to"]);            
            }
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            clear_budget_to_budget_transfer_line();
            $Mode = 'RESET';
        }
}

if ($Mode == 'Delete') {
    $msg = handle_delete_budget_to_budget_transfer_line($_POST['bbt_no'], $selected_id);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            clear_budget_to_budget_transfer_line();
            $Mode = 'RESET';
        }
}

if (isset($_POST['cancel'])) {
    clear_budget_to_budget_transfer_line();
    
    $Mode = 'RESET';
    $selected_id = -1;
}  

if ($Mode == 'RESET') {
    $selected_id = -1;
}

global $Ajax;

start_form();

    if (isset($_POST['bbt_no']) && $_POST['bbt_no'] > 0) {
        hidden('bbt_no', $_POST['bbt_no']);
    } else {
        $_POST['bbt_no'] = 0;
    }

    $Ajax->activate('_page_body');
    $Ajax->activate('All_details');
    div_start('All_details');
    
        display_budget_to_budget_transfer_header($_POST['bbt_no']);

        display_budget_to_budget_transfer_details($_POST['bbt_no'], input_num('to_jo_id'), $_POST['from_loc_code'], $Mode, $selected_id); 

        $header = get_budget_to_budget_transfer_header($_POST['bbt_no']);
            if (is_array($header)) {
                submit_center_first('Save_BBT', _("Save Budget To Budget Transfer"), '', 'default');    
                submit_center_last('Process_BBT', _("Process Budget To Budget Transfer"), _("Process"), 'default');   br(2);
            } 
            
            start_table();
                submit('Refresh', 'Refresh');
            end_table();

    div_end();
    
end_form(2);
end_page(@$_GET['popup'], false, false);        

?>