<?php


function bill_approval_report_division_wise_table_display($company_code, $begin_date, $end_date)
{
    $title = ("Bill Approvals - Division Wise");
    display_title($title);
    
    start_table(TABLESTYLE, "width=60%");
    $th = array("Department", "IPC Value Total");

    table_header($th);
//    $month = get_month($month_id);
    $result = get_bill_approval_report_division_wise_table($company_code, $begin_date, $end_date);

        while ($row = db_fetch($result)){
            alt_table_row_color($k);

            label_cell($row['department_name']);       
            amount_cell($row['total']);

            end_row();
        }
    end_table();
}


function get_bill_approval_report_division_wise_table($company_code, $begin_date, $end_date)
{
    $db_name = get_group_company_db_name($company_code);

    $sql = "SELECT lv.value_name department_name,
                    SUM(pi.total) total  
            FROM ".TB_PREF."project_invoices pi 
                JOIN ".TB_PREF."projects_master pm 
                    ON pi.project_id = pm.project_id 
                JOIN ".$db_name.".".TB_PREF."list_values lv
                    ON lv.id = pm.department_id  
            WHERE pi.approved_on >= ".db_escape($begin_date)." 
                AND pi.approved_on <= ".db_escape($end_date)."
                AND pi.approval_status = ".db_escape(AS_APPROVED)."
            GROUP BY pm.department_id 
            ORDER BY lv.value_name";
    
    return db_query($sql);
}

?>