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