Changed $path_to_root and stamped copyright
[fa-stable.git] / reporting / rep707.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 2;
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Profit and Loss Statement
18 // ----------------------------------------------------------------
19 $path_to_root="..";
20
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
26 //----------------------------------------------------------------------------------------------------
27
28 // trial_inquiry_controls();
29 print_profit_and_loss_statement();
30
31 //----------------------------------------------------------------------------------------------------
32
33 function Achieve($d1, $d2)
34 {
35         if ($d1 == 0 && $d2 == 0)
36                 return 0;
37         elseif ($d2 == 0)
38                 return 999;
39         $ret = ($d1 / $d2 * 100.0);
40         if ($ret > 999)
41                 $ret = 999;
42         return $ret;
43 }
44
45 //----------------------------------------------------------------------------------------------------
46
47 function print_profit_and_loss_statement()
48 {
49         global $comp_path, $path_to_root;
50
51         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
52         $dim = get_company_pref('use_dimension');
53         $dimension = $dimension2 = 0;
54
55         $from = $_POST['PARAM_0'];
56         $to = $_POST['PARAM_1'];
57         $compare = $_POST['PARAM_2'];
58         if ($dim == 2)
59         {
60                 $dimension = $_POST['PARAM_3'];
61                 $dimension2 = $_POST['PARAM_4'];
62                 $graphics = $_POST['PARAM_5'];
63                 $comments = $_POST['PARAM_6'];
64         }
65         else if ($dim == 1)
66         {
67                 $dimension = $_POST['PARAM_3'];
68                 $graphics = $_POST['PARAM_4'];
69                 $comments = $_POST['PARAM_5'];
70         }
71         else
72         {
73                 $graphics = $_POST['PARAM_3'];
74                 $comments = $_POST['PARAM_4'];
75         }
76         if ($graphics)
77         {
78                 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
79                 $pg = new graph();
80         }
81         $dec = 0;
82         $pdec = user_percent_dec();
83
84         $cols = array(0, 50, 200, 350, 425,     500);
85         //------------0--1---2----3----4----5--
86
87         $headers = array(_('Account'), _('Account Name'), _('Period'), _('Accumulated'), _('Achieved %'));
88
89         $aligns = array('left', 'left', 'right', 'right', 'right');
90
91     if ($dim == 2)
92     {
93         $params =   array(      0 => $comments,
94                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
95                         2 => array('text' => _('Dimension')." 1",
96                             'from' => get_dimension_string($dimension), 'to' => ''),
97                         3 => array('text' => _('Dimension')." 2",
98                             'from' => get_dimension_string($dimension2), 'to' => ''));
99     }
100     else if ($dim == 1)
101     {
102         $params =   array(      0 => $comments,
103                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
104                         2 => array('text' => _('Dimension'),
105                             'from' => get_dimension_string($dimension), 'to' => ''));
106     }
107     else
108     {
109         $params =   array(      0 => $comments,
110                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
111     }
112
113
114         if ($compare == 0 || $compare == 2)
115         {
116                 $end = $to;
117                 if ($compare == 2)
118                 {
119                         $begin = $from;
120                         $headers[3] = _('Budget');
121                 }
122                 else
123                         $begin = begin_fiscalyear();
124         }
125         elseif ($compare == 1)
126         {
127                 $begin = add_months($from, -12);
128                 $end = add_months($to, -12);
129                 $headers[3] = _('Period Y-1');
130         }
131
132         $rep = new FrontReport(_('Profit and Loss Statement'), "ProfitAndLoss.pdf", user_pagesize());
133
134         $rep->Font();
135         $rep->Info($params, $cols, $headers, $aligns);
136         $rep->Header();
137
138         $classname = '';
139         $group = '';
140         $totalper = 0.0;
141         $totalacc = 0.0;
142         $classper = 0.0;
143         $classacc = 0.0;
144         $salesper = 0.0;
145         $salesacc = 0.0;
146
147         $accounts = get_gl_accounts_all(0);
148
149         while ($account=db_fetch($accounts))
150         {
151                 $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
152
153                 if ($compare == 2)
154                         $acc_balance = get_budget_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
155                 else
156                         $acc_balance = get_gl_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
157                 if (!$per_balance && !$acc_balance)
158                         continue;
159
160                 if ($account['AccountClassName'] != $classname)
161                 {
162                         if ($classname != '')
163                         {
164                                 $closeclass = true;
165                         }
166                 }
167
168                 if ($account['AccountTypeName'] != $group)
169                 {
170                         if ($group != '')
171                         {
172                                 $rep->Line($rep->row + 6);
173                                 $rep->row -= 6;
174                                 $rep->TextCol(0, 2,     _('Total') . " " . $group);
175                                 $rep->TextCol(2, 3,     number_format2($totalper, $dec));
176                                 $rep->TextCol(3, 4,     number_format2($totalacc, $dec));
177                                 $rep->TextCol(4, 5,     number_format2(Achieve($totalper, $totalacc), $pdec));
178                                 if ($graphics)
179                                 {
180                                         $pg->x[] = $group;
181                                         $pg->y[] = abs($totalper);
182                                         $pg->z[] = abs($totalacc);
183                                 }
184                                 $totalper = $totalacc = 0.0;
185                                 $rep->row -= ($rep->lineHeight + 4);
186                                 if ($closeclass)
187                                 {
188                                         $rep->Line($rep->row + 6);
189                                         $rep->row -= 6;
190                                         $rep->Font('bold');
191                                         $rep->TextCol(0, 2,     _('Total') . " " . $classname);
192                                         $rep->TextCol(2, 3,     number_format2($classper, $dec));
193                                         $rep->TextCol(3, 4,     number_format2($classacc, $dec));
194                                         $rep->TextCol(4, 5,     number_format2(Achieve($classper, $classacc), $pdec));
195                                         $rep->Font();
196                                         $salesper += $classper;
197                                         $salesacc += $classacc;
198                                         $classper = $classacc = 0.0;
199                                         $rep->NewLine(3);
200                                         $closeclass = false;
201                                 }
202                         }
203                         if ($account['AccountClassName'] != $classname)
204                         {
205                                 $rep->Font('bold');
206                                 $rep->TextCol(0, 5, $account['AccountClassName']);
207                                 $rep->Font();
208                                 $rep->row -= ($rep->lineHeight + 4);
209                         }
210                         $group = $account['AccountTypeName'];
211                         $rep->TextCol(0, 5, $account['AccountTypeName']);
212                         $rep->Line($rep->row - 4);
213                         $rep->row -= ($rep->lineHeight + 4);
214                 }
215                 $classname = $account['AccountClassName'];
216
217                 $per_balance *= -1;
218                 $acc_balance *= -1;
219                 $totalper += $per_balance;
220                 $totalacc += $acc_balance;
221                 $classper += $per_balance;
222                 $classacc += $acc_balance;
223                 $rep->TextCol(0, 1,     $account['account_code']);
224                 $rep->TextCol(1, 2,     $account['account_name']);
225
226                 $rep->TextCol(2, 3,     number_format2($per_balance, $dec));
227                 $rep->TextCol(3, 4,     number_format2($acc_balance, $dec));
228                 $rep->TextCol(4, 5,     number_format2(Achieve($per_balance, $acc_balance), $pdec));
229
230                 $rep->NewLine();
231
232                 if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
233                 {
234                         $rep->Line($rep->row - 2);
235                         $rep->Header();
236                 }
237         }
238         if ($account['AccountClassName'] != $classname)
239         {
240                 if ($classname != '')
241                 {
242                         $closeclass = true;
243                 }
244         }
245         if ($account['AccountTypeName'] != $group)
246         {
247                 if ($group != '')
248                 {
249                         $rep->Line($rep->row + 6);
250                         $rep->row -= 6;
251                         $rep->TextCol(0, 2,     _('Total') . " " . $group);
252                         $rep->TextCol(2, 3,     number_format2($totalper, $dec));
253                         $rep->TextCol(3, 4,     number_format2($totalacc, $dec));
254                         $rep->TextCol(4, 5,     number_format2(Achieve($totalper, $totalacc), $pdec));
255                         if ($graphics)
256                         {
257                                 $pg->x[] = $group;
258                                 $pg->y[] = abs($totalper);
259                                 $pg->z[] = abs($totalacc);
260                         }
261                         $rep->row -= ($rep->lineHeight + 4);
262                         if ($closeclass)
263                         {
264                                 $rep->Line($rep->row + 6);
265                                 $calculateper = $salesper + $classper;
266                                 $calculateacc = $salesacc + $classacc;
267                                 $rep->row -= 6;
268
269                                 $rep->Font('bold');
270                                 $rep->TextCol(0, 2,     _('Total') . " " . $classname);
271                                 $rep->TextCol(2, 3,     number_format2($classper, $dec));
272                                 $rep->TextCol(3, 4,     number_format2($classacc, $dec));
273                                 $rep->TextCol(4, 5,     number_format2(Achieve($classper, $classacc), $pdec));
274
275                                 $rep->row -= ($rep->lineHeight + 8);
276                                 $rep->TextCol(0, 2,     _('Calculated Return'));
277                                 $rep->TextCol(2, 3,     number_format2($calculateper, $dec));
278                                 $rep->TextCol(3, 4,     number_format2($calculateacc, $dec));
279                                 $rep->TextCol(4, 5,     number_format2(Achieve($calculateper, $calculateacc), $pdec));
280                                 if ($graphics)
281                                 {
282                                         $pg->x[] = _('Calculated Return');
283                                         $pg->y[] = abs($calculateper);
284                                         $pg->z[] = abs($calculateacc);
285                                 }
286
287                                 $rep->Font();
288
289                                 $rep->NewLine();
290                         }
291                 }
292         }
293         $rep->Line($rep->row);
294         if ($graphics)
295         {
296                 global $decseps, $graph_skin;
297                 $pg->title     = $rep->title;
298                 $pg->axis_x    = _("Group");
299                 $pg->axis_y    = _("Amount");
300                 $pg->graphic_1 = $headers[2];
301                 $pg->graphic_2 = $headers[3];
302                 $pg->type      = $graphics;
303                 $pg->skin      = $graph_skin;
304                 $pg->built_in  = false;
305                 $pg->fontfile  = $path_to_root . "/reporting/fonts/Vera.ttf";
306                 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
307                 $filename = $comp_path.'/'.user_company(). "/pdf_files/test.png";
308                 $pg->display($filename, true);
309                 $w = $pg->width / 1.5;
310                 $h = $pg->height / 1.5;
311                 $x = ($rep->pageWidth - $w) / 2;
312                 $rep->NewLine(2);
313                 if ($rep->row - $h < $rep->bottomMargin)
314                         $rep->Header();
315                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
316         }
317         $rep->End();
318 }
319
320 ?>