After heavy testing a new set of nested subtypes in COA, Monthly Breakdown, Balance...
[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         $accounts = get_gl_accounts_all(0);
158
159         while ($account=db_fetch($accounts))
160         {
161                 if ($account['account_code'] == null && $account['parent'] > 0)
162                         continue;
163
164                 if ($account['account_code'] != null)
165                 {
166                         $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
167
168                         if ($compare == 2)
169                                 $acc_balance = get_budget_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
170                         else
171                                 $acc_balance = get_gl_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
172                         if (!$per_balance && !$acc_balance)
173                                 continue;
174                 }
175
176                 if ($account['AccountClassName'] != $classname)
177                 {
178                         if ($classname != '')
179                         {
180                                 $closeclass = true;
181                         }
182                 }
183
184                 if ($account['AccountTypeName'] != $typename[$level])
185                 {
186                         if ($typename[$level] != '')
187                         {
188                                 for ( ; $level >= 0, $typename[$level] != ''; $level--) 
189                                 {
190                                         if ($account['parent'] == $closing[$level] || $account['parent'] < $last)
191                                         {
192                                                 $rep->row += 6;
193                                                 $rep->Line($rep->row);
194                                                 $rep->NewLine();
195                                                 $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
196                                                 $rep->AmountCol(2, 3, $typeper[$level], $dec);
197                                                 $rep->AmountCol(3, 4, $typeacc[$level], $dec);
198                                                 $rep->AmountCol(4, 5, Achieve($typeper[$level], $typeacc[$level]), $pdec);
199                                                 if ($graphics)
200                                                 {
201                                                         $pg->x[] = $typename[$level];
202                                                         $pg->y[] = abs($typeper[$level]);
203                                                         $pg->z[] = abs($typeacc[$level]);
204                                                 }
205                                                 $typeper[$level] = $typeacc[$level] = 0.0;
206                                         }
207                                         else
208                                                 break;
209                                         $rep->NewLine();
210                                 }
211                                 //$rep->NewLine();
212                                 if ($closeclass)
213                                 {
214                                         $rep->row += 6;
215                                         $rep->Line($rep->row);
216                                         $rep->NewLine();
217                                         $rep->Font('bold');
218                                         $rep->TextCol(0, 2,     _('Total') . " " . $classname);
219                                         $rep->AmountCol(2, 3, $classper, $dec);
220                                         $rep->AmountCol(3, 4, $classacc, $dec);
221                                         $rep->AmountCol(4, 5, Achieve($classper, $classacc), $pdec);
222                                         $rep->Font();
223                                         $salesper += $classper;
224                                         $salesacc += $classacc;
225                                         $classper = $classacc = 0.0;
226                                         $rep->NewLine(2);
227                                         $closeclass = false;
228                                 }
229                         }
230                         if ($account['AccountClassName'] != $classname)
231                         {
232                                 $rep->Font('bold');
233                                 $rep->TextCol(0, 5, $account['AccountClassName']);
234                                 $rep->Font();
235                                 $rep->NewLine();
236                         }
237                         $level++;
238                         if ($account['parent'] != $last)
239                                 $last = $account['parent'];
240                         $typename[$level] = $account['AccountTypeName'];
241                         $closing[$level] = $account['parent'];
242                         $rep->row -= 4;
243                         $rep->TextCol(0, 5, $account['AccountTypeName']);
244                         $rep->row -= 4;
245                         $rep->Line($rep->row);
246                         $rep->NewLine();
247                 }
248                 $classname = $account['AccountClassName'];
249
250                 if ($account['account_code'] != null)
251                 {
252                         $per_balance *= -1;
253                         $acc_balance *= -1;
254                 
255                         for ($i = 0; $i <= $level; $i++)
256                         {
257                                 $typeper[$i] += $per_balance;
258                                 $typeacc[$i] += $acc_balance;
259                         }
260                         $classper += $per_balance;
261                         $classacc += $acc_balance;
262                         $rep->TextCol(0, 1,     $account['account_code']);
263                         $rep->TextCol(1, 2,     $account['account_name']);
264
265                         $rep->AmountCol(2, 3, $per_balance, $dec);
266                         $rep->AmountCol(3, 4, $acc_balance, $dec);
267                         $rep->AmountCol(4, 5, Achieve($per_balance, $acc_balance), $pdec);
268
269                         $rep->NewLine();
270
271                         if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
272                         {
273                                 $rep->Line($rep->row - 2);
274                                 $rep->Header();
275                         }
276                 }       
277         }
278         if ($account['AccountClassName'] != $classname)
279         {
280                 if ($classname != '')
281                 {
282                         $closeclass = true;
283                 }
284         }
285         if ($account['AccountTypeName'] != $typename[$level])
286         {
287                 if ($typename[$level] != '')
288                 {
289                         for ( ; $level >= 0, $typename[$level] != ''; $level--) 
290                         {
291                                 if ($account['parent'] == $closing[$level] || $account['parent'] < $last)
292                                 {
293                                         $rep->row += 6;
294                                         $rep->Line($rep->row);
295                                         $rep->NewLine();
296                                         $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
297                                         $rep->AmountCol(2, 3, $typeper[$level], $dec);
298                                         $rep->AmountCol(3, 4, $typeacc[$level], $dec);
299                                         $rep->AmountCol(4, 5, Achieve($typeper[$level], $typeacc[$level]), $pdec);
300                                         if ($graphics)
301                                         {
302                                                 $pg->x[] = $typename[$level];
303                                                 $pg->y[] = abs($typeper[$level]);
304                                                 $pg->z[] = abs($typeacc[$level]);
305                                         }
306                                         $typeper[$level] = $typeacc[$level] = 0.0;
307                                 }
308                                 else
309                                         break;
310                                 $rep->NewLine();
311                         }
312                         //$rep->NewLine();
313                         if ($closeclass)
314                         {
315                                 $rep->Line($rep->row + 6);
316                                 $calculateper = $salesper + $classper;
317                                 $calculateacc = $salesacc + $classacc;
318                                 $rep->row += 6;
319                                 $rep->Line($rep->row);
320                                 $rep->NewLine();
321
322                                 $rep->Font('bold');
323                                 $rep->TextCol(0, 2,     _('Total') . " " . $classname);
324                                 $rep->AmountCol(2, 3, $classper, $dec);
325                                 $rep->AmountCol(3, 4, $classacc, $dec);
326                                 $rep->AmountCol(4, 5, Achieve($classper, $classacc), $pdec);
327
328                                 $rep->NewLine(2);
329                                 $rep->TextCol(0, 2,     _('Calculated Return'));
330                                 $rep->AmountCol(2, 3, $calculateper, $dec);
331                                 $rep->AmountCol(3, 4, $calculateacc, $dec);
332                                 $rep->AmountCol(4, 5, Achieve($calculateper, $calculateacc), $pdec);
333                                 if ($graphics)
334                                 {
335                                         $pg->x[] = _('Calculated Return');
336                                         $pg->y[] = abs($calculateper);
337                                         $pg->z[] = abs($calculateacc);
338                                 }
339
340                                 $rep->Font();
341
342                                 $rep->NewLine();
343                         }
344                 }
345         }
346         $rep->Line($rep->row);
347         if ($graphics)
348         {
349                 global $decseps, $graph_skin;
350                 $pg->title     = $rep->title;
351                 $pg->axis_x    = _("Group");
352                 $pg->axis_y    = _("Amount");
353                 $pg->graphic_1 = $headers[2];
354                 $pg->graphic_2 = $headers[3];
355                 $pg->type      = $graphics;
356                 $pg->skin      = $graph_skin;
357                 $pg->built_in  = false;
358                 $pg->fontfile  = $path_to_root . "/reporting/fonts/Vera.ttf";
359                 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
360                 $filename = $comp_path.'/'.user_company(). "/pdf_files/test.png";
361                 $pg->display($filename, true);
362                 $w = $pg->width / 1.5;
363                 $h = $pg->height / 1.5;
364                 $x = ($rep->pageWidth - $w) / 2;
365                 $rep->NewLine(2);
366                 if ($rep->row - $h < $rep->bottomMargin)
367                         $rep->Header();
368                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
369         }
370         $rep->End();
371 }
372
373 ?>