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