<?php


function project_invoices_collections_table_month_wise_display($project_id, $from_month_display_order, $to_month_display_order)
{
//    display_heading2("Project P&L");
    
    $invoice_types_in = SCVT_PROJECT_INVOICES_CERTIFIED;
    $credit_note_types_in = SCVT_PROJECT_CREDIT_NOTES_CERTIFIED;
    
    $receipt_types_in = SCVT_PROJECT_INVOICE_RECEIPTS;
    $payment_types_in = SCVT_PROJECT_CREDIT_NOTE_PAYMENTS;
            
    display_title("Project Invoices Collections and Outstandings");
    
    start_table(TABLESTYLE, "width=75%");
    $th = array("Month", "Invoices (IPCs)", "Credit Notes", "Collections (Invoices)", "Payments (Credit Notes)", "Month Outstandings", "Cumulative Outstandings");

    table_header($th);
    $months_result = get_months_range_by_display_order($from_month_display_order, $to_month_display_order);
    
    $k = $total_invoices = $total_credit_notes = $total_receipts = $total_payments = $outstandings_total = 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'];
        
        $invoices = get_summary_projects_month_range_project_value($month_display_order, $month_display_order, $project_id, $invoice_types_in);
        amount_cell($invoices);
        $total_invoices += $invoices;
        
        $credit_notes = get_summary_projects_month_range_project_value($month_display_order, $month_display_order, $project_id, $credit_note_types_in);
        amount_cell($credit_notes);
        $total_credit_notes += $credit_notes;
        
        $receipts = get_summary_projects_month_range_project_value($month_display_order, $month_display_order, $project_id, $receipt_types_in);
        amount_cell($receipts);
        $total_receipts += $receipts;
        
        $payments = get_summary_projects_month_range_project_value($month_display_order, $month_display_order, $project_id, $payment_types_in);
        amount_cell($payments);
        $total_payments += $payments;        
        
        $outstandings = ($invoices - $credit_notes) - ($receipts - $payments);       
        if($outstandings >= 0){
            amount_cell($outstandings);
        }
        else{
            amount_cell($outstandings);
        }
        
        $outstandings_total += $outstandings;
        if($outstandings_total >= 0){
            amount_cell($outstandings_total);
        }
        else{
            amount_cell($outstandings_total);
        }
        end_row();
    }
    start_row();
        label_cell(text_bold("Totals"), "align=right");
        amount_cell($total_invoices, true);
        amount_cell($total_credit_notes, true);
        amount_cell($total_receipts, true);
        amount_cell($total_payments, true);
        label_cell('');
        amount_cell($outstandings_total, true); 
        
        end_row();
    end_table();
}

