<?php




function department_wise_period_sales_graph_display(&$pg, $company_code, $from_month_display_order, $to_month_display_order)
{
    department_wise_period_sales_graph_set_values($pg, $company_code, $from_month_display_order, $to_month_display_order);

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

    $pg->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "Department Wise Direct Sales";
    $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 department_wise_period_sales_graph_set_values(&$pg, $group_company_code, $from_month_display_order, $to_month_display_order) 
{

        $company_codes_in = "''"; 
    
        $value_types_in = SCVT_SALES_INVOICES.",".SCVT_SALES_CREDIT_NOTES;
        
        $result = get_group_company_all_list_values($group_company_code, 'DEPARTMENTS', "'Y'");
        $data = array();
        while ($department = db_fetch($result)) {
            $order_total = $discounts_total = $taxes_total = 0;
            
            if($department['id'] == 1651){
                $sss = 90;
            }
            $name = $department["value_name"];

            $department_ids_in = "'".$department['id']."'";     
            $department_sales = get_summary_saless_month_range_department_total($department_ids_in, $value_types_in, $from_month_display_order, $to_month_display_order);
            if(isset($department_sales['order_total'])){
                $order_total = $department_sales['order_total'];
                $discounts_total = $department_sales['discounts_total'];
                $taxes_total = $department_sales['taxes_total'];    
            }

            $line = array("name" =>$name, "order_total" => $order_total, "discounts_total" => $discounts_total, "taxes_total" => $taxes_total);
            $data[] = $line;

        }
        
        array_multisort(array_column($data, "order_total"), SORT_DESC, $data);
        
        $i = 0;
        foreach ($data as $value) {
            if ($pg != null) {
                $pg->x[$i] = $value['name'];
                $pg->y[$i] = $value['order_total'] - $value['discounts_total'];
//                $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($value['order_total']);
                
                
//                $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'];
//            }
        }

}

