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, $tags, &$pg, $graphics, &$labels, &$serie1, &$serie2)
31 $code_per_balance = 0;
32 $code_acc_balance = 0;
33 $per_balance_total = 0;
34 $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 if ($tags != -1 && is_array($tags) && $tags[0] != false)
45 if (!is_record_in_tags($tags, TAG_ACCOUNT, $account['account_code']))
48 $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
51 $acc_balance = get_budget_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
53 $acc_balance = get_gl_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
54 if (!$per_balance && !$acc_balance)
57 //Print Type Title if it has atleast one non-zero account
62 $rep->TextCol(0, 5, $typename);
64 $rep->Line($rep->row);
68 $rep->TextCol(0, 1, $account['account_code']);
69 $rep->TextCol(1, 2, $account['account_name']);
71 $rep->AmountCol(2, 3, $per_balance * $convert, $dec);
72 $rep->AmountCol(3, 4, $acc_balance * $convert, $dec);
73 $rep->AmountCol(4, 5, Achieve($per_balance, $acc_balance), $pdec);
77 if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
79 $rep->Line($rep->row - 2);
83 $code_per_balance += $per_balance;
84 $code_acc_balance += $acc_balance;
87 //Get Account groups/types under this group/type
88 $result = get_account_types(false, false, $type);
89 while ($accounttype=db_fetch($result))
91 //Print Type Title if has sub types and not previously printed
96 $rep->TextCol(0, 5, $typename);
98 $rep->Line($rep->row);
102 $totals_arr = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert, $dec,
103 $pdec, $rep, $dimension, $dimension2, $tags, $pg, $graphics, $labels, $serie1, $serie2);
104 $per_balance_total += $totals_arr[0];
105 $acc_balance_total += $totals_arr[1];
108 //Display Type Summary if total is != 0 OR head is printed (Needed in case of unused hierarchical COA)
109 if (($code_per_balance + $per_balance_total + $code_acc_balance + $acc_balance_total) != 0 || $printtitle)
112 $rep->Line($rep->row);
114 $rep->TextCol(0, 2, _('Total') . " " . $typename);
115 $rep->AmountCol(2, 3, ($code_per_balance + $per_balance_total) * $convert, $dec);
116 $rep->AmountCol(3, 4, ($code_acc_balance + $acc_balance_total) * $convert, $dec);
117 $rep->AmountCol(4, 5, Achieve(($code_per_balance + $per_balance_total), ($code_acc_balance + $acc_balance_total)), $pdec);
120 $labels[] = $typename;
121 $serie1[] = abs($code_per_balance + $per_balance_total);
122 $serie2[] = abs($code_acc_balance + $acc_balance_total);
127 $totals_arr[0] = $code_per_balance + $per_balance_total;
128 $totals_arr[1] = $code_acc_balance + $acc_balance_total;
132 print_profit_and_loss_statement();
134 //----------------------------------------------------------------------------------------------------
136 function Achieve($d1, $d2)
138 if ($d1 == 0 && $d2 == 0)
142 $ret = ($d1 / $d2 * 100.0);
148 //----------------------------------------------------------------------------------------------------
150 function print_profit_and_loss_statement()
152 global $path_to_root, $SysPrefs;
154 $dim = get_company_pref('use_dimension');
155 $dimension = $dimension2 = 0;
157 $from = $_POST['PARAM_0'];
158 $to = $_POST['PARAM_1'];
159 $compare = $_POST['PARAM_2'];
162 $dimension = $_POST['PARAM_3'];
163 $dimension2 = $_POST['PARAM_4'];
164 $tags = (isset($_POST['PARAM_5']) ? $_POST['PARAM_5'] : -1);
165 $decimals = $_POST['PARAM_6'];
166 $graphics = $_POST['PARAM_7'];
167 $comments = $_POST['PARAM_8'];
168 $orientation = $_POST['PARAM_9'];
169 $destination = $_POST['PARAM_10'];
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 $orientation = $_POST['PARAM_8'];
179 $destination = $_POST['PARAM_9'];
183 $tags = (isset($_POST['PARAM_3']) ? $_POST['PARAM_3'] : -1);
184 $decimals = $_POST['PARAM_4'];
185 $graphics = $_POST['PARAM_5'];
186 $comments = $_POST['PARAM_6'];
187 $orientation = $_POST['PARAM_7'];
188 $destination = $_POST['PARAM_8'];
191 include_once($path_to_root . "/reporting/includes/excel_report.inc");
193 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
194 $orientation = ($orientation ? 'L' : 'P');
200 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
201 $pg = new Chart($graphics);
206 $dec = user_price_dec();
207 $pdec = user_percent_dec();
209 $cols = array(0, 60, 200, 350, 425, 500);
210 //------------0--1---2----3----4----5--
212 $headers = array(_('Account'), _('Account Name'), _('Period'), _('Accumulated'), _('Achieved %'));
214 $aligns = array('left', 'left', 'right', 'right', 'right');
218 $params = array( 0 => $comments,
219 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
220 2 => array('text' => _('Dimension')." 1",
221 'from' => get_dimension_string($dimension), 'to' => ''),
222 3 => array('text' => _('Dimension')." 2",
223 'from' => get_dimension_string($dimension2), 'to' => ''),
224 4 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''));
228 $params = array( 0 => $comments,
229 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
230 2 => array('text' => _('Dimension'),
231 'from' => get_dimension_string($dimension), 'to' => ''),
232 3 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''));
236 $params = array( 0 => $comments,
237 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
238 2 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''));
242 if ($compare == 0 || $compare == 2)
248 $headers[3] = _('Budget');
251 $begin = begin_fiscalyear();
253 elseif ($compare == 1)
255 $begin = add_months($from, -12);
256 $end = add_months($to, -12);
257 if (date_comp($to, end_month($to)) == 0) // compensate for leap years. If to-date equal end month
258 $end = end_month($end); // then the year-1 should also be end month
259 $headers[3] = _('Period Y-1');
262 $rep = new FrontReport(_('Profit and Loss Statement'), "ProfitAndLoss", user_pagesize(), 9, $orientation);
263 if ($orientation == 'L')
264 recalculate_cols($cols);
267 $rep->Info($params, $cols, $headers, $aligns);
273 $classresult = get_account_classes(false, 0);
274 while ($class = db_fetch($classresult))
276 $class_per_total = 0;
277 $class_acc_total = 0;
278 $convert = get_class_type_convert($class["ctype"]);
282 $rep->TextCol(0, 5, $class["class_name"]);
286 //Get Account groups/types under this group/type with no parents
287 $typeresult = get_account_types(false, $class['cid'], -1);
288 while ($accounttype=db_fetch($typeresult))
290 $classtotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert, $dec, $pdec, $rep, $dimension, $dimension2, $tags, $pg, $graphics, $labels, $serie1, $serie2);
291 $class_per_total += $classtotal[0];
292 $class_acc_total += $classtotal[1];
295 //Print Class Summary
297 $rep->Line($rep->row);
300 $rep->TextCol(0, 2, _('Total') . " " . $class["class_name"]);
301 $rep->AmountCol(2, 3, $class_per_total * $convert, $dec);
302 $rep->AmountCol(3, 4, $class_acc_total * $convert, $dec);
303 $rep->AmountCol(4, 5, Achieve($class_per_total, $class_acc_total), $pdec);
307 $salesper += $class_per_total;
308 $salesacc += $class_acc_total;
312 $rep->TextCol(0, 2, _('Calculated Return'));
313 $rep->AmountCol(2, 3, $salesper *-1, $dec); // always convert
314 $rep->AmountCol(3, 4, $salesacc * -1, $dec);
315 $rep->AmountCol(4, 5, Achieve($salesper, $salesacc), $pdec);
318 $rep->Line($rep->row);
321 $labels[] = _('Calculated Return');
322 $serie1[] = abs($salesper);
323 $serie2[] = abs($salesacc);
324 $pg->setStream('png');
325 $pg->setLabels($labels);
326 $pg->addSerie($headers[2], $serie1);
327 $pg->addSerie($headers[3], $serie2);
328 $pg->setTitle($rep->title);
329 $pg->setXTitle(_("Group"));
330 $pg->setYTitle(_("Amount"));
331 $pg->setDTitle(number_format2(abs($salesper)));
332 $pg->setValues(true);
333 $pg->latin_notation = ($SysPrefs->decseps[user_dec_sep()] != ".");
334 $filename = company_path(). "/pdf_files/". random_id().".png";
335 $pg->display($filename, true);
336 $w = $pg->width / 1.5;
337 $h = $pg->height / 1.5;
338 $x = ($rep->pageWidth - $w) / 2;
340 if ($rep->row - $h < $rep->bottomMargin)
342 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);