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

$path_to_root = "..";
include_once($path_to_root . "/includes/ui/items_cart.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/inventory/includes/inventory_db.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/includes/project_cost_functions.inc");
include_once($path_to_root . "/inventory/includes/lots_validation.inc");
include_once($path_to_root . "/inventory/includes/serial_no_validation.inc");
include_once($path_to_root . "/inventory/includes/db/serial_no_adjustments_db.inc");
include_once($path_to_root . "/inventory/includes/db/serial_no_db.inc");
include_once($path_to_root . "/projects/includes/db/project_sites_db.inc");
include_once($path_to_root . "/inventory/includes/inventory_adjustment_requests_ui.inc");
include_once($path_to_root . "/inventory/includes/db/inventory_adjustment_request_db.inc");
include_once($path_to_root . "/includes/db/comments_db.inc");
include_once($path_to_root . "/includes/approval_functions.inc");
include_once($path_to_root . "/includes/ui/ui_view.inc");
include_once($path_to_root . "/includes/types.inc");
include_once($path_to_root . "/includes/ui/ui_input.inc");
include_once($path_to_root . "/includes/ui/ui_lists.inc");
include_once($path_to_root . "/includes/ui/ui_lists_custom.inc");
include_once($path_to_root . "/assets/includes/db/assets_db.inc");
include_once($path_to_root . "/includes/data_checks_custom.inc"); 
include_once($path_to_root . "/includes/ui/ui_globals_custom.inc"); 
include_once($path_to_root . "/includes/ui/ui_view_custom.inc"); 

global $systypes_array;
$order_name = 'inv_adjust_req';
$order_no = 0;

if(isset($_GET['OrderNo']) && $_GET['OrderNo'] > 0){
    unset($_SESSION[$order_name]);
    unset($_SESSION['movement_type']);
    $order_no = $_GET['OrderNo'];
    $sah = get_stock_adjustment_request_header_record($order_no);
    if($sah['type'] == ST_INVENTORY_RECEIPT_REQUEST){
        $page_security = 'SA_INVENTORY_RECEIPT_REQUEST';
        $_POST['Increase'] = 1;
        $_SESSION['movement_type'] = ST_INVENTORY_RECEIPT_REQUEST;
        $mv_type = ST_INVENTORY_RECEIPT_REQUEST;
    }else{
        $page_security = 'SA_INVENTORY_ISSUE_REQUEST';
        $_POST['Increase'] = 0;
        $_SESSION['movement_type'] = ST_INVENTORY_ISSUE_REQUEST;
        $mv_type = ST_INVENTORY_ISSUE_REQUEST;
    }
}

if(isset($_GET['Positive'])){
    if($_GET['Positive'] == 1){
        $page_security = 'SA_INVENTORY_RECEIPT_REQUEST';
        $_POST['Increase'] = 1;
        $_SESSION['movement_type'] = ST_INVENTORY_RECEIPT_REQUEST;
        $mv_type = ST_INVENTORY_RECEIPT_REQUEST;
    }else{
        $page_security = 'SA_INVENTORY_ISSUE_REQUEST';
        $_POST['Increase'] = 0;
        $_SESSION['movement_type'] = ST_INVENTORY_ISSUE_REQUEST;
        $mv_type = ST_INVENTORY_ISSUE_REQUEST;
    }
}

if(isset($_GET['Positive'])){
    unset($_SESSION[$order_name]);
}

if(!($order_no > 0)){
    if(isset($_SESSION[$order_name]->trans_no) && $_SESSION[$order_name]->trans_no > 0){
        $order_no = $_SESSION[$order_name]->trans_no;
    }
}

$js = "";
$js .= get_js_open_window(800, 500);
$js .= get_js_date_picker();

if($_SESSION['movement_type'] == ST_INVENTORY_RECEIPT_REQUEST){
    $page_security = 'SA_INVENTORY_RECEIPT_REQUEST';
    page(_($help_context = "Inventory Receipt Request"), false, false, "", $js);
}elseif($_SESSION['movement_type'] == ST_INVENTORY_ISSUE_REQUEST){
    $page_security = 'SA_INVENTORY_ISSUE_REQUEST';
    page(_($help_context = "Inventory Issue Request"), false, false, "", $js);
}else{
    $page_security = '';
    page(_($help_context = "Unknown Type"), false, false, "", $js);
}

//-----------------------------------------------------------------------------------------------

check_db_has_costable_items(_("There are no inventory items defined in the system which can be adjusted (Purchased or Manufactured)."));

check_db_has_movement_types(_("There are no inventory movement types defined in the system. Please define at least one inventory adjustment type."));

//-----------------------------------------------------------------------------------------------

if(isset($_GET['AddedID'])){
    global $systypes_array;
    
    $trans_no = $_GET['AddedID'];
    $sarh = get_stock_adjustment_request_header($trans_no);
    $trans_type = $sarh['type'];
    $approval_status = $sarh['approval_status'];

    $message = get_session_variable('ST_INV_ADJ_REQ_messages');
    clear_session_variable('ST_INV_ADJ_REQ_messages');

    if($trans_type == ST_INVENTORY_RECEIPT_REQUEST){
        $type_name = $systypes_array[ST_INVENTORY_RECEIPT_REQUEST];
        $positive = 1;
    }elseif($trans_type == ST_INVENTORY_ISSUE_REQUEST){
        $type_name = $systypes_array[ST_INVENTORY_ISSUE_REQUEST];
        $positive = 0;
    }else{
        $type_name = "Unknown type";
    }

    display_notification_centered($message);
    display_note(get_trans_view_str($trans_type, $trans_no, _("&View this $type_name")), 0, 1);
    
    if ($approval_status == AS_APPROVED) {
        hyperlink_params("$path_to_root/inventory/process_inventory_adjustments.php", _("Issue Items"), "trans_no=$trans_no");
    }
    
    display_note(print_document_link($trans_no, _("&Print This $type_name"), true, $trans_type), 0, 0);
    hyperlink_no_params($_SERVER['PHP_SELF']."?Positive=$positive", _("Enter &Another $type_name"));
    hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
    display_footer_exit();
}

//-----------------------------------------------------------------------------

if(isset($_POST['Update_Release'])){
    handle_release_inventory_adjustment_request($order_name);
} 

if(isset($_POST['Back_To_Originator'])){
    send_inventory_adjustment_request_back_to_originator($order_name);    
}

if(isset($_POST['Reject'])){
    reject_inventory_adjustment_request($order_name);    
}

//-----------------------------------------------------------------------------------------------

$id = find_submit('Delete');
if($id != -1){
    handle_delete_inventory_adjustment_request_item($order_name, $id, $_SESSION['movement_type']);
}

if(isset($_POST['AddItem'])){
    $order_no = handle_new_inventory_adjustment_request_item($order_name);
}
    
if(isset($_POST['UpdateItem'])){
    handle_update_inventory_adjustment_request_item($order_name);
}

if(isset($_POST['Save_Header'])){
    handle_update_inventory_adjustment_request_header($order_name, true);
}

if(isset($_POST['CancelItemChanges'])){
    $id = find_submit("Edit");
    unset($_POST["Edit$id"]);
}

//-----------------------------------------------------------------------------------------------
if($order_no > 0){
    if(isset($_SESSION[$order_name]->trans_no) && $order_no != $_SESSION[$order_name]->trans_no){
        handle_load_existing_order($order_name, $order_no);
    }elseif(!(isset($_SESSION[$order_name]))){
        handle_load_existing_order($order_name, $order_no);
    }else{
        handle_load_existing_order($order_name, $order_no);
    }
}elseif(isset($_GET['Positive']) && !isset($_SESSION[$order_name])){
    handle_new_order($order_name);
}

//-----------------------------------------------------------------------------------------------

    start_form();
        hidden("guid",substr(md5(microtime()),rand(0,26),15));
        unset($msgss);

        if($order_no > 0){
            $msgss = $systypes_array[$_SESSION['movement_type']] ." #".$order_no;
        }else{
            $msgss = "New ".$systypes_array[$_SESSION['movement_type']];
        }
        br();
        
        display_inventory_adjustment_request_header($_SESSION[$order_name], $msgss);
        br(1);
        
        if(isset($_SESSION[$order_name]->trans_no) && $_SESSION[$order_name]->trans_no > 0) {
            submit_center_first('Save_Header', 'Save', '', 'default');
            br(2);
    //        display_inventory_adjustment_request_cost_update_message($_SESSION['movement_type'], $_POST['project_id']);
    //        br(1);

            submit_center_first('Update_Release', 'Release for Approval', '', 'default');
            submit('Back_To_Originator', "Back To Originator", true, '', 'default');
            submit_center_last('Reject', "Reject", '', 'default');
            br(1);
        }
//            submit_center_first('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
            
        start_outer_table(TABLESTYLE, "width=98%", 10);
            display_inventory_adjustment_request_items(_("Adjustment Items"), $_SESSION[$order_name]);
            adjustment_request_options_controls($_SESSION[$order_name]);
        end_outer_table(1, false);

            
        br(2);
    end_form();
end_page();

?>