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