14112f24bd5674f1e1ad7e9567397a6671d62901
[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                 $decimals = $_POST['PARAM_5'];
160                 $graphics = $_POST['PARAM_6'];
161                 $comments = $_POST['PARAM_7'];
162                 $destination = $_POST['PARAM_8'];
163         }
164         else if ($dim == 1)
165         {
166                 $dimension = $_POST['PARAM_3'];
167                 $decimals = $_POST['PARAM_4'];
168                 $graphics = $_POST['PARAM_5'];
169                 $comments = $_POST['PARAM_6'];
170                 $destination = $_POST['PARAM_7'];
171         }
172         else
173         {
174                 $decimals = $_POST['PARAM_3'];
175                 $graphics = $_POST['PARAM_4'];
176                 $comments = $_POST['PARAM_5'];
177                 $destination = $_POST['PARAM_6'];
178         }
179         if ($destination)
180                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
181         else
182                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
183         if ($graphics)
184         {
185                 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
186                 $pg = new graph();
187         }
188         if (!$decimals)
189                 $dec = 0;
190         else
191                 $dec = user_price_dec();
192         $pdec = user_percent_dec();
193
194         $cols = array(0, 50, 200, 350, 425,     500);
195         //------------0--1---2----3----4----5--
196
197         $headers = array(_('Account'), _('Account Name'), _('Period'), _('Accumulated'), _('Achieved %'));
198
199         $aligns = array('left', 'left', 'right', 'right', 'right');
200
201     if ($dim == 2)
202     {
203         $params =   array(      0 => $comments,
204                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
205                         2 => array('text' => _('Dimension')." 1",
206                             'from' => get_dimension_string($dimension), 'to' => ''),
207                         3 => array('text' => _('Dimension')." 2",
208                             'from' => get_dimension_string($dimension2), 'to' => ''));
209     }
210     else if ($dim == 1)
211     {
212         $params =   array(      0 => $comments,
213                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
214                         2 => array('text' => _('Dimension'),
215                             'from' => get_dimension_string($dimension), 'to' => ''));
216     }
217     else
218     {
219         $params =   array(      0 => $comments,
220                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
221     }
222
223
224         if ($compare == 0 || $compare == 2)
225         {
226                 $end = $to;
227                 if ($compare == 2)
228                 {
229                         $begin = $from;
230                         $headers[3] = _('Budget');
231                 }
232                 else
233                         $begin = begin_fiscalyear();
234         }
235         elseif ($compare == 1)
236         {
237                 $begin = add_months($from, -12);
238                 $end = add_months($to, -12);
239                 $headers[3] = _('Period Y-1');
240         }
241
242         $rep = new FrontReport(_('Profit and Loss Statement'), "ProfitAndLoss", user_pagesize());
243
244         $rep->Font();
245         $rep->Info($params, $cols, $headers, $aligns);
246         $rep->Header();
247
248         $classper = 0.0;
249         $classacc = 0.0;
250         $salesper = 0.0;
251         $salesacc = 0.0;        
252
253         $classresult = get_account_classes(false, 0);
254         while ($class = db_fetch($classresult))
255         {
256                 $class_per_total = 0;
257                 $class_acc_total = 0;
258                 $convert = get_class_type_convert($class["ctype"]);             
259                 
260                 //Print Class Name      
261                 $rep->Font('bold');
262                 $rep->TextCol(0, 5, $class["class_name"]);
263                 $rep->Font();
264                 $rep->NewLine();
265                 
266                 //Get Account groups/types under this group/type with no parents
267                 $typeresult = get_account_types(false, $class['cid'], -1);
268                 while ($accounttype=db_fetch($typeresult))
269                 {
270                         $classtotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert, $dec, 
271                                 $pdec, $rep, $dimension, $dimension2, $pg, $graphics);
272                         $class_per_total += $classtotal[0];
273                         $class_acc_total += $classtotal[1];                     
274                 }
275                 
276                 //Print Class Summary   
277                 $rep->row += 6;
278                 $rep->Line($rep->row);
279                 $rep->NewLine();
280                 $rep->Font('bold');
281                 $rep->TextCol(0, 2,     _('Total') . " " . $class["class_name"]);
282                 $rep->AmountCol(2, 3, $class_per_total * $convert, $dec);
283                 $rep->AmountCol(3, 4, $class_acc_total * $convert, $dec);
284                 $rep->AmountCol(4, 5, Achieve($class_per_total, $class_acc_total), $pdec);
285                 $rep->Font();
286                 $rep->NewLine(2);       
287
288                 $salesper += $class_per_total;
289                 $salesacc += $class_acc_total;
290         }
291         
292         $rep->Font('bold');     
293         $rep->TextCol(0, 2,     _('Calculated Return'));
294         $rep->AmountCol(2, 3, $salesper *-1, $dec); // always convert
295         $rep->AmountCol(3, 4, $salesacc * -1, $dec);
296         $rep->AmountCol(4, 5, Achieve($salesper, $salesacc), $pdec);
297         if ($graphics)
298         {
299                 $pg->x[] = _('Calculated Return');
300                 $pg->y[] = abs($salesper);
301                 $pg->z[] = abs($salesacc);
302         }
303         $rep->Font();
304         $rep->NewLine();
305         $rep->Line($rep->row);
306         if ($graphics)
307         {
308                 global $decseps, $graph_skin;
309                 $pg->title     = $rep->title;
310                 $pg->axis_x    = _("Group");
311                 $pg->axis_y    = _("Amount");
312                 $pg->graphic_1 = $headers[2];
313                 $pg->graphic_2 = $headers[3];
314                 $pg->type      = $graphics;
315                 $pg->skin      = $graph_skin;
316                 $pg->built_in  = false;
317                 $pg->fontfile  = $path_to_root . "/reporting/fonts/Vera.ttf";
318                 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
319                 $filename = $comp_path.'/'.user_company(). "/pdf_files/test.png";
320                 $pg->display($filename, true);
321                 $w = $pg->width / 1.5;
322                 $h = $pg->height / 1.5;
323                 $x = ($rep->pageWidth - $w) / 2;
324                 $rep->NewLine(2);
325                 if ($rep->row - $h < $rep->bottomMargin)
326                         $rep->Header();
327                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
328         }
329                 
330         $rep->End();
331 }
332
333 ?>