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="../..";
15 include_once($path_to_root . "/includes/session.inc");
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");
21 include_once($path_to_root . "/gl/includes/gl_db.inc");
25 $js = get_js_date_picker();
27 page(_($help_context = "Profit & Loss Drilldown"), false, false, "", $js);
29 //----------------------------------------------------------------------------------------------------
34 $Ajax->activate('pl_tbl');
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"];
46 //----------------------------------------------------------------------------------------------------
48 function display_type ($type, $typename, $from, $to, $begin, $end, $compare, $convert,
49 &$dec, &$pdec, &$rep, $dimension=0, $dimension2=0, $drilldown, $path_to_root)
53 $code_per_balance = 0;
54 $code_acc_balance = 0;
55 $per_balance_total = 0;
56 $acc_balance_total = 0;
58 $totals_arr = array();
60 //Get Accounts directly under this group/type
61 $result = get_gl_accounts(null, null, $type);
63 while ($account=db_fetch($result))
65 $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
68 $acc_balance = get_budget_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
70 $acc_balance = get_gl_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
71 if (!$per_balance && !$acc_balance)
74 if ($drilldown && $levelptr == 0)
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>";
81 start_row("class='stockmankobg'");
83 amount_cell($per_balance * $convert);
84 amount_cell($acc_balance * $convert);
85 amount_cell(Achieve($per_balance, $acc_balance));
89 $code_per_balance += $per_balance;
90 $code_acc_balance += $acc_balance;
95 //Get Account groups/types under this group/type
96 $result = get_account_types(false, false, $type);
97 while ($accounttype=db_fetch($result))
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];
105 //Display Type Summary if total is != 0
106 if (($code_per_balance + $per_balance_total + $code_acc_balance + $acc_balance_total) != 0)
108 if ($drilldown && $type == $_POST["AccGrp"])
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)));
117 //START Patch#1 : Display only direct child types
118 $acctype1 = get_account_type($type);
119 $parent1 = $acctype1["parent"];
120 if ($drilldown && $parent1 == $_POST["AccGrp"])
122 //elseif ($drilldown && $type != $_POST["AccGrp"])
124 $url = "<a href='$path_to_root/gl/inquiry/profit_loss.php?TransFromDate="
125 . $from . "&TransToDate=" . $to . "&Compare=" . $compare
126 . "&AccGrp=" . $type ."'>" . $typename ."</a>";
128 alt_table_row_color($k);
130 amount_cell(($code_per_balance + $per_balance_total) * $convert);
131 amount_cell(($code_acc_balance + $acc_balance_total) * $convert);
132 amount_cell(Achieve(($code_per_balance + $per_balance_total), ($code_acc_balance + $acc_balance_total)));
137 $totals_arr[0] = $code_per_balance + $per_balance_total;
138 $totals_arr[1] = $code_acc_balance + $acc_balance_total;
142 function Achieve($d1, $d2)
144 if ($d1 == 0 && $d2 == 0)
148 $ret = ($d1 / $d2 * 100.0);
154 function inquiry_controls()
156 start_table("class='tablestyle_noborder'");
158 date_cells(_("From:"), 'TransFromDate', '', null, -30);
159 date_cells(_("To:"), 'TransToDate');
163 $sel = array(_("Accumulated"), _("Period Y-1"), _("Budget"));
164 echo "<td>"._("Compare to").":</td>\n";
166 echo array_selector('Compare', null, $sel);
169 submit_cells('Show',_("Show"),'','', 'default');
175 //----------------------------------------------------------------------------------------------------
177 function display_profit_and_loss()
179 global $comp_path, $path_to_root, $table_style, $sel;
181 $dim = get_company_pref('use_dimension');
182 $dimension = $dimension2 = 0;
184 $from = $_POST['TransFromDate'];
185 $to = $_POST['TransToDate'];
186 $compare = $_POST['Compare'];
188 if (isset($_POST["AccGrp"]) && (strlen($_POST['AccGrp']) > 0))
189 $drilldown = 1; // Deeper Level
191 $drilldown = 0; // Root level
194 $pdec = user_percent_dec();
196 if ($compare == 0 || $compare == 2)
204 $begin = begin_fiscalyear();
206 elseif ($compare == 1)
208 $begin = add_months($from, -12);
209 $end = add_months($to, -12);
214 start_table("width=50% $table_style");
217 <td class='tableheader'>" . _("Group/Account Name") . "</td>
218 <td class='tableheader'>" . _("Period") . "</td>
219 <td class='tableheader'>" . $sel[$compare] . "</td>
220 <td class='tableheader'>" . _("Achieved %") . "</td>
223 if (!$drilldown) //Root Level
232 $classresult = get_account_classes(false, 0);
233 while ($class = db_fetch($classresult))
235 $class_per_total = 0;
236 $class_acc_total = 0;
237 $convert = get_class_type_convert($class["ctype"]);
240 table_section_title($class["class_name"],4);
243 //Get Account groups/types under this group/type
244 $typeresult = get_account_types(false, $class['cid'], -1);
245 while ($accounttype=db_fetch($typeresult))
247 $TypeTotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert,
248 $dec, $pdec, $rep, $dimension, $dimension2, $drilldown, $path_to_root);
249 $class_per_total += $TypeTotal[0];
250 $class_acc_total += $TypeTotal[1];
252 if ($TypeTotal[0] != 0 || $TypeTotal[1] != 0 )
254 $url = "<a href='$path_to_root/gl/inquiry/profit_loss.php?TransFromDate="
255 . $from . "&TransToDate=" . $to . "&Compare=" . $compare
256 . "&AccGrp=" . $accounttype['id'] ."'>" . $accounttype['name'] ."</a>";
258 alt_table_row_color($k);
260 amount_cell($TypeTotal[0] * $convert);
261 amount_cell($TypeTotal[1] * $convert);
262 amount_cell(Achieve($TypeTotal[0], $TypeTotal[1]));
267 //Print Class Summary
269 start_row("class='inquirybg' style='font-weight:bold'");
270 label_cell(_('Total') . " " . $class["class_name"]);
271 amount_cell($class_per_total * $convert);
272 amount_cell($class_acc_total * $convert);
273 amount_cell(Achieve($class_per_total, $class_acc_total));
276 $salesper += $class_per_total;
277 $salesacc += $class_acc_total;
280 start_row("class='inquirybg' style='font-weight:bold'");
281 label_cell(_('Calculated Return'));
282 amount_cell($salesper *-1);
283 amount_cell($salesacc * -1);
284 amount_cell(achieve($salesper, $salesacc));
290 //Level Pointer : Global variable defined in order to control display of root
294 $accounttype = get_account_type($_POST["AccGrp"]);
295 $classid = $accounttype["class_id"];
296 $class = get_account_class($classid);
297 $convert = get_class_type_convert($class["ctype"]);
300 table_section_title(get_account_type_name($_POST["AccGrp"]),4);
303 $classtotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert,
304 $dec, $pdec, $rep, $dimension, $dimension2, $drilldown, $path_to_root);
309 end_table(1); // outer table
313 //----------------------------------------------------------------------------------------------------
319 display_profit_and_loss();