<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_MATERIAL_REQ_VIEW';
$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/material_requisitions_db.inc");
include_once($path_to_root . "/inventory/includes/db/material_requisitions_inquiry_db.inc");
include_once($path_to_root . "/inventory/includes/db/outstanding_material_requisitions_inquiry_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 Material Requisitions", false, false, "", $js);
//}

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(1000, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();
page(_($help_context = "Outstanding Material Requisitions"), false, false, "", $js);

if (isset($_GET['Approves']))
{
	$_SESSION['current_user_for_mr'] = $_SESSION['wa_current_user']->user;
}

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)
{
	global $trans_type;
	return get_trans_view_str(ST_MATERIAL_REQ, $row['mr_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";
//    if($row['approval_status'] == AS_NEW || $row['approval_status'] == AS_WITH_THE_APPROVER || $row['approval_status'] == AS_AWAITING_APPROVAL)
//    {
//        return pager_link( _("Edit"), "/inventory/material_requisitions.php?$modify=" . $row['mr_no'], ICON_EDIT);
//    }
//    else
//    {
//        return null;
//    }
//    
//}

function dispatch_link($row)
{
    if($row['approval_status'] == AS_APPROVED || $row['approval_status'] == AS_ISSUED)
    {
        return pager_link( _("Issue"),"/inventory/issue_materials.php?mr_no=" .$row['mr_no']);
    }
    elseif($row['approval_status'] == AS_COMPLETED)
    {
        return pager_link( _("---"),"/inventory/issue_materials.php?mr_no=" .$row['mr_no']);
    }
    else 
    {
        return null;
    }
  		
}


//function return_link($row)
//{
//    if($row['approval_status'] == AS_APPROVED || $row['approval_status'] == AS_ISSUED || $row['approval_status'] == AS_COMPLETED)
//    {
//        return pager_link( _("Return"),"/inventory/return_materials.php?mr_no=" .$row['mr_no']);
//    }
//    else 
//    {
//        return null;
//    }
//  		
//}


function status_name($row)
{
    global $approval_statuses;
    
    return $approval_statuses[$row["approval_status"]];
}


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

function order_link($row)
{
    if($row['approval_status'] == AS_APPROVED || $row['approval_status'] == AS_ISSUED || $row['approval_status'] == AS_COMPLETED)
    {
        if($row['closed_for_prs']){
            return pager_link(_('---'), "/inventory/material_requisition_pr.php?mr_no=" .$row['mr_no']);
        }
        else{
            return pager_link( _("Purchase"),"/inventory/material_requisition_pr.php?mr_no=" .$row['mr_no']);
        }
    }
    else 
    {
        return null;
    }
}

//function convert_created_date($row)
//{
//  return sql2date($row['created_on']);
//}


function approved_on($row)
{
  return substr($row['approved_on'], 0, 16);
}


function convert_required_date($row)
{
  return sql2date($row['mr_required_date']);
}

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('SearchOrders')) 
{
	$Ajax->activate('orders_tbl');
}

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(_("MR #:"), 'mr_no', '',null, '', false);
        ref_cells(_("MR Ref"), 'mr_reference', '',null, '', false);

        date_cells(_("Approved From:"), 'required_by_from', '', null, -30);
        date_cells(_("Approved To:"), 'required_by_to', '', null, +1);
    end_row();
end_table();

start_table(TABLESTYLE_NOBORDER);
    start_row();
        project_list_cells("Project", 'project_id',null, '');
        system_users_list_cells("Originator", 'originator',null, '');
        locations_list_cells(_("Issuing Location:"), 'issuing_location', null, true, true);
        submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
    end_row();
end_table();

//---------------------------------------------------------------------------------------------
//	Orders inquiry table
//



$sql = get_sql_for_outstanding_material_requisitions_inquiry(@$_POST['required_by_from'], @$_POST['required_by_to'], 
//            @$_POST['created_on_from'], @$_POST['created_on_to'],  
            @$_POST['issuing_location'],
            @$_POST['mr_no'], @$_POST['mr_reference'], @$_POST['mr_complete'], 
            @$_POST['project_id'],@$_POST['originator'],
            @$selected_stock_item );


	$cols = array(
		_("MR #") => array('fun'=>'view_link'), 
		_("Ref"),
                _("Issu. Loc"),
                _("Project"),     
		_("Required By") => array('fun'=>'convert_required_date', 'align' => 'center'),
		_("Originator"),
		_("Status") => array('fun'=>'status_name', 'align' => 'center'), 
		_("Approved On") => array('fun'=>'approved_on', 'align' => 'center'));

	array_append($cols, array(
       // array('insert'=>true, 'fun'=>'edit_link'),
        array('insert'=>true, 'fun'=>'order_link', 'align' => 'center'), 
		array('insert'=>true, 'fun'=>'dispatch_link', 'align' => 'center'), 
       // array('insert'=>true, 'fun'=>'return_link', 'align' => 'center'), 
		));


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




?>
