<?php

function display_utility_expenses_summary_filtering_criteria() {
    start_table(TABLESTYLE_NOBORDER);
        start_row();
            value_list_cells('Entry Type:', 'entry_type_id', 'UTILITY_BILL_ENTRY_TYPES', null, '', true);
            date_cells(_("From:"), 'from_date', '', null, -30);
            date_cells(_("To:"), 'to_date');

            submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), 'default');
        end_row();
    end_table(2);
}

function display_utility_expenses_summary($from_date_, $to_date_, $entry_type_id=0) {
    global $approval_statuses;

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

        $th = array(_("Entry Type"), _("Tran. Date"), _("Due Date"), _("Project"), _("Supplier"), _("Trans#"), _("Reference"), _("Rate Include Taxes"), _("Tax Group"), 
                    _("Approval Status"), _("Expenses"), _("Taxes"), _("Total"));
        table_header($th);

        $k = $total_taxes = $total_net = $day_total = $accumulate = 0;
        $prev_date = $current_date = "";

        $result = get_utility_expenses_summary($from_date_, $to_date_, $entry_type_id);
            while ($trans = db_fetch($result)) {        
                alt_table_row_color($k);

                    $current_date = $trans['date'];

                    if ($prev_date != "" && $prev_date != $current_date) {
                        start_row();
                            label_cell(text_bold("Total for ".sql2date($prev_date)), 'align=right colspan=12');         
                            amount_cell($day_total, true);  
                        end_row();
                        $day_total = 0;
                    }
                    label_cell($trans['entry_type_name']);
                    label_cell(sql2date($current_date), 'align=center');
                    label_cell(sql2date($trans['due_date']), 'align=center');
                    label_cell(get_project_formatted_name($trans['project_id']));
                    label_cell(get_supplier_formatted_name($trans['supplier_id']));
                    $link = get_journal_trans_view_str(ST_UTILITY_BILLS, $trans['id']);
                    label_cell($link, 'align=center');
                    label_cell($trans['reference']);
                    label_cell(get_yes_no_name($trans['rate_include_taxes']));
                    label_cell($trans['tax_group_name']);
                    label_cell($approval_statuses[$trans['approval_status']], 'align=center');

                    $net_amount = $trans['total']; 
                    $taxes = $trans['taxes']; 
                    amount_cell($net_amount); 
                    amount_cell($taxes);
                    
                    $gross_total = $taxes + $net_amount;
                    amount_cell($gross_total); 

                    $total_net += $net_amount;
                    $total_taxes += $taxes;
                    $day_total += $gross_total;  
                    $accumulate += $gross_total;  

                end_row();

                $prev_date = $trans['date'];
            }

        start_row();
            label_cell(text_bold("Total for ".sql2date($prev_date)), 'align=right colspan=12');         
            amount_cell($day_total, true);  
        end_row();
        start_row();
            label_cell(text_bold("Period Total"), 'align=right colspan=10');         
            amount_cell($total_net, true);  
            amount_cell($total_taxes, true);  
            amount_cell($accumulate, true);  
        end_row();

    end_table(2);
}