<?php

function project_project_cash_flow_graph_display(&$pg0, $project_id, $from_month_display_order, $to_month_display_order)
{
    project_project_cash_flow_graph_set_values($pg0, $project_id, $from_month_display_order, $to_month_display_order);

    $random_id = 'ppcfg_'.$project_id."_".$from_month_display_order."_".time();
    
    $pg0->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "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_cash_flow_graph_set_values(&$pg0, $project_id, $from_month_display_order, $to_month_display_order) 
{
        $title = "";

        $project = get_project_record($project_id);

        $openning_balance = get_project_cf_openning_balance($project_id, $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_cash_flow_collections_month_total($project_id, $month['display_order']);
                $payments = get_project_cash_flow_payments_month_total($project_id, $month['display_order']);                
                $overhead = get_project_cash_flow_overhead_month_total($project_id, $month['display_order']);  
                $payments_plus_overhead = $payments + $overhead;
                $cashflow =  $openning_balance + $collections - $payments_plus_overhead;         
                
//                $profit = -2500000;
                $pg0->y[$i] = $collections;
                $pg0->z[$i] = $payments_plus_overhead;
                $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_cash_flow_payments_month_total($project_id, $month_display_order)
{
        $from_month_display_order = $to_month_display_order = $month_display_order;
    
        $sub_invoice_payments = get_project_cf_subcontractor_invoice_payments($project_id, $from_month_display_order, $to_month_display_order);
        $sub_credit_note_receipts = get_project_cf_subcontractor_credit_note_receipts($project_id, $from_month_display_order, $to_month_display_order);
        $sub_invoice_payments_credit_notes = $sub_invoice_payments + $sub_credit_note_receipts;
        
        $iou_petty_cash_payments = get_project_cf_iou_payments($project_id, $from_month_display_order, $to_month_display_order);
        $iou_advance_payments = get_project_cf_iou_advance_payments($project_id, $from_month_display_order, $to_month_display_order);
        $iou_petty_cash_payments_advance = $iou_petty_cash_payments + $iou_advance_payments;
        
        $utility_bill_payments = get_project_cf_utility_bill_payments($project_id, $from_month_display_order, $to_month_display_order);
        $utility_bill_returns = get_project_cf_utility_bill_returns($project_id, $from_month_display_order, $to_month_display_order);
        $utility_bill_payemnts_and_returns = $utility_bill_payments + $utility_bill_returns;
        
        $transport = get_project_cf_transport_payments($project_id, $from_month_display_order, $to_month_display_order);
        
        $direct_purchase_payments = get_project_cf_direct_purchase_payments($project_id, $from_month_display_order, $to_month_display_order);
        $direct_purchase_returns = get_project_cf_direct_purchase_credit_notes($project_id, $from_month_display_order, $to_month_display_order);
        $direct_purchase_payments_and_returns = $direct_purchase_payments + $direct_purchase_returns;
        
        $material_issue = get_project_cf_material_issues($project_id, $from_month_display_order, $to_month_display_order);
        $material_returns = get_project_cf_material_returns($project_id, $from_month_display_order, $to_month_display_order);
        $material_issues_and_returns = $material_issue - $material_returns;
        
        $project_payments = $sub_invoice_payments_credit_notes + 
                            $iou_petty_cash_payments_advance + 
                            $utility_bill_payemnts_and_returns + 
                            $transport + 
                            $direct_purchase_payments_and_returns + 
                            $material_issues_and_returns;
    
        return $project_payments;

        
}

function get_project_cash_flow_overhead_month_total($project_id, $month_display_order)
{
    $overhead = get_project_cf_overhead($project_id, $month_display_order, $month_display_order);
    
    return $overhead;
}


function get_project_cash_flow_collections_month_total($project_id, $month_display_order)
{
        $from_month_display_order = $to_month_display_order = $month_display_order;
        
        $invoice_receipts = get_project_cf_project_invoice_receipts($project_id, $from_month_display_order, $to_month_display_order); 
        $credit_note_payments = get_project_cf_project_credit_note_payments($project_id, $from_month_display_order, $to_month_display_order); 
        $invoice_receipts_credit_note_payments = $invoice_receipts + $credit_note_payments;
        
        return $invoice_receipts_credit_note_payments;
}


?>

