<?php


function department_cashflow_table_month_wise_display($department_id, $from_month_display_order, $to_month_display_order)
{            
    display_title("Project Cashflow");
    
    start_table(TABLESTYLE, "width=90%");
    $th = array("Month", "Collections", "Subcontractor Payments", "IOU Payments & Advances", "Utility Bill Payments", "Transport Payments", 
                "Direct Material Purchase Payments", "Cost of Material Issues", "Overhead",
                "Month Cashflow", "Cumulative Cashflow");

    table_header($th);
    
    start_row();
    $openning_balance = get_department_cashflow_openning_balance($department_id, $from_month_display_order);
    label_cell(text_bold("Openning Balance"), "colspan=10");
    amount_cell($openning_balance, true);
    end_row();
    
    $months_result = get_months_range_by_display_order($from_month_display_order, $to_month_display_order);
    
    $k = $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_overhead = $outstandings_total = 0;
    
//    $project = get_project_record($project_id);
    
    $department_ids_in = $department_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_department_cash_flow_collections_month_total($department_ids_in, $month_display_order, $month_display_order);
        amount_cell($collections);
        $total_collections += $collections;
        
        //Payments
        $sub_invoice_payments = get_department_cash_flow_subcontractor_invoice_payments($department_ids_in, $month_display_order, $month_display_order);
        $sub_credit_note_receipts = get_department_cash_flow_subcontractor_credit_note_receipts($department_ids_in, $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_department_cash_flow_iou_payments($department_ids_in, $month_display_order, $month_display_order);
        $iou_advance_payments = get_department_cash_flow_iou_advance_payments($department_ids_in, $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_department_cash_flow_utility_bill_payments($department_ids_in, $month_display_order, $month_display_order);
        $utility_bill_returns = get_department_cash_flow_utility_bill_returns($department_ids_in, $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_department_cash_flow_transport_payments($department_ids_in, $month_display_order, $month_display_order);
        amount_cell($transport);
        $total_transport += $transport;
        
        $direct_purchase_payments = get_department_cash_flow_direct_purchase_payments($department_ids_in, $month_display_order, $month_display_order);
        $direct_purchase_returns = get_department_cash_flow_direct_purchase_credit_notes($department_ids_in, $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_department_cash_flow_material_issues($department_ids_in, $month_display_order, $month_display_order);
        $material_returns = get_department_cash_flow_material_returns($department_ids_in, $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;
        
        
        $overhead = get_department_cashflow_overhead($department_ids_in, $month_display_order, $month_display_order);  
        amount_cell($overhead);
        $total_overhead += $overhead;
        
     
        
        $outstandings = $collections - ($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 
                                        + $overhead );
        if($outstandings >= 0){
            amount_cell($outstandings);
        }
        else{
            amount_cell($outstandings);
        }
        
        $outstandings_total += $outstandings;
        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();
        
    }
    start_row();
        label_cell(text_bold("Totals"), "align=right");
        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_overhead, true);
        label_cell('');
        amount_cell($outstandings_total, true); 
        
        end_row();
    end_table();
}


function get_department_cash_flow_material_issues($department_ids_in, $from_month_display_order, $to_month_display_order)
{
    $receipts_types_in = SCVT_MATERIAL_ISSUES;
    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $receipts_types_in);
}

function get_department_cash_flow_material_returns($department_ids_in, $from_month_display_order, $to_month_display_order)
{
    $receipts_types_in = SCVT_MATERIAL_RETURNS;
    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $receipts_types_in);
}

function get_department_cash_flow_direct_purchase_payments($department_ids_in, $from_month_display_order, $to_month_display_order)
{
    $receipts_types_in = SCVT_SUPPLIER_INVOICE_PAYMENTS;
    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $receipts_types_in);
}


function get_department_cash_flow_direct_purchase_credit_notes($department_ids_in, $from_month_display_order, $to_month_display_order)
{
    $receipts_types_in = SCVT_SUPPLIER_CREDIT_NOTE_RETURNS;
    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $receipts_types_in);
}

function get_department_cash_flow_transport_payments($department_ids_in, $from_month_display_order, $to_month_display_order)
{
    $receipts_types_in = SCVT_TRANSPORT_PAYMENTS;
    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $receipts_types_in);
}

function get_department_cash_flow_utility_bill_payments($department_ids_in, $from_month_display_order, $to_month_display_order)
{
    $receipts_types_in = SCVT_UTILITY_BILL_PAYMENTS;
    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $receipts_types_in);
}

function get_department_cash_flow_utility_bill_returns($department_ids_in, $from_month_display_order, $to_month_display_order)
{
    $receipts_types_in = SCVT_UTILITY_BILL_CREDIT_NOTE_RETURNS;
    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $receipts_types_in);
}


function get_department_cash_flow_iou_payments($department_ids_in, $from_month_display_order, $to_month_display_order)
{
    $receipts_types_in = SCVT_I_OWE_YOU_PAYMENTS;
    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $receipts_types_in);
}


function get_department_cash_flow_iou_advance_payments($department_ids_in, $from_month_display_order, $to_month_display_order)
{
    $receipts_types_in = SCVT_I_OWE_YOU_ADVANCES;
    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $receipts_types_in);
}


function get_department_cash_flow_subcontractor_credit_note_receipts($department_ids_in, $from_month_display_order, $to_month_display_order)
{
    $receipts_types_in = SCVT_SUBCONTRACTOR_CREDIT_NOTE_RETURNS;
    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $receipts_types_in);
}

function get_department_cash_flow_subcontractor_invoice_payments($department_ids_in, $from_month_display_order, $to_month_display_order)
{
    $receipts_types_in = SCVT_SUBCONTRACTOR_INVOICE_PAYMENTS;
    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $receipts_types_in);
}


function get_department_cash_flow_collections($department_ids_in, $from_month_display_order, $to_month_display_order)
{
        
    $invoice_receipts = get_department_cash_flow_project_invoice_receipts($department_ids_in, $from_month_display_order, $to_month_display_order); 
    $credit_note_payments = get_department_cash_flow_project_credit_note_payments($department_ids_in, $from_month_display_order, $to_month_display_order); 
    $invoice_receipts_credit_note_payments = $invoice_receipts + $credit_note_payments;

    return $invoice_receipts_credit_note_payments;        
            
}

function get_department_cash_flow_project_credit_note_payments($department_ids_in, $from_month_display_order, $to_month_display_order)
{
    $receipts_types_in = SCVT_PROJECT_CREDIT_NOTE_PAYMENTS;
    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $receipts_types_in);
}

function get_department_cash_flow_project_invoice_receipts($department_ids_in, $from_month_display_order, $to_month_display_order)
{
    $value_types_in = SCVT_PROJECT_INVOICE_RECEIPTS;
    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $value_types_in); 
}

function get_department_cashflow_openning_balance($department_id, $from_month_display_order)
{
    $openning_balance = 0;
    
    $result = get_department_projects($department_id);
    while($project = db_fetch($result)){
        $openning_balance += get_project_cf_openning_balance($project['project_id'], $from_month_display_order);
    }
    
    return $openning_balance;
}



function get_department_cashflow_overhead($department_ids_in, $from_month_display_order, $to_month_display_order)
{
//        $project_id = $project['project_id'];
    
        $value_types_in = SCVT_SUBCONTRACTOR_INVOICES;    
        $sub_invoices = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $value_types_in); 

        $value_types_in = SCVT_SUBCONTRACTOR_CREDIT_NOTES;
        $sub_credit_notes = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $value_types_in);
        $sub_invoices_credit_notes = $sub_invoices + $sub_credit_notes;
        
        
        $value_types_in = SCVT_I_OWE_YOU_PAYMENTS.",".SCVT_I_OWE_YOU_ADVANCES;
        $iou_petty_cash_expenses = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $value_types_in);
        
        
        $value_types_in = SCVT_UTILITY_BILLS;    
        $utility_bill = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $value_types_in);        
        $value_types_in = SCVT_UTILITY_BILL_CREDIT_NOTES;
        $utility_credit = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $value_types_in);
        $utility_bills_and_credits = $utility_bill - $utility_credit;
        
        
        $value_types_in = SCVT_VEHICLE_RUNNING_CHART;
        $transport = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $value_types_in);
        
        
        $value_types_in = SCVT_PR_ISSUES;
        $direct_purchases = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $value_types_in);
        $value_types_in = SCVT_SITE_MATERIAL_RETURNS;
        $direct_purchase_returns = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $value_types_in);        
        $direct_purchases_and_returns = $direct_purchases - $direct_purchase_returns;
        
        
        $value_types_in = SCVT_MATERIAL_ISSUES;
        $material_issue = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $value_types_in);
        $value_types_in = SCVT_MATERIAL_RETURNS;
        $material_returns = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_ids_in, $value_types_in);  
        $material_issues_and_returns = $material_issue - $material_returns;
        
        
        $project_expenses = $sub_invoices_credit_notes + $iou_petty_cash_expenses + $utility_bills_and_credits + 
                            $transport + $direct_purchases_and_returns + $material_issues_and_returns;

        $average_overhead_factor = get_department_average_overhead_factor($department_ids_in);
        
        $overhead = $project_expenses * $average_overhead_factor;
        
        return $overhead;
    
}


