<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_JOB_MATERIAL_ISSUE_RETURN_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_issues_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();

global $systypes_array;

page(_($help_context = $systypes_array[ST_JOB_MANUAL_COST]."s and ".$systypes_array[ST_JOB_MANUAL_COST_RETURN]."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 _parent_trans_no($row){ 
    return get_inventory_trans_view_str($row['parent_type'], $row['parent_trans_no']);
}

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

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 _location($row){
    return $row['location_name'];
}

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

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

function _total($row){
    return number_format($row['total'], 2);
}

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_NEW, AS_WITH_THE_APPROVER, AS_AWAITING_APPROVAL, AS_BACK_TO_ORIGINATOR))) {
        if ($row['type'] == ST_JOB_MANUAL_COST) { 
            $name_value = "ModifyJMC_No=".$row['id'];
            
            return pager_link( _("Edit"), "/inventory/job_manual_costs.php?$name_value", ICON_EDIT);
        } elseif ($row['type'] == ST_JOB_MANUAL_COST_RETURN) {
            $name_value = "jmcr_no=".$row['id'];
            
            return pager_link( _("Edit"), "/inventory/job_manual_cost_returns.php?$name_value", ICON_EDIT);
        } else {
            return null;
        }
    } elseif (in_array($row['approval_status'], array(AS_APPROVED))) {
        $name_value = "jmc_no=".$row['id'];
        
        if ($row['type'] == ST_JOB_MANUAL_COST) { 
            return pager_link( _("Return"), "/inventory/job_manual_cost_returns.php?$name_value", ICON_RECEIVE);
        } else {
            return null;
        } 
    } 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("JMC Trans #:"), 'parent_trans_no', '',null, '', true);
            $types_array[-1] = "";
            $types_array[ST_JOB_MANUAL_COST] = $systypes_array[ST_JOB_MANUAL_COST];
            $types_array[ST_JOB_MANUAL_COST_RETURN] = $systypes_array[ST_JOB_MANUAL_COST_RETURN];
            custom_values_list_cells(text_bold("Type:"), "type", $types_array, null, false, true);
            ref_cells(text_bold("Job Card #:"), 'jo_id', '',null, '', true);
            ref_cells("Reference:", 'reference', '',null, '', true);
        end_row();
    end_table();

    start_table(TABLESTYLE_NOBORDER);
        start_row();
            date_cells("From Date:", 'from_date', '', null, -30);
            date_cells("To Date:", 'to_date', '', null, 0);
            locations_list_cells(_("Location:"), 'loc_code', null, '', true);
            project_list_cells(_("Project:"), 'project_id', null, _('Select a project'), true);
            
            $ap_values_array[-1] = "";
            $ap_values_array[AS_NEW] = $approval_statuses[AS_NEW];
            $ap_values_array[AS_AWAITING_APPROVAL] = $approval_statuses[AS_AWAITING_APPROVAL];
            $ap_values_array[AS_APPROVED] = $approval_statuses[AS_APPROVED];
            $ap_values_array[AS_CLOSED] = $approval_statuses[AS_CLOSED];
            $ap_values_array[AS_PROCESSED] = $approval_statuses[AS_PROCESSED];
            $ap_values_array[AS_BACK_TO_ORIGINATOR] = $approval_statuses[AS_BACK_TO_ORIGINATOR];
            $ap_values_array[AS_REJECTED] = $approval_statuses[AS_REJECTED];
            custom_values_list_cells("Status:", "ap_status", $ap_values_array, null, false, true);
            
            submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
        end_row();    
    end_table();
    
    $jmc_types_array = array(ST_JOB_MANUAL_COST, ST_JOB_MANUAL_COST_RETURN);
    
    if ($_POST['type'] != -1) {
        $jmc_types_array = array($_POST['type']);
    }
    
    $sql = get_sql_for_job_material_issues_inquiry($_POST['trans_no'], $_POST['parent_trans_no'], $jmc_types_array, $_POST['jo_id'], $_POST['reference'], $_POST['project_id'], 
                                                   $_POST['from_date'], $_POST['to_date'], $_POST['loc_code'], $_POST['ap_status']);  
        
	$cols = array(  _("Trans No") => array('fun'=>'_trans_no', 'align'=>'center'), 
                        _("JMC Trans No") => array('fun'=>'_parent_trans_no', 'align'=>'center'), 
                        _("Type") => array('fun'=>'_type'), 
                        _("Job Card #") => array('fun'=>'_jo_id', 'align'=>'center'), 
                        _("Reference") => array('fun'=>'_reference', 'align'=>'left'), 
                        _("Location") => array('fun'=>'_location'), 
                        _("Project") => array('fun'=>'_project_name'),      
                        _("Date") => array('fun'=>'_tran_date', 'align'=>'center'),
                        _("Total") => array('fun'=>'_total', 'align'=>'right'),
                        _("Approval 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 = "92%";

    display_db_pager($table);

end_form(2);
end_page();

?>