Again fixed nested subtypes in account types correctly in COA, Monthly Breakdown...
[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         $closing = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
121         $level = 0;
122         $last = -1;
123         
124         $closeclass = false;
125         $rep->NewLine();
126
127         $types = get_account_types_all(1);
128
129         while ($type=db_fetch($types))
130         {
131                 if (!num_accounts_in_type($type['AccountType'], $type['parent']))
132                         continue;
133                 if ($type['AccountClassName'] != $classname)
134                 {
135                         if ($classname != '')
136                         {
137                                 $closeclass = true;
138                         }
139                 }
140
141                 if ($type['AccountTypeName'] != $typename[$level])
142                 {
143                         //$rep->NewLine();
144                         //$rep->TextCol(0, 5,   "level = $level, closing[level] = ".$closing[$level].", type[parent] = ".$type['parent']." last = ".$last);
145                         //$rep->NewLine();
146                         if ($typename[$level] != '')
147                         {
148                                 for ( ; $level >= 0, $typename[$level] != ''; $level--) 
149                                 {
150                                         if ($type['parent'] == $closing[$level] || $type['parent'] == $last || $type['parent'] <= 0)
151                                         {
152                                                 $rep->row += 6;
153                                                 $rep->Line($rep->row);
154                                                 $rep->NewLine();
155                                                 $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
156                                                 $rep->AmountCol(2, 3, $typeopen[$level], $dec);
157                                                 $rep->AmountCol(3, 4, $typeperiod[$level], $dec);
158                                                 $rep->AmountCol(4, 5, $typeclose[$level], $dec);
159                                                 if ($graphics)
160                                                 {
161                                                         $pg->x[] = $$typename[$level];
162                                                         $pg->y[] = abs($typeclose[$level]);
163                                                 }
164                                                 $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
165                                         }       
166                                         else
167                                                 break;
168                                         $rep->NewLine();
169                                 }
170                                 //$rep->NewLine();
171                                 if ($closeclass)
172                                 {
173                                         $rep->row += 6;
174                                         $rep->Line($rep->row);
175                                         $rep->NewLine();
176                                         $rep->Font('bold');
177                                         $rep->TextCol(0, 2,     _('Total') . " " . $classname);
178                                         $rep->AmountCol(2, 3, $classopen, $dec);
179                                         $rep->AmountCol(3, 4, $classperiod, $dec);
180                                         $rep->AmountCol(4, 5, $classclose, $dec);
181                                         $rep->Font();
182                                         $assetsopen += $classopen;
183                                         $assetsperiod += $classperiod;
184                                         $assetsclose += $classclose;
185                                         $classopen = $classperiod = $classclose = 0.0;
186                                         $rep->NewLine(2);
187                                         $closeclass = false;
188                                 }
189                         }
190                         if ($type['AccountClassName'] != $classname)
191                         {
192                                 $rep->Font('bold');
193                                 $rep->TextCol(0, 5, $type['AccountClassName']);
194                                 $rep->Font();
195                                 $rep->NewLine();
196                         }
197                         $level++;
198                         if ($type['parent'] != $last)
199                                 $last = $type['parent'];
200                         $typename[$level] = $type['AccountTypeName'];
201                         $closing[$level] = $type['parent'];
202                         $rep->row -= 4;
203                         $rep->TextCol(0, 5, $type['AccountTypeName']);
204                         $rep->row -= 4;
205                         $rep->Line($rep->row);
206                         $rep->NewLine();
207                 }
208                 $classname = $type['AccountClassName'];
209
210                 $accounts = get_gl_accounts_in_type($type['AccountType']);
211                 while ($account=db_fetch($accounts))
212                 {
213                         $prev_balance = get_gl_balance_from_to("", $from, $account["account_code"], $dimension, $dimension2);
214
215                         $curr_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
216
217                         if (!$prev_balance && !$curr_balance)
218                                 continue;
219
220                         for ($i = 0; $i <= $level; $i++)
221                         {
222                                 $typeopen[$i] += $prev_balance;
223                                 $typeperiod[$i] += $curr_balance;
224                                 $typeclose[$i] = $typeopen[$i] + $typeperiod[$i];
225                         }
226                         $classopen += $prev_balance;
227                         $classperiod += $curr_balance;
228                         $classclose = $classopen + $classperiod;
229                         $rep->TextCol(0, 1,     $account['account_code']);
230                         $rep->TextCol(1, 2,     $account['account_name']);
231
232                         $rep->AmountCol(2, 3, $prev_balance, $dec);
233                         $rep->AmountCol(3, 4, $curr_balance, $dec);
234                         $rep->AmountCol(4, 5, $curr_balance + $prev_balance, $dec);
235
236                         $rep->NewLine();
237
238                         if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
239                         {
240                                 $rep->Line($rep->row - 2);
241                                 $rep->Header();
242                         }
243                 }       
244         }
245         if ($type['AccountClassName'] != $classname)
246         {
247                 if ($classname != '')
248                 {
249                         $closeclass = true;
250                 }
251         }
252         if ($type['AccountTypeName'] != $typename[$level])
253         {
254                 if ($typename[$level] != '')
255                 {
256                         for ( ; $level >= 0, $typename[$level] != ''; $level--) 
257                         {
258                                 if ($type['parent'] == $closing[$level] || $type['parent'] == $last || $type['parent'] <= 0)
259                                 {
260                                         $rep->row += 6;
261                                         $rep->Line($rep->row);
262                                         $rep->NewLine();
263                                         $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
264                                         $rep->AmountCol(2, 3, $typeopen[$level], $dec);
265                                         $rep->AmountCol(3, 4, $typeperiod[$level], $dec);
266                                         $rep->AmountCol(4, 5, $typeclose[$level], $dec);
267                                         if ($graphics)
268                                         {
269                                                 $pg->x[] = $$typename[$level];
270                                                 $pg->y[] = abs($typeclose[$level]);
271                                         }
272                                         $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
273                                 }
274                                 else
275                                         break;
276                                 $rep->NewLine();
277                         }
278                         //$rep->NewLine();
279                         if ($closeclass)
280                         {
281                                 $calculateopen = -$assetsopen - $classopen;
282                                 $calculateperiod = -$assetsperiod - $classperiod;
283                                 $calculateclose = -$assetsclose  - $classclose;
284                                 $rep->row += 6;
285                                 $rep->Line($rep->row);
286                                 $rep->NewLine();
287                                 $rep->TextCol(0, 2,     _('Calculated Return'));
288                                 $rep->AmountCol(2, 3, $calculateopen, $dec);
289                                 $rep->AmountCol(3, 4, $calculateperiod, $dec);
290                                 $rep->AmountCol(4, 5, $calculateclose, $dec);
291                                 if ($graphics)
292                                 {
293                                         $pg->x[] = _('Calculated Return');
294                                         $pg->y[] = abs($calculateclose);
295                                 }
296                                 $rep->NewLine(2);
297                                 $rep->Font('bold');
298                                 $rep->TextCol(0, 2,     _('Total') . " " . $classname);
299                                 $rep->AmountCol(2, 3, -$assetsopen, $dec);
300                                 $rep->AmountCol(3, 4, -$assetsperiod, $dec);
301                                 $rep->AmountCol(4, 5, -$assetsclose, $dec);
302                                 $rep->Font();
303                                 $rep->NewLine();
304                         }
305                 }
306         }
307         $rep->Line($rep->row);
308         if ($graphics)
309         {
310                 global $decseps, $graph_skin;
311                 $pg->title     = $rep->title;
312                 $pg->axis_x    = _("Group");
313                 $pg->axis_y    = _("Amount");
314                 $pg->graphic_1 = $to;
315                 $pg->type      = $graphics;
316                 $pg->skin      = $graph_skin;
317                 $pg->built_in  = false;
318                 $pg->fontfile  = $path_to_root . "/reporting/fonts/Vera.ttf";
319                 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
320                 $filename =  $comp_path.'/'.user_company()."/pdf_files/test.png";
321                 $pg->display($filename, true);
322                 $w = $pg->width / 1.5;
323                 $h = $pg->height / 1.5;
324                 $x = ($rep->pageWidth - $w) / 2;
325                 $rep->NewLine(2);
326                 if ($rep->row - $h < $rep->bottomMargin)
327                         $rep->Header();
328                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
329         }
330         $rep->End();
331 }
332
333 ?>