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

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


    if (isset($_GET['type'])) {
        $type = $_GET['type'];
    } elseif (isset($_POST['type'])) {
        $type = $_POST['type'];
    } 
    
global $systypes_array;
    
page($systypes_array[$type]." Inquiry", false, false, "", $js);

function _view_link($row) {
    return get_inventory_trans_view_str($row['type'], $row['mrt_no']);
}

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

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

function _dispatch_link($row) {
    if ($row['type'] == ST_REQTRANSFER && in_array($row['approval_status'], array(AS_NEW))) {
        return pager_link( _("Dispatch"),"/inventory/material_transfer_receipts.php?mrt_no=".$row['mrt_no'], ICON_RECEIVE);
    } else {
        return null;
    }
}

global $approval_statuses;

start_form();

    if (isset($_GET['type'])) {
        hidden('type', $_GET['type']);
    } elseif (isset($_POST['type'])) {
        hidden('type', $_POST['type']);
    }

    global $systypes_array;

    start_table(TABLESTYLE_NOBORDER);
        start_row();
            ref_cells(_("MRT #:"), 'mrt_no', '',null, '', false);
            ref_cells(_("MRT Ref"), 'mrt_reference', '',null, '', false);
            date_cells(_("From Date:"), 'from_date', '', null, -15);
            date_cells(_("To Date:"), 'to_date', '', null, +15);
        end_row();

        start_row();
            locations_list_cells(_("Receiving Location:"), 'receiving_location', null, true, true);
            locations_list_cells(_("Issuing Location:"), 'issuing_location', null, true, true);
            
            $values_array[-1] = "";
            $values_array[AS_NEW] = $approval_statuses[AS_NEW];
            $values_array[AS_COMPLETED] = $approval_statuses[AS_COMPLETED];
            $values_array[AS_CLOSED] = $approval_statuses[AS_CLOSED];
            custom_values_list_cells("Status:", 'filtering_status', $values_array, null, false, true);
            
            submit_cells('SearchOrders', _("Search"), '', _('Search'));
        end_row();
    end_table(2);

$sql = get_sql_for_mrt_inquiry($type, $_POST['from_date'], $_POST['to_date'], $_POST['receiving_location'], $_POST['issuing_location'], 
                               $_POST['mrt_no'], $_POST['mrt_reference'],  @$_POST['filtering_status']);

	$cols = array(
		_("MTR #") => array('fun'=>'_view_link', 'align'=>'center'), 
		_("Ref"),
		_("Issuing Location"), 
		_("Issuing Project"), 
		_("Receiving Location"),
		_("Created On") => array('fun'=>'_created_on', 'align'=>'center'),
                _("Comment"),
                _("Status")=> array('fun'=>'_status', 'align'=>'center')
		);

        if ($type == ST_REQTRANSFER) {
            array_append($cols, array( array('insert'=>true, 'fun'=>'_dispatch_link', 'align'=>'center')));
        }


$table =& new_db_pager('orders_tbl', $sql, $cols);

$table->width = "85%";

display_db_pager($table);


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

?>