<?php

function dashboard_purchase_price_variance($from_month_display_order, $to_month_display_order) {
    
    display_heading('Item Purchases');    br();
    
    display_heading2('(Based on Purchase Order Detail Converted Base Unit Prices)');    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, "width=99%");
    start_table(TABLESTYLE, "width$table_width%");
        
        $th = array("Item", "Base Unit");
            foreach ($month_ranges_array as $month) {
                $th[] = $month['month_name'];
            }
            $th[] = "Min Max Price Variance";

            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_price_variance_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);
                label_cell($item['base_unit_name'], "align=center");
                
                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";
                    $max_row = dashboard_get_item_period_max_price_details($item_code, $from_date, $to_date);
                    $min_row = dashboard_get_item_period_min_price_details($item_code, $from_date, $to_date);

                    if(is_array($max_row)){
                        if($max_row['po_detail_item'] != $min_row['po_detail_item']){//two records
                            if($max_row['delivery_date'] > $min_row['delivery_date']){// price increased
                                $price_late = $max_row['max_price'];
                                $price_early = $min_row['min_price'];
                                $variance = (($price_late - $price_early)/$price_early)*100;
                                $variance_str = '<font color="red">'.number_format($variance,2).'%'.'</font>';
                            }else{// price deceased
                                $price_late = $min_row['min_price'];
                                $price_early = $max_row['max_price'];
                                $variance = (($price_late - $price_early)/$price_early)*100;
                                $variance_str = number_format($variance,2).'%';
                            }   
                            $str = number_format($price_late,2)."<br>";
                            $str .= number_format($price_early,2);
                            $price_link = get_dashboard_view_str(DASH_SC_PURCHASE_PRICE_VARIANCE, $parameters, $str);              
                            
                            
                            $str = $price_link."<br>(".$variance_str.")";
                        }else{
                            $price = $max_row['max_price'];
                            $str = number_format($price,2);
                            $price_link = get_dashboard_view_str(DASH_SC_PURCHASE_PRICE_VARIANCE, $parameters, $str);                         
                            $str = $price_link;
                        }
                    }else{
                        $str = '';
                    }
                    label_cell($str, 'align=right');
                }   
                
                label_cell(number_format(($item['min_max_variance']*100),2)."%", 'align=right');
                end_row();
            }
        
                
            
    end_table(2);
}
