<?php

function project_project_estimated_cash_flow_graph_display(&$pg0, $project_id, $from_month_display_order, $to_month_display_order)
{
    project_project_estimated_cash_flow_graph_set_values($pg0, $project_id, $from_month_display_order, $to_month_display_order);

    $random_id = 'ppecfg_'.$project_id."_".$from_month_display_order."_".time();
    
    $pg0->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "Estimated Project Cashflow";
    $data['x_axis_name'] = "Month";
    $data['y_axis_name'] = "Value";
    $data['graph1_name'] = "Collections";
    $data['graph2_name'] = "Payments";
    $data['graph3_name'] = "Cashflow";
    
    
    //echo "<a href='".$path_to_root."/admin/dashboard_uda_level_1.php?month_id=".$month_id."&division_code=UDA_0001&level=".$level."'>";
    display_graph($pg0, $data, 1, $random_id);//line graph = 4
//    source_graphic($today, $title, , $pg0, "Target", _("Achievement"), 1, $random_id, "Trend");
    //echo "</a>";
}

function project_project_estimated_cash_flow_graph_set_values(&$pg0, $project_id, $from_month_display_order, $to_month_display_order) 
{
        $title = "";

        $project = get_project_record($project_id);

        $from_month = get_month_by_display_order($from_month_display_order);
        $upto_date = date2sql(add_days(sql2date($from_month['month_begin_date']), -1));
        
        $openning_balance = get_project_estimated_cash_flow_openning_balance($project_id, $upto_date, $from_month_display_order);
            
        $result = get_months_range_by_display_order($from_month_display_order, $to_month_display_order);
        $i = 0;
        while ($month = db_fetch($result)) {

            $name = $month["description"];
            if ($pg0 != null) {
                $pg0->x[$i] = $name;
                
                $collections = get_project_estimated_cash_flow_collections_month_total($project_id, $month['month_begin_date'], $month['month_end_date']);
                $payments = get_project_estimated_cash_flow_payments_month_total($project_id, $month['id']);                
 
                $cashflow =  $openning_balance + $collections - $payments;         
                
//                $profit = -2500000;
                $pg0->y[$i] = $collections;
                $pg0->z[$i] = $payments;
                $pg0->z1[$i] = $cashflow;
//                $pg0->z1[$i] = 150000;
                
                $pg0->colour_type[$i] = set_bar_colour_type($cashflow);
                
                $openning_balance = $cashflow;
                
            }
            
            
            $i++;
            
//            if($i == 1 && $myrow['achievement'] == null){
//                $title = "No data for ".$myrow['division_name'];
//            }
//            else{
//                $title = $myrow['division_name'];
//            }
        }

        return $title;

}


function get_project_estimated_cash_flow_payments_month_total($project_id, $month_id)
{
    return get_project_expenses_schedule_month_value($project_id, $month_id);
    
}



function get_project_estimated_cash_flow_collections_month_total($project_id, $start_date, $end_date)
{
        return get_project_billing_schedule_summary($project_id, $start_date, $end_date); 

}


function get_project_estimated_cash_flow_openning_balance($project_id, $upto_date, $upto_month_display_order)
{
        $ob_billing = get_project_billing_schedule_summary($project_id, INFINITE_START_DATE, $upto_date); 
         
        $ob_payments = get_project_expenses_schedule_summary($project_id, 0, $upto_month_display_order);
        
        return $ob_billing - $ob_payments;

}



?>


