<?php

function purchasing_delevary_lead_time_for_the_period_graph_display(&$pg4, $from_month_display_order, $to_month_display_order)
{
    purchasing_delevary_lead_time_for_the_period_graph_set_values($pg4, $from_month_display_order, $to_month_display_order);
    
    $random_id = 'pdltfpgd_'.time();
    
    $pg4->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "Purchasing Delevary Lead Time";
    $data['x_axis_name'] = "Months";
    $data['y_axis_name'] = "Total";
    $data['graph1_name'] = "Witnin 10 Days";
    $data['graph2_name'] = "Witnin 30 Days";
    $data['graph3_name'] = "More Than 30 Days";

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

}

function purchasing_delevary_lead_time_for_the_period_graph_set_values(&$pg4, $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)){
        $days = get_purchasing_delevary_lead_time_for_the_period($month['month_begin_date'], $month['month_end_date']);
        $d10 = $days["d10"];
        $d30 = $days["d30"];
        $d999 = $days["d999"];

        $month_name = $month['description'];      

        $name = $month_name;
            if ($pg4 != null){
                $pg4->x[$i] = $name;              
                $pg4->y[$i] = $d10;
                $pg4->z[$i] = $d30;
                $pg4->z1[$i] = $d999;
                $pg4->colour_type[$i] = set_bar_colour_type($d999);
            }

            $i++;
    }

        return $title;
}


function get_purchasing_delevary_lead_time_for_the_period($month_begin_date, $month_end_date)
{       
    $sql = "SELECT DISTINCT prd.requisition_id, prh.pr_date  
                    
            FROM ".TB_PREF."purchase_requisitions prh  
                JOIN ".TB_PREF."purchase_requisition_details prd 
                    ON prh.pr_no = prd.requisition_id 
                INNER JOIN ".TB_PREF."pr_po_details pr_po  
                    ON prd.id = pr_po.pr_line_id  
                INNER JOIN ".TB_PREF."purch_order_details pod  
                    ON pod.po_detail_item = pr_po.po_line_id   
                INNER JOIN ".TB_PREF."grn_batch grnb  
                    ON grnb.purch_order_no = pod.order_no     
                
            WHERE grnb.delivery_date >= ".db_escape($month_begin_date)." 
                AND grnb.delivery_date <= ".db_escape($month_end_date);
    
    $result = db_query($sql);
    
    $d10 = $d30 = $d999 = 0;
    while($pr = db_fetch($result)){
        $days = get_purchasing_first_delevary_days_for_pr($pr["requisition_id"], $month_begin_date, $month_end_date, $pr["pr_date"]);
   
        if($days < 0){
            
        }
        elseif($days <=10){
            $d10++;
        }
        elseif($days <=30){
            $d30++;
        }
        else{
            $d999++;
        }
    }
    
    return array("d10"=>$d10, "d30"=>$d30, "d999"=>$d999);
}


function get_purchasing_first_delevary_days_for_pr($pr_id, $month_begin_date, $month_end_date, $pr_date)
{       
    $days = -1;
    
    $sql = "SELECT grnb.delivery_date 
                    
            FROM ".TB_PREF."purchase_requisition_details prd 
                INNER JOIN ".TB_PREF."pr_po_details pr_po  
                    ON prd.id = pr_po.pr_line_id  
                INNER JOIN ".TB_PREF."purch_order_details pod  
                    ON pod.po_detail_item = pr_po.po_line_id   
                INNER JOIN ".TB_PREF."grn_batch grnb  
                    ON grnb.purch_order_no = pod.order_no     
                
            WHERE prd.requisition_id = ".db_escape($pr_id)." 
            ORDER BY grnb.delivery_date LIMIT 1 ";
            
    
    $result = db_query($sql);
    
    $delivery = db_fetch($result);
    if(isset($delivery["delivery_date"]) && $delivery["delivery_date"] > "0000-00-00")
    {
        if($delivery["delivery_date"] >= $month_begin_date && $delivery["delivery_date"] <= $month_end_date){
            $days = abs(date_diff2(sql2date($pr_date), sql2date($delivery['delivery_date']), 'd')); 
        }
    }
    
    return $days;
}


?>