4 // ----------------------------------------------------------------
8 // Title: Profit and Loss Statement
9 // ----------------------------------------------------------------
12 include_once($path_to_root . "includes/session.inc");
13 include_once($path_to_root . "includes/date_functions.inc");
14 include_once($path_to_root . "includes/data_checks.inc");
15 include_once($path_to_root . "gl/includes/gl_db.inc");
17 //----------------------------------------------------------------------------------------------------
19 // trial_inquiry_controls();
20 print_profit_and_loss_statement();
22 //----------------------------------------------------------------------------------------------------
24 function Achieve($d1, $d2)
26 if ($d1 == 0 && $d2 == 0)
30 $ret = ($d1 / $d2 * 100.0);
36 //----------------------------------------------------------------------------------------------------
38 function print_profit_and_loss_statement()
42 include_once($path_to_root . "reporting/includes/pdf_report.inc");
43 $dim = get_company_pref('use_dimension');
44 $dimension = $dimension2 = 0;
46 $from = $_POST['PARAM_0'];
47 $to = $_POST['PARAM_1'];
48 $compare = $_POST['PARAM_2'];
51 $dimension = $_POST['PARAM_3'];
52 $dimension2 = $_POST['PARAM_4'];
53 $graphics = $_POST['PARAM_5'];
54 $comments = $_POST['PARAM_6'];
58 $dimension = $_POST['PARAM_3'];
59 $graphics = $_POST['PARAM_4'];
60 $comments = $_POST['PARAM_5'];
64 $graphics = $_POST['PARAM_3'];
65 $comments = $_POST['PARAM_4'];
69 include_once($path_to_root . "reporting/includes/class.graphic.inc");
73 $pdec = user_percent_dec();
75 $cols = array(0, 50, 200, 350, 425, 500);
76 //------------0--1---2----3----4----5--
78 $headers = array(_('Account'), _('Account Name'), _('Period'), _('Accumulated'), _('Achieved %'));
80 $aligns = array('left', 'left', 'right', 'right', 'right');
84 $params = array( 0 => $comments,
85 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
86 2 => array('text' => _('Dimension')." 1",
87 'from' => get_dimension_string($dimension), 'to' => ''),
88 3 => array('text' => _('Dimension')." 2",
89 'from' => get_dimension_string($dimension2), 'to' => ''));
93 $params = array( 0 => $comments,
94 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
95 2 => array('text' => _('Dimension'),
96 'from' => get_dimension_string($dimension), 'to' => ''));
100 $params = array( 0 => $comments,
101 1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
105 if ($compare == 0 || $compare == 2)
111 $headers[3] = _('Budget');
114 $begin = begin_fiscalyear();
116 elseif ($compare == 1)
118 $begin = add_months($from, -12);
119 $end = add_months($to, -12);
120 $headers[3] = _('Period Y-1');
123 $rep = new FrontReport(_('Profit and Loss Statement'), "ProfitAndLoss.pdf", user_pagesize());
126 $rep->Info($params, $cols, $headers, $aligns);
138 $accounts = get_gl_accounts_all(0);
140 while ($account=db_fetch($accounts))
142 $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
145 $acc_balance = get_budget_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
147 $acc_balance = get_gl_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
148 if (!$per_balance && !$acc_balance)
151 if ($account['AccountClassName'] != $classname)
153 if ($classname != '')
159 if ($account['AccountTypeName'] != $group)
163 $rep->Line($rep->row + 6);
165 $rep->TextCol(0, 2, _('Total') . " " . $group);
166 $rep->TextCol(2, 3, number_format2($totalper, $dec));
167 $rep->TextCol(3, 4, number_format2($totalacc, $dec));
168 $rep->TextCol(4, 5, number_format2(Achieve($totalper, $totalacc), $pdec));
172 $pg->y[] = abs($totalper);
173 $pg->z[] = abs($totalacc);
175 $totalper = $totalacc = 0.0;
176 $rep->row -= ($rep->lineHeight + 4);
179 $rep->Line($rep->row + 6);
182 $rep->TextCol(0, 2, _('Total') . " " . $classname);
183 $rep->TextCol(2, 3, number_format2($classper, $dec));
184 $rep->TextCol(3, 4, number_format2($classacc, $dec));
185 $rep->TextCol(4, 5, number_format2(Achieve($classper, $classacc), $pdec));
187 $salesper += $classper;
188 $salesacc += $classacc;
189 $classper = $classacc = 0.0;
194 if ($account['AccountClassName'] != $classname)
197 $rep->TextCol(0, 5, $account['AccountClassName']);
199 $rep->row -= ($rep->lineHeight + 4);
201 $group = $account['AccountTypeName'];
202 $rep->TextCol(0, 5, $account['AccountTypeName']);
203 $rep->Line($rep->row - 4);
204 $rep->row -= ($rep->lineHeight + 4);
206 $classname = $account['AccountClassName'];
210 $totalper += $per_balance;
211 $totalacc += $acc_balance;
212 $classper += $per_balance;
213 $classacc += $acc_balance;
214 $rep->TextCol(0, 1, $account['account_code']);
215 $rep->TextCol(1, 2, $account['account_name']);
217 $rep->TextCol(2, 3, number_format2($per_balance, $dec));
218 $rep->TextCol(3, 4, number_format2($acc_balance, $dec));
219 $rep->TextCol(4, 5, number_format2(Achieve($per_balance, $acc_balance), $pdec));
223 if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
225 $rep->Line($rep->row - 2);
229 if ($account['AccountClassName'] != $classname)
231 if ($classname != '')
236 if ($account['AccountTypeName'] != $group)
240 $rep->Line($rep->row + 6);
242 $rep->TextCol(0, 2, _('Total') . " " . $group);
243 $rep->TextCol(2, 3, number_format2($totalper, $dec));
244 $rep->TextCol(3, 4, number_format2($totalacc, $dec));
245 $rep->TextCol(4, 5, number_format2(Achieve($totalper, $totalacc), $pdec));
249 $pg->y[] = abs($totalper);
250 $pg->z[] = abs($totalacc);
252 $rep->row -= ($rep->lineHeight + 4);
255 $rep->Line($rep->row + 6);
256 $calculateper = $salesper + $classper;
257 $calculateacc = $salesacc + $classacc;
261 $rep->TextCol(0, 2, _('Total') . " " . $classname);
262 $rep->TextCol(2, 3, number_format2($classper, $dec));
263 $rep->TextCol(3, 4, number_format2($classacc, $dec));
264 $rep->TextCol(4, 5, number_format2(Achieve($classper, $classacc), $pdec));
266 $rep->row -= ($rep->lineHeight + 8);
267 $rep->TextCol(0, 2, _('Calculated Return'));
268 $rep->TextCol(2, 3, number_format2($calculateper, $dec));
269 $rep->TextCol(3, 4, number_format2($calculateacc, $dec));
270 $rep->TextCol(4, 5, number_format2(Achieve($calculateper, $calculateacc), $pdec));
273 $pg->x[] = _('Calculated Return');
274 $pg->y[] = abs($calculateper);
275 $pg->z[] = abs($calculateacc);
284 $rep->Line($rep->row);
287 global $decseps, $graph_skin;
288 $pg->title = $rep->title;
289 $pg->axis_x = _("Group");
290 $pg->axis_y = _("Amount");
291 $pg->graphic_1 = $headers[2];
292 $pg->graphic_2 = $headers[3];
293 $pg->type = $graphics;
294 $pg->skin = $graph_skin;
295 $pg->built_in = false;
296 $pg->fontfile = $path_to_root . "reporting/fonts/Vera.ttf";
297 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
298 $filename = $path_to_root . "reporting/pdf_files/test.png";
299 $pg->display($filename, true);
300 $w = $pg->width / 1.5;
301 $h = $pg->height / 1.5;
302 $x = ($rep->pageWidth - $w) / 2;
304 if ($rep->row - $h < $rep->bottomMargin)
306 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);