Incorrect Journal Balance (sales invoice) when many decimals in tax and price.
[fa-stable.git] / reporting / rep707.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:       Profit and Loss Statement
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, $begin, $end, $compare, $convert, &$dec, &$pdec, &$rep, $dimension, $dimension2, $tags, &$pg, $graphics, &$labels, &$serie1, &$serie2)
30 {
31         $code_per_balance = 0;
32         $code_acc_balance = 0;
33         $per_balance_total = 0;
34         $acc_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                 $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
49
50                 if ($compare == 2)
51                         $acc_balance = get_budget_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
52                 else
53                         $acc_balance = get_gl_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
54                 if (!$per_balance && !$acc_balance)
55                         continue;
56                 
57                 //Print Type Title if it has atleast one non-zero account       
58                 if (!$printtitle)
59                 {
60                         $printtitle = 1;
61                         $rep->row -= 4;
62                         $rep->TextCol(0, 5, $typename);
63                         $rep->row -= 4;
64                         $rep->Line($rep->row);
65                         $rep->NewLine();                
66                 }                       
67
68                 $rep->TextCol(0, 1,     $account['account_code']);
69                 $rep->TextCol(1, 2,     $account['account_name']);
70
71                 $rep->AmountCol(2, 3, $per_balance * $convert, $dec);
72                 $rep->AmountCol(3, 4, $acc_balance * $convert, $dec);
73                 $rep->AmountCol(4, 5, Achieve($per_balance, $acc_balance), $pdec);
74
75                 $rep->NewLine();
76
77                 if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
78                 {
79                         $rep->Line($rep->row - 2);
80                         $rep->NewPage();
81                 }
82
83                 $code_per_balance += $per_balance;
84                 $code_acc_balance += $acc_balance;
85         }
86                 
87         //Get Account groups/types under this group/type
88         $result = get_account_types(false, false, $type);
89         while ($accounttype=db_fetch($result))
90         {
91                 //Print Type Title if has sub types and not previously printed
92                 if (!$printtitle)
93                 {
94                         $printtitle = 1;
95                         $rep->row -= 4;
96                         $rep->TextCol(0, 5, $typename);
97                         $rep->row -= 4;
98                         $rep->Line($rep->row);
99                         $rep->NewLine();                
100                 }
101
102                 $totals_arr = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert, $dec, 
103                         $pdec, $rep, $dimension, $dimension2, $tags, $pg, $graphics, $labels, $serie1, $serie2);
104                 $per_balance_total += $totals_arr[0];
105                 $acc_balance_total += $totals_arr[1];
106         }
107
108         //Display Type Summary if total is != 0 OR head is printed (Needed in case of unused hierarchical COA) 
109         if (($code_per_balance + $per_balance_total + $code_acc_balance + $acc_balance_total) != 0 || $printtitle)
110         {
111                 $rep->row += 6;
112                 $rep->Line($rep->row);
113                 $rep->NewLine();
114                 $rep->TextCol(0, 2,     _('Total') . " " . $typename);
115                 $rep->AmountCol(2, 3, ($code_per_balance + $per_balance_total) * $convert, $dec);
116                 $rep->AmountCol(3, 4, ($code_acc_balance + $acc_balance_total) * $convert, $dec);
117                 $rep->AmountCol(4, 5, Achieve(($code_per_balance + $per_balance_total), ($code_acc_balance + $acc_balance_total)), $pdec);              
118                 if ($graphics)
119                 {
120                         $labels[] = $typename;
121                         $serie1[] = abs($code_per_balance + $per_balance_total);
122                         $serie2[] = abs($code_acc_balance + $acc_balance_total);
123                 }
124                 $rep->NewLine();
125         }
126         
127         $totals_arr[0] = $code_per_balance + $per_balance_total;
128         $totals_arr[1] = $code_acc_balance + $acc_balance_total;
129         return $totals_arr;
130 }
131
132 print_profit_and_loss_statement();
133
134 //----------------------------------------------------------------------------------------------------
135
136 function Achieve($d1, $d2)
137 {
138         if ($d1 == 0 && $d2 == 0)
139                 return 0;
140         elseif ($d2 == 0)
141                 return 999;
142         $ret = ($d1 / $d2 * 100.0);
143         if ($ret > 999)
144                 $ret = 999;
145         return $ret;
146 }
147
148 //----------------------------------------------------------------------------------------------------
149
150 function print_profit_and_loss_statement()
151 {
152         global $path_to_root, $SysPrefs;
153
154         $dim = get_company_pref('use_dimension');
155         $dimension = $dimension2 = 0;
156
157         $from = $_POST['PARAM_0'];
158         $to = $_POST['PARAM_1'];
159         $compare = $_POST['PARAM_2'];
160         if ($dim == 2)
161         {
162                 $dimension = $_POST['PARAM_3'];
163                 $dimension2 = $_POST['PARAM_4'];
164                 $tags = (isset($_POST['PARAM_5']) ? $_POST['PARAM_5'] : -1);
165                 $decimals = $_POST['PARAM_6'];
166                 $graphics = $_POST['PARAM_7'];
167                 $comments = $_POST['PARAM_8'];
168                 $orientation = $_POST['PARAM_9'];
169                 $destination = $_POST['PARAM_10'];
170         }
171         elseif ($dim == 1)
172         {
173                 $dimension = $_POST['PARAM_3'];
174                 $tags = (isset($_POST['PARAM_4']) ? $_POST['PARAM_4'] : -1);
175                 $decimals = $_POST['PARAM_5'];
176                 $graphics = $_POST['PARAM_6'];
177                 $comments = $_POST['PARAM_7'];
178                 $orientation = $_POST['PARAM_8'];
179                 $destination = $_POST['PARAM_9'];
180         }
181         else
182         {
183                 $tags = (isset($_POST['PARAM_3']) ? $_POST['PARAM_3'] : -1);
184                 $decimals = $_POST['PARAM_4'];
185                 $graphics = $_POST['PARAM_5'];
186                 $comments = $_POST['PARAM_6'];
187                 $orientation = $_POST['PARAM_7'];
188                 $destination = $_POST['PARAM_8'];
189         }
190         if ($destination)
191                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
192         else
193                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
194         $orientation = ($orientation ? 'L' : 'P');
195         $labels = array();
196         $serie1 = array();
197         $serie2 = array();
198         if ($graphics)
199         {
200                 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
201                 $pg = new Chart($graphics);
202         }
203         if (!$decimals)
204                 $dec = 0;
205         else
206                 $dec = user_price_dec();
207         $pdec = user_percent_dec();
208
209         $cols = array(0, 60, 200, 350, 425,     500);
210         //------------0--1---2----3----4----5--
211
212         $headers = array(_('Account'), _('Account Name'), _('Period'), _('Accumulated'), _('Achieved %'));
213
214         $aligns = array('left', 'left', 'right', 'right', 'right');
215
216     if ($dim == 2)
217     {
218         $params =   array(      0 => $comments,
219                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
220                         2 => array('text' => _('Dimension')." 1",
221                             'from' => get_dimension_string($dimension), 'to' => ''),
222                         3 => array('text' => _('Dimension')." 2",
223                             'from' => get_dimension_string($dimension2), 'to' => ''),
224                         4 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''));
225     }
226     elseif ($dim == 1)
227     {
228         $params =   array(      0 => $comments,
229                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
230                         2 => array('text' => _('Dimension'),
231                             'from' => get_dimension_string($dimension), 'to' => ''),
232                         3 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''));
233     }
234     else
235     {
236         $params =   array(      0 => $comments,
237                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
238                                     2 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''));
239     }
240
241
242         if ($compare == 0 || $compare == 2)
243         {
244                 $end = $to;
245                 if ($compare == 2)
246                 {
247                         $begin = $from;
248                         $headers[3] = _('Budget');
249                 }
250                 else
251                         $begin = begin_fiscalyear();
252         }
253         elseif ($compare == 1)
254         {
255                 $begin = add_months($from, -12);
256                 $end = add_months($to, -12);
257                 if (date_comp($to, end_month($to)) == 0) // compensate for leap years. If to-date equal end month 
258                         $end = end_month($end);                          // then the year-1 should also be end month    
259                 $headers[3] = _('Period Y-1');
260         }
261
262         $rep = new FrontReport(_('Profit and Loss Statement'), "ProfitAndLoss", user_pagesize(), 9, $orientation);
263     if ($orientation == 'L')
264         recalculate_cols($cols);
265
266         $rep->Font();
267         $rep->Info($params, $cols, $headers, $aligns);
268         $rep->NewPage();
269
270         $salesper = 0.0;
271         $salesacc = 0.0;        
272
273         $classresult = get_account_classes(false, 0);
274         while ($class = db_fetch($classresult))
275         {
276                 $class_per_total = 0;
277                 $class_acc_total = 0;
278                 $convert = get_class_type_convert($class["ctype"]);             
279                 
280                 //Print Class Name      
281                 $rep->Font('bold');
282                 $rep->TextCol(0, 5, $class["class_name"]);
283                 $rep->Font();
284                 $rep->NewLine();
285                 
286                 //Get Account groups/types under this group/type with no parents
287                 $typeresult = get_account_types(false, $class['cid'], -1);
288                 while ($accounttype=db_fetch($typeresult))
289                 {
290                         $classtotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert, $dec, $pdec, $rep, $dimension, $dimension2, $tags, $pg, $graphics, $labels, $serie1, $serie2);
291                         $class_per_total += $classtotal[0];
292                         $class_acc_total += $classtotal[1];                     
293                 }
294                 
295                 //Print Class Summary   
296                 $rep->row += 6;
297                 $rep->Line($rep->row);
298                 $rep->NewLine();
299                 $rep->Font('bold');
300                 $rep->TextCol(0, 2,     _('Total') . " " . $class["class_name"]);
301                 $rep->AmountCol(2, 3, $class_per_total * $convert, $dec);
302                 $rep->AmountCol(3, 4, $class_acc_total * $convert, $dec);
303                 $rep->AmountCol(4, 5, Achieve($class_per_total, $class_acc_total), $pdec);
304                 $rep->Font();
305                 $rep->NewLine(2);       
306
307                 $salesper += $class_per_total;
308                 $salesacc += $class_acc_total;
309         }
310         
311         $rep->Font('bold');     
312         $rep->TextCol(0, 2,     _('Calculated Return'));
313         $rep->AmountCol(2, 3, $salesper *-1, $dec); // always convert
314         $rep->AmountCol(3, 4, $salesacc * -1, $dec);
315         $rep->AmountCol(4, 5, Achieve($salesper, $salesacc), $pdec);
316         $rep->Font();
317         $rep->NewLine();
318         $rep->Line($rep->row);
319         if ($graphics)
320         {
321                 $labels[] = _('Calculated Return');
322                 $serie1[] = abs($salesper);
323                 $serie2[] = abs($salesacc);
324                 $pg->setStream('png');
325                 $pg->setLabels($labels);
326                 $pg->addSerie($headers[2], $serie1);
327                 $pg->addSerie($headers[3], $serie2);
328                 $pg->setTitle($rep->title);
329                 $pg->setXTitle(_("Group"));
330                 $pg->setYTitle(_("Amount"));
331                 $pg->setDTitle(number_format2(abs($salesper)));
332                 $pg->setValues(true);
333                 $pg->latin_notation = ($SysPrefs->decseps[user_dec_sep()] != ".");
334                 $filename = company_path(). "/pdf_files/". random_id().".png";
335                 $pg->display($filename, true);
336                 $w = $pg->width / 1.5;
337                 $h = $pg->height / 1.5;
338                 $x = ($rep->pageWidth - $w) / 2;
339                 $rep->NewLine(2);
340                 if ($rep->row - $h < $rep->bottomMargin)
341                         $rep->NewPage();
342                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
343         }
344                 
345         $rep->End();
346 }
347