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 // ----------------------------------------------------------------
14 // $ Revision: 2.3.3-1
15 // Creator: Chaitanya-India <3chaitanya@gmail.com>
17 // Title: Bank Statements w/Reconcile
18 // Desc: Bank Statement w/ Reconcile like the normal Bank Statement but with reconcile columns
19 // ----------------------------------------------------------------
22 include_once($path_to_root . "/includes/session.inc");
23 include_once($path_to_root . "/includes/date_functions.inc");
24 include_once($path_to_root . "/includes/data_checks.inc");
25 include_once($path_to_root . "/gl/includes/gl_db.inc");
27 //----------------------------------------------------------------------------------------------------
29 print_bank_transactions_reconcile();
31 //----------------------------------------------------------------------------------------------------
33 function get_bank_balance_to($to, $account)
36 $sql = "SELECT SUM(amount) FROM ".TB_PREF."bank_trans WHERE bank_act='$account'
37 AND trans_date < '$to'";
38 $result = db_query($sql, "The starting balance on hand could not be calculated");
39 $row = db_fetch_row($result);
43 function get_bank_transactions($from, $to, $account)
45 $from = date2sql($from);
47 $sql = "SELECT trans.*, com.memo_
49 .TB_PREF."bank_trans trans
50 LEFT JOIN ".TB_PREF."comments com ON trans.type = com.type AND trans.trans_no = com.id
51 WHERE trans.bank_act = '$account'
52 AND trans_date >= '$from'
53 AND trans_date <= '$to'
55 ORDER BY trans_date,trans.id";
57 return db_query($sql,"The transactions for '$account' could not be retrieved");
60 function print_bank_transactions_reconcile()
62 global $path_to_root, $systypes_array;
64 $acc = $_POST['PARAM_0'];
65 $from = $_POST['PARAM_1'];
66 $to = $_POST['PARAM_2'];
67 $comments = $_POST['PARAM_3'];
68 $destination = $_POST['PARAM_4'];
70 include_once($path_to_root . "/reporting/includes/excel_report.inc");
72 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
74 $rep = new FrontReport(_('Bank Statement w/Reconcile'), "BankStatementReconcile", user_pagesize(), 9, "L");
75 $dec = user_price_dec();
77 $cols = array(0, 90, 120, 170, 225, 450, 500, 550, 600, 660, 700);
79 $aligns = array('left', 'left', 'left', 'left', 'left', 'right', 'right', 'right', 'center', 'left');
81 $headers = array(_('Type'), _('#'), _('Reference'), _('Date'), _('Person/Item'),
82 _('Debit'), _('Credit'), _('Balance'), _('Reco Date'), _('Narration'));
84 $account = get_bank_account($acc);
85 $act = $account['bank_account_name']." - ".$account['bank_curr_code']." - ".$account['bank_account_number'];
86 $params = array( 0 => $comments,
87 1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
88 2 => array('text' => _('Bank Account'),'from' => $act,'to' => ''));
91 $rep->Info($params, $cols, $headers, $aligns);
95 $prev_balance = get_bank_balance_to($from, $account["id"]);
97 $trans = get_bank_transactions($from, $to, $account['id']);
99 $rows = db_num_rows($trans);
100 if ($prev_balance != 0.0 || $rows != 0)
103 $rep->TextCol(0, 3, $act);
104 $rep->TextCol(3, 5, _('Opening Balance'));
105 if ($prev_balance > 0.0)
106 $rep->AmountCol(5, 6, abs($prev_balance), $dec);
108 $rep->AmountCol(6, 7, abs($prev_balance), $dec);
110 $total = $prev_balance;
112 // Keep a running total as we loop through
114 $total_debit = $total_credit = 0;
118 while ($myrow=db_fetch($trans))
120 $total += $myrow['amount'];
122 $rep->TextCol(0, 1, $systypes_array[$myrow["type"]]);
123 $rep->TextCol(1, 2, $myrow['trans_no']);
124 $rep->TextCol(2, 3, $myrow['ref']);
125 $rep->DateCol(3, 4, $myrow["trans_date"], true);
126 $rep->TextCol(4, 5, payment_person_name($myrow["person_type_id"],$myrow["person_id"], false));
127 if ($myrow['amount'] > 0.0)
129 $rep->AmountCol(5, 6, abs($myrow['amount']), $dec);
130 $total_debit += abs($myrow['amount']);
134 $rep->AmountCol(6, 7, abs($myrow['amount']), $dec);
135 $total_credit += abs($myrow['amount']);
137 $rep->AmountCol(7, 8, $total, $dec);
138 if ($myrow["reconciled"] && $myrow["reconciled"] != '0000-00-00')
139 $rep->DateCol(8, 9, $myrow["reconciled"], true);
140 $rep->TextCol(9, 10, $myrow['memo_']);
142 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
144 $rep->Line($rep->row - 2);
151 // Print totals for the debit and credit columns.
152 $rep->TextCol(3, 5, _("Total Debit / Credit"));
153 $rep->AmountCol(5, 6, $total_debit, $dec);
154 $rep->AmountCol(6, 7, $total_credit, $dec);
158 $rep->TextCol(3, 5, _("Ending Balance"));
160 $rep->AmountCol(5, 6, abs($total), $dec);
162 $rep->AmountCol(6, 7, abs($total), $dec);
166 // Print the difference between starting and ending balances.
167 $net_change = ($total - $prev_balance);
168 $rep->TextCol(3, 5, _("Net Change"));
170 $rep->AmountCol(5, 6, $net_change, $dec, 0, 0, 0, 0, null, 1, True);
172 $rep->AmountCol(6, 7, $net_change, $dec, 0, 0, 0, 0, null, 1, True);
176 // Calculate Bank Balance as per reco
177 $date = date2sql($to);
178 $sql = "SELECT SUM(IF(reconciled<='$date' AND reconciled !='0000-00-00', amount, 0)) as reconciled,
179 SUM(amount) as books_total
180 FROM ".TB_PREF."bank_trans trans
181 WHERE bank_act=".db_escape($account['id'])."
182 AND trans_date <= '$date'";
184 // ." AND trans.reconciled IS NOT NULL";
185 //display_notification($sql);
186 $t_result = db_query($sql,"Cannot retrieve reconciliation data");
188 if ($t_row = db_fetch($t_result)) {
189 $books_total = $t_row['books_total'];
190 $reconciled = $t_row['reconciled'];
192 $difference = $books_total - $reconciled;
194 // Bank Balance (by Reco)
196 $rep->TextCol(3, 5, _("Bank Balance"));
197 if ($reconciled > 0.0)
198 $rep->AmountCol(5, 6, abs($reconciled), $dec);
200 $rep->AmountCol(6, 7, abs($reconciled), $dec);
206 $rep->TextCol(3, 5, _("Difference"));
207 if ($difference > 0.0)
208 $rep->AmountCol(5, 6, abs($difference), $dec);
210 $rep->AmountCol(6, 7, abs($difference), $dec);
214 $rep->Line($rep->row - $rep->lineHeight + 4);