Merged stable branch up to 2.3.10
[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 //----------------------------------------------------------------------------------------------------
28
29 print_trial_balance();
30
31 //----------------------------------------------------------------------------------------------------
32
33 function print_trial_balance()
34 {
35         global $path_to_root;
36
37         $dim = get_company_pref('use_dimension');
38         $dimension = $dimension2 = 0;
39
40         $from = $_POST['PARAM_0'];
41         $to = $_POST['PARAM_1'];
42         $zero = $_POST['PARAM_2'];
43         $balances = $_POST['PARAM_3'];
44         if ($dim == 2)
45         {
46                 $dimension = $_POST['PARAM_4'];
47                 $dimension2 = $_POST['PARAM_5'];
48                 $comments = $_POST['PARAM_6'];
49                 $destination = $_POST['PARAM_7'];
50         }
51         else if ($dim == 1)
52         {
53                 $dimension = $_POST['PARAM_4'];
54                 $comments = $_POST['PARAM_5'];
55                 $destination = $_POST['PARAM_6'];
56         }
57         else
58         {
59                 $comments = $_POST['PARAM_4'];
60                 $destination = $_POST['PARAM_5'];
61         }
62         if ($destination)
63                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
64         else
65                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
66         $dec = user_price_dec();
67
68         //$cols2 = array(0, 50, 230, 330, 430, 530);
69         $cols2 = array(0, 50, 190, 310, 430, 530);
70         //-------------0--1---2----3----4----5--
71
72         $headers2 = array('', '', _('Brought Forward'), _('This Period'), _('Balance'));
73
74         $aligns2 = array('left', 'left', 'left', 'left', 'left');
75
76         //$cols = array(0, 50, 200, 250, 300,   350, 400, 450, 500,     550);
77         $cols = array(0, 50, 150, 210, 270,     330, 390, 450, 510,     570);
78         //------------0--1---2----3----4----5----6----7----8----9--
79
80         $headers = array(_('Account'), _('Account Name'), _('Debit'), _('Credit'), _('Debit'),
81                 _('Credit'), _('Debit'), _('Credit'));
82
83         $aligns = array('left', 'left', 'right', 'right', 'right', 'right',     'right', 'right');
84
85     if ($dim == 2)
86     {
87         $params =   array(      0 => $comments,
88                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
89                         2 => array('text' => _('Dimension')." 1",
90                             'from' => get_dimension_string($dimension), 'to' => ''),
91                         3 => array('text' => _('Dimension')." 2",
92                             'from' => get_dimension_string($dimension2), 'to' => ''));
93     }
94     else if ($dim == 1)
95     {
96         $params =   array(      0 => $comments,
97                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
98                         2 => array('text' => _('Dimension'),
99                             'from' => get_dimension_string($dimension), 'to' => ''));
100     }
101     else
102     {
103         $params =   array(      0 => $comments,
104                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
105     }
106
107         $rep = new FrontReport(_('Trial Balance'), "TrialBalance", user_pagesize());
108
109         $rep->Font();
110         $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
111         $rep->NewPage();
112
113         $accounts = get_gl_accounts();
114
115         $pdeb = $pcre = $cdeb = $ccre = $tdeb = $tcre = $pbal = $cbal = $tbal = 0;
116         $begin = begin_fiscalyear();
117         if (date1_greater_date2($begin, $from))
118                 $begin = $from;
119         $begin = add_days($begin, -1);
120         while ($account=db_fetch($accounts))
121         {
122                 $prev = get_balance($account["account_code"], $dimension, $dimension2, $begin, $from, false, false);
123                 $curr = get_balance($account["account_code"], $dimension, $dimension2, $from, $to, true, true);
124                 $tot = get_balance($account["account_code"], $dimension, $dimension2, $begin, $to, false, true);
125
126                 if ($zero == 0 && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
127                         continue;
128                 $rep->TextCol(0, 1, $account['account_code']);
129                 $rep->TextCol(1, 2,     $account['account_name']);
130                 if ($balances != 0)
131                 {
132                         if ($prev['balance'] >= 0.0)
133                                 $rep->AmountCol(2, 3, $prev['balance'], $dec);
134                         else
135                                 $rep->AmountCol(3, 4, abs($prev['balance']), $dec);
136                         if ($curr['balance'] >= 0.0)
137                                 $rep->AmountCol(4, 5, $curr['balance'], $dec);
138                         else
139                                 $rep->AmountCol(5, 6, abs($curr['balance']), $dec);
140                         if ($tot['balance'] >= 0.0)
141                                 $rep->AmountCol(6, 7, $tot['balance'], $dec);
142                         else
143                                 $rep->AmountCol(7, 8, abs($tot['balance']), $dec);
144                 }
145                 else
146                 {
147                         $rep->AmountCol(2, 3, $prev['debit'], $dec);
148                         $rep->AmountCol(3, 4, $prev['credit'], $dec);
149                         $rep->AmountCol(4, 5, $curr['debit'], $dec);
150                         $rep->AmountCol(5, 6, $curr['credit'], $dec);
151                         $rep->AmountCol(6, 7, $tot['debit'], $dec);
152                         $rep->AmountCol(7, 8, $tot['credit'], $dec);
153                         $pdeb += $prev['debit'];
154                         $pcre += $prev['credit'];
155                         $cdeb += $curr['debit'];
156                         $ccre += $curr['credit'];
157                         $tdeb += $tot['debit'];
158                         $tcre += $tot['credit'];
159                         
160                 }       
161                 $pbal += $prev['balance'];
162                 $cbal += $curr['balance'];
163                 $tbal += $tot['balance'];
164                 $rep->NewLine();
165
166                 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
167                 {
168                         $rep->Line($rep->row - 2);
169                         $rep->NewPage();
170                 }
171         }
172         $rep->Line($rep->row);
173         $rep->NewLine();
174         $rep->Font('bold');
175
176         //$prev = get_balance(null, $dimension, $dimension2, $begin, $from, false, false);
177         //$curr = get_balance(null, $dimension, $dimension2, $from, $to, true, true);
178         //$tot = get_balance(null, $dimension, $dimension2, $begin, $to, false, true);
179
180         if ($balances == 0)
181         {
182                 $rep->TextCol(0, 2, _("Total"));
183                 $rep->AmountCol(2, 3, $pdeb, $dec);
184                 $rep->AmountCol(3, 4, $pcre, $dec);
185                 $rep->AmountCol(4, 5, $cdeb, $dec);
186                 $rep->AmountCol(5, 6, $ccre, $dec);
187                 $rep->AmountCol(6, 7, $tdeb, $dec);
188                 $rep->AmountCol(7, 8, $tcre, $dec);
189                 $rep->NewLine();
190         }       
191         $rep->TextCol(0, 2, _("Ending Balance"));
192
193         if ($pbal >= 0.0)
194                 $rep->AmountCol(2, 3, $pbal, $dec);
195         else
196                 $rep->AmountCol(3, 4, abs($pbal), $dec);
197         if ($cbal >= 0.0)
198                 $rep->AmountCol(4, 5, $cbal, $dec);
199         else
200                 $rep->AmountCol(5, 6, abs($cbal), $dec);
201         if ($tbal >= 0.0)
202                 $rep->AmountCol(6, 7, $tbal, $dec);
203         else
204                 $rep->AmountCol(7, 8, abs($tbal), $dec);
205         $rep->NewLine();
206                 
207         $rep->Line($rep->row);
208         if (($pbal = round2($pbal, $dec)) != 0.0)
209         {
210                 $rep->NewLine(2);
211                 $rep->Font();
212                 $rep->TextCol(0, 8, _("The Opening Balance is not in balance, probably due to a non closed Previous Fiscalyear."));
213         }       
214         $rep->End();
215 }
216
217 ?>