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 // ----------------------------------------------------------------
17 // Title: Balance Sheet
18 // ----------------------------------------------------------------
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");
26 //----------------------------------------------------------------------------------------------------
28 print_balance_sheet();
31 //----------------------------------------------------------------------------------------------------
33 function print_balance_sheet()
35 global $comp_path, $path_to_root;
37 $dim = get_company_pref('use_dimension');
38 $dimension = $dimension2 = 0;
40 $from = $_POST['PARAM_0'];
41 $to = $_POST['PARAM_1'];
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'];
52 $dimension = $_POST['PARAM_2'];
53 $graphics = $_POST['PARAM_3'];
54 $comments = $_POST['PARAM_4'];
55 $destination = $_POST['PARAM_5'];
59 $graphics = $_POST['PARAM_2'];
60 $comments = $_POST['PARAM_3'];
61 $destination = $_POST['PARAM_4'];
64 include_once($path_to_root . "/reporting/includes/excel_report.inc");
66 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
69 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
74 $cols = array(0, 50, 200, 350, 425, 500);
75 //------------0--1---2----3----4----5--
77 $headers = array(_('Account'), _('Account Name'), _('Open Balance'), _('Period'),
80 $aligns = array('left', 'left', 'right', 'right', 'right');
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' => ''));
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' => ''));
100 $params = array( 0 => $comments,
101 1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
104 $rep = new FrontReport(_('Balance Sheet'), "BalanceSheet", user_pagesize());
106 $rep->Info($params, $cols, $headers, $aligns);
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);
136 $accounts = get_gl_accounts_all(1);
138 while ($account=db_fetch($accounts))
140 if ($account['account_code'] == null && $account['parent'] > 0)
142 if ($account['account_code'] != null)
144 $prev_balance = get_gl_balance_from_to("", $from, $account["account_code"], $dimension, $dimension2);
146 $curr_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
148 if (!$prev_balance && !$curr_balance)
151 if ($account['AccountClassName'] != $classname)
153 if ($classname != '')
158 if ($account['AccountTypeName'] != $typename[$level])
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);
163 if ($typename[$level] != '')
165 for ( ; $level >= 0, $typename[$level] != ''; $level--)
167 if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0 || $closeclass)
170 $rep->Line($rep->row);
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);
178 $pg->x[] = $typename[$level];
179 $pg->y[] = abs($typeclose[$level]);
181 $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
191 $rep->Line($rep->row);
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);
199 if ($ctype == CL_EQUITY)
201 $equityopen += $classopen;
202 $equityperiod += $classperiod;
203 $equityclose += $classclose;
205 if ($ctype == CL_LIABILITIES)
207 $lopen += $classopen;
208 $lperiod += $classperiod;
209 $lclose += $classclose;
211 $assetsopen += $classopen;
212 $assetsperiod += $classperiod;
213 $assetsclose += $classclose;
214 $classopen = $classperiod = $classclose = 0.0;
219 if ($account['AccountClassName'] != $classname)
222 $rep->TextCol(0, 5, $account['AccountClassName']);
227 if ($account['parent'] != $last)
228 $last = $account['parent'];
229 $typename[$level] = $account['AccountTypeName'];
230 $closing[$level] = $account['parent'];
232 $rep->TextCol(0, 5, $account['AccountTypeName']);
234 $rep->Line($rep->row);
237 $classname = $account['AccountClassName'];
238 $ctype = $account['ClassType'];
239 $convert = get_class_type_convert($ctype);
241 if ($account['account_code'] != null)
243 for ($i = 0; $i <= $level; $i++)
245 $typeopen[$i] += $prev_balance;
246 $typeperiod[$i] += $curr_balance;
247 $typeclose[$i] = $typeopen[$i] + $typeperiod[$i];
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']);
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);
261 if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
263 $rep->Line($rep->row - 2);
268 if ($account['AccountClassName'] != $classname)
270 if ($classname != '')
275 if ($account['AccountTypeName'] != $typename[$level])
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);
280 if ($typename[$level] != '')
282 for ( ; $level >= 0, $typename[$level] != ''; $level--)
284 if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0 || $closeclass)
287 $rep->Line($rep->row);
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);
295 $pg->x[] = $typename[$level];
296 $pg->y[] = abs($typeclose[$level]);
298 $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
307 $calculateopen = -$assetsopen - $classopen;
308 $calculateperiod = -$assetsperiod - $classperiod;
309 $calculateclose = -$assetsclose - $classclose;
310 if ($ctype == CL_EQUITY)
312 $equityopen += $classopen;
313 $equityperiod += $classperiod;
314 $equityclose += $classclose;
317 $rep->Line($rep->row);
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);
325 $pg->x[] = _('Calculated Return');
326 $pg->y[] = abs($calculateclose);
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);
336 if ($equityopen != 0.0 || $equityperiod != 0.0 || $equityclose != 0.0 ||
337 $lopen != 0.0 || $lperiod != 0.0 || $lclose != 0.0)
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);
351 $rep->Line($rep->row);
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;
370 if ($rep->row - $h < $rep->bottomMargin)
372 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);