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