<?php




function all_departments_salary_expenses_graph_display(&$pg, $company_code, $from_month_display_order, $to_month_display_order)
{
    all_departments_salary_expenses_graph_set_values($pg, $company_code, $from_month_display_order, $to_month_display_order);

    $random_id = 'adseg_'.$company_code."_".$from_month_display_order."_".time();

    $pg->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "Salary Cost";
    $data['x_axis_name'] = "Departments";
    $data['y_axis_name'] = "Rs.";
    $data['graph1_name'] = "Salary";
    $data['graph2_name'] = "";
    $data['graph3_name'] = "";
    
    
    //echo "<a href='".$path_to_root."/admin/dashboard_uda_level_1.php?month_id=".$month_id."&division_code=UDA_0001&level=".$level."'>";
    display_graph($pg, $data, 1, $random_id);
//    source_graphic($today, $title, , $pg, "Target", _("Achievement"), 1, $random_id, "Trend");
    //echo "</a>";
}


function all_departments_salary_expenses_graph_set_values(&$pg, $group_company_code, $from_month_display_order, $to_month_display_order) 
{
    if($group_company_code == "SAW"){
        $company_codes_in = "'SAW', 'SAWC', 'LPS'"; 
    }
    elseif($group_company_code == "VEE"){
        $company_codes_in = "'VEE', 'VEEC'"; 
    }
        $salary_components_in = SCVT_TOTAL_SALARY_COST;
        
//        $result = get_group_company_all_list_values($company_code, 'DEPARTMENTS');        
//        $result = all_departments_salary_expenses_get_values($month_id, $company_codes_in, $salary_components_in);
        $result = get_summary_payrolls_month_range_company_values($company_codes_in, $salary_components_in, $from_month_display_order, $to_month_display_order);
        $i = 0;
        while ($myrow = db_fetch($result)) {
            $department = get_group_company_list_value($group_company_code, 'DEPARTMENTS', $myrow['department_code']);
            $name = $department["value_name"];
            if ($pg != null) {
                $pg->x[$i] = $name;
                
//                $department_codes_in = "'".$myrow['department_code']."'";     
//                $salary_total = get_summary_payrolls_value_total($month_id, $company_codes_in, $department_codes_in, $salary_components_in);
                $salary_total = $myrow['total'];  
                //$salary_paid_employees = get_summary_payrolls_value_total($month_id, $company_codes_in, $department_codes_in, $salary_paid_employee_in);
                
                $salary_cost = $salary_total;
                $pg->y[$i] = $salary_cost;
//                $pg->z[$i] = $average_salary;
                
//                $pg->y[$i] = $myrow['target'];
//                $pg->z1[$i] = $pg->z[$i]; // for trend calculation
                $pg->colour_type[$i] = set_bar_colour_type($salary_cost);
                
                
//                $pg->y[$i] = $myrow['achievement'];
//                $pg->z[$i] = $myrow['target'];
//                $pg->z1[$i] = $myrow['target'];
//                $pg->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'];
//            }
        }

}


function all_departments_salary_expenses_get_values($month_id, $company_codes_in, $salary_components_in)
{
    $sql = "SELECT DISTINCT department_code, SUM(value) total    
            FROM ".TB_PREF."monthly_summary_payrolls  
            WHERE month_id = ". db_escape($month_id)."  AND 
                    company_code IN (".$company_codes_in.") AND 
                    value_type IN (".$salary_components_in.") 
            GROUP BY department_code 
            ORDER BY total DESC ";
    
    return db_query($sql);
    
}
