<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_MANAGE_RAW_MATERIAL_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 . "/sales/includes/sales_ui.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/manufacturing/includes/db/material_issue_outs_db.inc");
include_once($path_to_root . "/manufacturing/includes/db/material_requisitions_db.inc");




//set_page_security( @$_POST['order_view_mode'],
//	array(	'OutstandingOnly' => 'SA_SALESDELIVERY',
//			'InvoiceTemplates' => 'SA_SALESINVOICE'),
//	array(	'OutstandingOnly' => 'SA_SALESDELIVERY',
//			'InvoiceTemplates' => 'SA_SALESINVOICE')
//);

//if (get_post('type'))
//	$trans_type = $_POST['type'];
//elseif (isset($_GET['type']) && $_GET['type'] == ST_SALESQUOTE)
//	$trans_type = ST_SALESQUOTE;
//else
//	$trans_type = ST_SALESORDER;
//
//if ($trans_type == ST_SALESORDER)
//{
//	if (isset($_GET['OutstandingOnly']) && ($_GET['OutstandingOnly'] == true))
//	{
//		$_POST['order_view_mode'] = 'OutstandingOnly';
//		$_SESSION['page_title'] = _($help_context = "Search Outstanding Sales Orders");
//	}
//	elseif (isset($_GET['InvoiceTemplates']) && ($_GET['InvoiceTemplates'] == true))
//	{
//		$_POST['order_view_mode'] = 'InvoiceTemplates';
//		$_SESSION['page_title'] = _($help_context = "Search Template for Invoicing");
//	}
//	elseif (isset($_GET['DeliveryTemplates']) && ($_GET['DeliveryTemplates'] == true))
//	{
//		$_POST['order_view_mode'] = 'DeliveryTemplates';
//		$_SESSION['page_title'] = _($help_context = "Select Template for Delivery");
//	}
//	elseif (!isset($_POST['order_view_mode']))
//	{
//		$_POST['order_view_mode'] = false;
//		$_SESSION['page_title'] = _($help_context = "Search All Material Requisitions");
//	}
//}
//else
//{
//	$_POST['order_view_mode'] = "Quotations";
//	$_SESSION['page_title'] = _($help_context = "Search All Sales Quotations");
//}

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 Raw Material Requisitions", false, false, "", $js);
}

if (isset($_GET['selected_customer']))
{
	$selected_customer = $_GET['selected_customer'];
}
elseif (isset($_POST['selected_customer']))
{
	$selected_customer = $_POST['selected_customer'];
}
else
	$selected_customer = -1;

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

if (isset($_POST['SelectStockFromList']) && ($_POST['SelectStockFromList'] != "") &&
	($_POST['SelectStockFromList'] != ALL_TEXT))
{
 	$selected_stock_item = $_POST['SelectStockFromList'];
}
else
{
	unset($selected_stock_item);
}
//---------------------------------------------------------------------------------------------
//	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['mr_required_date'])));
}

function view_link($dummy, $order_no)
{
	global $trans_type;
	return  get_customer_trans_view_str($trans_type, $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 '';
	global $trans_type;
	$modify = "ModifyMRNumber";
  return pager_link( _("Edit"),
    "/manufacturing/material_requisitions.php?$modify=" . $row['mr_no'], ICON_EDIT);
}

function dispatch_link($row)
{
  		return pager_link( _("Dispatch"),
			"/manufacturing/material_issue_outs.php?mr_no=" .$row['mr_no'], ICON_DOC);
}

function invoice_link($row)
{
	global $trans_type;
	if ($trans_type == ST_SALESORDER)
  		return pager_link( _("Invoice"),
			"/sales/sales_order_entry.php?NewInvoice=" .$row["order_no"], ICON_DOC);
	else
		return '';
}

function delivery_link($row)
{
  return pager_link( _("Delivery"),
	"/sales/sales_order_entry.php?NewDelivery=" .$row['order_no'], ICON_DOC);
}

function order_link($row)
{
  return pager_link( _("Sales Order"),
	"/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
}

function tmpl_checkbox($row)
{
	global $trans_type;
	if ($trans_type == ST_SALESQUOTE)
		return '';
	if (@$_GET['popup'])
		return '';
	$name = "chgtpl" .$row['order_no'];
	$value = $row['type'] ? 1:0;

// save also in hidden field for testing during 'Update'

 return checkbox(null, $name, $value, true,
 	_('Set this order as a template for direct deliveries/invoices'))
	. hidden('last['.$row['order_no'].']', $value, false);
}
//---------------------------------------------------------------------------------------------
// Update db record if respective checkbox value has changed.
//
function change_tpl_flag($id)
{
	global	$Ajax;
	
  	$sql = "UPDATE ".TB_PREF."sales_orders SET type = !type WHERE order_no=$id";

  	db_query($sql, "Can't change sales order type");
	$Ajax->activate('orders_tbl');
}


$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(_("Required By From:"), 'required_by_from', '', null, -15);
date_cells(_("Required By To:"), 'required_by_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(_("Requested Location:"), 'requested_location', null, true, true);
locations_list_cells(_("Issuing Location:"), 'issuing_location', null, true, true);

ref_cells(_("MR #:"), 'mr_no', '',null, '', false);
ref_cells(_("MR Ref"), 'mr_reference', '',null, '', false);

end_row();
end_table();

start_table(TABLESTYLE_NOBORDER);
start_row();

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

yesno_list_cells("Completed :", 'mr_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_mr_view(@$_POST['required_by_from'], @$_POST['required_by_to'], 
            @$_POST['created_on from'], @$_POST['created_on_to'],  
            @$_POST['requested_location'],  @$_POST['issuing_location'],
            @$_POST['mr_no'],  @$_POST['mr_reference'], @$_POST['mr_complete'], 
            @$selected_stock_item );


	$cols = array(
		_("MR #"),
		_("Ref"),
		_("Req. Loc."),
		_("Issu. Loc"), 
		_("Required By"),
		_("Comments"),
		_("Completed"),
		_("Requestor"), 
		_("MR Created On"));

	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();
}
?>
