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