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 // trial_inquiry_controls();
29 print_profit_and_loss_statement();
31 //----------------------------------------------------------------------------------------------------
33 function Achieve($d1, $d2)
35 if ($d1 == 0 && $d2 == 0)
39 $ret = ($d1 / $d2 * 100.0);
45 //----------------------------------------------------------------------------------------------------
47 function print_profit_and_loss_statement()
49 global $comp_path, $path_to_root;
51 $dim = get_company_pref('use_dimension');
52 $dimension = $dimension2 = 0;
54 $from = $_POST['PARAM_0'];
55 $to = $_POST['PARAM_1'];
56 $compare = $_POST['PARAM_2'];
59 $dimension = $_POST['PARAM_3'];
60 $dimension2 = $_POST['PARAM_4'];
61 $graphics = $_POST['PARAM_5'];
62 $comments = $_POST['PARAM_6'];
63 $destination = $_POST['PARAM_7'];
67 $dimension = $_POST['PARAM_3'];
68 $graphics = $_POST['PARAM_4'];
69 $comments = $_POST['PARAM_5'];
70 $destination = $_POST['PARAM_6'];
74 $graphics = $_POST['PARAM_3'];
75 $comments = $_POST['PARAM_4'];
76 $destination = $_POST['PARAM_5'];
78 if (isset($destination) && $destination)
80 include_once($path_to_root . "/reporting/includes/excel_report.inc");
81 $filename = "ProfitAndLoss.xml";
85 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
86 $filename = "ProfitAndLoss.pdf";
90 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
94 $pdec = user_percent_dec();
96 $cols = array(0, 50, 200, 350, 425, 500);
97 //------------0--1---2----3----4----5--
99 $headers = array(_('Account'), _('Account Name'), _('Period'), _('Accumulated'), _('Achieved %'));
101 $aligns = array('left', 'left', 'right', 'right', 'right');
105 $params = array( 0 => $comments,
106 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
107 2 => array('text' => _('Dimension')." 1",
108 'from' => get_dimension_string($dimension), 'to' => ''),
109 3 => array('text' => _('Dimension')." 2",
110 'from' => get_dimension_string($dimension2), 'to' => ''));
114 $params = array( 0 => $comments,
115 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
116 2 => array('text' => _('Dimension'),
117 'from' => get_dimension_string($dimension), 'to' => ''));
121 $params = array( 0 => $comments,
122 1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
126 if ($compare == 0 || $compare == 2)
132 $headers[3] = _('Budget');
135 $begin = begin_fiscalyear();
137 elseif ($compare == 1)
139 $begin = add_months($from, -12);
140 $end = add_months($to, -12);
141 $headers[3] = _('Period Y-1');
144 $rep = new FrontReport(_('Profit and Loss Statement'), $filename, user_pagesize());
147 $rep->Info($params, $cols, $headers, $aligns);
159 $accounts = get_gl_accounts_all(0);
161 while ($account=db_fetch($accounts))
163 $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
166 $acc_balance = get_budget_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
168 $acc_balance = get_gl_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
169 if (!$per_balance && !$acc_balance)
172 if ($account['AccountClassName'] != $classname)
174 if ($classname != '')
180 if ($account['AccountTypeName'] != $group)
185 $rep->Line($rep->row);
187 $rep->TextCol(0, 2, _('Total') . " " . $group);
188 $rep->AmountCol(2, 3, $totalper, $dec);
189 $rep->AmountCol(3, 4, $totalacc, $dec);
190 $rep->AmountCol(4, 5, Achieve($totalper, $totalacc), $pdec);
194 $pg->y[] = abs($totalper);
195 $pg->z[] = abs($totalacc);
197 $totalper = $totalacc = 0.0;
202 $rep->Line($rep->row);
205 $rep->TextCol(0, 2, _('Total') . " " . $classname);
206 $rep->AmountCol(2, 3, $classper, $dec);
207 $rep->AmountCol(3, 4, $classacc, $dec);
208 $rep->AmountCol(4, 5, Achieve($classper, $classacc), $pdec);
210 $salesper += $classper;
211 $salesacc += $classacc;
212 $classper = $classacc = 0.0;
217 if ($account['AccountClassName'] != $classname)
220 $rep->TextCol(0, 5, $account['AccountClassName']);
224 $group = $account['AccountTypeName'];
226 $rep->TextCol(0, 5, $account['AccountTypeName']);
228 $rep->Line($rep->row);
231 $classname = $account['AccountClassName'];
235 $totalper += $per_balance;
236 $totalacc += $acc_balance;
237 $classper += $per_balance;
238 $classacc += $acc_balance;
239 $rep->TextCol(0, 1, $account['account_code']);
240 $rep->TextCol(1, 2, $account['account_name']);
242 $rep->AmountCol(2, 3, $per_balance, $dec);
243 $rep->AmountCol(3, 4, $acc_balance, $dec);
244 $rep->AmountCol(4, 5, Achieve($per_balance, $acc_balance), $pdec);
248 if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
250 $rep->Line($rep->row - 2);
254 if ($account['AccountClassName'] != $classname)
256 if ($classname != '')
261 if ($account['AccountTypeName'] != $group)
266 $rep->Line($rep->row);
268 $rep->TextCol(0, 2, _('Total') . " " . $group);
269 $rep->AmountCol(2, 3, $totalper, $dec);
270 $rep->AmountCol(3, 4, $totalacc, $dec);
271 $rep->AmountCol(4, 5, Achieve($totalper, $totalacc), $pdec);
275 $pg->y[] = abs($totalper);
276 $pg->z[] = abs($totalacc);
281 $rep->Line($rep->row + 6);
282 $calculateper = $salesper + $classper;
283 $calculateacc = $salesacc + $classacc;
285 $rep->Line($rep->row);
289 $rep->TextCol(0, 2, _('Total') . " " . $classname);
290 $rep->AmountCol(2, 3, $classper, $dec);
291 $rep->AmountCol(3, 4, $classacc, $dec);
292 $rep->AmountCol(4, 5, Achieve($classper, $classacc), $pdec);
295 $rep->TextCol(0, 2, _('Calculated Return'));
296 $rep->AmountCol(2, 3, $calculateper, $dec);
297 $rep->AmountCol(3, 4, $calculateacc, $dec);
298 $rep->AmountCol(4, 5, Achieve($calculateper, $calculateacc), $pdec);
301 $pg->x[] = _('Calculated Return');
302 $pg->y[] = abs($calculateper);
303 $pg->z[] = abs($calculateacc);
312 $rep->Line($rep->row);
315 global $decseps, $graph_skin;
316 $pg->title = $rep->title;
317 $pg->axis_x = _("Group");
318 $pg->axis_y = _("Amount");
319 $pg->graphic_1 = $headers[2];
320 $pg->graphic_2 = $headers[3];
321 $pg->type = $graphics;
322 $pg->skin = $graph_skin;
323 $pg->built_in = false;
324 $pg->fontfile = $path_to_root . "/reporting/fonts/Vera.ttf";
325 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
326 $filename = $comp_path.'/'.user_company(). "/pdf_files/test.png";
327 $pg->display($filename, true);
328 $w = $pg->width / 1.5;
329 $h = $pg->height / 1.5;
330 $x = ($rep->pageWidth - $w) / 2;
332 if ($rep->row - $h < $rep->bottomMargin)
334 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);