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 // 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 $dim = get_company_pref('use_dimension');
68 $dimension = $dimension2 = 0;
70 $from = $_POST['PARAM_0'];
71 $to = $_POST['PARAM_1'];
72 $zero = $_POST['PARAM_2'];
73 $balances = $_POST['PARAM_3'];
76 $dimension = $_POST['PARAM_4'];
77 $dimension2 = $_POST['PARAM_5'];
78 $comments = $_POST['PARAM_6'];
79 $destination = $_POST['PARAM_7'];
83 $dimension = $_POST['PARAM_4'];
84 $comments = $_POST['PARAM_5'];
85 $destination = $_POST['PARAM_6'];
89 $comments = $_POST['PARAM_4'];
90 $destination = $_POST['PARAM_5'];
94 include_once($path_to_root . "/reporting/includes/excel_report.inc");
95 $filename = "TrialBalance.xml";
99 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
100 $filename = "TrialBalance.pdf";
102 $dec = user_price_dec();
104 //$cols2 = array(0, 50, 230, 330, 430, 530);
105 $cols2 = array(0, 50, 190, 310, 430, 530);
106 //-------------0--1---2----3----4----5--
108 $headers2 = array('', '', _('Brought Forward'), _('This Period'), _('Balance'));
110 $aligns2 = array('left', 'left', 'left', 'left', 'left');
112 //$cols = array(0, 50, 200, 250, 300, 350, 400, 450, 500, 550);
113 $cols = array(0, 50, 150, 210, 270, 330, 390, 450, 510, 570);
114 //------------0--1---2----3----4----5----6----7----8----9--
116 $headers = array(_('Account'), _('Account Name'), _('Debit'), _('Credit'), _('Debit'),
117 _('Credit'), _('Debit'), _('Credit'));
119 $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'right', 'right');
123 $params = array( 0 => $comments,
124 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
125 2 => array('text' => _('Dimension')." 1",
126 'from' => get_dimension_string($dimension), 'to' => ''),
127 3 => array('text' => _('Dimension')." 2",
128 'from' => get_dimension_string($dimension2), 'to' => ''));
132 $params = array( 0 => $comments,
133 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
134 2 => array('text' => _('Dimension'),
135 'from' => get_dimension_string($dimension), 'to' => ''));
139 $params = array( 0 => $comments,
140 1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
143 $rep = new FrontReport(_('Trial Balance'), $filename, user_pagesize());
146 $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
149 $accounts = get_gl_accounts();
151 $pdeb = $pcre = $cdeb = $ccre = $tdeb = $tcre = $pbal = $cbal = $tbal = 0;
152 $begin = begin_fiscalyear();
153 if (date1_greater_date2($begin, $from))
155 $begin = add_days($begin, -1);
156 while ($account=db_fetch($accounts))
158 $prev = get_balance($account["account_code"], $dimension, $dimension2, $begin, $from, false, false);
159 $curr = get_balance($account["account_code"], $dimension, $dimension2, $from, $to, true, true);
160 $tot = get_balance($account["account_code"], $dimension, $dimension2, $begin, $to, false, true);
162 if ($zero == 0 && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
164 $rep->TextCol(0, 1, $account['account_code']);
165 $rep->TextCol(1, 2, $account['account_name']);
168 if ($prev['balance'] >= 0.0)
169 $rep->AmountCol(2, 3, $prev['balance'], $dec);
171 $rep->AmountCol(3, 4, abs($prev['balance']), $dec);
172 if ($curr['balance'] >= 0.0)
173 $rep->AmountCol(4, 5, $curr['balance'], $dec);
175 $rep->AmountCol(5, 6, abs($curr['balance']), $dec);
176 if ($tot['balance'] >= 0.0)
177 $rep->AmountCol(6, 7, $tot['balance'], $dec);
179 $rep->AmountCol(7, 8, abs($tot['balance']), $dec);
183 $rep->AmountCol(2, 3, $prev['debit'], $dec);
184 $rep->AmountCol(3, 4, $prev['credit'], $dec);
185 $rep->AmountCol(4, 5, $curr['debit'], $dec);
186 $rep->AmountCol(5, 6, $curr['credit'], $dec);
187 $rep->AmountCol(6, 7, $tot['debit'], $dec);
188 $rep->AmountCol(7, 8, $tot['credit'], $dec);
189 $pdeb += $prev['debit'];
190 $pcre += $prev['credit'];
191 $cdeb += $curr['debit'];
192 $ccre += $curr['credit'];
193 $tdeb += $tot['debit'];
194 $tcre += $tot['credit'];
197 $pbal += $prev['balance'];
198 $cbal += $curr['balance'];
199 $tbal += $tot['balance'];
202 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
204 $rep->Line($rep->row - 2);
208 $rep->Line($rep->row);
212 //$prev = get_balance(null, $dimension, $dimension2, $begin, $from, false, false);
213 //$curr = get_balance(null, $dimension, $dimension2, $from, $to, true, true);
214 //$tot = get_balance(null, $dimension, $dimension2, $begin, $to, false, true);
218 $rep->TextCol(0, 2, _("Total"));
219 $rep->AmountCol(2, 3, $pdeb, $dec);
220 $rep->AmountCol(3, 4, $pcre, $dec);
221 $rep->AmountCol(4, 5, $cdeb, $dec);
222 $rep->AmountCol(5, 6, $ccre, $dec);
223 $rep->AmountCol(6, 7, $tdeb, $dec);
224 $rep->AmountCol(7, 8, $tcre, $dec);
227 $rep->TextCol(0, 2, _("Ending Balance"));
230 $rep->AmountCol(2, 3, $pbal, $dec);
232 $rep->AmountCol(3, 4, abs($pbal), $dec);
234 $rep->AmountCol(4, 5, $cbal, $dec);
236 $rep->AmountCol(5, 6, abs($cbal), $dec);
238 $rep->AmountCol(6, 7, $tbal, $dec);
240 $rep->AmountCol(7, 8, abs($tbal), $dec);
243 $rep->Line($rep->row);