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, $tags, $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 $orientation = $_POST['PARAM_9'];
170 $destination = $_POST['PARAM_10'];
174 $dimension = $_POST['PARAM_3'];
175 $tags = (isset($_POST['PARAM_4']) ? $_POST['PARAM_4'] : -1);
176 $decimals = $_POST['PARAM_5'];
177 $graphics = $_POST['PARAM_6'];
178 $comments = $_POST['PARAM_7'];
179 $orientation = $_POST['PARAM_8'];
180 $destination = $_POST['PARAM_9'];
184 $tags = (isset($_POST['PARAM_3']) ? $_POST['PARAM_3'] : -1);
185 $decimals = $_POST['PARAM_4'];
186 $graphics = $_POST['PARAM_5'];
187 $comments = $_POST['PARAM_6'];
188 $orientation = $_POST['PARAM_7'];
189 $destination = $_POST['PARAM_8'];
192 include_once($path_to_root . "/reporting/includes/excel_report.inc");
194 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
195 $orientation = ($orientation ? 'L' : 'P');
198 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
204 $dec = user_price_dec();
205 $pdec = user_percent_dec();
207 $cols = array(0, 50, 200, 350, 425, 500);
208 //------------0--1---2----3----4----5--
210 $headers = array(_('Account'), _('Account Name'), _('Period'), _('Accumulated'), _('Achieved %'));
212 $aligns = array('left', 'left', 'right', 'right', 'right');
216 $params = array( 0 => $comments,
217 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
218 2 => array('text' => _('Dimension')." 1",
219 'from' => get_dimension_string($dimension), 'to' => ''),
220 3 => array('text' => _('Dimension')." 2",
221 'from' => get_dimension_string($dimension2), 'to' => ''),
222 4 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''));
226 $params = array( 0 => $comments,
227 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
228 2 => array('text' => _('Dimension'),
229 'from' => get_dimension_string($dimension), 'to' => ''),
230 3 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''));
234 $params = array( 0 => $comments,
235 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
236 2 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''));
240 if ($compare == 0 || $compare == 2)
246 $headers[3] = _('Budget');
249 $begin = begin_fiscalyear();
251 elseif ($compare == 1)
253 $begin = add_months($from, -12);
254 $end = add_months($to, -12);
255 $headers[3] = _('Period Y-1');
258 $rep = new FrontReport(_('Profit and Loss Statement'), "ProfitAndLoss", user_pagesize(), 9, $orientation);
259 if ($orientation == 'L')
260 recalculate_cols($cols);
263 $rep->Info($params, $cols, $headers, $aligns);
269 $classresult = get_account_classes(false, 0);
270 while ($class = db_fetch($classresult))
272 $class_per_total = 0;
273 $class_acc_total = 0;
274 $convert = get_class_type_convert($class["ctype"]);
278 $rep->TextCol(0, 5, $class["class_name"]);
282 //Get Account groups/types under this group/type with no parents
283 $typeresult = get_account_types(false, $class['cid'], -1);
284 while ($accounttype=db_fetch($typeresult))
286 $classtotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert, $dec,
287 $pdec, $rep, $dimension, $dimension2, $tags, $pg, $graphics);
288 $class_per_total += $classtotal[0];
289 $class_acc_total += $classtotal[1];
292 //Print Class Summary
294 $rep->Line($rep->row);
297 $rep->TextCol(0, 2, _('Total') . " " . $class["class_name"]);
298 $rep->AmountCol(2, 3, $class_per_total * $convert, $dec);
299 $rep->AmountCol(3, 4, $class_acc_total * $convert, $dec);
300 $rep->AmountCol(4, 5, Achieve($class_per_total, $class_acc_total), $pdec);
304 $salesper += $class_per_total;
305 $salesacc += $class_acc_total;
309 $rep->TextCol(0, 2, _('Calculated Return'));
310 $rep->AmountCol(2, 3, $salesper *-1, $dec); // always convert
311 $rep->AmountCol(3, 4, $salesacc * -1, $dec);
312 $rep->AmountCol(4, 5, Achieve($salesper, $salesacc), $pdec);
315 $pg->x[] = _('Calculated Return');
316 $pg->y[] = abs($salesper);
317 $pg->z[] = abs($salesacc);
321 $rep->Line($rep->row);
324 global $decseps, $graph_skin;
325 $pg->title = $rep->title;
326 $pg->axis_x = _("Group");
327 $pg->axis_y = _("Amount");
328 $pg->graphic_1 = $headers[2];
329 $pg->graphic_2 = $headers[3];
330 $pg->type = $graphics;
331 $pg->skin = $graph_skin;
332 $pg->built_in = false;
333 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
334 $filename = company_path(). "/pdf_files/". uniqid("").".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);