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 check_cells(_("Group totals only"), 'GroupTotalOnly', null);
64 submit_cells('Show',_("Show"),'','', 'default');
70 //----------------------------------------------------------------------------------------------------
72 function display_trial_balance($type, $typename)
74 global $path_to_root, $SysPrefs,
75 $k, $pdeb, $pcre, $cdeb, $ccre, $tdeb, $tcre, $pbal, $cbal, $tbal;
77 $printtitle = 0; //Flag for printing type name
81 //Get Accounts directly under this group/type
82 $accounts = get_gl_accounts(null, null, $type);
84 $begin = get_fiscalyear_begin_for_date($_POST['TransFromDate']);
85 if (date1_greater_date2($begin, $_POST['TransFromDate']))
86 $begin = $_POST['TransFromDate'];
87 $begin = add_days($begin, -1);
99 while ($account = db_fetch($accounts))
101 //Print Type Title if it has atleast one non-zero account
104 if (!check_value('GroupTotalOnly'))
106 start_row("class='inquirybg' style='font-weight:bold'");
107 label_cell(_("Group")." - ".$type ." - ".$typename, "colspan=8");
113 // 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.
114 // 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.
115 if (@$SysPrefs->clear_trial_balance_opening)
117 $open = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin, $begin, false, true);
118 $offset = min($open['debit'], $open['credit']);
122 $prev = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin, $_POST['TransFromDate'], false, false);
123 $curr = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $_POST['TransFromDate'], $_POST['TransToDate'], true, true);
124 $tot = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin, $_POST['TransToDate'], false, true);
125 if (check_value("NoZero") && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
127 if (!check_value('GroupTotalOnly'))
129 alt_table_row_color($k);
131 $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>";
134 label_cell($account["account_name"]);
136 if (check_value('Balance'))
138 if (!check_value('GroupTotalOnly'))
140 display_debit_or_credit_cells($prev['balance']);
141 display_debit_or_credit_cells($curr['balance']);
142 display_debit_or_credit_cells($tot['balance']);
147 if (!check_value('GroupTotalOnly'))
149 amount_cell($prev['debit']-$offset);
150 amount_cell($prev['credit']-$offset);
151 amount_cell($curr['debit']);
152 amount_cell($curr['credit']);
153 amount_cell($tot['debit']-$offset);
154 amount_cell($tot['credit']-$offset);
156 $pdeb += $prev['debit'];
157 $pcre += $prev['credit'];
158 $cdeb += $curr['debit'];
159 $ccre += $curr['credit'];
160 $tdeb += $tot['debit'];
161 $tcre += $tot['credit'];
163 $pbal += $prev['balance'];
164 $cbal += $curr['balance'];
165 $tbal += $tot['balance'];
169 //Get Account groups/types under this group/type
170 $result = get_account_types(false, false, $type);
171 while ($accounttype=db_fetch($result))
173 //Print Type Title if has sub types and not previously printed
176 start_row("class='inquirybg' style='font-weight:bold'");
177 label_cell(_("Group")." - ".$type ." - ".$typename, "colspan=8");
182 display_trial_balance($accounttype["id"], $accounttype["name"].' ('.$typename.')');
185 start_row("class='inquirybg' style='font-weight:bold'");
186 if (!check_value('GroupTotalOnly'))
187 label_cell(_("Total") ." - ".$typename, "colspan=2");
189 label_cell(" - ".$typename, "colspan=2");
192 if (!check_value('Balance'))
194 amount_cell($pdeb-$Apdeb );
195 amount_cell($pcre-$Apcre);
196 amount_cell($cdeb-$Acdeb );
197 amount_cell($ccre-$Accre );
198 amount_cell($tdeb-$Atdeb );
199 amount_cell($tcre-$Atcre);
203 display_debit_or_credit_cells($pbal-$Apbal);
204 display_debit_or_credit_cells($cbal-$Acbal );
205 display_debit_or_credit_cells($tbal-$Atbal);
210 //----------------------------------------------------------------------------------------------------
212 gl_inquiry_controls();
214 if (isset($_POST['TransFromDate']))
216 $row = get_current_fiscalyear();
217 if (date1_greater_date2($_POST['TransFromDate'], sql2date($row['end'])))
219 display_error(_("The from date cannot be bigger than the fiscal year end."));
220 set_focus('TransFromDate');
224 div_start('balance_tbl');
225 if (!isset($_POST['Dimension']))
226 $_POST['Dimension'] = 0;
227 if (!isset($_POST['Dimension2']))
228 $_POST['Dimension2'] = 0;
229 start_table(TABLESTYLE);
231 <td rowspan=2 class='tableheader'>" . _("Account") . "</td>
232 <td rowspan=2 class='tableheader'>" . _("Account Name") . "</td>
233 <td colspan=2 class='tableheader'>" . _("Brought Forward") . "</td>
234 <td colspan=2 class='tableheader'>" . _("This Period") . "</td>
235 <td colspan=2 class='tableheader'>" . _("Balance") . "</td>
237 <td class='tableheader'>" . _("Debit") . "</td>
238 <td class='tableheader'>" . _("Credit") . "</td>
239 <td class='tableheader'>" . _("Debit") . "</td>
240 <td class='tableheader'>" . _("Credit") . "</td>
241 <td class='tableheader'>" . _("Debit") . "</td>
242 <td class='tableheader'>" . _("Credit") . "</td>
247 //display_trial_balance();
249 $classresult = get_account_classes(false);
250 while ($class = db_fetch($classresult))
252 start_row("class='inquirybg' style='font-weight:bold'");
253 label_cell(_("Class")." - ".$class['cid'] ." - ".$class['class_name'], "colspan=8");
256 //Get Account groups/types under this group/type with no parents
257 $typeresult = get_account_types(false, $class['cid'], -1);
258 while ($accounttype=db_fetch($typeresult))
260 display_trial_balance($accounttype["id"], $accounttype["name"]);
264 if (!check_value('Balance'))
266 start_row("class='inquirybg' style='font-weight:bold'");
267 label_cell(_("Total") ." - ".$_POST['TransToDate'], "colspan=2");
276 start_row("class='inquirybg' style='font-weight:bold'");
277 label_cell(_("Ending Balance") ." - ".$_POST['TransToDate'], "colspan=2");
278 display_debit_or_credit_cells($pbal);
279 display_debit_or_credit_cells($cbal);
280 display_debit_or_credit_cells($tbal);
284 if (($pbal = round2($pbal, user_price_dec())) != 0 && $_POST['Dimension'] == 0 && $_POST['Dimension2'] == 0)
285 display_warning(_("The Opening Balance is not in balance, probably due to a non closed Previous Fiscalyear."));
288 //----------------------------------------------------------------------------------------------------