<?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");
include_once($path_to_root . "/transport/includes/db/vehicles_db.inc");
include_once($path_to_root . "/assets/includes/db/assets_db.inc");
include_once($path_to_root . "/admin/db/list_values_db.inc");
include_once($path_to_root . "/gl/includes/db/utility_bills_db.inc");

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

page(_($help_context = "View Utility Bills"), true, false, "", $js);


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);
br(1);
start_table(TABLESTYLE2, "width=60%");

start_row();
label_cells("Entry Type", $myrow['entry_type_name'], "class='tableheader2'");
end_row();

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();

label_cells(_("Due Date"), sql2date($myrow['due_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(_("Expenses Total"), number_format($myrow['total'],2), "class='tableheader2'");
end_row();

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

label_cells(_("Staxes"), number_format($myrow['staxes'],2), "class='tableheader2'");
end_row();

label_cells(_("Total"), text_bold(number_format($myrow['total'] + $myrow['taxes'],2)), "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);


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

global $expense_categories, $daily_item_types_array; 

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

display_heading("Details");

$result = view_utility_bill_details($utility_bill_id);

$th = array(_("Line #"),_("Bill Ref."), _('Item Type'), _('Item'), _('Unit'), _('Qty'), _('Unit Price'), _('Amount'), _('Expense Category'), _('Expense Item'), _("Budeget"), _("Expense Type"), 
            _("Job"));  
                          

    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['id'], "align=center");
        label_cell($row_b['bill_reference']);
        label_cell($daily_item_types_array[$row_b["item_type"]]);
        if (in_array($row_b["item_type"], array(NSIT_STOCK_ITEM, NSIT_NON_STOCK_ITEM))) {
            label_cell($row_b["bill_no"].' - '.$row_b["description"]);
        } else {
            label_cell(nl2br($row_b["description"]));
        }
        label_cell($row_b["unit_name"]);
        label_cell(number_format($row_b["tran_qty"], 2), "align=right");
        label_cell(number_format($row_b["tran_unit_price"], 2), "align=right");
        label_cell(number_format($row_b["amount"], 2), "align=right");

        $line_total = $row_b['amount'];
        if($row_b["expense_category_id"] > 0){
            label_cell($expense_categories[$row_b["expense_category_id"]]);
        }
        else{
            label_cell('');
        }

        $expense_item_name = '';
        switch ($row_b['expense_category_id']) {
            case EC_PETTY_CASH_EXPENSE:
                $expense_item_name = '';
                break;
            case EC_STOCK_ITEM_RECEIPT:
                $link = get_petty_cash_item_receive_str(ST_I_OWE_YOU_BATCH, $batch_no, $row_b["id"], "Receive items");
                $expense_item_name = $link;
                break;
            case EC_VEHICLE_MAINTENANCE:
                $expense_item_name = get_vehicle_formatted_name($row_b["expense_item_id"]);
                break;
            case EC_UTILITY_ITEM:
                $utility_item = get_listvalue_by_id($row_b["expense_item_id"]);
                $expense_item_name = $utility_item['value_name'];
                break;
            case EC_ASSET_MAINTENANCE:
                $expense_item_name = get_asset_formatted_name($row_b["expense_item_id"]);
                break;

            default:
                break;
        }
                
        label_cell($expense_item_name);  
                
        label_cell($row_b['task_description']);
        label_cell($row_b['expense_description']);
        label_cell($row_b['project_site_description']);
    
    end_row();
    
    $utility_bill_total += $line_total;
    $k++;
}

start_row();
label_cell(text_bold("Total"), "colspan=7 align=right");
label_cell(text_bold(number_format($utility_bill_total, 2)), "align=right");
label_cell("", "colspan=7");
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 = $staxes_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();
    $k++;
    
    if ($row_b['suspended'] == 0) {
        $taxes_total += $line_tax_amount;
    } else {
        $staxes_total += $line_tax_amount;
    }
}

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

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


br(2);

global $systypes_array;
start_table(TABLESTYLE2, "width=90%");

    display_heading("Utility Bill Stock Receipts");
        $th_2 = array(_("Trans #"), _('Type'), _("Reference"), _('Tran Date'), _("Project Name"), _('Supplier'), _('Total'));  
        table_header($th_2);
        
        $k2 = 0;
        $result_2 = get_utility_bill_stock_receipts_by_source(ST_UTILITY_BILLS, $utility_bill_id);
            while($row_2 = db_fetch($result_2)){

                alt_table_row_color($k2);

                    label_cell(get_journal_trans_view_str($row_2['type'], $row_2['trans_no']), "align=center");
                    label_cell($systypes_array[$row_2['type']]);
                    label_cell($row_2['reference']);
                    label_cell(sql2date($row_2['date']));
                    label_cell(get_project_formatted_name($row_2['project_id']));
                    label_cell(get_supplier_formatted_name($row_2['supplier_id']));
                    label_cell(number_format($row_2['total'], 2), "align=right");

                end_row();
            }

end_table(2);

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

?>