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

$path_to_root = "..";
//include($path_to_root . "/purchasing/includes/po_class.inc");

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/purchasing/includes/ui/purchasing_ui.inc");
include_once($path_to_root . "/purchasing/includes/db/purchase_requisitions_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");
include_once($path_to_root . "/inventory/includes/process_inventory_adjustments_ui.inc");
include_once($path_to_root . "/includes/ui/ui_input.inc");
include_once($path_to_root . "/includes/approval_functions.inc");
//include_once($path_to_root . "/purchasing/includes/db/po_db.inc");

include_once($path_to_root . "/inventory/includes/db/inventory_adjustment_request_db.inc");
include_once($path_to_root . "/inventory/includes/db/adjustments_db.inc");
include_once($path_to_root . "/inventory/includes/inventory_adjustments_ui.inc");
include_once($path_to_root . "/includes/ui/items_cart.inc");
include_once($path_to_root . "/includes/db/inventory_db.inc");
include_once($path_to_root . "/includes/common_functions.inc");
include_once($path_to_root . "/inventory/includes/db/items_adjust_db.inc");
include_once($path_to_root . "/includes/project_cost_functions.inc");
include_once($path_to_root . "/includes/ui/ui_view.inc");
include_once($path_to_root . "/inventory/includes/db/movement_types_db.inc");
include_once($path_to_root . "/sales/includes/db/customers_db.inc");
include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc");
include_once($path_to_root . "/payroll/includes/db/employees_db.inc");



$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(900, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();

//page(_($help_context = "Process Inventory ".$page_name."Adjustment"), false, false, "", $js);






if (isset($_GET['trans_id'])){
    $trans_no = $_GET['trans_id'];
    $inv_adj = get_stock_adjustment_header_record($trans_no);
    $trans_type = $inv_adj['type'];
    if($inv_adj['type'] == ST_INVENTORY_RECEIPT){
        $page_security = 'SA_INVENTORY_RECEIPT';
        $page_name = 'Receipt ';
    }elseif($inv_adj['type'] == ST_INVENTORY_ISSUE){
        $page_security = 'SA_INVENTORY_ISSUE';
        $page_name = 'Issue ';
    }
}
else{
    if(isset($_GET['trans_no']) && $_GET['trans_no'] > 0){
        $_POST['trans_no'] = $_GET['trans_no'];            
    }elseif(isset($_POST['selected_order']) && $_POST['selected_order'] > 0){
        $_POST['trans_no'] = $_POST['selected_order'];
    }else{
        page(_($help_context = "Process Inventory Adjustment"), false, false, "", $js);
        die ("<br>" . _("This page must be called with an inventory adjustment request number."));
    }
    
    $row = get_stock_adjustment_request_header_record($_POST['trans_no']);
    if($row['type'] == ST_INVENTORY_RECEIPT_REQUEST){
        $page_name = 'Receipt ';
        $increase = true;
        $page_security = 'SA_INVENTORY_RECEIPT';
    }elseif($row['type'] == ST_INVENTORY_ISSUE_REQUEST){
        $page_name = 'Issue ';
        $increase = false;
        $page_security = 'SA_INVENTORY_ISSUE';
    }else{
        $page_name = '';
        $increase = '';
    }
}
    
page(_($help_context = "Process Inventory ".$page_name." Request"), false, false, "", $js);       
        

if (isset($_GET['trans_id'])){
    
    global $systypes_array;
    
        $trans_no = $_GET['trans_id'];
        $inv_adj = get_stock_adjustment_header_record($trans_no);
	$trans_type = $inv_adj['type'];
        $type_name = $systypes_array[$trans_type];
        
        $message = get_session_variable('ST_PINV_ADJ_messages');
        clear_session_variable('ST_PINV_ADJ_messages');
    
	display_notification_centered($message);
        
	display_note(get_trans_view_str($trans_type, $trans_no, _("&View this $type_name")), 0, 1);

//	display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL &Postings for 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();
	
}


$current_stock_adj = new items_cart($row['type'], $_POST['trans_no']);
$_SESSION['stock_adj'] = $current_stock_adj;
$stk_adj = $_SESSION['stock_adj'];
        

if (isset($_POST['ProcessInventoryAdjustment'])){

    $can_p_msg = can_process_inventory_adjustment_request($stk_adj);
    if ($can_p_msg['success'] !== true){
        display_error($can_p_msg['message']);
    }
    else{
        $stk_adj->tran_date = $_POST['in_trans_date'];
        $stk_adj->reference = $_POST['ref'];
        
        $msg = process_stock_adjustment_request($stk_adj->trans_no, $stk_adj->line_items, $stk_adj->location_name, ($stk_adj->tran_date), 
                                                $increase, $stk_adj->reference, $stk_adj->comment, $stk_adj->movement_type, $stk_adj->project_id, $task_id=0, 
                                                $stk_adj->sample_items, $stk_adj->trans_type, $stk_adj->row_state);
        if($msg['success'] !== true){
            display_error_cancel($msg['message']);  
        }
        else{

            $trans_no = $msg['tran_no'];

            meta_forward($_SERVER['PHP_SELF'], "trans_id=$trans_no");
        }
    }
}
    
            
     start_form();
        hidden('selected_order', $_POST['trans_no']);
            
        display_process_inventory_adjustments_summary($stk_adj);
        display_process_inventory_adjustments_options_controls($stk_adj);
        display_process_inventory_adjustments_details(_("Item Details"), $stk_adj);


        if (is_array($stk_adj->line_items)){
            submit_center_first('ProcessInventoryAdjustment', 'Process', '', 'default');
        }

    end_form();
    
end_page(true, false, false, ST_PURCHORDER, $_POST['trans_no']);

?>