<?php

function dashboard_project_item_consuption($from_month_display_order, $to_month_display_order, $project_id, $qty_val) {
    
    global $systypes_array;
//    display_heading2('(Based on Goods Received Notes and Tax Free Item Costs)');  
    
    $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);
    $category_array = $month_ranges_array;
    
    br();
    
    start_table(TABLESTYLE, "width=$table_width%");
        
        $th = array("Category","Item", "Unit");
            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'];
            
            if($qty_val == 1){
                $items_res = dashboard_get_project_item_consuption_items_value($from_date, $to_date, $project_id);
            }
            elseif($qty_val == 2){
                $items_res = dashboard_get_project_item_consuption_items_qty($from_date, $to_date, $project_id);
            }
            
            $k = 0;
            
            $previous_cat_name = "";
            $current_catr_name = "";
            
            while ($item = db_fetch($items_res)) {

                $current_catr_name = $item['category_name'];
                if($qty_val == 1 && $current_catr_name != $previous_cat_name && $previous_cat_name != ""){
                    dashboard_display_simple_month_totals($category_array, 3, $previous_cat_name." Total ");   
                    foreach ($category_array as &$cat_month) {
                        $cat_month['month_value'] = 0;
                    }
                }
                
                alt_table_row_color($k);
                
                label_cell($current_catr_name);
                $item_code = $item['item_code'];
                $item_name = get_stock_item_formatted_name($item_code, false, $item['item_description']);
                label_cell($item_name);
                label_cell($item['unit_name']);
                
                foreach ($month_ranges_array as $key=>&$month) {
                    
                    $from_date = $month['month_begin'];
                    $to_date = $month['month_end'];

                    $parameters = "pid=".$project_id."&item=".$item_code."&from_date=$from_date&to_date=$to_date";
                    
                    if($qty_val == 1){
                        $total = dashboard_get_period_project_item_consuption_total($project_id, $item_code, $from_date, $to_date);
                        $month['month_value'] += $total; 
                        $category_array[$key]['month_value'] += $total;                         
                    }
                    elseif($qty_val == 2){
                        $total = dashboard_get_period_project_item_consuption_qty($project_id, $item_code, $from_date, $to_date);
                    }

                    if($total != 0){
                        $str_count = get_dashboard_view_str(DASH_SC_PROJECT_ITEM_CONSUMPTION, $parameters, number_format($total,2));
                    }else{
//                        $str_count = number_format($total,2);
                        $str_count = "";
                    }
                    label_cell($str_count, 'align=right');
                }   
                
                
                label_cell(number_format($item['grand_total'],2), 'align=right');
                end_row();
                
                $previous_cat_name = $current_catr_name;
            }
            
        if($qty_val == 1){
            dashboard_display_simple_month_totals($category_array, 3, $previous_cat_name." Total ");  
            dashboard_display_simple_month_totals($month_ranges_array, 3);           
        }
                
            
    end_table(2);
}
