Incorrect Journal Balance (sales invoice) when many decimals in tax and price.
[fa-stable.git] / reporting / rep706.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, Chaitanya for the recursive version 2009-02-05.
16 // date_:       2005-05-19
17 // Title:       Balance Sheet
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 include_once($path_to_root . "/admin/db/tags_db.inc");
26
27 //----------------------------------------------------------------------------------------------------
28
29 function display_type ($type, $typename, $from, $to, $convert, &$dec, &$rep, $dimension, $dimension2, $tags, &$pg, $graphics, &$labels, &$serie1, &$serie2)
30 {
31         $code_open_balance = 0;
32         $code_period_balance = 0;
33         $open_balance_total = 0;
34         $period_balance_total = 0;
35         $totals_arr = array();
36
37         $printtitle = 0; //Flag for printing type name  
38         
39         //Get Accounts directly under this group/type
40         $result = get_gl_accounts(null, null, $type);   
41         while ($account=db_fetch($result))
42         {
43                 if ($tags != -1 && is_array($tags) && $tags[0] != false)
44                 {
45                         if (!is_record_in_tags($tags, TAG_ACCOUNT, $account['account_code']))
46                                 continue;
47                 }       
48                 $prev_balance = get_gl_balance_from_to("", $from, $account["account_code"], $dimension, $dimension2);
49
50                 $curr_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
51
52                 if (!$prev_balance && !$curr_balance)
53                         continue;
54                 
55                 //Print Type Title if it has atleast one non-zero account       
56                 if (!$printtitle)
57                 {
58                         $printtitle = 1;
59                         $rep->row -= 4;
60                         $rep->TextCol(0, 5, $typename);
61                         $rep->row -= 4;
62                         $rep->Line($rep->row);
63                         $rep->NewLine();                
64                 }                       
65
66                 $rep->TextCol(0, 1,     $account['account_code']);
67                 $rep->TextCol(1, 2,     $account['account_name']);
68
69                 $rep->AmountCol(2, 3, $prev_balance * $convert, $dec);
70                 $rep->AmountCol(3, 4, $curr_balance * $convert, $dec);
71                 $rep->AmountCol(4, 5, ($prev_balance + $curr_balance) * $convert, $dec);
72
73                 $rep->NewLine();
74
75                 $code_open_balance += $prev_balance;
76                 $code_period_balance += $curr_balance;
77         }
78                 
79         //Get Account groups/types under this group/type
80         $result = get_account_types(false, false, $type);
81         while ($accounttype=db_fetch($result))
82         {
83                 //Print Type Title if has sub types and not previously printed
84                 if (!$printtitle)
85                 {
86                         $printtitle = 1;
87                         $rep->row -= 4;
88                         $rep->TextCol(0, 5, $typename);
89                         $rep->row -= 4;
90                         $rep->Line($rep->row);
91                         $rep->NewLine();                
92                 }
93
94                 $totals_arr = display_type($accounttype["id"], $accounttype["name"], $from, $to, $convert, $dec, 
95                         $rep, $dimension, $dimension2, $tags, $pg, $graphics, $labels, $serie1, $serie2);
96                 $open_balance_total += $totals_arr[0];
97                 $period_balance_total += $totals_arr[1];
98         }
99
100         //Display Type Summary if total is != 0 OR head is printed (Needed in case of unused hierarchical COA) 
101         if (($code_open_balance + $open_balance_total + $code_period_balance + $period_balance_total) != 0 || $printtitle)
102         {
103                 $rep->row += 6;
104                 $rep->Line($rep->row);
105                 $rep->NewLine();
106                 $rep->TextCol(0, 2,     _('Total') . " " . $typename);
107                 $rep->AmountCol(2, 3, ($code_open_balance + $open_balance_total) * $convert, $dec);
108                 $rep->AmountCol(3, 4, ($code_period_balance + $period_balance_total) * $convert, $dec);
109                 $rep->AmountCol(4, 5, ($code_open_balance + $open_balance_total + $code_period_balance + $period_balance_total) * $convert, $dec);              
110                 if ($graphics)
111                 {
112                         $labels[] = $typename;
113                         $serie1[] = abs(($code_period_balance + $period_balance_total) * $convert);
114                         $serie2[] = abs(($code_open_balance + $open_balance_total + $code_period_balance + $period_balance_total) * $convert);
115                 }
116                 $rep->NewLine();
117         }
118         
119         $totals_arr[0] = $code_open_balance + $open_balance_total;
120         $totals_arr[1] = $code_period_balance + $period_balance_total;
121         return $totals_arr;
122 }
123
124 print_balance_sheet();
125
126 //----------------------------------------------------------------------------------------------------
127
128 function print_balance_sheet()
129 {
130         global $path_to_root, $SysPrefs;
131
132         $dim = get_company_pref('use_dimension');
133         $dimension = $dimension2 = 0;
134
135         $from = $_POST['PARAM_0'];
136         $to = $_POST['PARAM_1'];
137         if ($dim == 2)
138         {
139                 $dimension = $_POST['PARAM_2'];
140                 $dimension2 = $_POST['PARAM_3'];
141                 $tags = (isset($_POST['PARAM_4']) ? $_POST['PARAM_4'] : -1);
142                 $decimals = $_POST['PARAM_5'];
143                 $graphics = $_POST['PARAM_6'];
144                 $comments = $_POST['PARAM_7'];
145                 $orientation = $_POST['PARAM_8'];
146                 $destination = $_POST['PARAM_9'];
147         }
148         elseif ($dim == 1)
149         {
150                 $dimension = $_POST['PARAM_2'];
151                 $tags = (isset($_POST['PARAM_3']) ? $_POST['PARAM_3'] : -1);
152                 $decimals = $_POST['PARAM_4'];
153                 $graphics = $_POST['PARAM_5'];
154                 $comments = $_POST['PARAM_6'];
155                 $orientation = $_POST['PARAM_7'];
156                 $destination = $_POST['PARAM_8'];
157         }
158         else
159         {
160                 $tags = (isset($_POST['PARAM_2']) ? $_POST['PARAM_2'] : -1);
161                 $decimals = $_POST['PARAM_3'];
162                 $graphics = $_POST['PARAM_4'];
163                 $comments = $_POST['PARAM_5'];
164                 $orientation = $_POST['PARAM_6'];
165                 $destination = $_POST['PARAM_7'];
166         }
167         if ($destination)
168                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
169         else
170                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
171         $orientation = ($orientation ? 'L' : 'P');
172         $labels = array();
173         $serie1 = array();
174         $serie2 = array();
175         if ($graphics)
176         {
177                 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
178                 $pg = new Chart($graphics);
179         }
180         if (!$decimals)
181                 $dec = 0;
182         else
183                 $dec = user_price_dec();
184
185         $cols = array(0, 60, 200, 350, 425,     500);
186         //------------0--1---2----3----4----5--
187
188         $headers = array(_('Account'), _('Account Name'), _('Open Balance'), _('Period'),
189                 _('Close Balance'));
190
191         $aligns = array('left', 'left', 'right', 'right', 'right');
192
193     if ($dim == 2)
194     {
195         $params =   array(      0 => $comments,
196                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
197                         2 => array('text' => _('Dimension')." 1",
198                             'from' => get_dimension_string($dimension), 'to' => ''),
199                         3 => array('text' => _('Dimension')." 2",
200                             'from' => get_dimension_string($dimension2), 'to' => ''),
201                         4 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''));
202     }
203     elseif ($dim == 1)
204     {
205         $params =   array(      0 => $comments,
206                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
207                         2 => array('text' => _('Dimension'),
208                             'from' => get_dimension_string($dimension), 'to' => ''),
209                         3 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''));
210     }
211     else
212     {
213         $params =   array(      0 => $comments,
214                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
215                                     2 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''));
216     }
217
218         $rep = new FrontReport(_('Balance Sheet'), "BalanceSheet", user_pagesize(), 9, $orientation);
219     if ($orientation == 'L')
220         recalculate_cols($cols);
221         $rep->Font();
222         $rep->Info($params, $cols, $headers, $aligns);
223         $rep->NewPage();
224
225         $calc_open = $calc_period = 0.0;
226         $equity_open = $equity_period = 0.0;
227         $liability_open = $liability_period = 0.0;
228         $econvert = $lconvert = 0;
229         
230         $classresult = get_account_classes(false, 1);
231         while ($class = db_fetch($classresult))
232         {
233                 $class_open_total = 0;
234                 $class_period_total = 0;
235                 $convert = get_class_type_convert($class["ctype"]);             
236                 
237                 //Print Class Name      
238                 $rep->Font('bold');
239                 $rep->TextCol(0, 5, $class["class_name"]);
240                 $rep->Font();
241                 $rep->NewLine();
242                 
243                 //Get Account groups/types under this group/type with no parents
244                 $typeresult = get_account_types(false, $class['cid'], -1);
245                 while ($accounttype=db_fetch($typeresult))
246                 {
247                         $classtotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $convert, $dec, $rep, $dimension, $dimension2, $tags, $pg, $graphics, $labels, $serie1, $serie2);
248                         $class_open_total += $classtotal[0];
249                         $class_period_total += $classtotal[1];                  
250                 }
251                 
252                 //Print Class Summary   
253                 $rep->row += 6;
254                 $rep->Line($rep->row);
255                 $rep->NewLine();
256                 $rep->Font('bold');
257                 $rep->TextCol(0, 2,     _('Total') . " " . $class["class_name"]);
258                 $rep->AmountCol(2, 3, $class_open_total * $convert, $dec);
259                 $rep->AmountCol(3, 4, $class_period_total * $convert, $dec);
260                 $rep->AmountCol(4, 5, ($class_open_total + $class_period_total) * $convert, $dec);
261                 $rep->Font();
262                 $rep->NewLine(2);       
263
264                 $calc_open += $class_open_total;
265                 $calc_period += $class_period_total;
266                 if ($class['ctype'] == CL_EQUITY)
267                 {
268                         $equity_open += $class_open_total;
269                         $equity_period += $class_period_total;
270                         $econvert = $convert;
271                 }
272                 elseif ($class['ctype'] == CL_LIABILITIES)
273                 {
274                         $liability_open += $class_open_total;
275                         $liability_period += $class_period_total;
276                         $lconvert = $convert;
277                 }
278         }
279         $rep->Font();   
280         $rep->TextCol(0, 2,     _('Calculated Return'));
281         if ($lconvert == 1)
282         {
283                 $calc_open *= -1;
284                 $calc_period *= -1;
285         }       
286         $rep->AmountCol(2, 3, $calc_open, $dec); // never convert
287         $rep->AmountCol(3, 4, $calc_period, $dec);
288         $rep->AmountCol(4, 5, $calc_open + $calc_period, $dec);
289         $rep->NewLine(2);
290
291         $rep->Font('bold');     
292         $rep->TextCol(0, 2,     _('Total') . " " . _('Liabilities') . _(' and ') . _('Equities'));
293         $topen = $equity_open * $econvert + $liability_open * $lconvert + $calc_open;
294         $tperiod = $equity_period * $econvert + $liability_period * $lconvert + $calc_period;
295         $tclose = $topen + $tperiod;
296         $rep->AmountCol(2, 3, $topen, $dec);
297         $rep->AmountCol(3, 4, $tperiod, $dec);
298         $rep->AmountCol(4, 5, $tclose, $dec);
299         
300         $rep->Font();
301         $rep->NewLine();
302         $rep->Line($rep->row);
303         if ($graphics)
304         {
305                 $labels[] = _('Calculated Return');
306                 $serie1[] = abs($calc_period);
307                 $serie2[] = abs($calc_open + $calc_period);
308                 $pg->setStream('png');
309                 $pg->setLabels($labels);
310                 $pg->addSerie($headers[3], $serie1);
311                 $pg->addSerie($headers[4], $serie2);
312                 $pg->setTitle($rep->title);
313                 $pg->setXTitle(_("Group"));
314                 $pg->setYTitle(_("Amount"));
315                 $pg->setDTitle(number_format2(abs($calc_open + $calc_period)));
316                 $pg->setValues(true);
317                 $pg->latin_notation = ($SysPrefs->decseps[user_dec_sep()] != ".");
318                 $filename = company_path(). "/pdf_files/". random_id().".png";
319                 $pg->display($filename, true);
320                 $w = $pg->width / 1.5;
321                 $h = $pg->height / 1.5;
322                 $x = ($rep->pageWidth - $w) / 2;
323                 $rep->NewLine(2);
324                 if ($rep->row - $h < $rep->bottomMargin)
325                         $rep->NewPage();
326                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
327         }
328         $rep->End();
329 }
330