<?php


function project_estimated_cashflow_table_month_wise_display($project_id, $from_month_display_order, $to_month_display_order)
{

            
    display_title("Estimated Project Cashflow");
    
    start_table(TABLESTYLE, "width=60%");
    $th = array("Month", "Collections", "Expenses",
                "Month Cashflow", "Cumulative Cashflow");

    table_header($th);
    
    start_row();
    $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);
    label_cell(text_bold("Openning Balance"), "colspan=4");

    if($openning_balance >= 0){
        amount_cell($openning_balance);
    }
    else{
        label_cell('<font color="red">'.text_bold("(".price_format(-$openning_balance).")").'</font>', "align=right");
    }
    
    end_row();
    
    $months_result = get_months_range_by_display_order($from_month_display_order, $to_month_display_order);
    
    $k = $total_collections = $total_payments = $total_cashflow = 0;
    
//    $project = get_project_record($project_id);
    
    while($month = db_fetch($months_result)){
        alt_table_row_color($k);

        label_cell($month['description']);
//        $month_display_order = $month['display_order'];
        
        // Collections
        $collections = get_project_estimated_cash_flow_collections_month_total($project_id, $month['month_begin_date'], $month['month_end_date']);
        amount_cell($collections);
        $total_collections += $collections;
        
        //Payments
        $payments = get_project_estimated_cash_flow_payments_month_total($project_id, $month['id']);                
        amount_cell($payments);
        $total_payments += $payments;
        
        $month_cashflow =  $openning_balance + $collections - $payments;         
        amount_cell($month_cashflow);
        
        $openning_balance = $month_cashflow;
        
     
        $total_cashflow += $month_cashflow;
        if($total_cashflow >= 0){
            amount_cell($total_cashflow);
        }
        else{
            label_cell('<font color="red">'.text_bold("(".price_format(-$total_cashflow).")").'</font>', "align=right");
        }
        end_row();
        
    }
    start_row();
        label_cell(text_bold("Totals"), "align=right");
        amount_cell($total_collections, true);
        amount_cell($total_payments, true);

        label_cell('');

        if($total_cashflow >= 0){
            amount_cell($total_cashflow, true);
        }
        else{
            label_cell('<font color="red">'.text_bold("(".price_format(-$total_cashflow).")").'</font>', "align=right");
        }
    
        
        end_row();
    end_table();
}

