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