<?php

function display_company_cash_folw_filetrs() {
    start_table(TABLESTYLE_NOBORDER);

        start_row();
            $years_array = get_years_array();
                custom_values_list_cells('Year:', 'year_id', $years_array, null, 'Select an year', true);
            $months_array = get_months_array();
                custom_values_list_cells('Month:', 'month_id', $months_array, null, 'Select a month', true);
            submit_cells('Refresh', _("Refresh"), '', _('Refresh'), false, ICON_SUBMIT);
        end_row();

    end_table(1);
}

function display_company_cash_folw_tabs() {
    
    
    
        tabbed_content_start('tabs', array( 'cash_flow' => array(_('&Cash Flow'), 1),
                                            'bank_accounts' => array(_('&Bank Accounts'), 1),
                                            'loans' => array(_('&Loans'), 1)
//                                            'deposits' => array(_('Deposits'), 1)
                                            ));    
        
        br(2);
	
            switch (get_post('_tabs_sel')) {
		default:
		case 'cash_flow':
                    if ($_POST['year_id'] > 0 && $_POST['month_id'] > 0) {
                        display_company_cash_folw($_POST['year_id'], $_POST['month_id']);
                    } else {
                         display_note('Please select an year and a month!', 1, 1, 'style=color:red');
                    }
                        
			break;
                        
		case 'bank_accounts':
                    if ($_POST['year_id'] > 0 && $_POST['month_id'] > 0) {
                        display_company_cash_folw_bank_accounts_summary($_POST['year_id'], $_POST['month_id'], 0);      br(2);
                        display_company_cash_folw_bank_accounts_summary($_POST['year_id'], $_POST['month_id'], 1);
                    } else {
                         display_note('Please select an year and a month!', 1, 1, 'style=color:red');
                    }
                    
			break;
                        
		case 'loans':
                    if ($_POST['year_id'] > 0 && $_POST['month_id'] > 0) {
                        display_company_loans($_POST['year_id'], $_POST['month_id'], 0);      br(2);
                    } else {
                         display_note('Please select an year and a month!', 1, 1, 'style=color:red');
                    }
                    
			break;
            }
            br();
        tabbed_content_end();

        
}



function display_company_loans($year_id, $month_id) {
    $from_date = "$year_id-$month_id-01";
    $to_date = date("Y-m-t", strtotime($from_date));
    
    if (! ($to_date < date2sql(Today()))) {
        $to_date = date2sql(Today());
    }
    
        display_heading('Company Loans Details');      br();

            start_table(TABLESTYLE, "width=98%");

                $th = array(_("Loan"), _("Reference"), _("Type"), _("Loan <br> Capital"), _("Loan <br> Interest"), _("Start <br> Date"), _("Last Paid On"), _("On Hold"), 
                            _("Total Capital Paid"), _("Total Interest Paid"), _("Total Late Interest Paid"), _("Total Outstanding"));
                    table_header($th);
                    
                        $result = get_company_loans_details($to_date);
                            while ($row = db_fetch($result)) {
                                start_row();
                                    label_cell($row['description']);
                                    label_cell($row['name']);
                                    label_cell($row['loan_type_name']);
                                    label_cell(number_format($row['loan_amount'], 2), 'align=right');
                                    label_cell(number_format($row['interest'], 2), 'align=right');
                                    label_cell(sql2date($row['start_date']), 'align=center');
                                    label_cell(sql2date($row['last_payment_date']), 'align=center');
                                    label_cell(get_yes_no_name($row['inactive']), 'align=center');
                                    label_cell(number_format($row['total_captal_paid'], 2), 'align=right');
                                    label_cell(number_format($row['total_interest_paid'], 2), 'align=right');
                                    label_cell(number_format($row['total_late_interest_paid'], 2), 'align=right');
                                    
                                    $capital_outstanding = $row['loan_amount'] - $row['total_captal_paid'];
                                    $interest_outstanding = $row['interest'] - $row['total_interest_paid'];
                                    
                                    $total_outstanding = 0;
                                        if ($capital_outstanding > 0) {
                                            $total_outstanding += $capital_outstanding;
                                        }
                                        if ($interest_outstanding > 0) {
                                            $total_outstanding += $interest_outstanding;
                                        }
                                    label_cell(number_format($total_outstanding, 2), 'align=right');
                                end_row();
                            }
                    
            end_table();
}


        
function get_years_array() {
    $years_array = array();
            
        $result = get_all_list_values('YEAR');
            while ($row = db_fetch($result)) {
                $years_array[$row['value_name']] = $row['value_name'];
            }
            
            krsort($years_array);
            
        return $years_array;
}

function get_months_array() {
    $months_array = array();
            
        $result = get_all_list_values('MONTHS');
            while ($row = db_fetch($result)) {
                $months_array[$row['value_code']] = $row['value_name'];
            }
            
            ksort($months_array);
            
        return $months_array;
}

function display_company_cash_folw($year_id, $month_id) {
    $first_month_start = $from_date = "$year_id-$month_id-01";
    $last_month_end_date = date("Y-m-t", strtotime($from_date));
    
    $total_months_in_array = $total_months_out_array = array();
    
        display_heading("Cash Flow");      br();

            $months_array = get_previous_months_array_by_date($from_date, 5);
            
            start_table(TABLESTYLE, "width=90%");

                $set_first_month_start = false;
                $th = array(_("Type"));
                    foreach ($months_array as $month_begin => $value) {
                        if ($set_first_month_start === false) {
                            $first_month_start = $month_begin;
                        }
                        
                        $set_first_month_start = true;
                        
                        $col_name = date('F Y', strtotime($month_begin));
                            $th[] = $col_name;
                            
                            $total_months_in_array[$month_begin] = 0;
                            $total_months_out_array[$month_begin] = 0;
                    }
                    
                        table_header($th);
                        
                            $heder_length = count($th);

                            start_row();
                                label_cell(text_bold("Cash In"), "colspan=$heder_length style=background-color:#0ec9ba align=center"); 
                            end_row();

                            display_company_cash_flow_ins($months_array, $total_months_in_array, $first_month_start, $last_month_end_date);

                            
                            start_row();
                                label_cell(text_bold("Cash Out"), "colspan=$heder_length style=background-color:#0ec9ba align=center");
                            end_row();

                            display_company_cash_flow_outs($months_array, $total_months_out_array, $first_month_start, $last_month_end_date);

                            
                            
                            start_row();
                                label_cell(text_bold("Cash Flow Summary"), "colspan=$heder_length style=background-color:#0ec9ba align=center"); 
                            end_row();
                            
                            start_row();
                                label_cell(text_bold("Month Net Cash Flow:"), 'align=right');
                                
                                foreach ($months_array as $month_begin => $value) {
                                    $month_ins = $total_months_in_array[$month_begin];
                                    $month_outs = $total_months_out_array[$month_begin];

                                    $profit_lost = ($month_ins - $month_outs);
                                    $color = ($profit_lost > 0) ? 'green' : 'red';

                                    label_cell(text_bold(number_format($profit_lost, 2)), "style=color:$color; align=right");
                                }
                            end_row();
                            
                            start_row();
                                label_cell(text_bold("6 Months Accumulated Cash Flow:"), 'align=right');
                                $accumulated = 0;
                                foreach ($months_array as $month_begin => $value) {
                                    $month_ins = $total_months_in_array[$month_begin];
                                    $month_outs = $total_months_out_array[$month_begin];

                                    $profit_lost = ($month_ins - $month_outs);
                                    $accumulated += $profit_lost;
                                        
                                    $color = ($accumulated > 0) ? 'green' : 'red';

                                    label_cell(text_bold(number_format($accumulated, 2)), "style=color:$color; align=right");
                                }
                            end_row();
                            
                            
                            display_loans_summary($months_array, $heder_length);
                            
                            start_row();
                                vertical_space("height=50px colspan=$heder_length");
                            end_row();
                            
                            start_row();
                                label_cell(text_bold("Bank Accounts Summary"), "colspan=$heder_length style=background-color:#0ec9ba align=center"); 
                            end_row();
                            display_bank_accounts($months_array, $heder_length, 0);
                            
                            display_bank_accounts($months_array, $heder_length, 1);
            end_table(3);
}

function get_previous_months_array_by_date($from_date, $no_months_backward=5) {
    $months_array = array();
    
        $y = $no_months_backward;
    
            for ($x=0; $x<=$y; $x++) {
                $pre_month_begin = date('Y-m-01', strtotime("-$no_months_backward months", strtotime($from_date)));
                $month_end_date = date("Y-m-t", strtotime($pre_month_begin));

                    $months_array[$pre_month_begin] = array('month_begin_date' => $pre_month_begin, 'month_end_date' => $month_end_date);

                        $no_months_backward--;
            }

                return $months_array;
}

function display_company_cash_flow_ins($months_array, &$total_months_in_array, $first_month_start, $last_month_end) {
    global $systypes_array;
    
    start_row();
        $first_row = true;

        foreach ($months_array as $key => $month) {
            $total_cash_in = 0;
            display_customer_payments_summary(ST_CUSTPAYMENT, $month['month_begin_date'], $month['month_end_date'], $total_cash_in, $first_row);  
            $total_months_in_array[$key] += $total_cash_in;
            $first_row = false;
        }
    end_row();
        
    start_row();
        $first_row = true;
        foreach ($months_array as $key => $month) {
            $total_cash_in = 0;
            display_iou_payment_returns_summary(ST_I_OWE_YOU_PAYMENT, $month['month_begin_date'], $month['month_end_date'], $total_cash_in, $first_row);  
            $total_months_in_array[$key] += $total_cash_in;
            $first_row = false;
        }
    end_row();
        
    start_row();
        label_cell(text_bold("Bank Account Deposits"), 'colspan=7');
    end_row();
    
        display_bank_trans_deposit_summary(ST_BANKDEPOSIT, $first_month_start, $last_month_end, PT_CUSTOMER, $months_array, $total_months_in_array);
        display_bank_trans_deposit_summary(ST_BANKDEPOSIT, $first_month_start, $last_month_end, PT_MISC, $months_array, $total_months_in_array);
//        display_bank_trans_deposit_summary(ST_BANKDEPOSIT, $first_month_start, $last_month_end, PT_QUICKENTRY, $months_array, $total_months_in_array);
//        display_bank_trans_deposit_summary(ST_BANKDEPOSIT, $first_month_start, $last_month_end, PT_SUPPLIER, $months_array, $total_months_in_array);
//        display_bank_trans_deposit_summary(ST_BANKDEPOSIT, $first_month_start, $last_month_end, PT_EMPLOYEE, $months_array, $total_months_in_array);
        
    start_row();
        label_cell(text_bold("Bank Account Payments"), 'colspan=7');
    end_row();
        display_bank_trans_payment_summary(ST_BANKPAYMENT, $first_month_start, $last_month_end, PT_CUSTOMER, $months_array, $total_months_in_array);
        
        
    start_row();
        vertical_space('height=17px colspan=7');
    end_row();    
        
    start_row();
        label_cell(text_bold($systypes_array[ST_CASH_RECEIPT]."s"), 'colspan=7');
    end_row();    
        display_journal_trans_deposit_summary(ST_CASH_RECEIPT, $first_month_start, $last_month_end, PT_CUSTOMER, $months_array, $total_months_in_array);
//        display_journal_trans_deposit_summary(ST_CASH_RECEIPT, $first_month_start, $last_month_end, PT_SUPPLIER, $months_array, $total_months_in_array);
//        display_journal_trans_deposit_summary(ST_CASH_RECEIPT, $first_month_start, $last_month_end, PT_EMPLOYEE, $months_array, $total_months_in_array);
        display_journal_trans_deposit_summary(ST_CASH_RECEIPT, $first_month_start, $last_month_end, PT_MISC, $months_array, $total_months_in_array);
//        display_journal_trans_deposit_summary(ST_CASH_RECEIPT, $first_month_start, $last_month_end, PT_QUICKENTRY, $months_array, $total_months_in_array);
        
    start_row();
        label_cell(text_bold($systypes_array[ST_CASH_PAYMENT]."s"), 'colspan=7');
    end_row();    
//        display_journal_trans_deposit_summary(ST_CASH_PAYMENT, $first_month_start, $last_month_end, PT_SUPPLIER, $months_array, $total_months_in_array);
//        display_journal_trans_deposit_summary(ST_CASH_PAYMENT, $first_month_start, $last_month_end, PT_EMPLOYEE, $months_array, $total_months_in_array);
//        display_journal_trans_deposit_summary(ST_CASH_PAYMENT, $first_month_start, $last_month_end, PT_MISC, $months_array, $total_months_out_array);
//        display_journal_trans_deposit_summary(ST_CASH_PAYMENT, $first_month_start, $last_month_end, PT_QUICKENTRY, $months_array, $total_months_out_array);
        display_journal_trans_deposit_summary(ST_CASH_PAYMENT, $first_month_start, $last_month_end, PT_CUSTOMER, $months_array, $total_months_in_array);
    
        
//    start_row();
//        $first_row = true;
//        foreach ($months_array as $key => $month) {
//            $total_cash_in = 0;
//            display_company_loan_repayment_returns_summary(ST_COMP_LOAN_REPAYMENT, $month['month_begin_date'], $month['month_end_date'], $total_cash_in, $first_row);  
//            $total_months_in_array[$key] += $total_cash_in;
//            $first_row = false;
//        }
//    end_row();
        
    start_row();
        vertical_space('height=17px colspan=7');
    end_row();
    
    start_row();
    
    $first_row = true;
    start_row();
        label_cell(text_bold("Bank Account Other Payments"), 'colspan=7');
    end_row();
//        foreach ($months_array as $key => $month) {
//            $total_cash_in = 0;
//            display_bank_account_other_receipts_summary(ST_JOURNAL, $month['month_begin_date'], $month['month_end_date'], $total_cash_in, $first_row);  
            display_bank_account_other_receipts_summary(ST_JOURNAL, $first_month_start, $last_month_end, $months_array, $total_months_in_array);
//            $total_months_in_array[$key] += $total_cash_in;
//            $first_row = false;
//        }
    end_row();
        
    start_row();
        $first_row = true;
        foreach ($months_array as $key => $month) {
            $total_cash_in = 0;
            display_month_cash_in_adjustments_summary(ST_MONTH_CASH_IN_AJUSTMENT, $month['month_begin_date'], $month['month_end_date'], $total_cash_in, $first_row);  
            $total_months_in_array[$key] += $total_cash_in;
            $first_row = false;
        }
    end_row();
        
//        display_suplier_credit_notes_summary(ST_SUPPCREDIT, $from_date, $to_date, $total_cash_in);  
        
        start_row();
            $first_row = true;
                foreach ($total_months_in_array as $month_total) {
                    print_customer_payments_cash_in_total($month_total, $first_row);

                        $first_row = false;
                }
        end_row();
}

function display_company_cash_flow_outs($months_array, &$total_months_out_array, $first_month_start, $last_month_end) {
    global $systypes_array;
    
    start_row();
        $first_row = true;

        foreach ($months_array as $key => $month) {
            $total_cash_out = 0;
            print_supplier_payments_summary(ST_SUPPAYMENT, $month['month_begin_date'], $month['month_end_date'], $total_cash_out, $first_row);  
            $total_months_out_array[$key] += $total_cash_out;
            $first_row = false;
        }
    end_row();
    
    start_row();
        label_cell(text_bold("Bank Account Payments"), 'colspan=7');
    end_row();
    
        display_bank_trans_payment_summary(ST_BANKPAYMENT, $first_month_start, $last_month_end, PT_SUPPLIER, $months_array, $total_months_out_array);
        display_bank_trans_payment_summary(ST_BANKPAYMENT, $first_month_start, $last_month_end, PT_EMPLOYEE, $months_array, $total_months_out_array);
        display_bank_trans_payment_summary(ST_BANKPAYMENT, $first_month_start, $last_month_end, PT_MISC, $months_array, $total_months_out_array);
//        display_bank_trans_payment_summary(ST_BANKPAYMENT, $first_month_start, $last_month_end, PT_QUICKENTRY, $months_array, $total_months_out_array);
//        display_bank_trans_payment_summary(ST_BANKPAYMENT, $first_month_start, $last_month_end, PT_CUSTOMER, $months_array, $total_months_out_array);
        
     start_row();
        label_cell(text_bold("Bank Account Deposits"), 'colspan=7');
    end_row();
        display_bank_trans_deposit_summary(ST_BANKDEPOSIT, $first_month_start, $last_month_end, PT_SUPPLIER, $months_array, $total_months_out_array);
        display_bank_trans_deposit_summary(ST_BANKDEPOSIT, $first_month_start, $last_month_end, PT_EMPLOYEE, $months_array, $total_months_out_array);
        
    start_row();
        vertical_space('height=17px colspan=7');
    end_row();
        
    start_row();
        label_cell(text_bold($systypes_array[ST_CASH_PAYMENT]."s"), 'colspan=7');
    end_row();
    
        display_journal_trans_deposit_summary(ST_CASH_PAYMENT, $first_month_start, $last_month_end, PT_SUPPLIER, $months_array, $total_months_out_array);
        display_journal_trans_deposit_summary(ST_CASH_PAYMENT, $first_month_start, $last_month_end, PT_EMPLOYEE, $months_array, $total_months_out_array);
        display_journal_trans_deposit_summary(ST_CASH_PAYMENT, $first_month_start, $last_month_end, PT_MISC, $months_array, $total_months_out_array);
//        display_journal_trans_deposit_summary(ST_CASH_PAYMENT, $first_month_start, $last_month_end, PT_QUICKENTRY, $months_array, $total_months_out_array);
//        display_journal_trans_deposit_summary(ST_CASH_PAYMENT, $first_month_start, $last_month_end, PT_CUSTOMER, $months_array, $total_months_out_array);
    
    start_row();
        label_cell(text_bold($systypes_array[ST_CASH_RECEIPT]."s"), 'colspan=7');
    end_row();    
//        display_journal_trans_deposit_summary(ST_CASH_RECEIPT, $first_month_start, $last_month_end, PT_CUSTOMER, $months_array, $total_months_in_array);
        display_journal_trans_deposit_summary(ST_CASH_RECEIPT, $first_month_start, $last_month_end, PT_SUPPLIER, $months_array, $total_months_out_array);
        display_journal_trans_deposit_summary(ST_CASH_RECEIPT, $first_month_start, $last_month_end, PT_EMPLOYEE, $months_array, $total_months_out_array);
//        display_journal_trans_deposit_summary(ST_CASH_RECEIPT, $first_month_start, $last_month_end, PT_MISC, $months_array, $total_months_in_array);
//        display_journal_trans_deposit_summary(ST_CASH_RECEIPT, $first_month_start, $last_month_end, PT_QUICKENTRY, $months_array, $total_months_in_array);
      
        
    start_row();
        vertical_space('height=17px colspan=7');
    end_row();
    
    start_row();
        $first_row = true;
        foreach ($months_array as $key => $month) {
            $total_cash_out = 0;
            display_iou_bank_payments_summary(ST_IOU_BANKPAYMENT, $month['month_begin_date'], $month['month_end_date'], $total_cash_out, $first_row);  
            $total_months_out_array[$key] += $total_cash_out;
            $first_row = false;
        }
    end_row();
        
    start_row();
        $first_row = true;
        foreach ($months_array as $key => $month) {
            $total_cash_out = 0;
            display_iou_bank_payments_summary(ST_IOU_BANKPAYMENT_RETURN, $month['month_begin_date'], $month['month_end_date'], $total_cash_out, $first_row);  
            $total_months_out_array[$key] += $total_cash_out;
            $first_row = false;
        }
    end_row();
        
    start_row();
        $first_row = true;
        foreach ($months_array as $key => $month) {
            $total_cash_out = 0;
            display_iou_payments_summary(ST_I_OWE_YOU_PAYMENT, $month['month_begin_date'], $month['month_end_date'], $total_cash_out, $first_row);  
            $total_months_out_array[$key] += $total_cash_out;
            $first_row = false;
        }
    end_row();
    
    start_row();
        $first_row = true;
        foreach ($months_array as $key => $month) {
            $total_cash_out = 0;
            display_iou_advances_summary(ST_I_OWE_YOU_ADVANCES, $month['month_begin_date'], $month['month_end_date'], $total_cash_out, $first_row);  
            $total_months_out_array[$key] += $total_cash_out;
            $first_row = false;
        }
    end_row();
    
    start_row();
        vertical_space('height=17px colspan=7');
    end_row();
    
    start_row();
        $first_row = true;
        foreach ($months_array as $key => $month) {
            $total_cash_out = 0;
            display_company_loan_repayments_summary(ST_COMP_LOAN_REPAYMENT, $month['month_begin_date'], $month['month_end_date'], $total_cash_out, $first_row);  
            $total_months_out_array[$key] += $total_cash_out;
            $first_row = false;
        }
    end_row();
    
            
    start_row();
        $first_row = true;
        foreach ($months_array as $key => $month) {
            $total_cash_in = 0;
            display_company_loan_repayment_returns_summary(ST_COMP_LOAN_REPAYMENT, $month['month_begin_date'], $month['month_end_date'], $total_cash_in, $first_row);  
            $total_months_out_array[$key] += $total_cash_in;
            $first_row = false;
        }
    end_row();
    
    start_row();
        vertical_space('height=17px colspan=7');
    end_row();
    
    start_row();
        $first_row = true;
        start_row();
            label_cell(text_bold("Bank Account Other Payments"), 'colspan=7');
        end_row();

        display_bank_account_other_payments_summary(ST_JOURNAL, $first_month_start, $last_month_end, $months_array, $total_months_out_array);  
    end_row();
    
    start_row();
        $first_row = true;
        foreach ($months_array as $key => $month) {
            $total_cash_out = 0;
            display_month_cash_out_adjustments_summary(ST_MONTH_CASH_OUT_AJUSTMENT, $month['month_begin_date'], $month['month_end_date'], $total_cash_out, $first_row);  
            $total_months_out_array[$key] += $total_cash_out;
            $first_row = false;
        }
    end_row();
    
//        print_customer_credit_notes_summary(ST_CUSTCREDIT, $from_date, $to_date, $total_cash_out);  

//        print_salaries_summary(ST_SALARY_ADVANCE_PAYMENT, $from_date, $to_date, $total_cash_out);  
        
        start_row();
            $first_row = true;
                foreach ($total_months_out_array as $month_total) {
                    print_customer_payments_cash_out_total($month_total, $first_row);

                        $first_row = false;
                }
        end_row();
}


function display_bank_trans_deposit_summary($tran_type, $first_month_start, $last_month_end, $person_type_id, $months_array, &$total_months_in_array) {
    global $payment_person_types;
    
    start_row();
        label_cell(spaces(15).$payment_person_types[$person_type_id], 'colspan=7');
    end_row();

        $gl_trans_types = get_bank_trans_gl_trans_types_by_filters($tran_type, $first_month_start, $last_month_end, $person_type_id);
            while ($gl_trans_type = db_fetch($gl_trans_types)) {
                $gl_trans_type_id = $gl_trans_type['gl_trans_type_id'];

                start_row();
                    $gl_trns_type = get_list_value($gl_trans_type_id);
                    label_cell(spaces(30).$gl_trns_type['value_name']);
                    $first_row = true;

                    foreach ($months_array as $key => $month) {
                        $month_begin_date = $month['month_begin_date'];
                        $month_end_date = $month['month_end_date'];

                        $total_cash_in = 0;

                        $total = get_bank_transactions_summary($tran_type, $month_begin_date, $month_end_date, $person_type_id, $gl_trans_type_id);
                        $total = abs($total);
                        
                        if(in_array($person_type_id, array(PT_SUPPLIER, PT_EMPLOYEE))){
                            $total *= -1;
                        }
                        
                            $parameters = "tran_type=".$tran_type."&from_date=".sql2date($month_begin_date)."&to_date=".sql2date($month_end_date).
                                          "&ptype=".$person_type_id."&gl_trans_type_id=".$gl_trans_type_id;
                            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                                label_cell($amount_link, "align=right");

                        $total_cash_in += $total;
                        $total_months_in_array[$key] += $total_cash_in;
                        $first_row = false;
                    }
                end_row();
            }
}

function display_bank_trans_payment_summary($tran_type, $first_month_start, $last_month_end, $person_type_id, $months_array, &$total_months_out_array) {
    global $payment_person_types;
    
    start_row();
        label_cell(spaces(15).$payment_person_types[$person_type_id], 'colspan=7');
    end_row();

        $gl_trans_types = get_bank_trans_gl_trans_types_by_filters($tran_type, $first_month_start, $last_month_end, $person_type_id);
            while ($gl_trans_type = db_fetch($gl_trans_types)) {
                $gl_trans_type_id = $gl_trans_type['gl_trans_type_id'];

                start_row();
                    $gl_trns_type = get_list_value($gl_trans_type_id);
                    label_cell(spaces(30).$gl_trns_type['value_name']);
                    $first_row = true;

                    foreach ($months_array as $key => $month) {
                        $month_begin_date = $month['month_begin_date'];
                        $month_end_date = $month['month_end_date'];

                        $total_cash_in = 0;

                        $total = get_bank_transactions_summary($tran_type, $month_begin_date, $month_end_date, $person_type_id, $gl_trans_type_id);
                        $total = abs($total);
                        
                        if(in_array($person_type_id, array(PT_CUSTOMER))){
                            $total *= -1;
                        }
                        
                            $parameters = "tran_type=".$tran_type."&from_date=".sql2date($month_begin_date)."&to_date=".sql2date($month_end_date).
                                          "&ptype=".$person_type_id."&gl_trans_type_id=".$gl_trans_type_id;
                            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                                label_cell($amount_link, "align=right");

                        $total_cash_in += $total;
                        $total_months_out_array[$key] += $total_cash_in;
                        $first_row = false;
                    }
                end_row();
            }
}

function display_journal_trans_deposit_summary($tran_type, $first_month_start, $last_month_end, $person_type_id, $months_array, &$total_months_in_array) {
    global $payment_person_types;
    
    start_row();
        label_cell(spaces(15).$payment_person_types[$person_type_id], 'colspan=7');
    end_row();

        $gl_trans_types = get_journal_trans_gl_trans_types_by_filters($tran_type, $first_month_start, $last_month_end, $person_type_id);
            while ($gl_trans_type = db_fetch($gl_trans_types)) {
                $gl_trans_type_id = $gl_trans_type['gl_trans_type_id'];

                start_row();
                    $gl_trns_type = get_list_value($gl_trans_type_id);
                    label_cell(spaces(30).$gl_trns_type['value_name']);
                    $first_row = true;

                    foreach ($months_array as $key => $month) {
                        $month_begin_date = $month['month_begin_date'];
                        $month_end_date = $month['month_end_date'];

                        $total_cash_in = 0;

                        $total = get_journal_transactions_summary($tran_type, $month_begin_date, $month_end_date, $person_type_id, $gl_trans_type_id);
                        $total = abs($total);
                        
                        if($tran_type == ST_CASH_PAYMENT){
                            if($person_type_id == PT_CUSTOMER){
                                $total *= -1;
                            }
                        }
                        if($tran_type == ST_CASH_RECEIPT){
                            if(in_array($person_type_id, array(PT_SUPPLIER, PT_EMPLOYEE))){
                                $total *= -1;
                            }
                        }
                        
                            $parameters = "tran_type=".$tran_type."&from_date=".sql2date($month_begin_date)."&to_date=".sql2date($month_end_date).
                                          "&ptype=".$person_type_id."&gl_trans_type_id=".$gl_trans_type_id;
                            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                                label_cell($amount_link, "align=right");

                        $total_cash_in += $total;
                        $total_months_in_array[$key] += $total_cash_in;
                        $first_row = false;
                    }
                end_row();
            }
}        

function display_customer_payments_summary($tran_type, $from_date, $to_date, &$total_cash_in, $first_row) {
    global $path_to_root;
    
    $total = get_customer_payments_summary($tran_type, $from_date, $to_date);
    $total = abs($total);
    
        if ($first_row === true) {
//            $link = "<a href='$path_to_root/sales/inquiry/customer_transactions_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Customer Payments" . "</a>";
//                label_cell($link);
                label_cell(text_bold("Customer Payments"));
        }
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
    
    $total_cash_in += $total;
}

function display_suplier_credit_notes_summary($tran_type, $from_date, $to_date, &$total_cash_in) {
    global $path_to_root;
    
    $total = get_suplier_credit_notes_summary($tran_type, $from_date, $to_date);
    $total = abs($total);
    
        start_row();
            $link = "<a href='$path_to_root/purchasing/inquiry/supplier_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Supplier Credit Notes" . "</a>";
                label_cell($link);
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
        end_row();
    
    $total_cash_in += $total;
}

function display_iou_payment_returns_summary($tran_type, $from_date, $to_date, &$total_cash_in, $first_row) {
    global $path_to_root;
    
    $total = get_iou_payments_summary($from_date, $to_date, 1);
    $total = abs($total);
    
        if ($first_row === true) {
//            $link = "<a href='$path_to_root/petty_cash/inquiry/i_owe_you_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "I Owe You Payment Returns" . "</a>";
//                label_cell($link);
                label_cell(text_bold("I Owe You Payment Returns"));
        }
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&param_1=1";
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
    
    $total_cash_in += $total;
}

function display_bank_deposits_summary($tran_type, $from_date, $to_date, &$total_cash_in, $first_row, $person_type) {
    global $path_to_root;
    
    $total = get_bank_transactions_summary($tran_type, $from_date, $to_date, $person_type);
    $total = abs($total);
    
    switch ($person_type) {
        case PT_CUSTOMER:
            $display_name = spaces(10)."Customer Deposits";
            break;
        case PT_SUPPLIER:
            $display_name = spaces(10)."Supplier Return Adjustments";
            break;
        case PT_EMPLOYEE:
            $display_name = spaces(10)."Employee Deposits";
            break;
        case PT_MISC:
            $display_name = spaces(10)."Miscellaneous";
            break;
        default:
            $display_name = spaces(10)."Other";
            break;
    }
    
        if ($first_row === true) {
            $link = "<a href='$path_to_root/gl/inquiry/bank_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $display_name . "</a>";
                label_cell($link);
        }
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&ptype=".$person_type;
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
    
    $total_cash_in += $total;
}

function display_journal_receipts_summary($tran_type, $from_date, $to_date, &$total_cash_in, $first_row) {
    global $path_to_root, $systypes_array;
    
    $total = get_journal_transactions_summary($tran_type, $from_date, $to_date);
    $total = abs($total);
    
        if ($first_row === true) {
            $name = "Cash Receipts";
            $link = "<a href='$path_to_root/gl/inquiry/journal_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
                label_cell($link);
        }
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
    
    $total_cash_in += $total;
}

function display_company_loan_repayment_returns_summary($tran_type, $from_date, $to_date, &$total_cash_in, $first_row) {
    global $path_to_root;
    
    $total = get_company_loan_repayments_summary($from_date, $to_date, 1);
    $total = -abs($total);
    
        if ($first_row === true) {
//            $link = "<a href='$path_to_root/finance/inquiry/company_loans_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Company Loan Repayment Returns" . "</a>";
//                label_cell($link);
                label_cell(text_bold("Company Loan Repayment Returns"));
        }
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&param_1=1";
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
    
    $total_cash_in += $total;
}

//function display_bank_account_other_receipts_summary($tran_type, $from_date, $to_date, &$total_cash_in, $first_row) {
//    global $path_to_root;
//    
//    $total = get_bank_account_other_receipts_payments_summary($tran_type, $from_date, $to_date, 1);
//    $total = abs($total);
//    
//        if ($first_row === true) {
//            $link = "<a href='$path_to_root/gl/inquiry/bank_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Bank Account Other Receipts (GL)" . "</a>";
//                label_cell($link);
//        }
//                
//            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&param_1=1";
//            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
//                label_cell($amount_link, "align=right");
//    
//    $total_cash_in += $total;
//}


function display_bank_account_other_receipts_summary($tran_type, $first_month_start, $last_month_end, $months_array, &$total_months_in_array) {
    $gl_trans_types = get_bank_trans_gl_trans_types_by_filters($tran_type, $first_month_start, $last_month_end, -1, 1);
        while ($gl_trans_type = db_fetch($gl_trans_types)) {
            $gl_trans_type_id = $gl_trans_type['gl_trans_type_id'];

            start_row();
                $gl_trns_type = get_list_value($gl_trans_type_id);
                label_cell(spaces(30).$gl_trns_type['value_name']);
                $first_row = true;

                foreach ($months_array as $key => $month) {
                    $month_begin_date = $month['month_begin_date'];
                    $month_end_date = $month['month_end_date'];

                    $total_cash_in = 0;

                    $total = get_bank_account_other_receipts_payments_summary($tran_type, $month_begin_date, $month_end_date, 1, $gl_trans_type_id);
                    $total = abs($total);

                        $parameters = "tran_type=".$tran_type."&from_date=".sql2date($month_begin_date)."&to_date=".sql2date($month_end_date).
                                      "&ptype=-1&gl_trans_type_id=".$gl_trans_type_id."&param_1=1";
                        $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                            label_cell($amount_link, "align=right");

                    $total_cash_in += $total;
                    $total_months_in_array[$key] += $total_cash_in;
                    $first_row = false;
                }
            end_row();
        }
}  


function display_month_cash_in_adjustments_summary($tran_type, $from_date, $to_date, &$total_cash_in, $first_row) {
    global $path_to_root;
    
    $cash = get_listvalue_by_code('MONTH_REP_ADJ_TYPES', 'CASH_IN');
    $total = get_month_reporting_adjustments_summary($cash['id'], $from_date, $to_date);
    $total = $total;
    
        if ($first_row === true) {
//            $link = "<a href='$path_to_root/gl/inquiry/bank_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Month Cash In Adjustments" . "</a>";
//                label_cell($link);
                label_cell(text_bold("Month Cash In Adjustments"));
        }
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&param_1=".$cash['id'];
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
    
    $total_cash_in += $total;
}


function  print_customer_credit_notes_summary($tran_type, $from_date, $to_date, &$total_cash_out) {
    global $path_to_root;
    
    $total = get_customer_credit_notes_summary($tran_type, $from_date, $to_date);
    $total = abs($total);
    
        start_row();
            $name = ("Customer Credit Notes");
            $link = "<a href='$path_to_root/projects/inquiry/project_credit_notes_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
                label_cell($link);
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
        end_row();
    
    $total_cash_out += $total;
}

function  print_supplier_payments_summary($tran_type, $from_date, $to_date, &$total_cash_out, $first_row) {
    global $path_to_root;
    
    $total = get_supplier_payments_summary($tran_type, $from_date, $to_date);
    $total = abs($total);
    
        if ($first_row === true) {
            $name = ("Supplier Payments");
//            $link = "<a href='$path_to_root/purchasing/inquiry/supplier_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
//                label_cell($link);
                label_cell(text_bold($name));
        }
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
    
    $total_cash_out += $total;
}

function  print_bank_payments_summary($tran_type, $from_date, $to_date, &$total_cash_out, $first_row, $person_type) {
    global $path_to_root;
    
    $display_name = "";
    
    switch ($person_type) {
        case PT_CUSTOMER:
            $display_name = spaces(10)."Customer Return Adjustments";
            break;
        case PT_SUPPLIER:
            $display_name = spaces(10)."Supplier Payments";
            break;
        case PT_EMPLOYEE:
            $display_name = spaces(10)."Employee Payments";
            break;
        case PT_MISC:
            $display_name = spaces(10)."Miscellaneous";
            break;
        default:
            $display_name = spaces(10)."Other";
            break;
    }
    
    $total = get_bank_transactions_summary($tran_type, $from_date, $to_date, $person_type);
    $total = abs($total);
    
        if ($first_row === true) {
//            $name = ("Bank Payments");
            $link = "<a href='$path_to_root/gl/inquiry/bank_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $display_name . "</a>";
                label_cell($link);
        }
                
        $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&ptype=".$person_type;
        $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
        label_cell($amount_link, "align=right");
    
    $total_cash_out += $total;
}

function display_journal_payments_summary($tran_type, $from_date, $to_date, &$total_cash_out, $first_row) {
    global $path_to_root, $systypes_array;
    
    $total = get_journal_transactions_summary($tran_type, $from_date, $to_date);
    $total = abs($total);
    
        if ($first_row === true) {
            $name = "Cash Payments";
            $link = "<a href='$path_to_root/gl/inquiry/journal_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
                label_cell($link);
        }
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
    
    $total_cash_out += $total;
}

function  print_salaries_summary($tran_type, $from_date, $to_date, &$total_cash_out) {
    global $path_to_root;
    
    $total = abs(1545986);
    
        start_row();
            $name = ("Salaries");
            $link = "<a href='$path_to_root/gl/inquiry/bank_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
                label_cell($link);
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
        end_row();
    
    $total_cash_out += $total;
}

function display_iou_bank_payments_summary($tran_type, $from_date, $to_date, &$total_cash_out, $first_row) {
    global $path_to_root;
    
//    $total = get_iou_bank_payments_summary($from_date, $to_date, 0);
    $total = get_bank_transactions_summary($tran_type, $from_date, $to_date);
    $total = -($total);
    
        if ($first_row === true) {
            if ($tran_type == ST_IOU_BANKPAYMENT) {
                $name = ("I Owe You Bank Payments");
            }elseif($tran_type == ST_IOU_BANKPAYMENT_RETURN){
                $name = ("I Owe You Bank Payment Returns");
            }
             label_cell(text_bold($name));
        }
//            $link = "<a href='$path_to_root/petty_cash/inquiry/i_owe_you_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
//                label_cell($link);
        
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&param_1=0";
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
    
    $total_cash_out += $total;
}

function display_iou_payments_summary($tran_type, $from_date, $to_date, &$total_cash_out, $first_row) {
    global $path_to_root;
    
    $total = get_iou_payments_summary($from_date, $to_date, 0);
    $total = ($total);
    
        if ($first_row === true) {
            $name = ("I Owe You Payments");
//            $link = "<a href='$path_to_root/petty_cash/inquiry/i_owe_you_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
//                label_cell($link);
                label_cell(text_bold($name));
        }
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&param_1=0";
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
    
    $total_cash_out += $total;
}

function display_iou_advances_summary($tran_type, $from_date, $to_date, &$total_cash_out, $first_row) {
    global $path_to_root;
    
    $total = get_iou_advances_summary($from_date, $to_date);
    $total = abs($total);

        if ($first_row === true) {
            $name = ("I Owe You Advances");
//            $link = "<a href='$path_to_root/petty_cash/inquiry/i_owe_you_advances_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
//                label_cell($link);
                label_cell(text_bold($name));
        }

            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");

    $total_cash_out += $total;
}

function display_company_loan_repayments_summary($tran_type, $from_date, $to_date, &$total_cash_out, $first_row) {
    global $path_to_root;
    
    $total = get_company_loan_repayments_summary($from_date, $to_date, 0);
    $total = abs($total);
    
        if ($first_row === true) {
            $name = ("Company Loan Repayments");
//            $link = "<a href='$path_to_root/finance/inquiry/company_loans_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
//                label_cell($link);
                label_cell(text_bold($name));
        }
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&param_1=0";
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
    
    $total_cash_out += $total;
}

//function display_bank_account_other_payments_summary($tran_type, $from_date, $to_date, &$total_cash_out, $first_row) {
//    global $path_to_root;
//    
//    $total = get_bank_account_other_receipts_payments_summary($tran_type, $from_date, $to_date, 0);
//    $total = abs($total);
//    
//        if ($first_row === true) {
//            $link = "<a href='$path_to_root/gl/inquiry/bank_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Bank Account Other Payments" . "</a>";
//                label_cell($link);
//        }
//                
//            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&param_1=0";
//            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
//                label_cell($amount_link, "align=right");
//    
//    $total_cash_out += $total;
//}

function display_bank_account_other_payments_summary($tran_type, $first_month_start, $last_month_end, $months_array, &$total_months_out_array) {
    
    $gl_trans_types = get_bank_trans_gl_trans_types_by_filters($tran_type, $first_month_start, $last_month_end, -1, 0);
        while ($gl_trans_type = db_fetch($gl_trans_types)) {
            $gl_trans_type_id = $gl_trans_type['gl_trans_type_id'];

            start_row();
                $gl_trns_type = get_list_value($gl_trans_type_id);
                label_cell(spaces(30).$gl_trns_type['value_name']);
                $first_row = true;

                foreach ($months_array as $key => $month) {
                    $month_begin_date = $month['month_begin_date'];
                    $month_end_date = $month['month_end_date'];

                    $total_cash_out = 0;

                    $total = get_bank_account_other_receipts_payments_summary($tran_type, $month_begin_date, $month_end_date, 0, $gl_trans_type_id);
                    $total = abs($total);

                        $parameters = "tran_type=".$tran_type."&from_date=".sql2date($month_begin_date)."&to_date=".sql2date($month_end_date).
                                      "&ptype=-1&gl_trans_type_id=".$gl_trans_type_id."&param_1=0";
                        $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                            label_cell($amount_link, "align=right");

                    $total_cash_out += $total;
                    $total_months_out_array[$key] += $total_cash_out;
                    $first_row = false;
                }
            end_row();
        }
}

function display_month_cash_out_adjustments_summary($tran_type, $from_date, $to_date, &$total_cash_in, $first_row) {
    global $path_to_root;
    
    $cash = get_listvalue_by_code('MONTH_REP_ADJ_TYPES', 'CASH_OUT');
    $total = get_month_reporting_adjustments_summary($cash['id'], $from_date, $to_date);
    $total = ($total);
    
        if ($first_row === true) {
//            $link = "<a href='$path_to_root/gl/inquiry/bank_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Month Cash Out Adjustments" . "</a>";
//                label_cell($link);
                label_cell(text_bold("Month Cash Out Adjustments"));
        }
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&param_1=".$cash['id'];
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
    
    $total_cash_in += $total;
}

function print_customer_payments_cash_in_total($month_total, $first_row) {
    if ($first_row === true) {
        label_cell(text_bold("Cash In Total:"), "align=right");
    }
        amount_cell($month_total, true);
}

function print_customer_payments_cash_out_total($total_cash_out, $first_row) {
    if ($first_row === true) {
        label_cell(text_bold("Cash Out Total:"), "align=right");
    }
        amount_cell($total_cash_out, true);
}

function display_company_cash_folw_bank_accounts_summary($year_id, $month_id, $internal) {
    
    global $path_to_root;
    
    $from_date = "$year_id-$month_id-01";
    $to_date = date("Y-m-t", strtotime($from_date));
    
    if($internal){
        $heading = "Bank Accounts Summary (Internal)";
    }
    else{        
        $heading = "Bank Accounts Summary (Banks)";
    }
        display_heading($heading);      
        
        br();

            start_table(TABLESTYLE, "width=98%");

                $th = array(_("Bank Account"), _("Opening <br> Balance"), _("Customer <br> Payment"), _("Supplier <br> Payment"), _("Bank <br> Deposit"), 
                            _("Bank <br> Payment"), _("Company <br> Loan Repayment"), _("Petty Cash <br> Float Reimbursment"), _("Bank Transfer <br> Balance"), _("Journal"), 
                            _("Other <br>(GL Credit/Debit)"), _("Month Net"), _("Closing Balance"));
                    table_header($th);
                    
                    $last_from_date = INFINITE_START_DATE;
                    $previous_month_last_date = date('Y-m-t', strtotime("-1 months", strtotime($from_date)));

                    $totals = array();
                    $totals['opening_balance'] = 0;
                    $totals['customer_payment'] = 0;
                    $totals['supplier_payment'] = 0;
                    $totals['bank_deposit'] = 0;
                    $totals['bank_payment'] = 0;
                    $totals['company_loan_payment'] = 0;
                    $totals['petty_cash_foat_reimbursment'] = 0;
                    $totals['banktransfer'] = 0;
                    $totals['journal'] = 0;
                    $totals['gl_debit'] = 0; 
                    $totals['other'] = 0; 
                    $totals['total_of_all_types'] = 0;
                    $totals['closing_balance'] = 0;
                    
                        $bank_accounts = get_bank_accounts_and_opening_balance($last_from_date, $previous_month_last_date, $internal);
                            while ($bank = db_fetch($bank_accounts)) {
                                start_row();
                                $link = "<a href='$path_to_root/gl/inquiry/bank_inquiry.php?bank_account=".$bank['id']."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $bank['bank_account_name'] . "</a>";
                                    label_cell($link);

                                    label_cell(number_format($bank['opening_balance'], 2), 'align=right');
                                    $totals['opening_balance'] += $bank['opening_balance'];

                                        $tr_tp_row = get_company_cash_folw_bank_accounts_summary($bank['id'], $from_date, $to_date);
                                            if (is_array($tr_tp_row)) {
                                                    label_cell(number_format($tr_tp_row['customer_payment'], 2), 'align=right');
                                                    $totals['customer_payment'] += $tr_tp_row['customer_payment'];
                                                    
                                                    label_cell(number_format($tr_tp_row['supplier_payment'], 2), 'align=right');
                                                    $totals['supplier_payment'] += $tr_tp_row['supplier_payment'];
                                                    
                                                    label_cell(number_format($tr_tp_row['bank_deposit'], 2), 'align=right');
                                                    $totals['bank_deposit'] += $tr_tp_row['bank_deposit'];
                                                    
                                                    label_cell(number_format($tr_tp_row['bank_payment'], 2), 'align=right');
                                                    $totals['bank_payment'] += $tr_tp_row['bank_payment'];
                                                    
                                                    label_cell(number_format($tr_tp_row['company_loan_payment'], 2), 'align=right');
                                                    $totals['company_loan_payment'] += $tr_tp_row['company_loan_payment'];
                                                    
                                                    label_cell(number_format($tr_tp_row['petty_cash_foat_reimbursment'], 2), 'align=right');
                                                    $totals['petty_cash_foat_reimbursment'] += $tr_tp_row['petty_cash_foat_reimbursment'];
                                                    
                                                    label_cell(number_format($tr_tp_row['banktransfer'], 2), 'align=right');
                                                    $totals['banktransfer'] += $tr_tp_row['banktransfer'];
                                                    
                                                    label_cell(number_format($tr_tp_row['journal'], 2), 'align=right');
                                                    $totals['journal'] += $tr_tp_row['journal'];
                                                    
                                                    label_cell(number_format(($tr_tp_row['gl_debit']+$tr_tp_row['gl_credit']), 2), 'align=right');
                                                    $totals['other'] += $tr_tp_row['gl_debit'] + $tr_tp_row['gl_credit'];
                                                    
                                                    label_cell(number_format($tr_tp_row['total_of_all_types'], 2), 'align=right');
                                                    $totals['total_of_all_types'] += $tr_tp_row['total_of_all_types'];

                                                    label_cell(number_format(($bank['opening_balance']+$tr_tp_row['total_of_all_types']), 2), 'align=right');
                                                    $totals['closing_balance'] += $bank['opening_balance'] + $tr_tp_row['total_of_all_types'];
                                                    
                                            } else {
                                                for ($i=0; $i<=10; $i++) {
                                                    label_cell('');
                                                }
                                            }
                                        
                                end_row();
                            }
                            
                            start_row();
                                    label_cell(text_bold("Total"), 'align=right');
                                    label_cell(text_bold(number_format($totals['opening_balance'], 2)), 'align=right');
                                    label_cell(text_bold(number_format($totals['customer_payment'], 2)), 'align=right');
                                    label_cell(text_bold(number_format($totals['supplier_payment'], 2)), 'align=right');
                                    label_cell(text_bold(number_format($totals['bank_deposit'], 2)), 'align=right');
                                    label_cell(text_bold(number_format($totals['bank_payment'], 2)), 'align=right');
                                    label_cell(text_bold(number_format($totals['company_loan_payment'], 2)), 'align=right');
                                    label_cell(text_bold(number_format($totals['petty_cash_foat_reimbursment'], 2)), 'align=right');
                                    label_cell(text_bold(number_format($totals['banktransfer'], 2)), 'align=right');
                                    label_cell(text_bold(number_format($totals['journal'], 2)), 'align=right');
                                    label_cell(text_bold(number_format(($totals['other']), 2)), 'align=right');
                                    label_cell(text_bold(number_format(($totals['total_of_all_types']), 2)), 'align=right');
                                    label_cell(text_bold(number_format($totals['closing_balance'], 2)), 'align=right');

                            end_row();
                    
            end_table();
}

function display_new_loans_summary($tran_type, $from_date, $to_date, &$new_loans_total, $first_row, $sub_type) {
    global $path_to_root;
    
    $total = get_loans_amount_by_from_date_to_date($from_date, $to_date);
    $total = abs($total);
    
        if ($first_row === true) {
//            $link = "<a href='$path_to_root/sales/inquiry/customer_transactions_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Customer Payments" . "</a>";
//                label_cell($link);
        }
                
        $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&ptype=$sub_type";
        $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
        label_cell($amount_link, "align=right");
    
        $new_loans_total += $total;
}

function display_closed_loans_summary($tran_type, $from_date, $to_date, &$new_loans_total, $first_row, $sub_type) {
    global $path_to_root;
    
    $total = get_closed_loans_summary($from_date, $to_date);
    
        if ($first_row === true) {
//            $link = "<a href='$path_to_root/sales/inquiry/customer_transactions_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Customer Payments" . "</a>";
//                label_cell($link);
        }
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&ptype=$sub_type";
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
    
    $new_loans_total += $total;
}

function display_loans_capital_repayments_summary($tran_type, $from_date, $to_date, &$new_loans_total, $first_row, $sub_type, &$payments_array) {
    global $path_to_root;
    
    $loan_payments = get_company_loan_payments_by_from_date_to_date($from_date, $to_date);
    $total = $loan_payments['total_payment'];
    
        if ($first_row === true) {
//            $link = "<a href='$path_to_root/sales/inquiry/customer_transactions_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Customer Payments" . "</a>";
//                label_cell($link);
        }
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&ptype=$sub_type";
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
                
            $payments_array[$to_date] = $total;
    
    $new_loans_total += $total;
}

function display_loans_interest_payments_summary($tran_type, $from_date, $to_date, &$new_loans_total, $first_row, $sub_type, &$payments_array) {
    global $path_to_root;
    
    $loan_payments = get_company_loan_payments_by_from_date_to_date($from_date, $to_date);
    $total = $loan_payments['total_interest'];
    
        if ($first_row === true) {
//            $link = "<a href='$path_to_root/sales/inquiry/customer_transactions_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Customer Payments" . "</a>";
//                label_cell($link);
        }
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&ptype=$sub_type";
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
                
            $payments_array[$to_date] = $total;
    
    $new_loans_total += $total;
}

function display_loans_late_interest_payments_summary($tran_type, $from_date, $to_date, &$new_loans_total, $first_row, $sub_type, &$payments_array) {
    global $path_to_root;
    
    $loan_payments = get_company_loan_payments_by_from_date_to_date($from_date, $to_date);
    $total = $loan_payments['total_late_interest'];
    
        if ($first_row === true) {
//            $link = "<a href='$path_to_root/sales/inquiry/customer_transactions_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Customer Payments" . "</a>";
//                label_cell($link);
        }
                
            $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&ptype=$sub_type";
            $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($total, 2));
                label_cell($amount_link, "align=right");
                
            $payments_array[$to_date] = $total;
    
    $new_loans_total += $total;
}

function display_loans_capital_balances_summary($tran_type, $from_date, $to_date, &$all_period_loan_balances, $first_row, $sub_type) {
    global $path_to_root;
    
//    $all_loans_capital_balance = $all_loans_estimated_balance = 0;
    $loan_balances = array();
    $loan_balances["capital_balance"] = 0;
    $loan_balances["interest_balance"] = 0;
    $loan_balances["estimated_total_balance"] = 0;
    $loan_balances["saving_on_interest"] = 0;
    
    $loans_res = get_period_active_loans($from_date, $to_date);
    
    while ($loan = db_fetch($loans_res)) {
        $capital_settlement = get_total_loan_capital_settlement_to_date($loan["id"], $to_date);
        
        $loan_capital_balance = $loan['loan_amount'] - $capital_settlement;
        $loan_balances["capital_balance"] += $loan_capital_balance;
        
        $average_interest = $loan['average_interest'];
        
//        $interest_factor = $interest_amount / $loan['loan_amount'];
        $loan_estimated_interest_balance = $loan_capital_balance * ($average_interest/100);
        $loan_balances["interest_balance"] += $loan_estimated_interest_balance;
        
        $loan_estimated_total_balance = $loan_capital_balance + $loan_estimated_interest_balance;
        $loan_balances["estimated_total_balance"] += $loan_estimated_total_balance;
        
        $interest_paid = get_total_interest_settlement_to_date($loan["id"], $to_date);
        $due_interest_for_capital_settlement = $capital_settlement * $average_interest / 100;
        $saving_on_interest = $due_interest_for_capital_settlement - $interest_paid;
        
        $loan_balances["saving_on_interest"] += $saving_on_interest;
        
    }
        if ($first_row === true) {
//            $link = "<a href='$path_to_root/sales/inquiry/customer_transactions_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Customer Payments" . "</a>";
//                label_cell($link);
        }
                
        $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&ptype=$sub_type";
        $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($loan_balances["capital_balance"], 2));
        label_cell($amount_link, "align=right");
    
        $all_period_loan_balances[$to_date] = $loan_balances;
}

function display_loans_balances_summary($tran_type, $from_date, $to_date, &$all_period_loan_balances, $first_row, $sub_type) {
    global $path_to_root;
    
//    $all_loans_balance = 0;
    
//    $loans_res = get_period_active_loans($from_date, $to_date);
    
//    while ($loan = db_fetch($loans_res)) {
//        $loan_settlement = get_total_loan_settlement_to_date($loan["id"], $to_date);
//        $loan_balance = $loan['loan_amount'] + $loan['interest'] - $loan_settlement;
//        $all_loans_balance += $loan_balance;
//    }
        if ($first_row === true) {
//            $link = "<a href='$path_to_root/sales/inquiry/customer_transactions_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Customer Payments" . "</a>";
//                label_cell($link);
        }
                
        $this_period_loan_balances = $all_period_loan_balances[$to_date];
        
        $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&ptype=$sub_type";
        $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($this_period_loan_balances["estimated_total_balance"], 2));
        label_cell($amount_link, "align=right");
    
//    $new_loans_total = $all_loans_balance;
}

function display_saving_loss_on_interest($tran_type, $from_date, $to_date, &$all_period_loan_balances, $first_row, $sub_type) {
    global $path_to_root;
    
//    $all_loans_balance = 0;
    
//    $loans_res = get_period_active_loans($from_date, $to_date);
    
//    while ($loan = db_fetch($loans_res)) {
//        $loan_settlement = get_total_loan_settlement_to_date($loan["id"], $to_date);
//        $loan_balance = $loan['loan_amount'] + $loan['interest'] - $loan_settlement;
//        $all_loans_balance += $loan_balance;
//    }
        if ($first_row === true) {
//            $link = "<a href='$path_to_root/sales/inquiry/customer_transactions_inquiry.php?from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Customer Payments" . "</a>";
//                label_cell($link);
        }
                
        $this_period_loan_balances = $all_period_loan_balances[$to_date];       
        
        
        $parameters = "tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&ptype=$sub_type";
        $amount_link = get_data_view_str(DV_COMPANY_CASH_FLOW_TRANSACTIONS, $parameters, number_format($this_period_loan_balances["saving_on_interest"], 2));
        label_cell($amount_link, "align=right");
    
//    $new_loans_total = $all_loans_balance;
}

function display_loans_summary($months_array, $heder_length) {
    start_row();
        vertical_space("height=50px colspan=$heder_length");
    end_row();
    
    $heading = "Loans Summary";
//        start_row();
//            label_cell(text_bold($heading), "style=color:blue; align=center colspan=$heder_length");
//        end_row();        
        
        start_row();
            label_cell(text_bold($heading), "colspan=$heder_length style=background-color:#0ec9ba align=center"); 
        end_row();

        $new_loans_total = 0;
        
            start_row();
                label_cell(text_bold("New Loans:"), 'align=right');
                $first_row = true;
                foreach ($months_array as $month_begin => $value) {
                    $month_begin_date = $value['month_begin_date'];
                    $month_end_date = $value['month_end_date'];
//                    $new_loans_total = get_loans_amount_by_from_date_to_date($month_begin_date, $month_end_date);
//                        label_cell(text_bold(number_format($new_loans_total, 2)), "align=right");
                        
                    display_new_loans_summary(ST_COMPANY_LOANS, $month_begin_date, $month_end_date, $new_loans_total, $first_row, 'NL');
                    $first_row = false;
                }
            end_row();
            
            start_row();
                label_cell(text_bold("Completed Loans:"), 'align=right');
                $first_row = true;
                foreach ($months_array as $month_begin => $value) {
                    $month_begin_date = $value['month_begin_date'];
                    $month_end_date = $value['month_end_date'];
//                    $new_loans_total = get_loans_amount_by_from_date_to_date($month_begin_date, $month_end_date);
//                        label_cell(text_bold(number_format($new_loans_total, 2)), "align=right");
                        
                    display_closed_loans_summary(ST_COMPANY_LOANS, $month_begin_date, $month_end_date, $new_loans_total, $first_row, 'CL');
                    $first_row = false;
                }
            end_row();

            $capital_payments_array = array();
            start_row();
                label_cell(text_bold("Capital Repayments:"), 'align=right');
                $first_row = true;
                foreach ($months_array as $month_begin => $value) {
                    $month_begin_date = $value['month_begin_date'];
                    $month_end_date = $value['month_end_date'];
                    
                    display_loans_capital_repayments_summary(ST_COMPANY_LOANS, $month_begin_date, $month_end_date, $new_loans_total, $first_row, 'CR', $capital_payments_array);
                    $first_row = false;
//                    $loan_payments = get_company_loan_payments_by_from_date_to_date($month_begin_date, $month_end_date);
//                        label_cell(text_bold(number_format($loan_payments['total_payment'], 2)), "align=right");
//
//                        $capital_payments_array[$month_end_date] = $loan_payments['total_payment'];
                }
            end_row();

            $interest_payments_array = array();
            start_row();
                label_cell(text_bold("Interest Payments:"), 'align=right');
                $first_row = true;
                foreach ($months_array as $month_begin => $value) {
                    $month_begin_date = $value['month_begin_date'];
                    $month_end_date = $value['month_end_date'];
                    display_loans_interest_payments_summary(ST_COMPANY_LOANS, $month_begin_date, $month_end_date, $new_loans_total, $first_row, 'IP', $interest_payments_array);
                    $first_row = false;
//                    $interest_payments = get_company_loan_payments_by_from_date_to_date($month_begin_date, $month_end_date);
//                        label_cell(text_bold(number_format($interest_payments['total_interest'], 2)), "align=right");
//
//                        $interest_payments_array[$month_end_date] = $interest_payments['total_interest'];
                }
            end_row();

            $late_interest_payments_array = array();
            start_row();
                label_cell(text_bold("Late Interest Payments:"), 'align=right');
                $first_row = true;
                foreach ($months_array as $month_begin => $value) {
                    $month_begin_date = $value['month_begin_date'];
                    $month_end_date = $value['month_end_date'];
                    display_loans_late_interest_payments_summary(ST_COMPANY_LOANS, $month_begin_date, $month_end_date, $new_loans_total, $first_row, 'LI', $late_interest_payments_array);
                    $first_row = false;
//                    $late_interest_payments = get_company_loan_payments_by_from_date_to_date($month_begin_date, $month_end_date);
//                        label_cell(text_bold(number_format($late_interest_payments['total_late_interest'], 2)), "align=right");
                }
            end_row();

            start_row();
                label_cell(text_bold("Total Repayments")."\n\r(With Late Interest):", 'align=right');

                foreach ($months_array as $month_begin => $value) {
                    $month_begin_date = $value['month_begin_date'];
                    $month_end_date = $value['month_end_date'];
                    $total_repayments = get_company_loan_payments_by_from_date_to_date($month_begin_date, $month_end_date);
                    label_cell(text_bold(number_format($total_repayments['total_repayment'], 2)), "align=right");
                }
            end_row();

            $all_period_loan_balances = array();
            start_row();
                label_cell(nl2br(text_bold("Capital Balance:")."\n\r(Loan capital - Capital repayments)"), 'align=right');

                foreach ($months_array as $month_begin => $value) {
                    $month_begin_date = $value['month_begin_date'];
                    $month_end_date = $value['month_end_date'];
//                    $total_loans_amount = get_loans_amount_by_from_date_to_date(null, $month_end_date, 0);
                    display_loans_capital_balances_summary(ST_COMPANY_LOANS, $month_begin_date, $month_end_date, $all_period_loan_balances, $first_row, 'CB');
                    $first_row = false;

//                    $capital_payments_total = $capital_payments_array[$month_end_date];
//                    $interest_payments_total = $interest_payments_array[$month_end_date];
//
//                        $loan_balance = $total_loans_amount - ($capital_payments_total + $interest_payments_total);
//                            label_cell(text_bold(number_format($loan_balance, 2)), "align=right");
                }
            end_row();

            start_row();
                label_cell(nl2br(text_bold("Capital & Interest Balance:")."\n\r(Loan capital + interest) - Capital and interest repayments"), 'align=right');

                foreach ($months_array as $month_begin => $value) {
                    $month_end_date = $value['month_end_date'];
//                    $total_loans_amount = get_loans_amount_by_from_date_to_date(null, $month_end_date, 0);
                    display_loans_balances_summary(ST_COMPANY_LOANS, $month_begin_date, $month_end_date, $all_period_loan_balances, $first_row, 'LB');
                    $first_row = false;

//                    $capital_payments_total = $capital_payments_array[$month_end_date];
//                    $interest_payments_total = $interest_payments_array[$month_end_date];
//
//                        $loan_balance = $total_loans_amount - ($capital_payments_total + $interest_payments_total);
//                            label_cell(text_bold(number_format($loan_balance, 2)), "align=right");
                }
            end_row();

            start_row();
                label_cell(nl2br(text_bold("Saving / Additional Payments On Interest:")."\n\r(Based on average interest)"), 'align=right');

                foreach ($months_array as $month_begin => $value) {
                    $month_end_date = $value['month_end_date'];
                    display_saving_loss_on_interest(ST_COMPANY_LOANS, $month_begin_date, $month_end_date, $all_period_loan_balances, $first_row, 'ADI');
                    $first_row = false;

                }
            end_row();
            

}



function display_bank_accounts($months_array, $heder_length, $internal) {
    if ($internal) {
        $heading = "Bank Accounts (Internal)";
    } else {        
        $heading = "Bank Accounts (Banks)";
    }
    
        $opening_balance_array = $month_net_array = $closing_balance_array = $data_array = array();
        
        $month_begin_date = INFINITE_START_DATE;
        $months_array_keys = (array_keys($months_array));
        $month_begin_date_start = strtotime($months_array_keys[0]);
        $previous_month_last_date = date('Y-m-t', strtotime("-1 months", $month_begin_date_start));
        $opening_balance = get_bank_accounts_opening_balance($month_begin_date, $previous_month_last_date, $internal);
                    
            $first_row = true;
                foreach ($months_array as $month_begin => $value) {
//                    $opening_balance = $month_net = $closing_balance = 0;

//                        $month_begin_date = ($first_row === true) ? INFINITE_START_DATE : $value['month_begin_date'];
//                        $previous_month_last_date = date('Y-m-t', strtotime("-1 months", strtotime($value['month_begin_date'])));
                        $month_begin_date = $value['month_begin_date'];
                        $month_end_date = $value['month_end_date'];

//                            $bank_accounts = get_bank_accounts_and_opening_balance($month_begin_date, $previous_month_last_date, $internal);
                            $month_net = get_company_cash_folw_bank_accounts_period_net($month_begin_date, $month_end_date, $internal);
//                            while ($bank = db_fetch($bank_accounts)) {
//                                $opening_balance += $bank['opening_balance'];
//
//                                $tr_tp_row = get_company_cash_folw_bank_accounts_summary($bank['id'], $value['month_begin_date'], $value['month_end_date']);
//                                if (is_array($tr_tp_row)) {
//                                    $month_net += $tr_tp_row['total_of_all_types'];
//                                    $closing_balance += $bank['opening_balance'] + $tr_tp_row['total_of_all_types'];
//                                }
//                            }

                            $opening_balance_array[$value['month_begin_date']] = $opening_balance;
                            $month_net_array[$value['month_begin_date']] = $month_net;
                            $closing_balance = $opening_balance + $month_net;
                            $closing_balance_array[$value['month_begin_date']] = $closing_balance;
                            
                            $opening_balance = $closing_balance;

                            $first_row = false;
                }
                    
                    $data_array['Opening Balance'] = $opening_balance_array;
                    $data_array['Month Net'] = $month_net_array;
                    $data_array['Closing Balance'] = $closing_balance_array;
                    

            start_row();
                label_cell(text_bold($heading), "align=left colspan=$heder_length");
            end_row();

                foreach ($data_array as $type_name => $data) {
                    start_row();
                        label_cell(text_bold("$type_name:"), 'align=right');

                        foreach ($data as $month_begin => $amount) {
                            label_cell(text_bold(number_format($amount, 2)), "align=right");
                        }
                    end_row();
                }
}
