+
+function fmt_debit($row)
+{
+ $value = $row["amount"];
+ return $value>=0 ? price_format($value) : '';
+}
+
+function fmt_credit($row)
+{
+ $value = -$row["amount"];
+ return $value>0 ? price_format($value) : '';
+}
+
+function fmt_person($row)
+{
+ return payment_person_name($row["person_type_id"],$row["person_id"]);
+}
+
+$update_pager = false;
+function update_data()
+{
+ global $Ajax, $update_pager;
+
+ unset($_POST["beg_balance"]);
+ unset($_POST["end_balance"]);
+ $Ajax->activate('summary');
+ $update_pager = true;
+}
+//---------------------------------------------------------------------------------------------
+// Update db record if respective checkbox value has changed.
+//
+function change_tpl_flag($reconcile_id)
+{
+ global $Ajax;
+
+ if (!check_date())
+ return false;
+
+ if (get_post('bank_date')=='') // new reconciliation
+ $Ajax->activate('bank_date');
+
+ $_POST['bank_date'] = date2sql(get_post('reconcile_date'));
+ $reconcile_value = check_value("rec_".$reconcile_id)
+ ? ("'".$_POST['bank_date'] ."'") : 'NULL';
+ $sql = "UPDATE ".TB_PREF."bank_trans SET reconciled=".db_escape($reconcile_value)
+ ." WHERE id=".db_escape($reconcile_id);
+
+ db_query($sql, "Can't change reconciliation status");
+ // save last reconcilation status (date, end balance)
+ $sql2="UPDATE ".TB_PREF."bank_accounts SET last_reconciled_date='"
+ .date2sql($_POST["reconcile_date"])."',
+ ending_reconcile_balance=".input_num("end_balance")
+ ." WHERE id=".db_escape($_POST["bank_account"]);
+
+ $result = db_query($sql2,"Error updating reconciliation information");
+ $Ajax->activate('reconciled');
+ $Ajax->activate('difference');
+ return true;
+}
+
+if (!isset($_POST['reconcile_date'])) { // init page
+ $_POST['reconcile_date'] = new_doc_date();
+// $_POST['bank_date'] = date2sql(Today());
+}
+
+if (list_updated('bank_account')) {
+ $Ajax->activate('bank_date');
+ update_data();
+}
+if (list_updated('bank_date')) {
+ $_POST['reconcile_date'] =
+ get_post('bank_date')=='' ? Today() : sql2date($_POST['bank_date']);
+ update_data();
+}
+if (get_post('_reconcile_date_changed')) {
+ $_POST['bank_date'] = check_date() ? date2sql(get_post('reconcile_date')) : '';
+ $Ajax->activate('bank_date');
+ update_data();
+}
+
+$id = find_submit('_rec_');
+if ($id != -1)
+ change_tpl_flag($id);
+
+if (isset($_POST['Reconcile'])) {
+ set_focus('bank_date');
+ foreach($_POST['last'] as $id => $value)
+ if ($value != check_value('rec_'.$id))
+ if(!change_tpl_flag($id)) break;
+ $Ajax->activate('_page_body');
+}
+