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

$page_security = 'SA_UTILITY_BILL_INQ';
if (!@$_GET['popup'])
	$path_to_root = "../..";
else	
	$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 . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/gl/includes/db/utility_bill_inquiry_db.inc");


$js = '';

if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}

page($help_context = "Utility Bills Inquiry", false, false, "", $js);
//---------------------------------------------------------------------------------------------

simple_page_mode(true);

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

if (isset($_GET['project_id']))
{
	$_POST['project_id'] = $_GET['project_id'];
}
if (isset($_GET['from_date']))
{
	$_POST['from_date'] = $_GET['from_date'];
}
if (isset($_GET['to_date']))
{
	$_POST['to_date'] = $_GET['to_date'];
}

function id($row)
{
    return get_journal_trans_view_str(ST_UTILITY_BILLS, $row['id']);
}

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

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

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


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

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

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

function _taxes($row) {
    return number_format($row['taxes'], 2);
}

function _updated_on($row) {
    if($row['updated_on'] == '1970-01-01 00:00:00'){
        return ($row['created_on']);
    }else{
        return ($row['updated_on']);        
    }
}

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

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


function edit_link($row) 
{
    if($row['approval_status'] == AS_NEW || $row['approval_status'] == AS_WITH_THE_APPROVER || $row['approval_status'] == AS_AWAITING_APPROVAL){
        $modify = "UBNo";           
        return pager_link(_("Edit"), "/gl/utility_bills.php?$modify=". $row['id'], ICON_EDIT);
    }
    else{
        return null;
    }
}

//function issue_link($row)
//{  
//    return pager_link(_("Issue"),"/transport/asset_issue.php?ARNo=".$row['id'], ICON_DOC);    		
//}

//function return_link($row)
//{
//    return pager_link(_("Return"),"/transport/asset_return.php?ARNo=".$row['id'], ICON_GL);   
//}
//--------------------------------------------------------------------------------------------------


if ($Mode == 'RESET')
{
	$selected_id = -1;
}

if (isset($_POST['_selected_id_update']))
{
	$selected_id = $_POST['selected_id'];
	//$Ajax->activate('_page_body');
}

//if (list_updated('employee_code')) 
	$Ajax->activate('_page_body');
//--------------------------------------------------------------------------------------------------

start_form();

    start_table();
        start_row();
            ref_cells(text_bold(_("UB #:")), 'ub_no', '',null, '', false);
            value_list_cells('Entry Type:', 'entry_type_id', 'UTILITY_BILL_ENTRY_TYPES', null, '', true);
            ref_cells(_("UB Ref"), 'ub_reference', '',null, '', false);

            date_cells("From Date:", 'from_date', '', null, -30);
            date_cells("To Date:", 'to_date', '', null, +15);
        end_row();  
    end_table();

    start_table();
        start_row();
            project_list_cells(_("Project:"), 'project_id', null, _('Select a project'), true);        
            supplier_list_cells(_("Supplier:"), 'supplier_id', null, '', true);
            
            $tmp_approval_statuses_array = array();
            $tmp_approval_statuses_array[-1] = '';
            $tmp_approval_statuses_array[AS_NEW] = $document_approval_statuses[AS_NEW];
            $tmp_approval_statuses_array[AS_AWAITING_APPROVAL] = $document_approval_statuses[AS_AWAITING_APPROVAL];
            $tmp_approval_statuses_array[AS_APPROVED] = $document_approval_statuses[AS_APPROVED];
            $tmp_approval_statuses_array[AS_BACK_TO_ORIGINATOR] = $document_approval_statuses[AS_BACK_TO_ORIGINATOR];
            $tmp_approval_statuses_array[AS_REJECTED] = $document_approval_statuses[AS_REJECTED];
            custom_values_list_cells('Approval Status:', 'approval_status', $tmp_approval_statuses_array, null, false, true);

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

    end_table(1);

//---------------------------------------------------------------------------------------------

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

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

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


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

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

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


    $sql = get_sql_for_utility_bill_inquiry($ub_no, $ub_reference, $project_id, $supplier_id, $from_date, $to_date, $_POST['entry_type_id'], $_POST['approval_status']);    
    
    $cols = array(
                _("ID")=> array('fun'=>'id', 'align' => 'center'), 
                _("Entry Type")=> array('fun'=>'_entry_type_name'),                         
                _("Reference")=> array('fun'=>'reference'),                         
                _("Date")=> array('fun'=>'tran_date', 'align' => 'center'), 
                _("Project Name")=>array('fun'=>'project_name'),                
                _("Supplier")=>array('fun'=>'supp_name'), 
                _("Expenses")=> array('fun'=>'_expenses', 'align' => 'right'),
                _("Taxes")=> array('fun'=>'_taxes', 'align' => 'right'),
                _("Total")=> array('fun'=>'_total', 'align' => 'right'),
                _("Approval Status")=> array('fun'=>'approval_status', 'align' => 'center'), 
                _("Updated On")=> array('fun'=>'_updated_on', '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);

br();

if (!@$_GET['popup'])
{
    end_form();
    end_page();
}	


?>
