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