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: Profit and Loss Statement
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_profit_and_loss_statement();
30 //----------------------------------------------------------------------------------------------------
32 function Achieve($d1, $d2)
34 if ($d1 == 0 && $d2 == 0)
38 $ret = ($d1 / $d2 * 100.0);
44 //----------------------------------------------------------------------------------------------------
46 function print_profit_and_loss_statement()
48 global $comp_path, $path_to_root;
50 $dim = get_company_pref('use_dimension');
51 $dimension = $dimension2 = 0;
53 $from = $_POST['PARAM_0'];
54 $to = $_POST['PARAM_1'];
55 $compare = $_POST['PARAM_2'];
58 $dimension = $_POST['PARAM_3'];
59 $dimension2 = $_POST['PARAM_4'];
60 $graphics = $_POST['PARAM_5'];
61 $comments = $_POST['PARAM_6'];
62 $destination = $_POST['PARAM_7'];
66 $dimension = $_POST['PARAM_3'];
67 $graphics = $_POST['PARAM_4'];
68 $comments = $_POST['PARAM_5'];
69 $destination = $_POST['PARAM_6'];
73 $graphics = $_POST['PARAM_3'];
74 $comments = $_POST['PARAM_4'];
75 $destination = $_POST['PARAM_5'];
78 include_once($path_to_root . "/reporting/includes/excel_report.inc");
80 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
83 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
87 $pdec = user_percent_dec();
89 $cols = array(0, 50, 200, 350, 425, 500);
90 //------------0--1---2----3----4----5--
92 $headers = array(_('Account'), _('Account Name'), _('Period'), _('Accumulated'), _('Achieved %'));
94 $aligns = array('left', 'left', 'right', 'right', 'right');
98 $params = array( 0 => $comments,
99 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
100 2 => array('text' => _('Dimension')." 1",
101 'from' => get_dimension_string($dimension), 'to' => ''),
102 3 => array('text' => _('Dimension')." 2",
103 'from' => get_dimension_string($dimension2), 'to' => ''));
107 $params = array( 0 => $comments,
108 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
109 2 => array('text' => _('Dimension'),
110 'from' => get_dimension_string($dimension), 'to' => ''));
114 $params = array( 0 => $comments,
115 1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
119 if ($compare == 0 || $compare == 2)
125 $headers[3] = _('Budget');
128 $begin = begin_fiscalyear();
130 elseif ($compare == 1)
132 $begin = add_months($from, -12);
133 $end = add_months($to, -12);
134 $headers[3] = _('Period Y-1');
137 $rep = new FrontReport(_('Profit and Loss Statement'), "ProfitAndLoss", user_pagesize());
140 $rep->Info($params, $cols, $headers, $aligns);
145 $typeper = array(0,0,0,0,0,0,0,0,0,0);
146 $typeacc = array(0,0,0,0,0,0,0,0,0,0);
147 $typename = array('','','','','','','','','','');
148 $closing = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
160 $accounts = get_gl_accounts_all(0);
162 while ($account=db_fetch($accounts))
164 if ($account['account_code'] == null && $account['parent'] > 0)
167 if ($account['account_code'] != null)
169 $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
172 $acc_balance = get_budget_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
174 $acc_balance = get_gl_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
175 if (!$per_balance && !$acc_balance)
179 if ($account['AccountClassName'] != $classname)
181 if ($classname != '')
187 if ($account['AccountTypeName'] != $typename[$level])
189 if ($typename[$level] != '')
191 for ( ; $level >= 0, $typename[$level] != ''; $level--)
193 if ($account['parent'] == $closing[$level] || $account['parent'] < $last)
196 $rep->Line($rep->row);
198 $rep->TextCol(0, 2, _('Total') . " " . $typename[$level]);
199 $rep->AmountCol(2, 3, $typeper[$level] * $convert, $dec);
200 $rep->AmountCol(3, 4, $typeacc[$level] * $convert, $dec);
201 $rep->AmountCol(4, 5, Achieve($typeper[$level], $typeacc[$level]), $pdec);
204 $pg->x[] = $typename[$level];
205 $pg->y[] = abs($typeper[$level]);
206 $pg->z[] = abs($typeacc[$level]);
208 $typeper[$level] = $typeacc[$level] = 0.0;
218 $rep->Line($rep->row);
221 $rep->TextCol(0, 2, _('Total') . " " . $classname);
222 $rep->AmountCol(2, 3, $classper * $convert, $dec);
223 $rep->AmountCol(3, 4, $classacc * $convert, $dec);
224 $rep->AmountCol(4, 5, Achieve($classper, $classacc), $pdec);
226 $salesper += $classper;
227 $salesacc += $classacc;
228 $classper = $classacc = 0.0;
233 if ($account['AccountClassName'] != $classname)
236 $rep->TextCol(0, 5, $account['AccountClassName']);
241 if ($account['parent'] != $last)
242 $last = $account['parent'];
243 $typename[$level] = $account['AccountTypeName'];
244 $closing[$level] = $account['parent'];
246 $rep->TextCol(0, 5, $account['AccountTypeName']);
248 $rep->Line($rep->row);
251 $convert = get_account_class_convert($account['ClassID']);
252 $classname = $account['AccountClassName'];
254 if ($account['account_code'] != null)
256 //$per_balance *= -1;
257 //$acc_balance *= -1;
259 for ($i = 0; $i <= $level; $i++)
261 $typeper[$i] += $per_balance;
262 $typeacc[$i] += $acc_balance;
264 $classper += $per_balance;
265 $classacc += $acc_balance;
266 $rep->TextCol(0, 1, $account['account_code']);
267 $rep->TextCol(1, 2, $account['account_name']);
269 $rep->AmountCol(2, 3, $per_balance * $convert, $dec);
270 $rep->AmountCol(3, 4, $acc_balance * $convert, $dec);
271 $rep->AmountCol(4, 5, Achieve($per_balance, $acc_balance), $pdec);
275 if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
277 $rep->Line($rep->row - 2);
282 if ($account['AccountClassName'] != $classname)
284 if ($classname != '')
289 if ($account['AccountTypeName'] != $typename[$level])
291 if ($typename[$level] != '')
293 for ( ; $level >= 0, $typename[$level] != ''; $level--)
295 if ($account['parent'] == $closing[$level] || $account['parent'] < $last)
298 $rep->Line($rep->row);
300 $rep->TextCol(0, 2, _('Total') . " " . $typename[$level]);
301 $rep->AmountCol(2, 3, $typeper[$level] * $convert, $dec);
302 $rep->AmountCol(3, 4, $typeacc[$level] * $convert, $dec);
303 $rep->AmountCol(4, 5, Achieve($typeper[$level], $typeacc[$level]), $pdec);
306 $pg->x[] = $typename[$level];
307 $pg->y[] = abs($typeper[$level]);
308 $pg->z[] = abs($typeacc[$level]);
310 $typeper[$level] = $typeacc[$level] = 0.0;
319 $rep->Line($rep->row + 6);
320 $calculateper = $salesper + $classper;
321 $calculateacc = $salesacc + $classacc;
323 $rep->Line($rep->row);
327 $rep->TextCol(0, 2, _('Total') . " " . $classname);
328 $rep->AmountCol(2, 3, $classper * $convert, $dec);
329 $rep->AmountCol(3, 4, $classacc * $convert, $dec);
330 $rep->AmountCol(4, 5, Achieve($classper, $classacc), $pdec);
333 $rep->TextCol(0, 2, _('Calculated Return'));
334 $rep->AmountCol(2, 3, $calculateper *-1, $dec); // always convert
335 $rep->AmountCol(3, 4, $calculateacc * -1, $dec);
336 $rep->AmountCol(4, 5, Achieve($calculateper, $calculateacc), $pdec);
339 $pg->x[] = _('Calculated Return');
340 $pg->y[] = abs($calculateper);
341 $pg->z[] = abs($calculateacc);
350 $rep->Line($rep->row);
353 global $decseps, $graph_skin;
354 $pg->title = $rep->title;
355 $pg->axis_x = _("Group");
356 $pg->axis_y = _("Amount");
357 $pg->graphic_1 = $headers[2];
358 $pg->graphic_2 = $headers[3];
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);