Merging changes form main trunk 2.2.5-2.2.6
[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 = 'SA_GLANALYTIC';
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt, Chaitanya for the recursive version 2009-02-05.
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 function display_type ($type, $typename, $from, $to, $convert, &$dec, &$rep, $dimension, $dimension2, &$pg, $graphics)
29 {
30         $code_open_balance = 0;
31         $code_period_balance = 0;
32         $open_balance_total = 0;
33         $period_balance_total = 0;
34         unset($totals_arr);
35         $totals_arr = array();
36
37         $printtitle = 0; //Flag for printing type name  
38         
39         //Get Accounts directly under this group/type
40         $result = get_gl_accounts(null, null, $type);   
41         while ($account=db_fetch($result))
42         {
43                 $prev_balance = get_gl_balance_from_to("", $from, $account["account_code"], $dimension, $dimension2);
44
45                 $curr_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
46
47                 if (!$prev_balance && !$curr_balance)
48                         continue;
49                 
50                 //Print Type Title if it has atleast one non-zero account       
51                 if (!$printtitle)
52                 {
53                         $printtitle = 1;
54                         $rep->row -= 4;
55                         $rep->TextCol(0, 5, $typename);
56                         $rep->row -= 4;
57                         $rep->Line($rep->row);
58                         $rep->NewLine();                
59                 }                       
60
61                 $rep->TextCol(0, 1,     $account['account_code']);
62                 $rep->TextCol(1, 2,     $account['account_name']);
63
64                 $rep->AmountCol(2, 3, $prev_balance * $convert, $dec);
65                 $rep->AmountCol(3, 4, $curr_balance * $convert, $dec);
66                 $rep->AmountCol(4, 5, ($prev_balance + $curr_balance) * $convert, $dec);
67
68                 $rep->NewLine();
69
70                 $code_open_balance += $prev_balance;
71                 $code_period_balance += $curr_balance;
72         }
73                 
74         //Get Account groups/types under this group/type
75         $result = get_account_types(false, false, $type);
76         while ($accounttype=db_fetch($result))
77         {
78                 //Print Type Title if has sub types and not previously printed
79                 if (!$printtitle)
80                 {
81                         $printtitle = 1;
82                         $rep->row -= 4;
83                         $rep->TextCol(0, 5, $typename);
84                         $rep->row -= 4;
85                         $rep->Line($rep->row);
86                         $rep->NewLine();                
87                 }
88
89                 $totals_arr = display_type($accounttype["id"], $accounttype["name"], $from, $to, $convert, $dec, 
90                         $rep, $dimension, $dimension2, $pg, $graphics);
91                 $open_balance_total += $totals_arr[0];
92                 $period_balance_total += $totals_arr[1];
93         }
94
95         //Display Type Summary if total is != 0 OR head is printed (Needed in case of unused hierarchical COA) 
96         if (($code_open_balance + $open_balance_total + $code_period_balance + $period_balance_total) != 0 || $printtitle)
97         {
98                 $rep->row += 6;
99                 $rep->Line($rep->row);
100                 $rep->NewLine();
101                 $rep->TextCol(0, 2,     _('Total') . " " . $typename);
102                 $rep->AmountCol(2, 3, ($code_open_balance + $open_balance_total) * $convert, $dec);
103                 $rep->AmountCol(3, 4, ($code_period_balance + $period_balance_total) * $convert, $dec);
104                 $rep->AmountCol(4, 5, ($code_open_balance + $open_balance_total + $code_period_balance + $period_balance_total) * $convert, $dec);              
105                 if ($graphics)
106                 {
107                         $pg->x[] = $typename;
108                         $pg->y[] = abs($code_open_balance + $open_balance_total);
109                         $pg->z[] = abs($code_period_balance + $period_balance_total);
110                 }
111                 $rep->NewLine();
112         }
113         
114         $totals_arr[0] = $code_open_balance + $open_balance_total;
115         $totals_arr[1] = $code_period_balance + $period_balance_total;
116         return $totals_arr;
117 }
118
119 print_balance_sheet();
120
121 //----------------------------------------------------------------------------------------------------
122
123 function print_balance_sheet()
124 {
125         global $comp_path, $path_to_root;
126
127         $dim = get_company_pref('use_dimension');
128         $dimension = $dimension2 = 0;
129
130         $from = $_POST['PARAM_0'];
131         $to = $_POST['PARAM_1'];
132         if ($dim == 2)
133         {
134                 $dimension = $_POST['PARAM_2'];
135                 $dimension2 = $_POST['PARAM_3'];
136                 $decimals = $_POST['PARAM_4'];
137                 $graphics = $_POST['PARAM_5'];
138                 $comments = $_POST['PARAM_6'];
139                 $destination = $_POST['PARAM_7'];
140         }
141         else if ($dim == 1)
142         {
143                 $dimension = $_POST['PARAM_2'];
144                 $decimals = $_POST['PARAM_3'];
145                 $graphics = $_POST['PARAM_4'];
146                 $comments = $_POST['PARAM_5'];
147                 $destination = $_POST['PARAM_6'];
148         }
149         else
150         {
151                 $decimals = $_POST['PARAM_2'];
152                 $graphics = $_POST['PARAM_3'];
153                 $comments = $_POST['PARAM_4'];
154                 $destination = $_POST['PARAM_5'];
155         }
156         if ($destination)
157                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
158         else
159                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
160         if ($graphics)
161         {
162                 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
163                 $pg = new graph();
164         }
165         if (!$decimals)
166                 $dec = 0;
167         else
168                 $dec = user_price_dec();
169
170         $cols = array(0, 50, 200, 350, 425,     500);
171         //------------0--1---2----3----4----5--
172
173         $headers = array(_('Account'), _('Account Name'), _('Open Balance'), _('Period'),
174                 _('Close Balance'));
175
176         $aligns = array('left', 'left', 'right', 'right', 'right');
177
178     if ($dim == 2)
179     {
180         $params =   array(      0 => $comments,
181                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
182                         2 => array('text' => _('Dimension')." 1",
183                             'from' => get_dimension_string($dimension), 'to' => ''),
184                         3 => array('text' => _('Dimension')." 2",
185                             'from' => get_dimension_string($dimension2), 'to' => ''));
186     }
187     else if ($dim == 1)
188     {
189         $params =   array(      0 => $comments,
190                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
191                         2 => array('text' => _('Dimension'),
192                             'from' => get_dimension_string($dimension), 'to' => ''));
193     }
194     else
195     {
196         $params =   array(      0 => $comments,
197                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
198     }
199
200         $rep = new FrontReport(_('Balance Sheet'), "BalanceSheet", user_pagesize());
201         $rep->Font();
202         $rep->Info($params, $cols, $headers, $aligns);
203         $rep->Header();
204
205         $calc_open = $calc_period = 0.0;
206         $equity_open = $equity_period = 0.0;
207         $liability_open = $liability_period = 0.0;
208         $econvert = $lconvert = 0;
209         
210         $classresult = get_account_classes(false, 1);
211         while ($class = db_fetch($classresult))
212         {
213                 $class_open_total = 0;
214                 $class_period_total = 0;
215                 $convert = get_class_type_convert($class["ctype"]);             
216                 
217                 //Print Class Name      
218                 $rep->Font('bold');
219                 $rep->TextCol(0, 5, $class["class_name"]);
220                 $rep->Font();
221                 $rep->NewLine();
222                 
223                 //Get Account groups/types under this group/type with no parents
224                 $typeresult = get_account_types(false, $class['cid'], -1);
225                 while ($accounttype=db_fetch($typeresult))
226                 {
227                         $classtotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $convert, $dec, 
228                                 $rep, $dimension, $dimension2, $pg, $graphics);
229                         $class_open_total += $classtotal[0];
230                         $class_period_total += $classtotal[1];                  
231                 }
232                 
233                 //Print Class Summary   
234                 $rep->row += 6;
235                 $rep->Line($rep->row);
236                 $rep->NewLine();
237                 $rep->Font('bold');
238                 $rep->TextCol(0, 2,     _('Total') . " " . $class["class_name"]);
239                 $rep->AmountCol(2, 3, $class_open_total * $convert, $dec);
240                 $rep->AmountCol(3, 4, $class_period_total * $convert, $dec);
241                 $rep->AmountCol(4, 5, ($class_open_total + $class_period_total) * $convert, $dec);
242                 $rep->Font();
243                 $rep->NewLine(2);       
244
245                 $calc_open += $class_open_total;
246                 $calc_period += $class_period_total;
247                 if ($class['ctype'] == CL_EQUITY)
248                 {
249                         $equity_open += $class_open_total;
250                         $equity_period += $class_period_total;
251                         $econvert = $convert;
252                 }
253                 elseif ($class['ctype'] == CL_LIABILITIES)
254                 {
255                         $liability_open += $class_open_total;
256                         $liability_period += $class_period_total;
257                         $lconvert = $convert;
258                 }
259         }
260         $rep->Font();   
261         $rep->TextCol(0, 2,     _('Calculated Return'));
262         if ($lconvert == 1)
263         {
264                 $calc_open *= -1;
265                 $calc_period *= -1;
266         }       
267         $rep->AmountCol(2, 3, $calc_open, $dec); // never convert
268         $rep->AmountCol(3, 4, $calc_period, $dec);
269         $rep->AmountCol(4, 5, $calc_open + $calc_period, $dec);
270         $rep->NewLine(2);
271
272         $rep->Font('bold');     
273         $rep->TextCol(0, 2,     _('Total') . " " . _('Liabilities') . _(' and ') . _('Equities'));
274         $topen = $equity_open * $econvert + $liability_open * $lconvert + $calc_open;
275         $tperiod = $equity_period * $econvert + $liability_period * $lconvert + $calc_period;
276         $tclose = $topen + $tperiod;
277         $rep->AmountCol(2, 3, $topen, $dec);
278         $rep->AmountCol(3, 4, $tperiod, $dec);
279         $rep->AmountCol(4, 5, $tclose, $dec);
280         
281         $rep->Font();
282         $rep->NewLine();
283         $rep->Line($rep->row);
284         if ($graphics)
285         {
286                 $pg->x[] = _('Calculated Return');
287                 $pg->y[] = abs($calc_open);
288                 $pg->z[] = abs($calc_period);
289                 global $decseps, $graph_skin;
290                 $pg->title     = $rep->title;
291                 $pg->axis_x    = _("Group");
292                 $pg->axis_y    = _("Amount");
293                 $pg->graphic_1 = $headers[2];
294                 $pg->graphic_2 = $headers[3];
295                 $pg->type      = $graphics;
296                 $pg->skin      = $graph_skin;
297                 $pg->built_in  = false;
298                 $pg->fontfile  = $path_to_root . "/reporting/fonts/Vera.ttf";
299                 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
300                 $filename = $comp_path.'/'.user_company(). "/pdf_files/test.png";
301                 $pg->display($filename, true);
302                 $w = $pg->width / 1.5;
303                 $h = $pg->height / 1.5;
304                 $x = ($rep->pageWidth - $w) / 2;
305                 $rep->NewLine(2);
306                 if ($rep->row - $h < $rep->bottomMargin)
307                         $rep->Header();
308                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
309         }
310         $rep->End();
311 }
312
313 ?>