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

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/gl/includes/db/utility_bills_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"); 

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

page(_($help_context = "View Utility Bill Stock Receipt"), true, false, "", $js);

if ($_GET['trans_no'] != "") {
    $trans_no = $_GET['trans_no'];
}
if ($_GET['type'] != "") {
    $type = $_GET['type'];
}

$row = get_utility_bill_stock_receipt($type, $trans_no);

$project_id = $row['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 Stock Receipt Trans No: " . $trans_no);

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

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

label_cells(_("Project"), get_project_formatted_name($row['project_id']), "class='tableheader2'");
end_row();

label_cells(_("Supplier"), get_supplier_formatted_name($row['supplier_id']), "class='tableheader2'");
end_row();

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

label_cells(_("Due Date"), sql2date($row['due_date']), "class='tableheader2'");
end_row();

if($row['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"), $row['tax_group_name'], "class='tableheader2'");
end_row();

label_cells(_("Expenses Total"), number_format($row['total'],2), "class='tableheader2'");
end_row();

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

label_cells(_("Total"), text_bold(number_format($row['total'] + $row['taxes'],2)), "class='tableheader2'");
end_row();

start_row();
    label_cells("Source #", $row['source_trans_no'] > 0 ? get_journal_trans_view_str($row['source_type'], $row['source_trans_no']) : '', "class='tableheader2'");
end_row();

end_table(2);


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

global $expense_categories, $daily_item_types_array; 

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

display_heading("Details");

$result = get_utility_bill_stock_receipt_details($type, $trans_no);

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

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

        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["stock_id"].' - '.$row_b["description"]);
                } else {
                    label_cell(nl2br($row_b["description"]));
                }
                label_cell($row_b["unit_name"]);
                label_cell(number_format($row_b["tran_qty"], 4), "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);

end_page(true, false, false, ST_UTILITY_BILL_STOCK_RECEIPT, $trans_no);

?>