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