<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PERSONAL_MATERIAL_REQ_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 . "/inventory/includes/db/job_material_requisitions_db.inc");
include_once($path_to_root . "/sales/includes/sales_ui.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc"); 

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

page(_($help_context = $systypes_array[ST_PERSONAL_MATERIAL_REQ]."s Inquiry"), 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 _reference($row){
    return $row['reference'];
}

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

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

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

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

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

function _created_from($row){
    global $systypes_array;
    if ($row['source_type'] > 0) {
        return $systypes_array[$row['source_type']];
    }
    return null;
}

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

function _edit_link($row){
    global $systypes_array; 
    
    if (in_array($row['approval_status'], array(AS_APPROVED))) {
        $pmr_no = "pmr_no=".$row['id'];
        return pager_link( _($systypes_array[ST_PERSONAL_MATERIAL_TRANSFER]), "/inventory/personal_material_transfers.php?$pmr_no", ICON_DOC);
    } elseif (in_array($row['approval_status'], array(AS_NEW, AS_WITH_THE_APPROVER, AS_AWAITING_APPROVAL, AS_BACK_TO_ORIGINATOR))) {
        $name_value = "ModifyPMR_No=".$row['id'];
        return pager_link( _("Edit"), "/inventory/personal_material_requisitions.php?$name_value", ICON_EDIT);
    } 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("Reference:", 'reference', '',null, '', true);
            date_cells("From Date:", 'from_date', '', null, -30);
            date_cells("To Date:", 'to_date', '', null, +0);
        end_row();
    end_table();

    start_table(TABLESTYLE_NOBORDER);
        start_row();
            value_list_cells("Department:", "department_id", "DEPARTMENTS", null, "", true);
            project_list_cells(_("Project:"), 'project_id', null, _('Select a project'), true);
            locations_list_cells(_("From Location:"), 'loc_code', null, "", true);
            
            $values_array[-1] = "";
            $values_array[AS_NEW] = $approval_statuses[AS_NEW];
            $values_array[AS_AWAITING_APPROVAL] = $approval_statuses[AS_AWAITING_APPROVAL];
            $values_array[AS_APPROVED] = $approval_statuses[AS_APPROVED];
            $values_array[AS_COMPLETED] = $approval_statuses[AS_COMPLETED];
            $values_array[AS_PROCESSED] = $approval_statuses[AS_PROCESSED];
            $values_array[AS_BACK_TO_ORIGINATOR] = $approval_statuses[AS_BACK_TO_ORIGINATOR];
            $values_array[AS_REJECTED] = $approval_statuses[AS_REJECTED];
            custom_values_list_cells("Approval Status:", "ap_status", $values_array, null, false, true);
            submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
        end_row();    
    end_table();
    
    $sql = get_sql_for_job_material_requisitions_inquiry($_POST['trans_no'], 0, $_POST['reference'], $_POST['project_id'], $_POST['from_date'], $_POST['to_date'], 
                                                         $_POST['loc_code'], $_POST['ap_status'], type: ST_PERSONAL_MATERIAL_REQ, department_id: $_POST['department_id']);  
        
	$cols = array(  _("Trans #") => array('fun'=>'_trans_no', 'align'=>'center'), 
                        _("Ref") => array('fun'=>'_reference', 'align'=>'left'), 
                        _("Date") => array('fun'=>'_tran_date', 'align'=>'center'),
                        _("Department") => array('fun'=>'_department'), 
                        _("From Location") => array('fun'=>'_from_location'), 
                        _("To Location") => array('fun'=>'_to_location'), 
                        _("Project") => array('fun'=>'_project_name'),      
                        _("Approval Status") => array('fun'=>'_approval_status', 'align'=>'center'), 
                        _("Edit/ Transfer") => array('fun'=>'_edit_link', 'align'=>'center'));

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

    $table->width = "95%";

    display_db_pager($table);

end_form(2);
end_page();

?>