2566216c5b981fe229ae65ffe3b6d57d6f4f113f
[fa-stable.git] / reporting / rep601.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.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Bank Accounts Transactions
18 // ----------------------------------------------------------------
19 $path_to_root="..";
20
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");
25
26 //----------------------------------------------------------------------------------------------------
27
28 print_bank_transactions();
29
30 //----------------------------------------------------------------------------------------------------
31
32 function get_bank_balance_to($to, $account)
33 {
34         $to = date2sql($to);
35         $sql = "SELECT SUM(amount) 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);
39         return $row[0];
40 }
41
42 function get_bank_transactions($from, $to, $account)
43 {
44         $from = date2sql($from);
45         $to = date2sql($to);
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
51                 ORDER BY trans_date, id";
52
53         return db_query($sql,"The transactions for '$account' could not be retrieved");
54 }
55
56 function print_bank_transactions()
57 {
58         global $path_to_root, $systypes_array;
59
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'];
67
68         if ($destination)
69                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
70         else
71                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
72
73         $orientation = ($orientation ? 'L' : 'P');
74         $rep = new FrontReport(_('Bank Statement'), "BankStatement", user_pagesize(), 9, $orientation);
75         $dec = user_price_dec();
76
77         $cols = array(0, 90, 120, 170, 225, 350, 400, 460, 520);
78
79         $aligns = array('left', 'left', 'left', 'left', 'left', 'right', 'right', 'right');
80
81         $headers = array(_('Type'),     _('#'), _('Reference'), _('Date'), _('Person/Item'),
82                 _('Debit'),     _('Credit'), _('Balance'));
83
84         if ($orientation == 'L')
85                 recalculate_cols($cols);
86         $sql = "SELECT id, bank_account_name, bank_curr_code, bank_account_number FROM ".TB_PREF."bank_accounts";
87         if ($acc != ALL_TEXT)
88                 $sql .= " WHERE id = $acc";
89         $result = db_query($sql, "could not retreive bank accounts");
90         while ($account=db_fetch($result))
91         {
92                 $act = $account['bank_account_name']." - ".$account['bank_curr_code']." - ".$account['bank_account_number'];
93                 $params =   array(      0 => $comments,
94                         1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
95                         2 => array('text' => _('Bank Account'),'from' => $act,'to' => ''));
96
97                 $rep->Font();
98                 $rep->pageNumber = 0;
99                 $rep->Info($params, $cols, $headers, $aligns);
100                 $rep->NewPage();
101
102
103                 $prev_balance = get_bank_balance_to($from, $account["id"]);
104
105                 $trans = get_bank_transactions($from, $to, $account['id']);
106
107                 $rows = db_num_rows($trans);
108                 if ($prev_balance != 0.0 || $rows != 0)
109                 {
110                         $rep->Font('bold');
111                         $rep->TextCol(0, 3,     $act);
112                         $rep->TextCol(3, 5, _('Opening Balance'));
113                         if ($prev_balance > 0.0)
114                                 $rep->AmountCol(5, 6, abs($prev_balance), $dec);
115                         else
116                                 $rep->AmountCol(6, 7, abs($prev_balance), $dec);
117                         $rep->Font();
118                         $total = $prev_balance;
119                         $rep->NewLine(2);
120                         $total_debit = $total_credit = 0;
121                         if ($rows > 0)
122                         {
123                                 // Keep a running total as we loop through
124                                 // the transactions.
125                                 
126                                 while ($myrow=db_fetch($trans))
127                                 {
128                                         if ($zero == 0 && $myrow['amount'] == 0.0)
129                                                 continue;
130                                         $total += $myrow['amount'];
131
132                                         $rep->TextCol(0, 1, $systypes_array[$myrow["type"]]);
133                                         $rep->TextCol(1, 2,     $myrow['trans_no']);
134                                         $rep->TextCol(2, 3,     $myrow['ref']);
135                                         $rep->DateCol(3, 4,     $myrow["trans_date"], true);
136                                         $rep->TextCol(4, 5,     payment_person_name($myrow["person_type_id"],$myrow["person_id"], false));
137                                         if ($myrow['amount'] > 0.0)
138                                         {
139                                                 $rep->AmountCol(5, 6, abs($myrow['amount']), $dec);
140                                                 $total_debit += abs($myrow['amount']);
141                                         }
142                                         else
143                                         {
144                                                 $rep->AmountCol(6, 7, abs($myrow['amount']), $dec);
145                                                 $total_credit += abs($myrow['amount']);
146                                         }
147                                         $rep->AmountCol(7, 8, $total, $dec);
148                                         $rep->NewLine();
149                                         if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
150                                         {
151                                                 $rep->Line($rep->row - 2);
152                                                 $rep->NewPage();
153                                         }
154                                 }
155                                 $rep->NewLine();
156                         }
157                         
158                         // Print totals for the debit and credit columns.
159                         $rep->TextCol(3, 5, _("Total Debit / Credit"));
160                         $rep->AmountCol(5, 6, $total_debit, $dec);
161                         $rep->AmountCol(6, 7, $total_credit, $dec);
162                         $rep->NewLine(2);
163
164                         $rep->Font('bold');
165                         $rep->TextCol(3, 5,     _("Ending Balance"));
166                         if ($total > 0.0)
167                                 $rep->AmountCol(5, 6, abs($total), $dec);
168                         else
169                                 $rep->AmountCol(6, 7, abs($total), $dec);
170                         $rep->Font();
171                         $rep->Line($rep->row - $rep->lineHeight + 4);
172                         $rep->NewLine(2, 1);
173                         
174                         // Print the difference between starting and ending balances.
175                         $net_change = ($total - $prev_balance); 
176                         $rep->TextCol(3, 5, _("Net Change"));
177                         if ($total > 0.0)
178                                 $rep->AmountCol(5, 6, $net_change, $dec, 0, 0, 0, 0, null, 1, True);
179                         else
180                                 $rep->AmountCol(6, 7, $net_change, $dec, 0, 0, 0, 0, null, 1, True);
181                 }
182         }
183         $rep->End();
184 }
185