[0004964] Balance Sheet Drilldown, Profit and Loss Drilldown: Fixed invalid navigatio...
[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 (user_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)
51 {
52         global $path_to_root, $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);
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                 {
108                         $url = "<a href='$path_to_root/gl/inquiry/balance_sheet.php?TransFromDate=" 
109                                 . $from . "&TransToDate=" . $to . "&Dimension=" . $dimension . "&Dimension2=" . $dimension2 
110                                 . "&AccGrp=" . $type ."'>" . $type . " " . $typename ."</a>";
111                                 
112                         alt_table_row_color($k);
113                         label_cell($url);
114                         amount_cell(($acctstotal + $typestotal) * $convert);
115                         end_row();
116                 }
117         }
118         return ($acctstotal + $typestotal);
119 }       
120         
121 function inquiry_controls()
122 {
123         $dim = get_company_pref('use_dimension');
124     start_table(TABLESTYLE_NOBORDER);
125         date_cells(_("As at:"), 'TransToDate');
126         if ($dim >= 1)
127                 dimensions_list_cells(_("Dimension")." 1:", 'Dimension', null, true, " ", false, 1);
128         if ($dim > 1)
129                 dimensions_list_cells(_("Dimension")." 2:", 'Dimension2', null, true, " ", false, 2);
130         submit_cells('Show',_("Show"),'','', 'default');
131     end_table();
132
133         hidden('TransFromDate');
134         hidden('AccGrp');
135 }
136
137 function display_balance_sheet()
138 {
139         global $path_to_root;
140         
141         $to = $_POST['TransToDate'];
142         $from = get_fiscalyear_begin_for_date($to);
143
144         if (!isset($_POST['Dimension']))
145                 $_POST['Dimension'] = 0;
146         if (!isset($_POST['Dimension2']))
147                 $_POST['Dimension2'] = 0;
148         $dimension = $_POST['Dimension'];
149         $dimension2 = $_POST['Dimension2'];
150         $lconvert = $econvert = 1;
151         if (isset($_POST["AccGrp"]) && (strlen($_POST['AccGrp']) > 0))
152                 $drilldown = 1; // Deeper Level
153         else
154                 $drilldown = 0; // Root level   
155
156         div_start('balance_tbl');
157         
158         start_table(TABLESTYLE, "width='30%'");
159                 
160         if (!$drilldown) //Root Level
161         {               
162                 $equityclose = 0.0;
163                 $lclose = 0.0; 
164                 $calculateclose = 0.0;          
165
166                 //Get classes for BS
167                 $classresult = get_account_classes(false, 1);
168         
169                 while ($class = db_fetch($classresult))
170                 {       
171                         $classclose = 0.0;
172                         $convert = get_class_type_convert($class["ctype"]);
173                         $ctype = $class["ctype"];
174
175                         //Print Class Name      
176                         table_section_title($class["class_name"]);
177                         
178                         //Get Account groups/types under this group/type
179                         $typeresult = get_account_types(false, $class['cid'], -1);
180                         
181                         $k = 0;
182                         while ($accounttype=db_fetch($typeresult))
183                         {
184                                 $TypeTotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, 
185                                                 $convert, $dimension, $dimension2, $drilldown); 
186                                 //Print Summary 
187                                 if ($TypeTotal != 0 )
188                                 {
189                                         $url = "<a href='$path_to_root/gl/inquiry/balance_sheet.php?TransFromDate=" 
190                                                 . $from . "&TransToDate=" . $to . "&Dimension=" . $dimension . "&Dimension2=" . $dimension2 
191                                                 . "&AccGrp=" . $accounttype['id'] ."'>" . $accounttype['id']." ".$accounttype['name'] ."</a>";  
192                                         alt_table_row_color($k);
193                                         label_cell($url);
194                                         amount_cell($TypeTotal * $convert);
195                                         end_row();
196                                 }
197                                 $classclose += $TypeTotal;
198                         }                               
199
200                         //Print Class Summary
201                         start_row("class='inquirybg' style='font-weight:bold'");
202                         label_cell(_('Total') . " " . $class["class_name"]);
203                         amount_cell($classclose * $convert);
204                         end_row();              
205                         
206                         if ($ctype == CL_EQUITY)
207                         {
208                                 $equityclose += $classclose;
209                                 $econvert = $convert;
210                         }
211                         if ($ctype == CL_LIABILITIES)
212                         {
213                                 $lclose += $classclose;
214                                 $lconvert = $convert;
215                         }
216         
217                         $calculateclose += $classclose;
218                 }
219                 
220                 if ($lconvert == 1)
221                         $calculateclose *= -1;
222                 //Final Report Summary
223                 $url = "<a href='$path_to_root/gl/inquiry/profit_loss.php?TransFromDate=" 
224                                 . $from."&TransToDate=".$to . "&Dimension=" . $dimension . "&Dimension2=" . $dimension2
225                         ."&Compare=0'>"._('Calculated Return')."</a>";          
226                 
227                 start_row("class='inquirybg' style='font-weight:bold'");
228                 label_cell($url);
229                 amount_cell($calculateclose);
230                 end_row();              
231                 
232                 start_row("class='inquirybg' style='font-weight:bold'");
233                 label_cell(_('Total') . " " . _('Liabilities') . _(' and ') . _('Equities'));
234                 amount_cell($lclose * $lconvert + $equityclose * $econvert + $calculateclose);
235                 end_row();
236         }
237         else //Drill Down
238         {
239                 //Level Pointer : Global variable defined in order to control display of root 
240                 global $levelptr;
241                 $levelptr = 0;
242                 
243                 $accounttype = get_account_type($_POST["AccGrp"]);
244                 $classid = $accounttype["class_id"];
245                 $class = get_account_class($classid);
246                 $convert = get_class_type_convert($class["ctype"]); 
247                 
248                 //Print Class Name      
249                 table_section_title($_POST["AccGrp"]. " " . get_account_type_name($_POST["AccGrp"]));   
250                 
251                 $classclose = display_type($accounttype["id"], $accounttype["name"], $from, $to, 
252                         $convert, $dimension, $dimension2, $drilldown);
253         }
254         
255         end_table(); // outer table
256         hyperlink_params($_SERVER['PHP_SELF'], _("Back"), "TransFromDate=". $from . "&TransToDate=" . $to . "&Dimension=" . $dimension . "&Dimension2=" . $dimension2);
257         div_end();
258 }
259
260 //----------------------------------------------------------------------------------------------------
261
262 start_form();
263
264 inquiry_controls();
265
266 display_balance_sheet();
267
268 end_form();
269
270 end_page(false, true);
271