<?php


function department_cashflow_table_month_wise_display($department_id, $from_month_display_order, $to_month_display_order)
{

        
    display_title("Department Cashflow");
    
    start_table(TABLESTYLE, "width=95%");
    $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_department_cf_openning_balance($department_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_payment = 
            $total_sales_invoice_receipts_and_credit_note_payment = 
            $total_sub_invoice_payments_and_credit_note_receipts = 
            $total_iou_payments_and_iou_advances = 
            $total_utility_bill_payments_and_credit_note_returns = 
            $total_transport_payments = 
            $total_direct_purchase_payments_and_credit_note_returns = 
            $total_material_issues_and_returns = 
            $total_sales_material_purchase_payments_and_credit_note_payments = 
            $total_overhead = 
            $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'];
        
        // Collections
        $project_invoice_receipts = get_department_cf_project_invoice_receipts($department_id, $month_display_order, $month_display_order); 
        $project_credit_note_payments = get_department_cf_project_credit_note_payments($department_id, $month_display_order, $month_display_order); 
        $project_invoice_receipts_and_credit_note_payment = $project_invoice_receipts - $project_credit_note_payments;
        amount_cell($project_invoice_receipts_and_credit_note_payment);
        $total_project_invoice_receipts_and_credit_note_payment += $project_invoice_receipts_and_credit_note_payment;
        
        $sales_invoice_receipts = get_department_cf_sales_invoice_receipts($department_id, $month_display_order, $month_display_order); 
        $sales_credit_note_payments = get_department_cf_sales_credit_note_payments($department_id, $month_display_order, $month_display_order); 
        $sales_invoice_receipts_and_credit_note_payment = $sales_invoice_receipts - $sales_credit_note_payments;
        amount_cell($sales_invoice_receipts_and_credit_note_payment);
        $total_sales_invoice_receipts_and_credit_note_payment += $sales_invoice_receipts_and_credit_note_payment;
        
        $collections = $project_invoice_receipts_and_credit_note_payment + $sales_invoice_receipts_and_credit_note_payment;
        amount_cell($collections);
        
        //Payments
        $sub_invoice_payments = get_department_cf_subcontractor_invoice_payments($department_id, $month_display_order, $month_display_order);
        $sub_credit_note_receipts = get_department_cf_subcontractor_credit_note_receipts($department_id, $month_display_order, $month_display_order);
        $sub_invoice_payments_and_credit_note_receipts = $sub_invoice_payments + $sub_credit_note_receipts;
        amount_cell($sub_invoice_payments_and_credit_note_receipts);
        $total_sub_invoice_payments_and_credit_note_receipts += $sub_invoice_payments_and_credit_note_receipts;        
        
        $iou_payments = get_department_cf_iou_payments($department_id, $month_display_order, $month_display_order);
        $iou_advances = get_department_cf_iou_advance_payments($department_id, $month_display_order, $month_display_order);
        $iou_payments_and_iou_advances = $iou_payments + $iou_advances;
        amount_cell($iou_payments_and_iou_advances);
        $total_iou_payments_and_iou_advances += $iou_payments_and_iou_advances;
        
        $utility_bill_payments = get_department_cf_utility_bill_payments($department_id, $month_display_order, $month_display_order);
        $utility_bill_credit_note_returns = get_department_cf_utility_bill_credit_note_returns($department_id, $month_display_order, $month_display_order);
        $utility_bill_payments_and_credit_note_returns = $utility_bill_payments + $utility_bill_credit_note_returns;
        amount_cell($utility_bill_payments_and_credit_note_returns);
        $total_utility_bill_payments_and_credit_note_returns += $utility_bill_payments_and_credit_note_returns;
        
        $transport_payments = get_department_cf_transport_payments($department_id, $month_display_order, $month_display_order);
        amount_cell($transport_payments);
        $total_transport_payments += $transport_payments;
        
        $direct_purchase_payments = get_department_cf_direct_purchase_payments($department_id, $month_display_order, $month_display_order);
        $direct_purchase_credit_note_returns = get_department_cf_direct_purchase_credit_note_returns($department_id, $month_display_order, $month_display_order);
        $direct_purchase_payments_and_credit_note_returns = $direct_purchase_payments + $direct_purchase_credit_note_returns;
        amount_cell($direct_purchase_payments_and_credit_note_returns);
        $total_direct_purchase_payments_and_credit_note_returns += $direct_purchase_payments_and_credit_note_returns;
        
        $material_issues = get_department_cf_material_issues($department_id, $month_display_order, $month_display_order);
        $material_returns = get_department_cf_material_returns($department_id, $month_display_order, $month_display_order);
        $material_issues_and_returns = $material_issues + $material_returns;
        amount_cell($material_issues_and_returns);
        $total_material_issues_and_returns += $material_issues_and_returns;
        
        $sales_material_purchase_payments = get_department_cf_sales_material_purchase_payments($department_id, $month_display_order, $month_display_order);
        $sales_material_purchase_credit_note_returns = get_department_cf_sales_material_purchase_credit_note_returns($department_id, $month_display_order, $month_display_order);
        $sales_material_purchase_payments_and_credit_note_payments = $sales_material_purchase_payments + $sales_material_purchase_credit_note_returns;
        amount_cell($sales_material_purchase_payments_and_credit_note_payments);
        $total_sales_material_purchase_payments_and_credit_note_payments += $sales_material_purchase_payments_and_credit_note_payments;
        
        $payments = $sub_invoice_payments_and_credit_note_receipts 
                    + $iou_payments_and_iou_advances 
                    + $utility_bill_payments_and_credit_note_returns 
                    + $transport_payments 
                    + $direct_purchase_payments_and_credit_note_returns 
                    + $material_issues_and_returns
                    + $sales_material_purchase_payments_and_credit_note_payments;
        amount_cell($payments);
//        label_cell('$payments');
        
        $overhead = get_department_cf_overhead($department_id, $month_display_order, $month_display_order); 
        amount_cell($overhead);
        $total_overhead += $overhead;
        
        $outstandings = $collections - ($payments + $overhead);
        if($outstandings >= 0){
            amount_cell($outstandings);
        }
        else{
            label_cell('<font color="red">'."(".price_format(-$outstandings).")".'</font>', "align=right");
        }
        
        $outstandings_total += $outstandings + $openning_balance;
        if($outstandings_total >= 0){
            amount_cell($outstandings_total, true);
        }
        else{
            label_cell('<font color="red">'.text_bold("(".price_format(-$outstandings_total).")").'</font>', "align=right");
        }
        end_row();
        
    }
    start_row();
        label_cell(text_bold("Totals"), "align=right");
        amount_cell($total_project_invoice_receipts_and_credit_note_payment, true);
        amount_cell($total_sales_invoice_receipts_and_credit_note_payment, true);
        $total_collections = $total_project_invoice_receipts_and_credit_note_payment + $total_sales_invoice_receipts_and_credit_note_payment;
        amount_cell($total_collections, true);
        
        amount_cell($total_sub_invoice_payments_and_credit_note_receipts, true);
        amount_cell($total_iou_payments_and_iou_advances, true);
        amount_cell($total_utility_bill_payments_and_credit_note_returns, true);
        amount_cell($total_transport_payments, true);
        amount_cell($total_direct_purchase_payments_and_credit_note_returns, true); 
        amount_cell($total_material_issues_and_returns, true); 
        amount_cell($total_sales_material_purchase_payments_and_credit_note_payments, true); 
        
        $total_payments = $total_sub_invoice_payments_and_credit_note_receipts 
                            + $total_iou_payments_and_iou_advances 
                            + $total_utility_bill_payments_and_credit_note_returns 
                            + $total_transport_payments 
                            + $total_direct_purchase_payments_and_credit_note_returns 
                            + $total_material_issues_and_returns 
                            + $total_sales_material_purchase_payments_and_credit_note_payments;
        amount_cell($total_payments, true); 
        amount_cell($total_overhead, true); 
        label_cell('');

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

