c75efdf3cdf82fb200449db2b377bd9ba5c70e79
[fa-stable.git] / reporting / rep602.php
1 <?php
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>
16 // date_:       2005-05-19
17 // Title:       Bank Statements w/Reconcile
18 // Desc:        Bank Statement w/ Reconcile like the normal Bank Statement but with reconcile columns
19 // ----------------------------------------------------------------
20 $path_to_root="..";
21
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");
26
27 //----------------------------------------------------------------------------------------------------
28
29 print_bank_transactions_reconcile();
30
31 //----------------------------------------------------------------------------------------------------
32
33 function get_bank_balance_to($to, $account)
34 {
35         $to = date2sql($to);
36         $sql = "SELECT SUM(amount+charge) 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);
40         return $row[0];
41 }
42
43 function get_bank_transactions($from, $to, $account)
44 {
45         $from = date2sql($from);
46         $to = date2sql($to);
47         $sql = "SELECT trans.*, com.memo_
48                         FROM "
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'
54                 AND (amount !=0 OR charge !=0)
55                 ORDER BY trans_date,trans.id";
56
57         return db_query($sql,"The transactions for '$account' could not be retrieved");
58 }
59
60 function print_bank_transactions_reconcile()
61 {
62         global $path_to_root, $systypes_array;
63
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'];
69         if ($destination)
70                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
71         else
72                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
73
74         $rep = new FrontReport(_('Bank Statement w/Reconcile'), "BankStatementReconcile", user_pagesize(), 9, "L");
75         $dec = user_price_dec();
76
77         $cols = array(0, 90, 120, 170, 225, 400, 450, 500, 550, 600, 660, 700);
78
79         $aligns = array('left', 'left', 'left', 'left', 'left', 'right', 'right', 'right', 'right', 'center', 'left');
80
81         $headers = array(_('Type'),     _('#'), _('Reference'), _('Date'), _('Person/Item'),
82                 _('Debit'),     _('Credit'), _('Fee Incl.'), _('Balance'), _('Reco Date'), _('Narration'));
83
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' => ''));
89
90         $rep->Font();
91         $rep->Info($params, $cols, $headers, $aligns);
92         $rep->NewPage();
93
94
95         $prev_balance = get_bank_balance_to($from, $account["id"]);
96
97         $trans = get_bank_transactions($from, $to, $account['id']);
98
99         $rows = db_num_rows($trans);
100         if ($prev_balance != 0.0 || $rows != 0)
101         {
102                 $rep->Font('bold');
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);
107                 else
108                         $rep->AmountCol(6, 7, abs($prev_balance), $dec);
109                 $rep->Font();
110                 $total = $prev_balance;
111                 $rep->NewLine(2);
112                 // Keep a running total as we loop through
113                 // the transactions.
114                 $total_debit = $total_credit = $charges = 0;
115                 if ($rows > 0)
116                 {
117                         
118                         while ($myrow=db_fetch($trans))
119                         {
120                                 $total += $myrow['amount']+$myrow['charge'];
121                                 $charges += $myrow['charge'];
122                                 $amount = abs($myrow['amount']);
123
124                                 $rep->TextCol(0, 1, $systypes_array[$myrow["type"]]);
125                                 $rep->TextCol(1, 2,     $myrow['trans_no']);
126                                 $rep->TextCol(2, 3,     $myrow['ref']);
127                                 $rep->DateCol(3, 4,     $myrow["trans_date"], true);
128                                 $rep->TextCol(4, 5,     payment_person_name($myrow["person_type_id"],$myrow["person_id"], false));
129                                 if ($myrow['amount'] > 0.0)
130                                 {
131                                         $rep->AmountCol(5, 6, $amount, $dec);
132                                         $total_debit += $amount;
133                                 }
134                                 else
135                                 {
136                                         $rep->AmountCol(6, 7, $amount, $dec);
137                                         $total_credit += $amount;
138                                 }
139                                 if ($myrow['charge'])
140                                         $rep->AmountCol(7, 8, -$myrow['charge'], $dec);
141
142                                 $rep->AmountCol(8, 9, $total, $dec);
143                                 if ($myrow["reconciled"] && $myrow["reconciled"] != '0000-00-00')
144                                         $rep->DateCol(9, 10,    $myrow["reconciled"], true);
145                                 $rep->TextCol(10, 11, $myrow['memo_']);
146                                 $rep->NewLine();
147                                 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
148                                 {
149                                         $rep->Line($rep->row - 2);
150                                         $rep->NewPage();
151                                 }
152                         }
153                         $rep->NewLine();
154                 }
155                 
156                 // Print totals for the debit and credit columns.
157                 $rep->TextCol(3, 5, _("Total Debit / Credit / Fees"));
158                 $rep->AmountCol(5, 6, $total_debit, $dec);
159                 $rep->AmountCol(6, 7, $total_credit, $dec);
160                 $rep->AmountCol(7, 8, -$charges, $dec);
161                 $rep->NewLine(2);
162
163                 $rep->Font('bold');
164                 $rep->TextCol(3, 5,     _("Ending Balance"));
165                 if ($total > 0.0)
166                         $rep->AmountCol(5, 6, $total, $dec);
167                 else
168                         $rep->AmountCol(6, 7, -$total, $dec);
169                 $rep->Font();
170                 $rep->NewLine(2);       
171                 
172                 // Print the difference between starting and ending balances.
173                 $net_change = ($total - $prev_balance); 
174                 $rep->TextCol(3, 5, _("Net Change"));
175                 if ($total > 0.0)
176                         $rep->AmountCol(5, 6, $net_change, $dec, 0, 0, 0, 0, null, 1, True);
177                 else
178                         $rep->AmountCol(6, 7, $net_change, $dec, 0, 0, 0, 0, null, 1, True);
179                 $rep->Font();
180                 $rep->NewLine(2);       
181                 
182                 // Calculate Bank Balance as per reco
183                 $date = date2sql($to);
184                 $sql = "SELECT SUM(IF(reconciled<='$date' AND reconciled !='0000-00-00', amount+charge, 0)) as reconciled,
185                                  SUM(amount) as books_total
186                         FROM ".TB_PREF."bank_trans trans
187                         WHERE bank_act=".db_escape($account['id'])."
188                         AND trans_date <= '$date'";     
189                         
190                 //      ." AND trans.reconciled IS NOT NULL";
191                 //display_notification($sql);
192                 $t_result = db_query($sql,"Cannot retrieve reconciliation data");
193
194                 if ($t_row = db_fetch($t_result)) {
195                         $books_total = $t_row['books_total'];
196                         $reconciled = $t_row['reconciled'];
197                 }                       
198                 $difference = $books_total - $reconciled;               
199                 
200                 // Bank Balance (by Reco)
201                 $rep->Font('bold');
202                 $rep->TextCol(3, 5,     _("Bank Balance"));
203                 if ($reconciled > 0.0)
204                         $rep->AmountCol(5, 6, abs($reconciled), $dec);
205                 else
206                         $rep->AmountCol(6, 7, abs($reconciled), $dec);
207                 $rep->Font();
208                 $rep->NewLine(2);       
209
210                 // Reco Difference
211                 $rep->Font('bold');
212                 $rep->TextCol(3, 5,     _("Difference"));
213                 if ($difference > 0.0)
214                         $rep->AmountCol(5, 6, abs($difference), $dec);
215                 else
216                         $rep->AmountCol(6, 7, abs($difference), $dec);
217                 $rep->Font();
218                 $rep->NewLine(2);       
219                         
220                 $rep->Line($rep->row - $rep->lineHeight + 4);
221                 $rep->NewLine(2, 1);
222                         
223         }
224         $rep->End();
225 }
226