Wrong default function parameters in display_type. Fixed.
[fa-stable.git] / gl / inquiry / profit_loss.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 $path_to_root="../..";
14
15 include_once($path_to_root . "/includes/session.inc");
16
17 include_once($path_to_root . "/includes/date_functions.inc");
18 include_once($path_to_root . "/includes/ui.inc");
19 include_once($path_to_root . "/includes/data_checks.inc");
20
21 include_once($path_to_root . "/gl/includes/gl_db.inc");
22
23 $js = "";
24 if (user_use_date_picker())
25         $js = get_js_date_picker();
26
27 page(_($help_context = "Profit & Loss Drilldown"), false, false, "", $js);
28
29 $compare_types = array(
30         _("Accumulated"),
31         _("Period Y-1"),
32         _("Budget")
33 );
34 //----------------------------------------------------------------------------------------------------
35 // Ajax updates
36
37 if (get_post('Show')) 
38 {
39         $Ajax->activate('pl_tbl');
40 }
41
42 if (isset($_GET["TransFromDate"]))
43         $_POST["TransFromDate"] = $_GET["TransFromDate"];       
44 if (isset($_GET["TransToDate"]))
45         $_POST["TransToDate"] = $_GET["TransToDate"];
46 if (isset($_GET["Compare"]))
47         $_POST["Compare"] = $_GET["Compare"];
48 if (isset($_GET["Dimension"]))
49         $_POST["Dimension"] = $_GET["Dimension"];
50 if (isset($_GET["Dimension2"]))
51         $_POST["Dimension2"] = $_GET["Dimension2"];
52 if (isset($_GET["AccGrp"]))
53         $_POST["AccGrp"] = $_GET["AccGrp"];
54
55 //----------------------------------------------------------------------------------------------------
56
57 function display_type ($type, $typename, $from, $to, $begin, $end, $compare, $convert,
58         $dimension, $dimension2, $drilldown)
59 {
60         global $path_to_root, $levelptr, $k;
61                 
62         $code_per_balance = 0;
63         $code_acc_balance = 0;
64         $per_balance_total = 0;
65         $acc_balance_total = 0;
66         unset($totals_arr);
67         $totals_arr = array();
68         
69         //Get Accounts directly under this group/type
70         $result = get_gl_accounts(null, null, $type);   
71
72         while ($account=db_fetch($result))
73         {
74                 $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
75
76                 if ($compare == 2)
77                         $acc_balance = get_budget_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
78                 else
79                         $acc_balance = get_gl_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
80                 if (!$per_balance && !$acc_balance)
81                         continue;
82                 
83                 if ($drilldown && $levelptr == 0)
84                 {
85                         $url = "<a href='$path_to_root/gl/inquiry/gl_account_inquiry.php?TransFromDate=" 
86                                 . $from . "&TransToDate=" . $to . "&Dimension=" . $dimension . "&Dimension2=" . $dimension2 
87                                 . "&account=" . $account['account_code'] . "'>" . $account['account_code'] 
88                                 ." ". $account['account_name'] ."</a>";                         
89                                 
90                         start_row("class='stockmankobg'");
91                         label_cell($url);
92                         amount_cell($per_balance * $convert);
93                         amount_cell($acc_balance * $convert);
94                         amount_cell(Achieve($per_balance, $acc_balance));
95                         end_row();
96                 }
97                         
98                 $code_per_balance += $per_balance;
99                 $code_acc_balance += $acc_balance;
100         }
101
102         $levelptr = 1;
103         
104         //Get Account groups/types under this group/type
105         $result = get_account_types(false, false, $type);
106         while ($accounttype=db_fetch($result))
107         {       
108                 $totals_arr = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, 
109                         $compare, $convert, $dimension, $dimension2, $drilldown);
110                 $per_balance_total += $totals_arr[0];
111                 $acc_balance_total += $totals_arr[1];
112         }
113
114         //Display Type Summary if total is != 0 
115         if (($code_per_balance + $per_balance_total + $code_acc_balance + $acc_balance_total) != 0)
116         {
117                 if ($drilldown && $type == $_POST["AccGrp"])
118                 {               
119                         start_row("class='inquirybg' style='font-weight:bold'");
120                         label_cell(_('Total') . " " . $typename);
121                         amount_cell(($code_per_balance + $per_balance_total) * $convert);
122                         amount_cell(($code_acc_balance + $acc_balance_total) * $convert);
123                         amount_cell(Achieve(($code_per_balance + $per_balance_total), ($code_acc_balance + $acc_balance_total)));
124                         end_row();
125                 }
126                 //START Patch#1 : Display  only direct child types
127                 $acctype1 = get_account_type($type);
128                 $parent1 = $acctype1["parent"];
129                 if ($drilldown && $parent1 == $_POST["AccGrp"])
130                 //END Patch#2           
131                 //elseif ($drilldown && $type != $_POST["AccGrp"])
132                 {       
133                         $url = "<a href='$path_to_root/gl/inquiry/profit_loss.php?TransFromDate=" 
134                                 . $from . "&TransToDate=" . $to . "&Compare=" . $compare . "&Dimension=" . $dimension . "&Dimension2=" . $dimension2
135                                 . "&AccGrp=" . $type ."'>" . $type . " " . $typename ."</a>";
136                                 
137                         alt_table_row_color($k);
138                         label_cell($url);
139                         amount_cell(($code_per_balance + $per_balance_total) * $convert);
140                         amount_cell(($code_acc_balance + $acc_balance_total) * $convert);
141                         amount_cell(Achieve(($code_per_balance + $per_balance_total), ($code_acc_balance + $acc_balance_total)));
142                         end_row();
143                 }
144         }
145         
146         $totals_arr[0] = $code_per_balance + $per_balance_total;
147         $totals_arr[1] = $code_acc_balance + $acc_balance_total;
148         return $totals_arr;
149 }       
150         
151 function Achieve($d1, $d2)
152 {
153         if ($d1 == 0 && $d2 == 0)
154                 return 0;
155         elseif ($d2 == 0)
156                 return 999;
157         $ret = ($d1 / $d2 * 100.0);
158         if ($ret > 999)
159                 $ret = 999;
160         return $ret;
161 }
162
163 function inquiry_controls()
164 {  
165         global $compare_types;
166
167         $dim = get_company_pref('use_dimension');
168     start_table(TABLESTYLE_NOBORDER);
169     
170         $date = today();
171         if (!isset($_POST['TransToDate']))
172                 $_POST['TransToDate'] = end_month($date);
173         if (!isset($_POST['TransFromDate']))
174                 $_POST['TransFromDate'] = add_days(end_month($date), -user_transaction_days());
175     date_cells(_("From:"), 'TransFromDate');
176         date_cells(_("To:"), 'TransToDate');
177         
178         echo "<td>"._("Compare to").":</td>\n";
179         echo "<td>";
180         echo array_selector('Compare', null, $compare_types);
181         echo "</td>\n"; 
182
183         if ($dim >= 1)
184                 dimensions_list_cells(_("Dimension")." 1:", 'Dimension', null, true, " ", false, 1);
185         if ($dim > 1)
186                 dimensions_list_cells(_("Dimension")." 2:", 'Dimension2', null, true, " ", false, 2);
187         
188         submit_cells('Show',_("Show"),'','', 'default');
189     end_table();
190
191         hidden('AccGrp');
192 }
193
194 //----------------------------------------------------------------------------------------------------
195
196 function display_profit_and_loss($compare)
197 {
198         global $path_to_root, $compare_types;
199
200         if (!isset($_POST['Dimension']))
201                 $_POST['Dimension'] = 0;
202         if (!isset($_POST['Dimension2']))
203                 $_POST['Dimension2'] = 0;
204         $dimension = $_POST['Dimension'];
205         $dimension2 = $_POST['Dimension2'];
206
207         $from = $_POST['TransFromDate'];
208         $to = $_POST['TransToDate'];
209         
210         if (isset($_POST["AccGrp"]) && (strlen($_POST['AccGrp']) > 0))
211                 $drilldown = 1; // Deeper Level
212         else
213                 $drilldown = 0; // Root level
214         
215         if ($compare == 0 || $compare == 2)
216         {
217                 $end = $to;
218                 if ($compare == 2)
219                 {
220                         $begin = $from;
221                 }
222                 else
223                         $begin = begin_fiscalyear();
224         }
225         elseif ($compare == 1)
226         {
227                 $begin = add_months($from, -12);
228                 $end = add_months($to, -12);
229         }
230         
231         div_start('pl_tbl');
232
233         start_table(TABLESTYLE, "width='50%'");
234
235         $tableheader =  "<tr>
236         <td class='tableheader'>" . _("Group/Account Name") . "</td>
237         <td class='tableheader'>" . _("Period") . "</td>
238                 <td class='tableheader'>" . $compare_types[$compare] . "</td>
239                 <td class='tableheader'>" . _("Achieved %") . "</td>
240         </tr>"; 
241         
242         if (!$drilldown) //Root Level
243         {
244                 $salesper = 0.0;
245                 $salesacc = 0.0;        
246         
247                 //Get classes for PL
248                 $classresult = get_account_classes(false, 0);
249                 while ($class = db_fetch($classresult))
250                 {
251                         $class_per_total = 0;
252                         $class_acc_total = 0;
253                         $convert = get_class_type_convert($class["ctype"]);             
254                         
255                         //Print Class Name      
256                         table_section_title($class["class_name"],4);    
257                         echo $tableheader;
258                         
259                         //Get Account groups/types under this group/type
260                         $typeresult = get_account_types(false, $class['cid'], -1);
261                         $k = 0; // row color
262                         while ($accounttype=db_fetch($typeresult))
263                         {
264                                 $TypeTotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, 
265                                         $compare, $convert, $dimension, $dimension2, $drilldown);
266                                 $class_per_total += $TypeTotal[0];
267                                 $class_acc_total += $TypeTotal[1];      
268
269                                 if ($TypeTotal[0] != 0 || $TypeTotal[1] != 0 )
270                                 {
271                                         $url = "<a href='$path_to_root/gl/inquiry/profit_loss.php?TransFromDate=" 
272                                                 . $from . "&TransToDate=" . $to . "&Compare=" . $compare . "&Dimension=" . $dimension . "&Dimension2=" . $dimension2
273                                                 . "&AccGrp=" . $accounttype['id'] ."'>" . $accounttype['id'] . " " . $accounttype['name'] ."</a>";
274                                                 
275                                         alt_table_row_color($k);
276                                         label_cell($url);
277                                         amount_cell($TypeTotal[0] * $convert);
278                                         amount_cell($TypeTotal[1] * $convert);
279                                         amount_cell(Achieve($TypeTotal[0], $TypeTotal[1]));
280                                         end_row();
281                                 }
282                         }
283                         
284                         //Print Class Summary
285                         
286                         start_row("class='inquirybg' style='font-weight:bold'");
287                         label_cell(_('Total') . " " . $class["class_name"]);
288                         amount_cell($class_per_total * $convert);
289                         amount_cell($class_acc_total * $convert);
290                         amount_cell(Achieve($class_per_total, $class_acc_total));
291                         end_row();                      
292                         
293                         $salesper += $class_per_total;
294                         $salesacc += $class_acc_total;
295                 }
296                 
297                 start_row("class='inquirybg' style='font-weight:bold'");
298                 label_cell(_('Calculated Return'));
299                 amount_cell($salesper *-1);
300                 amount_cell($salesacc * -1);
301                 amount_cell(achieve($salesper, $salesacc));
302                 end_row();              
303
304         }
305         else 
306         {
307                 //Level Pointer : Global variable defined in order to control display of root 
308                 global $levelptr;
309                 $levelptr = 0;
310                 
311                 $accounttype = get_account_type($_POST["AccGrp"]);
312                 $classid = $accounttype["class_id"];
313                 $class = get_account_class($classid);
314                 $convert = get_class_type_convert($class["ctype"]); 
315                 
316                 //Print Class Name      
317                 table_section_title($_POST["AccGrp"] . " " . get_account_type_name($_POST["AccGrp"]),4);        
318                 echo $tableheader;
319                 
320                 $classtotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, 
321                         $compare, $convert, $dimension, $dimension2, $drilldown);
322                 
323         }
324                 
325
326         end_table(); // outer table
327         hyperlink_params($_SERVER['PHP_SELF'], _("Back"), "TransFromDate=". $from . "&TransToDate=" . $to . "&Dimension=" . $dimension . "&Dimension2=" . $dimension2);
328         div_end();
329 }
330
331 //----------------------------------------------------------------------------------------------------
332
333 start_form();
334
335 inquiry_controls();
336
337 display_profit_and_loss(get_post('Compare'));
338
339 end_form();
340
341 end_page(false, true);
342