Added a Balance Sheet drilldown inquiry in Banking and GL
[fa-stable.git] / gl / inquiry / gl_account_inquiry.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_GLTRANSVIEW';
13 $path_to_root = "../..";
14 include_once($path_to_root . "/includes/session.inc");
15
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 set_focus('account');
25 if ($use_popup_windows)
26         $js .= get_js_open_window(800, 500);
27 if ($use_date_picker)
28         $js .= get_js_date_picker();
29
30 page(_($help_context = "General Ledger Inquiry"), false, false, '', $js);
31
32 //----------------------------------------------------------------------------------------------------
33 // Ajax updates
34 //
35 if (get_post('Show')) 
36 {
37         $Ajax->activate('trans_tbl');
38 }
39
40 if (isset($_GET["account"]))
41         $_POST["account"] = $_GET["account"];
42 if (isset($_GET["TransFromDate"]))
43         $_POST["TransFromDate"] = $_GET["TransFromDate"];
44 if (isset($_GET["TransToDate"]))
45         $_POST["TransToDate"] = $_GET["TransToDate"];
46 if (isset($_GET["Dimension"]))
47         $_POST["Dimension"] = $_GET["Dimension"];
48 if (isset($_GET["Dimension2"]))
49         $_POST["Dimension2"] = $_GET["Dimension2"];
50 if (isset($_GET["amount_min"]))
51         $_POST["amount_min"] = $_GET["amount_min"];
52 if (isset($_GET["amount_max"]))
53         $_POST["amount_max"] = $_GET["amount_max"];
54
55 if (!isset($_POST["amount_min"]))
56         $_POST["amount_min"] = price_format(0);
57 if (!isset($_POST["amount_max"]))
58         $_POST["amount_max"] = price_format(0);
59
60 //----------------------------------------------------------------------------------------------------
61
62 function gl_inquiry_controls()
63 {
64         global $table_style2;
65
66         $dim = get_company_pref('use_dimension');
67     start_form();
68
69     start_table("class='tablestyle_noborder'");
70         start_row();
71     gl_all_accounts_list_cells(_("Account:"), 'account', null, false, false, "All Accounts");
72         date_cells(_("from:"), 'TransFromDate', '', null, -30);
73         date_cells(_("to:"), 'TransToDate');
74     end_row();
75         end_table();
76
77         start_table();
78         start_row();
79         if ($dim >= 1)
80                 dimensions_list_cells(_("Dimension")." 1:", 'Dimension', null, true, " ", false, 1);
81         if ($dim > 1)
82                 dimensions_list_cells(_("Dimension")." 2:", 'Dimension2', null, true, " ", false, 2);
83
84         small_amount_cells(_("Amount min:"), 'amount_min', null);
85         small_amount_cells(_("Amount max:"), 'amount_max', null);
86         submit_cells('Show',_("Show"),'','', 'default');
87         end_row();
88         end_table();
89
90         echo '<hr>';
91     end_form();
92 }
93
94 //----------------------------------------------------------------------------------------------------
95
96 function show_results()
97 {
98         global $path_to_root, $table_style, $systypes_array;
99
100         if (!isset($_POST["account"]))
101                 $_POST["account"] = null;
102
103         $act_name = $_POST["account"] ? get_gl_account_name($_POST["account"]) : "";
104         $dim = get_company_pref('use_dimension');
105
106     /*Now get the transactions  */
107     if (!isset($_POST['Dimension']))
108         $_POST['Dimension'] = 0;
109     if (!isset($_POST['Dimension2']))
110         $_POST['Dimension2'] = 0;
111         $result = get_gl_transactions($_POST['TransFromDate'], $_POST['TransToDate'], -1,
112         $_POST["account"], $_POST['Dimension'], $_POST['Dimension2'], null,
113         input_num('amount_min'), input_num('amount_max'));
114
115         $colspan = ($dim == 2 ? "6" : ($dim == 1 ? "5" : "4"));
116
117         if ($_POST["account"] != null)
118                 display_heading($_POST["account"]. "&nbsp;&nbsp;&nbsp;".$act_name);
119
120         // Only show balances if an account is specified AND we're not filtering by amounts
121         $show_balances = $_POST["account"] != null && 
122                      input_num("amount_min") == 0 && 
123                      input_num("amount_max") == 0;
124                 
125         start_table($table_style);
126         
127         $first_cols = array(_("Type"), _("#"), _("Date"));
128         
129         if ($_POST["account"] == null)
130             $account_col = array(_("Account"));
131         else
132             $account_col = array();
133         
134         if ($dim == 2)
135                 $dim_cols = array(_("Dimension")." 1", _("Dimension")." 2");
136         else if ($dim == 1)
137                 $dim_cols = array(_("Dimension"));
138         else
139                 $dim_cols = array();
140         
141         if ($show_balances)
142             $remaining_cols = array(_("Person/Item"), _("Debit"), _("Credit"), _("Balance"), _("Memo"));
143         else
144             $remaining_cols = array(_("Person/Item"), _("Debit"), _("Credit"), _("Memo"));
145             
146         $th = array_merge($first_cols, $account_col, $dim_cols, $remaining_cols);
147                         
148         table_header($th);
149         if ($_POST["account"] != null && is_account_balancesheet($_POST["account"]))
150                 $begin = "";
151         else
152         {
153                 $begin = begin_fiscalyear();
154                 if (date1_greater_date2($begin, $_POST['TransFromDate']))
155                         $begin = $_POST['TransFromDate'];
156                 $begin = add_days($begin, -1);
157         }
158
159         $bfw = 0;
160         if ($show_balances) {
161             $bfw = get_gl_balance_from_to($begin, $_POST['TransFromDate'], $_POST["account"], $_POST['Dimension'], $_POST['Dimension2']);
162         start_row("class='inquirybg'");
163         label_cell("<b>"._("Opening Balance")." - ".$_POST['TransFromDate']."</b>", "colspan=$colspan");
164         display_debit_or_credit_cells($bfw);
165         label_cell("");
166         label_cell("");
167         end_row();
168         }
169         
170         $running_total = $bfw;
171         $j = 1;
172         $k = 0; //row colour counter
173
174         while ($myrow = db_fetch($result))
175         {
176
177         alt_table_row_color($k);
178
179         $running_total += $myrow["amount"];
180
181         $trandate = sql2date($myrow["tran_date"]);
182
183         label_cell($systypes_array[$myrow["type"]]);
184                 label_cell(get_gl_view_str($myrow["type"], $myrow["type_no"], $myrow["type_no"], true));
185         label_cell($trandate);
186         
187         if ($_POST["account"] == null)
188             label_cell($myrow["account"] . ' ' . get_gl_account_name($myrow["account"]));
189         
190                 if ($dim >= 1)
191                         label_cell(get_dimension_string($myrow['dimension_id'], true));
192                 if ($dim > 1)
193                         label_cell(get_dimension_string($myrow['dimension2_id'], true));
194                 label_cell(payment_person_name($myrow["person_type_id"],$myrow["person_id"]));
195                 display_debit_or_credit_cells($myrow["amount"]);
196                 if ($show_balances)
197                     amount_cell($running_total);
198         label_cell($myrow['memo_']);
199         end_row();
200
201         $j++;
202         if ($j == 12)
203         {
204                 $j = 1;
205                 table_header($th);
206         }
207         }
208         //end of while loop
209
210         if ($show_balances) {
211         start_row("class='inquirybg'");
212         label_cell("<b>" . _("Ending Balance") ." - ".$_POST['TransToDate']. "</b>", "colspan=$colspan");
213         display_debit_or_credit_cells($running_total);
214         label_cell("");
215         label_cell("");
216         end_row();
217         }
218
219         end_table(2);
220         if (db_num_rows($result) == 0)
221                 display_note(_("No general ledger transactions have been created for the specified criteria."), 0, 1);
222
223 }
224
225 //----------------------------------------------------------------------------------------------------
226
227 gl_inquiry_controls();
228
229 div_start('trans_tbl');
230
231 if (get_post('Show') || get_post('account'))
232     show_results();
233
234 div_end();
235
236 //----------------------------------------------------------------------------------------------------
237
238 end_page();
239
240 ?>