<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PURCH_REQ_ALLOC_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/purchase_requisitions_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 Allocation Inquiry"), false, false, "", $js);
}
if (isset($_GET['order_number']))
{
	$order_number = $_GET['order_number'];
}




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



//-----------------------------------------------------------------------------------
// 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');
}
//---------------------------------------------------------------------------------------------

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

start_table(TABLESTYLE_NOBORDER);
start_row();
ref_cells(_("PR No:"), 'pr_no', '',null, '', true);
ref_cells(_("PO No:"), 'po_no', '',null, '', true);
date_cells(_("From:"), 'from_date', '', null, -30);
date_cells(_("To:"), 'to_date', '', null, +1);
project_list_cells(_("Project:"), 'project_id', null, true);
supplier_list_cells(_("Supplier:"), 'supplier', null, true);
end_row();
end_table();

start_table(TABLESTYLE_NOBORDER);
start_row();

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

submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
end_row();
end_table(1);
//---------------------------------------------------------------------------------------------
if (isset($_POST['pr_no']))
{
	$requisition_number = $_POST['pr_no'];
}
if (isset($_POST['from_date']))
{
	$req_date_from = $_POST['from_date'];
}
if (isset($_POST['to_date']))
{
	$req_date_to = $_POST['to_date'];
}
if (isset($_POST['project_id']))
{
	$project = $_POST['project_id'];
}
if (isset($_POST['supplier']))
{
	$supplier = $_POST['supplier'];
}
if (isset($_POST['po_no']))
{
	$po_no = $_POST['po_no'];
}
//if (isset($_POST['SelectStockFromList']) &&	($_POST['SelectStockFromList'] != "") &&
//	($_POST['SelectStockFromList'] != ALL_TEXT))
//{
// 	$selected_stock_item = $_POST['SelectStockFromList'];
//}
//else
//{
//	unset($selected_stock_item);
//}

//---------------------------------------------------------------------------------------------
function trans_view($trans)
{
	return get_trans_view_str(ST_PURCH_REQ, $trans["pr_no"]);
}
function po_view($trans)
{
	return get_trans_view_str(ST_PURCHORDER, $trans["order_no"]);
}
function indent_view($trans)
{
	return get_supplier_trans_view_str(ST_PR_DETAIL_INDENTS, $trans["pr_line_id"], $trans['requested_unit_qty']);
}
//function ord_qty($trans)
//{
//	return get_purchase_requisition_trans_view_str(ST_PR_DETAIL_ALLOCATIONS,$trans["pr_line_id"], $trans['requested_unit_qty']);
//   
//}

function format_created_date($row)
{

    return sql2date($row["created_on"]);
}

function format_required_by_date($row)
{

    return sql2date($row["required_date"]);
}

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

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

$sql = get_sql_for_purchase_allocation_requisitions($requisition_number, date2sql($req_date_from),date2sql($req_date_to),$project,$supplier,$po_no);

 $cols  = array(_("PR#")  => array('fun'=>'trans_view', 'ord'=>''),
                _("Project"),
                _("Location"),
                _("Item"),
                _("Unit"),
                _("Req. Qty") => array('fun'=>'indent_view', 'align' => 'center'), 
                _("Required Date") => array('fun'=>'format_required_by_date', 'align' => 'center'), 
                _("Supplier"),
                _("Ord. Location"),
                _("Ord. Unit"), 
                _("Ord. Qty"),
                _("Ord. Price"),
                _("Ord. Dis."),
                _("PO No.") => array('fun'=>'po_view', 'ord'=>'', 'align' => 'center')
     );

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

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

$table->width = "90%";

display_db_pager($table);

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