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_RECONCILE';
14 include($path_to_root . "/includes/db_pager.inc");
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");
22 include_once($path_to_root . "/includes/banking.inc");
25 if ($SysPrefs->use_popup_windows)
26 $js .= get_js_open_window(800, 500);
27 if (user_use_date_picker())
28 $js .= get_js_date_picker();
30 add_js_file('reconcile.js');
32 page(_($help_context = "Reconcile Bank Account"), false, false, "", $js);
34 check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));
36 function check_date() {
37 if (!is_date(get_post('reconcile_date'))) {
38 display_error(_("Invalid reconcile date format"));
39 set_focus('reconcile_date');
45 // This function can be used directly in table pager
46 // if we would like to change page layout.
48 function rec_checkbox($row)
50 $name = "rec_" .$row['id'];
51 $hidden = 'last['.$row['id'].']';
52 $value = $row['reconciled'] != '';
54 // save also in hidden field for testing during 'Reconcile'
55 return checkbox(null, $name, $value, true, _('Reconcile this transaction'))
56 . hidden($hidden, $value, false);
59 function systype_name($dummy, $type)
61 global $systypes_array;
63 return $systypes_array[$type];
66 function trans_view($trans)
68 return get_trans_view_str($trans["type"], $trans["trans_no"]);
71 function gl_view($row)
73 return get_gl_view_str($row["type"], $row["trans_no"]);
76 function fmt_debit($row)
78 $value = $row["amount"];
79 return $value>=0 ? price_format($value) : '';
82 function fmt_credit($row)
84 $value = -$row["amount"];
85 return $value>0 ? price_format($value) : '';
88 function fmt_person($trans)
90 return get_counterparty_name($trans["type"], $trans["trans_no"]);
93 function fmt_memo($row)
95 $value = $row["memo_"];
99 function update_data()
103 unset($_POST["beg_balance"]);
104 unset($_POST["end_balance"]);
105 $Ajax->activate('summary');
107 //---------------------------------------------------------------------------------------------
108 // Update db record if respective checkbox value has changed.
110 function change_tpl_flag($reconcile_id)
115 && check_value("rec_".$reconcile_id)) // temporary fix
118 if (get_post('bank_date')=='') // new reconciliation
119 $Ajax->activate('bank_date');
121 $_POST['bank_date'] = date2sql(get_post('reconcile_date'));
122 $reconcile_value = check_value("rec_".$reconcile_id)
123 ? ("'".$_POST['bank_date'] ."'") : 'NULL';
125 update_reconciled_values($reconcile_id, $reconcile_value, $_POST['reconcile_date'],
126 input_num('end_balance'), $_POST['bank_account']);
128 $Ajax->activate('reconciled');
129 $Ajax->activate('difference');
133 function set_tpl_flag($reconcile_id)
137 if (check_value("rec_".$reconcile_id))
140 if (get_post('bank_date')=='') // new reconciliation
141 $Ajax->activate('bank_date');
143 $_POST['bank_date'] = date2sql(get_post('reconcile_date'));
144 $reconcile_value = ("'".$_POST['bank_date'] ."'");
146 update_reconciled_values($reconcile_id, $reconcile_value, $_POST['reconcile_date'],
147 input_num('end_balance'), $_POST['bank_account']);
149 $Ajax->activate('reconciled');
150 $Ajax->activate('difference');
153 if (!isset($_POST['reconcile_date'])) { // init page
154 $_POST['reconcile_date'] = new_doc_date();
155 // $_POST['bank_date'] = date2sql(Today());
158 if (list_updated('bank_account')) {
159 $Ajax->activate('bank_date');
162 if (list_updated('bank_date')) {
163 $_POST['reconcile_date'] =
164 get_post('bank_date')=='' ? Today() : sql2date($_POST['bank_date']);
167 if (get_post('_reconcile_date_changed')) {
168 $_POST['bank_date'] = check_date() ? date2sql(get_post('reconcile_date')) : '';
169 $Ajax->activate('bank_date');
173 $id = find_submit('_rec_');
175 change_tpl_flag($id);
178 if (isset($_POST['Reconcile'])) {
179 set_focus('bank_date');
180 foreach($_POST['last'] as $id => $value)
181 if ($value != check_value('rec_'.$id))
182 if(!change_tpl_flag($id)) break;
184 $Ajax->activate('_page_body');
187 if (isset($_POST['ReconcileAll'])) {
188 set_focus('bank_date');
189 foreach($_POST['last'] as $id => $value)
192 $Ajax->activate('_page_body');
195 //------------------------------------------------------------------------------------------------
197 start_table(TABLESTYLE_NOBORDER);
199 bank_accounts_list_cells(_("Account:"), 'bank_account', null, true);
201 bank_reconciliation_list_cells(_("Bank Statement:"), get_post('bank_account'),
202 'bank_date', null, true, _("New"));
206 $result = get_max_reconciled(get_post('reconcile_date'), $_POST['bank_account']);
208 if ($row = db_fetch($result)) {
209 $_POST["reconciled"] = price_format($row["end_balance"]-$row["beg_balance"]);
210 $total = $row["total"];
211 if (!isset($_POST["beg_balance"])) { // new selected account/statement
212 $_POST["last_date"] = sql2date($row["last_date"]);
213 $_POST["beg_balance"] = price_format($row["beg_balance"]);
214 $_POST["end_balance"] = price_format($row["end_balance"]);
215 if (get_post('bank_date')) {
216 // if it is the last updated bank statement retrieve ending balance
218 $row = get_ending_reconciled($_POST['bank_account'], $_POST['bank_date']);
220 $_POST["end_balance"] = price_format($row["ending_reconcile_balance"]);
228 div_start('summary');
230 start_table(TABLESTYLE);
231 $th = array(_("Reconcile Date"), _("Beginning<br>Balance"),
232 _("Ending<br>Balance"), _("Account<br>Total"),_("Reconciled<br>Amount"), _("Difference"));
236 date_cells("", "reconcile_date", _('Date of bank statement to reconcile'),
237 get_post('bank_date')=='', 0, 0, 0, null, true);
239 amount_cells_ex("", "beg_balance", 15);
241 amount_cells_ex("", "end_balance", 15);
243 $reconciled = input_num('reconciled');
244 $difference = input_num("end_balance") - input_num("beg_balance") - $reconciled;
247 amount_cell($reconciled, false, '', "reconciled");
248 amount_cell($difference, false, '', "difference");
254 //------------------------------------------------------------------------------------------------
256 if (!isset($_POST['bank_account']))
257 $_POST['bank_account'] = "";
259 $sql = get_sql_for_bank_account_reconcile(get_post('bank_account'), get_post('reconcile_date'));
261 $act = get_bank_account($_POST["bank_account"]);
262 display_heading($act['bank_account_name']." - ".$act['bank_curr_code']);
266 _("Type") => array('fun'=>'systype_name', 'ord'=>''),
267 _("#") => array('fun'=>'trans_view', 'ord'=>''),
270 _("Debit") => array('align'=>'right', 'fun'=>'fmt_debit'),
271 _("Credit") => array('align'=>'right','insert'=>true, 'fun'=>'fmt_credit'),
272 _("Person/Item") => array('fun'=>'fmt_person'),
273 _("Memo") => array('fun'=>'fmt_memo'),
274 array('insert'=>true, 'fun'=>'gl_view'),
275 "X"=>array('insert'=>true, 'fun'=>'rec_checkbox')
277 $table =& new_db_pager('trans_tbl', $sql, $cols);
279 $table->width = "80%";
280 display_db_pager($table);
284 submit('Reconcile', _("Reconcile"), true, '', null);
285 submit('ReconcileAll', _("Reconcile All"), true, '');
289 //------------------------------------------------------------------------------------------------