Added option to select how to present Balance Sheet and P&L Statement
[fa-stable.git] / reporting / rep706.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:       Balance Sheet
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_balance_sheet();
29
30
31 //----------------------------------------------------------------------------------------------------
32
33 function print_balance_sheet()
34 {
35         global $comp_path, $path_to_root;
36
37         $dim = get_company_pref('use_dimension');
38         $dimension = $dimension2 = 0;
39
40         $from = $_POST['PARAM_0'];
41         $to = $_POST['PARAM_1'];
42         if ($dim == 2)
43         {
44                 $dimension = $_POST['PARAM_2'];
45                 $dimension2 = $_POST['PARAM_3'];
46                 $graphics = $_POST['PARAM_4'];
47                 $comments = $_POST['PARAM_5'];
48                 $destination = $_POST['PARAM_6'];
49         }
50         else if ($dim == 1)
51         {
52                 $dimension = $_POST['PARAM_2'];
53                 $graphics = $_POST['PARAM_3'];
54                 $comments = $_POST['PARAM_4'];
55                 $destination = $_POST['PARAM_5'];
56         }
57         else
58         {
59                 $graphics = $_POST['PARAM_2'];
60                 $comments = $_POST['PARAM_3'];
61                 $destination = $_POST['PARAM_4'];
62         }
63         if ($destination)
64                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
65         else
66                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
67         if ($graphics)
68         {
69                 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
70                 $pg = new graph();
71         }
72         $dec = 0;
73
74         $cols = array(0, 50, 200, 350, 425,     500);
75         //------------0--1---2----3----4----5--
76
77         $headers = array(_('Account'), _('Account Name'), _('Open Balance'), _('Period'),
78                 _('Close Balance'));
79
80         $aligns = array('left', 'left', 'right', 'right', 'right');
81
82     if ($dim == 2)
83     {
84         $params =   array(      0 => $comments,
85                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
86                         2 => array('text' => _('Dimension')." 1",
87                             'from' => get_dimension_string($dimension), 'to' => ''),
88                         3 => array('text' => _('Dimension')." 2",
89                             'from' => get_dimension_string($dimension2), 'to' => ''));
90     }
91     else if ($dim == 1)
92     {
93         $params =   array(      0 => $comments,
94                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
95                         2 => array('text' => _('Dimension'),
96                             'from' => get_dimension_string($dimension), 'to' => ''));
97     }
98     else
99     {
100         $params =   array(      0 => $comments,
101                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
102     }
103
104         $rep = new FrontReport(_('Balance Sheet'), "BalanceSheet", user_pagesize());
105         $rep->Font();
106         $rep->Info($params, $cols, $headers, $aligns);
107         $rep->Header();
108         $classname = '';
109         $group = '';
110         $totalopen = 0.0;
111         $totalperiod = 0.0;
112         $totalclose = 0.0;
113         $classopen = 0.0;
114         $classperiod = 0.0;
115         $classclose = 0.0;
116         $assetsopen = 0.0;
117         $assetsperiod = 0.0;
118         $assetsclose = 0.0;
119         $closeclass = false;
120         $convert = 1;
121         $rep->NewLine();
122
123         $accounts = get_gl_accounts_all(1);
124
125         while ($account=db_fetch($accounts))
126         {
127                 $prev_balance = get_gl_balance_from_to("", $from, $account["account_code"], $dimension, $dimension2);
128
129                 $curr_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
130
131                 if (!$prev_balance && !$curr_balance)
132                         continue;
133                 if ($account['AccountClassName'] != $classname)
134                 {
135                         if ($classname != '')
136                         {
137                                 $closeclass = true;
138                         }
139                 }
140
141                 if ($account['AccountTypeName'] != $group)
142                 {
143                         if ($group != '')
144                         {
145                                 $rep->row += 6;
146                                 $rep->Line($rep->row);
147                                 $rep->NewLine();
148                                 $rep->TextCol(0, 2,     _('Total') . " " . $group);
149                                 $rep->AmountCol(2, 3, $totalopen * $convert, $dec);
150                                 $rep->AmountCol(3, 4, $totalperiod * $convert, $dec);
151                                 $rep->AmountCol(4, 5, $totalclose * $convert, $dec);
152                                 if ($graphics)
153                                 {
154                                         $pg->x[] = $group;
155                                         $pg->y[] = abs($totalclose);
156                                 }
157                                 $totalopen = $totalperiod = $totalclose = 0.0;
158                                 $rep->NewLine();
159                                 if ($closeclass)
160                                 {
161                                         $rep->row += 6;
162                                         $rep->Line($rep->row);
163                                         $rep->NewLine();
164                                         $rep->Font('bold');
165                                         $rep->TextCol(0, 2,     _('Total') . " " . $classname);
166                                         $rep->AmountCol(2, 3, $classopen * $convert, $dec);
167                                         $rep->AmountCol(3, 4, $classperiod * $convert, $dec);
168                                         $rep->AmountCol(4, 5, $classclose * $convert, $dec);
169                                         $rep->Font();
170                                         $assetsopen += $classopen;
171                                         $assetsperiod += $classperiod;
172                                         $assetsclose += $classclose;
173                                         $classopen = $classperiod = $classclose = 0.0;
174                                         $rep->NewLine(2);
175                                         $closeclass = false;
176                                 }
177                         }
178                         if ($account['AccountClassName'] != $classname)
179                         {
180                                 $rep->Font('bold');
181                                 $rep->TextCol(0, 5, $account['AccountClassName']);
182                                 $rep->Font();
183                                 $rep->NewLine();
184                         }
185                         $group = $account['AccountTypeName'];
186                         if (get_sign_convert($account['account_type']))
187                                 $convert = -1;
188                         else
189                                 $convert = 1;
190                         $rep->row -= 4;
191                         $rep->TextCol(0, 5, $account['AccountTypeName']);
192                         $rep->row -= 4;
193                         $rep->Line($rep->row);
194                         $rep->NewLine();
195                 }
196                 $classname = $account['AccountClassName'];
197                 $totalopen += $prev_balance;
198                 $totalperiod += $curr_balance;
199                 $totalclose = $totalopen + $totalperiod;
200                 $classopen += $prev_balance;
201                 $classperiod += $curr_balance;
202                 $classclose = $classopen + $classperiod;
203                 $rep->TextCol(0, 1,     $account['account_code']);
204                 $rep->TextCol(1, 2,     $account['account_name']);
205
206                 $rep->AmountCol(2, 3, $prev_balance * $convert, $dec);
207                 $rep->AmountCol(3, 4, $curr_balance * $convert, $dec);
208                 $rep->AmountCol(4, 5, ($curr_balance + $prev_balance) * $convert, $dec);
209
210                 $rep->NewLine();
211
212                 if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
213                 {
214                         $rep->Line($rep->row - 2);
215                         $rep->Header();
216                 }
217         }
218         if ($account['AccountClassName'] != $classname)
219         {
220                 if ($classname != '')
221                 {
222                         $closeclass = true;
223                 }
224         }
225         if ($account['AccountTypeName'] != $group)
226         {
227                 if ($group != '')
228                 {
229                         $rep->row += 6;
230                         $rep->Line($rep->row);
231                         $rep->NewLine();
232                         $rep->TextCol(0, 2,     _('Total') . " " . $group);
233                         $rep->AmountCol(2, 3, $totalopen * $convert, $dec);
234                         $rep->AmountCol(3, 4, $totalperiod * $convert, $dec);
235                         $rep->AmountCol(4, 5, $totalclose * $convert, $dec);
236                         if ($graphics)
237                         {
238                                 $pg->x[] = $group;
239                                 $pg->y[] = abs($totalclose);
240                         }
241                         $rep->NewLine();
242                         if ($closeclass)
243                         {
244                                 $calculateopen = -$assetsopen - $classopen;
245                                 $calculateperiod = -$assetsperiod - $classperiod;
246                                 $calculateclose = -$assetsclose  - $classclose;
247                                 $rep->row += 6;
248                                 $rep->Line($rep->row);
249                                 $rep->NewLine();
250                                 $rep->TextCol(0, 2,     _('Calculated Return'));
251                                 $rep->AmountCol(2, 3, $calculateopen * $convert, $dec);
252                                 $rep->AmountCol(3, 4, $calculateperiod * $convert, $dec);
253                                 $rep->AmountCol(4, 5, $calculateclose * $convert, $dec);
254                                 if ($graphics)
255                                 {
256                                         $pg->x[] = _('Calculated Return');
257                                         $pg->y[] = abs($calculateclose);
258                                 }
259                                 $rep->NewLine(2);
260                                 $rep->Font('bold');
261                                 $rep->TextCol(0, 2,     _('Total') . " " . $classname);
262                                 $rep->AmountCol(2, 3, -$assetsopen * $convert, $dec);
263                                 $rep->AmountCol(3, 4, -$assetsperiod * $convert, $dec);
264                                 $rep->AmountCol(4, 5, -$assetsclose * $convert, $dec);
265                                 $rep->Font();
266                                 $rep->NewLine();
267                         }
268                 }
269         }
270         $rep->Line($rep->row);
271         if ($graphics)
272         {
273                 global $decseps, $graph_skin;
274                 $pg->title     = $rep->title;
275                 $pg->axis_x    = _("Group");
276                 $pg->axis_y    = _("Amount");
277                 $pg->graphic_1 = $to;
278                 $pg->type      = $graphics;
279                 $pg->skin      = $graph_skin;
280                 $pg->built_in  = false;
281                 $pg->fontfile  = $path_to_root . "/reporting/fonts/Vera.ttf";
282                 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
283                 $filename =  $comp_path.'/'.user_company()."/pdf_files/test.png";
284                 $pg->display($filename, true);
285                 $w = $pg->width / 1.5;
286                 $h = $pg->height / 1.5;
287                 $x = ($rep->pageWidth - $w) / 2;
288                 $rep->NewLine(2);
289                 if ($rep->row - $h < $rep->bottomMargin)
290                         $rep->Header();
291                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
292         }
293         $rep->End();
294 }
295
296 ?>