<?php

function view_dashboard_utility_expenses_details11111111($expense_type_id, $from_date, $to_date) {
    $sql_ub = "SELECT ".ST_UTILITY_BILLS." trans_type, ub.id, ub.date, 
                    ubd.bill_no, ubd.bill_reference, ubd.expense_category_id, ubd.description, ubd.amount, 
                    pm.description project_name  
            FROM ".TB_PREF."utility_bills ub   
                JOIN ".TB_PREF."utility_bill_details ubd    
                    ON ub.id = ubd.ub_id  
                JOIN ".TB_PREF."projects_master pm 
                    ON ub.project_id = pm.project_id 
            WHERE ub.date >= ".db_escape($from_date)." 
                AND ub.date <= ".db_escape($to_date)." 
                AND ubd.expense_type_id = ".db_escape($expense_type_id);
            
    
    $sql_ubcn = "SELECT ".ST_UTILITY_BILL_CREDIT_NOTE." trans_type, ub.id ,ub.date, 
                    ubd.bill_no, ubd.bill_reference, ubd.expense_category_id, ubd.description, -ubd.amount,  
                    pm.description project_name  
            FROM ".TB_PREF."utility_bill_credit_notes ub   
                JOIN ".TB_PREF."utility_bill_credit_note_details ubd    
                    ON ub.id = ubd.ubcn_id    
                JOIN ".TB_PREF."projects_master pm 
                    ON ub.project_id = pm.project_id 
            WHERE ub.date >= ".db_escape($from_date)." 
                AND ub.date <= ".db_escape($to_date)." 
                AND ubd.expense_type_id = ".db_escape($expense_type_id);
    
    $sql = $sql_ub." UNION ".$sql_ubcn;
    
    $sql = "SELECT trans_type, id, date, 
                    bill_no, bill_reference, expense_category_id, description, amount,  
                    project_name  
            FROM ($sql) ub 
            ORDER BY ub.date"; 
                
        return db_query($sql, "view_dashboard_utility_expenses_details"); 
}


function view_dashboard_utility_expenses_details($from_date, $to_date, $expense_type_id=0, $project_id=-1, $linked_field_type_id=0, $linked_field_id=-1){   
    
    $sql_ub = dashboard_get_sql_for_utility_bill_expense_types($from_date, $to_date, $expense_type_id, $project_id, $linked_field_type_id, $linked_field_id);
    $sql_ubcn = dashboard_get_sql_for_utility_bill_credit_note_expense_types($from_date, $to_date, $expense_type_id, $project_id, $linked_field_type_id, $linked_field_id);
    
    
    $sql = $sql_ub." UNION ".$sql_ubcn;
    
    $sql = "SELECT trans_type, trans_no, date, 
                    bill_no, bill_reference, expense_category_id, description, amount,  
                    project_id  
            FROM ($sql) ub 
            ORDER BY ub.date"; 
                
        return db_query($sql, "view_dashboard_utility_expenses_details"); 
}