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/ui.inc");
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/admin/db/fiscalyears_db.inc");
20 include_once($path_to_root . "/includes/data_checks.inc");
22 include_once($path_to_root . "/gl/includes/gl_db.inc");
26 $js = get_js_date_picker();
28 page(_($help_context = "Trial Balance"), false, false, "", $js);
31 $pdeb = $pcre = $cdeb = $ccre = $tdeb = $tcre = $pbal = $cbal = $tbal = 0;
33 //----------------------------------------------------------------------------------------------------
38 $Ajax->activate('balance_tbl');
42 function gl_inquiry_controls()
44 $dim = get_company_pref('use_dimension');
47 start_table(TABLESTYLE_NOBORDER);
50 if (!isset($_POST['TransFromDate']))
51 $_POST['TransFromDate'] = begin_month($date);
52 if (!isset($_POST['TransToDate']))
53 $_POST['TransToDate'] = end_month($date);
54 date_cells(_("From:"), 'TransFromDate');
55 date_cells(_("To:"), 'TransToDate');
57 dimensions_list_cells(_("Dimension")." 1:", 'Dimension', null, true, " ", false, 1);
59 dimensions_list_cells(_("Dimension")." 2:", 'Dimension2', null, true, " ", false, 2);
60 check_cells(_("No zero values"), 'NoZero', null);
61 check_cells(_("Only balances"), 'Balance', null);
63 submit_cells('Show',_("Show"),'','', 'default');
68 //----------------------------------------------------------------------------------------------------
70 function display_trial_balance($type, $typename)
72 global $path_to_root, $clear_trial_balance_opening;
74 global $k, $pdeb, $pcre, $cdeb, $ccre, $tdeb, $tcre, $pbal, $cbal, $tbal;
75 $printtitle = 0; //Flag for printing type name
79 //$accounts = get_gl_accounts();
80 //Get Accounts directly under this group/type
81 $accounts = get_gl_accounts(null, null, $type);
83 $begin = get_fiscalyear_begin_for_date($_POST['TransFromDate']);
84 //$begin = begin_fiscalyear();
85 if (date1_greater_date2($begin, $_POST['TransFromDate']))
86 $begin = $_POST['TransFromDate'];
87 $begin = add_days($begin, -1);
89 while ($account = db_fetch($accounts))
91 //Print Type Title if it has atleast one non-zero account
94 start_row("class='inquirybg' style='font-weight:bold'");
95 label_cell(_("Group")." - ".$type ." - ".$typename, "colspan=8");
100 // FA doesn't really clear the closed year, therefore the brought forward balance includes all the transactions from the past, even though the balance is null.
101 // If we want to remove the balanced part for the past years, this option removes the common part from from the prev and tot figures.
102 if (@$clear_trial_balance_opening)
104 $open = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin, $begin, false, true);
105 $offset = min($open['debit'], $open['credit']);
109 $prev = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin, $_POST['TransFromDate'], false, false);
110 $curr = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $_POST['TransFromDate'], $_POST['TransToDate'], true, true);
111 $tot = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin, $_POST['TransToDate'], false, true);
112 if (check_value("NoZero") && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
114 alt_table_row_color($k);
116 $url = "<a href='$path_to_root/gl/inquiry/gl_account_inquiry.php?TransFromDate=" . $_POST["TransFromDate"] . "&TransToDate=" . $_POST["TransToDate"] . "&account=" . $account["account_code"] . "&Dimension=" . $_POST["Dimension"] . "&Dimension2=" . $_POST["Dimension2"] . "'>" . $account["account_code"] . "</a>";
119 label_cell($account["account_name"]);
120 if (check_value('Balance'))
122 display_debit_or_credit_cells($prev['balance']);
123 display_debit_or_credit_cells($curr['balance']);
124 display_debit_or_credit_cells($tot['balance']);
129 amount_cell($prev['debit']-$offset);
130 amount_cell($prev['credit']-$offset);
131 amount_cell($curr['debit']);
132 amount_cell($curr['credit']);
133 amount_cell($tot['debit']-$offset);
134 amount_cell($tot['credit']-$offset);
135 $pdeb += $prev['debit'];
136 $pcre += $prev['credit'];
137 $cdeb += $curr['debit'];
138 $ccre += $curr['credit'];
139 $tdeb += $tot['debit'];
140 $tcre += $tot['credit'];
142 $pbal += $prev['balance'];
143 $cbal += $curr['balance'];
144 $tbal += $tot['balance'];
148 //Get Account groups/types under this group/type
149 $result = get_account_types(false, false, $type);
150 while ($accounttype=db_fetch($result))
152 //Print Type Title if has sub types and not previously printed
155 start_row("class='inquirybg' style='font-weight:bold'");
156 label_cell(_("Group")." - ".$type ." - ".$typename, "colspan=8");
160 display_trial_balance($accounttype["id"], $accounttype["name"].' ('.$typename.')');
164 //----------------------------------------------------------------------------------------------------
166 gl_inquiry_controls();
168 if (isset($_POST['TransFromDate']))
170 $row = get_current_fiscalyear();
171 if (date1_greater_date2($_POST['TransFromDate'], sql2date($row['end'])))
173 display_error(_("The from date cannot be bigger than the fiscal year end."));
174 set_focus('TransFromDate');
178 div_start('balance_tbl');
179 if (!isset($_POST['Dimension']))
180 $_POST['Dimension'] = 0;
181 if (!isset($_POST['Dimension2']))
182 $_POST['Dimension2'] = 0;
183 start_table(TABLESTYLE);
185 <td rowspan=2 class='tableheader'>" . _("Account") . "</td>
186 <td rowspan=2 class='tableheader'>" . _("Account Name") . "</td>
187 <td colspan=2 class='tableheader'>" . _("Brought Forward") . "</td>
188 <td colspan=2 class='tableheader'>" . _("This Period") . "</td>
189 <td colspan=2 class='tableheader'>" . _("Balance") . "</td>
191 <td class='tableheader'>" . _("Debit") . "</td>
192 <td class='tableheader'>" . _("Credit") . "</td>
193 <td class='tableheader'>" . _("Debit") . "</td>
194 <td class='tableheader'>" . _("Credit") . "</td>
195 <td class='tableheader'>" . _("Debit") . "</td>
196 <td class='tableheader'>" . _("Credit") . "</td>
201 //display_trial_balance();
203 $classresult = get_account_classes(false);
204 while ($class = db_fetch($classresult))
206 start_row("class='inquirybg' style='font-weight:bold'");
207 label_cell(_("Class")." - ".$class['cid'] ." - ".$class['class_name'], "colspan=8");
210 //Get Account groups/types under this group/type with no parents
211 $typeresult = get_account_types(false, $class['cid'], -1);
212 while ($accounttype=db_fetch($typeresult))
214 display_trial_balance($accounttype["id"], $accounttype["name"]);
218 //$prev = get_balance(null, $begin, $_POST['TransFromDate'], false, false);
219 //$curr = get_balance(null, $_POST['TransFromDate'], $_POST['TransToDate'], true, true);
220 //$tot = get_balance(null, $begin, $_POST['TransToDate'], false, true);
221 if (!check_value('Balance'))
223 start_row("class='inquirybg' style='font-weight:bold'");
224 label_cell(_("Total") ." - ".$_POST['TransToDate'], "colspan=2");
233 start_row("class='inquirybg' style='font-weight:bold'");
234 label_cell(_("Ending Balance") ." - ".$_POST['TransToDate'], "colspan=2");
235 display_debit_or_credit_cells($pbal);
236 display_debit_or_credit_cells($cbal);
237 display_debit_or_credit_cells($tbal);
241 if (($pbal = round2($pbal, user_price_dec())) != 0 && $_POST['Dimension'] == 0 && $_POST['Dimension2'] == 0)
242 display_warning(_("The Opening Balance is not in balance, probably due to a non closed Previous Fiscalyear."));
245 //----------------------------------------------------------------------------------------------------