1b93637b44eca60d46d61cfe5cf2fe6faa1dde4d
[fa-stable.git] / reporting / rep707.php
1 <?php
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 = 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 print_profit_and_loss_statement();
29
30 //----------------------------------------------------------------------------------------------------
31
32 function Achieve($d1, $d2)
33 {
34         if ($d1 == 0 && $d2 == 0)
35                 return 0;
36         elseif ($d2 == 0)
37                 return 999;
38         $ret = ($d1 / $d2 * 100.0);
39         if ($ret > 999)
40                 $ret = 999;
41         return $ret;
42 }
43
44 //----------------------------------------------------------------------------------------------------
45
46 function print_profit_and_loss_statement()
47 {
48         global $comp_path, $path_to_root;
49
50         $dim = get_company_pref('use_dimension');
51         $dimension = $dimension2 = 0;
52
53         $from = $_POST['PARAM_0'];
54         $to = $_POST['PARAM_1'];
55         $compare = $_POST['PARAM_2'];
56         if ($dim == 2)
57         {
58                 $dimension = $_POST['PARAM_3'];
59                 $dimension2 = $_POST['PARAM_4'];
60                 $graphics = $_POST['PARAM_5'];
61                 $comments = $_POST['PARAM_6'];
62                 $destination = $_POST['PARAM_7'];
63         }
64         else if ($dim == 1)
65         {
66                 $dimension = $_POST['PARAM_3'];
67                 $graphics = $_POST['PARAM_4'];
68                 $comments = $_POST['PARAM_5'];
69                 $destination = $_POST['PARAM_6'];
70         }
71         else
72         {
73                 $graphics = $_POST['PARAM_3'];
74                 $comments = $_POST['PARAM_4'];
75                 $destination = $_POST['PARAM_5'];
76         }
77         if ($destination)
78                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
79         else
80                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
81         if ($graphics)
82         {
83                 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
84                 $pg = new graph();
85         }
86         $dec = 0;
87         $pdec = user_percent_dec();
88
89         $cols = array(0, 50, 200, 350, 425,     500);
90         //------------0--1---2----3----4----5--
91
92         $headers = array(_('Account'), _('Account Name'), _('Period'), _('Accumulated'), _('Achieved %'));
93
94         $aligns = array('left', 'left', 'right', 'right', 'right');
95
96     if ($dim == 2)
97     {
98         $params =   array(      0 => $comments,
99                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
100                         2 => array('text' => _('Dimension')." 1",
101                             'from' => get_dimension_string($dimension), 'to' => ''),
102                         3 => array('text' => _('Dimension')." 2",
103                             'from' => get_dimension_string($dimension2), 'to' => ''));
104     }
105     else if ($dim == 1)
106     {
107         $params =   array(      0 => $comments,
108                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
109                         2 => array('text' => _('Dimension'),
110                             'from' => get_dimension_string($dimension), 'to' => ''));
111     }
112     else
113     {
114         $params =   array(      0 => $comments,
115                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
116     }
117
118
119         if ($compare == 0 || $compare == 2)
120         {
121                 $end = $to;
122                 if ($compare == 2)
123                 {
124                         $begin = $from;
125                         $headers[3] = _('Budget');
126                 }
127                 else
128                         $begin = begin_fiscalyear();
129         }
130         elseif ($compare == 1)
131         {
132                 $begin = add_months($from, -12);
133                 $end = add_months($to, -12);
134                 $headers[3] = _('Period Y-1');
135         }
136
137         $rep = new FrontReport(_('Profit and Loss Statement'), "ProfitAndLoss", user_pagesize());
138
139         $rep->Font();
140         $rep->Info($params, $cols, $headers, $aligns);
141         $rep->Header();
142
143         $classname = '';
144         
145         $typeper = array(0,0,0,0,0,0,0,0,0,0);
146         $typeacc = array(0,0,0,0,0,0,0,0,0,0);
147         $typename = array('','','','','','','','','','');
148         $closing = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
149         $level = 0;
150
151         $classper = 0.0;
152         $classacc = 0.0;
153         $salesper = 0.0;
154         $salesacc = 0.0;
155         $last = -1;
156
157         $closeclass = false;
158         $convert = 1;
159         $ctype = 0;
160
161         $accounts = get_gl_accounts_all(0);
162
163         while ($account=db_fetch($accounts))
164         {
165                 if ($account['account_code'] == null && $account['parent'] > 0)
166                         continue;
167
168                 if ($account['account_code'] != null)
169                 {
170                         $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
171
172                         if ($compare == 2)
173                                 $acc_balance = get_budget_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
174                         else
175                                 $acc_balance = get_gl_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
176                         if (!$per_balance && !$acc_balance)
177                                 continue;
178                 }
179
180                 if ($account['AccountClassName'] != $classname)
181                 {
182                         if ($classname != '')
183                         {
184                                 $closeclass = true;
185                         }
186                 }
187
188                 if ($account['AccountTypeName'] != $typename[$level])
189                 {
190                         if ($typename[$level] != '')
191                         {
192                                 for ( ; $level >= 0, $typename[$level] != ''; $level--) 
193                                 {
194                                         if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0)
195                                         {
196                                                 $rep->row += 6;
197                                                 $rep->Line($rep->row);
198                                                 $rep->NewLine();
199                                                 $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
200                                                 $rep->AmountCol(2, 3, $typeper[$level] * $convert, $dec);
201                                                 $rep->AmountCol(3, 4, $typeacc[$level] * $convert, $dec);
202                                                 $rep->AmountCol(4, 5, Achieve($typeper[$level], $typeacc[$level]), $pdec);
203                                                 if ($graphics)
204                                                 {
205                                                         $pg->x[] = $typename[$level];
206                                                         $pg->y[] = abs($typeper[$level]);
207                                                         $pg->z[] = abs($typeacc[$level]);
208                                                 }
209                                                 $typeper[$level] = $typeacc[$level] = 0.0;
210                                         }
211                                         else
212                                                 break;
213                                         $rep->NewLine();
214                                 }
215                                 //$rep->NewLine();
216                                 if ($closeclass)
217                                 {
218                                         $rep->row += 6;
219                                         $rep->Line($rep->row);
220                                         $rep->NewLine();
221                                         $rep->Font('bold');
222                                         $rep->TextCol(0, 2,     _('Total') . " " . $classname);
223                                         $rep->AmountCol(2, 3, $classper * $convert, $dec);
224                                         $rep->AmountCol(3, 4, $classacc * $convert, $dec);
225                                         $rep->AmountCol(4, 5, Achieve($classper, $classacc), $pdec);
226                                         $rep->Font();
227                                         $salesper += $classper;
228                                         $salesacc += $classacc;
229                                         $classper = $classacc = 0.0;
230                                         $rep->NewLine(2);
231                                         $closeclass = false;
232                                 }
233                         }
234                         if ($account['AccountClassName'] != $classname)
235                         {
236                                 $rep->Font('bold');
237                                 $rep->TextCol(0, 5, $account['AccountClassName']);
238                                 $rep->Font();
239                                 $rep->NewLine();
240                         }
241                         $level++;
242                         if ($account['parent'] != $last)
243                                 $last = $account['parent'];
244                         $typename[$level] = $account['AccountTypeName'];
245                         $closing[$level] = $account['parent'];
246
247                         $rep->row -= 4;
248                         $rep->TextCol(0, 5, $account['AccountTypeName']);
249                         $rep->row -= 4;
250                         $rep->Line($rep->row);
251                         $rep->NewLine();
252                 }
253                 $classname = $account['AccountClassName'];
254                 $ctype = $account['ClassType'];
255                 $convert = get_class_type_convert($ctype); 
256                 
257                 if ($account['account_code'] != null)
258                 {
259                         //$per_balance *= -1;
260                         //$acc_balance *= -1;
261                 
262                         for ($i = 0; $i <= $level; $i++)
263                         {
264                                 $typeper[$i] += $per_balance;
265                                 $typeacc[$i] += $acc_balance;
266                         }
267                         $classper += $per_balance;
268                         $classacc += $acc_balance;
269                         $rep->TextCol(0, 1,     $account['account_code']);
270                         $rep->TextCol(1, 2,     $account['account_name']);
271
272                         $rep->AmountCol(2, 3, $per_balance * $convert, $dec);
273                         $rep->AmountCol(3, 4, $acc_balance * $convert, $dec);
274                         $rep->AmountCol(4, 5, Achieve($per_balance, $acc_balance), $pdec);
275
276                         $rep->NewLine();
277
278                         if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
279                         {
280                                 $rep->Line($rep->row - 2);
281                                 $rep->Header();
282                         }
283                 }       
284         }
285         if ($account['AccountClassName'] != $classname)
286         {
287                 if ($classname != '')
288                 {
289                         $closeclass = true;
290                 }
291         }
292         if ($account['AccountTypeName'] != $typename[$level])
293         {
294                 if ($typename[$level] != '')
295                 {
296                         for ( ; $level >= 0, $typename[$level] != ''; $level--) 
297                         {
298                                 if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0)
299                                 {
300                                         $rep->row += 6;
301                                         $rep->Line($rep->row);
302                                         $rep->NewLine();
303                                         $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
304                                         $rep->AmountCol(2, 3, $typeper[$level] * $convert, $dec);
305                                         $rep->AmountCol(3, 4, $typeacc[$level] * $convert, $dec);
306                                         $rep->AmountCol(4, 5, Achieve($typeper[$level], $typeacc[$level]), $pdec);
307                                         if ($graphics)
308                                         {
309                                                 $pg->x[] = $typename[$level];
310                                                 $pg->y[] = abs($typeper[$level]);
311                                                 $pg->z[] = abs($typeacc[$level]);
312                                         }
313                                         $typeper[$level] = $typeacc[$level] = 0.0;
314                                 }
315                                 else
316                                         break;
317                                 $rep->NewLine();
318                         }
319                         //$rep->NewLine();
320                         if ($closeclass)
321                         {
322                                 $rep->Line($rep->row + 6);
323                                 $calculateper = $salesper + $classper;
324                                 $calculateacc = $salesacc + $classacc;
325                                 $rep->row += 6;
326                                 $rep->Line($rep->row);
327                                 $rep->NewLine();
328
329                                 $rep->Font('bold');
330                                 $rep->TextCol(0, 2,     _('Total') . " " . $classname);
331                                 $rep->AmountCol(2, 3, $classper * $convert, $dec);
332                                 $rep->AmountCol(3, 4, $classacc * $convert, $dec);
333                                 $rep->AmountCol(4, 5, Achieve($classper, $classacc), $pdec);
334
335                                 $rep->NewLine(2);
336                                 $rep->TextCol(0, 2,     _('Calculated Return'));
337                                 $rep->AmountCol(2, 3, $calculateper *-1, $dec); // always convert
338                                 $rep->AmountCol(3, 4, $calculateacc * -1, $dec);
339                                 $rep->AmountCol(4, 5, Achieve($calculateper, $calculateacc), $pdec);
340                                 if ($graphics)
341                                 {
342                                         $pg->x[] = _('Calculated Return');
343                                         $pg->y[] = abs($calculateper);
344                                         $pg->z[] = abs($calculateacc);
345                                 }
346
347                                 $rep->Font();
348
349                                 $rep->NewLine();
350                         }
351                 }
352         }
353         $rep->Line($rep->row);
354         if ($graphics)
355         {
356                 global $decseps, $graph_skin;
357                 $pg->title     = $rep->title;
358                 $pg->axis_x    = _("Group");
359                 $pg->axis_y    = _("Amount");
360                 $pg->graphic_1 = $headers[2];
361                 $pg->graphic_2 = $headers[3];
362                 $pg->type      = $graphics;
363                 $pg->skin      = $graph_skin;
364                 $pg->built_in  = false;
365                 $pg->fontfile  = $path_to_root . "/reporting/fonts/Vera.ttf";
366                 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
367                 $filename = $comp_path.'/'.user_company(). "/pdf_files/test.png";
368                 $pg->display($filename, true);
369                 $w = $pg->width / 1.5;
370                 $h = $pg->height / 1.5;
371                 $x = ($rep->pageWidth - $w) / 2;
372                 $rep->NewLine(2);
373                 if ($rep->row - $h < $rep->bottomMargin)
374                         $rep->Header();
375                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
376         }
377         $rep->End();
378 }
379
380 ?>