<?php

function sales_top_customers_graph_display(&$pg0, $month_begin_date, $month_end_date)
{
    sales_top_customers_graph_set_values($pg0, $month_begin_date, $month_end_date);

    $random_id = 'stcg_'.time();
    
    $pg0->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "Top 10 Customers";
    $data['x_axis_name'] = "Customer";
    $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 sales_top_customers_graph_set_values(&$pg0, $month_begin_date, $month_end_date) 
{
    $title = "";
    $result = get_top_customers($month_begin_date, $month_end_date);
        $i = 0;
        while ($myrow = db_fetch($result)) {
            $name = $myrow["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_top_customers($month_begin_date, $month_end_date)
{      
    $sql = "SELECT SUM((ov_amount + ov_discount) * rate * IF(trans.type = ".ST_CUSTCREDIT.", -1, 1)) AS total,
                    d.debtor_no, 
                    d.name 
            FROM ".TB_PREF."debtor_trans AS trans, ".TB_PREF."debtors_master AS d 
            WHERE trans.debtor_no=d.debtor_no 
                AND (trans.type = ".ST_SALESINVOICE." OR trans.type = ".ST_CUSTCREDIT.") 
                AND tran_date >= ". db_escape($month_begin_date)." AND tran_date <= ". db_escape($month_end_date);
    
    $sql .= " GROUP by d.debtor_no ORDER BY total DESC, d.debtor_no 
            LIMIT 10";
        
    return db_query($sql);
}


?>








































































































































































































































































