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