*** empty log message ***
[fa-stable.git] / reporting / rep601.php
1 <?php
2
3 $page_security = 2;
4 // ----------------------------------------------------------------
5 // $ Revision:  2.0 $
6 // Creator:     Joe Hunt
7 // date_:       2005-05-19
8 // Title:       Bank Accounts Transactions
9 // ----------------------------------------------------------------
10 $path_to_root="../";
11
12 include_once($path_to_root . "includes/session.inc");
13 include_once($path_to_root . "includes/date_functions.inc");
14 include_once($path_to_root . "includes/data_checks.inc");
15 include_once($path_to_root . "gl/includes/gl_db.inc");
16
17 //----------------------------------------------------------------------------------------------------
18
19 // trial_inquiry_controls();
20 print_bank_transactions();
21
22 //----------------------------------------------------------------------------------------------------
23
24 function get_bank_balance_to($to, $account)
25 {
26         $to = date2sql($to);
27         $sql = "SELECT SUM(amount) FROM ".TB_PREF."bank_trans WHERE bank_act='$account'
28         AND trans_date < '$to'";
29         $result = db_query($sql, "The starting balance on hand could not be calculated");
30         $row = db_fetch_row($result);
31         return $row[0];
32 }
33
34 function get_bank_transactions($from, $to, $account)
35 {
36         $from = date2sql($from);
37         $to = date2sql($to);
38         $sql = "SELECT ".TB_PREF."bank_trans.* FROM ".TB_PREF."bank_trans
39                 WHERE ".TB_PREF."bank_trans.bank_act = '$account'
40                 AND trans_date >= '$from'
41                 AND trans_date <= '$to'
42                 ORDER BY trans_date,".TB_PREF."bank_trans.id";
43
44         return db_query($sql,"The transactions for '$account' could not be retrieved");
45 }
46
47 function print_bank_transactions()
48 {
49         global $path_to_root;
50
51         include_once($path_to_root . "reporting/includes/pdf_report.inc");
52
53         $rep = new FrontReport(_('Bank Statement'), "BankStatement.pdf", user_pagesize());
54
55         $acc = $_POST['PARAM_0'];
56         $from = $_POST['PARAM_1'];
57         $to = $_POST['PARAM_2'];
58         $comments = $_POST['PARAM_3'];
59
60         $dec = user_price_dec();
61
62         $cols = array(0, 90, 110, 170, 225, 350, 400, 460, 520);
63
64         $aligns = array('left', 'left', 'left', 'left', 'left', 'right', 'right', 'right');
65
66         $headers = array(_('Type'),     _('#'), _('Reference'), _('Date'), _('Person/Item'),
67                 _('Debit'),     _('Credit'), _('Balance'));
68
69         $account = get_bank_account($acc);
70         $act = $account['bank_account_name']." - ".$account['bank_curr_code']." - ".$account['bank_account_number'];
71         $params =   array(      0 => $comments,
72             1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
73             2 => array('text' => _('Bank Account'),'from' => $act,'to' => ''));
74
75         $rep->Font();
76         $rep->Info($params, $cols, $headers, $aligns);
77         $rep->Header();
78
79
80         $prev_balance = get_bank_balance_to($from, $account["id"]);
81
82         $trans = get_bank_transactions($from, $to, $account['id']);
83
84         $rows = db_num_rows($trans);
85         if ($prev_balance != 0.0 || $rows != 0)
86         {
87                 $rep->Font('bold');
88                 $rep->TextCol(0, 3,     $act);
89                 $rep->TextCol(3, 5, _('Opening Balance'));
90                 if ($prev_balance > 0.0)
91                         $rep->TextCol(6, 7,     number_format2(abs($prev_balance), $dec));
92                 else
93                         $rep->TextCol(7, 8,     number_format2(abs($prev_balance), $dec));
94                 $rep->Font();
95                 $total = $prev_balance;
96                 $rep->NewLine(2);
97                 if ($rows > 0)
98                 {
99                         while ($myrow=db_fetch($trans))
100                         {
101                                 $total += $myrow['amount'];
102
103                                 $rep->TextCol(0, 1,     systypes::name($myrow["type"]));
104                                 $rep->TextCol(1, 2,     $myrow['trans_no']);
105                                 $rep->TextCol(2, 3,     $myrow['ref']);
106                                 $rep->TextCol(3, 4,     sql2date($myrow["trans_date"]));
107                                 $rep->TextCol(4, 5,     payment_person_types::person_name($myrow["person_type_id"],$myrow["person_id"], false));
108                                 if ($myrow['amount'] > 0.0)
109                                         $rep->TextCol(5, 6,     number_format2(abs($myrow['amount']), $dec));
110                                 else
111                                         $rep->TextCol(6, 7,     number_format2(abs($myrow['amount']), $dec));
112                                 $rep->TextCol(7, 8,     number_format2($total, $dec));
113                                 $rep->NewLine();
114                                 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
115                                 {
116                                         $rep->Line($rep->row - 2);
117                                         $rep->Header();
118                                 }
119                         }
120                         $rep->NewLine();
121                 }
122                 $rep->Font('bold');
123                 $rep->TextCol(3, 5,     _("Ending Balance"));
124                 if ($total > 0.0)
125                         $rep->TextCol(6, 7,     number_format2(abs($total), $dec));
126                 else
127                         $rep->TextCol(7, 8,     number_format2(abs($total), $dec));
128                 $rep->Font();
129                 $rep->Line($rep->row - $rep->lineHeight + 4);
130                 $rep->NewLine(2, 1);
131         }
132         $rep->End();
133 }
134
135 ?>