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");
24 if (user_use_date_picker())
25 $js = get_js_date_picker();
27 page(_($help_context = "Profit & Loss Drilldown"), false, false, "", $js);
29 $compare_types = array(
34 //----------------------------------------------------------------------------------------------------
39 $Ajax->activate('pl_tbl');
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"];
55 //----------------------------------------------------------------------------------------------------
57 function display_type ($type, $typename, $from, $to, $begin, $end, $compare, $convert,
58 $dimension=0, $dimension2=0, $drilldown, $path_to_root)
62 $code_per_balance = 0;
63 $code_acc_balance = 0;
64 $per_balance_total = 0;
65 $acc_balance_total = 0;
67 $totals_arr = array();
69 //Get Accounts directly under this group/type
70 $result = get_gl_accounts(null, null, $type);
72 while ($account=db_fetch($result))
74 $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
77 $acc_balance = get_budget_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
79 $acc_balance = get_gl_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
80 if (!$per_balance && !$acc_balance)
83 if ($drilldown && $levelptr == 0)
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>";
90 start_row("class='stockmankobg'");
92 amount_cell($per_balance * $convert);
93 amount_cell($acc_balance * $convert);
94 amount_cell(Achieve($per_balance, $acc_balance));
98 $code_per_balance += $per_balance;
99 $code_acc_balance += $acc_balance;
104 //Get Account groups/types under this group/type
105 $result = get_account_types(false, false, $type);
106 while ($accounttype=db_fetch($result))
108 $totals_arr = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end,
109 $compare, $convert, $dimension, $dimension2, $drilldown, $path_to_root);
110 $per_balance_total += $totals_arr[0];
111 $acc_balance_total += $totals_arr[1];
114 //Display Type Summary if total is != 0
115 if (($code_per_balance + $per_balance_total + $code_acc_balance + $acc_balance_total) != 0)
117 if ($drilldown && $type == $_POST["AccGrp"])
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)));
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"])
131 //elseif ($drilldown && $type != $_POST["AccGrp"])
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>";
137 alt_table_row_color($k);
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)));
146 $totals_arr[0] = $code_per_balance + $per_balance_total;
147 $totals_arr[1] = $code_acc_balance + $acc_balance_total;
151 function Achieve($d1, $d2)
153 if ($d1 == 0 && $d2 == 0)
157 $ret = ($d1 / $d2 * 100.0);
163 function inquiry_controls()
165 global $compare_types;
167 $dim = get_company_pref('use_dimension');
168 start_table(TABLESTYLE_NOBORDER);
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');
178 echo "<td>"._("Compare to").":</td>\n";
180 echo array_selector('Compare', null, $compare_types);
184 dimensions_list_cells(_("Dimension")." 1:", 'Dimension', null, true, " ", false, 1);
186 dimensions_list_cells(_("Dimension")." 2:", 'Dimension2', null, true, " ", false, 2);
188 submit_cells('Show',_("Show"),'','', 'default');
194 //----------------------------------------------------------------------------------------------------
196 function display_profit_and_loss($compare)
198 global $path_to_root, $compare_types;
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'];
207 $from = $_POST['TransFromDate'];
208 $to = $_POST['TransToDate'];
210 if (isset($_POST["AccGrp"]) && (strlen($_POST['AccGrp']) > 0))
211 $drilldown = 1; // Deeper Level
213 $drilldown = 0; // Root level
215 if ($compare == 0 || $compare == 2)
223 $begin = begin_fiscalyear();
225 elseif ($compare == 1)
227 $begin = add_months($from, -12);
228 $end = add_months($to, -12);
233 start_table(TABLESTYLE, "width='50%'");
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>
242 if (!$drilldown) //Root Level
248 $classresult = get_account_classes(false, 0);
249 while ($class = db_fetch($classresult))
251 $class_per_total = 0;
252 $class_acc_total = 0;
253 $convert = get_class_type_convert($class["ctype"]);
256 table_section_title($class["class_name"],4);
259 //Get Account groups/types under this group/type
260 $typeresult = get_account_types(false, $class['cid'], -1);
262 while ($accounttype=db_fetch($typeresult))
264 $TypeTotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert,
265 $dimension, $dimension2, $drilldown, $path_to_root);
266 $class_per_total += $TypeTotal[0];
267 $class_acc_total += $TypeTotal[1];
269 if ($TypeTotal[0] != 0 || $TypeTotal[1] != 0 )
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>";
275 alt_table_row_color($k);
277 amount_cell($TypeTotal[0] * $convert);
278 amount_cell($TypeTotal[1] * $convert);
279 amount_cell(Achieve($TypeTotal[0], $TypeTotal[1]));
284 //Print Class Summary
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));
293 $salesper += $class_per_total;
294 $salesacc += $class_acc_total;
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));
307 //Level Pointer : Global variable defined in order to control display of root
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"]);
317 table_section_title($_POST["AccGrp"] . " " . get_account_type_name($_POST["AccGrp"]),4);
320 $classtotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert,
321 $dimension, $dimension2, $drilldown, $path_to_root);
326 end_table(); // outer table
327 hyperlink_params($_SERVER['PHP_SELF'], _("Back"), "TransFromDate=". $from . "&TransToDate=" . $to . "&Dimension=" . $dimension . "&Dimension2=" . $dimension2);
331 //----------------------------------------------------------------------------------------------------
337 display_profit_and_loss(get_post('Compare'));
341 end_page(false, true);