<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_OUTSIDE_MATERIAL_RECEIPT';
$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 . "/inventory/includes/db/outside_material_receipts_db.inc");

$js = "";
$js .= get_js_open_window(800, 500);
$js .= get_js_date_picker();

global $systypes_array;

page(_($help_context = "Outstanding ".$systypes_array[ST_OUTSIDE_MATERIAL_RECEIPT]."s"), false, false, "", $js);

if (isset($_GET['trans_no'])) {
    $_POST['trans_no'] = $_GET['trans_no'];
}

function _trans_no($row){ 
    return get_inventory_trans_view_str($row['type'], $row['trans_no']);
}

function _req_trans_no($row){ 
    return get_inventory_trans_view_str($row['req_type'], $row['req_trans_no']);
}

function _jo_id($row){ 
    $jo_id = $row['jo_id'];
    return get_service_trans_view_str(ST_JOB_ORDER, $jo_id);
}

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

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

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

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

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

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

function _edit_link($row){
    if (in_array($row['approval_status'], array(AS_APPROVED))) {
        $jmr_id = "omrc_no=".$row['id'];
        return pager_link( _("Outsuide Material Return"), "/inventory/outside_material_returns.php?$jmr_id", ICON_DOC);
    } else {
        return null;
    }
}

global $systypes_array, $approval_statuses;

start_form();

    start_table(TABLESTYLE_NOBORDER);
        start_row();
            ref_cells(text_bold("Trans #:"), 'trans_no', '',null, '', true);
            ref_cells(text_bold("Req. Trans #:"), 'req_trans_no', '',null, '', true);
            ref_cells(text_bold("Job Card #:"), 'jo_id', '',null, '', true);
            ref_cells("Reference:", 'reference', '',null, '', true);
            date_cells("From Date:", 'from_date', '', null, -30);
            date_cells("To Date:", 'to_date', '', null);
        end_row();
    end_table();

    start_table(TABLESTYLE_NOBORDER);
        start_row();
            text_cells(_("From Location:"), 'from_location', null, 20, 200);
            locations_list_cells(_("To Location:"), 'to_loc_code', null, '', true);
            project_list_cells(_("Project:"), 'project_id', null, _('Select a project'), true);

            submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
        end_row();    
    end_table();

    $_POST['from_location'] = isset($_POST['from_location']) ? $_POST['from_location'] : '';
    
    $sql = get_sql_for_outside_material_receipts_inquiry($_POST['trans_no'], $_POST['req_trans_no'], $_POST['jo_id'], $_POST['reference'], $_POST['project_id'], 
                                                         $_POST['from_date'], $_POST['to_date'], $_POST['from_location'], $_POST['to_loc_code'], AS_APPROVED);  
        
	$cols = array(  _("Trans #") => array('fun'=>'_trans_no', 'align'=>'center'),       
                        _("Req Trans #") => array('fun'=>'_req_trans_no', 'align'=>'center'), 
                        _("Job Card #") => array('fun'=>'_jo_id', 'align'=>'center'), 
                        _("Ref") => array('fun'=>'_reference', 'align'=>'left'), 
                        _("From Location") => array('fun'=>'_from_location'), 
                        _("To Location") => array('fun'=>'_to_location'), 
                        _("Project") => array('fun'=>'_project_name'),      
                        _("Date") => array('fun'=>'_tran_date', 'align'=>'center'),
                        _("Status") => array('fun'=>'_approval_status', 'align'=>'center'));

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

    $table =& new_db_pager('orders_tbl', $sql, $cols);

    $table->width = "90%";

    display_db_pager($table);

end_form(3);
end_page();

?>