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