<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_APPROVE_PURCH_REQUISITIONS';
$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_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 = "Approve Purchase Requisitions"), false, false, "", $js);
}
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');
}
//---------------------------------------------------------------------------------------------

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

//start_table(TABLESTYLE_NOBORDER);
//start_row();
//ref_cells(_("pr no:"), 'pr_no', '',null, '', true);
//date_cells(_("from:"), 'OrdersAfterDate', '', null, -30);
//date_cells(_("to:"), 'OrdersToDate', '', null, +1);
//
//locations_list_cells(_("into location:"), 'StockLocation', null, true);
//ref_cells(_("reference no:"), 'reference', '',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['reference']))
//{
//	$reference = $_POST['reference'];
//}
//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_supplier_trans_view_str(ST_PURCH_REQ, $trans["pr_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?" . SID 
                                . "ModifyPrNumber=" . $row["pr_no"], ICON_EDIT);
    }
    else{
                return null;
    }
  	
}

function dispatch_link($row)
{
    if($row['approval_status'] == AS_APPROVED || $row['approval_status'] == AS_ISSUED)
    {
        return pager_link( _("Dispatch"),"/purchasing/pr_issues.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["pr_date"]);
}


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

$current_user_id = $_SESSION['wa_current_user']->user;

$sql = get_sql_for_approve_purchase_requisition($current_user_id);

$cols = array(
		_("Pr No") => array('fun'=>'trans_view', 'ord'=>''), 
		_("Reference"), 
//		_("Supplier") => array('ord'=>''),
		_("Location"),
        _("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('insert'=>true, 'fun'=>'edit_link'),
);

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

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

$table->width = "80%";

display_db_pager($table);

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