<?php

function department_department_cash_flow_graph_display(&$pg2, $department_id, $from_month_display_order, $to_month_display_order)
{
    department_department_cash_flow_graph_set_values($pg2, $department_id, $from_month_display_order, $to_month_display_order);

    $random_id = 'ddcfg_'.$department_id."_".$from_month_display_order."_".time();
    
    $pg2->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "Department Cashflow";
    $data['x_axis_name'] = "Month";
    $data['y_axis_name'] = "Value";
    $data['graph1_name'] = "Collections";
    $data['graph2_name'] = "Payments";
    $data['graph3_name'] = "Cashflow";
    
    display_graph($pg2, $data, 1, $random_id);//line graph = 4
}

function department_department_cash_flow_graph_set_values(&$pg2, $department_id, $from_month_display_order, $to_month_display_order) 
{
        $title = "";

        $openning_balance = get_department_cf_openning_balance($department_id, $from_month_display_order);
            
        $result = get_months_range_by_display_order($from_month_display_order, $to_month_display_order);
        $i = 0;
        while ($month = db_fetch($result)) {

            $name = $month["description"];
            if ($pg2 != null) {
                $pg2->x[$i] = $name;
                
                $collections = get_department_cash_flow_collections_month_total($department_id, $month['display_order']);
                $payments = get_department_cash_flow_payments_month_total($department_id, $month['display_order']);                
                $overhead = get_department_cash_flow_overhead_month_total($department_id, $month['display_order']);  
                $payments_plus_overhead = $payments + $overhead;
                $cashflow =  $openning_balance + $collections - $payments_plus_overhead;         

                $pg2->y[$i] = $collections;
                $pg2->z[$i] = $payments_plus_overhead;
                $pg2->z1[$i] = $cashflow;
                
                $pg2->colour_type[$i] = set_bar_colour_type($cashflow);
                
                $openning_balance = $cashflow;
            }
            $i++;
        }
        return $title;
}

/*
function get_department_cf_openning_balance($department_id, $to_month_display_order){
    
        $from_month_display_order = 0;

        $before_month = get_before_month_by_display_order($to_month_display_order);
        $before_month_display_order = $before_month['display_order'];
    
        $invoice_receipts = get_department_cf_department_invoice_receipts($department_id, $from_month_display_order, $before_month_display_order); 
        $credit_note_payents = get_department_cf_department_credit_note_payments($department_id, $from_month_display_order, $before_month_display_order); 
        $invoice_receipts_credit_note_payments = $invoice_receipts + $credit_note_payents;
        
        $sub_invoice_payments = get_department_cf_subcontractor_invoice_payments($department_id, $from_month_display_order, $before_month_display_order);
        $sub_credit_note_receipts = get_department_cf_subcontractor_credit_note_receipts($department_id, $from_month_display_order, $before_month_display_order);
        $sub_invoice_payments_credit_notes = $sub_invoice_payments + $sub_credit_note_receipts;
        
        $iou_petty_cash_payments = get_department_cf_iou_payments($department_id, $from_month_display_order, $before_month_display_order);
        $iou_advance_payments = get_department_cf_iou_advance_payments($department_id, $from_month_display_order, $before_month_display_order);
        $iou_petty_cash_payments_advance = $iou_petty_cash_payments + $iou_advance_payments;
        
        $utility_bill_payments = get_department_cf_utility_bill_payments($department_id, $from_month_display_order, $before_month_display_order);
        $utility_bill_returns = get_department_cf_utility_bill_returns($department_id, $from_month_display_order, $before_month_display_order);
        $utility_bill_payemnts_and_returns = $utility_bill_payments + $utility_bill_returns;
        
        $transport = get_department_cf_transport_payments($department_id, $from_month_display_order, $before_month_display_order);
        
        $direct_purchase_payments = get_department_cf_direct_purchase_payments($department_id, $from_month_display_order, $before_month_display_order);
        $direct_purchase_returns = get_department_cf_direct_purchase_credit_notes($department_id, $from_month_display_order, $before_month_display_order);
        $direct_purchase_payments_and_returns = $direct_purchase_payments + $direct_purchase_returns;
        
        $material_issue = get_department_cf_material_issues($department_id, $from_month_display_order, $before_month_display_order);
        $material_returns = get_department_cf_material_returns($department_id, $from_month_display_order, $before_month_display_order);
        $material_issues_and_returns = $material_issue - $material_returns;
        
        $project_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;

        $overhead = get_department_cf_overhead($department_id, $from_month_display_order, $before_month_display_order);        
        
        $opening_balance = $invoice_receipts_credit_note_payments - ($project_payments + $overhead);
        return $opening_balance;    
}
 * *
 */

function get_department_cf_department_invoice_receipts($department_id, $from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_PROJECT_INVOICE_RECEIPTS;
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_id, $receipts_types_in);
}

function get_department_cf_department_credit_note_payments($department_id, $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_id, $receipts_types_in);
}


function get_department_cf_material_issues($department_id, $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_id, $receipts_types_in);
}

function get_department_cf_material_returns($department_id, $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_id, $receipts_types_in);
}


function get_department_cf_direct_purchase_credit_notes($department_id, $from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_SITE_MATERIAL_RETURNS;
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_id, $receipts_types_in);
}

/*
function get_department_cf_overhead($department_id, $from_month_display_order, $to_month_display_order)
{    
        $receipts_types_in = SCVT_SUBCONTRACTOR_INVOICES;    
        $sub_invoices = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_id, $receipts_types_in);        
        $receipts_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_id, $receipts_types_in);
        $sub_invoices_credit_notes = $sub_invoices + $sub_credit_notes;
        
        
        $receipts_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_id, $receipts_types_in);
        
        
        $receipts_types_in = SCVT_UTILITY_BILLS;    
        $utility_bill = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_id, $receipts_types_in);        
        $receipts_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_id, $receipts_types_in);
        $utility_bills_and_credits = $utility_bill - $utility_credit;
        
        
        $receipts_types_in = SCVT_VEHICLE_RUNNING_CHART;
        $transport = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_id, $receipts_types_in);
        
        
        $receipts_types_in = SCVT_PR_ISSUES;
        $direct_purchases = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_id, $receipts_types_in);
        $receipts_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_id, $receipts_types_in);        
        $direct_purchases_and_returns = $direct_purchases - $direct_purchase_returns;
        
        
        $receipts_types_in = SCVT_MATERIAL_ISSUES;
        $material_issue = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_id, $receipts_types_in);
        $receipts_types_in = SCVT_MATERIAL_RETURNS;
        $material_returns = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_id, $receipts_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_id);
        $overhead = $project_expenses * $average_overhead_factor;
        
        return $overhead;
}
 * 
 */

function get_department_cash_flow_payments_month_total($department_id, $month_display_order){
    
        $from_month_display_order = $to_month_display_order = $month_display_order;
    
        $sub_invoice_payments = get_department_cf_subcontractor_invoice_payments($department_id, $from_month_display_order, $to_month_display_order);
        $sub_credit_note_receipts = get_department_cf_subcontractor_credit_note_receipts($department_id, $from_month_display_order, $to_month_display_order);
        $sub_invoice_payments_credit_notes = $sub_invoice_payments + $sub_credit_note_receipts;
        
        $iou_petty_cash_payments = get_department_cf_iou_payments($department_id, $from_month_display_order, $to_month_display_order);
        $iou_advance_payments = get_department_cf_iou_advance_payments($department_id, $from_month_display_order, $to_month_display_order);
        $iou_petty_cash_payments_advance = $iou_petty_cash_payments + $iou_advance_payments;
        
        $utility_bill_payments = get_department_cf_utility_bill_payments($department_id, $from_month_display_order, $to_month_display_order);
        $utility_bill_returns = get_department_cf_utility_bill_credit_note_returns($department_id, $from_month_display_order, $to_month_display_order);
        $utility_bill_payemnts_and_returns = $utility_bill_payments + $utility_bill_returns;
        
        $transport = get_department_cf_transport_payments($department_id, $from_month_display_order, $to_month_display_order);
        
        $direct_purchase_payments = get_department_cf_direct_purchase_payments($department_id, $from_month_display_order, $to_month_display_order);
        $direct_purchase_returns = get_department_cf_direct_purchase_credit_notes($department_id, $from_month_display_order, $to_month_display_order);
        $direct_purchase_payments_and_returns = $direct_purchase_payments + $direct_purchase_returns;
        
        $material_issue = get_department_cf_material_issues($department_id, $from_month_display_order, $to_month_display_order);
        $material_returns = get_department_cf_material_returns($department_id, $from_month_display_order, $to_month_display_order);
        $material_issues_and_returns = $material_issue - $material_returns;
        
        $project_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;
    
        return $project_payments;    
}

function get_department_cash_flow_overhead_month_total($department_id, $month_display_order){
    
    $overhead = get_department_cf_overhead($department_id, $month_display_order, $month_display_order);
    
    return $overhead;
}

function get_department_cash_flow_collections_month_total($department_id, $month_display_order){
    
        $from_month_display_order = $to_month_display_order = $month_display_order;
        
        $invoice_receipts = get_department_cf_department_invoice_receipts($department_id, $from_month_display_order, $to_month_display_order); 
        $credit_note_payents = get_department_cf_department_credit_note_payments($department_id, $from_month_display_order, $to_month_display_order); 
        $invoice_receipts_credit_note_payments = $invoice_receipts + $credit_note_payents;
        
        return $invoice_receipts_credit_note_payments;
}

?>