<?php


function ipa_submission_division_wise_table_display($company_code, $begin_date, $end_date)
{
    $title = ("IPA submission - Division Wise");
    display_title($title);
    
    start_table(TABLESTYLE, "width=60%");
    $th = array("Department", "No of IPAs Submitted", "IPA Submission Value Total");

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

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

            label_cell($row['department_name']);       
            label_cell($row['count_ipas'],'align=right');    
            amount_cell($row['total']);

            end_row();
        }
    end_table();
}


function get_ipa_submission_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,
                    COUNT(pi.order_no) count_ipas  
            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 ord_date >= ".db_escape($begin_date)." 
                AND ord_date <= ".db_escape($end_date)."
                AND pi.approval_status IN (".db_escape(AS_NEW).",".db_escape(AS_AWAITING_APPROVAL).",".db_escape(AS_WITH_THE_APPROVER).","
                                            .db_escape(AS_BACK_TO_ORIGINATOR).")
            GROUP BY pm.department_id 
            ORDER BY lv.value_name";
    
    return db_query($sql);
}

?>