[0004212] Work Order Entry: fixed error when voided WO refence is reused.
[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                 ORDER BY trans_date, id";
51
52         return db_query($sql,"The transactions for '$account' could not be retrieved");
53 }
54
55 function print_bank_transactions()
56 {
57         global $path_to_root, $systypes_array;
58
59         $acc = $_POST['PARAM_0'];
60         $from = $_POST['PARAM_1'];
61         $to = $_POST['PARAM_2'];
62         $zero = $_POST['PARAM_3'];
63         $comments = $_POST['PARAM_4'];
64         $orientation = $_POST['PARAM_5'];
65         $destination = $_POST['PARAM_6'];
66         if ($destination)
67                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
68         else
69                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
70
71         $orientation = ($orientation ? 'L' : 'P');
72         $rep = new FrontReport(_('Bank Statement'), "BankStatement", user_pagesize(), 9, $orientation);
73         $dec = user_price_dec();
74
75         $cols = array(0, 90, 120, 170, 225, 350, 400, 460, 520);
76
77         $aligns = array('left', 'left', 'left', 'left', 'left', 'right', 'right', 'right');
78
79         $headers = array(_('Type'),     _('#'), _('Reference'), _('Date'), _('Person/Item'),
80                 _('Debit'),     _('Credit'), _('Balance'));
81
82         $account = get_bank_account($acc);
83         $act = $account['bank_account_name']." - ".$account['bank_curr_code']." - ".$account['bank_account_number'];
84         $params =   array(      0 => $comments,
85             1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
86             2 => array('text' => _('Bank Account'),'from' => $act,'to' => ''));
87
88     if ($orientation == 'L')
89         recalculate_cols($cols);
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                 $total_debit = $total_credit = 0;
113                 if ($rows > 0)
114                 {
115                         // Keep a running total as we loop through
116                         // the transactions.
117                         
118                         while ($myrow=db_fetch($trans))
119                         {
120                                 if ($zero == 0 && $myrow['amount'] == 0.0)
121                                         continue;
122                                 $total += $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,     get_counterparty_name($myrow["type"], $myrow["trans_no"], false));
129                                 if ($myrow['amount'] > 0.0)
130                                 {
131                                         $rep->AmountCol(5, 6, abs($myrow['amount']), $dec);
132                                         $total_debit += abs($myrow['amount']);
133                                 }
134                                 else
135                                 {
136                                         $rep->AmountCol(6, 7, abs($myrow['amount']), $dec);
137                                         $total_credit += abs($myrow['amount']);
138                                 }
139                                 $rep->AmountCol(7, 8, $total, $dec);
140                                 $rep->NewLine();
141                                 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
142                                 {
143                                         $rep->Line($rep->row - 2);
144                                         $rep->NewPage();
145                                 }
146                         }
147                         $rep->NewLine();
148                 }
149                 
150                 // Print totals for the debit and credit columns.
151                 $rep->TextCol(3, 5, _("Total Debit / Credit"));
152                 $rep->AmountCol(5, 6, $total_debit, $dec);
153                 $rep->AmountCol(6, 7, $total_credit, $dec);
154                 $rep->NewLine(2);
155
156                 $rep->Font('bold');
157                 $rep->TextCol(3, 5,     _("Ending Balance"));
158                 if ($total > 0.0)
159                         $rep->AmountCol(5, 6, abs($total), $dec);
160                 else
161                         $rep->AmountCol(6, 7, abs($total), $dec);
162                 $rep->Font();
163                 $rep->Line($rep->row - $rep->lineHeight + 4);
164                 $rep->NewLine(2, 1);
165                 
166                 // Print the difference between starting and ending balances.
167                 $net_change = ($total - $prev_balance); 
168                 $rep->TextCol(3, 5, _("Net Change"));
169                 if ($total > 0.0)
170                         $rep->AmountCol(5, 6, $net_change, $dec, 0, 0, 0, 0, null, 1, True);
171                 else
172                         $rep->AmountCol(6, 7, $net_change, $dec, 0, 0, 0, 0, null, 1, True);
173         }
174         $rep->End();
175 }
176