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 ***********************************************************************/
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 print_trial_balance();
30 //----------------------------------------------------------------------------------------------------
31 function get_balance($account, $dimension, $dimension2, $from, $to, $from_incl=true, $to_incl=true)
33 $sql = "SELECT SUM(IF(amount >= 0, amount, 0)) as debit, SUM(IF(amount < 0, -amount, 0)) as credit, SUM(amount) as balance
34 FROM ".TB_PREF."gl_trans,".TB_PREF."chart_master,".TB_PREF."chart_types, ".TB_PREF."chart_class
35 WHERE ".TB_PREF."gl_trans.account=".TB_PREF."chart_master.account_code AND ".TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id
36 AND ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid AND";
39 $sql .= " account='$account' AND";
41 $sql .= " dimension_id=$dimension AND";
43 $sql .= " dimension2_id=$dimension2 AND";
44 $from_date = date2sql($from);
46 $sql .= " tran_date >= '$from_date' AND";
48 $sql .= " tran_date > IF(".TB_PREF."chart_class.balance_sheet=1, '0000-00-00', '$from_date') AND";
49 $to_date = date2sql($to);
51 $sql .= " tran_date <= '$to_date' ";
53 $sql .= " tran_date < '$to_date' ";
55 $result = db_query($sql,"No general ledger accounts were returned");
57 return db_fetch($result);
60 //----------------------------------------------------------------------------------------------------
62 function print_trial_balance()
66 $dim = get_company_pref('use_dimension');
67 $dimension = $dimension2 = 0;
69 $from = $_POST['PARAM_0'];
70 $to = $_POST['PARAM_1'];
71 $zero = $_POST['PARAM_2'];
72 $balances = $_POST['PARAM_3'];
75 $dimension = $_POST['PARAM_4'];
76 $dimension2 = $_POST['PARAM_5'];
77 $comments = $_POST['PARAM_6'];
78 $destination = $_POST['PARAM_7'];
82 $dimension = $_POST['PARAM_4'];
83 $comments = $_POST['PARAM_5'];
84 $destination = $_POST['PARAM_6'];
88 $comments = $_POST['PARAM_4'];
89 $destination = $_POST['PARAM_5'];
92 include_once($path_to_root . "/reporting/includes/excel_report.inc");
94 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
95 $dec = user_price_dec();
97 //$cols2 = array(0, 50, 230, 330, 430, 530);
98 $cols2 = array(0, 50, 190, 310, 430, 530);
99 //-------------0--1---2----3----4----5--
101 $headers2 = array('', '', _('Brought Forward'), _('This Period'), _('Balance'));
103 $aligns2 = array('left', 'left', 'left', 'left', 'left');
105 //$cols = array(0, 50, 200, 250, 300, 350, 400, 450, 500, 550);
106 $cols = array(0, 50, 150, 210, 270, 330, 390, 450, 510, 570);
107 //------------0--1---2----3----4----5----6----7----8----9--
109 $headers = array(_('Account'), _('Account Name'), _('Debit'), _('Credit'), _('Debit'),
110 _('Credit'), _('Debit'), _('Credit'));
112 $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'right', 'right');
116 $params = array( 0 => $comments,
117 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
118 2 => array('text' => _('Dimension')." 1",
119 'from' => get_dimension_string($dimension), 'to' => ''),
120 3 => array('text' => _('Dimension')." 2",
121 'from' => get_dimension_string($dimension2), 'to' => ''));
125 $params = array( 0 => $comments,
126 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
127 2 => array('text' => _('Dimension'),
128 'from' => get_dimension_string($dimension), 'to' => ''));
132 $params = array( 0 => $comments,
133 1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
136 $rep = new FrontReport(_('Trial Balance'), "TrialBalance", user_pagesize());
139 $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
142 $accounts = get_gl_accounts();
144 $pdeb = $pcre = $cdeb = $ccre = $tdeb = $tcre = $pbal = $cbal = $tbal = 0;
145 $begin = begin_fiscalyear();
146 if (date1_greater_date2($begin, $from))
148 $begin = add_days($begin, -1);
149 while ($account=db_fetch($accounts))
151 $prev = get_balance($account["account_code"], $dimension, $dimension2, $begin, $from, false, false);
152 $curr = get_balance($account["account_code"], $dimension, $dimension2, $from, $to, true, true);
153 $tot = get_balance($account["account_code"], $dimension, $dimension2, $begin, $to, false, true);
155 if ($zero == 0 && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
157 $rep->TextCol(0, 1, $account['account_code']);
158 $rep->TextCol(1, 2, $account['account_name']);
161 if ($prev['balance'] >= 0.0)
162 $rep->AmountCol(2, 3, $prev['balance'], $dec);
164 $rep->AmountCol(3, 4, abs($prev['balance']), $dec);
165 if ($curr['balance'] >= 0.0)
166 $rep->AmountCol(4, 5, $curr['balance'], $dec);
168 $rep->AmountCol(5, 6, abs($curr['balance']), $dec);
169 if ($tot['balance'] >= 0.0)
170 $rep->AmountCol(6, 7, $tot['balance'], $dec);
172 $rep->AmountCol(7, 8, abs($tot['balance']), $dec);
176 $rep->AmountCol(2, 3, $prev['debit'], $dec);
177 $rep->AmountCol(3, 4, $prev['credit'], $dec);
178 $rep->AmountCol(4, 5, $curr['debit'], $dec);
179 $rep->AmountCol(5, 6, $curr['credit'], $dec);
180 $rep->AmountCol(6, 7, $tot['debit'], $dec);
181 $rep->AmountCol(7, 8, $tot['credit'], $dec);
182 $pdeb += $prev['debit'];
183 $pcre += $prev['credit'];
184 $cdeb += $curr['debit'];
185 $ccre += $curr['credit'];
186 $tdeb += $tot['debit'];
187 $tcre += $tot['credit'];
190 $pbal += $prev['balance'];
191 $cbal += $curr['balance'];
192 $tbal += $tot['balance'];
195 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
197 $rep->Line($rep->row - 2);
201 $rep->Line($rep->row);
205 //$prev = get_balance(null, $dimension, $dimension2, $begin, $from, false, false);
206 //$curr = get_balance(null, $dimension, $dimension2, $from, $to, true, true);
207 //$tot = get_balance(null, $dimension, $dimension2, $begin, $to, false, true);
211 $rep->TextCol(0, 2, _("Total"));
212 $rep->AmountCol(2, 3, $pdeb, $dec);
213 $rep->AmountCol(3, 4, $pcre, $dec);
214 $rep->AmountCol(4, 5, $cdeb, $dec);
215 $rep->AmountCol(5, 6, $ccre, $dec);
216 $rep->AmountCol(6, 7, $tdeb, $dec);
217 $rep->AmountCol(7, 8, $tcre, $dec);
220 $rep->TextCol(0, 2, _("Ending Balance"));
223 $rep->AmountCol(2, 3, $pbal, $dec);
225 $rep->AmountCol(3, 4, abs($pbal), $dec);
227 $rep->AmountCol(4, 5, $cbal, $dec);
229 $rep->AmountCol(5, 6, abs($cbal), $dec);
231 $rep->AmountCol(6, 7, $tbal, $dec);
233 $rep->AmountCol(7, 8, abs($tbal), $dec);
236 $rep->Line($rep->row);