<?php

function purchasing_trade_discount_for_the_period_graph_display(&$pg1, $from_month_display_order, $to_month_display_order)
{
    purchasing_trade_discount_for_the_period_graph_set_values($pg1, $from_month_display_order, $to_month_display_order);
    
    $random_id = 'ptd_'.time();
    
    $pg1->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "Purchasing Trade Discount";
    $data['x_axis_name'] = "Month";
    $data['y_axis_name'] = "Discounts Total";
    $data['graph1_name'] = "";
    $data['graph2_name'] = "";
    $data['graph3_name'] = "";

    display_graph($pg1, $data, 1, $random_id);

}

function purchasing_trade_discount_for_the_period_graph_set_values(&$pg1, $from_month_display_order, $to_month_display_order) 
{
    $title = "";
    $months = get_months_range_by_display_order($from_month_display_order, $to_month_display_order);

    $i = 0;
    while($month = db_fetch($months)){
        $discount_total = get_purchasing_trade_discount_for_the_period($month['month_begin_date'], $month['month_end_date']);
        
        $month_name = $month['description'];

        $name = $month_name;
            if ($pg1 != null){
                $pg1->x[$i] = $name;              
                $pg1->y[$i] = $discount_total;
                $pg1->colour_type[$i] = set_bar_colour_type($discount_total);
            }
            $i++;
    }
        return $title;
}


function get_purchasing_trade_discount_for_the_period($month_begin_date, $month_end_date)
{       
    $sql = "SELECT SUM((pod.order_unit_price * pod.order_unit_price_discount / 100) * pod.order_unit_qty) AS discount_total                        
            FROM ".TB_PREF."purch_orders po 
                JOIN ".TB_PREF."purch_order_details pod 
                    ON po.order_no = pod.order_no  

            WHERE po.ord_date >= ".db_escape($month_begin_date)." 
                    
                AND po.ord_date <= ".db_escape($month_end_date);
    
    $result = db_query($sql);
    return db_result_value($result, "discount_total");

}


?>