Profit and Loss Statement. Compensate for to-date = leap-year in period compare.
[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, $SysPrefs;
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         elseif ($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, 60, 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     elseif ($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                 if (date_comp($to, end_month($to)) == 0) // compensate for leap years. If to-date equal end month 
256                         $end = end_month($end);                          // then the year-1 should also be end month    
257                 $headers[3] = _('Period Y-1');
258         }
259
260         $rep = new FrontReport(_('Profit and Loss Statement'), "ProfitAndLoss", user_pagesize(), 9, $orientation);
261     if ($orientation == 'L')
262         recalculate_cols($cols);
263
264         $rep->Font();
265         $rep->Info($params, $cols, $headers, $aligns);
266         $rep->NewPage();
267
268         $salesper = 0.0;
269         $salesacc = 0.0;        
270
271         $classresult = get_account_classes(false, 0);
272         while ($class = db_fetch($classresult))
273         {
274                 $class_per_total = 0;
275                 $class_acc_total = 0;
276                 $convert = get_class_type_convert($class["ctype"]);             
277                 
278                 //Print Class Name      
279                 $rep->Font('bold');
280                 $rep->TextCol(0, 5, $class["class_name"]);
281                 $rep->Font();
282                 $rep->NewLine();
283                 
284                 //Get Account groups/types under this group/type with no parents
285                 $typeresult = get_account_types(false, $class['cid'], -1);
286                 while ($accounttype=db_fetch($typeresult))
287                 {
288                         $classtotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert, $dec, 
289                                 $pdec, $rep, $dimension, $dimension2, $tags, $pg, $graphics);
290                         $class_per_total += $classtotal[0];
291                         $class_acc_total += $classtotal[1];                     
292                 }
293                 
294                 //Print Class Summary   
295                 $rep->row += 6;
296                 $rep->Line($rep->row);
297                 $rep->NewLine();
298                 $rep->Font('bold');
299                 $rep->TextCol(0, 2,     _('Total') . " " . $class["class_name"]);
300                 $rep->AmountCol(2, 3, $class_per_total * $convert, $dec);
301                 $rep->AmountCol(3, 4, $class_acc_total * $convert, $dec);
302                 $rep->AmountCol(4, 5, Achieve($class_per_total, $class_acc_total), $pdec);
303                 $rep->Font();
304                 $rep->NewLine(2);       
305
306                 $salesper += $class_per_total;
307                 $salesacc += $class_acc_total;
308         }
309         
310         $rep->Font('bold');     
311         $rep->TextCol(0, 2,     _('Calculated Return'));
312         $rep->AmountCol(2, 3, $salesper *-1, $dec); // always convert
313         $rep->AmountCol(3, 4, $salesacc * -1, $dec);
314         $rep->AmountCol(4, 5, Achieve($salesper, $salesacc), $pdec);
315         if ($graphics)
316         {
317                 $pg->x[] = _('Calculated Return');
318                 $pg->y[] = abs($salesper);
319                 $pg->z[] = abs($salesacc);
320         }
321         $rep->Font();
322         $rep->NewLine();
323         $rep->Line($rep->row);
324         if ($graphics)
325         {
326                 $pg->title     = $rep->title;
327                 $pg->axis_x    = _("Group");
328                 $pg->axis_y    = _("Amount");
329                 $pg->graphic_1 = $headers[2];
330                 $pg->graphic_2 = $headers[3];
331                 $pg->type      = $graphics;
332                 $pg->skin      = $SysPrefs->graph_skin;
333                 $pg->built_in  = false;
334                 $pg->latin_notation = ($SysPrefs->decseps[user_dec_sep()] != ".");
335                 $filename = company_path(). "/pdf_files/". random_id().".png";
336                 $pg->display($filename, true);
337                 $w = $pg->width / 1.5;
338                 $h = $pg->height / 1.5;
339                 $x = ($rep->pageWidth - $w) / 2;
340                 $rep->NewLine(2);
341                 if ($rep->row - $h < $rep->bottomMargin)
342                         $rep->NewPage();
343                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
344         }
345                 
346         $rep->End();
347 }
348