<?php


function display_graph($pg, $data, $type = 2, $random_id = 999) {
    
    $title = $data['title'];
    $x_axis = $data['x_axis_name'];
    $y_axis = $data['y_axis_name'];
    $graphic1 = $data['graph1_name'];
    
    $graphic2 = null;
    if (isset($data['graph2_name']) && strlen($data['graph2_name']) > 0){
        $graphic2 = $data['graph2_name'];
    }
    
    $graphic3 = null;
    if (isset($data['graph3_name']) && strlen($data['graph3_name']) > 0){
        $graphic3 = $data['graph3_name'];
    }
    
    
    if (count($pg->y) == 0 || (count($pg->y) == 1 && $pg->y[0] == 0)){
        display_title("No data for $title");
        return;
    }
    display_title("$title");
    //$pg->title     = $title . " - " . $today;
    $pg->axis_x = $x_axis;
//    $pg->axis_y = _("Amount");
    $pg->axis_y = $y_axis;
    
    $pg->graphic_1 = $graphic1;
    if ($graphic2 != null){
        $pg->graphic_2 = $graphic2;
    }
    if ($graphic3 != null){
        $pg->graphic_3 = $graphic3;
    }
    
    $pg->type = $type;
    $pg->skin = 1;
    $pg->built_in = false;
//    $filename = company_path() . "/pdf_files/" . random_id() . ".png";
    $filename = company_path() . "/graph_files/" . $random_id . ".png";
    $pg->display($filename, true);
    start_table(TABLESTYLE);
    start_row();
    echo "<td>";
    echo "<img src='$filename' border='0' alt='$title'>";
    echo "</td>";
    end_row();
    end_table(1);
}


function dashboard_get_month_ranges_array($from_month_display_order, $to_month_display_order) {
    $month_ranges_array = array();
    
        $result = get_months_range_by_display_order($from_month_display_order, $to_month_display_order);
            while ($row = db_fetch($result)) {
                $month_ranges_array[$row['display_order']] = array('month_id' => $row['id'], 
                                                'month_name' => $row['description'], 
                                                'short_name' => $row['short_name'], 
                                                'month_begin' => $row['month_begin_date'], 
                                                'month_end' => $row['month_end_date'],
                                                'month_value' => 0,
                                                'month_totals' => array());
            }
        
        return $month_ranges_array;
}

function dashboard_calculate_table_width($month_ranges_array) {
    
//    foreach ($month_ranges_array as $month) {
//                $month_totals = $month['month_totals'];
//                $total = 0;
//    }
    
    $k = count(array_keys($month_ranges_array));
    $width = ($k * 10) +20;
    if($width > 98){
        $width = 98;
    }elseif($width < 20){
        $width = 40;
    }
    
    return $width;
}

function dashboard_adjust_month_ranges_dates(&$month_ranges_array, $days=30) {
    
    foreach ($month_ranges_array as &$m) {
        $begin_date = &$m['month_begin'];
        $end_date = &$m['month_end'];

        $m['month_begin'] = date("Y-m-d", strtotime($begin_date." $days days"));
        $m['month_end'] = date("Y-m-d", strtotime($end_date." $days days"));
    }
    
    
}

function dashboard_get_from_month($from_month_post_name){
    
    get_offset_month($starting_month_display_order, $offset_months);
    
}


function dashboard_set_from_to_months($from_month_post_name, $to_month_post_name){
    
    if(isset_post_numeric($to_month_post_name)){
        $to_month_id = $_POST[$to_month_post_name];
    }else{
        $to_month_id = get_global_dashboard_to_month();
        if(!(isset($to_month_id) && $to_month_id > 0)){
            $current_month = get_current_month();
            $to_month_id = $current_month['id'];            
        }
    }
    
    if(isset_post_numeric($from_month_post_name)){
        $from_month_id = $_POST[$from_month_post_name];
    }else{
        $from_month_id = get_global_dashboard_from_month();
        if(!(isset($from_month_id) && $from_month_id > 0)){
            $to_month = get_month($to_month_id);
            $from_month = get_offset_month($to_month['display_order'], -6);
            $from_month_id = $from_month['id'];
        }
    }
    
    $_POST[$from_month_post_name] = $from_month_id;
    $_POST[$to_month_post_name] = $to_month_id;
    
}


function dashboard_display_simple_month_totals($month_ranges_array, $label_col_span=1, $label="")
{
    if($label == ""){
        $label = "Totals";
    }
    start_row();
    label_cell(text_bold($label), 'colspan='.$label_col_span.' align=right');

    $grand_total = 0;
    foreach ($month_ranges_array as $month_t) {
        $total = $month_t['month_value'];
        $str_count = number_format($total,2);
        label_cell(text_bold($str_count), 'align=right');                
        $grand_total += $total; 
    }   
    label_cell(text_bold(number_format($grand_total,2)), 'align=right'); 
    end_row();
}



function dashboard_get_achievement_background_colour($value)
{
    $colour = '';
    
    if($value <= 50){
    }
    elseif($value <= 60){
        $colour = "'background-color:rgb(255, 255, 204)'";
    }
    elseif($value <= 70){
        $colour = "'background-color:rgb(255, 204, 204)'";
    }
    elseif($value <= 80){
        $colour = "'background-color:rgb(255, 153, 153)'";
    }
    elseif($value <= 90){
        $colour = "'background-color:rgb(255, 102, 102)'";
    }
    elseif($value <= 100){
        $colour = "'background-color:rgb(255, 80, 80)'";
    }
    elseif($value > 100){
        $colour = "'background-color:rgb(138,141,143)'";
        $colour = "'background-color:rgb(204, 0, 0)'";
    }
    else{
        
    }
    
    return $colour;
}

function dashboard_display_coloured_achievement_cell($percentage, $text_bold=false){
            
        $rgb = dashboard_get_achievement_background_colour($percentage);
        if($rgb){
            $colour = "style=".$rgb;
            if($text_bold){
                $percentage_text = text_bold(number_format($percentage,2));
            }else{
                $percentage_text = number_format($percentage,2);                
            }
        }else{
            $colour = "";
            $percentage_text = number_format($percentage,2);
        }
        
        if($percentage != 0){
//            amount_cell($percentage, true);
            label_cell($percentage_text, 'align=right '.$colour); 
        }else{
            label_cell("");
        }
}