<?php

function sales_top_items_graph_display(&$pg1, $month_begin_date, $month_end_date)
{
    sales_top_items_graph_set_values($pg1, $month_begin_date, $month_end_date);

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

function sales_top_items_graph_set_values(&$pg1, $month_begin_date, $month_end_date) 
{
    $title = "";
    $result = get_sales_top_items($month_begin_date, $month_end_date);
        $i = 0;
        while ($myrow = db_fetch($result)) {
            $name = $myrow["description"];
            if ($pg1 != null) {
                $pg1->x[$i] = $name;              

                $pg1->y[$i] = $myrow["total"];
//                $pg1->z[$i] = $receipts;
                
//                $pg1->y[$i] = $myrow['target'];
//                $pg1->z1[$i] = $pg1->z[$i]; // for trend calculation
                $pg1->colour_type[$i] = set_bar_colour_type($myrow["total"]);
                
                
//                $pg1->y[$i] = $myrow['achievement'];
//                $pg1->z[$i] = $myrow['target'];
//                $pg1->z1[$i] = $myrow['target'];
//                $pg1->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_sales_top_items($month_begin_date, $month_end_date)
{
    $sql = "SELECT SUM((trans.unit_price * trans.quantity) * d.rate) AS total, 
                    s.stock_id, 
                    s.description,
                    SUM(trans.quantity) AS qty 
                    
            FROM ".TB_PREF."debtor_trans_details AS trans, ".TB_PREF."stock_master AS s, ".TB_PREF."debtor_trans AS d
            WHERE trans.stock_id=s.stock_id AND trans.debtor_trans_type=d.type AND trans.debtor_trans_no=d.trans_no
                AND (d.type = ".ST_SALESINVOICE." OR d.type = ".ST_CUSTCREDIT.") ";

    $sql .= " AND d.tran_date >= ". db_escape($month_begin_date)." AND d.tran_date <= ". db_escape($month_end_date)."  
            GROUP by s.stock_id 
            ORDER BY total 
            DESC, s.stock_id 
            LIMIT 10";
    
    return db_query($sql);
}


?>










































































































































































































































































