EOL formatting cleanup.
[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 = 'SA_GLANALYTIC';
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/ui.inc");
23 include_once($path_to_root . "/includes/date_functions.inc");
24 include_once($path_to_root . "/includes/data_checks.inc");
25 include_once($path_to_root . "/gl/includes/gl_db.inc");
26
27 $pdeb = $pcre = $cdeb = $ccre = $tdeb = $tcre = $pbal = $cbal = $tbal = 0;
28
29 //----------------------------------------------------------------------------------------------------
30
31 function display_type ($type, $typename, &$dec, &$rep, $from, $to, $zero, $balances, $dimension, $dimension2)
32 {
33         global $pdeb, $pcre, $cdeb, $ccre, $tdeb, $tcre, $pbal, $cbal, $tbal;
34         
35         $printtitle = 0; //Flag for printing type name  
36         
37         //Get Accounts directly under this group/type
38         $accounts = get_gl_accounts(null, null, $type); 
39         
40         $begin = begin_fiscalyear();
41         if (date1_greater_date2($begin, $from))
42                 $begin = $from;
43         $begin = add_days($begin, -1);
44         while ($account=db_fetch($accounts))
45         {
46                 //Print Type Title if it has atleast one non-zero account       
47                 if (!$printtitle)
48                 {       
49                         $rep->row -= 4;
50                         $rep->TextCol(0, 8, _("Group")." - ".$type ." - ".$typename);   
51                         $printtitle = 1;
52                         $rep->row -= 4;
53                         $rep->Line($rep->row);
54                         $rep->NewLine();                        
55                 }
56                 
57                 $prev = get_balance($account["account_code"], $dimension, $dimension2, $begin, $from, false, false);
58                 $curr = get_balance($account["account_code"], $dimension, $dimension2, $from, $to, true, true);
59                 $tot = get_balance($account["account_code"], $dimension, $dimension2, $begin, $to, false, true);
60
61                 if ($zero == 0 && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
62                         continue;
63                 $rep->TextCol(0, 1, $account['account_code']);
64                 $rep->TextCol(1, 2,     $account['account_name']);
65                 if ($balances != 0)
66                 {
67                         if ($prev['balance'] >= 0.0)
68                                 $rep->AmountCol(2, 3, $prev['balance'], $dec);
69                         else
70                                 $rep->AmountCol(3, 4, abs($prev['balance']), $dec);
71                         if ($curr['balance'] >= 0.0)
72                                 $rep->AmountCol(4, 5, $curr['balance'], $dec);
73                         else
74                                 $rep->AmountCol(5, 6, abs($curr['balance']), $dec);
75                         if ($tot['balance'] >= 0.0)
76                                 $rep->AmountCol(6, 7, $tot['balance'], $dec);
77                         else
78                                 $rep->AmountCol(7, 8, abs($tot['balance']), $dec);
79                 }
80                 else
81                 {
82                         $rep->AmountCol(2, 3, $prev['debit'], $dec);
83                         $rep->AmountCol(3, 4, $prev['credit'], $dec);
84                         $rep->AmountCol(4, 5, $curr['debit'], $dec);
85                         $rep->AmountCol(5, 6, $curr['credit'], $dec);
86                         $rep->AmountCol(6, 7, $tot['debit'], $dec);
87                         $rep->AmountCol(7, 8, $tot['credit'], $dec);
88                         $pdeb += $prev['debit'];
89                         $pcre += $prev['credit'];
90                         $cdeb += $curr['debit'];
91                         $ccre += $curr['credit'];
92                         $tdeb += $tot['debit'];
93                         $tcre += $tot['credit'];
94                 }       
95
96                 $pbal += $prev['balance'];
97                 $cbal += $curr['balance'];
98                 $tbal += $tot['balance'];
99                 $rep->NewLine();
100
101                 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
102                 {
103                         $rep->Line($rep->row - 2);
104                         $rep->NewPage();
105                 }
106         }
107                 
108         //Get Account groups/types under this group/type
109         $result = get_account_types(false, false, $type);
110         while ($accounttype=db_fetch($result))
111         {
112                 //Print Type Title if has sub types and not previously printed
113                 if (!$printtitle)
114                 {
115                         $rep->row -= 4;
116                         $rep->TextCol(0, 8, _("Group")." - ".$type ." - ".$typename);   
117                         $printtitle = 1;
118                         $rep->row -= 4;
119                         $rep->Line($rep->row);
120                         $rep->NewLine();                
121                 }
122                 display_type($accounttype["id"], $accounttype["name"].' ('.$typename.')', $dec, $rep, $from, $to, $zero, $balances, $dimension, $dimension2);
123         }
124 }
125
126 //----------------------------------------------------------------------------------------------------
127
128 print_trial_balance();
129
130 //----------------------------------------------------------------------------------------------------
131
132 function print_trial_balance()
133 {
134         global $path_to_root;
135         global $pdeb, $pcre, $cdeb, $ccre, $tdeb, $tcre, $pbal, $cbal, $tbal;
136
137         $dim = get_company_pref('use_dimension');
138         $dimension = $dimension2 = 0;
139
140         $from = $_POST['PARAM_0'];
141         $to = $_POST['PARAM_1'];
142         $zero = $_POST['PARAM_2'];
143         $balances = $_POST['PARAM_3'];
144         if ($dim == 2)
145         {
146                 $dimension = $_POST['PARAM_4'];
147                 $dimension2 = $_POST['PARAM_5'];
148                 $comments = $_POST['PARAM_6'];
149                 $destination = $_POST['PARAM_7'];
150         }
151         else if ($dim == 1)
152         {
153                 $dimension = $_POST['PARAM_4'];
154                 $comments = $_POST['PARAM_5'];
155                 $destination = $_POST['PARAM_6'];
156         }
157         else
158         {
159                 $comments = $_POST['PARAM_4'];
160                 $destination = $_POST['PARAM_5'];
161         }
162         if ($destination)
163                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
164         else
165                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
166         $dec = user_price_dec();
167
168         $cols2 = array(0, 50, 190, 310, 430, 530);
169         //-------------0--1---2----3----4----5--
170
171         $headers2 = array('', '', _('Brought Forward'), _('This Period'), _('Balance'));
172
173         $aligns2 = array('left', 'left', 'left', 'left', 'left');
174
175         $cols = array(0, 50, 150, 210, 270,     330, 390, 450, 510,     570);
176         //------------0--1---2----3----4----5----6----7----8--
177
178         $headers = array(_('Account'), _('Account Name'), _('Debit'), _('Credit'), _('Debit'),
179                 _('Credit'), _('Debit'), _('Credit'));
180
181         $aligns = array('left', 'left', 'right', 'right', 'right', 'right',     'right', 'right');
182
183     if ($dim == 2)
184     {
185         $params =   array(      0 => $comments,
186                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
187                         2 => array('text' => _('Dimension')." 1",
188                             'from' => get_dimension_string($dimension), 'to' => ''),
189                         3 => array('text' => _('Dimension')." 2",
190                             'from' => get_dimension_string($dimension2), 'to' => ''));
191     }
192     else if ($dim == 1)
193     {
194         $params =   array(      0 => $comments,
195                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
196                         2 => array('text' => _('Dimension'),
197                             'from' => get_dimension_string($dimension), 'to' => ''));
198     }
199     else
200     {
201         $params =   array(      0 => $comments,
202                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
203     }
204
205         $rep = new FrontReport(_('Trial Balance'), "TrialBalance", user_pagesize());
206
207         $rep->Font();
208         $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
209         $rep->NewPage();
210         
211         $classresult = get_account_classes(false);
212         while ($class = db_fetch($classresult))
213         {
214                 $rep->Font('bold');
215                 $rep->TextCol(0, 1, $class['cid']);
216                 $rep->TextCol(1, 4, $class['class_name']);
217                 $rep->Font();
218                 $rep->NewLine();
219
220                 //Get Account groups/types under this group/type with no parents
221                 $typeresult = get_account_types(false, $class['cid'], -1);
222                 while ($accounttype=db_fetch($typeresult))
223                 {
224                         display_type($accounttype["id"], $accounttype["name"], $dec, $rep, $from, $to,  $zero, $balances, $dimension, $dimension2);
225                 }
226                 $rep->NewLine();
227         }
228         $rep->Line($rep->row);
229         $rep->NewLine();
230         $rep->Font('bold');
231
232         if ($balances == 0)
233         {
234                 $rep->TextCol(0, 2, _("Total"));
235                 $rep->AmountCol(2, 3, $pdeb, $dec);
236                 $rep->AmountCol(3, 4, $pcre, $dec);
237                 $rep->AmountCol(4, 5, $cdeb, $dec);
238                 $rep->AmountCol(5, 6, $ccre, $dec);
239                 $rep->AmountCol(6, 7, $tdeb, $dec);
240                 $rep->AmountCol(7, 8, $tcre, $dec);
241                 $rep->NewLine();
242         }       
243         $rep->TextCol(0, 2, _("Ending Balance"));
244
245         if ($pbal >= 0.0)
246                 $rep->AmountCol(2, 3, $pbal, $dec);
247         else
248                 $rep->AmountCol(3, 4, abs($pbal), $dec);
249         if ($cbal >= 0.0)
250                 $rep->AmountCol(4, 5, $cbal, $dec);
251         else
252                 $rep->AmountCol(5, 6, abs($cbal), $dec);
253         if ($tbal >= 0.0)
254                 $rep->AmountCol(6, 7, $tbal, $dec);
255         else
256                 $rep->AmountCol(7, 8, abs($tbal), $dec);
257         $rep->NewLine();
258                 
259         $rep->Line($rep->row + 10);
260         if (($pbal = round2($pbal, $dec)) != 0.0 && $dimension == 0 && $dimension2 == 0)
261         {
262                 $rep->NewLine(2);
263                 $rep->Font();
264                 $rep->TextCol(0, 8, _("The Opening Balance is not in balance, probably due to a non closed Previous Fiscalyear."));
265         }       
266         $rep->End();
267 }
268
269 ?>