Optimizing some reports.
[fa-stable.git] / reporting / rep704.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_GLREP';
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       GL Accounts Transactions
18 // ----------------------------------------------------------------
19 $path_to_root="..";
20
21 include_once($path_to_root . "/includes/session.inc");
22 include_once($path_to_root . "/admin/db/fiscalyears_db.inc");
23 include_once($path_to_root . "/includes/date_functions.inc");
24 include_once($path_to_root . "/includes/data_checks.inc");
25 include_once($path_to_root . "/gl/includes/gl_db.inc");
26
27 //----------------------------------------------------------------------------------------------------
28
29 print_GL_transactions();
30
31 //----------------------------------------------------------------------------------------------------
32
33 function print_GL_transactions()
34 {
35         global $path_to_root, $systypes_array;
36
37         $dim = get_company_pref('use_dimension');
38         $dimension = $dimension2 = 0;
39
40         $from = $_POST['PARAM_0'];
41         $to = $_POST['PARAM_1'];
42         $fromacc = $_POST['PARAM_2'];
43         $toacc = $_POST['PARAM_3'];
44         if ($dim == 2)
45         {
46                 $dimension = $_POST['PARAM_4'];
47                 $dimension2 = $_POST['PARAM_5'];
48                 $comments = $_POST['PARAM_6'];
49                 $orientation = $_POST['PARAM_7'];
50                 $destination = $_POST['PARAM_8'];
51         }
52         elseif ($dim == 1)
53         {
54                 $dimension = $_POST['PARAM_4'];
55                 $comments = $_POST['PARAM_5'];
56                 $orientation = $_POST['PARAM_6'];
57                 $destination = $_POST['PARAM_7'];
58         }
59         else
60         {
61                 $comments = $_POST['PARAM_4'];
62                 $orientation = $_POST['PARAM_5'];
63                 $destination = $_POST['PARAM_6'];
64         }
65         if ($destination)
66                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
67         else
68                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
69         $orientation = ($orientation ? 'L' : 'P');
70
71         $rep = new FrontReport(_('GL Account Transactions'), "GLAccountTransactions", user_pagesize(), 9, $orientation);
72         $dec = user_price_dec();
73
74         if ($dim == 2)
75         {
76                 $cols = array(0, 65, 105, 125, 175, 230, 290, 345, 405, 465, 525);
77                 //------------0--1---2----3----4----5----6----7----8----9----10-------
78                 //------------------------dim1-dim2-----------------------------------
79                 $headers = array(_('Type'),     _('Ref'), _('#'),       _('Date'), _('Dimension')." 1", _('Dimension')." 2",
80                         _('Person/Item'), _('Debit'),   _('Credit'), _('Balance'));
81         }
82         elseif ($dim == 1)
83         {
84                 $cols = array(0, 65, 105, 125, 175, 260, 260, 345, 405, 465, 525);
85                 //------------0--1---2----3----4----5----6----7----8----9----10-------
86                 //------------------------dim1----------------------------------------
87                 $headers = array(_('Type'),     _('Ref'), _('#'),       _('Date'), _('Dimension'), "", _('Person/Item'),
88                         _('Debit'),     _('Credit'), _('Balance'));
89         }
90         else
91         {
92                 $cols = array(0, 65, 105, 125, 175, 175, 175, 345, 405, 465, 525);
93                 //------------0--1---2----3----4----5----6----7----8----9----10-------
94                 //--------------------------------------------------------------------
95                 $headers = array(_('Type'),     _('Ref'), _('#'),       _('Date'), "", "", _('Person/Item'),
96                         _('Debit'),     _('Credit'), _('Balance'));
97         }
98         $aligns = array('left', 'left', 'left', 'left', 'left', 'left', 'left', 'right', 'right', 'right');
99
100         if ($dim == 2)
101         {
102         $params =   array(      0 => $comments,
103                                     1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
104                                     2 => array('text' => _('Accounts'),'from' => $fromacc,'to' => $toacc),
105                         3 => array('text' => _('Dimension')." 1", 'from' => get_dimension_string($dimension),
106                             'to' => ''),
107                         4 => array('text' => _('Dimension')." 2", 'from' => get_dimension_string($dimension2),
108                             'to' => ''));
109     }
110     elseif ($dim == 1)
111     {
112         $params =   array(      0 => $comments,
113                                     1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
114                                     2 => array('text' => _('Accounts'),'from' => $fromacc,'to' => $toacc),
115                         3 => array('text' => _('Dimension'), 'from' => get_dimension_string($dimension),
116                             'to' => ''));
117     }
118     else
119     {
120         $params =   array(      0 => $comments,
121                                     1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
122                                     2 => array('text' => _('Accounts'),'from' => $fromacc,'to' => $toacc));
123     }
124     if ($orientation == 'L')
125         recalculate_cols($cols);
126
127         $rep->Font();
128         $rep->Info($params, $cols, $headers, $aligns);
129         $rep->NewPage();
130
131         $accounts = get_gl_accounts($fromacc, $toacc);
132
133         while ($account=db_fetch($accounts))
134         {
135                 if (is_account_balancesheet($account["account_code"]))
136                         $begin = "";
137                 else
138                 {
139                         $begin = get_fiscalyear_begin_for_date($from);
140                         if (date1_greater_date2($begin, $from))
141                                 $begin = $from;
142                         $begin = add_days($begin, -1);
143                 }
144                 $prev_balance = get_gl_balance_from_to($begin, $from, $account["account_code"], $dimension, $dimension2);
145
146                 $trans = get_gl_transactions($from, $to, -1, $account['account_code'], $dimension, $dimension2);
147                 $rows = db_num_rows($trans);
148                 if ($prev_balance == 0.0 && $rows == 0)
149                         continue;
150                 $rep->Font('bold');
151                 $rep->TextCol(0, 4,     $account['account_code'] . " " . $account['account_name'], -2);
152                 $rep->TextCol(4, 6, _('Opening Balance'));
153                 if ($prev_balance > 0.0)
154                         $rep->AmountCol(7, 8, abs($prev_balance), $dec);
155                 else
156                         $rep->AmountCol(8, 9, abs($prev_balance), $dec);
157                 $rep->Font();
158                 $total = $prev_balance;
159                 $rep->NewLine(2);
160                 if ($rows > 0)
161                 {
162                         while ($myrow=db_fetch($trans))
163                         {
164                                 $total += $myrow['amount'];
165
166                                 $rep->TextCol(0, 1, $systypes_array[$myrow["type"]], -2);
167                                 $reference = get_reference($myrow["type"], $myrow["type_no"]);
168                                 $rep->TextCol(1, 2, $reference);
169                                 $rep->TextCol(2, 3,     $myrow['type_no'], -2);
170                                 $rep->DateCol(3, 4,     $myrow["tran_date"], true);
171                                 if ($dim >= 1)
172                                         $rep->TextCol(4, 5,     get_dimension_string($myrow['dimension_id']));
173                                 if ($dim > 1)
174                                         $rep->TextCol(5, 6,     get_dimension_string($myrow['dimension2_id']));
175                                 $txt = payment_person_name($myrow["person_type_id"],$myrow["person_id"], false);
176                                 $memo = $myrow['memo_'];
177                                 if ($txt != "")
178                                 {
179                                         if ($memo != "")
180                                                 $txt = $txt."/".$memo;
181                                 }
182                                 else
183                                         $txt = $memo;
184                                 $rep->TextCol(6, 7,     $txt, -2);
185                                 if ($myrow['amount'] > 0.0)
186                                         $rep->AmountCol(7, 8, abs($myrow['amount']), $dec);
187                                 else
188                                         $rep->AmountCol(8, 9, abs($myrow['amount']), $dec);
189                                 $rep->TextCol(9, 10, number_format2($total, $dec));
190                                 $rep->NewLine();
191                                 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
192                                 {
193                                         $rep->Line($rep->row - 2);
194                                         $rep->NewPage();
195                                 }
196                         }
197                         $rep->NewLine();
198                 }
199                 $rep->Font('bold');
200                 $rep->TextCol(4, 6,     _("Ending Balance"));
201                 if ($total > 0.0)
202                         $rep->AmountCol(7, 8, abs($total), $dec);
203                 else
204                         $rep->AmountCol(8, 9, abs($total), $dec);
205                 $rep->Font();
206                 $rep->Line($rep->row - $rep->lineHeight + 4);
207                 $rep->NewLine(2, 1);
208         }
209         $rep->End();
210 }
211