<?php

function dashboard_purchase_top_items($from_month_display_order, $to_month_display_order) {
    
    display_heading('Item Purchases');    br();
    
    display_heading2('(Based on Goods Received Notes and Tax Free Item Costs)');    br();
    
    $month_ranges_array = dashboard_get_month_ranges_array($from_month_display_order, $to_month_display_order);
    $table_width = dashboard_calculate_table_width($month_ranges_array);
    
    start_table(TABLESTYLE, "with$table_width%");
        
        $th = array("Item");
            foreach ($month_ranges_array as $month) {
                $th[] = $month['month_name'];
            }
            $th[] = "Total";

            table_header($th);
            
            $from_month = get_month_by_display_order($from_month_display_order);
            $to_month = get_month_by_display_order($to_month_display_order);
            
            $from_date = $from_month['month_begin_date'];
            $to_date = $to_month['month_end_date'];
            
            $item_res = dashboard_get_period_grn_items($from_date, $to_date);
            
            $k = 0;
            
            
            while ($item = db_fetch($item_res)) {
                alt_table_row_color($k);
                $item_code = $item['item_code'];
                $item_name = get_stock_item_formatted_name($item_code);
                label_cell($item_name);
                
                foreach ($month_ranges_array as &$month) {
                    
                    $from_date = $month['month_begin'];
                    $to_date = $month['month_end'];
                    $parameters = "item=$item_code&from_date=$from_date&to_date=$to_date";
                    $total = dashboard_get_item_period_grns_total($item_code, $from_date, $to_date);

                    if($total > 0){
                        $str_count = get_dashboard_view_str(DASH_SC_PURCHASE_TOP_ITEMS, $parameters, number_format($total,2));
                        $month['month_value'] += $total; 
                    }else{
//                        $str_count = number_format($total,2);
                        $str_count = "";
                    }
                    label_cell($str_count, 'align=right');
                }   
                
                label_cell(number_format($item['grned_total'],2), 'align=right');
                end_row();
            }
        
        dashboard_display_simple_month_totals($month_ranges_array);              
            
    end_table(2);
}
