2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU Affero General Public License,
5 AGPL, as published by the Free Software Foundation, either version
6 3 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/agpl-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 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
52 $dim = get_company_pref('use_dimension');
53 $dimension = $dimension2 = 0;
55 $from = $_POST['PARAM_0'];
56 $to = $_POST['PARAM_1'];
57 $compare = $_POST['PARAM_2'];
60 $dimension = $_POST['PARAM_3'];
61 $dimension2 = $_POST['PARAM_4'];
62 $graphics = $_POST['PARAM_5'];
63 $comments = $_POST['PARAM_6'];
67 $dimension = $_POST['PARAM_3'];
68 $graphics = $_POST['PARAM_4'];
69 $comments = $_POST['PARAM_5'];
73 $graphics = $_POST['PARAM_3'];
74 $comments = $_POST['PARAM_4'];
78 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
82 $pdec = user_percent_dec();
84 $cols = array(0, 50, 200, 350, 425, 500);
85 //------------0--1---2----3----4----5--
87 $headers = array(_('Account'), _('Account Name'), _('Period'), _('Accumulated'), _('Achieved %'));
89 $aligns = array('left', 'left', 'right', 'right', 'right');
93 $params = array( 0 => $comments,
94 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
95 2 => array('text' => _('Dimension')." 1",
96 'from' => get_dimension_string($dimension), 'to' => ''),
97 3 => array('text' => _('Dimension')." 2",
98 'from' => get_dimension_string($dimension2), 'to' => ''));
102 $params = array( 0 => $comments,
103 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
104 2 => array('text' => _('Dimension'),
105 'from' => get_dimension_string($dimension), 'to' => ''));
109 $params = array( 0 => $comments,
110 1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
114 if ($compare == 0 || $compare == 2)
120 $headers[3] = _('Budget');
123 $begin = begin_fiscalyear();
125 elseif ($compare == 1)
127 $begin = add_months($from, -12);
128 $end = add_months($to, -12);
129 $headers[3] = _('Period Y-1');
132 $rep = new FrontReport(_('Profit and Loss Statement'), "ProfitAndLoss.pdf", user_pagesize());
135 $rep->Info($params, $cols, $headers, $aligns);
147 $accounts = get_gl_accounts_all(0);
149 while ($account=db_fetch($accounts))
151 $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
154 $acc_balance = get_budget_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
156 $acc_balance = get_gl_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
157 if (!$per_balance && !$acc_balance)
160 if ($account['AccountClassName'] != $classname)
162 if ($classname != '')
168 if ($account['AccountTypeName'] != $group)
172 $rep->Line($rep->row + 6);
174 $rep->TextCol(0, 2, _('Total') . " " . $group);
175 $rep->TextCol(2, 3, number_format2($totalper, $dec));
176 $rep->TextCol(3, 4, number_format2($totalacc, $dec));
177 $rep->TextCol(4, 5, number_format2(Achieve($totalper, $totalacc), $pdec));
181 $pg->y[] = abs($totalper);
182 $pg->z[] = abs($totalacc);
184 $totalper = $totalacc = 0.0;
185 $rep->row -= ($rep->lineHeight + 4);
188 $rep->Line($rep->row + 6);
191 $rep->TextCol(0, 2, _('Total') . " " . $classname);
192 $rep->TextCol(2, 3, number_format2($classper, $dec));
193 $rep->TextCol(3, 4, number_format2($classacc, $dec));
194 $rep->TextCol(4, 5, number_format2(Achieve($classper, $classacc), $pdec));
196 $salesper += $classper;
197 $salesacc += $classacc;
198 $classper = $classacc = 0.0;
203 if ($account['AccountClassName'] != $classname)
206 $rep->TextCol(0, 5, $account['AccountClassName']);
208 $rep->row -= ($rep->lineHeight + 4);
210 $group = $account['AccountTypeName'];
211 $rep->TextCol(0, 5, $account['AccountTypeName']);
212 $rep->Line($rep->row - 4);
213 $rep->row -= ($rep->lineHeight + 4);
215 $classname = $account['AccountClassName'];
219 $totalper += $per_balance;
220 $totalacc += $acc_balance;
221 $classper += $per_balance;
222 $classacc += $acc_balance;
223 $rep->TextCol(0, 1, $account['account_code']);
224 $rep->TextCol(1, 2, $account['account_name']);
226 $rep->TextCol(2, 3, number_format2($per_balance, $dec));
227 $rep->TextCol(3, 4, number_format2($acc_balance, $dec));
228 $rep->TextCol(4, 5, number_format2(Achieve($per_balance, $acc_balance), $pdec));
232 if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
234 $rep->Line($rep->row - 2);
238 if ($account['AccountClassName'] != $classname)
240 if ($classname != '')
245 if ($account['AccountTypeName'] != $group)
249 $rep->Line($rep->row + 6);
251 $rep->TextCol(0, 2, _('Total') . " " . $group);
252 $rep->TextCol(2, 3, number_format2($totalper, $dec));
253 $rep->TextCol(3, 4, number_format2($totalacc, $dec));
254 $rep->TextCol(4, 5, number_format2(Achieve($totalper, $totalacc), $pdec));
258 $pg->y[] = abs($totalper);
259 $pg->z[] = abs($totalacc);
261 $rep->row -= ($rep->lineHeight + 4);
264 $rep->Line($rep->row + 6);
265 $calculateper = $salesper + $classper;
266 $calculateacc = $salesacc + $classacc;
270 $rep->TextCol(0, 2, _('Total') . " " . $classname);
271 $rep->TextCol(2, 3, number_format2($classper, $dec));
272 $rep->TextCol(3, 4, number_format2($classacc, $dec));
273 $rep->TextCol(4, 5, number_format2(Achieve($classper, $classacc), $pdec));
275 $rep->row -= ($rep->lineHeight + 8);
276 $rep->TextCol(0, 2, _('Calculated Return'));
277 $rep->TextCol(2, 3, number_format2($calculateper, $dec));
278 $rep->TextCol(3, 4, number_format2($calculateacc, $dec));
279 $rep->TextCol(4, 5, number_format2(Achieve($calculateper, $calculateacc), $pdec));
282 $pg->x[] = _('Calculated Return');
283 $pg->y[] = abs($calculateper);
284 $pg->z[] = abs($calculateacc);
293 $rep->Line($rep->row);
296 global $decseps, $graph_skin;
297 $pg->title = $rep->title;
298 $pg->axis_x = _("Group");
299 $pg->axis_y = _("Amount");
300 $pg->graphic_1 = $headers[2];
301 $pg->graphic_2 = $headers[3];
302 $pg->type = $graphics;
303 $pg->skin = $graph_skin;
304 $pg->built_in = false;
305 $pg->fontfile = $path_to_root . "/reporting/fonts/Vera.ttf";
306 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
307 $filename = $comp_path.'/'.user_company(). "/pdf_files/test.png";
308 $pg->display($filename, true);
309 $w = $pg->width / 1.5;
310 $h = $pg->height / 1.5;
311 $x = ($rep->pageWidth - $w) / 2;
313 if ($rep->row - $h < $rep->bottomMargin)
315 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);