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="../..";
15 include_once($path_to_root . "/includes/session.inc");
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");
21 include_once($path_to_root . "/gl/includes/gl_db.inc");
25 $js = get_js_date_picker();
27 page(_($help_context = "Balance Sheet Drilldown"), false, false, "", $js);
29 //----------------------------------------------------------------------------------------------------
34 $Ajax->activate('balance_tbl');
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"];
44 //----------------------------------------------------------------------------------------------------
46 function is_of_account_type($accttype,$typeslist)
48 return in_array($accttype, $typeslist);
51 function get_child_account_types($acctype)
54 $parentsarr = array();
55 $childernsarr = array();
57 array_push($parentsarr, $acctype);
58 while (sizeof($parentsarr)>0)
60 $parent = array_pop($parentsarr);
61 array_push($childernsarr,$parent);
64 $list = substr($list,0,-1);
68 function pushchilds($parent)
72 $sql = "SELECT id FROM ".TB_PREF."chart_types WHERE parent=".$parent;
73 $result = db_query($sql,"Query failed");
74 while ($myrow=db_fetch($result))
76 array_push($parentsarr, $myrow['id']);
80 function inquiry_controls()
83 start_table("class='tablestyle_noborder'");
84 date_cells(_("As at:"), 'TransToDate');
85 submit_cells('Show',_("Show"),'','', 'default');
88 hidden('TransFromDate');
93 function print_balance_sheet()
95 global $comp_path, $path_to_root, $table_style;
97 $from = begin_fiscalyear();
98 $to = $_POST['TransToDate'];
100 $dim = get_company_pref('use_dimension');
101 $dimension = $dimension2 = 0;
117 $typeopen = array(0,0,0,0,0,0,0,0,0,0);
118 $typeperiod = array(0,0,0,0,0,0,0,0,0,0);
119 $typeclose = array(0,0,0,0,0,0,0,0,0,0);
120 $typename = array('','','','','','','','','','');
121 $acctype = array('','','','','','','','','','');
122 $closing = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
123 //$parent = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
131 $accounts = get_gl_accounts_all(1);
133 div_start('balance_tbl');
135 start_table("width=30% $table_style");
137 while ($account=db_fetch($accounts))
139 if ($account['account_code'] == null && $account['parent'] > 0)
142 //Check for confirming the account type
143 if (isset($_POST['AccGrp']) && (strlen($_POST['AccGrp']) > 0) )
145 $sub_types_arr = get_child_account_types($_POST['AccGrp']);
146 if (!is_of_account_type($account['AccountType'], $sub_types_arr))
150 if ($account['account_code'] != null)
152 $prev_balance = get_gl_balance_from_to("", $from, $account["account_code"], $dimension, $dimension2);
154 $curr_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
156 if (!$prev_balance && !$curr_balance)
159 if ($account['AccountClassName'] != $classname)
161 if ($classname != '')
166 if ($account['AccountTypeName'] != $typename[$level])
168 if ($typename[$level] != '')
170 for ( ; $level >= 0, $typename[$level] != ''; $level--)
172 if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0 || $closeclass)
175 $url = "<a href='$path_to_root/gl/inquiry/balance_sheet.php?TransFromDate="
176 . $from . "&TransToDate=" . $to
177 . "&AccGrp=" . $acctype[$level] . "'>" . $typename[$level] . "</a>";
179 alt_table_row_color($k);
181 amount_cell($typeclose[$level] * $convert);
184 $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
192 start_row("class='inquirybg' style='font-weight:bold'");
193 label_cell(_('Total') . " " . $classname);
194 amount_cell($classclose * $convert, true);
197 if ($ctype == CL_EQUITY)
199 $equityopen += $classopen;
200 $equityperiod += $classperiod;
201 $equityclose += $classclose;
203 if ($ctype == CL_LIABILITIES)
205 $lopen += $classopen;
206 $lperiod += $classperiod;
207 $lclose += $classclose;
209 $assetsopen += $classopen;
210 $assetsperiod += $classperiod;
211 $assetsclose += $classclose;
212 $classopen = $classperiod = $classclose = 0.0;
217 if ($account['AccountClassName'] != $classname)
219 if (isset($_POST['AccGrp']) && (strlen($_POST['AccGrp']) > 0))
220 table_section_title($account['AccountTypeName']);
222 table_section_title($account['AccountClassName']);
225 if ($account['parent'] != $last)
226 $last = $account['parent'];
227 $typename[$level] = $account['AccountTypeName'];
229 $acctype[$level] = $account['AccountType'];
231 $closing[$level] = $account['parent'];
234 $classname = $account['AccountClassName'];
235 $classtype = $account['AccountType'];
236 $ctype = $account['ClassType'];
237 $convert = get_class_type_convert($ctype);
239 if ($account['account_code'] != null)
241 for ($i = 0; $i <= $level; $i++)
243 $typeopen[$i] += $prev_balance;
244 $typeperiod[$i] += $curr_balance;
245 $typeclose[$i] = $typeopen[$i] + $typeperiod[$i];
247 $classopen += $prev_balance;
248 $classperiod += $curr_balance;
249 $classclose = $classopen + $classperiod;
251 //Show accounts details only for drill down and direct child of Account Group
252 if ( isset($_POST['AccGrp']) && ($account['AccountType'] == $_POST['AccGrp']))
254 $url = "<a href='$path_to_root/gl/inquiry/gl_account_inquiry.php?TransFromDate="
255 . $from . "&TransToDate=" . $to
256 . "&account=" . $account['account_code'] . "'>" . $account['account_code']
257 ." ". $account['account_name'] ."</a>";
259 start_row("class='stockmankobg'");
261 amount_cell(($curr_balance + $prev_balance) * $convert);
267 if ($account['AccountClassName'] != $classname)
269 if ($classname != '')
274 if ($account['AccountTypeName'] != $typename[$level])
277 if ($typename[$level] != '')
279 for ( ; $level >= 0, $typename[$level] != ''; $level--)
281 if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0 || $closeclass)
283 //Inside drill down, no hyperlink
284 if (isset($_POST['AccGrp']) && (strlen($_POST['AccGrp']) > 0) && ($acctype[$level] == $_POST['AccGrp']))
286 start_row("class='inquirybg' style='font-weight:bold'");
287 label_cell(_('Total') . " " .$typename[$level]);
288 amount_cell($typeclose[$level] * $convert);
293 $url = "<a href='$path_to_root/gl/inquiry/balance_sheet.php?TransFromDate="
294 . $from . "&TransToDate=" . $to
295 . "&AccGrp=" . $acctype[$level] . "'>" . $typename[$level] . "</a>";
297 alt_table_row_color($k);
299 amount_cell($typeclose[$level] * $convert);
303 $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
311 if (($closeclass) && !(isset($_POST['AccGrp']) && (strlen($_POST['AccGrp']) > 0)) )
313 $calculateopen = -$assetsopen - $classopen;
314 $calculateperiod = -$assetsperiod - $classperiod;
315 $calculateclose = -$assetsclose - $classclose;
316 if ($ctype == CL_EQUITY)
318 $equityopen += $classopen;
319 $equityperiod += $classperiod;
320 $equityclose += $classclose;
323 alt_table_row_color($k);
324 label_cell(_('Calculated Return'));
325 amount_cell($calculateclose * $convert);
328 start_row("class='inquirybg' style='font-weight:bold'");
329 label_cell(_('Total') . " " . $classname);
330 amount_cell(-$assetsclose * $convert);
333 if ($equityopen != 0.0 || $equityperiod != 0.0 || $equityclose != 0.0 ||
334 $lopen != 0.0 || $lperiod != 0.0 || $lclose != 0.0)
336 alt_table_row_color($k);
337 label_cell(_('Total') . " " . _('Liabilities') . _(' and ') . _('Equities'));
338 amount_cell(($lclose + $equityclose + $calculateclose) * -1);
344 end_table(1); // outer table
348 //----------------------------------------------------------------------------------------------------
354 print_balance_sheet();