<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_STOCK_RESERVATIONS_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 . "/sales/includes/sales_ui.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/inventory/includes/db/stock_reservations_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 Stock Reservations", false, false, "", $js);
}

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

//---------------------------------------------------------------------------------------------
//	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['sr_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['sr_complete'] == 0){
            return pager_link( _("Dispatch"),"/inventory/stock_reservation_releases.php?sr_no=" .$row['id'], 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['sr_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();

date_cells(_("SR Date From:"), 'SR_date_from', '', null, -15);
date_cells(_("SR Date To:"), 'SR_date_to', '', null, +15);

date_cells(_("Created On From:"), 'created_on from', '', null, -30);
date_cells(_("Created On To:"), 'created_on_to', '', null, +1);

end_row();
end_table();

start_table(TABLESTYLE_NOBORDER);
start_row();

locations_list_cells(_("Location:"), 'location', null, true, true);

ref_cells(_("SR #:"), 'sr_no', '',null, '', false);
ref_cells(_("SR Ref"), 'sr_reference', '',null, '', false);

end_row();
end_table();

start_table(TABLESTYLE_NOBORDER);
start_row();

stock_items_list_cells(_("Item:"), 'StockId', null, true, false);

yesno_list_cells("Completed :", 'sr_complete');

submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
//hidden('order_view_mode', $_POST['order_view_mode']);
//hidden('type', $trans_type);

end_row();

end_table(1);
//---------------------------------------------------------------------------------------------
//	Orders inquiry table
//
$sql = get_sql_for_stock_reservation_inquiry(@$_POST['SR_date_from'], @$_POST['SR_date_to'], 
            @$_POST['created_on from'], @$_POST['created_on_to'],  
            @$_POST['location'],
            @$_POST['sr_no'],  @$_POST['sr_reference'], @$_POST['sr_complete'], 
            @$_POST['StockId']);


	$cols = array(
		_("SR #") => array('fun'=>'view_link', 'align'=>'center'), 
		_("Ref"),
		_("Location"),
		_("Project"),
		_("SR Date")=> array('fun'=>'format_required_by_date', 'align' => 'center'), 
		_("Originator") ,
        _("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 = "80%";

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

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