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 $decimals = $_POST['PARAM_5'];
160 $graphics = $_POST['PARAM_6'];
161 $comments = $_POST['PARAM_7'];
162 $destination = $_POST['PARAM_8'];
166 $dimension = $_POST['PARAM_3'];
167 $decimals = $_POST['PARAM_4'];
168 $graphics = $_POST['PARAM_5'];
169 $comments = $_POST['PARAM_6'];
170 $destination = $_POST['PARAM_7'];
174 $decimals = $_POST['PARAM_3'];
175 $graphics = $_POST['PARAM_4'];
176 $comments = $_POST['PARAM_5'];
177 $destination = $_POST['PARAM_6'];
180 include_once($path_to_root . "/reporting/includes/excel_report.inc");
182 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
185 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
191 $dec = user_price_dec();
192 $pdec = user_percent_dec();
194 $cols = array(0, 50, 200, 350, 425, 500);
195 //------------0--1---2----3----4----5--
197 $headers = array(_('Account'), _('Account Name'), _('Period'), _('Accumulated'), _('Achieved %'));
199 $aligns = array('left', 'left', 'right', 'right', 'right');
203 $params = array( 0 => $comments,
204 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
205 2 => array('text' => _('Dimension')." 1",
206 'from' => get_dimension_string($dimension), 'to' => ''),
207 3 => array('text' => _('Dimension')." 2",
208 'from' => get_dimension_string($dimension2), 'to' => ''));
212 $params = array( 0 => $comments,
213 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
214 2 => array('text' => _('Dimension'),
215 'from' => get_dimension_string($dimension), 'to' => ''));
219 $params = array( 0 => $comments,
220 1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
224 if ($compare == 0 || $compare == 2)
230 $headers[3] = _('Budget');
233 $begin = begin_fiscalyear();
235 elseif ($compare == 1)
237 $begin = add_months($from, -12);
238 $end = add_months($to, -12);
239 $headers[3] = _('Period Y-1');
242 $rep = new FrontReport(_('Profit and Loss Statement'), "ProfitAndLoss", user_pagesize());
245 $rep->Info($params, $cols, $headers, $aligns);
253 $classresult = get_account_classes(false, 0);
254 while ($class = db_fetch($classresult))
256 $class_per_total = 0;
257 $class_acc_total = 0;
258 $convert = get_class_type_convert($class["ctype"]);
262 $rep->TextCol(0, 5, $class["class_name"]);
266 //Get Account groups/types under this group/type with no parents
267 $typeresult = get_account_types(false, $class['cid'], -1);
268 while ($accounttype=db_fetch($typeresult))
270 $classtotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert, $dec,
271 $pdec, $rep, $dimension, $dimension2, $pg, $graphics);
272 $class_per_total += $classtotal[0];
273 $class_acc_total += $classtotal[1];
276 //Print Class Summary
278 $rep->Line($rep->row);
281 $rep->TextCol(0, 2, _('Total') . " " . $class["class_name"]);
282 $rep->AmountCol(2, 3, $class_per_total * $convert, $dec);
283 $rep->AmountCol(3, 4, $class_acc_total * $convert, $dec);
284 $rep->AmountCol(4, 5, Achieve($class_per_total, $class_acc_total), $pdec);
288 $salesper += $class_per_total;
289 $salesacc += $class_acc_total;
293 $rep->TextCol(0, 2, _('Calculated Return'));
294 $rep->AmountCol(2, 3, $salesper *-1, $dec); // always convert
295 $rep->AmountCol(3, 4, $salesacc * -1, $dec);
296 $rep->AmountCol(4, 5, Achieve($salesper, $salesacc), $pdec);
299 $pg->x[] = _('Calculated Return');
300 $pg->y[] = abs($salesper);
301 $pg->z[] = abs($salesacc);
305 $rep->Line($rep->row);
308 global $decseps, $graph_skin;
309 $pg->title = $rep->title;
310 $pg->axis_x = _("Group");
311 $pg->axis_y = _("Amount");
312 $pg->graphic_1 = $headers[2];
313 $pg->graphic_2 = $headers[3];
314 $pg->type = $graphics;
315 $pg->skin = $graph_skin;
316 $pg->built_in = false;
317 $pg->fontfile = $path_to_root . "/reporting/fonts/Vera.ttf";
318 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
319 $filename = $comp_path.'/'.user_company(). "/pdf_files/test.png";
320 $pg->display($filename, true);
321 $w = $pg->width / 1.5;
322 $h = $pg->height / 1.5;
323 $x = ($rep->pageWidth - $w) / 2;
325 if ($rep->row - $h < $rep->bottomMargin)
327 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);