<?php

function display_subheading($subheading) {
    display_heading($subheading);     br(1);

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

            $th = array(_("Date"), _("No"), _("Reference"), _("Bank Account"), _("Customer / Supplier/ Employee"), _("Amount"));
                table_header($th);
}

function display_subtotals($total_amount, $subheading) {
        label_cell("Total for ".$subheading." :","align=right colspan=5");
        label_cell(text_bold(number_format($total_amount,2)),"align=right");

    end_table();
}

function print_customer_payments($type, $from_date, $to_date) {
    $total_amount = $total_settlement = 0;

        $subheading = "Customer Payments";

        $cus_pay_result = get_customer_payments($type, $from_date, $to_date);
            if (! (db_num_rows($cus_pay_result) > 0)) {
                $msg = "No any Customer Payments found";
                    display_note($msg);    br(3);
                    return;
            }

            display_subheading($subheading);

                while ($cus_pay = db_fetch($cus_pay_result)) {
                    start_row();
                        label_cell(sql2date($cus_pay['tran_date']),"align=center");
                        $link = get_trans_view_str($type, $cus_pay['trans_no']);
                            label_cell($link, "align=center");
                        label_cell($cus_pay['reference']);
                        label_cell($cus_pay['bank_account_name']);

                        $emp_name = '';
                        if (strlen($cus_pay['debtor_no']) > 0) {
                            $emp_name = get_customer_name($cus_pay['debtor_no']);
                        }
                        label_cell($emp_name);
                        
                        $amount = abs($cus_pay["total_cost"]);
                            label_cell(number_format($amount,2), "align=right");
                    end_row();

                    $total_amount += $amount;
                    $total_settlement += $amount;
                }

                display_subtotals($total_amount, $subheading);
}

function print_supplier_credit_notes($type, $from_date, $to_date) {
    $total_amount = $total_settlement = 0;

        $subheading = "Supplier Credit Notes";

        $sup_crd_result = get_suplier_credit_notes($type, $from_date, $to_date);
            if (! (db_num_rows($sup_crd_result) > 0)) {
                $msg = "No any Supplier Credit Notes found";
                    display_note($msg);    br(3);
                    return;
            }

            display_subheading($subheading);

                while ($sup_crd = db_fetch($sup_crd_result)) {
                    start_row();
                        label_cell(sql2date($sup_crd['tran_date']),"align=center");
                        $link = get_trans_view_str($type, $sup_crd['trans_no']);
                            label_cell($link, "align=center");
                        label_cell($sup_crd['reference']);

                        $emp_name = '';
                        if (strlen($sup_crd['supplier_id']) > 0) {
                            $emp_name = get_supplier_name($sup_crd['supplier_id']);
                        }
                        label_cell($emp_name);
                        
                        $amount = abs($sup_crd["total_cost"]);
                            label_cell(number_format($amount,2), "align=right");
                    end_row();

                    $total_amount += $amount;
                    $total_settlement += $amount;
                }

                display_subtotals($total_amount, $subheading);
}

function print_iou_payment_returns($type, $from_date, $to_date, $param_1) {
    $total_amount = $total_settlement = 0;

        if ($param_1 == 1) {
            $subheading = "I Owe You Payment Returns";
        } elseif ($param_1 == 0) {
            $subheading = "I Owe You Payments";
        }

        $iou_pay_result = get_iou_payments($from_date, $to_date, $param_1);
            if (! (db_num_rows($iou_pay_result) > 0)) {
                $msg = "No any $subheading found";
                    display_note($msg);    br(3);
                    return;
            }

            display_subheading($subheading);

                while ($iou_pay = db_fetch($iou_pay_result)) {
                    start_row();
                        label_cell(sql2date($iou_pay['tran_date']),"align=center");
                        $link = get_trans_view_str($type, $iou_pay['trans_no']);
                            label_cell($link, "align=center");
                        label_cell($iou_pay['reference']);
                        label_cell($iou_pay['float_name']);

                        $emp_name = '';
                        if (strlen($iou_pay['employee_code']) > 0) {
                            $emp_name = get_group_company_employee_formatted_long_name($iou_pay['company_code'], $iou_pay['employee_code']);
//                            $ename = get_employee($iou_pay['employee_code']);
//                            $emp_name = $ename['employee_code'].' - '.$ename['employee_code'].' '.$ename['last_name'];
                        }
                        label_cell($emp_name);
                        
                        $amount = abs($iou_pay["total_cost"]);
                            label_cell(number_format($amount,2), "align=right");
                    end_row();

                    $total_amount += $amount;
                    $total_settlement += $amount;
                }

                display_subtotals($total_amount, $subheading);
}

function print_bank_transactions($type, $from_date, $to_date) {
    global $systypes_array;
    
    $total_amount = $total_settlement = 0;

        $subheading = $systypes_array[$type];

        $bnk_trn_result = get_bank_transactions($type, $from_date, $to_date);
            if (! (db_num_rows($bnk_trn_result) > 0)) {
                $msg = "No any $subheading found";
                    display_note($msg);    br(3);
                    return;
            }

            display_subheading($subheading);

                while ($bnk_trn = db_fetch($bnk_trn_result)) {
                    start_row();
                        label_cell(sql2date($bnk_trn['trans_date']),"align=center");
                        $link = get_trans_view_str($type, $bnk_trn['trans_no']);
                            label_cell($link, "align=center");
                        label_cell($bnk_trn['reference']);
                        label_cell($bnk_trn['bank_account_name']);

                        $person_name = '';
                        if (strlen($bnk_trn['person_id']) > 0) {
                            $person_name = payment_person_name($bnk_trn['person_type_id'], $bnk_trn['person_id']);
                        }
                            label_cell($person_name);
                        
                        $amount = abs($bnk_trn["total_cost"]);
                            label_cell(number_format($amount,2), "align=right");
                    end_row();

                    $total_amount += $amount;
                    $total_settlement += $amount;
                }

                display_subtotals($total_amount, $subheading);
}

function print_journal_transactions($type, $from_date, $to_date) {
    global $systypes_array;
    
    $total_amount = $total_settlement = 0;

        $subheading = $systypes_array[$type];

        $jou_result = get_journal_transactions($type, $from_date, $to_date);
            if (! (db_num_rows($jou_result) > 0)) {
                $msg = "No any $subheading found";
                    display_note($msg);    br(3);
                    return;
            }

            display_subheading($subheading);

                while ($jou = db_fetch($jou_result)) {
                    start_row();
                        label_cell(sql2date($jou['tran_date']),"align=center");
                        $link = get_trans_view_str($type, $jou['trans_no']);
                            label_cell($link, "align=center");
                        label_cell($jou['reference']);
                        label_cell($jou['float_name']);

                        $person_name = '';
                        if (strlen($jou['person_id']) > 0) {
                            $person_name = payment_person_name($jou['person_type_id'], $jou['person_id']);
                        }
                            label_cell($person_name);
                        
                        $amount = abs($jou["total_cost"]);
                            label_cell(number_format($amount,2), "align=right");
                    end_row();

                    $total_amount += $amount;
                    $total_settlement += $amount;
                }

                display_subtotals($total_amount, $subheading);
}

function print_company_loan_repayment_returns($type, $from_date, $to_date, $param_1) {
    $total_amount = $total_settlement = 0;

        if ($param_1 == 1) {
            $subheading = "Company Loan Repayment Returns";
        } elseif ($param_1 == 0) {
            $subheading = "Company Loan Repayments";
        }

        $com_lon_result = get_company_loan_repayments($from_date, $to_date, $param_1);
            if (! (db_num_rows($com_lon_result) > 0)) {
                $msg = "No any $subheading found";
                    display_note($msg);    br(3);
                    return;
            }

            display_subheading($subheading);

                while ($com_lon = db_fetch($com_lon_result)) {
                    start_row();
                        label_cell(sql2date($com_lon['tran_date']),"align=center");
                        $link = get_trans_view_str($type, $com_lon['id']);
                            label_cell($link, "align=center");
                        label_cell($com_lon['reference']);
                        label_cell($com_lon['bank_account_name']);

                        $loan_name = $com_lon['description'];
                        $loan_full_amout = number_format($com_lon['loan_amount'] + $com_lon['interest']);
                        $loan_name .= " (".$loan_full_amout.")";
                        label_cell($loan_name);
                        
                        $amount = abs($com_lon["total_cost"]);
                            label_cell(number_format($amount,2), "align=right");
                    end_row();

                    $total_amount += $amount;
                    $total_settlement += $amount;
                }

                display_subtotals($total_amount, $subheading);
}

function print_bank_account_other_receipts_payments($type, $from_date, $to_date, $param_1) {
    $total_amount = $total_settlement = 0;

        if ($param_1 == 1) {
            $subheading = "Bank Account Other Receipts";
        } elseif ($param_1 == 0) {
            $subheading = "Bank Account Other Payments";
        }

        $bnk_acc_result = get_bank_account_other_receipts_payments($type, $from_date, $to_date, $param_1);
            if (! (db_num_rows($bnk_acc_result) > 0)) {
                $msg = "No any $subheading found";
                    display_note($msg);    br(3);
                    return;
            }

            display_subheading($subheading);

                while ($bnk_acc = db_fetch($bnk_acc_result)) {
                    start_row();
                        label_cell(sql2date($bnk_acc['trans_date']),"align=center");
                        $link = get_trans_view_str($type, $bnk_acc['trans_no']);
                            label_cell($link, "align=center");
                        label_cell($bnk_acc['reference']);
                        
                        $name = $bnk_acc['bank_account_name'];
                        label_cell($name);
                        
                        $comment_res = get_comments($type, $bnk_acc['trans_no']);
                        $comment = db_fetch($comment_res);
                        label_cell($comment['memo_']);
                        
                        $amount = abs($bnk_acc["total_cost"]);
                            label_cell(number_format($amount,2), "align=right");
                    end_row();

                    $total_amount += $amount;
                    $total_settlement += $amount;
                }

                display_subtotals($total_amount, $subheading);
}

function print_month_report_adjustments($type, $from_date, $to_date, $param_1) {
    global $systypes_array;
    
    $total_amount = $total_settlement = 0;

        $subheading = $systypes_array[$type];

        $rep_adj_result = get_month_reporting_adjustments($param_1, $from_date, $to_date);
            if (! (db_num_rows($rep_adj_result) > 0)) {
                $msg = "No any $subheading found";
                    display_note($msg);    br(3);
                    return;
            }

            display_subheading($subheading);

                while ($rep_adj = db_fetch($rep_adj_result)) {
                    start_row();
                        label_cell(sql2date($rep_adj['date']),"align=center");
                        $link = get_trans_view_str($type, $rep_adj['id']);
                            label_cell($link, "align=center");
                        label_cell('');

                        label_cell('');
                        
                        $amount = abs($rep_adj["total_cost"]);
                            label_cell(number_format($amount,2), "align=right");
                    end_row();

                    $total_amount += $amount;
                    $total_settlement += $amount;
                }

                display_subtotals($total_amount, $subheading);
}

function print_customer_credit_notes($type, $from_date, $to_date) {
    $total_amount = $total_settlement = 0;

        $subheading = "Customer Credit Notes";

        $cus_crd_result = get_customer_credit_notes($type, $from_date, $to_date);
            if (! (db_num_rows($cus_crd_result) > 0)) {
                $msg = "No any Customer Credit Notes found";
                    display_note($msg);    br(3);
                    return;
            }

            display_subheading($subheading);

                while ($cus_crd = db_fetch($cus_crd_result)) {
                    start_row();
                        label_cell(sql2date($cus_crd['tran_date']),"align=center");
                        $link = get_trans_view_str($type, $cus_crd['trans_no']);
                            label_cell($link, "align=center");
                        label_cell($cus_crd['reference']);

                        $emp_name = '';
                        if (strlen($cus_crd['debtor_no']) > 0) {
                            $emp_name = get_customer_name($cus_crd['debtor_no']);
                        }
                        label_cell($emp_name);
                        
                        $amount = abs($cus_crd["total_cost"]);
                            label_cell(number_format($amount,2), "align=right");
                    end_row();

                    $total_amount += $amount;
                    $total_settlement += $amount;
                }

                display_subtotals($total_amount, $subheading);
}

function print_supplier_payments($type, $from_date, $to_date) {
    $total_amount = $total_settlement = 0;

        $subheading = "Supplier Payments";

        $sup_pay_result = get_supplier_payments($type, $from_date, $to_date);
            if (! (db_num_rows($sup_pay_result) > 0)) {
                $msg = "No any Supplier Payments found";
                    display_note($msg);    br(3);
                    return;
            }

            display_subheading($subheading);

                while ($sup_pay = db_fetch($sup_pay_result)) {
                    start_row();
                        label_cell(sql2date($sup_pay['tran_date']),"align=center");
                        $link = get_trans_view_str($type, $sup_pay['trans_no']);
                            label_cell($link, "align=center");
                        label_cell($sup_pay['reference']);
                        label_cell($sup_pay['bank_account_name']);

                        $emp_name = '';
                        if (strlen($sup_pay['supplier_id']) > 0) {
                            $emp_name = get_supplier_name($sup_pay['supplier_id']);
                        }
                        label_cell($emp_name);
                        
                        $amount = abs($sup_pay["total_cost"]);
                            label_cell(number_format($amount,2), "align=right");
                    end_row();

                    $total_amount += $amount;
                    $total_settlement += $amount;
                }

                display_subtotals($total_amount, $subheading);
}

function print_iou_advances($type, $from_date, $to_date) {
    $total_amount = $total_settlement = 0;

        $subheading = "I Owe You Advances";

        $iou_adv_result = get_iou_advances($from_date, $to_date);
            if (! (db_num_rows($iou_adv_result) > 0)) {
                $msg = "No any I Owe You Advances found";
                    display_note($msg);    br(3);
                    return;
            }

            display_subheading($subheading);

                while ($iou_adv = db_fetch($iou_adv_result)) {
                    start_row();
                        label_cell(sql2date($iou_adv['tran_date']),"align=center");
                        $link = get_trans_view_str($type, $iou_adv['trans_no']);
                            label_cell($link, "align=center");
                        label_cell($iou_adv['reference']);
                        label_cell($iou_adv['float_name']);

                        $emp_name = '';
                        if (strlen($iou_adv['employee_code']) > 0) {
                            $emp_name = get_group_company_employee_formatted_long_name($iou_adv['company_code'], $iou_adv['employee_code']);
                        }
                        label_cell($emp_name);
                        
                        $amount = abs($iou_adv["total_cost"]);
                            label_cell(number_format($amount,2), "align=right");
                    end_row();

                    $total_amount += $amount;
                    $total_settlement += $amount;
                }

                display_subtotals($total_amount, $subheading);
}


function display_company_cash_flow_transactions($type, $from_date, $to_date, $param_1=null) {

    switch ($type) {
        case ST_CUSTPAYMENT:
            print_customer_payments(ST_CUSTPAYMENT, $from_date, $to_date); 
            break;
//        case ST_SUPPCREDIT:
//            print_supplier_credit_notes(ST_SUPPCREDIT, $from_date, $to_date); 
//            break;
        case ST_I_OWE_YOU_PAYMENT:
            print_iou_payment_returns(ST_I_OWE_YOU_PAYMENT, $from_date, $to_date, $param_1); 
            break;
        case ST_BANKDEPOSIT:
            print_bank_transactions(ST_BANKDEPOSIT, $from_date, $to_date); 
            break;
        case ST_CASH_RECEIPT:
            print_journal_transactions(ST_CASH_RECEIPT, $from_date, $to_date); 
            break;
        case ST_COMP_LOAN_REPAYMENT:
            print_company_loan_repayment_returns(ST_COMP_LOAN_REPAYMENT, $from_date, $to_date, $param_1); 
            break;
        case ST_JOURNAL:
            print_bank_account_other_receipts_payments(ST_JOURNAL, $from_date, $to_date, $param_1); 
            break;
        case ST_MONTH_CASH_IN_AJUSTMENT:
            print_month_report_adjustments(ST_MONTH_CASH_IN_AJUSTMENT, $from_date, $to_date, $param_1); 
            break;
        
//        case ST_CUSTCREDIT:
//            print_customer_credit_notes(ST_CUSTCREDIT, $from_date, $to_date); 
//            break;
        case ST_SUPPAYMENT:
            print_supplier_payments(ST_SUPPAYMENT, $from_date, $to_date); 
            break;
        case ST_BANKPAYMENT:
            print_bank_transactions(ST_BANKPAYMENT, $from_date, $to_date); 
            break;
        case ST_I_OWE_YOU_ADVANCES:
            print_iou_advances(ST_I_OWE_YOU_ADVANCES, $from_date, $to_date); 
            break;
        case ST_CASH_PAYMENT:
            print_journal_transactions(ST_CASH_PAYMENT, $from_date, $to_date); 
            break;
        case ST_MONTH_CASH_OUT_AJUSTMENT:
            print_month_report_adjustments(ST_MONTH_CASH_OUT_AJUSTMENT, $from_date, $to_date, $param_1); 
            break;
        
        default:
            break;
    }    
}

?>