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