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