<?php


function company_cashflow_table_month_wise_display($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'"; 
    }
        
    display_title("Company Cashflow");
    
    start_table(TABLESTYLE, "width=95%");
    $th = array("Month", 
                nl2br("Customer ".PHP_EOL."Payments"), 
                nl2br("Bank ".PHP_EOL."Deposits"), 
                nl2br("Total ".PHP_EOL."Collections"), 
                nl2br("Supplier ".PHP_EOL."Payments"), 
                nl2br("IOU ".PHP_EOL."Payments"), 
                nl2br("IOU ".PHP_EOL."Advances"), 
                nl2br("Bank ".PHP_EOL."Payments"), 
                nl2br("Salary ".PHP_EOL."Expenses"), 
                nl2br("Loan ".PHP_EOL."Payments"), 
                nl2br("Total ".PHP_EOL."Payments"), 
                nl2br("Month ".PHP_EOL."Cashflow"), 
                nl2br("Cumulative ".PHP_EOL."Cashflow"));

    table_header($th);
    
    start_row();
    $openning_balance = get_company_cf_openning_balance($company_codes_in, $from_month_display_order);
    label_cell(text_bold("Openning Balance"), "colspan=12");

    if($openning_balance >= 0){
        amount_cell($openning_balance);
    }
    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_customer_payment_receipts = 
            $total_bank_deposits = 
            $total_supplier_payments = 
            $total_iou_payments = 
            $total_iou_advance_payments = 
            $total_bank_payments = 
            $total_salary_expenses = 
            $total_loan_payments = $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
        $customer_payment_receipts = get_company_cf_company_customer_payment_receipts($month_display_order, $month_display_order); 
        amount_cell($customer_payment_receipts);
        $total_customer_payment_receipts += $customer_payment_receipts;
        
        $bank_deposits = get_company_cf_company_bank_deposits($month_display_order, $month_display_order); 
        amount_cell($bank_deposits);
        $total_bank_deposits += $bank_deposits;
        
        $collections = $customer_payment_receipts + $bank_deposits;
        amount_cell($collections);
        
        //Payments
        $supplier_payments = get_company_cf_company_supplier_payments($month_display_order, $month_display_order);
        amount_cell($supplier_payments);
        $total_supplier_payments += $supplier_payments;
        
        $iou_payments = get_company_cf_iou_payments($month_display_order, $month_display_order);
        amount_cell($iou_payments);
        $total_iou_payments += $iou_payments;
        
        $iou_advance_payments = get_company_cf_iou_advance_payments($month_display_order, $month_display_order);
        amount_cell($iou_advance_payments);
        $total_iou_advance_payments += $iou_advance_payments;
        
        $bank_payments = get_company_cf_company_bank_payments($month_display_order, $month_display_order);
        amount_cell($bank_payments);
        $total_bank_payments += $bank_payments;
        
        $salary_expenses = get_company_cf_company_salary_expenses($company_codes_in, $month_display_order, $month_display_order);
        amount_cell($salary_expenses);
        $total_salary_expenses += $salary_expenses;
        
        $loan_payments = get_company_cf_loan_payments($month_display_order, $month_display_order);
        amount_cell($loan_payments);
        $total_loan_payments += $loan_payments;
        
        $payments = $supplier_payments + $iou_payments + $iou_advance_payments + $bank_payments + $salary_expenses + $loan_payments;
        amount_cell($payments);
        
        $outstandings = $collections - $payments;
        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_customer_payment_receipts, true);
        amount_cell($total_bank_deposits, true);
        $total_collections = $total_customer_payment_receipts + $total_bank_deposits;
        amount_cell($total_collections, true);
        
        amount_cell($total_supplier_payments, true);
        amount_cell($total_iou_payments, true);
        amount_cell($total_iou_advance_payments, true);
        amount_cell($total_bank_payments, true);
        amount_cell($total_salary_expenses, true); 
        amount_cell($total_loan_payments, true); 
        $total_payments = $total_supplier_payments + $total_iou_payments + $total_iou_advance_payments + $total_bank_payments + $total_salary_expenses + $total_loan_payments;
        amount_cell($total_payments, true); 
        label_cell('');

        $outstandings_total = $total_collections - $total_payments;
        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();
}

