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