<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PURCH_REQ_ISSUE_INQ';
$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 . "/purchasing/includes/ui/purchasing_ui.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/purchasing/includes/db/pr_issues_inquiry_db.inc");


if (!@$_GET['popup'])
{
	$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 = "Purchase Requisition Issues Inquiry"), false, false, "", $js);
}

if(isset($_GET['project_id'])){
    $_POST['project_id'] = $_GET['project_id'];
}

if(isset($_GET['FromDate'])){
    $_POST['OrdersAfterDate'] = $_GET['FromDate'];
}

if(isset($_GET['ToDate'])){
    $_POST['OrdersToDate'] = $_GET['ToDate'];
}


if (isset($_GET['order_number']))
{
	$order_number = $_GET['order_number'];
}

//-----------------------------------------------------------------------------------
// Ajax updates
//
if (isset($_GET['Approves']))
{
	$_SESSION['current_user_for_pr'] = $_SESSION['wa_current_user']->user;
}
if (get_post('SearchOrders')) 
{
	$Ajax->activate('orders_tbl');
} elseif (get_post('_order_number_changed')) 
{
	$disable = get_post('order_number') !== '';

	$Ajax->addDisable(true, 'OrdersAfterDate', $disable);
	$Ajax->addDisable(true, 'OrdersToDate', $disable);
	$Ajax->addDisable(true, 'StockLocation', $disable);
	$Ajax->addDisable(true, '_SelectStockFromList_edit', $disable);
	$Ajax->addDisable(true, 'SelectStockFromList', $disable);

	if ($disable) {
		$Ajax->addFocus(true, 'order_number');
	} else
		$Ajax->addFocus(true, 'OrdersAfterDate');

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

function pri_no($trans)
{
    return get_trans_view_str(ST_PR_ISSUE, $trans["pri_no"]);
}


function source_pr_no($trans)
{
    return get_trans_view_str(ST_PURCH_REQ, $trans["source_pr_no"]);
}


function po_number($trans)
{
    if($trans["po_no"] > 0){
           return get_trans_view_str(ST_PURCHORDER, $trans["po_no"]);
    }    
      
}

//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)
//    {
//        return pager_link( _("Edit"),"/purchasing/purchase_requisitions.php?ModifyPrNumber=" . $row["pr_no"], ICON_EDIT);
//    }
//    else{
//                return null;
//    }
//  	
//}

//function dispatch_link($row)
//{
//    if(($row['approval_status'] == AS_APPROVED || $row['approval_status'] == AS_ISSUED) && $row['pr_complete'] == 0)
//    {
//        return pager_link( _("Dispatch"),"/purchasing/pr_issues_select_po.php?pr_no=".$row['pr_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["pri_date"]);
}

function return_link($row) {
    
    if($row['status'] == AS_NEW){
        return pager_link( _("Return"),"/purchasing/purchase_requisition_returns.php?pri_no=".$row['pri_no']);
    }
    else{
        return null;
    }
}

//function pr_complete_name($row)
//{
//    return get_yes_no_name($row['pr_complete']);
//}

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


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

start_table(TABLESTYLE_NOBORDER);
start_row();
ref_cells(_("PRI#:"), 'pri_no', '',null, '', true);
ref_cells(_("PR#:"), 'pr_no', '',null, '', true);
date_cells(_("from:"), 'OrdersAfterDate', '', null, -30);
date_cells(_("to:"), 'OrdersToDate', '', null, +1);

end_row();
end_table();

start_table(TABLESTYLE_NOBORDER);
start_row();

locations_list_cells(_("Location:"), 'StockLocation', null, '', false);
project_list_cells("Project:", "project_id", null, '');
user_list_cells("Originator", 'originator_id', null, '');

//stock_items_list_cells(_("for item:"), 'SelectStockFromList', null, true);

submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
end_row();
end_table(1);

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

$pri_number= 0;
if (isset($_POST['pri_no'])){
	$pri_number = $_POST['pri_no'];
}

$pr_number= 0;
if (isset($_POST['pr_no'])){
	$pr_number = $_POST['pr_no'];
}

$date_after = '';
if (isset($_POST['OrdersAfterDate'])){
	$date_after = $_POST['OrdersAfterDate'];
}

$date_before = '';
if (isset($_POST['OrdersToDate'])){
	$date_before = $_POST['OrdersToDate'];
}


$project_id = '';
if (isset($_POST['project_id'])){
	$project_id = $_POST['project_id'];
}


$location = '';
if (isset($_POST['StockLocation'])){
	$location = $_POST['StockLocation'];
}


$originator_id = '';
if (isset($_POST['originator_id'])){
	$originator_id = $_POST['originator_id'];
}


$sql = get_sql_for_pr_issues($date_after, $date_before, $pri_number, $pr_number,
                                $location, $project_id, $originator_id);

$cols = array(
            _("PRI#") => array('fun'=>'pri_no', 'ord'=>'', 'align'=>'center'),                        
            _("Reference"), 
            _("PR#") => array('fun'=>'source_pr_no', 'ord'=>'', 'align'=>'center'),   
            _("Location"),
            _("Project"),
            _("Date")=> array('fun'=>'format_required_by_date', 'align' => 'center'), 
            _("Issued By") ,
            _("PO#")=> array('fun'=>'po_number', 'align'=>'center'), 
            _("")=> array('insert'=>true, 'fun'=>'return_link', 'align' => 'center')

);

if (get_post('StockLocation') != $all_items) {
	$cols[_("Location")] = 'skip';
}
//---------------------------------------------------------------------------------------------------

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

$table->width = "70%";

display_db_pager($table);

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


?>
