<?php

function purchasing_new_suppliers_for_the_period_graph_display(&$pg2, $from_month_display_order, $to_month_display_order)
{
    purchasing_new_suppliers_for_the_period_graph_set_values($pg2, $from_month_display_order, $to_month_display_order);
    
    $random_id = 'pns_'.time();
    
    $pg2->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "New Suppliers";
    $data['x_axis_name'] = "Month";
    $data['y_axis_name'] = "New Suppliers";
    $data['graph1_name'] = "";
    $data['graph2_name'] = "";
    $data['graph3_name'] = "";

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

}

function purchasing_new_suppliers_for_the_period_graph_set_values(&$pg2, $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)){
        $new_suppliers_count = get_purchasing_new_suppliers_for_the_period($month['month_begin_date'], $month['month_end_date']);
        $month_name = $month['description'];        

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


function get_purchasing_new_suppliers_for_the_period($month_begin_date, $month_end_date)
{
    $count= 0;
    
    $supliers = get_purchasing_suppliers_for_the_period($month_begin_date, $month_end_date);
    while($suplier = db_fetch($supliers)){
        $old_po_no = get_purchasing_supplier_old_po($month_begin_date, $suplier['supplier_id']);

        if(!($old_po_no > 0)){
            $count++;
        }
    }
    
    return $count;
}

function get_purchasing_suppliers_for_the_period($month_begin_date, $month_end_date)
{       
    $sql = "SELECT DISTINCT po.supplier_id     
            FROM ".TB_PREF."purch_orders po  
            WHERE po.ord_date >= ".db_escape($month_begin_date)." 
                AND po.ord_date <= ".db_escape($month_end_date)." 
                AND po.approval_status IN (".db_escape(AS_APPROVED).",".db_escape(AS_CLOSED).") 
            GROUP BY po.supplier_id";
    
    return db_query($sql);

}


function get_purchasing_supplier_old_po($month_begin_date, $supplier_id)
{       
    $sql = "SELECT po.order_no   
            FROM ".TB_PREF."purch_orders po 
            WHERE po.ord_date < ".db_escape($month_begin_date)." 
                AND po.supplier_id = ".db_escape($supplier_id)." 
                AND po.approval_status IN (".db_escape(AS_APPROVED).",".db_escape(AS_CLOSED).") 
            LIMIT 1";
    
    $result = db_query($sql);
    return db_result_value($result, "order_no");

}


?>