<?php
/**********************************************************************
***********************************************************************/




function _type($row){
    global $systypes_array;
    return $systypes_array[$row['type']];
}
function _status($row){
    global $approval_statuses;
    return $approval_statuses[$row['approval_status']];
}
function _request_no($row){
//    atr_id
    if($row['type'] == ST_ASSET_TRANSFER){
        $atrh = get_asset_transfer_request_header($row['atr_id']);    
        return get_asset_trans_view_str(ST_ASSET_TRANSFER_REQUEST, $atrh['id']);        
    }
    elseif($row['type'] == ST_ASSET_REQUISITION_TRANSFER){
        return get_asset_trans_view_str(ST_ASSET_TRANSFER_REQUEST, $row['atr_id']);        
    }
    else{
        return get_asset_trans_view_str($row['req_type'], $row['req_trans_no']);
    }
}
function _type_no($row){
    return get_asset_trans_view_str($row['type'], $row['trans_no']);
}
function _req_id($row){
    return get_asset_trans_view_str(ST_ASSET_TRANSFER_REQUEST, $row['req_id']);
}
function _reference($row){
    return $row['reference'];     
}
function _from_loc($row){
    return $row['from_location_name'];     
}
function _to_loc($row){
    return $row['to_location_name'];     
}
function _company($row){
    return get_group_company_name($row['company_code']);   
}
function _employee_name($row){
    $employee_name = get_group_company_employee($row['company_code'], $row['employee_code']);
    return $employee_name['employee_code'].' - '.$employee_name['initials'].' '.$employee_name['last_name'];
}
function _comment($row){
    return $row['comment'];     
}
function _tran_date($row){
    return sql2date($row['tran_date']);     
}
function _total($row){
    return number_format($row['total'], 2);     
}

function _edit_link($row){  
    global $systypes_array;
    
    switch ($row['type']) {
        case ST_ASSET_TRANSFER:
            if ($row['approval_status'] != AS_APPROVED) {
                return pager_link($systypes_array[ST_ASSET_TRANSFER],"/assets/direct_asset_transfers.php?ATNo=".$row['id'], ICON_DOC);  
            }
            break;
            
        case ST_ASSET_REQUISITION_TRANSFER:
            if ($row['approval_status'] != AS_COMPLETED) {
                return pager_link(_("Asset Transfer Receipts"),"/assets/asset_transfer_receipts.php?ARTNo=".$row['id'], ICON_DOC);  
            } else {
                return '';
            }

        default:
            break;
    }
    
    return null;		
}

function asset_transfer_inquiry_filtering_parameters()
{
    
    $values_array = build_asset_transaction_types_array();
        
    start_table();
    start_row();
        ref_cells("#", "trans_no");        
        custom_values_list_cells("Type:", 'filter_type', $values_array, null, '', true);
        date_cells("From Date:", 'from_date', '', null, -30);
        date_cells("To Date:", 'to_date', '', null, 0);
        asset_locations_list_cells("From Location", 'from_location', null, "", true);
        asset_locations_list_cells("To Location", 'to_location', null, "", true);
        
        submit_cells('SearchOrders', _("Search"),'',_('Search'), 'default');
    end_row();  
end_table(1);
}

function build_asset_transaction_types_array()
{
    global $systypes_array;
    $values_array = array();
    
    $values_array[ST_ASSET_TRANSFER] = $systypes_array[ST_ASSET_TRANSFER];
    $values_array[ST_ASSET_REQUISITION_TRANSFER] = $systypes_array[ST_ASSET_REQUISITION_TRANSFER];
    $values_array[ST_ASSET_TRANSFER_RECEIPT] = $systypes_array[ST_ASSET_TRANSFER_RECEIPT]; 
    
    return $values_array;
}

function display_asset_transfer_inquiry($filter_type=-1, $complete='')
{
    $trans_no = 0;
    if(isset($_POST['trans_no'])){
      $trans_no = $_POST['trans_no'];
    }

    $from_date = date2sql($_POST['from_date']);
    $to_date = date2sql($_POST['to_date']);

    $from_location = "";
    if(isset($_POST['from_location'])){
      $from_location = $_POST['from_location'];
    }
    $to_location = "";
    if(isset($_POST['to_location'])){
      $to_location = $_POST['to_location'];
    }

    $sql = get_sql_for_asset_transfer_inquiry($trans_no, $from_date, $to_date, $from_location, $to_location, $complete, $filter_type);    

    $cols = array(
                _("Date")=>array('fun' => '_tran_date', 'align' => 'center'),    
                _("Type")=> array('fun' => '_type'),                           
                _("#")=> array('fun' => '_type_no', 'align' => 'center'),                           
                _("Source")=> array('fun' => '_request_no', 'align' => 'center'),                           
                _("Reference")=> array('fun' => '_reference'),                                          
                _("From Location")=> array('fun' => '_from_loc'),                        
                _("To Location")=> array('fun' => '_to_loc'),      
                _("Comment")=> array('fun' => '_comment'),
                _("Status")=> array('fun' => '_status', 'align' => 'center'));

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

    $table =& new_db_pager('orders_tbl', $sql, $cols, null, null);
    $table->width = "98%";
    display_db_pager($table);
}