<?php


function all_departments_bids_and_new_projects_graph_display(&$pg0, $company_code, $from_month_display_order, $to_month_display_order)
{
    all_departments_bids_and_new_projects_graph_set_values($pg0, $company_code, $from_month_display_order, $to_month_display_order);

    $random_id = 'adbnpg_'.$company_code."_".$from_month_display_order."_".time();
    
    $pg0->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "Bids and New Projects";
    $data['x_axis_name'] = "Departments";
    $data['y_axis_name'] = "Count";
    $data['graph1_name'] = "New Projects"; 
    $data['graph2_name'] = "Bids";
    $data['graph3_name'] = "Wins";
       
    
    //echo "<a href='".$path_to_root."/admin/dashboard_uda_level_1.php?month_id=".$month_id."&division_code=UDA_0001&level=".$level."'>";
    display_graph($pg0, $data, 1, $random_id);
//    source_graphic($today, $title, , $pg0, "Target", _("Achievement"), 1, $random_id, "Trend");
    //echo "</a>";
}



function all_departments_bids_and_new_projects_graph_set_values(&$pg0, $group_company_code, $from_month_display_order, $to_month_display_order) 
{

        $title = "";
        if($group_company_code == "SAW"){
            $company_codes_in = "'SAW', 'SAWC', 'LPS'"; 
        }
        elseif($group_company_code == "VEE"){
            $company_codes_in = "'VEE', 'VEEC'"; 
        }
        
        $from_month = get_month_by_display_order($from_month_display_order);
        $to_month = get_month_by_display_order($to_month_display_order);
        $month_begin = $from_month['month_begin_date'];
        $month_end = $to_month['month_end_date'];
        
//        $result = get_summary_payrolls_month_range_departments($company_codes_in, $salary_components_in, $from_month_display_order, $to_month_display_order);
        $result = get_group_company_all_list_values($group_company_code, 'DEPARTMENTS', "'Y'");
//        $result = get_departments();
        $i = $est_projects = $wins = 0;
        
        while ($myrow = db_fetch($result)) {
            $department = get_group_company_list_value($group_company_code, 'DEPARTMENTS', $myrow['value_code']);
            $name = $department["value_name"];
            if ($pg0 != null) {
                $est_projects = $wins = 0;
                $pg0->x[$i] = $name;
                
//                $department_codes_in = "'".$myrow['department_code']."'";     
                get_month_range_est_projects_count($department['id'], $month_begin, $month_end, $est_projects, $wins);
                
                $sys_count = get_month_range_sys_projects_count($department['id'], $month_begin, $month_end);
//                $sys_count =5;

                $pg0->y[$i] = $sys_count ;
                $pg0->z[$i] = $est_projects ;
                $pg0->z1[$i] = $wins ;
                
//                $pg0->y[$i] = $myrow['target'];
//                $pg0->z1[$i] = $pg0->z[$i]; // for trend calculation
                $pg0->colour_type[$i] = set_bar_colour_type($sys_count);
                
                
//                $pg0->y[$i] = $myrow['achievement'];
//                $pg0->z[$i] = $myrow['target'];
//                $pg0->z1[$i] = $myrow['target'];
//                $pg0->colour_type[$i] = set_bar_colour_type($myrow['achievement']);
            }
            
            
            $i++;
            
//            if($i == 1 && $myrow['achievement'] == null){
//                $title = "No data for ".$myrow['division_name'];
//            }
//            else{
//                $title = $myrow['division_name'];
//            }
        }

        return $title;

}


function get_departments()
{ 
    $sql = "SELECT * 
            FROM ".TB_PREF."list_values  
            WHERE list_code = 'DEPARTMENTS' 
            ORDER BY id";
    
    return db_query($sql);
}

function get_month_range_est_projects_count($department_id, $month_begin, $month_end, &$est_projects, &$wins)
{ 
    

    $sql = "SELECT p.est_project_id, 
                    (SELECT c.id FROM ".TB_PREF."est_project_competitors c 
                        WHERE c.est_project_id = p.est_project_id  AND c.competitor_type = 1 AND c.bid_winner = 1 LIMIT 1) won  
            FROM ".TB_PREF."est_projects_master p 
            WHERE p.start_date >= ".db_escape($month_begin)." AND p.start_date <= ".db_escape($month_end)."
                AND p.department_id = ".db_escape($department_id)."";
    
    $result = db_query($sql);
    while ($row1 = db_fetch($result)){
        $est_projects++;
        
        if($row1['won'] > 0){
            $wins++;
        }
    }
}


function get_month_range_sys_projects_count($department_id, $month_begin_date, $month_end_date)
{ 
//    $row = get_month($month_id);
//    $month_begin = $row['month_begin_date'];
//    $month_end = $row['month_end_date'];

    $sql = "SELECT COUNT(project_id) sys_count 
            FROM ".TB_PREF."projects_master 
            WHERE start_date >= ".db_escape($month_begin_date)." AND start_date <= ".db_escape($month_end_date)."
                AND department_id = ".db_escape($department_id)."";
    
    $result = db_query($sql);
    return db_result_value($result, "sys_count");
}




















































































