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");
25 include_once($path_to_root . "/admin/db/tags_db.inc");
27 //----------------------------------------------------------------------------------------------------
29 function display_type ($type, $typename, $from, $to, $begin, $end, $compare, $convert, &$dec, &$pdec, &$rep, $dimension, $dimension2,
30 $tags, &$pg, $graphics)
32 $code_per_balance = 0;
33 $code_acc_balance = 0;
34 $per_balance_total = 0;
35 $acc_balance_total = 0;
36 $totals_arr = array();
38 $printtitle = 0; //Flag for printing type name
40 //Get Accounts directly under this group/type
41 $result = get_gl_accounts(null, null, $type);
42 while ($account=db_fetch($result))
44 if ($tags != -1 && is_array($tags) && $tags[0] != false)
46 if (!is_record_in_tags($tags, TAG_ACCOUNT, $account['account_code']))
49 $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
52 $acc_balance = get_budget_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
54 $acc_balance = get_gl_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
55 if (!$per_balance && !$acc_balance)
58 //Print Type Title if it has atleast one non-zero account
63 $rep->TextCol(0, 5, $typename);
65 $rep->Line($rep->row);
69 $rep->TextCol(0, 1, $account['account_code']);
70 $rep->TextCol(1, 2, $account['account_name']);
72 $rep->AmountCol(2, 3, $per_balance * $convert, $dec);
73 $rep->AmountCol(3, 4, $acc_balance * $convert, $dec);
74 $rep->AmountCol(4, 5, Achieve($per_balance, $acc_balance), $pdec);
78 if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
80 $rep->Line($rep->row - 2);
84 $code_per_balance += $per_balance;
85 $code_acc_balance += $acc_balance;
88 //Get Account groups/types under this group/type
89 $result = get_account_types(false, false, $type);
90 while ($accounttype=db_fetch($result))
92 //Print Type Title if has sub types and not previously printed
97 $rep->TextCol(0, 5, $typename);
99 $rep->Line($rep->row);
103 $totals_arr = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert, $dec,
104 $pdec, $rep, $dimension, $dimension2, $pg, $graphics);
105 $per_balance_total += $totals_arr[0];
106 $acc_balance_total += $totals_arr[1];
109 //Display Type Summary if total is != 0 OR head is printed (Needed in case of unused hierarchical COA)
110 if (($code_per_balance + $per_balance_total + $code_acc_balance + $acc_balance_total) != 0 || $printtitle)
113 $rep->Line($rep->row);
115 $rep->TextCol(0, 2, _('Total') . " " . $typename);
116 $rep->AmountCol(2, 3, ($code_per_balance + $per_balance_total) * $convert, $dec);
117 $rep->AmountCol(3, 4, ($code_acc_balance + $acc_balance_total) * $convert, $dec);
118 $rep->AmountCol(4, 5, Achieve(($code_per_balance + $per_balance_total), ($code_acc_balance + $acc_balance_total)), $pdec);
121 $pg->x[] = $typename;
122 $pg->y[] = abs($code_per_balance + $per_balance_total);
123 $pg->z[] = abs($code_acc_balance + $acc_balance_total);
128 $totals_arr[0] = $code_per_balance + $per_balance_total;
129 $totals_arr[1] = $code_acc_balance + $acc_balance_total;
133 print_profit_and_loss_statement();
135 //----------------------------------------------------------------------------------------------------
137 function Achieve($d1, $d2)
139 if ($d1 == 0 && $d2 == 0)
143 $ret = ($d1 / $d2 * 100.0);
149 //----------------------------------------------------------------------------------------------------
151 function print_profit_and_loss_statement()
153 global $path_to_root;
155 $dim = get_company_pref('use_dimension');
156 $dimension = $dimension2 = 0;
158 $from = $_POST['PARAM_0'];
159 $to = $_POST['PARAM_1'];
160 $compare = $_POST['PARAM_2'];
163 $dimension = $_POST['PARAM_3'];
164 $dimension2 = $_POST['PARAM_4'];
165 $tags = (isset($_POST['PARAM_5']) ? $_POST['PARAM_5'] : -1);
166 $decimals = $_POST['PARAM_6'];
167 $graphics = $_POST['PARAM_7'];
168 $comments = $_POST['PARAM_8'];
169 $destination = $_POST['PARAM_9'];
173 $dimension = $_POST['PARAM_3'];
174 $tags = (isset($_POST['PARAM_4']) ? $_POST['PARAM_4'] : -1);
175 $decimals = $_POST['PARAM_5'];
176 $graphics = $_POST['PARAM_6'];
177 $comments = $_POST['PARAM_7'];
178 $destination = $_POST['PARAM_8'];
182 $tags = (isset($_POST['PARAM_3']) ? $_POST['PARAM_3'] : -1);
183 $decimals = $_POST['PARAM_4'];
184 $graphics = $_POST['PARAM_5'];
185 $comments = $_POST['PARAM_6'];
186 $destination = $_POST['PARAM_7'];
189 include_once($path_to_root . "/reporting/includes/excel_report.inc");
191 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
194 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
200 $dec = user_price_dec();
201 $pdec = user_percent_dec();
203 $cols = array(0, 50, 200, 350, 425, 500);
204 //------------0--1---2----3----4----5--
206 $headers = array(_('Account'), _('Account Name'), _('Period'), _('Accumulated'), _('Achieved %'));
208 $aligns = array('left', 'left', 'right', 'right', 'right');
212 $params = array( 0 => $comments,
213 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
214 2 => array('text' => _('Dimension')." 1",
215 'from' => get_dimension_string($dimension), 'to' => ''),
216 3 => array('text' => _('Dimension')." 2",
217 'from' => get_dimension_string($dimension2), 'to' => ''),
218 4 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''));
222 $params = array( 0 => $comments,
223 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
224 2 => array('text' => _('Dimension'),
225 'from' => get_dimension_string($dimension), 'to' => ''),
226 3 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''));
230 $params = array( 0 => $comments,
231 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
232 2 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''));
236 if ($compare == 0 || $compare == 2)
242 $headers[3] = _('Budget');
245 $begin = begin_fiscalyear();
247 elseif ($compare == 1)
249 $begin = add_months($from, -12);
250 $end = add_months($to, -12);
251 $headers[3] = _('Period Y-1');
254 $rep = new FrontReport(_('Profit and Loss Statement'), "ProfitAndLoss", user_pagesize());
257 $rep->Info($params, $cols, $headers, $aligns);
263 $classresult = get_account_classes(false, 0);
264 while ($class = db_fetch($classresult))
266 $class_per_total = 0;
267 $class_acc_total = 0;
268 $convert = get_class_type_convert($class["ctype"]);
272 $rep->TextCol(0, 5, $class["class_name"]);
276 //Get Account groups/types under this group/type with no parents
277 $typeresult = get_account_types(false, $class['cid'], -1);
278 while ($accounttype=db_fetch($typeresult))
280 $classtotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert, $dec,
281 $pdec, $rep, $dimension, $dimension2, $tags, $pg, $graphics);
282 $class_per_total += $classtotal[0];
283 $class_acc_total += $classtotal[1];
286 //Print Class Summary
288 $rep->Line($rep->row);
291 $rep->TextCol(0, 2, _('Total') . " " . $class["class_name"]);
292 $rep->AmountCol(2, 3, $class_per_total * $convert, $dec);
293 $rep->AmountCol(3, 4, $class_acc_total * $convert, $dec);
294 $rep->AmountCol(4, 5, Achieve($class_per_total, $class_acc_total), $pdec);
298 $salesper += $class_per_total;
299 $salesacc += $class_acc_total;
303 $rep->TextCol(0, 2, _('Calculated Return'));
304 $rep->AmountCol(2, 3, $salesper *-1, $dec); // always convert
305 $rep->AmountCol(3, 4, $salesacc * -1, $dec);
306 $rep->AmountCol(4, 5, Achieve($salesper, $salesacc), $pdec);
309 $pg->x[] = _('Calculated Return');
310 $pg->y[] = abs($salesper);
311 $pg->z[] = abs($salesacc);
315 $rep->Line($rep->row);
318 global $decseps, $graph_skin;
319 $pg->title = $rep->title;
320 $pg->axis_x = _("Group");
321 $pg->axis_y = _("Amount");
322 $pg->graphic_1 = $headers[2];
323 $pg->graphic_2 = $headers[3];
324 $pg->type = $graphics;
325 $pg->skin = $graph_skin;
326 $pg->built_in = false;
327 $pg->fontfile = $path_to_root . "/reporting/fonts/Vera.ttf";
328 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
329 $filename = company_path(). "/pdf_files/test.png";
330 $pg->display($filename, true);
331 $w = $pg->width / 1.5;
332 $h = $pg->height / 1.5;
333 $x = ($rep->pageWidth - $w) / 2;
335 if ($rep->row - $h < $rep->bottomMargin)
337 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);