d08ef0b0900ae2f4b21a685335d83bfe4a065899
[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 \r
29 //----------------------------------------------------------------------------------------------------\r
30 \r
31 function display_type ($type, $typename, &$dec, &$rep, $from, $to, $zero, $balances, $dimension, $dimension2)\r
32 {\r
33         global $pdeb, $pcre, $cdeb, $ccre, $tdeb, $tcre, $pbal, $cbal, $tbal;\r
34         \r
35         $printtitle = 0; //Flag for printing type name  \r
36         \r
37         //Get Accounts directly under this group/type\r
38         $accounts = get_gl_accounts(null, null, $type); \r
39         \r
40         $begin = begin_fiscalyear();\r
41         if (date1_greater_date2($begin, $from))\r
42                 $begin = $from;\r
43         $begin = add_days($begin, -1);\r
44         while ($account=db_fetch($accounts))\r
45         {\r
46                 //Print Type Title if it has atleast one non-zero account       \r
47                 if (!$printtitle)\r
48                 {       \r
49                         $rep->row -= 4;\r
50                         $rep->TextCol(0, 8, _("Group")." - ".$type ." - ".$typename);   \r
51                         $printtitle = 1;\r
52                         $rep->row -= 4;\r
53                         $rep->Line($rep->row);\r
54                         $rep->NewLine();                        \r
55                 }\r
56                 \r
57                 $prev = get_balance($account["account_code"], $dimension, $dimension2, $begin, $from, false, false);\r
58                 $curr = get_balance($account["account_code"], $dimension, $dimension2, $from, $to, true, true);\r
59                 $tot = get_balance($account["account_code"], $dimension, $dimension2, $begin, $to, false, true);\r
60 \r
61                 if ($zero == 0 && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])\r
62                         continue;\r
63                 $rep->TextCol(0, 1, $account['account_code']);\r
64                 $rep->TextCol(1, 2,     $account['account_name']);\r
65                 if ($balances != 0)\r
66                 {\r
67                         if ($prev['balance'] >= 0.0)\r
68                                 $rep->AmountCol(2, 3, $prev['balance'], $dec);\r
69                         else\r
70                                 $rep->AmountCol(3, 4, abs($prev['balance']), $dec);\r
71                         if ($curr['balance'] >= 0.0)\r
72                                 $rep->AmountCol(4, 5, $curr['balance'], $dec);\r
73                         else\r
74                                 $rep->AmountCol(5, 6, abs($curr['balance']), $dec);\r
75                         if ($tot['balance'] >= 0.0)\r
76                                 $rep->AmountCol(6, 7, $tot['balance'], $dec);\r
77                         else\r
78                                 $rep->AmountCol(7, 8, abs($tot['balance']), $dec);\r
79                 }\r
80                 else\r
81                 {\r
82                         $rep->AmountCol(2, 3, $prev['debit'], $dec);\r
83                         $rep->AmountCol(3, 4, $prev['credit'], $dec);\r
84                         $rep->AmountCol(4, 5, $curr['debit'], $dec);\r
85                         $rep->AmountCol(5, 6, $curr['credit'], $dec);\r
86                         $rep->AmountCol(6, 7, $tot['debit'], $dec);\r
87                         $rep->AmountCol(7, 8, $tot['credit'], $dec);\r
88                         $pdeb += $prev['debit'];\r
89                         $pcre += $prev['credit'];\r
90                         $cdeb += $curr['debit'];\r
91                         $ccre += $curr['credit'];\r
92                         $tdeb += $tot['debit'];\r
93                         $tcre += $tot['credit'];\r
94                 }       \r
95 \r
96                 $pbal += $prev['balance'];\r
97                 $cbal += $curr['balance'];\r
98                 $tbal += $tot['balance'];\r
99                 $rep->NewLine();\r
100 \r
101                 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)\r
102                 {\r
103                         $rep->Line($rep->row - 2);\r
104                         $rep->NewPage();\r
105                 }\r
106         }\r
107                 \r
108         //Get Account groups/types under this group/type\r
109         $result = get_account_types(false, false, $type);\r
110         while ($accounttype=db_fetch($result))\r
111         {\r
112                 //Print Type Title if has sub types and not previously printed\r
113                 if (!$printtitle)\r
114                 {\r
115                         $rep->row -= 4;\r
116                         $rep->TextCol(0, 8, _("Group")." - ".$type ." - ".$typename);   \r
117                         $printtitle = 1;\r
118                         $rep->row -= 4;\r
119                         $rep->Line($rep->row);\r
120                         $rep->NewLine();                \r
121                 }\r
122                 display_type($accounttype["id"], $accounttype["name"].' ('.$typename.')', $dec, $rep, $from, $to, $zero, $balances, $dimension, $dimension2);\r
123         }\r
124 }\r
125 \r
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;\r
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);\r
176         //------------0--1---2----3----4----5----6----7----8--\r
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         \r
211         $classresult = get_account_classes(false);\r
212         while ($class = db_fetch($classresult))\r
213         {
214                 $rep->Font('bold');\r
215                 $rep->TextCol(0, 1, $class['cid']);\r
216                 $rep->TextCol(1, 4, $class['class_name']);\r
217                 $rep->Font();\r
218                 $rep->NewLine();\r
219
220                 //Get Account groups/types under this group/type with no parents\r
221                 $typeresult = get_account_types(false, $class['cid'], -1);\r
222                 while ($accounttype=db_fetch($typeresult))\r
223                 {
224                         display_type($accounttype["id"], $accounttype["name"], $dec, $rep, $from, $to,  $zero, $balances, $dimension, $dimension2);\r
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 ?>