[0000081] Changed Trial Balance to show all debits and credits (option to only show...
[fa-stable.git] / reporting / rep708.php
1 <?php
2
3 $page_security = 2;
4 // ----------------------------------------------------------------
5 // $ Revision:  2.0 $
6 // Creator:     Joe Hunt
7 // date_:       2005-05-19
8 // Title:       Trial Balance
9 // ----------------------------------------------------------------
10 $path_to_root="../";
11
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");
16
17 //----------------------------------------------------------------------------------------------------
18
19 // trial_inquiry_controls();
20 print_trial_balance();
21
22 //----------------------------------------------------------------------------------------------------
23 function get_balance($account, $dimension, $dimension2, $from, $to, $from_incl=true, $to_incl=true) 
24 {
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";
29                 
30         if ($account != null)
31                 $sql .= " account='$account' AND";
32         if ($dimension > 0)
33                 $sql .= " dimension_id=$dimension AND";
34         if ($dimension2 > 0)
35                 $sql .= " dimension2_id=$dimension2 AND";
36         $from_date = date2sql($from);
37         if ($from_incl)
38                 $sql .= " tran_date >= '$from_date'  AND";
39         else
40                 $sql .= " tran_date > IF(".TB_PREF."chart_class.balance_sheet=1, '0000-00-00', '$from_date') AND";
41         $to_date = date2sql($to);
42         if ($to_incl)
43                 $sql .= " tran_date <= '$to_date' ";
44         else
45                 $sql .= " tran_date < '$to_date' ";
46
47         $result = db_query($sql,"No general ledger accounts were returned");
48
49         return db_fetch($result);
50 }
51
52 //----------------------------------------------------------------------------------------------------
53
54 function print_trial_balance()
55 {
56         global $path_to_root;
57
58         include_once($path_to_root . "reporting/includes/pdf_report.inc");
59         $dim = get_company_pref('use_dimension');
60         $dimension = $dimension2 = 0;
61
62         $from = $_POST['PARAM_0'];
63         $to = $_POST['PARAM_1'];
64         $zero = $_POST['PARAM_2'];
65         $balances = $_POST['PARAM_3'];
66         if ($dim == 2)
67         {
68                 $dimension = $_POST['PARAM_4'];
69                 $dimension2 = $_POST['PARAM_5'];
70                 $comments = $_POST['PARAM_6'];
71         }
72         else if ($dim == 1)
73         {
74                 $dimension = $_POST['PARAM_4'];
75                 $comments = $_POST['PARAM_5'];
76         }
77         else
78         {
79                 $comments = $_POST['PARAM_4'];
80         }
81         $dec = user_price_dec();
82
83         $cols2 = array(0, 50, 230, 330, 430, 530);
84         //-------------0--1---2----3----4----5--
85
86         $headers2 = array('', '', _('Brought Forward'), _('This Period'), _('Balance'));
87
88         $aligns2 = array('left', 'left', 'left', 'left', 'left');
89
90         $cols = array(0, 50, 200, 250, 300,     350, 400, 450, 500,     550);
91         //------------0--1---2----3----4----5----6----7----8----9--
92
93         $headers = array(_('Account'), _('Account Name'), _('Debit'), _('Credit'), _('Debit'),
94                 _('Credit'), _('Debit'), _('Credit'));
95
96         $aligns = array('left', 'left', 'right', 'right', 'right', 'right',     'right', 'right');
97
98     if ($dim == 2)
99     {
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' => ''));
106     }
107     else if ($dim == 1)
108     {
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' => ''));
113     }
114     else
115     {
116         $params =   array(      0 => $comments,
117                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
118     }
119
120         $rep = new FrontReport(_('Trial Balance'), "TrialBalance.pdf", user_pagesize());
121
122         $rep->Font();
123         $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
124         $rep->Header();
125
126         $accounts = get_gl_accounts();
127
128         $begin = begin_fiscalyear();
129         if (date1_greater_date2($begin, $from))
130                 $begin = $from;
131         $begin = add_days($begin, -1);
132         while ($account=db_fetch($accounts))
133         {
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);
137
138                 if ($zero == 0 && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
139                         continue;
140                 $rep->TextCol(0, 1, $account['account_code']);
141                 $rep->TextCol(1, 2,     $account['account_name']);
142                 if ($balances != 0)
143                 {
144                         if ($prev['balance'] >= 0.0)
145                                 $rep->TextCol(2, 3,     number_format2($prev['balance'], $dec));
146                         else
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));
150                         else
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));
154                         else
155                                 $rep->TextCol(7, 8,     number_format2(abs($tot['balance']), $dec));
156                 }
157                 else
158                 {
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));
165                 }       
166                 $rep->NewLine();
167
168                 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
169                 {
170                         $rep->Line($rep->row - 2);
171                         $rep->Header();
172                 }
173         }
174         $rep->Line($rep->row);
175         $rep->NewLine();
176         $rep->Font('bold');
177
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);
181
182         if ($balances == 0)
183         {
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));
191                 $rep->NewLine();
192         }       
193         $rep->TextCol(0, 2, _("Ending Balance"));
194
195         if ($prev['balance'] >= 0.0)
196                 $rep->TextCol(2, 3,     number_format2($prev['balance'], $dec));
197         else
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));
201         else
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));
205         else
206                 $rep->TextCol(7, 8,     number_format2(abs($tot['balance']), $dec));
207         
208         $rep->Line($rep->row - 6);
209         
210         $rep->End();
211 }
212
213 ?>