Fixed many issues in output HTML code according to HTML 4.01 Transitional format.
[fa-stable.git] / gl / inquiry / balance_sheet.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 $path_to_root="../..";
14
15 include_once($path_to_root . "/includes/session.inc");
16
17 include_once($path_to_root . "/includes/date_functions.inc");
18 include_once($path_to_root . "/includes/ui.inc");
19 include_once($path_to_root . "/includes/data_checks.inc");
20
21 include_once($path_to_root . "/gl/includes/gl_db.inc");
22
23 $js = "";
24 if ($use_date_picker)
25         $js = get_js_date_picker();
26
27 page(_($help_context = "Balance Sheet Drilldown"), false, false, "", $js);
28
29 //----------------------------------------------------------------------------------------------------
30 // Ajax updates
31
32 if (get_post('Show')) 
33 {
34         $Ajax->activate('balance_tbl');
35 }
36
37 if (isset($_GET["TransFromDate"]))
38         $_POST["TransFromDate"] = $_GET["TransFromDate"];       
39 if (isset($_GET["TransToDate"]))
40         $_POST["TransToDate"] = $_GET["TransToDate"];
41 if (isset($_GET["Dimension"]))
42         $_POST["Dimension"] = $_GET["Dimension"];
43 if (isset($_GET["Dimension2"]))
44         $_POST["Dimension2"] = $_GET["Dimension2"];
45 if (isset($_GET["AccGrp"]))
46         $_POST["AccGrp"] = $_GET["AccGrp"];     
47
48 //----------------------------------------------------------------------------------------------------
49
50 function display_type ($type, $typename, $from, $to, $convert, $dimension, $dimension2, $drilldown, $path_to_root)
51 {
52         global $levelptr, $k;
53         
54         $acctstotal = 0;
55         $typestotal = 0;
56         
57         //Get Accounts directly under this group/type
58         $result = get_gl_accounts(null, null, $type);   
59                 
60         while ($account=db_fetch($result))
61     {
62         $net_balance = get_gl_trans_from_to("", $to, $account["account_code"], $dimension, $dimension2);
63         if (!$net_balance)
64             continue;
65         
66         if ($drilldown && $levelptr == 0)
67         {
68             $url = "<a href='$path_to_root/gl/inquiry/gl_account_inquiry.php?TransFromDate=" 
69                 . $from . "&TransToDate=" . $to . "&Dimension=" . $dimension . "&Dimension2=" . $dimension2 
70                 . "&account=" . $account['account_code'] . "'>" . $account['account_code'] 
71                 ." ". $account['account_name'] ."</a>";                
72                 
73             start_row("class='stockmankobg'");
74             label_cell($url);
75             amount_cell(($net_balance) * $convert);
76             end_row();
77         }
78         
79         $acctstotal += $net_balance;
80     }   
81         
82         $levelptr = 1;
83
84         //Get Account groups/types under this group/type
85         $result = get_account_types(false, false, $type);
86         while ($accounttype=db_fetch($result))
87         {                       
88                 $typestotal += display_type($accounttype["id"], $accounttype["name"], $from, $to, 
89                         $convert, $dimension, $dimension2, $drilldown, $path_to_root);  
90         }
91
92         //Display Type Summary if total is != 0  
93         if (($acctstotal + $typestotal) != 0)
94         {
95                 if ($drilldown && $type == $_POST["AccGrp"])
96                 {               
97                         start_row("class='inquirybg' style='font-weight:bold'");
98                         label_cell(_('Total') . " " . $typename);
99                         amount_cell(($acctstotal + $typestotal) * $convert);
100                         end_row();
101                 }
102                 //START Patch#1 : Display  only direct child types
103                 $acctype1 = get_account_type($type);
104                 $parent1 = $acctype1["parent"];
105                 if ($drilldown && $parent1 == $_POST["AccGrp"])
106                 //END Patch#2           
107                 //elseif ($drilldown && $type != $_POST["AccGrp"])
108                 {
109                         $url = "<a href='$path_to_root/gl/inquiry/balance_sheet.php?TransFromDate=" 
110                                 . $from . "&TransToDate=" . $to . "&Dimension=" . $dimension . "&Dimension2=" . $dimension2 
111                                 . "&AccGrp=" . $type ."'>" . $type . " " . $typename ."</a>";
112                                 
113                         alt_table_row_color($k);
114                         label_cell($url);
115                         amount_cell(($acctstotal + $typestotal) * $convert);
116                         end_row();
117                 }
118         }
119         return ($acctstotal + $typestotal);
120 }       
121         
122 function inquiry_controls()
123 {
124         $dim = get_company_pref('use_dimension');
125     start_table(TABLESTYLE_NOBORDER);
126         date_cells(_("As at:"), 'TransToDate');
127         if ($dim >= 1)
128                 dimensions_list_cells(_("Dimension")." 1:", 'Dimension', null, true, " ", false, 1);
129         if ($dim > 1)
130                 dimensions_list_cells(_("Dimension")." 2:", 'Dimension2', null, true, " ", false, 2);
131         submit_cells('Show',_("Show"),'','', 'default');
132     end_table();
133
134         hidden('TransFromDate');
135         hidden('AccGrp');
136 }
137
138 function display_balance_sheet()
139 {
140         global $path_to_root;
141         
142         $from = begin_fiscalyear();
143         $to = $_POST['TransToDate'];
144         
145         if (!isset($_POST['Dimension']))
146                 $_POST['Dimension'] = 0;
147         if (!isset($_POST['Dimension2']))
148                 $_POST['Dimension2'] = 0;
149         $dimension = $_POST['Dimension'];
150         $dimension2 = $_POST['Dimension2'];
151         $lconvert = $econvert = 1;
152         if (isset($_POST["AccGrp"]) && (strlen($_POST['AccGrp']) > 0))
153                 $drilldown = 1; // Deeper Level
154         else
155                 $drilldown = 0; // Root level   
156
157         div_start('balance_tbl');
158         
159         start_table(TABLESTYLE, "width='30%'");
160                 
161         if (!$drilldown) //Root Level
162         {               
163                 $equityclose = 0.0;
164                 $lclose = 0.0; 
165                 $calculateclose = 0.0;          
166
167                 $parent = -1;
168
169                 //Get classes for BS
170                 $classresult = get_account_classes(false, 1);
171         
172                 while ($class = db_fetch($classresult))
173                 {       
174                         $classclose = 0.0;
175                         $convert = get_class_type_convert($class["ctype"]);
176                         $ctype = $class["ctype"];
177                         $classname = $class["class_name"];      
178
179                         //Print Class Name      
180                         table_section_title($class["class_name"]);
181                         
182                         //Get Account groups/types under this group/type
183                         $typeresult = get_account_types(false, $class['cid'], -1);
184                                 
185                         while ($accounttype=db_fetch($typeresult))
186                         {
187                                 $TypeTotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, 
188                                                 $convert, $dimension, $dimension2, $drilldown, $path_to_root);  
189                                 //Print Summary 
190                                 if ($TypeTotal != 0 )
191                                 {
192                                         $url = "<a href='$path_to_root/gl/inquiry/balance_sheet.php?TransFromDate=" 
193                                                 . $from . "&TransToDate=" . $to . "&Dimension=" . $dimension . "&Dimension2=" . $dimension2 
194                                                 . "&AccGrp=" . $accounttype['id'] ."'>" . $accounttype['id']." ".$accounttype['name'] ."</a>";  
195                                         alt_table_row_color($k);
196                                         label_cell($url);
197                                         amount_cell($TypeTotal * $convert);
198                                         end_row();
199                                 }
200                                 $classclose += $TypeTotal;
201                         }                               
202
203                         //Print Class Summary
204                         start_row("class='inquirybg' style='font-weight:bold'");
205                         label_cell(_('Total') . " " . $class["class_name"]);
206                         amount_cell($classclose * $convert);
207                         end_row();              
208                         
209                         if ($ctype == CL_EQUITY)
210                         {
211                                 $equityclose += $classclose;
212                                 $econvert = $convert;
213                         }
214                         if ($ctype == CL_LIABILITIES)
215                         {
216                                 $lclose += $classclose;
217                                 $lconvert = $convert;
218                         }
219         
220                         $calculateclose += $classclose;
221                 }
222                 
223                 if ($lconvert == 1)
224                         $calculateclose *= -1;
225                 //Final Report Summary
226                 $url = "<a href='$path_to_root/gl/inquiry/profit_loss.php?TransFromDate=" 
227                                 . $from."&TransToDate=".$to . "&Dimension=" . $dimension . "&Dimension2=" . $dimension2
228                         ."&Compare=0'>"._('Calculated Return')."</a>";          
229                 
230                 start_row("class='inquirybg' style='font-weight:bold'");
231                 label_cell($url);
232                 amount_cell($calculateclose);
233                 end_row();              
234                 
235                 start_row("class='inquirybg' style='font-weight:bold'");
236                 label_cell(_('Total') . " " . _('Liabilities') . _(' and ') . _('Equities'));
237                 amount_cell($lclose * $lconvert + $equityclose * $econvert + $calculateclose);
238                 end_row();
239         }
240         else //Drill Down
241         {
242                 //Level Pointer : Global variable defined in order to control display of root 
243                 global $levelptr;
244                 $levelptr = 0;
245                 
246                 $accounttype = get_account_type($_POST["AccGrp"]);
247                 $classid = $accounttype["class_id"];
248                 $class = get_account_class($classid);
249                 $convert = get_class_type_convert($class["ctype"]); 
250                 
251                 //Print Class Name      
252                 table_section_title($_POST["AccGrp"]. " " . get_account_type_name($_POST["AccGrp"]));   
253                 
254                 $classclose = display_type($accounttype["id"], $accounttype["name"], $from, $to, 
255                         $convert, $dimension, $dimension2, $drilldown, $path_to_root);
256         }
257         
258         end_table(1); // outer table
259         div_end();
260 }
261
262 //----------------------------------------------------------------------------------------------------
263
264 start_form();
265
266 inquiry_controls();
267
268 display_balance_sheet();
269
270 end_form();
271
272 end_page();
273
274 ?>
275