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 // ----------------------------------------------------------------
15 // Creator: Joe Hunt, Chaitanya for the recursive version 2009-02-05.
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 function display_type ($type, $typename, $from, $to, $begin, $end, $compare, $convert, &$dec, &$pdec, &$rep, $dimension, $dimension2, &$pg, $graphics)
30 $code_per_balance = 0;
31 $code_acc_balance = 0;
32 $per_balance_total = 0;
33 $acc_balance_total = 0;
35 $totals_arr = array();
37 $printtitle = 0; //Flag for printing type name
39 //Get Accounts directly under this group/type
40 $result = get_gl_accounts(null, null, $type);
41 while ($account=db_fetch($result))
43 $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
46 $acc_balance = get_budget_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
48 $acc_balance = get_gl_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
49 if (!$per_balance && !$acc_balance)
52 //Print Type Title if it has atleast one non-zero account
57 $rep->TextCol(0, 5, $typename);
59 $rep->Line($rep->row);
63 $rep->TextCol(0, 1, $account['account_code']);
64 $rep->TextCol(1, 2, $account['account_name']);
66 $rep->AmountCol(2, 3, $per_balance * $convert, $dec);
67 $rep->AmountCol(3, 4, $acc_balance * $convert, $dec);
68 $rep->AmountCol(4, 5, Achieve($per_balance, $acc_balance), $pdec);
72 if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
74 $rep->Line($rep->row - 2);
78 $code_per_balance += $per_balance;
79 $code_acc_balance += $acc_balance;
82 //Get Account groups/types under this group/type
83 $result = get_account_types(false, false, $type);
84 while ($accounttype=db_fetch($result))
86 //Print Type Title if has sub types and not previously printed
91 $rep->TextCol(0, 5, $typename);
93 $rep->Line($rep->row);
97 $totals_arr = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert, $dec,
98 $pdec, $rep, $dimension, $dimension2, $pg, $graphics);
99 $per_balance_total += $totals_arr[0];
100 $acc_balance_total += $totals_arr[1];
103 //Display Type Summary if total is != 0 OR head is printed (Needed in case of unused hierarchical COA)
104 if (($code_per_balance + $per_balance_total + $code_acc_balance + $acc_balance_total) != 0 || $printtitle)
107 $rep->Line($rep->row);
109 $rep->TextCol(0, 2, _('Total') . " " . $typename);
110 $rep->AmountCol(2, 3, ($code_per_balance + $per_balance_total) * $convert, $dec);
111 $rep->AmountCol(3, 4, ($code_acc_balance + $acc_balance_total) * $convert, $dec);
112 $rep->AmountCol(4, 5, Achieve(($code_per_balance + $per_balance_total), ($code_acc_balance + $acc_balance_total)), $pdec);
115 $pg->x[] = $typename;
116 $pg->y[] = abs($code_per_balance + $per_balance_total);
117 $pg->z[] = abs($code_acc_balance + $acc_balance_total);
122 $totals_arr[0] = $code_per_balance + $per_balance_total;
123 $totals_arr[1] = $code_acc_balance + $acc_balance_total;
127 print_profit_and_loss_statement();
129 //----------------------------------------------------------------------------------------------------
131 function Achieve($d1, $d2)
133 if ($d1 == 0 && $d2 == 0)
137 $ret = ($d1 / $d2 * 100.0);
143 //----------------------------------------------------------------------------------------------------
145 function print_profit_and_loss_statement()
147 global $comp_path, $path_to_root;
149 $dim = get_company_pref('use_dimension');
150 $dimension = $dimension2 = 0;
152 $from = $_POST['PARAM_0'];
153 $to = $_POST['PARAM_1'];
154 $compare = $_POST['PARAM_2'];
157 $dimension = $_POST['PARAM_3'];
158 $dimension2 = $_POST['PARAM_4'];
159 $graphics = $_POST['PARAM_5'];
160 $comments = $_POST['PARAM_6'];
161 $destination = $_POST['PARAM_7'];
165 $dimension = $_POST['PARAM_3'];
166 $graphics = $_POST['PARAM_4'];
167 $comments = $_POST['PARAM_5'];
168 $destination = $_POST['PARAM_6'];
172 $graphics = $_POST['PARAM_3'];
173 $comments = $_POST['PARAM_4'];
174 $destination = $_POST['PARAM_5'];
177 include_once($path_to_root . "/reporting/includes/excel_report.inc");
179 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
182 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
186 $pdec = user_percent_dec();
188 $cols = array(0, 50, 200, 350, 425, 500);
189 //------------0--1---2----3----4----5--
191 $headers = array(_('Account'), _('Account Name'), _('Period'), _('Accumulated'), _('Achieved %'));
193 $aligns = array('left', 'left', 'right', 'right', 'right');
197 $params = array( 0 => $comments,
198 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
199 2 => array('text' => _('Dimension')." 1",
200 'from' => get_dimension_string($dimension), 'to' => ''),
201 3 => array('text' => _('Dimension')." 2",
202 'from' => get_dimension_string($dimension2), 'to' => ''));
206 $params = array( 0 => $comments,
207 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
208 2 => array('text' => _('Dimension'),
209 'from' => get_dimension_string($dimension), 'to' => ''));
213 $params = array( 0 => $comments,
214 1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
218 if ($compare == 0 || $compare == 2)
224 $headers[3] = _('Budget');
227 $begin = begin_fiscalyear();
229 elseif ($compare == 1)
231 $begin = add_months($from, -12);
232 $end = add_months($to, -12);
233 $headers[3] = _('Period Y-1');
236 $rep = new FrontReport(_('Profit and Loss Statement'), "ProfitAndLoss", user_pagesize());
239 $rep->Info($params, $cols, $headers, $aligns);
247 $classresult = get_account_classes(false, 0);
248 while ($class = db_fetch($classresult))
250 $class_per_total = 0;
251 $class_acc_total = 0;
252 $convert = get_class_type_convert($class["ctype"]);
256 $rep->TextCol(0, 5, $class["class_name"]);
260 //Get Account groups/types under this group/type with no parents
261 $typeresult = get_account_types(false, $class['cid'], -1);
262 while ($accounttype=db_fetch($typeresult))
264 $classtotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert, $dec,
265 $pdec, $rep, $dimension, $dimension2, $pg, $graphics);
266 $class_per_total += $classtotal[0];
267 $class_acc_total += $classtotal[1];
270 //Print Class Summary
272 $rep->Line($rep->row);
275 $rep->TextCol(0, 2, _('Total') . " " . $class["class_name"]);
276 $rep->AmountCol(2, 3, $class_per_total * $convert, $dec);
277 $rep->AmountCol(3, 4, $class_acc_total * $convert, $dec);
278 $rep->AmountCol(4, 5, Achieve($class_per_total, $class_acc_total), $pdec);
282 $salesper += $class_per_total;
283 $salesacc += $class_acc_total;
287 $rep->TextCol(0, 2, _('Calculated Return'));
288 $rep->AmountCol(2, 3, $salesper *-1, $dec); // always convert
289 $rep->AmountCol(3, 4, $salesacc * -1, $dec);
290 $rep->AmountCol(4, 5, Achieve($salesper, $salesacc), $pdec);
293 $pg->x[] = _('Calculated Return');
294 $pg->y[] = abs($salesper);
295 $pg->z[] = abs($salesacc);
299 $rep->Line($rep->row);
302 global $decseps, $graph_skin;
303 $pg->title = $rep->title;
304 $pg->axis_x = _("Group");
305 $pg->axis_y = _("Amount");
306 $pg->graphic_1 = $headers[2];
307 $pg->graphic_2 = $headers[3];
308 $pg->type = $graphics;
309 $pg->skin = $graph_skin;
310 $pg->built_in = false;
311 $pg->fontfile = $path_to_root . "/reporting/fonts/Vera.ttf";
312 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
313 $filename = $comp_path.'/'.user_company(). "/pdf_files/test.png";
314 $pg->display($filename, true);
315 $w = $pg->width / 1.5;
316 $h = $pg->height / 1.5;
317 $x = ($rep->pageWidth - $w) / 2;
319 if ($rep->row - $h < $rep->bottomMargin)
321 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);