<?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_issues_inquiry_db.inc");

include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/includes/common_functions.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(800, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();
page(_($help_context = "Material Issues Inquiry"), 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($_GET['project_id']))
{
	$_POST['project_id'] = $_GET['project_id'];
}
if (isset($_GET['from_date']))
{
	$_POST['from_date'] = $_GET['from_date'];
}
if (isset($_GET['to_date']))
{
	$_POST['to_date'] = $_GET['to_date'];
}

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

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 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 im_no($row)
{ 
    return get_inventory_trans_view_str(ST_MATERIAL_ISSUE, $row['im_no']);
}

function reference($row)
{
    return $row['reference'];

}

function issuing_location($row)
{
    return $row['issuing_location'];

}

function receiving_location($row)
{
    return $row['location_name'];

}

function project_name($row)
{
    return $row['project_description'];

}


function rm_date($row)
{
    return sql2date($row['im_date']);

}

function source_mr_no($row)
{
    return get_inventory_trans_view_str(ST_MATERIAL_REQ, $row['source_mr_no']);

}


function company_name($row)
{
    return $row['company_name'];

}

function show_employee_name($row)
{
    if(strlen(($row['employee_code'] > 0))){
        return get_group_company_employee_formatted_long_name($row['company_code'], $row['employee_code']);
    }
    else{
        return;
    }
}

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

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();
        date_cells("From Date:", 'from_date', '', null, -30);
        date_cells("To Date:", 'to_date', '', null, +30);
        locations_list_cells(_("Receiving Location:"), 'receiving_location', null, true, true);
    end_row();
end_table();

start_table(TABLESTYLE_NOBORDER);
    start_row();
        ref_cells(_("Material Requisition #:"), 'mr_no', '',null, '', false);
        ref_cells(_("Material Issue #:"), 'im_no', '',null, '', false);
        project_list_cells(_("Project:"), 'project_id', null, _('Select a project'), true);
        submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
    end_row();
end_table();
//---------------------------------------------------------------------------------------------

$sql = get_sql_for_material_issues_inquiry($_POST['from_date'], $_POST['to_date'], $_POST['receiving_location'], 
                                           $_POST['mr_no'], $_POST['im_no'], $_POST['project_id']); 
        
	$cols = array(
		_("MI #") => array('fun'=>'im_no', 'align'=>'center'), 
		_("Ref") => array('fun'=>'reference', 'align'=>'right'), 
        _("Location") => array('fun'=>'receiving_location'), 
        _("Project") => array('fun'=>'project_name'),      
		_("Date") => array('fun'=>'rm_date'),  
		_("Company") => array('fun'=>'company_name'),
        _("Employee") => array('fun'=>'show_employee_name'),
        _("Source MR#") => array('fun'=>'source_mr_no', 'align' => 'center'));

	array_append($cols, array(
//        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 = "70%";

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


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



?>
