<?php


function project_cashflow_table_month_wise_display($project_id, $from_month_display_order, $to_month_display_order)
{            
    display_title("Project Cashflow");
    
    start_table(TABLESTYLE, "width=95%");
    $th = array("Month", "Collections", "Subcontractor Payments", "IOU Payments & Advances", "Utility Bill Payments", "Transport Payments", 
                nl2br("Direct Material ".PHP_EOL." Purchase Payments"), "Cost of Material Issues", "Overhead",
                "Month Cashflow", "Cumulative Cashflow");
    $th = array("Month", 
                nl2br("Project ".PHP_EOL." Invoice ".PHP_EOL." Collections"), 
                nl2br("Sales ".PHP_EOL." Invoice ".PHP_EOL." Collections"), 
                nl2br("Month ".PHP_EOL." Collections"), 
                nl2br("Subcontractor ".PHP_EOL." Invoice ".PHP_EOL." Payments"), 
                nl2br("IOU and ".PHP_EOL." Petty Cash ".PHP_EOL." Payments"), 
                nl2br("Utility Bill ".PHP_EOL." Payments"),  
                nl2br("Transport ".PHP_EOL." Payments"), 
                nl2br("Direct ".PHP_EOL." Material ".PHP_EOL." Purchase ".PHP_EOL." Payments"), 
                nl2br("Stock ".PHP_EOL." Material".PHP_EOL." Issues"), 
                nl2br("Sales ".PHP_EOL." Material ".PHP_EOL." Purchase ".PHP_EOL." Payments"), 
                nl2br("Month ".PHP_EOL." Payments"), 
                nl2br("Overhead"), 
                nl2br("Month ".PHP_EOL." Cashflow"), 
                nl2br("Cumulative ".PHP_EOL." Cashflow"));

    table_header($th);
    
    start_row();
    $openning_balance = get_project_cf_openning_balance($project_id, $from_month_display_order);
    label_cell(text_bold("Openning Balance"), "colspan=14");

    if($openning_balance >= 0){
        amount_cell($openning_balance, true);
    }
    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_project_invoice_receipts_and_credit_note_payments = $total_direct_sales_invoice_receipts_and_credit_note_payments = 
            $total_collections = $total_sub_invoice_payments_credit_notes = $total_iou_petty_cash_payments_advance = 
            $total_utility_bill_payemnts_and_returns = $total_transport = $total_direct_purchase_payments_and_returns = 
            $total_material_issues_and_returns = $total_sales_material_purchase_payments_and_credit_note_returns = 
            $total_payments = $total_overhead = $outstandings_total = $cumulative_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
        $project_invoice_receipts = get_project_cf_project_invoice_receipts($project_id, $month_display_order, $month_display_order); 
        $project_credit_note_payments = get_project_cf_project_credit_note_payments($project_id, $month_display_order, $month_display_order); 
        $project_invoice_receipts_and_credit_note_payments = $project_invoice_receipts + $project_credit_note_payments;
        amount_cell($project_invoice_receipts_and_credit_note_payments);
        $total_project_invoice_receipts_and_credit_note_payments = $project_invoice_receipts_and_credit_note_payments;
        
        
        $direct_sales_invoice_receipts = get_project_cf_direct_sales_invoice_receipts($project_id, $month_display_order, $month_display_order); 
        $direct_sales_credit_note_payments = get_project_cf_direct_sales_credit_note_payments($project_id, $month_display_order, $month_display_order); 
        $direct_sales_invoice_receipts_and_credit_note_payments = $direct_sales_invoice_receipts + $direct_sales_credit_note_payments;
        amount_cell($direct_sales_invoice_receipts_and_credit_note_payments);
        $total_direct_sales_invoice_receipts_and_credit_note_payments = $direct_sales_invoice_receipts_and_credit_note_payments;
        
        $collections = $project_invoice_receipts_and_credit_note_payments + $direct_sales_invoice_receipts_and_credit_note_payments;
        amount_cell($collections);
        $total_collections += $collections;
        
        //Payments
        $sub_invoice_payments = get_project_cf_subcontractor_invoice_payments($project_id, $month_display_order, $month_display_order);
        $sub_credit_note_receipts = get_project_cf_subcontractor_credit_note_receipts($project_id, $month_display_order, $month_display_order);
        $sub_invoice_payments_credit_notes = $sub_invoice_payments + $sub_credit_note_receipts;
        amount_cell($sub_invoice_payments_credit_notes);
        $total_sub_invoice_payments_credit_notes += $sub_invoice_payments_credit_notes;
        
        $iou_petty_cash_payments = get_project_cf_iou_payments($project_id, $month_display_order, $month_display_order);
        $iou_advance_payments = get_project_cf_iou_advance_payments($project_id, $month_display_order, $month_display_order);
        $iou_petty_cash_payments_advance = $iou_petty_cash_payments + $iou_advance_payments;
        amount_cell($iou_petty_cash_payments_advance);
        $total_iou_petty_cash_payments_advance += $iou_petty_cash_payments_advance;
        
        $utility_bill_payments = get_project_cf_utility_bill_payments($project_id, $month_display_order, $month_display_order);
        $utility_bill_returns = get_project_cf_utility_bill_returns($project_id, $month_display_order, $month_display_order);
        $utility_bill_payemnts_and_returns = $utility_bill_payments + $utility_bill_returns;
        amount_cell($utility_bill_payemnts_and_returns);
        $total_utility_bill_payemnts_and_returns += $utility_bill_payemnts_and_returns;
        
        $transport = get_project_cf_transport_payments($project_id, $month_display_order, $month_display_order);
        amount_cell($transport);
        $total_transport += $transport;
        
        $direct_purchase_payments = get_project_cf_direct_purchase_payments($project_id, $month_display_order, $month_display_order);
        $direct_purchase_returns = get_project_cf_direct_purchase_credit_notes($project_id, $month_display_order, $month_display_order);
        $direct_purchase_payments_and_returns = $direct_purchase_payments + $direct_purchase_returns;
        amount_cell($direct_purchase_payments_and_returns);
        $total_direct_purchase_payments_and_returns += $direct_purchase_payments_and_returns;
        
        $material_issue = get_project_cf_material_issues($project_id, $month_display_order, $month_display_order);
        $material_returns = get_project_cf_material_returns($project_id, $month_display_order, $month_display_order);
        $material_issues_and_returns = $material_issue - $material_returns;
        amount_cell($material_issues_and_returns);
        $total_material_issues_and_returns += $material_issues_and_returns;
        
        $sales_material_purchase_payments = get_project_cf_sales_material_purchase_payments($project_id, $month_display_order, $month_display_order);
        $sales_material_purchase_credit_note_returns = get_project_cf_sales_material_purchase_credit_note_returns($project_id, $month_display_order, $month_display_order);
        $sales_material_purchase_payments_and_credit_note_returns = $sales_material_purchase_payments + $sales_material_purchase_credit_note_returns;
        amount_cell($sales_material_purchase_payments_and_credit_note_returns);
        $total_sales_material_purchase_payments_and_credit_note_returns += $sales_material_purchase_payments_and_credit_note_returns;
        
        $month_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 
                                        + $sales_material_purchase_payments_and_credit_note_returns;
        
        amount_cell($month_payments);
        $total_payments += $month_payments;
        
        $overhead = get_project_cash_flow_overhead_month_total($project_id, $month['display_order']);  
        amount_cell($overhead);
        $total_overhead += $overhead;
        
     
        
        $month_cashflow = $collections - ($month_payments + $overhead );
        if($month_cashflow >= 0){
            amount_cell($month_cashflow);
        }
        else{
            label_cell('<font color="red">'.("(".price_format(-$month_cashflow).")").'</font>', "align=right");
        }
        
        $cumulative_cashflow += $month_cashflow + $openning_balance;
        if($cumulative_cashflow >= 0){
            amount_cell($cumulative_cashflow, true);
        }
        else{
            label_cell('<font color="red">'.text_bold("(".price_format(-$cumulative_cashflow).")").'</font>', "align=right");
        }
        end_row();
        
    }
    start_row();
        label_cell(text_bold("Totals"), "align=right");
        amount_cell($total_project_invoice_receipts_and_credit_note_payments, true);
        amount_cell($total_direct_sales_invoice_receipts_and_credit_note_payments, true);
        amount_cell($total_collections, true);
        amount_cell($total_sub_invoice_payments_credit_notes, true);
        amount_cell($total_iou_petty_cash_payments_advance, true);
        amount_cell($total_utility_bill_payemnts_and_returns, true);
        amount_cell($total_transport, true);
        amount_cell($total_direct_purchase_payments_and_returns, true);
        amount_cell($total_material_issues_and_returns, true);
        amount_cell($total_sales_material_purchase_payments_and_credit_note_returns, true);
        amount_cell($total_payments, true);
        amount_cell($total_overhead, true);
        label_cell('');

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

