2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_BANKREP';
13 // ----------------------------------------------------------------
17 // Title: Bank Accounts Transactions
18 // ----------------------------------------------------------------
21 include_once($path_to_root . "/includes/session.inc");
22 include_once($path_to_root . "/includes/date_functions.inc");
23 include_once($path_to_root . "/includes/data_checks.inc");
24 include_once($path_to_root . "/gl/includes/gl_db.inc");
26 //----------------------------------------------------------------------------------------------------
28 print_bank_transactions();
30 //----------------------------------------------------------------------------------------------------
32 function get_bank_balance_to($to, $account)
35 $sql = "SELECT SUM(amount+charge) FROM ".TB_PREF."bank_trans WHERE bank_act='$account'
36 AND trans_date < '$to'";
37 $result = db_query($sql, "The starting balance on hand could not be calculated");
38 $row = db_fetch_row($result);
42 function get_bank_transactions($from, $to, $account)
44 $from = date2sql($from);
46 $sql = "SELECT * FROM ".TB_PREF."bank_trans
47 WHERE bank_act = '$account'
48 AND trans_date >= '$from'
49 AND trans_date <= '$to'
50 AND (amount !=0 OR charge !=0)
51 ORDER BY trans_date, id";
53 return db_query($sql,"The transactions for '$account' could not be retrieved");
56 function print_bank_transactions()
58 global $path_to_root, $systypes_array;
60 $acc = $_POST['PARAM_0'];
61 $from = $_POST['PARAM_1'];
62 $to = $_POST['PARAM_2'];
63 $zero = $_POST['PARAM_3'];
64 $comments = $_POST['PARAM_4'];
65 $orientation = $_POST['PARAM_5'];
66 $destination = $_POST['PARAM_6'];
68 include_once($path_to_root . "/reporting/includes/excel_report.inc");
70 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
72 $orientation = ($orientation ? 'L' : 'P');
73 $rep = new FrontReport(_('Bank Statement'), "BankStatement", user_pagesize(), 9, $orientation);
74 $dec = user_price_dec();
76 $cols = array(0, 90, 120, 170, 225, 350, 400, 450, 500, 550);
78 $aligns = array('left', 'left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
80 $headers = array(_('Type'), _('#'), _('Reference'), _('Date'), _('Person/Item'),
81 _('Debit'), _('Credit'), _('Fee'), _('Balance'));
83 $account = get_bank_account($acc);
84 $act = $account['bank_account_name']." - ".$account['bank_curr_code']." - ".$account['bank_account_number'];
85 $params = array( 0 => $comments,
86 1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
87 2 => array('text' => _('Bank Account'),'from' => $act,'to' => ''));
89 if ($orientation == 'L')
90 recalculate_cols($cols);
92 $rep->Info($params, $cols, $headers, $aligns);
96 $prev_balance = get_bank_balance_to($from, $account["id"]);
98 $trans = get_bank_transactions($from, $to, $account['id']);
100 $rows = db_num_rows($trans);
101 if ($prev_balance != 0.0 || $rows != 0)
104 $rep->TextCol(0, 3, $act);
105 $rep->TextCol(3, 5, _('Opening Balance'));
106 if ($prev_balance > 0.0)
107 $rep->AmountCol(5, 6, $prev_balance, $dec);
109 $rep->AmountCol(6, 7, -$prev_balance, $dec);
111 $total = $prev_balance;
113 $total_debit = $total_credit = $charges = 0;
116 // Keep a running total as we loop through
119 while ($myrow=db_fetch($trans))
121 if ($zero == 0 && $myrow['amount'] == 0.0)
123 $total += $myrow['amount']+$myrow['charge'];
124 $charges += $myrow['charge'];
125 $amount = abs($myrow['amount']);
127 $rep->TextCol(0, 1, $systypes_array[$myrow["type"]]);
128 $rep->TextCol(1, 2, $myrow['trans_no']);
129 $rep->TextCol(2, 3, $myrow['ref']);
130 $rep->DateCol(3, 4, $myrow["trans_date"], true);
131 $rep->TextCol(4, 5, get_counterparty_name($myrow["type"], $myrow["trans_no"], false));
132 if ($myrow['amount'] > 0.0)
134 $rep->AmountCol(5, 6, $amount, $dec);
135 $total_debit += $amount;
139 $rep->AmountCol(6, 7, $amount, $dec);
140 $total_credit += $amount;
142 if ($myrow['charge'])
143 $rep->AmountCol(7, 8, -$myrow['charge'], $dec);
144 $rep->AmountCol(8, 9, $total, $dec);
146 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
148 $rep->Line($rep->row - 2);
155 // Print totals for the debit and credit columns.
156 $rep->TextCol(3, 5, _("Total Debit / Credit / Fees"));
157 $rep->AmountCol(5, 6, $total_debit, $dec);
158 $rep->AmountCol(6, 7, $total_credit, $dec);
159 $rep->AmountCol(7, 8, -$charges, $dec);
163 $rep->TextCol(3, 5, _("Ending Balance"));
165 $rep->AmountCol(5, 6, $total, $dec);
167 $rep->AmountCol(6, 7, -$total, $dec);
169 $rep->Line($rep->row - $rep->lineHeight + 4);
172 // Print the difference between starting and ending balances.
173 $net_change = ($total - $prev_balance);
174 $rep->TextCol(3, 5, _("Net Change"));
176 $rep->AmountCol(5, 6, $net_change, $dec, 0, 0, 0, 0, null, 1, True);
178 $rep->AmountCol(6, 7, $net_change, $dec, 0, 0, 0, 0, null, 1, True);