<?php

function purchasing_top_suppliers_graph_display(&$pg0, $month_begin_date, $month_end_date)
{
    purchasing_top_suppliers_graph_set_values($pg0, $month_begin_date, $month_end_date);

    $random_id = 'ptsg_'.time();
    
    $pg0->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "Top 10 Suppliers";
    $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_suppliers_graph_set_values(&$pg0, $month_begin_date, $month_end_date) 
{
    $title = "";
    $result = get_purchasing_top_suppliers($month_begin_date, $month_end_date);
        $i = 0;
        while ($myrow = db_fetch($result)) {
            $name = $myrow["supp_name"];
            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"]);
                
                
//                $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_purchasing_top_suppliers($month_begin_date, $month_end_date)
{   
//    $today = Today();
//    $today = date2sql(Today());
    
//    $sql = "SELECT trans.trans_no, trans.reference, trans.tran_date, 
//                   trans.due_date, s.supplier_id, s.supp_name, s.curr_code,
//                   (trans.ov_amount + trans.ov_gst + trans.ov_discount) AS total,
//                   (trans.ov_amount + trans.ov_gst + trans.ov_discount - trans.alloc) AS remainder,
//                   DATEDIFF('$today', trans.due_date) AS days 
//                       
//            FROM ".TB_PREF."supp_trans as trans, 
//                 ".TB_PREF."suppliers as s
//                     
//            WHERE s.supplier_id = trans.supplier_id
//                AND trans.type = ".ST_SUPPINVOICE." 
//                AND (ABS(trans.ov_amount + trans.ov_gst + trans.ov_discount) - trans.alloc) > ".FLOAT_COMP_DELTA."
//                AND DATEDIFF('$today', trans.due_date) > 0 
//            GROUP BY s.supp_ref
//            ORDER BY days DESC 
//            LIMIT 10";
    
    $sql = "SELECT SUM(trans.ov_amount + trans.ov_gst + trans.ov_discount) AS total,
                    s.supplier_id, s.supp_name 
                       
            FROM ".TB_PREF."supp_trans as trans, 
                 ".TB_PREF."suppliers as s
                     
            WHERE s.supplier_id = trans.supplier_id
                AND trans.type = ".ST_SUPPINVOICE."  
                AND trans.tran_date >= ".db_escape($month_begin_date)." 
                AND trans.tran_date <= ".db_escape($month_end_date)." 
            GROUP BY s.supplier_id 
            ORDER BY total DESC 
            LIMIT 10";
        
    return db_query($sql);
}


?>

