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

$page_security = 'SA_DAILY_ITEM_RETURN';
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 . "/includes/ui/ui_lists_group_companies.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/inventory/includes/ui/daily_item_return_ui.inc");
include_once($path_to_root . "/inventory/includes/db/daily_item_issue_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_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 . "/includes/db/inventory_db.inc"); 
include_once($path_to_root . "/includes/project_cost_functions.inc"); 
include_once($path_to_root . "/assets/includes/db/assets_db.inc");

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

global $systypes_array;

$status = true;

if (isset($_GET['dii_trans_no']) || isset($_POST['dii_trans_no'])) {
    $_POST['dii_trans_no'] = isset($_GET['dii_trans_no']) ? $_GET['dii_trans_no'] : $_POST['dii_trans_no'];
    $trans_no = 0;
    
    $header = get_daily_item_issue_header_record_type_trans_no(ST_DAILY_ITEM_ISSUE, $_POST['dii_trans_no']);
        if (! is_array($header)) {
            page(_($help_context = ''), false, false, "", $js);
            display_error(("Invalid transaction No."));
            display_footer_exit();
        } else {
            if ($header['approval_status'] != AS_APPROVED) {
                page(_($help_context = ''), false, false, "", $js);
                display_error(($systypes_array[ST_DAILY_ITEM_ISSUE]." #".$_POST['dii_trans_no']." is not approved."));
                display_footer_exit();
            }
        }
    
    $page_title = _($systypes_array[ST_DAILY_ITEM_RETURN]);
} elseif(isset($_GET['SavedID'])) { 
    $trans_no = $_GET['SavedID'];
    
    $page_title = _($systypes_array[ST_DAILY_ITEM_RETURN]);
} else {
    page(_($help_context = ''), false, false, "", $js);
    display_error(("Invalid transaction type."));
    display_footer_exit();
}

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

simple_page_mode(true);

if (isset($_GET['SavedID'])) {
    global $systypes_array;
    
    $iss_tp = ST_DAILY_ITEM_ISSUE;
    $trans_type = ST_DAILY_ITEM_RETURN;

    $type_name = $systypes_array[$trans_type];

    display_notification_centered("$type_name trans #$trans_no has been saved.");
    display_note(get_trans_view_str($trans_type, $trans_no, _("&View this $type_name #$trans_no.")), 0, 1);
    hyperlink_params("$path_to_root/inventory/inquiry/daily_item_issue_inquiry.php", "Create another $type_name", "type=$iss_tp");
    hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
    display_footer_exit();
}

if (isset($_POST['Create_DIR'])) {        
    $msg = save_daily_item_return($trans_no);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
            $status = false;
        } 
}

if (list_updated('tran_unit')) {
    $Ajax->activate('dir_details');
}

global $Ajax;

start_form();

    if (isset($_POST['dii_trans_no'])) {
        hidden('dii_trans_no', $_POST['dii_trans_no']);
    } 

    $Ajax->activate('nsit_header');
    $Ajax->activate('_page_body');
    $Ajax->activate('All_details');
    div_start('All_details');
    
        $pi_header = display_daily_item_return_header($trans_no, $status);

        display_daily_item_return_details($trans_no, $status);

        $Ajax->activate("control_items");

            submit_center_first('Create_DIR', _("Create DI Return"), '', 'default');
        
    div_end();
    
end_form(4);
end_page(@$_GET['popup'], false, false);        

?>