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 //-------------0--1---2----3----4----5--
86 $headers2 = array('', '', _('Brought Forward'), _('This Period'), _('Balance'));
88 $aligns2 = array('left', 'left', 'left', 'left', 'left');
90 $cols = array(0, 50, 200, 250, 300, 350, 400, 450, 500, 550);
91 //------------0--1---2----3----4----5----6----7----8----9--
93 $headers = array(_('Account'), _('Account Name'), _('Debit'), _('Credit'), _('Debit'),
94 _('Credit'), _('Debit'), _('Credit'));
96 $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'right', 'right');
100 $params = array( 0 => $comments,
101 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
102 2 => array('text' => _('Dimension')." 1",
103 'from' => get_dimension_string($dimension), 'to' => ''),
104 3 => array('text' => _('Dimension')." 2",
105 'from' => get_dimension_string($dimension2), 'to' => ''));
109 $params = array( 0 => $comments,
110 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
111 2 => array('text' => _('Dimension'),
112 'from' => get_dimension_string($dimension), 'to' => ''));
116 $params = array( 0 => $comments,
117 1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
120 $rep = new FrontReport(_('Trial Balance'), "TrialBalance.pdf", user_pagesize());
123 $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
126 $accounts = get_gl_accounts();
128 $begin = begin_fiscalyear();
129 if (date1_greater_date2($begin, $from))
131 $begin = add_days($begin, -1);
132 while ($account=db_fetch($accounts))
134 $prev = get_balance($account["account_code"], $dimension, $dimension2, $begin, $from, false, false);
135 $curr = get_balance($account["account_code"], $dimension, $dimension2, $from, $to, true, true);
136 $tot = get_balance($account["account_code"], $dimension, $dimension2, $begin, $to, false, true);
138 if ($zero == 0 && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
140 $rep->TextCol(0, 1, $account['account_code']);
141 $rep->TextCol(1, 2, $account['account_name']);
144 if ($prev['balance'] >= 0.0)
145 $rep->TextCol(2, 3, number_format2($prev['balance'], $dec));
147 $rep->TextCol(3, 4, number_format2(abs($prev['balance']), $dec));
148 if ($curr['balance'] >= 0.0)
149 $rep->TextCol(4, 5, number_format2($curr['balance'], $dec));
151 $rep->TextCol(5, 6, number_format2(abs($curr['balance']), $dec));
152 if ($tot['balance'] >= 0.0)
153 $rep->TextCol(6, 7, number_format2($tot['balance'], $dec));
155 $rep->TextCol(7, 8, number_format2(abs($tot['balance']), $dec));
159 $rep->TextCol(2, 3, number_format2($prev['debit'], $dec));
160 $rep->TextCol(3, 4, number_format2($prev['credit'], $dec));
161 $rep->TextCol(4, 5, number_format2($curr['debit'], $dec));
162 $rep->TextCol(5, 6, number_format2($curr['credit'], $dec));
163 $rep->TextCol(6, 7, number_format2($tot['debit'], $dec));
164 $rep->TextCol(7, 8, number_format2($tot['credit'], $dec));
168 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
170 $rep->Line($rep->row - 2);
174 $rep->Line($rep->row);
178 $prev = get_balance(null, $dimension, $dimension2, $begin, $from, false, false);
179 $curr = get_balance(null, $dimension, $dimension2, $from, $to, true, true);
180 $tot = get_balance(null, $dimension, $dimension2, $begin, $to, false, true);
184 $rep->TextCol(0, 2, _("Total"));
185 $rep->TextCol(2, 3, number_format2($prev['debit'], $dec));
186 $rep->TextCol(3, 4, number_format2($prev['credit'], $dec));
187 $rep->TextCol(4, 5, number_format2($curr['debit'], $dec));
188 $rep->TextCol(5, 6, number_format2($curr['credit'], $dec));
189 $rep->TextCol(6, 7, number_format2($tot['debit'], $dec));
190 $rep->TextCol(7, 8, number_format2($tot['credit'], $dec));
193 $rep->TextCol(0, 2, _("Ending Balance"));
195 if ($prev['balance'] >= 0.0)
196 $rep->TextCol(2, 3, number_format2($prev['balance'], $dec));
198 $rep->TextCol(3, 4, number_format2(abs($prev['balance']), $dec));
199 if ($curr['balance'] >= 0.0)
200 $rep->TextCol(4, 5, number_format2($curr['balance'], $dec));
202 $rep->TextCol(5, 6, number_format2(abs($curr['balance']), $dec));
203 if ($tot['balance'] >= 0.0)
204 $rep->TextCol(6, 7, number_format2($tot['balance'], $dec));
206 $rep->TextCol(7, 8, number_format2(abs($tot['balance']), $dec));
208 $rep->Line($rep->row - 6);