<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_MATERIAL_REQ';
$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 . "/includes/approval_functions.inc");
include_once($path_to_root . "/includes/project_cost_functions.inc");

include_once($path_to_root . "/inventory/includes/material_requisitions_ui.inc");
include_once($path_to_root . "/inventory/includes/mr_validations.inc");
include_once($path_to_root . "/inventory/includes/db/material_requisitions_db.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/purchasing/includes/pr_validations.inc");
include_once($path_to_root . "/payroll/includes/db/employees_db.inc");
include_once($path_to_root . "/projects/includes/db/project_sites_db.inc");
include_once($path_to_root . "/inventory/includes/db/alternate_units_db.inc");
include_once($path_to_root . "/assets/includes/db/assets_db.inc");


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

page(_($help_context = "Material Requisition"), 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 (get_post('cancel')) {
    
    if($_SESSION['mr_items']->approval_status == AS_APPROVED || $_SESSION['mr_items']->approval_status == AS_REJECTED)
    {
        $Ajax->activate('_page_body');
    }
    else
    {
        $_SESSION['mr_items']->clear_items();
        unset ($_SESSION['mr_items']);
        clear_data();
        set_focus('requesting_location');
        $Ajax->activate('_page_body');
    }
}
if (isset($_GET['NewRequisition']))
{
    if(isset($_SESSION['mr_items'])){
        $_SESSION['mr_items']->clear_items();
        unset ($_SESSION['mr_items']);
        clear_data();
        set_focus('requesting_location');
        $Ajax->activate('_page_body');
    }
    
}        
if (isset($_GET['ModifyMRNumber']) && is_numeric($_GET['ModifyMRNumber'])) 
{
    $help_context = 'Modifying Material Requisition';
    $_SESSION['page_title'] = sprintf( _("Modifying Material Requisition # %d"), $_GET['ModifyMRNumber']);
    
    	if (isset($_SESSION['mr_items']))
	{
		$_SESSION['mr_items']->clear_items();
		unset ($_SESSION['mr_items']);
	}

    $_SESSION['mr_items'] = new items_cart(ST_MATERIAL_REQ);
    
    populate_cart_from_database($_GET['ModifyMRNumber']);
}        
        
        //-----------------------------------------------------------------------------------------------


function clear_data()
{
    unset ($_POST['requesting_location']);
    unset ($_POST['issuing_location']);
    unset ($_POST['mr_reference']);
    unset ($_POST['mr_date']);
    
    unset ($_POST['stock_id']);
    unset ($_POST['line_comment']);
    
}


function populate_cart_from_database($mr_id)
{
    read_mr($mr_id, $_SESSION['mr_items']);
}

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

        
if (isset($_GET['AddedID'])) 
{
	//return;
        
    $trans_no = $_GET['AddedID'];
	$trans_type = ST_MATERIAL_REQ;

    $msg = get_session_variable('mr_messages');
        
	display_notification($msg);
	
    display_note(get_trans_view_str($trans_type, $trans_no, _("&View this Material Requisition")));
    
    hyperlink_params("$path_to_root/inventory/material_requisitions.php", _("Edit this Material Requisition"), "ModifyMRNumber=$trans_no");
        
//    br();

//	display_note(print_document_link($trans_no, _("&Print this Material Requisition"), true, $trans_type), 0, 0);

	hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter &another Material Requisition"));

//	hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
        
    clear_session_variable('mr_messages');

	display_footer_exit();
}
//--------------------------------------------------------------------------------------------------

function line_start_focus() {
  global 	$Ajax;

  $Ajax->activate('items_table');
  set_focus('_stock_id_edit');
}
//-----------------------------------------------------------------------------------------------

function handle_new_order()
{
	if (isset($_SESSION['mr_items']))
	{
		$_SESSION['mr_items']->clear_items();
		unset ($_SESSION['mr_items']);
	}

    $_SESSION['mr_items'] = new items_cart(ST_MATERIAL_REQ);
    $_POST['mr_date'] = new_doc_date();
//	if (!is_date_in_fiscalyear($_POST['mr_date']))
//		$_POST['mr_date'] = end_fiscalyear();
	$_SESSION['mr_items']->tran_date = $_POST['mr_date'];
        $_SESSION['mr_items']->next_approval_group_id = 0;
}


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

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

    send_mr_back_to_originator();
        
} 


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

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

    reject_mr();
        
} 


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

if (isset($_POST['Update_MR'])){
    
    if(can_process_mr()){
        update_mr();
    }
//   	meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
} 


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

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

    if(can_process_mr())
    {
        update_mr(true);
    }

        
        //return;
        
//   	meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
} 
if (isset($_POST['Add_MR'])){

    if(can_process_mr()){
        add_mr();
    }
        
//   	meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
} 


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

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

     if(can_process_mr()){
        add_mr(true);
    }
        
//   	meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
}
function end_processing($trans_no)
{
        //return;
 	$_SESSION['mr_items']->clear_items();
        //return;
	unset($_SESSION['mr_items']);         
        
   	meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
}






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

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

if (isset($_POST['UpdateItem']))
{
        handle_update_item();
}
if (isset($_POST['CancelItemChanges'])) {
	line_start_focus();
}
//-----------------------------------------------------------------------------------------------

if (isset($_GET['NewAdjustment']) || !isset($_SESSION['mr_items']))
{
	handle_new_order();
}

//-----------------------------------------------------------------------------------------------
start_form();

display_mr_header($_SESSION['mr_items']);

start_outer_table(TABLESTYLE, "width=90%", 10);

$Ajax->activate('mr_items_table');
div_start('mr_items_table');
display_mr_items(_("Material Requisition Items"), $_SESSION['mr_items']);
div_end();

mr_options_controls($_SESSION['mr_items']);

end_outer_table(1, false);


if((isset($_SESSION['mr_items'])) && ($_SESSION['mr_items']->order_id > 0))
{
    submit_center_first('Update_MR', _("Update"), '', 'default');
    submit('Update_Release', 'Release for Approval', true, '', 'default');
    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
    
    br();
    submit_center_first('Back_To_Originator', "Back To Originator", '', 'default');
    submit_center_last('Reject_MR', "Reject", '', 'default');

    
}
else
{
    submit_center_first('Add_MR', _("Save Requisition"), '', 'default');
    submit('Add_Release', 'Save and Release for Approval', true, '', 'default');
    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
}




end_form(3);
end_page();

?>
