<?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 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) {
    $from_date = "$year_id-$month_id-01";
    
    $total_months_in_array = $total_months_out_array = array();
    
        display_heading2("Cash Flow");      br();

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

                $th = array(_("Type"));
                    foreach ($months_array as $month_begin => $value) {
                        $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");
                            end_row();

                                display_company_cash_flow_ins($months_array, $total_months_in_array);

                            start_row();
                                label_cell(text_bold("Cash Out"), "colspan=$heder_length");
                            end_row();

                                display_company_cash_flow_outs($months_array, $total_months_out_array);

                            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();
            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) {
    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();
        $first_row = true;

        foreach ($months_array as $key => $month) {
            $total_cash_in = 0;

            display_bank_deposits_summary(ST_BANKDEPOSIT, $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_journal_receipts_summary(ST_CASH_RECEIPT, $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_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();
        $first_row = true;

        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);  

                $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) {
    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();
        $first_row = true;

        foreach ($months_array as $key => $month) {
            $total_cash_out = 0;

            print_bank_payments_summary(ST_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_journal_payments_summary(ST_CASH_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_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();
        $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_out = 0;

            display_bank_account_other_payments_summary(ST_JOURNAL, $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_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_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);
        }
                
            $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);
        }
                
            $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) {
    global $path_to_root;
    
    $total = get_bank_transactions_summary($tran_type, $from_date, $to_date);
    $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 Deposits" . "</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_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 = $systypes_array[$tran_type];
            $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);
        }
                
            $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" . "</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_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('REPORTING_ADJUSTMENT_TYPES', 'CASH_IN');
    $total = get_month_reporting_adjustments_summary($cash['id'], $from_date, $to_date);
    $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>" . "Month Cash In Adjustments" . "</a>";
                label_cell($link);
        }
                
            $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);
        }
                
            $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) {
    global $path_to_root;
    
    $total = get_bank_transactions_summary($tran_type, $from_date, $to_date);
    $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>" . $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 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 = $systypes_array[$tran_type];
            $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_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 = abs($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);
        }
                
            $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);
        }

            $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);
        }
                
            $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_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('REPORTING_ADJUSTMENT_TYPES', 'CASH_OUT');
    $total = get_month_reporting_adjustments_summary($cash['id'], $from_date, $to_date);
    $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>" . "Month Cash Out Adjustments" . "</a>";
                label_cell($link);
        }
                
            $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_heading2($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();
}

?>