Changed balance_sheet operation in class table to class type for sign convert in...
[fa-stable.git] / reporting / rep708.php
1 <?php
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 ***********************************************************************/
12 $page_security = 2;
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Trial Balance
18 // ----------------------------------------------------------------
19 $path_to_root="..";
20
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");
25
26 //----------------------------------------------------------------------------------------------------
27
28 print_trial_balance();
29
30 //----------------------------------------------------------------------------------------------------
31 function get_balance($account, $dimension, $dimension2, $from, $to, $from_incl=true, $to_incl=true) 
32 {
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";
37                 
38         if ($account != null)
39                 $sql .= " account='$account' AND";
40         if ($dimension > 0)
41                 $sql .= " dimension_id=$dimension AND";
42         if ($dimension2 > 0)
43                 $sql .= " dimension2_id=$dimension2 AND";
44         $from_date = date2sql($from);
45         if ($from_incl)
46                 $sql .= " tran_date >= '$from_date'  AND";
47         else
48                 $sql .= " tran_date > IF(balance_sheet>0 AND balance_sheet<".CL_INCOME.", '0000-00-00', '$from_date') AND";
49         $to_date = date2sql($to);
50         if ($to_incl)
51                 $sql .= " tran_date <= '$to_date' ";
52         else
53                 $sql .= " tran_date < '$to_date' ";
54
55         $result = db_query($sql,"No general ledger accounts were returned");
56
57         return db_fetch($result);
58 }
59
60 //----------------------------------------------------------------------------------------------------
61
62 function print_trial_balance()
63 {
64         global $path_to_root;
65
66         $dim = get_company_pref('use_dimension');
67         $dimension = $dimension2 = 0;
68
69         $from = $_POST['PARAM_0'];
70         $to = $_POST['PARAM_1'];
71         $zero = $_POST['PARAM_2'];
72         $balances = $_POST['PARAM_3'];
73         if ($dim == 2)
74         {
75                 $dimension = $_POST['PARAM_4'];
76                 $dimension2 = $_POST['PARAM_5'];
77                 $comments = $_POST['PARAM_6'];
78                 $destination = $_POST['PARAM_7'];
79         }
80         else if ($dim == 1)
81         {
82                 $dimension = $_POST['PARAM_4'];
83                 $comments = $_POST['PARAM_5'];
84                 $destination = $_POST['PARAM_6'];
85         }
86         else
87         {
88                 $comments = $_POST['PARAM_4'];
89                 $destination = $_POST['PARAM_5'];
90         }
91         if ($destination)
92                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
93         else
94                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
95         $dec = user_price_dec();
96
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--
100
101         $headers2 = array('', '', _('Brought Forward'), _('This Period'), _('Balance'));
102
103         $aligns2 = array('left', 'left', 'left', 'left', 'left');
104
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--
108
109         $headers = array(_('Account'), _('Account Name'), _('Debit'), _('Credit'), _('Debit'),
110                 _('Credit'), _('Debit'), _('Credit'));
111
112         $aligns = array('left', 'left', 'right', 'right', 'right', 'right',     'right', 'right');
113
114     if ($dim == 2)
115     {
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' => ''));
122     }
123     else if ($dim == 1)
124     {
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' => ''));
129     }
130     else
131     {
132         $params =   array(      0 => $comments,
133                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
134     }
135
136         $rep = new FrontReport(_('Trial Balance'), "TrialBalance", user_pagesize());
137
138         $rep->Font();
139         $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
140         $rep->Header();
141
142         $accounts = get_gl_accounts();
143
144         $pdeb = $pcre = $cdeb = $ccre = $tdeb = $tcre = $pbal = $cbal = $tbal = 0;
145         $begin = begin_fiscalyear();
146         if (date1_greater_date2($begin, $from))
147                 $begin = $from;
148         $begin = add_days($begin, -1);
149         while ($account=db_fetch($accounts))
150         {
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);
154
155                 if ($zero == 0 && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
156                         continue;
157                 $rep->TextCol(0, 1, $account['account_code']);
158                 $rep->TextCol(1, 2,     $account['account_name']);
159                 if ($balances != 0)
160                 {
161                         if ($prev['balance'] >= 0.0)
162                                 $rep->AmountCol(2, 3, $prev['balance'], $dec);
163                         else
164                                 $rep->AmountCol(3, 4, abs($prev['balance']), $dec);
165                         if ($curr['balance'] >= 0.0)
166                                 $rep->AmountCol(4, 5, $curr['balance'], $dec);
167                         else
168                                 $rep->AmountCol(5, 6, abs($curr['balance']), $dec);
169                         if ($tot['balance'] >= 0.0)
170                                 $rep->AmountCol(6, 7, $tot['balance'], $dec);
171                         else
172                                 $rep->AmountCol(7, 8, abs($tot['balance']), $dec);
173                 }
174                 else
175                 {
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'];
188                         
189                 }       
190                 $pbal += $prev['balance'];
191                 $cbal += $curr['balance'];
192                 $tbal += $tot['balance'];
193                 $rep->NewLine();
194
195                 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
196                 {
197                         $rep->Line($rep->row - 2);
198                         $rep->Header();
199                 }
200         }
201         $rep->Line($rep->row);
202         $rep->NewLine();
203         $rep->Font('bold');
204
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);
208
209         if ($balances == 0)
210         {
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);
218                 $rep->NewLine();
219         }       
220         $rep->TextCol(0, 2, _("Ending Balance"));
221
222         if ($pbal >= 0.0)
223                 $rep->AmountCol(2, 3, $pbal, $dec);
224         else
225                 $rep->AmountCol(3, 4, abs($pbal), $dec);
226         if ($cbal >= 0.0)
227                 $rep->AmountCol(4, 5, $cbal, $dec);
228         else
229                 $rep->AmountCol(5, 6, abs($cbal), $dec);
230         if ($tbal >= 0.0)
231                 $rep->AmountCol(6, 7, $tbal, $dec);
232         else
233                 $rep->AmountCol(7, 8, abs($tbal), $dec);
234         $rep->NewLine();
235         
236         $rep->Line($rep->row);
237         
238         $rep->End();
239 }
240
241 ?>