Merging version 2.1 RC to main trunk.
[fa-stable.git] / reporting / rep501.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 = 2;
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Dimension Summary
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_dimension_summary();
29
30 function getTransactions($from, $to)
31 {
32         $sql = "SELECT *
33                 FROM
34                         ".TB_PREF."dimensions
35                 WHERE reference >= '$from'
36                 AND reference <= '$to'
37                 ORDER BY
38                         reference";
39
40     return db_query($sql,"No transactions were returned");
41 }
42
43 function getYTD($dim)
44 {
45         $date = Today();
46         $date = begin_fiscalyear($date);
47         date2sql($date);
48         
49         $sql = "SELECT SUM(amount) AS Balance
50                 FROM
51                         ".TB_PREF."gl_trans
52                 WHERE (dimension_id = '$dim' OR dimension2_id = '$dim')
53                 AND tran_date >= '$date'";
54
55     $TransResult = db_query($sql,"No transactions were returned");
56         if (db_num_rows($TransResult) == 1)
57         {
58                 $DemandRow = db_fetch_row($TransResult);
59                 $balance = $DemandRow[0];
60         }
61         else
62                 $balance = 0.0;
63
64     return $balance;
65 }
66
67 //----------------------------------------------------------------------------------------------------
68
69 function print_dimension_summary()
70 {
71     global $path_to_root;
72
73     $fromdim = $_POST['PARAM_0'];
74     $todim = $_POST['PARAM_1'];
75     $showbal = $_POST['PARAM_2'];
76     $comments = $_POST['PARAM_3'];
77         $destination = $_POST['PARAM_4'];
78         if ($destination)
79                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
80         else
81                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
82
83         $cols = array(0, 50, 210, 250, 320, 395, 465,   515);
84
85         $headers = array(_('Reference'), _('Name'), _('Type'), _('Date'), _('Due Date'), _('Closed'), _('YTD'));
86
87         $aligns = array('left', 'left', 'left', 'left', 'left', 'left', 'right');
88
89     $params =   array(  0 => $comments,
90                                     1 => array('text' => _('Dimension'), 'from' => $fromdim, 'to' => $todim));
91
92     $rep = new FrontReport(_('Dimension Summary'), "DimensionSummary", user_pagesize());
93
94     $rep->Font();
95     $rep->Info($params, $cols, $headers, $aligns);
96     $rep->Header();
97
98         $res = getTransactions($fromdim, $todim);
99         while ($trans=db_fetch($res))
100         {
101                 $rep->TextCol(0, 1, $trans['reference']);
102                 $rep->TextCol(1, 2, $trans['name']);
103                 $rep->TextCol(2, 3, $trans['type_']);
104                 $rep->DateCol(3, 4, $trans['date_'], true);
105                 $rep->DateCol(4, 5, $trans['due_date'], true);
106                 if ($trans['closed'])
107                         $str = _('Yes');
108                 else
109                         $str = _('No');
110                 $rep->TextCol(5, 6, $str);
111                 if ($showbal)
112                 {
113                         $balance = getYTD($trans['id']);
114                         $rep->AmountCol(6, 7, $balance, 0);
115                 }       
116                 $rep->NewLine(1, 2);
117         }
118         $rep->Line($rep->row);
119     $rep->End();
120 }
121
122 ?>