<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PROJECT_COST_ALLOCATION_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 . "/projects/includes/db/material_requisition_transfers_db.inc");
include_once($path_to_root . "/projects/includes/db/project_cost_allocation_inquiry_db.inc");
include_once($path_to_root . "/projects/includes/db/tasks_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("Project Cost Allocation Inquiry", 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['mtr_required_date'])));
}

function view_link($row)
{
	return get_trans_view_str($row['tran_type'], $row['tran_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"),
    "/projects/material_requisitions.php?$modify=" . $row['mr_no'], ICON_EDIT);
}

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

function get_tran_type_name($row)
{
	global $systypes_array;
  		 $s = $systypes_array[$row["tran_type"]];
                 return $s;
}

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

function convert_date($row)
{
  return sql2date($row['updated_on']);
}

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

function view_budget_cost_allocations($row)
{
	return get_task_cost_allocation_details_view_str($row['task_id'], $row['task_id']);
}

$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();
ref_cells("Budject Id:", "budject_id", null, null, null, true);
end_row();

if(isset($_POST['budject_id']) && $_POST['budject_id'] > 0){
    $budget = get_task_record($_POST['budject_id']);
    $_POST['project_id'] = $budget['project_id'];
    $_POST['task_id'] = $budget['task_id'];
}

$Ajax->activate('project_id');
$Ajax->activate('task_id');

start_row();
project_list_cells(_("Project:"), 'project_id', null, _('Select a project'), true);
tasks_list_cells(_("Budget:"), 'task_id', $_POST['project_id'], null, _('Select a task'), true);
end_row();
end_table();

start_table(TABLESTYLE_NOBORDER);
start_row();
journal_types_list_cells(_("Type:"), "filterType");
date_cells(_("From:"), 'updated_on_from', '', null, -30);
date_cells(_("To:"), 'updated_on_to', '', null, +1);
submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
end_row();
end_table();

$sql = get_sql_for_pca_view(@$_POST['project_id'], @$_POST['task_id'], 
            @$_POST['filterType'],
            @$_POST['updated_on_from'], @$_POST['updated_on_to'], @$_POST['budject_id']);


	$cols = array(
                _("Id"), 
		_("BOQ Item Group"),  
                _("Budget Id") => array('fun'=>'view_budget_cost_allocations', 'align' => 'center'),
		_("Budget"),
		_("Tran Type") => array('fun'=>'get_tran_type_name'), 
		_("Tran No") => array('fun'=>'view_link'),
		_("Date") => array('fun'=>'convert_date', 'align' => 'center'),
		_("Amount") => array('align' => 'right'));

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