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