<?php

function company_company_cash_flow_graph_display(&$pg5, $group_company_code, $from_month_display_order, $to_month_display_order)
{
    if($group_company_code == "SAW"){
        $company_codes_in = "'SAW', 'SAWC', 'LPS'"; 
    }
    elseif($group_company_code == "VEE"){
        $company_codes_in = "'VEE', 'VEEC'"; 
    }
    
    company_company_cash_flow_graph_set_values($pg5, $company_codes_in, $from_month_display_order, $to_month_display_order);

    $random_id = "ddcfg_SAW_".$from_month_display_order."_".time();
    
    $pg5->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "Company 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($pg5, $data, 1, $random_id);//line graph = 4
}

function company_company_cash_flow_graph_set_values(&$pg5, $company_codes_in, $from_month_display_order, $to_month_display_order) 
{
        $title = "";

        $openning_balance = get_company_cf_openning_balance($company_codes_in, $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 ($pg5 != null) {
                $pg5->x[$i] = $name;
                
                $collections = get_company_cash_flow_collections_month_total($month['display_order']);
                $payments = get_company_cash_flow_payments_month_total($company_codes_in, $month['display_order']);                
                $cashflow =  $openning_balance + $collections - $payments;         

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

function get_company_cf_openning_balance($company_codes_in, $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'];
    
        // Collections
        $customer_payment_receipts = get_company_cf_company_customer_payment_receipts($from_month_display_order, $before_month_display_order); 
        
        $bank_deposits = get_company_cf_company_bank_deposits($from_month_display_order, $before_month_display_order); 
        
        $collections = $customer_payment_receipts + $bank_deposits;
        
        //Payments
        $supplier_payments = get_company_cf_company_supplier_payments($from_month_display_order, $before_month_display_order); 
        
        $iou_payments = get_company_cf_iou_payments($from_month_display_order, $before_month_display_order); 
        
        $iou_advance_payments = get_company_cf_iou_advance_payments($from_month_display_order, $before_month_display_order); 
        
        $bank_payments = get_company_cf_company_bank_payments($from_month_display_order, $before_month_display_order); 

        $salary_expenses = get_company_cf_company_salary_expenses($company_codes_in, $from_month_display_order, $before_month_display_order); 
        
        $loan_payments = get_company_cf_loan_payments($from_month_display_order, $before_month_display_order); 
        
        
        $payments = $supplier_payments + $iou_payments + $iou_advance_payments + $bank_payments + $salary_expenses + $loan_payments;      
        
        $opening_balance = $collections - $payments;
        return $opening_balance;    
}

function get_company_cf_company_customer_payment_receipts($from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_COMP_CUSTOMER_PAYMENT_RECEIPTS;
    return get_summary_company_month_range_value($from_month_display_order, $to_month_display_order, $receipts_types_in);
}

function get_company_cf_company_bank_deposits($from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_COMP_BANK_DEPOSITS;
    return get_summary_company_month_range_value($from_month_display_order, $to_month_display_order, $receipts_types_in);
}


function get_company_cf_company_supplier_payments($from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_COMP_SUPPLIER_PAYMENTS;
    return get_summary_company_month_range_value($from_month_display_order, $to_month_display_order, $receipts_types_in);
}

function get_company_cf_company_bank_payments($from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_COMP_BANK_PAYMENTS;
    return get_summary_company_month_range_value($from_month_display_order, $to_month_display_order, $receipts_types_in);
}

function get_company_cf_company_salary_expenses($company_codes_in, $from_month_display_order, $to_month_display_order){
    
    $value_types_in = SCVT_TOTAL_SALARY_COST;
    return get_summary_payrolls_month_range_company_total($company_codes_in, $value_types_in, $from_month_display_order, $to_month_display_order);
}

function get_company_cf_loan_payments($from_month_display_order, $to_month_display_order){
    $month_loan_payments = 0;
    $loan_payments = get_month_loan_repayments_summary($from_month_display_order, $to_month_display_order);
    if(isset($loan_payments['capital_paid'])){
        $capital_paid = $loan_payments['capital_paid'];
        $interest_paid = $loan_payments['interest_paid'];
        $late_interest_paid = $loan_payments['late_interest_paid'];
        $month_loan_payments = $capital_paid + $interest_paid + $late_interest_paid;
    }

    return $month_loan_payments;
    
}

function get_company_cf_iou_payments($from_month_display_order, $to_month_display_order)
{
    $value_types_in = SCVT_I_OWE_YOU_PAYMENTS;
    
    return get_summary_projects_month_range_company_value($from_month_display_order, $to_month_display_order, $value_types_in);
}

function get_company_cf_iou_advance_payments($from_month_display_order, $to_month_display_order)
{
    $value_types_in = SCVT_I_OWE_YOU_ADVANCES;
    
    return get_summary_projects_month_range_company_value($from_month_display_order, $to_month_display_order, $value_types_in);
}


function get_company_cash_flow_payments_month_total($company_codes_in, $month_display_order){
    
        $from_month_display_order = $to_month_display_order = $month_display_order;
    
        $supplier_payments = get_company_cf_company_supplier_payments($month_display_order, $month_display_order);
        
        $iou_payments = get_company_cf_iou_payments($from_month_display_order, $to_month_display_order);
        
        $iou_advance_payments = get_company_cf_iou_advance_payments($from_month_display_order, $to_month_display_order);
        
        $bank_payments = get_company_cf_company_bank_payments($month_display_order, $month_display_order);
        
        $salary_expenses = get_company_cf_company_salary_expenses($company_codes_in, $from_month_display_order, $to_month_display_order);
        
        $loan_payments = get_company_cf_loan_payments($month_display_order, $month_display_order);
        
        $total_payments = $supplier_payments + 
                            $iou_payments + 
                            $iou_advance_payments + 
                            $bank_payments + 
                            $salary_expenses + 
                            $loan_payments;
    
        return $total_payments;    
}


function get_company_cash_flow_collections_month_total($month_display_order)
{
        
        $customer_payment_receipts = get_company_cf_company_customer_payment_receipts($month_display_order, $month_display_order); 
        
        $bank_deposits = get_company_cf_company_bank_deposits($month_display_order, $month_display_order); 
        
        return $customer_payment_receipts + $bank_deposits;
}

?>