7f9fa243aac2ad48db3ed222b82b10e2248fd44a
[fa-stable.git] / reporting / rep708.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-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 // trial_inquiry_controls();
29 print_trial_balance();
30
31 //----------------------------------------------------------------------------------------------------
32 function get_balance($account, $dimension, $dimension2, $from, $to, $from_incl=true, $to_incl=true) 
33 {
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";
38                 
39         if ($account != null)
40                 $sql .= " account='$account' AND";
41         if ($dimension > 0)
42                 $sql .= " dimension_id=$dimension AND";
43         if ($dimension2 > 0)
44                 $sql .= " dimension2_id=$dimension2 AND";
45         $from_date = date2sql($from);
46         if ($from_incl)
47                 $sql .= " tran_date >= '$from_date'  AND";
48         else
49                 $sql .= " tran_date > IF(".TB_PREF."chart_class.balance_sheet=1, '0000-00-00', '$from_date') AND";
50         $to_date = date2sql($to);
51         if ($to_incl)
52                 $sql .= " tran_date <= '$to_date' ";
53         else
54                 $sql .= " tran_date < '$to_date' ";
55
56         $result = db_query($sql,"No general ledger accounts were returned");
57
58         return db_fetch($result);
59 }
60
61 //----------------------------------------------------------------------------------------------------
62
63 function print_trial_balance()
64 {
65         global $path_to_root;
66
67         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
68         $dim = get_company_pref('use_dimension');
69         $dimension = $dimension2 = 0;
70
71         $from = $_POST['PARAM_0'];
72         $to = $_POST['PARAM_1'];
73         $zero = $_POST['PARAM_2'];
74         $balances = $_POST['PARAM_3'];
75         if ($dim == 2)
76         {
77                 $dimension = $_POST['PARAM_4'];
78                 $dimension2 = $_POST['PARAM_5'];
79                 $comments = $_POST['PARAM_6'];
80         }
81         else if ($dim == 1)
82         {
83                 $dimension = $_POST['PARAM_4'];
84                 $comments = $_POST['PARAM_5'];
85         }
86         else
87         {
88                 $comments = $_POST['PARAM_4'];
89         }
90         $dec = user_price_dec();
91
92         //$cols2 = array(0, 50, 230, 330, 430, 530);
93         $cols2 = array(0, 50, 190, 310, 430, 530);
94         //-------------0--1---2----3----4----5--
95
96         $headers2 = array('', '', _('Brought Forward'), _('This Period'), _('Balance'));
97
98         $aligns2 = array('left', 'left', 'left', 'left', 'left');
99
100         //$cols = array(0, 50, 200, 250, 300,   350, 400, 450, 500,     550);
101         $cols = array(0, 50, 150, 210, 270,     330, 390, 450, 510,     570);
102         //------------0--1---2----3----4----5----6----7----8----9--
103
104         $headers = array(_('Account'), _('Account Name'), _('Debit'), _('Credit'), _('Debit'),
105                 _('Credit'), _('Debit'), _('Credit'));
106
107         $aligns = array('left', 'left', 'right', 'right', 'right', 'right',     'right', 'right');
108
109     if ($dim == 2)
110     {
111         $params =   array(      0 => $comments,
112                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
113                         2 => array('text' => _('Dimension')." 1",
114                             'from' => get_dimension_string($dimension), 'to' => ''),
115                         3 => array('text' => _('Dimension')." 2",
116                             'from' => get_dimension_string($dimension2), 'to' => ''));
117     }
118     else if ($dim == 1)
119     {
120         $params =   array(      0 => $comments,
121                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
122                         2 => array('text' => _('Dimension'),
123                             'from' => get_dimension_string($dimension), 'to' => ''));
124     }
125     else
126     {
127         $params =   array(      0 => $comments,
128                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
129     }
130
131         $rep = new FrontReport(_('Trial Balance'), "TrialBalance.pdf", user_pagesize());
132
133         $rep->Font();
134         $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
135         $rep->Header();
136
137         $accounts = get_gl_accounts();
138
139         $pdeb = $pcre = $cdeb = $ccre = $tdeb = $tcre = $pbal = $cbal = $tbal = 0;
140         $begin = begin_fiscalyear();
141         if (date1_greater_date2($begin, $from))
142                 $begin = $from;
143         $begin = add_days($begin, -1);
144         while ($account=db_fetch($accounts))
145         {
146                 $prev = get_balance($account["account_code"], $dimension, $dimension2, $begin, $from, false, false);
147                 $curr = get_balance($account["account_code"], $dimension, $dimension2, $from, $to, true, true);
148                 $tot = get_balance($account["account_code"], $dimension, $dimension2, $begin, $to, false, true);
149
150                 if ($zero == 0 && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
151                         continue;
152                 $rep->TextCol(0, 1, $account['account_code']);
153                 $rep->TextCol(1, 2,     $account['account_name']);
154                 if ($balances != 0)
155                 {
156                         if ($prev['balance'] >= 0.0)
157                                 $rep->TextCol(2, 3,     number_format2($prev['balance'], $dec));
158                         else
159                                 $rep->TextCol(3, 4,     number_format2(abs($prev['balance']), $dec));
160                         if ($curr['balance'] >= 0.0)
161                                 $rep->TextCol(4, 5,     number_format2($curr['balance'], $dec));
162                         else
163                                 $rep->TextCol(5, 6,     number_format2(abs($curr['balance']), $dec));
164                         if ($tot['balance'] >= 0.0)
165                                 $rep->TextCol(6, 7,     number_format2($tot['balance'], $dec));
166                         else
167                                 $rep->TextCol(7, 8,     number_format2(abs($tot['balance']), $dec));
168                 }
169                 else
170                 {
171                         $rep->TextCol(2, 3,     number_format2($prev['debit'], $dec));
172                         $rep->TextCol(3, 4,     number_format2($prev['credit'], $dec));
173                         $rep->TextCol(4, 5,     number_format2($curr['debit'], $dec));
174                         $rep->TextCol(5, 6,     number_format2($curr['credit'], $dec));
175                         $rep->TextCol(6, 7,     number_format2($tot['debit'], $dec));
176                         $rep->TextCol(7, 8,     number_format2($tot['credit'], $dec));
177                         $pdeb += $prev['debit'];
178                         $pcre += $prev['credit'];
179                         $cdeb += $curr['debit'];
180                         $ccre += $curr['credit'];
181                         $tdeb += $tot['debit'];
182                         $tcre += $tot['credit'];
183                         
184                 }       
185                 $pbal += $prev['balance'];
186                 $cbal += $curr['balance'];
187                 $tbal += $tot['balance'];
188                 $rep->NewLine();
189
190                 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
191                 {
192                         $rep->Line($rep->row - 2);
193                         $rep->Header();
194                 }
195         }
196         $rep->Line($rep->row);
197         $rep->NewLine();
198         $rep->Font('bold');
199
200         //$prev = get_balance(null, $dimension, $dimension2, $begin, $from, false, false);
201         //$curr = get_balance(null, $dimension, $dimension2, $from, $to, true, true);
202         //$tot = get_balance(null, $dimension, $dimension2, $begin, $to, false, true);
203
204         if ($balances == 0)
205         {
206                 $rep->TextCol(0, 2, _("Total"));
207                 $rep->TextCol(2, 3,     number_format2($pdeb, $dec));
208                 $rep->TextCol(3, 4,     number_format2($pcre, $dec));
209                 $rep->TextCol(4, 5,     number_format2($cdeb, $dec));
210                 $rep->TextCol(5, 6,     number_format2($ccre, $dec));
211                 $rep->TextCol(6, 7,     number_format2($tdeb, $dec));
212                 $rep->TextCol(7, 8,     number_format2($tcre, $dec));
213                 $rep->NewLine();
214         }       
215         $rep->TextCol(0, 2, _("Ending Balance"));
216
217         if ($pbal >= 0.0)
218                 $rep->TextCol(2, 3,     number_format2($pbal, $dec));
219         else
220                 $rep->TextCol(3, 4,     number_format2(abs($pbal), $dec));
221         if ($cbal >= 0.0)
222                 $rep->TextCol(4, 5,     number_format2($cbal, $dec));
223         else
224                 $rep->TextCol(5, 6,     number_format2(abs($cbal), $dec));
225         if ($tbal >= 0.0)
226                 $rep->TextCol(6, 7,     number_format2($tbal, $dec));
227         else
228                 $rep->TextCol(7, 8,     number_format2(abs($tbal), $dec));
229         
230         $rep->Line($rep->row - 6);
231         
232         $rep->End();
233 }
234
235 ?>