<?php

function purchasing_top_items_graph_display(&$pg0, $month_begin_date, $month_end_date)
{
    purchasing_top_items_graph_set_values($pg0, $month_begin_date, $month_end_date);

    $random_id = 'ptig_'.time();
    
    $pg0->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "Top 10 Items";
    $data['x_axis_name'] = "Supplier";
    $data['y_axis_name'] = "Amount";
    $data['graph1_name'] = "";
    $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($pg0, $data, 2, $random_id);
//    source_graphic($today, $title, , $pg0, "Target", _("Achievement"), 1, $random_id, "Trend");
    //echo "</a>";
}

function purchasing_top_items_graph_set_values(&$pg0, $month_begin_date, $month_end_date) 
{
    $title = "";
    $result = get_purchasing_top_items($month_begin_date, $month_end_date);
        $i = 0;
        while ($myrow = db_fetch($result)) {
            $name = $myrow["stock_id"]." - ".$myrow["description"];
            if ($pg0 != null) {
                $pg0->x[$i] = $name;              

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

        return $title;

}

function get_purchasing_top_items($month_begin_date, $month_end_date)
{   
    
    $sql = "SELECT SUM(i.qty_recd * i.unit_cost) AS total,
                    sm.stock_id, sm.description  
                       
            FROM ".TB_PREF."grn_batch as g 
                JOIN ".TB_PREF."grn_items as i 
                    ON g.id = i.grn_batch_id 
                JOIN ".TB_PREF."stock_master as sm  
                    ON sm.stock_id = i.item_code  
                     
            WHERE g.delivery_date >= ".db_escape($month_begin_date)." 
                AND g.delivery_date <= ".db_escape($month_end_date)." 
            GROUP BY i.item_code 
            ORDER BY total DESC 
            LIMIT 10";
        
    return db_query($sql);
}


?>

