f490e08d4ee942287f1630402490b8c141723a50
[fa-stable.git] / reporting / rep706.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:       Balance Sheet
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_balance_sheet();
29
30
31 //----------------------------------------------------------------------------------------------------
32
33 function print_balance_sheet()
34 {
35         global $comp_path, $path_to_root;
36
37         $dim = get_company_pref('use_dimension');
38         $dimension = $dimension2 = 0;
39
40         $from = $_POST['PARAM_0'];
41         $to = $_POST['PARAM_1'];
42         if ($dim == 2)
43         {
44                 $dimension = $_POST['PARAM_2'];
45                 $dimension2 = $_POST['PARAM_3'];
46                 $graphics = $_POST['PARAM_4'];
47                 $comments = $_POST['PARAM_5'];
48                 $destination = $_POST['PARAM_6'];
49         }
50         else if ($dim == 1)
51         {
52                 $dimension = $_POST['PARAM_2'];
53                 $graphics = $_POST['PARAM_3'];
54                 $comments = $_POST['PARAM_4'];
55                 $destination = $_POST['PARAM_5'];
56         }
57         else
58         {
59                 $graphics = $_POST['PARAM_2'];
60                 $comments = $_POST['PARAM_3'];
61                 $destination = $_POST['PARAM_4'];
62         }
63         if ($destination)
64                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
65         else
66                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
67         if ($graphics)
68         {
69                 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
70                 $pg = new graph();
71         }
72         $dec = 0;
73
74         $cols = array(0, 50, 200, 350, 425,     500);
75         //------------0--1---2----3----4----5--
76
77         $headers = array(_('Account'), _('Account Name'), _('Open Balance'), _('Period'),
78                 _('Close Balance'));
79
80         $aligns = array('left', 'left', 'right', 'right', 'right');
81
82     if ($dim == 2)
83     {
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' => ''));
90     }
91     else if ($dim == 1)
92     {
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' => ''));
97     }
98     else
99     {
100         $params =   array(      0 => $comments,
101                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
102     }
103
104         $rep = new FrontReport(_('Balance Sheet'), "BalanceSheet", user_pagesize());
105         $rep->Font();
106         $rep->Info($params, $cols, $headers, $aligns);
107         $rep->Header();
108         $classname = '';
109         $classopen = 0.0;
110         $classperiod = 0.0;
111         $classclose = 0.0;
112         $assetsopen = 0.0;
113         $assetsperiod = 0.0;
114         $assetsclose = 0.0;
115         
116         $typeopen = array(0,0,0,0,0,0,0,0,0,0);
117         $typeperiod = array(0,0,0,0,0,0,0,0,0,0);
118         $typeclose = array(0,0,0,0,0,0,0,0,0,0);
119         $typename = array('','','','','','','','','','');
120         $parent = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
121         $level = 0;
122         
123         $closeclass = false;
124         $rep->NewLine();
125
126         $accounts = get_gl_accounts_all(1);
127
128         while ($account=db_fetch($accounts))
129         {
130                 $prev_balance = get_gl_balance_from_to("", $from, $account["account_code"], $dimension, $dimension2);
131
132                 $curr_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
133
134                 if (!$prev_balance && !$curr_balance)
135                         continue;
136
137                 if ($account['AccountClassName'] != $classname)
138                 {
139                         if ($classname != '')
140                         {
141                                 $closeclass = true;
142                         }
143                 }
144
145                 if ($account['AccountTypeName'] != $typename[$level])
146                 {
147                         if ($typename[$level] != '' && $account['parent'] == -1)
148                         {
149                                 for ( ; $level >= 0; $level--)
150                                 {
151                                         $rep->row += 6;
152                                         $rep->Line($rep->row);
153                                         $rep->NewLine();
154                                         $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
155                                         $rep->AmountCol(2, 3, $typeopen[$level], $dec);
156                                         $rep->AmountCol(3, 4, $typeperiod[$level], $dec);
157                                         $rep->AmountCol(4, 5, $typeclose[$level], $dec);
158                                         $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
159                                         if ($parent[$level] == -1)
160                                                 break;
161                                         $rep->NewLine();
162                                 }
163                                 if ($graphics)
164                                 {
165                                         $pg->x[] = $$typename[$level];
166                                         $pg->y[] = abs($typeclose[$level]);
167                                 }
168                                 $rep->NewLine();
169                                 if ($closeclass)
170                                 {
171                                         $rep->row += 6;
172                                         $rep->Line($rep->row);
173                                         $rep->NewLine();
174                                         $rep->Font('bold');
175                                         $rep->TextCol(0, 2,     _('Total') . " " . $classname);
176                                         $rep->AmountCol(2, 3, $classopen, $dec);
177                                         $rep->AmountCol(3, 4, $classperiod, $dec);
178                                         $rep->AmountCol(4, 5, $classclose, $dec);
179                                         $rep->Font();
180                                         $assetsopen += $classopen;
181                                         $assetsperiod += $classperiod;
182                                         $assetsclose += $classclose;
183                                         $classopen = $classperiod = $classclose = 0.0;
184                                         $rep->NewLine(2);
185                                         $closeclass = false;
186                                 }
187                         }
188                         if ($account['AccountClassName'] != $classname)
189                         {
190                                 $rep->Font('bold');
191                                 $rep->TextCol(0, 5, $account['AccountClassName']);
192                                 $rep->Font();
193                                 $rep->NewLine();
194                         }
195                         if ($account['parent'] != -1 && $account['parent'] != $parent[$level])
196                                 $level++;
197                         $typename[$level] = $account['AccountTypeName'];
198                         $rep->row -= 4;
199                         $rep->TextCol(0, 5, $account['AccountTypeName']);
200                         $rep->row -= 4;
201                         $rep->Line($rep->row);
202                         $rep->NewLine();
203                 }
204                 $classname = $account['AccountClassName'];
205                 $parent[$level] = $account['parent'];   
206
207                 for ($i = 0; $i <= $level; $i++)
208                 {
209                         $typeopen[$i] += $prev_balance;
210                         $typeperiod[$i] += $curr_balance;
211                         $typeclose[$i] = $typeopen[$i] + $typeperiod[$i];
212                 }
213                 $classopen += $prev_balance;
214                 $classperiod += $curr_balance;
215                 $classclose = $classopen + $classperiod;
216                 $rep->TextCol(0, 1,     $account['account_code']);
217                 $rep->TextCol(1, 2,     $account['account_name']);
218
219                 $rep->AmountCol(2, 3, $prev_balance, $dec);
220                 $rep->AmountCol(3, 4, $curr_balance, $dec);
221                 $rep->AmountCol(4, 5, $curr_balance + $prev_balance, $dec);
222
223                 $rep->NewLine();
224
225                 if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
226                 {
227                         $rep->Line($rep->row - 2);
228                         $rep->Header();
229                 }
230         }
231         if ($account['AccountClassName'] != $classname)
232         {
233                 if ($classname != '')
234                 {
235                         $closeclass = true;
236                 }
237         }
238         if ($account['AccountTypeName'] != $typename[$level])
239         {
240                 if ($typename[$level] != '')
241                 {
242                         for ( ; $level >= 0; $level--)
243                         {
244                                 $rep->row += 6;
245                                 $rep->Line($rep->row);
246                                 $rep->NewLine();
247                                 $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
248                                 $rep->AmountCol(2, 3, $typeopen[$level], $dec);
249                                 $rep->AmountCol(3, 4, $typeperiod[$level], $dec);
250                                 $rep->AmountCol(4, 5, $typeclose[$level], $dec);
251                                 $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
252                                 if ($parent[$level] == -1)
253                                         break;
254                                 $rep->NewLine();
255                         }
256                         if ($graphics)
257                         {
258                                 $pg->x[] = $$typename[$level];
259                                 $pg->y[] = abs($typeclose[$level]);
260                         }
261                         $rep->NewLine();
262                         if ($closeclass)
263                         {
264                                 $calculateopen = -$assetsopen - $classopen;
265                                 $calculateperiod = -$assetsperiod - $classperiod;
266                                 $calculateclose = -$assetsclose  - $classclose;
267                                 $rep->row += 6;
268                                 $rep->Line($rep->row);
269                                 $rep->NewLine();
270                                 $rep->TextCol(0, 2,     _('Calculated Return'));
271                                 $rep->AmountCol(2, 3, $calculateopen, $dec);
272                                 $rep->AmountCol(3, 4, $calculateperiod, $dec);
273                                 $rep->AmountCol(4, 5, $calculateclose, $dec);
274                                 if ($graphics)
275                                 {
276                                         $pg->x[] = _('Calculated Return');
277                                         $pg->y[] = abs($calculateclose);
278                                 }
279                                 $rep->NewLine(2);
280                                 $rep->Font('bold');
281                                 $rep->TextCol(0, 2,     _('Total') . " " . $classname);
282                                 $rep->AmountCol(2, 3, -$assetsopen, $dec);
283                                 $rep->AmountCol(3, 4, -$assetsperiod, $dec);
284                                 $rep->AmountCol(4, 5, -$assetsclose, $dec);
285                                 $rep->Font();
286                                 $rep->NewLine();
287                         }
288                 }
289         }
290         $rep->Line($rep->row);
291         if ($graphics)
292         {
293                 global $decseps, $graph_skin;
294                 $pg->title     = $rep->title;
295                 $pg->axis_x    = _("Group");
296                 $pg->axis_y    = _("Amount");
297                 $pg->graphic_1 = $to;
298                 $pg->type      = $graphics;
299                 $pg->skin      = $graph_skin;
300                 $pg->built_in  = false;
301                 $pg->fontfile  = $path_to_root . "/reporting/fonts/Vera.ttf";
302                 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
303                 $filename =  $comp_path.'/'.user_company()."/pdf_files/test.png";
304                 $pg->display($filename, true);
305                 $w = $pg->width / 1.5;
306                 $h = $pg->height / 1.5;
307                 $x = ($rep->pageWidth - $w) / 2;
308                 $rep->NewLine(2);
309                 if ($rep->row - $h < $rep->bottomMargin)
310                         $rep->Header();
311                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
312         }
313         $rep->End();
314 }
315
316 ?>