<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_INVENTORY_ADJ_REQUESTS_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_items_adjustment_inquiry_db.inc");
include_once($path_to_root . "/includes/common_functions.inc");
include_once($path_to_root . "/inventory/includes/db/movement_types_db.inc");
include_once($path_to_root . "/includes/ui/ui_view.inc");

$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 = "Approve Inventory Adjustment Requests 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'])));
}


function id($row){ 
    return get_inventory_trans_view_str($row['adjustment_type'], $row['id']);
//    return $row['id'];
}

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

function type($row){
    global $systypes_array;
    return $systypes_array[$row['type']];
}

function increase($row){
    return $row['increase'];
}

function comment($row){
    return $row['comments'];
}

function movement_type($row){
//    $mvt = get_movement_type_id($row['movement_type']);
    return $row['movement_type'];
}

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

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

function sample_item($row){
    return $row['sample_items'];
}

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

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

function edit_link($row){
    
    if(!($row['approval_status'] == AS_PROCESSED || $row['approval_status'] == AS_APPROVED)){
        $modify = "OrderNo";
//        if($row['approval_status'] == AS_APPROVED)
//        {
            return pager_link( _("Edit"), "/inventory/inventory_adjustment_request.php?$modify=" . $row['id'], ICON_EDIT);
            
//        }else{
//            return null;
//        }
//    
    }else{
        return null;
    }

  
}

function edit_link4($row){
    
    if(!($row['approval_status'] == AS_PROCESSED)){
        $modify = "OrderNo";
        if($row['approval_status'] == AS_APPROVED){
            if($row['adjustment_type'] == ST_INVENTORY_ISSUE_REQUEST){
                return pager_link( _("Edit"), "/inventory/process_inventory_adjustments.php?" . SID . "trans_no=" . $row["id"], ICON_DOC);
            }else{
                return pager_link( _("Edit"), "/inventory/process_inventory_adjustments.php?" . SID . "trans_no=" . $row["id"], ICON_RECEIVE);
            }
        }else{
            return null;
        }
    }else{
        return null;
    }
}



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

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

br(1);

//start_table(TABLESTYLE_NOBORDER);
//    start_row();
//        ref_cells(text_bold("Request #:"), 'id', '',null, '', true);
//        ref_cells(text_bold("Reference:"), 'reference', '',null, '', true);
//        date_cells("From Date:", 'from_date', '', null, -30);
//        date_cells("To Date:", 'to_date', '', null, +30);
//        
//    end_row();
//end_table();
//
//start_table(TABLESTYLE_NOBORDER);
//    start_row();
//        
//        locations_list_cells(_("Location:"), 'receiving_location', null, true, true);
//        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_approve_stock_adjustment_request_inquiry($_POST['from_date'], $_POST['to_date'], $_POST['receiving_location'], $_POST['reference'], $_POST['project_id'], $_POST['id']);  

$current_user_id = $_SESSION['wa_current_user']->user;
$sql = get_sql_for_approve_stock_adjustment_request_inquiry($current_user_id);  
        
	$cols = array(  _("ID #") => array('fun'=>'id', 'align'=>'center'), 
                        _("Ref") => array('fun'=>'reference', 'align'=>'left'), 
                        _("Type") => array('fun'=>'type', 'align'=>'left'), 
                        
                        _("Movement Type") => array('fun'=>'movement_type', 'align'=>'left'), 
                        _("Location") => array('fun'=>'receiving_location'), 
                        _("Project") => array('fun'=>'project_name'),      
//                        _("Sample Items") => array('fun'=>'sample_item', 'align'=>'center'),      
                        _("Date") => array('fun'=>'ai_date', 'align'=>'center'),
//                        _("Comment") => array('fun'=>'comment', 'align'=>'left'), 
                        _("Status") => array('fun'=>'approval_status', 'align'=>'center'));

	array_append($cols, array(
            array('insert'=>true, 'fun'=>'edit_link', 'align'=>'center'),
            array('insert'=>true, 'fun'=>'edit_link4', 'align'=>'center'),
		));


$table =& new_db_pager('orders_tbl', $sql, $cols);
$table->set_marker('check_overdue', _("Marked items are overdue."));

$table->width = "96%";

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


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



?>

