<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_MATERIAL_TRANS_REQ_VIEW';
$path_to_root = "../..";

include_once($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/sales/includes/sales_ui.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/inventory/includes/db/material_requisition_transfers_db.inc");
include_once($path_to_root . "/inventory/includes/db/material_transfer_requisitions_db.inc");



if (!@$_GET['popup'])
{
	$js = "";
	if ($SysPrefs->use_popup_windows)
		$js .= get_js_open_window(900, 600);
	if (user_use_date_picker())
		$js .= get_js_date_picker();
	page("Manage Material Transfer Requisitions", false, false, "", $js);
}
if (isset($_GET['Approves']))
{
	$_SESSION['current_user_for_mtr'] = $_SESSION['wa_current_user']->user;
}
if (isset($_GET['selected_customer']))
{
	$selected_customer = $_GET['selected_customer'];
}
elseif (isset($_POST['selected_customer']))
{
	$selected_customer = $_POST['selected_customer'];
}
else
	$selected_customer = -1;

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

//	Query format functions
//
function check_overdue($row)
{
	global $trans_type;
	if ($trans_type == ST_SALESQUOTE)
		return (date1_greater_date2(Today(), sql2date($row['delivery_date'])));
	else
		return (date1_greater_date2(Today(), sql2date($row['mtr_required_date'])));
}

function view_link($dummy, $order_no)
{
	global $trans_type;
	return get_inventory_trans_view_str(ST_TRANSFERREQ, $order_no);
}

function prt_link($row)
{
	global $trans_type;
	return print_document_link($row['order_no'], _("Print"), true, $trans_type, ICON_PRINT);
}

function edit_link($row) 
{
	if (@$_GET['popup'])
		return '';
    
    if($row['approval_status'] == AS_NEW || $row['approval_status'] == AS_WITH_THE_APPROVER || $row['approval_status'] == AS_AWAITING_APPROVAL){
        $modify = "ModifyMRNumber";
        return pager_link( _("Edit"),"/inventory/material_transfer_requisitions.php?$modify=" . $row['mtr_no'], ICON_EDIT);
    }
    else{
        return null;
    }
}

function dispatch_link($row)
{
        if($row['approval_status'] == AS_APPROVED || $row['approval_status'] == AS_TRANSFERRED){
            return pager_link( _("Dispatch"),"/inventory/material_requisition_transfers.php?mtr_no=" .$row['mtr_no'], ICON_DOC);
        }
        else{
            return null;
        }
        
}

function status_name($row)
{
	global $approval_statuses;
    
    return $approval_statuses[$row["approval_status"]];
}


function format_created_date($row)
{
  	return sql2date($row['created_on']);
}

function format_required_by_date($row)
{
  	return sql2date($row['mtr_required_date']);
}


function view_approval_status($row)
{
    global $approval_statuses;
    
    return $approval_statuses[$row['approval_status']];
}


$id = find_submit('_chgtpl');
if ($id != -1)
	change_tpl_flag($id);

if (isset($_POST['Update']) && isset($_POST['last'])) {
	foreach($_POST['last'] as $id => $value)
		if ($value != check_value('chgtpl'.$id))
			change_tpl_flag($id);
}

//$show_dates = !in_array($_POST['order_view_mode'], array('OutstandingOnly', 'InvoiceTemplates', 'DeliveryTemplates'));
//---------------------------------------------------------------------------------------------
//	Order range form
//
if (get_post('_OrderNumber_changed') || get_post('_OrderReference_changed')) // enable/disable selection controls
{
	$disable = get_post('OrderNumber') !== '' || get_post('OrderReference') !== '';

  	if ($show_dates) {
			$Ajax->addDisable(true, 'OrdersAfterDate', $disable);
			$Ajax->addDisable(true, 'OrdersToDate', $disable);
	}

	$Ajax->activate('orders_tbl');
}

if (!@$_GET['popup'])
	start_form();

start_table(TABLESTYLE_NOBORDER);
    start_row();
        ref_cells(_("MTR #:"), 'mtr_no', '',null, '', false);
        ref_cells(_("MTR Ref"), 'mtr_reference', '',null, '', false);
        date_cells(_("Required By From:"), 'required_by_from', '', null, -15);
        date_cells(_("Required By To:"), 'required_by_to', '', null, +15);        

    end_row();

end_table();

start_table(TABLESTYLE_NOBORDER);
    start_row();
        locations_list_cells(_("Requested Location:"), 'requested_location', null, true, true);
        locations_list_cells(_("Issuing Location:"), 'issuing_location', null, true, true);
        
        $values_array[-1] = "";
        $values_array[AS_APPROVED] = $approval_statuses[AS_APPROVED];
        $values_array[AS_TRANSFERRED] = $approval_statuses[AS_TRANSFERRED];
        $values_array[AS_COMPLETED] = $approval_statuses[AS_COMPLETED];
        $values_array[AS_CLOSED] = $approval_statuses[AS_CLOSED];
        $values_array[AS_AWAITING_APPROVAL] = $approval_statuses[AS_AWAITING_APPROVAL];
        $values_array[AS_WITH_THE_APPROVER] = $approval_statuses[AS_WITH_THE_APPROVER];
        $values_array[AS_BACK_TO_ORIGINATOR] = $approval_statuses[AS_BACK_TO_ORIGINATOR];
        $values_array[AS_REJECTED] = $approval_statuses[AS_REJECTED];
        asort($values_array);
        custom_values_list_cells("Status:", 'filtering_status', $values_array, null, false, true);
        submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
    end_row();
end_table();

//---------------------------------------------------------------------------------------------
//	Orders inquiry table
//
$sql = get_sql_for_mtr_inquiry(@$_POST['required_by_from'], @$_POST['required_by_to'], 
            @$_POST['requested_location'],  @$_POST['issuing_location'],
            @$_POST['mtr_no'],  @$_POST['mtr_reference'],  $_SESSION['current_user_for_mtr'],  @$_POST['filtering_status']);


	$cols = array(
		_("MTR #") => array('fun'=>'view_link', 'align'=>'center'), 
		_("Ref"),
		_("Req. Loc."),
		_("Issu. Loc"), 
		_("Project"),
		_("Required By")=> array('fun'=>'format_required_by_date', 'align' => 'center'), 
		_("Originator") ,
        _("Status") => array('fun'=>'status_name', 'align' => 'center'), 
        _("Created On")=> array('fun'=>'format_created_date', 'align' => 'center'));

	array_append($cols, array(
		array('insert'=>true, 'fun'=>'dispatch_link'),
		array('insert'=>true, 'fun'=>'edit_link')));


$table =& new_db_pager('orders_tbl', $sql, $cols);
$table->set_marker('check_overdue', _("Marked items are overdue."));

$table->width = "90%";

display_db_pager($table);
submit_center('Update', _("Update"), true, '', null);

if (!@$_GET['popup'])
{
	end_form();
	end_page();
}

?>