Update from usntable branch.
[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 = 'SA_GLANALYTIC';
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         $classopen = 0.0;
110         $classperiod = 0.0;
111         $classclose = 0.0;
112         $assetsopen = 0.0;
113         $assetsperiod = 0.0;
114         $assetsclose = 0.0;
115         $equityopen = 0.0;
116         $equityperiod = 0.0;
117         $equityclose = 0.0;
118         $lopen = 0.0;
119         $lperiod = 0.0;
120         $lclose = 0.0;
121         
122         $typeopen = array(0,0,0,0,0,0,0,0,0,0);
123         $typeperiod = array(0,0,0,0,0,0,0,0,0,0);
124         $typeclose = array(0,0,0,0,0,0,0,0,0,0);
125         $typename = array('','','','','','','','','','');
126         $closing = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
127         //$parent = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
128         $level = 0;
129         $last = -1;
130         
131         $closeclass = false;
132         $ctype = 0;
133         $convert = 1;
134         $rep->NewLine();
135
136         $accounts = get_gl_accounts_all(1);
137
138         while ($account=db_fetch($accounts))
139         {
140                 if ($account['account_code'] == null && $account['parent'] > 0)
141                         continue;
142                 if ($account['account_code'] != null)
143                 {
144                         $prev_balance = get_gl_balance_from_to("", $from, $account["account_code"], $dimension, $dimension2);
145
146                         $curr_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
147
148                         if (!$prev_balance && !$curr_balance)
149                                 continue;
150                 }
151                 if ($account['AccountClassName'] != $classname)
152                 {
153                         if ($classname != '')
154                         {
155                                 $closeclass = true;
156                         }
157                 }
158                 if ($account['AccountTypeName'] != $typename[$level])
159                 {
160                         //$rep->NewLine();
161                         //$rep->TextCol(0, 5,   "type = ".$account['AccountType'].", level = $level, closing[0]-[1]-[2]-[3] = ".$closing[0]." ".$closing[1]." ".$closing[2]." ".$closing[3]." type[parent] = ".$account['parent']." last = ".$last);
162                         //$rep->NewLine();
163                         if ($typename[$level] != '')
164                         {
165                                 for ( ; $level >= 0, $typename[$level] != ''; $level--) 
166                                 {
167                                         if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0)
168                                         {
169                                                 $rep->row += 6;
170                                                 $rep->Line($rep->row);
171                                                 $rep->NewLine();
172                                                 $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
173                                                 $rep->AmountCol(2, 3, $typeopen[$level] * $convert, $dec);
174                                                 $rep->AmountCol(3, 4, $typeperiod[$level] * $convert, $dec);
175                                                 $rep->AmountCol(4, 5, $typeclose[$level] * $convert, $dec);
176                                                 if ($graphics)
177                                                 {
178                                                         $pg->x[] = $typename[$level];
179                                                         $pg->y[] = abs($typeclose[$level]);
180                                                 }
181                                                 $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
182                                         }       
183                                         else
184                                                 break;
185                                         $rep->NewLine();
186                                 }
187                                 //$rep->NewLine();
188                                 if ($closeclass)
189                                 {
190                                         $rep->row += 6;
191                                         $rep->Line($rep->row);
192                                         $rep->NewLine();
193                                         $rep->Font('bold');
194                                         $rep->TextCol(0, 2,     _('Total') . " " . $classname);
195                                         $rep->AmountCol(2, 3, $classopen * $convert, $dec);
196                                         $rep->AmountCol(3, 4, $classperiod * $convert, $dec);
197                                         $rep->AmountCol(4, 5, $classclose * $convert, $dec);
198                                         $rep->Font();
199                                         if ($ctype == CL_EQUITY)
200                                         {
201                                                 $equityopen += $classopen;
202                                                 $equityperiod += $classperiod;
203                                                 $equityclose += $classclose;
204                                         }
205                                         if ($ctype == CL_LIABILITIES)
206                                         {
207                                                 $lopen += $classopen;
208                                                 $lperiod += $classperiod;
209                                                 $lclose += $classclose;
210                                         }
211                                         $assetsopen += $classopen;
212                                         $assetsperiod += $classperiod;
213                                         $assetsclose += $classclose;
214                                         $classopen = $classperiod = $classclose = 0.0;
215                                         $rep->NewLine(2);
216                                         $closeclass = false;
217                                 }
218                         }
219                         if ($account['AccountClassName'] != $classname)
220                         {
221                                 $rep->Font('bold');
222                                 $rep->TextCol(0, 5, $account['AccountClassName']);
223                                 $rep->Font();
224                                 $rep->NewLine();
225                         }
226                         $level++;
227                         if ($account['parent'] != $last)
228                                 $last = $account['parent'];
229                         $typename[$level] = $account['AccountTypeName'];
230                         $closing[$level] = $account['parent'];
231                         $rep->row -= 4;
232                         $rep->TextCol(0, 5, $account['AccountTypeName']);
233                         $rep->row -= 4;
234                         $rep->Line($rep->row);
235                         $rep->NewLine();
236                 }
237                 $classname = $account['AccountClassName'];
238                 $ctype = $account['ClassType'];
239                 $convert = get_class_type_convert($ctype); 
240
241                 if ($account['account_code'] != null)
242                 {
243                         for ($i = 0; $i <= $level; $i++)
244                         {
245                                 $typeopen[$i] += $prev_balance;
246                                 $typeperiod[$i] += $curr_balance;
247                                 $typeclose[$i] = $typeopen[$i] + $typeperiod[$i];
248                         }
249                         $classopen += $prev_balance;
250                         $classperiod += $curr_balance;
251                         $classclose = $classopen + $classperiod;
252                         $rep->TextCol(0, 1,     $account['account_code']);
253                         $rep->TextCol(1, 2,     $account['account_name']);
254
255                         $rep->AmountCol(2, 3, $prev_balance * $convert, $dec);
256                         $rep->AmountCol(3, 4, $curr_balance * $convert, $dec);
257                         $rep->AmountCol(4, 5, ($curr_balance + $prev_balance) * $convert, $dec);
258
259                         $rep->NewLine();
260
261                         if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
262                         {
263                                 $rep->Line($rep->row - 2);
264                                 $rep->Header();
265                         }
266                 }       
267         }
268         if ($account['AccountClassName'] != $classname)
269         {
270                 if ($classname != '')
271                 {
272                         $closeclass = true;
273                 }
274         }
275         if ($account['AccountTypeName'] != $typename[$level])
276         {
277                 //$rep->NewLine();
278                 //$rep->TextCol(0, 5,   "type = ".$account['AccountType'].", level = $level, closing[0]-[1]-[2]-[3] = ".$closing[0]." ".$closing[1]." ".$closing[2]." ".$closing[3]." type[parent] = ".$account['parent']." last = ".$last);
279                 //$rep->NewLine();
280                 if ($typename[$level] != '')
281                 {
282                         for ( ; $level >= 0, $typename[$level] != ''; $level--) 
283                         {
284                                 if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0)
285                                 {
286                                         $rep->row += 6;
287                                         $rep->Line($rep->row);
288                                         $rep->NewLine();
289                                         $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
290                                         $rep->AmountCol(2, 3, $typeopen[$level] * $convert, $dec);
291                                         $rep->AmountCol(3, 4, $typeperiod[$level] * $convert, $dec);
292                                         $rep->AmountCol(4, 5, $typeclose[$level] * $convert, $dec);
293                                         if ($graphics)
294                                         {
295                                                 $pg->x[] = $typename[$level];
296                                                 $pg->y[] = abs($typeclose[$level]);
297                                         }
298                                         $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
299                                 }
300                                 else
301                                         break;
302                                 $rep->NewLine();
303                         }
304                         //$rep->NewLine();
305                         if ($closeclass)
306                         {
307                                 $calculateopen = -$assetsopen - $classopen;
308                                 $calculateperiod = -$assetsperiod - $classperiod;
309                                 $calculateclose = -$assetsclose  - $classclose;
310                                 if ($ctype == CL_EQUITY)
311                                 {
312                                         $equityopen += $classopen;
313                                         $equityperiod += $classperiod;
314                                         $equityclose += $classclose;
315                                 }
316                                 $rep->row += 6;
317                                 $rep->Line($rep->row);
318                                 $rep->NewLine();
319                                 $rep->TextCol(0, 2,     _('Calculated Return'));
320                                 $rep->AmountCol(2, 3, $calculateopen * $convert, $dec);
321                                 $rep->AmountCol(3, 4, $calculateperiod * $convert, $dec);
322                                 $rep->AmountCol(4, 5, $calculateclose * $convert, $dec);
323                                 if ($graphics)
324                                 {
325                                         $pg->x[] = _('Calculated Return');
326                                         $pg->y[] = abs($calculateclose);
327                                 }
328                                 $rep->NewLine(2);
329                                 $rep->Font('bold');
330                                 $rep->TextCol(0, 2,     _('Total') . " " . $classname);
331                                 $rep->AmountCol(2, 3, -$assetsopen * $convert, $dec);
332                                 $rep->AmountCol(3, 4, -$assetsperiod * $convert, $dec);
333                                 $rep->AmountCol(4, 5, -$assetsclose * $convert, $dec);
334                                 $rep->Font();
335                                 $rep->NewLine();
336                                 if ($equityopen != 0.0 || $equityperiod != 0.0 || $equityclose != 0.0 ||
337                                         $lopen != 0.0 || $lperiod != 0.0 || $lclose != 0.0)
338                                 {
339                                         $rep->NewLine();
340                                         $rep->Font('bold');
341                                         $rep->TextCol(0, 2,     _('Total') . " " . _('Liabilities') . _(' and ') . _('Equities'));
342                                         $rep->AmountCol(2, 3, ($lopen + $equityopen + $calculateopen) * -1, $dec);
343                                         $rep->AmountCol(3, 4, ($lperiod + $equityperiod + $calculateperiod) * -1, $dec);
344                                         $rep->AmountCol(4, 5, ($lclose + $equityclose + $calculateclose) * -1, $dec);
345                                         $rep->Font();
346                                         $rep->NewLine();
347                                 }
348                         }
349                 }
350         }
351         $rep->Line($rep->row);
352         if ($graphics)
353         {
354                 global $decseps, $graph_skin;
355                 $pg->title     = $rep->title;
356                 $pg->axis_x    = _("Group");
357                 $pg->axis_y    = _("Amount");
358                 $pg->graphic_1 = $to;
359                 $pg->type      = $graphics;
360                 $pg->skin      = $graph_skin;
361                 $pg->built_in  = false;
362                 $pg->fontfile  = $path_to_root . "/reporting/fonts/Vera.ttf";
363                 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
364                 $filename =  $comp_path.'/'.user_company()."/pdf_files/test.png";
365                 $pg->display($filename, true);
366                 $w = $pg->width / 1.5;
367                 $h = $pg->height / 1.5;
368                 $x = ($rep->pageWidth - $w) / 2;
369                 $rep->NewLine(2);
370                 if ($rep->row - $h < $rep->bottomMargin)
371                         $rep->Header();
372                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
373         }
374         $rep->End();
375 }
376
377 ?>