2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU Affero General Public License,
5 AGPL, as published by the Free Software Foundation, either version
6 3 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/agpl-3.0.html>.
11 ***********************************************************************/
13 // ----------------------------------------------------------------
17 // Title: Trial Balance
18 // ----------------------------------------------------------------
21 include_once($path_to_root . "/includes/session.inc");
22 include_once($path_to_root . "/includes/date_functions.inc");
23 include_once($path_to_root . "/includes/data_checks.inc");
24 include_once($path_to_root . "/gl/includes/gl_db.inc");
26 //----------------------------------------------------------------------------------------------------
28 // trial_inquiry_controls();
29 print_trial_balance();
31 //----------------------------------------------------------------------------------------------------
32 function get_balance($account, $dimension, $dimension2, $from, $to, $from_incl=true, $to_incl=true)
34 $sql = "SELECT SUM(IF(amount >= 0, amount, 0)) as debit, SUM(IF(amount < 0, -amount, 0)) as credit, SUM(amount) as balance
35 FROM ".TB_PREF."gl_trans,".TB_PREF."chart_master,".TB_PREF."chart_types, ".TB_PREF."chart_class
36 WHERE ".TB_PREF."gl_trans.account=".TB_PREF."chart_master.account_code AND ".TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id
37 AND ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid AND";
40 $sql .= " account='$account' AND";
42 $sql .= " dimension_id=$dimension AND";
44 $sql .= " dimension2_id=$dimension2 AND";
45 $from_date = date2sql($from);
47 $sql .= " tran_date >= '$from_date' AND";
49 $sql .= " tran_date > IF(".TB_PREF."chart_class.balance_sheet=1, '0000-00-00', '$from_date') AND";
50 $to_date = date2sql($to);
52 $sql .= " tran_date <= '$to_date' ";
54 $sql .= " tran_date < '$to_date' ";
56 $result = db_query($sql,"No general ledger accounts were returned");
58 return db_fetch($result);
61 //----------------------------------------------------------------------------------------------------
63 function print_trial_balance()
67 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
68 $dim = get_company_pref('use_dimension');
69 $dimension = $dimension2 = 0;
71 $from = $_POST['PARAM_0'];
72 $to = $_POST['PARAM_1'];
73 $zero = $_POST['PARAM_2'];
74 $balances = $_POST['PARAM_3'];
77 $dimension = $_POST['PARAM_4'];
78 $dimension2 = $_POST['PARAM_5'];
79 $comments = $_POST['PARAM_6'];
83 $dimension = $_POST['PARAM_4'];
84 $comments = $_POST['PARAM_5'];
88 $comments = $_POST['PARAM_4'];
90 $dec = user_price_dec();
92 //$cols2 = array(0, 50, 230, 330, 430, 530);
93 $cols2 = array(0, 50, 190, 310, 430, 530);
94 //-------------0--1---2----3----4----5--
96 $headers2 = array('', '', _('Brought Forward'), _('This Period'), _('Balance'));
98 $aligns2 = array('left', 'left', 'left', 'left', 'left');
100 //$cols = array(0, 50, 200, 250, 300, 350, 400, 450, 500, 550);
101 $cols = array(0, 50, 150, 210, 270, 330, 390, 450, 510, 570);
102 //------------0--1---2----3----4----5----6----7----8----9--
104 $headers = array(_('Account'), _('Account Name'), _('Debit'), _('Credit'), _('Debit'),
105 _('Credit'), _('Debit'), _('Credit'));
107 $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'right', 'right');
111 $params = array( 0 => $comments,
112 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
113 2 => array('text' => _('Dimension')." 1",
114 'from' => get_dimension_string($dimension), 'to' => ''),
115 3 => array('text' => _('Dimension')." 2",
116 'from' => get_dimension_string($dimension2), 'to' => ''));
120 $params = array( 0 => $comments,
121 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
122 2 => array('text' => _('Dimension'),
123 'from' => get_dimension_string($dimension), 'to' => ''));
127 $params = array( 0 => $comments,
128 1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
131 $rep = new FrontReport(_('Trial Balance'), "TrialBalance.pdf", user_pagesize());
134 $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
137 $accounts = get_gl_accounts();
139 $pdeb = $pcre = $cdeb = $ccre = $tdeb = $tcre = $pbal = $cbal = $tbal = 0;
140 $begin = begin_fiscalyear();
141 if (date1_greater_date2($begin, $from))
143 $begin = add_days($begin, -1);
144 while ($account=db_fetch($accounts))
146 $prev = get_balance($account["account_code"], $dimension, $dimension2, $begin, $from, false, false);
147 $curr = get_balance($account["account_code"], $dimension, $dimension2, $from, $to, true, true);
148 $tot = get_balance($account["account_code"], $dimension, $dimension2, $begin, $to, false, true);
150 if ($zero == 0 && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
152 $rep->TextCol(0, 1, $account['account_code']);
153 $rep->TextCol(1, 2, $account['account_name']);
156 if ($prev['balance'] >= 0.0)
157 $rep->TextCol(2, 3, number_format2($prev['balance'], $dec));
159 $rep->TextCol(3, 4, number_format2(abs($prev['balance']), $dec));
160 if ($curr['balance'] >= 0.0)
161 $rep->TextCol(4, 5, number_format2($curr['balance'], $dec));
163 $rep->TextCol(5, 6, number_format2(abs($curr['balance']), $dec));
164 if ($tot['balance'] >= 0.0)
165 $rep->TextCol(6, 7, number_format2($tot['balance'], $dec));
167 $rep->TextCol(7, 8, number_format2(abs($tot['balance']), $dec));
171 $rep->TextCol(2, 3, number_format2($prev['debit'], $dec));
172 $rep->TextCol(3, 4, number_format2($prev['credit'], $dec));
173 $rep->TextCol(4, 5, number_format2($curr['debit'], $dec));
174 $rep->TextCol(5, 6, number_format2($curr['credit'], $dec));
175 $rep->TextCol(6, 7, number_format2($tot['debit'], $dec));
176 $rep->TextCol(7, 8, number_format2($tot['credit'], $dec));
177 $pdeb += $prev['debit'];
178 $pcre += $prev['credit'];
179 $cdeb += $curr['debit'];
180 $ccre += $curr['credit'];
181 $tdeb += $tot['debit'];
182 $tcre += $tot['credit'];
185 $pbal += $prev['balance'];
186 $cbal += $curr['balance'];
187 $tbal += $tot['balance'];
190 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
192 $rep->Line($rep->row - 2);
196 $rep->Line($rep->row);
200 //$prev = get_balance(null, $dimension, $dimension2, $begin, $from, false, false);
201 //$curr = get_balance(null, $dimension, $dimension2, $from, $to, true, true);
202 //$tot = get_balance(null, $dimension, $dimension2, $begin, $to, false, true);
206 $rep->TextCol(0, 2, _("Total"));
207 $rep->TextCol(2, 3, number_format2($pdeb, $dec));
208 $rep->TextCol(3, 4, number_format2($pcre, $dec));
209 $rep->TextCol(4, 5, number_format2($cdeb, $dec));
210 $rep->TextCol(5, 6, number_format2($ccre, $dec));
211 $rep->TextCol(6, 7, number_format2($tdeb, $dec));
212 $rep->TextCol(7, 8, number_format2($tcre, $dec));
215 $rep->TextCol(0, 2, _("Ending Balance"));
218 $rep->TextCol(2, 3, number_format2($pbal, $dec));
220 $rep->TextCol(3, 4, number_format2(abs($pbal), $dec));
222 $rep->TextCol(4, 5, number_format2($cbal, $dec));
224 $rep->TextCol(5, 6, number_format2(abs($cbal), $dec));
226 $rep->TextCol(6, 7, number_format2($tbal, $dec));
228 $rep->TextCol(7, 8, number_format2(abs($tbal), $dec));
230 $rep->Line($rep->row - 6);