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