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");
25 if (user_use_date_picker())
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['TransToDate']))
51 $_POST['TransToDate'] = end_month($date);
52 if (!isset($_POST['TransFromDate']))
53 $_POST['TransFromDate'] = add_days(end_month($date), -user_transaction_days());
55 date_cells(_("From:"), 'TransFromDate');
56 date_cells(_("To:"), 'TransToDate');
58 dimensions_list_cells(_("Dimension")." 1:", 'Dimension', null, true, " ", false, 1);
60 dimensions_list_cells(_("Dimension")." 2:", 'Dimension2', null, true, " ", false, 2);
61 check_cells(_("No zero values"), 'NoZero', null);
62 check_cells(_("Only balances"), 'Balance', null);
63 submit_cells('Show',_("Show"),'','', 'default');
69 //----------------------------------------------------------------------------------------------------
71 function display_trial_balance($type, $typename)
73 global $path_to_root, $SysPrefs,
74 $k, $pdeb, $pcre, $cdeb, $ccre, $tdeb, $tcre, $pbal, $cbal, $tbal;
76 $printtitle = 0; //Flag for printing type name
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 if (date1_greater_date2($begin, $_POST['TransFromDate']))
85 $begin = $_POST['TransFromDate'];
86 $begin = add_days($begin, -1);
98 while ($account = db_fetch($accounts))
100 //Print Type Title if it has atleast one non-zero account
103 start_row("class='inquirybg' style='font-weight:bold'");
104 label_cell(_("Group")." - ".$type ." - ".$typename, "colspan=8");
109 // 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.
110 // 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.
111 if (@$SysPrefs->clear_trial_balance_opening)
113 $open = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin, $begin, false, true);
114 $offset = min($open['debit'], $open['credit']);
118 $prev = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin, $_POST['TransFromDate'], false, false);
119 $curr = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $_POST['TransFromDate'], $_POST['TransToDate'], true, true);
120 $tot = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin, $_POST['TransToDate'], false, true);
121 if (check_value("NoZero") && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
123 alt_table_row_color($k);
125 $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>";
128 label_cell($account["account_name"]);
129 if (check_value('Balance'))
131 display_debit_or_credit_cells($prev['balance']);
132 display_debit_or_credit_cells($curr['balance']);
133 display_debit_or_credit_cells($tot['balance']);
138 amount_cell($prev['debit']-$offset);
139 amount_cell($prev['credit']-$offset);
140 amount_cell($curr['debit']);
141 amount_cell($curr['credit']);
142 amount_cell($tot['debit']-$offset);
143 amount_cell($tot['credit']-$offset);
144 $pdeb += $prev['debit'];
145 $pcre += $prev['credit'];
146 $cdeb += $curr['debit'];
147 $ccre += $curr['credit'];
148 $tdeb += $tot['debit'];
149 $tcre += $tot['credit'];
151 $pbal += $prev['balance'];
152 $cbal += $curr['balance'];
153 $tbal += $tot['balance'];
157 //Get Account groups/types under this group/type
158 $result = get_account_types(false, false, $type);
159 while ($accounttype=db_fetch($result))
161 //Print Type Title if has sub types and not previously printed
164 start_row("class='inquirybg' style='font-weight:bold'");
165 label_cell(_("Group")." - ".$type ." - ".$typename, "colspan=8");
170 display_trial_balance($accounttype["id"], $accounttype["name"].' ('.$typename.')');
173 start_row("class='inquirybg' style='font-weight:bold'");
174 label_cell(_("Total") ." - ".$typename, "colspan=2");
176 if (!check_value('Balance'))
178 amount_cell($pdeb-$Apdeb );
179 amount_cell($pcre-$Apcre);
180 amount_cell($cdeb-$Acdeb );
181 amount_cell($ccre-$Accre );
182 amount_cell($tdeb-$Atdeb );
183 amount_cell($tcre-$Atcre);
187 display_debit_or_credit_cells($pbal-$Apbal);
188 display_debit_or_credit_cells($cbal-$Acbal );
189 display_debit_or_credit_cells($tbal-$Atbal);
194 //----------------------------------------------------------------------------------------------------
196 gl_inquiry_controls();
198 if (isset($_POST['TransFromDate']))
200 $row = get_current_fiscalyear();
201 if (date1_greater_date2($_POST['TransFromDate'], sql2date($row['end'])))
203 display_error(_("The from date cannot be bigger than the fiscal year end."));
204 set_focus('TransFromDate');
208 div_start('balance_tbl');
209 if (!isset($_POST['Dimension']))
210 $_POST['Dimension'] = 0;
211 if (!isset($_POST['Dimension2']))
212 $_POST['Dimension2'] = 0;
213 start_table(TABLESTYLE);
215 <td rowspan=2 class='tableheader'>" . _("Account") . "</td>
216 <td rowspan=2 class='tableheader'>" . _("Account Name") . "</td>
217 <td colspan=2 class='tableheader'>" . _("Brought Forward") . "</td>
218 <td colspan=2 class='tableheader'>" . _("This Period") . "</td>
219 <td colspan=2 class='tableheader'>" . _("Balance") . "</td>
221 <td class='tableheader'>" . _("Debit") . "</td>
222 <td class='tableheader'>" . _("Credit") . "</td>
223 <td class='tableheader'>" . _("Debit") . "</td>
224 <td class='tableheader'>" . _("Credit") . "</td>
225 <td class='tableheader'>" . _("Debit") . "</td>
226 <td class='tableheader'>" . _("Credit") . "</td>
231 //display_trial_balance();
233 $classresult = get_account_classes(false);
234 while ($class = db_fetch($classresult))
236 start_row("class='inquirybg' style='font-weight:bold'");
237 label_cell(_("Class")." - ".$class['cid'] ." - ".$class['class_name'], "colspan=8");
240 //Get Account groups/types under this group/type with no parents
241 $typeresult = get_account_types(false, $class['cid'], -1);
242 while ($accounttype=db_fetch($typeresult))
244 display_trial_balance($accounttype["id"], $accounttype["name"]);
248 if (!check_value('Balance'))
250 start_row("class='inquirybg' style='font-weight:bold'");
251 label_cell(_("Total") ." - ".$_POST['TransToDate'], "colspan=2");
260 start_row("class='inquirybg' style='font-weight:bold'");
261 label_cell(_("Ending Balance") ." - ".$_POST['TransToDate'], "colspan=2");
262 display_debit_or_credit_cells($pbal);
263 display_debit_or_credit_cells($cbal);
264 display_debit_or_credit_cells($tbal);
268 if (($pbal = round2($pbal, user_price_dec())) != 0 && $_POST['Dimension'] == 0 && $_POST['Dimension2'] == 0)
269 display_warning(_("The Opening Balance is not in balance, probably due to a non closed Previous Fiscalyear."));
272 //----------------------------------------------------------------------------------------------------