4 // ----------------------------------------------------------------
8 // Title: Trial Balance
9 // ----------------------------------------------------------------
12 include_once($path_to_root . "includes/session.inc");
13 include_once($path_to_root . "includes/date_functions.inc");
14 include_once($path_to_root . "includes/data_checks.inc");
15 include_once($path_to_root . "gl/includes/gl_db.inc");
17 //----------------------------------------------------------------------------------------------------
19 // trial_inquiry_controls();
20 print_trial_balance();
22 //----------------------------------------------------------------------------------------------------
23 function get_balance($account, $dimension, $dimension2, $from, $to, $from_incl=true, $to_incl=true)
25 $sql = "SELECT SUM(IF(amount >= 0, amount, 0)) as debit, SUM(IF(amount < 0, -amount, 0)) as credit, SUM(amount) as balance
26 FROM ".TB_PREF."gl_trans,".TB_PREF."chart_master,".TB_PREF."chart_types, ".TB_PREF."chart_class
27 WHERE ".TB_PREF."gl_trans.account=".TB_PREF."chart_master.account_code AND ".TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id
28 AND ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid AND";
31 $sql .= " account='$account' AND";
33 $sql .= " dimension_id=$dimension AND";
35 $sql .= " dimension2_id=$dimension2 AND";
36 $from_date = date2sql($from);
38 $sql .= " tran_date >= '$from_date' AND";
40 $sql .= " tran_date > IF(".TB_PREF."chart_class.balance_sheet=1, '0000-00-00', '$from_date') AND";
41 $to_date = date2sql($to);
43 $sql .= " tran_date <= '$to_date' ";
45 $sql .= " tran_date < '$to_date' ";
47 $result = db_query($sql,"No general ledger accounts were returned");
49 return db_fetch($result);
52 //----------------------------------------------------------------------------------------------------
54 function print_trial_balance()
58 include_once($path_to_root . "reporting/includes/pdf_report.inc");
59 $dim = get_company_pref('use_dimension');
60 $dimension = $dimension2 = 0;
62 $from = $_POST['PARAM_0'];
63 $to = $_POST['PARAM_1'];
64 $zero = $_POST['PARAM_2'];
65 $balances = $_POST['PARAM_3'];
68 $dimension = $_POST['PARAM_4'];
69 $dimension2 = $_POST['PARAM_5'];
70 $comments = $_POST['PARAM_6'];
74 $dimension = $_POST['PARAM_4'];
75 $comments = $_POST['PARAM_5'];
79 $comments = $_POST['PARAM_4'];
81 $dec = user_price_dec();
83 //$cols2 = array(0, 50, 230, 330, 430, 530);
84 $cols2 = array(0, 50, 190, 310, 430, 530);
85 //-------------0--1---2----3----4----5--
87 $headers2 = array('', '', _('Brought Forward'), _('This Period'), _('Balance'));
89 $aligns2 = array('left', 'left', 'left', 'left', 'left');
91 //$cols = array(0, 50, 200, 250, 300, 350, 400, 450, 500, 550);
92 $cols = array(0, 50, 150, 210, 270, 330, 390, 450, 510, 570);
93 //------------0--1---2----3----4----5----6----7----8----9--
95 $headers = array(_('Account'), _('Account Name'), _('Debit'), _('Credit'), _('Debit'),
96 _('Credit'), _('Debit'), _('Credit'));
98 $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'right', 'right');
102 $params = array( 0 => $comments,
103 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
104 2 => array('text' => _('Dimension')." 1",
105 'from' => get_dimension_string($dimension), 'to' => ''),
106 3 => array('text' => _('Dimension')." 2",
107 'from' => get_dimension_string($dimension2), 'to' => ''));
111 $params = array( 0 => $comments,
112 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
113 2 => array('text' => _('Dimension'),
114 'from' => get_dimension_string($dimension), 'to' => ''));
118 $params = array( 0 => $comments,
119 1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
122 $rep = new FrontReport(_('Trial Balance'), "TrialBalance.pdf", user_pagesize());
125 $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
128 $accounts = get_gl_accounts();
130 $pdeb = $pcre = $cdeb = $ccre = $tdeb = $tcre = $pbal = $cbal = $tbal = 0;
131 $begin = begin_fiscalyear();
132 if (date1_greater_date2($begin, $from))
134 $begin = add_days($begin, -1);
135 while ($account=db_fetch($accounts))
137 $prev = get_balance($account["account_code"], $dimension, $dimension2, $begin, $from, false, false);
138 $curr = get_balance($account["account_code"], $dimension, $dimension2, $from, $to, true, true);
139 $tot = get_balance($account["account_code"], $dimension, $dimension2, $begin, $to, false, true);
141 if ($zero == 0 && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
143 $rep->TextCol(0, 1, $account['account_code']);
144 $rep->TextCol(1, 2, $account['account_name']);
147 if ($prev['balance'] >= 0.0)
148 $rep->TextCol(2, 3, number_format2($prev['balance'], $dec));
150 $rep->TextCol(3, 4, number_format2(abs($prev['balance']), $dec));
151 if ($curr['balance'] >= 0.0)
152 $rep->TextCol(4, 5, number_format2($curr['balance'], $dec));
154 $rep->TextCol(5, 6, number_format2(abs($curr['balance']), $dec));
155 if ($tot['balance'] >= 0.0)
156 $rep->TextCol(6, 7, number_format2($tot['balance'], $dec));
158 $rep->TextCol(7, 8, number_format2(abs($tot['balance']), $dec));
162 $rep->TextCol(2, 3, number_format2($prev['debit'], $dec));
163 $rep->TextCol(3, 4, number_format2($prev['credit'], $dec));
164 $rep->TextCol(4, 5, number_format2($curr['debit'], $dec));
165 $rep->TextCol(5, 6, number_format2($curr['credit'], $dec));
166 $rep->TextCol(6, 7, number_format2($tot['debit'], $dec));
167 $rep->TextCol(7, 8, number_format2($tot['credit'], $dec));
168 $pdeb += $prev['debit'];
169 $pcre += $prev['credit'];
170 $cdeb += $curr['debit'];
171 $ccre += $curr['credit'];
172 $tdeb += $tot['debit'];
173 $tcre += $tot['credit'];
176 $pbal += $prev['balance'];
177 $cbal += $curr['balance'];
178 $tbal += $tot['balance'];
181 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
183 $rep->Line($rep->row - 2);
187 $rep->Line($rep->row);
191 //$prev = get_balance(null, $dimension, $dimension2, $begin, $from, false, false);
192 //$curr = get_balance(null, $dimension, $dimension2, $from, $to, true, true);
193 //$tot = get_balance(null, $dimension, $dimension2, $begin, $to, false, true);
197 $rep->TextCol(0, 2, _("Total"));
198 $rep->TextCol(2, 3, number_format2($pdeb, $dec));
199 $rep->TextCol(3, 4, number_format2($pcre, $dec));
200 $rep->TextCol(4, 5, number_format2($cdeb, $dec));
201 $rep->TextCol(5, 6, number_format2($ccre, $dec));
202 $rep->TextCol(6, 7, number_format2($tdeb, $dec));
203 $rep->TextCol(7, 8, number_format2($tcre, $dec));
206 $rep->TextCol(0, 2, _("Ending Balance"));
209 $rep->TextCol(2, 3, number_format2($pbal, $dec));
211 $rep->TextCol(3, 4, number_format2(abs($pbal), $dec));
213 $rep->TextCol(4, 5, number_format2($cbal, $dec));
215 $rep->TextCol(5, 6, number_format2(abs($cbal), $dec));
217 $rep->TextCol(6, 7, number_format2($tbal, $dec));
219 $rep->TextCol(7, 8, number_format2(abs($tbal), $dec));
221 $rep->Line($rep->row - 6);