<?php

function purchasing_grn_or_po_for_the_period_graph_display(&$pg3, $from_month_display_order, $to_month_display_order)
{
    purchasing_grn_or_po_for_the_period_graph_set_values($pg3, $from_month_display_order, $to_month_display_order);
    
    $random_id = 'pgrnpo_'.time();
    
    $pg3->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "Purchasing and Goods Receiving";
    $data['x_axis_name'] = "Months";
    $data['y_axis_name'] = "Value";
    $data['graph1_name'] = "POs Total";
    $data['graph2_name'] = "GRNs Total";
    $data['graph3_name'] = "";

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

}

function purchasing_grn_or_po_for_the_period_graph_set_values(&$pg3, $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)){
        $po_total = $grn_total = 0;
        
        $po_total = get_purchasing_po_for_the_period($month['month_begin_date'], $month['month_end_date']);
        $grn_total = get_purchasing_grn_for_the_period($month['month_begin_date'], $month['month_end_date']);
        $cn_total = get_dashboard_purchasing_credit_notes_for_the_period($month['month_begin_date'], $month['month_end_date']);

        $name = $month['description'];
            if ($pg3 != null){
                $pg3->x[$i] = $name;              
                $pg3->y[$i] = $po_total;
                $pg3->z[$i] = $grn_total + $cn_total;  
                $pg3->colour_type[$i] = set_bar_colour_type($po_total);
            }

            $i++;
    }

        return $title;
}


function get_purchasing_po_for_the_period($month_begin_date, $month_end_date)
{       
    $sql = "SELECT SUM(po.total) total     
            FROM ".TB_PREF."purch_orders po 
                
            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, "total");

}


function get_purchasing_grn_for_the_period($month_begin_date, $month_end_date)
{       
    $sql = "SELECT SUM(gi.tran_qty * gi.unit_cost) total     
            FROM ".TB_PREF."grn_batch gb   
                JOIN ".TB_PREF."grn_items gi 
                    ON gb.id = gi.grn_batch_id   
                
            WHERE gb.delivery_date >= ".db_escape($month_begin_date)." 
                AND gb.delivery_date <= ".db_escape($month_end_date);
    
    $result = db_query($sql);
    return db_result_value($result, "total");

}

function get_dashboard_purchasing_credit_notes_for_the_period($month_begin_date, $month_end_date)
{       
    $sql = "SELECT SUM(st.ov_amount) total     
            FROM ".TB_PREF."supp_trans st 
            WHERE st.type >= ".db_escape(ST_SUPPCREDIT)." 
                AND st.tran_date >= ".db_escape($month_begin_date)." 
                AND st.tran_date <= ".db_escape($month_end_date);
    
    $result = db_query($sql);
    return db_result_value($result, "total");

}


?>