<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_DAILY_TOOL_ISSUES_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 . "/assets/includes/db/daily_tool_issue_db.inc");
include_once($path_to_root . "/sales/includes/sales_ui.inc");

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

page(_($help_context = "Daily Tool Issues Inquiry"), false, false, "", $js);

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

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

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

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

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

function receiving_location($row){
    return $row['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_NEW, AS_WITH_THE_APPROVER, AS_AWAITING_APPROVAL, AS_BACK_TO_ORIGINATOR))) {
        $name_value = "ModifyIssue=".$row['trans_no'];
        
        return pager_link( _("Edit"), "/assets/daily_tool_issue.php?$name_value", ICON_EDIT);
    } else {
        return null;
    }
}

function edit_link_2($row){
    if ($row['approval_status'] == AS_APPROVED) {
        $name_value = "dti_trans_no=".$row['trans_no'];
        
        return pager_link( _("Edit"), "/assets/daily_tool_return.php?$name_value", ICON_CREDIT);
    } else {
        return null;
    }
}

global $systypes_array;

start_form();

    start_table(TABLESTYLE_NOBORDER);
        start_row();
            ref_cells(text_bold("Trans No #:"), '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, +30);
        end_row();
    end_table();

    start_table(TABLESTYLE_NOBORDER);
        start_row();
            locations_list_cells(_("Location:"), 'loc_code', 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_daily_tool_issues_inquiry($_POST['trans_no'], $_POST['reference'], $_POST['project_id'], $_POST['from_date'], $_POST['to_date'], 
                                             $_POST['loc_code']);  
        
	$cols = array(  _("Trans #") => array('fun'=>'_trans_no', 'align'=>'center'), 
                        _("Ref") => array('fun'=>'reference', 'align'=>'left'), 
                        _("Location") => array('fun'=>'receiving_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'),
            array('insert'=>true, 'fun'=>'edit_link_2', 'align'=>'center'),
		));

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

    $table->width = "90%";

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


end_form(3);
end_page();

?>