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 ***********************************************************************/
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);
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);
128 $accounts = get_gl_accounts_all(1);
130 while ($account=db_fetch($accounts))
132 if ($account['account_code'] == null && $account['parent'] > 0)
134 if ($account['account_code'] != null)
136 $prev_balance = get_gl_balance_from_to("", $from, $account["account_code"], $dimension, $dimension2);
138 $curr_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
140 if (!$prev_balance && !$curr_balance)
143 if ($account['AccountClassName'] != $classname)
145 if ($classname != '')
151 if ($account['AccountTypeName'] != $typename[$level])
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);
156 if ($typename[$level] != '')
158 for ( ; $level >= 0, $typename[$level] != ''; $level--)
160 if ($account['parent'] == $closing[$level] || $account['parent'] < $last)
163 $rep->Line($rep->row);
165 $rep->TextCol(0, 2, _('Total') . " " . $typename[$level]);
166 $rep->AmountCol(2, 3, $typeopen[$level], $dec);
167 $rep->AmountCol(3, 4, $typeperiod[$level], $dec);
168 $rep->AmountCol(4, 5, $typeclose[$level], $dec);
171 $pg->x[] = $$typename[$level];
172 $pg->y[] = abs($typeclose[$level]);
174 $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
184 $rep->Line($rep->row);
187 $rep->TextCol(0, 2, _('Total') . " " . $classname);
188 $rep->AmountCol(2, 3, $classopen, $dec);
189 $rep->AmountCol(3, 4, $classperiod, $dec);
190 $rep->AmountCol(4, 5, $classclose, $dec);
192 $assetsopen += $classopen;
193 $assetsperiod += $classperiod;
194 $assetsclose += $classclose;
195 $classopen = $classperiod = $classclose = 0.0;
200 if ($account['AccountClassName'] != $classname)
203 $rep->TextCol(0, 5, $account['AccountClassName']);
208 if ($account['parent'] != $last)
209 $last = $account['parent'];
210 $typename[$level] = $account['AccountTypeName'];
211 $closing[$level] = $account['parent'];
213 $rep->TextCol(0, 5, $account['AccountTypeName']);
215 $rep->Line($rep->row);
218 $classname = $account['AccountClassName'];
220 if ($account['account_code'] != null)
222 for ($i = 0; $i <= $level; $i++)
224 $typeopen[$i] += $prev_balance;
225 $typeperiod[$i] += $curr_balance;
226 $typeclose[$i] = $typeopen[$i] + $typeperiod[$i];
228 $classopen += $prev_balance;
229 $classperiod += $curr_balance;
230 $classclose = $classopen + $classperiod;
231 $rep->TextCol(0, 1, $account['account_code']);
232 $rep->TextCol(1, 2, $account['account_name']);
234 $rep->AmountCol(2, 3, $prev_balance, $dec);
235 $rep->AmountCol(3, 4, $curr_balance, $dec);
236 $rep->AmountCol(4, 5, $curr_balance + $prev_balance, $dec);
240 if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
242 $rep->Line($rep->row - 2);
247 if ($account['AccountClassName'] != $classname)
249 if ($classname != '')
254 if ($account['AccountTypeName'] != $typename[$level])
256 if ($typename[$level] != '')
258 for ( ; $level >= 0, $typename[$level] != ''; $level--)
260 if ($account['parent'] == $closing[$level] || $account['parent'] < $last)
263 $rep->Line($rep->row);
265 $rep->TextCol(0, 2, _('Total') . " " . $typename[$level]);
266 $rep->AmountCol(2, 3, $typeopen[$level], $dec);
267 $rep->AmountCol(3, 4, $typeperiod[$level], $dec);
268 $rep->AmountCol(4, 5, $typeclose[$level], $dec);
271 $pg->x[] = $$typename[$level];
272 $pg->y[] = abs($typeclose[$level]);
274 $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
283 $calculateopen = -$assetsopen - $classopen;
284 $calculateperiod = -$assetsperiod - $classperiod;
285 $calculateclose = -$assetsclose - $classclose;
287 $rep->Line($rep->row);
289 $rep->TextCol(0, 2, _('Calculated Return'));
290 $rep->AmountCol(2, 3, $calculateopen, $dec);
291 $rep->AmountCol(3, 4, $calculateperiod, $dec);
292 $rep->AmountCol(4, 5, $calculateclose, $dec);
295 $pg->x[] = _('Calculated Return');
296 $pg->y[] = abs($calculateclose);
300 $rep->TextCol(0, 2, _('Total') . " " . $classname);
301 $rep->AmountCol(2, 3, -$assetsopen, $dec);
302 $rep->AmountCol(3, 4, -$assetsperiod, $dec);
303 $rep->AmountCol(4, 5, -$assetsclose, $dec);
309 $rep->Line($rep->row);
312 global $decseps, $graph_skin;
313 $pg->title = $rep->title;
314 $pg->axis_x = _("Group");
315 $pg->axis_y = _("Amount");
316 $pg->graphic_1 = $to;
317 $pg->type = $graphics;
318 $pg->skin = $graph_skin;
319 $pg->built_in = false;
320 $pg->fontfile = $path_to_root . "/reporting/fonts/Vera.ttf";
321 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
322 $filename = $comp_path.'/'.user_company()."/pdf_files/test.png";
323 $pg->display($filename, true);
324 $w = $pg->width / 1.5;
325 $h = $pg->height / 1.5;
326 $x = ($rep->pageWidth - $w) / 2;
328 if ($rep->row - $h < $rep->bottomMargin)
330 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);