<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_UTILITY_BILL_VIEW';
$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/gl/includes/db/utility_bill_inquiry_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
page(_($help_context = "View Utility Bills"), true, false, "", $js);

//-------------------------------------------------------------------------------------------------
$woid = 0;
if ($_GET['trans_no'] != "")
{
	$utility_bill_id = $_GET['trans_no'];
}

$myrow = view_utiltiy_bill($utility_bill_id);

$project_id = $myrow['project_id'];
$user_id = $_SESSION['wa_current_user']->user;
$cap = can_access_project($project_id, $user_id);
if($cap['success'] !== true) 
{
    display_error($cap["message"]);
    set_focus('project_id');
    display_footer_exit();
    exit;
}


display_heading("Utility Bill No: " . $utility_bill_id);

start_table(TABLESTYLE2, "width=60%");

start_row();
label_cells("Reference", $myrow['reference'], "class='tableheader2'");
end_row();

label_cells(_("Project"), $myrow['project_name'], "class='tableheader2'");
end_row();

label_cells(_("Supplier"), $myrow['supp_name'], "class='tableheader2'");
end_row();

label_cells(_("Date"), sql2date($myrow['date']), "class='tableheader2'");
end_row();

if($myrow['rate_include_taxes'] == 1){
    $rate_include_tax = "Yes";
}else{
    $rate_include_tax = "No";
}
label_cells(_("Rate Include Taxes"), $rate_include_tax, "class='tableheader2'");
end_row();

label_cells(_("Tax Group"), $myrow['tax_group_name'], "class='tableheader2'");
end_row();

label_cells(_("Taxes"), $myrow['taxes'], "class='tableheader2'");
end_row();

label_cells(_("Total"), $myrow['total'], "class='tableheader2'");
end_row();

$s = get_project_trans_view_str(ST_APPROVAL_LOG, $utility_bill_id."&source_type_id=".ST_UTILITY_BILLS, "Approval Status");
label_cells($s,$approval_statuses[$myrow['approval_status']], "class='tableheader2'");
end_row();

end_table(2);


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

start_table(TABLESTYLE2, "width=80%");

display_heading("Details");

$result = view_utility_bill_details($utility_bill_id);

$th = array(_("Bill NO"),_("Bill Reference"), _('Description'),_('Amount'),
            _("Period From"),_("Period To"),_("Budeget")/*,_("Expense Type")*/);  
                          

    table_header($th);
    $k = $utility_bill_total = 0; //row colour counter
   // $total = 0;

while($row_b = db_fetch($result)){
    
    alt_table_row_color($k);
     
    label_cell($row_b['bill_no']);
    label_cell($row_b['bill_reference']);
    label_cell($row_b['description']);
    
    $line_total = $row_b['amount'];
    label_cell(number_format($line_total, 2), "align=right");
    label_cell(sql2date($row_b['period_from']), "align=center");
    label_cell(sql2date($row_b['period_to']), "align=center");
    label_cell($row_b['task_description']);
//    label_cell($row_b['expense_description']);
    
    end_row();
    
    $utility_bill_total += $line_total;
    $k++;
}

start_row();
label_cell("Total", "colspan=3");
label_cell(number_format($utility_bill_total, 2), "align=right");
label_cell("", "colspan=4");
end_row();

end_table(2);

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

start_table(TABLESTYLE2, "width=30%");

display_heading("Taxes");

$result = view_utility_bill_taxes($utility_bill_id);

$th = array(_("Tax Type"),_('Amount'));  
                          

table_header($th);
$k = $taxes_total = 0; //row colour counter
   // $total = 0;

while($row_b = db_fetch($result)){
    
    alt_table_row_color($k);

    label_cell($row_b['tax_type_name']);
    $line_tax_amount = $row_b['amount'];
    label_cell(number_format($line_tax_amount, 2), "align=right");
    
    end_row();
    $taxes_total += $line_tax_amount;
    $k++;
}

start_row();
label_cell("Taxes Total");
label_cell(number_format($taxes_total, 2), "align=right");
end_table();

br(2);

$final_total = $myrow['total'] + $myrow['taxes'];
display_allocations_to(PT_SUPPLIER, $myrow['supplier_id'], ST_UTILITY_BILLS, $utility_bill_id, 
		($final_total));

end_page(true, false, false, ST_UTILITY_BILLS, $utility_bill_id);

?>
