<?php

function display_utility_bill_expense_details_inquiry_selecting_parameters() {
    global $expense_categories;
    
    start_table(TABLESTYLE_NOBORDER);
        start_row();
            ref_cells(text_bold("Utility Bill No:"), 'id', '',null, '', true);
            ref_cells(text_bold("Reference:"), 'reference', '',null, '', true);
            supplier_list_cells("Supplier:", 'supplier_id', null, '', true);
            ref_cells(("Description (% %):"), 'description', '',null, '', true);
        end_row();
    end_table();
    
    start_table(TABLESTYLE_NOBORDER);
        start_row();  
            project_list_cells(_("Project:"), 'project_id', null, _('All projects'));
            date_cells(_("From:"), 'from_date', '', null, -14);
            date_cells(_("To:"), 'to_date', '', null, +7);
        end_row();
    end_table();
    
    start_table(TABLESTYLE_NOBORDER);
        start_row();    
            $available_expense_categories = $expense_categories; 
            unset($available_expense_categories[EC_STOCK_ITEM_RECEIPT]); 
            custom_values_list_cells("Expense Category:", "expense_category_id", $available_expense_categories, null, "", true);
    
            switch ($_POST['expense_category_id']) {
                case EC_PETTY_CASH_EXPENSE:
                    hidden('expense_item_id', 0);
                    break;
                case EC_VEHICLE_MAINTENANCE:
                    vehicles_list_cells("Vehicle", 'expense_item_id', null, '');
                    break;
                case EC_UTILITY_ITEM:
                    value_list_cells("Utility Item", 'expense_item_id', "UTILITY_ITEMS", null, '');
                    break;
                case EC_ASSET_MAINTENANCE:
                    asset_list_cells("Issuing Tool", 'expense_item_id', null, false);
                    break;

                default:
                    hidden('expense_item_id', 0);
                    $_POST['expense_item_id'] = 0;
                    break;
            }

            utility_expense_types_list_cells("Expense Type:", "expense_type_id", null, '', true);

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

function display_utility_bill_expense_details_inquiry($id=0, $reference=0, $supplier_id=0, $description='', $project_id=0, $from_date_=null, $to_date_=null, 
                                                      $expense_category_id=0, $expense_item_id=0, $expense_type_id=0) {
    
    global $expense_categories, $Ajax;
    
    $from_date = date2sql($from_date_);
    $to_date = date2sql($to_date_);
    
    $Ajax->activate('iou_pet_details');
    div_start('iou_pet_details');
    
        start_table(TABLESTYLE2, "width=99%");

            $th = array(_("UBD Id"), _("Uti.Bil. #"), _("Reference"), _("Bill #"), _("Bill Ref."), _("Supplier"), _("Project"), _("Expense Category"), 
                        _("Expense Item"), _("Expense Type"), _("Budget"), _("Date"), _("Invoice No"), _("Description"), _("Amount"));
            table_header($th);

            $j = 1;
            $total = $k =0;
            $result = get_utility_bill_expense_details_inquiry($id, $reference, $supplier_id, $description, $project_id, $from_date, $to_date, 
                                                               $expense_category_id, $expense_item_id, $expense_type_id);
                while ($row = db_fetch($result)) {
                    alt_table_row_color($k); 
                        
                        label_cell($row["id"], 'align=center');
                        label_cell(get_journal_trans_view_str(ST_UTILITY_BILLS, $row['ub_id']), 'align=center');
                        label_cell($row["reference"]);
                        label_cell($row["bill_no"]);
                        label_cell($row["bill_reference"]);
                        label_cell($row["supp_name"]);
                        label_cell($row["project_name"]);
                        
                        if ($row["expense_category_id"] > 0) {
                            label_cell($expense_categories[$row["expense_category_id"]]);
                        } else {
                            label_cell('');
                        }

                        $expense_item_name = '';
                        switch ($row['expense_category_id']) {
                            case EC_PETTY_CASH_EXPENSE:
                                break;
                            case EC_STOCK_ITEM_RECEIPT:
                                if ($row["stock_id"] > 0) {
                                    $expense_item_name = get_stock_item_formatted_name($row["stock_id"]);
                                }
                                break;
                            case EC_VEHICLE_MAINTENANCE:
                                $expense_item_name = get_vehicle_formatted_name($row["expense_item_id"]);
                                break;
                            case EC_UTILITY_ITEM:
                                $utility_item = get_listvalue_by_id($row["expense_item_id"]);
                                $expense_item_name = $utility_item['value_name'];
                                break;
                            case EC_ASSET_MAINTENANCE:
                                $expense_item_name = get_asset_formatted_name($row["expense_item_id"]);
                                break;

                            default:
                                break;
                        }
                        
                        label_cell($expense_item_name);                     

                        label_cell($row["expense_type_name"]);

                        label_cell($row["task_description"]);
                        label_cell(sql2date($row["date"]));
                        label_cell($row["bill_no"]);
                        label_cell($row["description"]);
                        amount_decimal_cell($row["amount"]);  

                    end_row();

                    $j++;
                        if ($j == 12) {
                            $j = 1;
                            table_header($th);
                        }
                        
                    $total += $row["amount"];
                }
    
                start_row();
                    label_cell(text_bold("Total:"), "align=right colspan=14");
                    amount_cell($total, true);
                end_row();

        end_table(3);
        
    div_end();
    
    return $total;
}

?>