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