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