New files from unstable branch
[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         $dim = get_company_pref('use_dimension');
65     start_form();
66
67     start_table(TABLESTYLE_NOBORDER);
68         start_row();
69     gl_all_accounts_list_cells(_("Account:"), 'account', null, false, false, _("All Accounts"));
70         date_cells(_("from:"), 'TransFromDate', '', null, -30);
71         date_cells(_("to:"), 'TransToDate');
72     end_row();
73         end_table();
74
75         start_table(TABLESTYLE_NOBORDER);
76         start_row();
77         if ($dim >= 1)
78                 dimensions_list_cells(_("Dimension")." 1:", 'Dimension', null, true, " ", false, 1);
79         if ($dim > 1)
80                 dimensions_list_cells(_("Dimension")." 2:", 'Dimension2', null, true, " ", false, 2);
81
82         small_amount_cells(_("Amount min:"), 'amount_min', null, " ");
83         small_amount_cells(_("Amount max:"), 'amount_max', null, " ");
84         submit_cells('Show',_("Show"),'','', 'default');
85         end_row();
86         end_table();
87
88         echo '<hr>';
89     end_form();
90 }
91
92 //----------------------------------------------------------------------------------------------------
93
94 function show_results()
95 {
96         global $path_to_root, $systypes_array;
97
98         if (!isset($_POST["account"]))
99                 $_POST["account"] = null;
100
101         $act_name = $_POST["account"] ? get_gl_account_name($_POST["account"]) : "";
102         $dim = get_company_pref('use_dimension');
103
104     /*Now get the transactions  */
105     if (!isset($_POST['Dimension']))
106         $_POST['Dimension'] = 0;
107     if (!isset($_POST['Dimension2']))
108         $_POST['Dimension2'] = 0;
109         $result = get_gl_transactions($_POST['TransFromDate'], $_POST['TransToDate'], -1,
110         $_POST["account"], $_POST['Dimension'], $_POST['Dimension2'], null,
111         input_num('amount_min'), input_num('amount_max'));
112
113         $colspan = ($dim == 2 ? "6" : ($dim == 1 ? "5" : "4"));
114
115         if ($_POST["account"] != null)
116                 display_heading($_POST["account"]. "&nbsp;&nbsp;&nbsp;".$act_name);
117
118         // Only show balances if an account is specified AND we're not filtering by amounts
119         $show_balances = $_POST["account"] != null && 
120                      input_num("amount_min") == 0 && 
121                      input_num("amount_max") == 0;
122                 
123         start_table(TABLESTYLE);
124         
125         $first_cols = array(_("Type"), _("#"), _("Date"));
126         
127         if ($_POST["account"] == null)
128             $account_col = array(_("Account"));
129         else
130             $account_col = array();
131         
132         if ($dim == 2)
133                 $dim_cols = array(_("Dimension")." 1", _("Dimension")." 2");
134         else if ($dim == 1)
135                 $dim_cols = array(_("Dimension"));
136         else
137                 $dim_cols = array();
138         
139         if ($show_balances)
140             $remaining_cols = array(_("Person/Item"), _("Debit"), _("Credit"), _("Balance"), _("Memo"));
141         else
142             $remaining_cols = array(_("Person/Item"), _("Debit"), _("Credit"), _("Memo"));
143             
144         $th = array_merge($first_cols, $account_col, $dim_cols, $remaining_cols);
145                         
146         table_header($th);
147         if ($_POST["account"] != null && is_account_balancesheet($_POST["account"]))
148                 $begin = "";
149         else
150         {
151                 $begin = begin_fiscalyear();
152                 if (date1_greater_date2($begin, $_POST['TransFromDate']))
153                         $begin = $_POST['TransFromDate'];
154                 $begin = add_days($begin, -1);
155         }
156
157         $bfw = 0;
158         if ($show_balances) {
159             $bfw = get_gl_balance_from_to($begin, $_POST['TransFromDate'], $_POST["account"], $_POST['Dimension'], $_POST['Dimension2']);
160         start_row("class='inquirybg'");
161         label_cell("<b>"._("Opening Balance")." - ".$_POST['TransFromDate']."</b>", "colspan=$colspan");
162         display_debit_or_credit_cells($bfw, true);
163         label_cell("");
164         label_cell("");
165         end_row();
166         }
167         
168         $running_total = $bfw;
169         $j = 1;
170         $k = 0; //row colour counter
171
172         while ($myrow = db_fetch($result))
173         {
174
175         alt_table_row_color($k);
176
177         $running_total += $myrow["amount"];
178
179         $trandate = sql2date($myrow["tran_date"]);
180
181         label_cell($systypes_array[$myrow["type"]]);
182                 label_cell(get_gl_view_str($myrow["type"], $myrow["type_no"], $myrow["type_no"], true));
183         label_cell($trandate);
184         
185         if ($_POST["account"] == null)
186             label_cell($myrow["account"] . ' ' . get_gl_account_name($myrow["account"]));
187         
188                 if ($dim >= 1)
189                         label_cell(get_dimension_string($myrow['dimension_id'], true));
190                 if ($dim > 1)
191                         label_cell(get_dimension_string($myrow['dimension2_id'], true));
192                 label_cell(payment_person_name($myrow["person_type_id"],$myrow["person_id"]));
193                 display_debit_or_credit_cells($myrow["amount"]);
194                 if ($show_balances)
195                     amount_cell($running_total);
196         label_cell($myrow['memo_']);
197         end_row();
198
199         $j++;
200         if ($j == 12)
201         {
202                 $j = 1;
203                 table_header($th);
204         }
205         }
206         //end of while loop
207
208         if ($show_balances) {
209         start_row("class='inquirybg'");
210         label_cell("<b>" . _("Ending Balance") ." - ".$_POST['TransToDate']. "</b>", "colspan=$colspan");
211         display_debit_or_credit_cells($running_total, true);
212         label_cell("");
213         label_cell("");
214         end_row();
215         }
216
217         end_table(2);
218         if (db_num_rows($result) == 0)
219                 display_note(_("No general ledger transactions have been created for the specified criteria."), 0, 1);
220
221 }
222
223 //----------------------------------------------------------------------------------------------------
224
225 gl_inquiry_controls();
226
227 div_start('trans_tbl');
228
229 if (get_post('Show') || get_post('account'))
230     show_results();
231
232 div_end();
233
234 //----------------------------------------------------------------------------------------------------
235
236 end_page();
237
238 ?>