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