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 // ----------------------------------------------------------------
17 // Title: Trial Balance
18 // ----------------------------------------------------------------
21 include_once($path_to_root . "/includes/session.inc");
22 include_once($path_to_root . "/admin/db/fiscalyears_db.inc");
23 include_once($path_to_root . "/includes/ui.inc");
24 include_once($path_to_root . "/includes/date_functions.inc");
25 include_once($path_to_root . "/includes/data_checks.inc");
26 include_once($path_to_root . "/gl/includes/gl_db.inc");
28 $pdeb = $pcre = $cdeb = $ccre = $tdeb = $tcre = $pbal = $cbal = $tbal = 0;
30 //----------------------------------------------------------------------------------------------------
32 function display_type ($type, $typename, &$dec, &$rep, $from, $to, $zero, $balances, $dimension, $dimension2)
34 global $pdeb, $pcre, $cdeb, $ccre, $tdeb, $tcre, $pbal, $cbal, $tbal;
36 $printtitle = 0; //Flag for printing type name
38 //Get Accounts directly under this group/type
39 $accounts = get_gl_accounts(null, null, $type);
41 $begin = get_fiscalyear_begin_for_date($from);
42 if (date1_greater_date2($begin, $from))
44 $begin = add_days($begin, -1);
45 while ($account=db_fetch($accounts))
47 //Print Type Title if it has atleast one non-zero account
51 $rep->TextCol(0, 8, _("Group")." - ".$type ." - ".$typename);
54 $rep->Line($rep->row);
58 $prev = get_balance($account["account_code"], $dimension, $dimension2, $begin, $from, false, false);
59 $curr = get_balance($account["account_code"], $dimension, $dimension2, $from, $to, true, true);
60 $tot = get_balance($account["account_code"], $dimension, $dimension2, $begin, $to, false, true);
62 if ($zero == 0 && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
64 $rep->TextCol(0, 1, $account['account_code']);
65 $rep->TextCol(1, 2, $account['account_name']);
68 if ($prev['balance'] >= 0.0)
69 $rep->AmountCol(2, 3, $prev['balance'], $dec);
71 $rep->AmountCol(3, 4, abs($prev['balance']), $dec);
72 if ($curr['balance'] >= 0.0)
73 $rep->AmountCol(4, 5, $curr['balance'], $dec);
75 $rep->AmountCol(5, 6, abs($curr['balance']), $dec);
76 if ($tot['balance'] >= 0.0)
77 $rep->AmountCol(6, 7, $tot['balance'], $dec);
79 $rep->AmountCol(7, 8, abs($tot['balance']), $dec);
83 $rep->AmountCol(2, 3, $prev['debit'], $dec);
84 $rep->AmountCol(3, 4, $prev['credit'], $dec);
85 $rep->AmountCol(4, 5, $curr['debit'], $dec);
86 $rep->AmountCol(5, 6, $curr['credit'], $dec);
87 $rep->AmountCol(6, 7, $tot['debit'], $dec);
88 $rep->AmountCol(7, 8, $tot['credit'], $dec);
89 $pdeb += $prev['debit'];
90 $pcre += $prev['credit'];
91 $cdeb += $curr['debit'];
92 $ccre += $curr['credit'];
93 $tdeb += $tot['debit'];
94 $tcre += $tot['credit'];
97 $pbal += $prev['balance'];
98 $cbal += $curr['balance'];
99 $tbal += $tot['balance'];
102 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
104 $rep->Line($rep->row - 2);
109 //Get Account groups/types under this group/type
110 $result = get_account_types(false, false, $type);
111 while ($accounttype=db_fetch($result))
113 //Print Type Title if has sub types and not previously printed
117 $rep->TextCol(0, 8, _("Group")." - ".$type ." - ".$typename);
120 $rep->Line($rep->row);
123 display_type($accounttype["id"], $accounttype["name"].' ('.$typename.')', $dec, $rep, $from, $to, $zero, $balances, $dimension, $dimension2);
127 //----------------------------------------------------------------------------------------------------
129 print_trial_balance();
131 //----------------------------------------------------------------------------------------------------
133 function print_trial_balance()
135 global $path_to_root;
136 global $pdeb, $pcre, $cdeb, $ccre, $tdeb, $tcre, $pbal, $cbal, $tbal;
138 $dim = get_company_pref('use_dimension');
139 $dimension = $dimension2 = 0;
141 $from = $_POST['PARAM_0'];
142 $to = $_POST['PARAM_1'];
143 $zero = $_POST['PARAM_2'];
144 $balances = $_POST['PARAM_3'];
147 $dimension = $_POST['PARAM_4'];
148 $dimension2 = $_POST['PARAM_5'];
149 $comments = $_POST['PARAM_6'];
150 $orientation = $_POST['PARAM_7'];
151 $destination = $_POST['PARAM_8'];
155 $dimension = $_POST['PARAM_4'];
156 $comments = $_POST['PARAM_5'];
157 $orientation = $_POST['PARAM_6'];
158 $destination = $_POST['PARAM_7'];
162 $comments = $_POST['PARAM_4'];
163 $orientation = $_POST['PARAM_5'];
164 $destination = $_POST['PARAM_6'];
167 include_once($path_to_root . "/reporting/includes/excel_report.inc");
169 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
170 $orientation = ($orientation ? 'L' : 'P');
171 $dec = user_price_dec();
173 $cols2 = array(0, 50, 190, 310, 430, 530);
174 //-------------0--1---2----3----4----5--
176 $headers2 = array('', '', _('Brought Forward'), _('This Period'), _('Balance'));
178 $aligns2 = array('left', 'left', 'left', 'left', 'left');
180 $cols = array(0, 50, 150, 210, 270, 330, 390, 450, 510, 570);
181 //------------0--1---2----3----4----5----6----7----8--
183 $headers = array(_('Account'), _('Account Name'), _('Debit'), _('Credit'), _('Debit'),
184 _('Credit'), _('Debit'), _('Credit'));
186 $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'right', 'right');
190 $params = array( 0 => $comments,
191 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
192 2 => array('text' => _('Dimension')." 1",
193 'from' => get_dimension_string($dimension), 'to' => ''),
194 3 => array('text' => _('Dimension')." 2",
195 'from' => get_dimension_string($dimension2), 'to' => ''));
199 $params = array( 0 => $comments,
200 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
201 2 => array('text' => _('Dimension'),
202 'from' => get_dimension_string($dimension), 'to' => ''));
206 $params = array( 0 => $comments,
207 1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
210 $rep = new FrontReport(_('Trial Balance'), "TrialBalance", user_pagesize(), 9, $orientation);
211 if ($orientation == 'L')
213 $rep->recalculate_cols($cols);
214 $rep->recalculate_cols($cols2);
217 $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
220 $classresult = get_account_classes(false);
221 while ($class = db_fetch($classresult))
224 $rep->TextCol(0, 1, $class['cid']);
225 $rep->TextCol(1, 4, $class['class_name']);
229 //Get Account groups/types under this group/type with no parents
230 $typeresult = get_account_types(false, $class['cid'], -1);
231 while ($accounttype=db_fetch($typeresult))
233 display_type($accounttype["id"], $accounttype["name"], $dec, $rep, $from, $to, $zero, $balances, $dimension, $dimension2);
237 $rep->Line($rep->row);
243 $rep->TextCol(0, 2, _("Total"));
244 $rep->AmountCol(2, 3, $pdeb, $dec);
245 $rep->AmountCol(3, 4, $pcre, $dec);
246 $rep->AmountCol(4, 5, $cdeb, $dec);
247 $rep->AmountCol(5, 6, $ccre, $dec);
248 $rep->AmountCol(6, 7, $tdeb, $dec);
249 $rep->AmountCol(7, 8, $tcre, $dec);
252 $rep->TextCol(0, 2, _("Ending Balance"));
255 $rep->AmountCol(2, 3, $pbal, $dec);
257 $rep->AmountCol(3, 4, abs($pbal), $dec);
259 $rep->AmountCol(4, 5, $cbal, $dec);
261 $rep->AmountCol(5, 6, abs($cbal), $dec);
263 $rep->AmountCol(6, 7, $tbal, $dec);
265 $rep->AmountCol(7, 8, abs($tbal), $dec);
268 $rep->Line($rep->row + 10);
269 if (($pbal = round2($pbal, $dec)) != 0.0 && $dimension == 0 && $dimension2 == 0)
273 $rep->TextCol(0, 8, _("The Opening Balance is not in balance, probably due to a non closed Previous Fiscalyear."));