<?php
/**********************************************************************
***********************************************************************/
//$page_security = 'SA_INVENTORY_RECEIPT';
$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_adjustments_ui.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");


$order_no = 0;

if (isset($_GET['OrderNo']) && $_GET['OrderNo'] > 0){
    $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;
    }
}

//else{
//    if ($_POST['Increase'] == 1){
//        $page_security = 'SA_INVENTORY_RECEIPT';
//        $_SESSION['movement_type'] = ST_INVENTORY_RECEIPT_REQUEST;
//        $mv_type = ST_INVENTORY_RECEIPT_REQUEST;
//    }
//    else{
//        $page_security = 'SA_INVENTORY_ISSUE';
//        $_SESSION['movement_type'] = ST_INVENTORY_ISSUE_REQUEST;
//        $mv_type = ST_INVENTORY_ISSUE_REQUEST;
//    }
//}


$order_name = 'inv_adjust_req';




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 = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
if (user_use_date_picker())
	$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'];
        
        $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);

	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['SaveInventoryAdjustmentRequest'])){
    $can_p_msg = can_commit_inventory_adjustment_request($order_name);
    
    if ($can_p_msg['success'] !== true){
        display_error($can_p_msg['message']);
        return false;
    }

//	$asa_msg = add_stock_adjustment($_SESSION[$order_name]->line_items,
//		$_POST['StockLocation'], $_POST['AdjDate'], $_POST['Increase'],
//		$_POST['ref'], $_POST['memo_'], $_POST['movement_type'], $_POST['project_id'], $_POST['task_id'], $_SESSION[$order_name]->sample_items, 
//        $_SESSION[$order_name]->trans_type, $_POST['linked_field_type'], $_POST['linked_field_id']);
        

    commit_stock_adjustment_request($order_name);
    
} /*end of process credit note */



if (isset($_POST['Update_Release'])){
    $can_p_msg = can_commit_inventory_adjustment_request($order_name);
    
    if ($can_p_msg['success'] !== true){
        display_error($can_p_msg['message']);
    }
    else{
        release_inventory_adjustment_request_for_approval($order_name);
    }
} /*end of process credit note */



if(isset($_POST['Back_To_Originator'])){
    $can_p_msg = can_commit_inventory_adjustment_request($order_name);
    
    if ($can_p_msg['success'] !== true){
        display_error($can_p_msg['message']);
    }
    else{
        send_inventory_adjustment_request_back_to_originator($_SESSION[$order_name]->trans_no);
    }
}



if(isset($_POST['Reject'])){
    $can_p_msg = can_commit_inventory_adjustment_request($order_name);
    
    if ($can_p_msg['success'] !== true){
        display_error($can_p_msg['message']);
    }
    else{
        reject_inventory_adjustment_request($_SESSION[$order_name]->trans_no);
    }
}


//-----------------------------------------------------------------------------------------------
$id = find_submit('Delete');
if ($id != -1)
	handle_delete_item($order_name, $id);

if (isset($_POST['AddItem']))
        handle_new_item($order_name);
    
if (isset($_POST['UpdateItem']))
	handle_update_item($order_name);

if (isset($_POST['CancelItemChanges'])) {
	line_start_focus();
}
//-----------------------------------------------------------------------------------------------
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['NewAdjustment']) && !isset($_SESSION[$order_name])){
}elseif (isset($_GET['Positive']) && !isset($_SESSION[$order_name])){

    handle_new_order($order_name);
}

//-----------------------------------------------------------------------------------------------
start_form();
hidden("guid",substr(md5(microtime()),rand(0,26),15));

display_inventory_adjustment_request_header($_SESSION[$order_name]);

//br(1);
display_inventory_adjustment_request_cost_update_message($_SESSION['movement_type'], $_POST['project_id']);
br(1);

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);

    submit_center_first('SaveInventoryAdjustmentRequest', _("Save Requisition"), '', 'default');
    submit('Update_Release', 'Release for Approval', true, '', 'default');
    br(2);
    submit_center_first('Back_To_Originator', "Back To Originator", '', 'default');
    submit_center_last('Reject', "Reject", '', 'default');
    br(1);
    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
br(1);
end_form();
end_page();

?>