75206499531941b0cbe7acba0e460898d3c6b185
[fa-stable.git] / reporting / rep601.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 2;
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 // trial_inquiry_controls();
29 print_bank_transactions();
30
31 //----------------------------------------------------------------------------------------------------
32
33 function get_bank_balance_to($to, $account)
34 {
35         $to = date2sql($to);
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);
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 ".TB_PREF."bank_trans.* FROM ".TB_PREF."bank_trans
48                 WHERE ".TB_PREF."bank_trans.bank_act = '$account'
49                 AND trans_date >= '$from'
50                 AND trans_date <= '$to'
51                 ORDER BY trans_date,".TB_PREF."bank_trans.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;
59
60         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
61
62         $rep = new FrontReport(_('Bank Statement'), "BankStatement.pdf", user_pagesize());
63
64         $acc = $_POST['PARAM_0'];
65         $from = $_POST['PARAM_1'];
66         $to = $_POST['PARAM_2'];
67         $comments = $_POST['PARAM_3'];
68
69         $dec = user_price_dec();
70
71         $cols = array(0, 90, 110, 170, 225, 350, 400, 460, 520);
72
73         $aligns = array('left', 'left', 'left', 'left', 'left', 'right', 'right', 'right');
74
75         $headers = array(_('Type'),     _('#'), _('Reference'), _('Date'), _('Person/Item'),
76                 _('Debit'),     _('Credit'), _('Balance'));
77
78         $account = get_bank_account($acc);
79         $act = $account['bank_account_name']." - ".$account['bank_curr_code']." - ".$account['bank_account_number'];
80         $params =   array(      0 => $comments,
81             1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
82             2 => array('text' => _('Bank Account'),'from' => $act,'to' => ''));
83
84         $rep->Font();
85         $rep->Info($params, $cols, $headers, $aligns);
86         $rep->Header();
87
88
89         $prev_balance = get_bank_balance_to($from, $account["id"]);
90
91         $trans = get_bank_transactions($from, $to, $account['id']);
92
93         $rows = db_num_rows($trans);
94         if ($prev_balance != 0.0 || $rows != 0)
95         {
96                 $rep->Font('bold');
97                 $rep->TextCol(0, 3,     $act);
98                 $rep->TextCol(3, 5, _('Opening Balance'));
99                 if ($prev_balance > 0.0)
100                         $rep->TextCol(5, 6,     number_format2(abs($prev_balance), $dec));
101                 else
102                         $rep->TextCol(6, 7,     number_format2(abs($prev_balance), $dec));
103                 $rep->Font();
104                 $total = $prev_balance;
105                 $rep->NewLine(2);
106                 if ($rows > 0)
107                 {
108                         while ($myrow=db_fetch($trans))
109                         {
110                                 $total += $myrow['amount'];
111
112                                 $rep->TextCol(0, 1,     systypes::name($myrow["type"]));
113                                 $rep->TextCol(1, 2,     $myrow['trans_no']);
114                                 $rep->TextCol(2, 3,     $myrow['ref']);
115                                 $rep->TextCol(3, 4,     sql2date($myrow["trans_date"]));
116                                 $rep->TextCol(4, 5,     payment_person_types::person_name($myrow["person_type_id"],$myrow["person_id"], false));
117                                 if ($myrow['amount'] > 0.0)
118                                         $rep->TextCol(5, 6,     number_format2(abs($myrow['amount']), $dec));
119                                 else
120                                         $rep->TextCol(6, 7,     number_format2(abs($myrow['amount']), $dec));
121                                 $rep->TextCol(7, 8,     number_format2($total, $dec));
122                                 $rep->NewLine();
123                                 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
124                                 {
125                                         $rep->Line($rep->row - 2);
126                                         $rep->Header();
127                                 }
128                         }
129                         $rep->NewLine();
130                 }
131                 $rep->Font('bold');
132                 $rep->TextCol(3, 5,     _("Ending Balance"));
133                 if ($total > 0.0)
134                         $rep->TextCol(5, 6,     number_format2(abs($total), $dec));
135                 else
136                         $rep->TextCol(6, 7,     number_format2(abs($total), $dec));
137                 $rep->Font();
138                 $rep->Line($rep->row - $rep->lineHeight + 4);
139                 $rep->NewLine(2, 1);
140         }
141         $rep->End();
142 }
143
144 ?>