Recurrent Invoices: fixed buggy call to non existing function and payment terms type...
[fa-stable.git] / gl / bank_account_reconcile.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_RECONCILE';
13 $path_to_root = "..";
14 include($path_to_root . "/includes/db_pager.inc");
15 include_once($path_to_root . "/includes/session.inc");
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 include_once($path_to_root . "/includes/banking.inc");
23
24 $js = "";
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();
29
30 add_js_file('reconcile.js');
31
32 page(_($help_context = "Reconcile Bank Account"), false, false, "", $js);
33
34 check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));
35
36 function check_date() {
37         if (!is_date(get_post('reconcile_date'))) {
38                 display_error(_("Invalid reconcile date format"));
39                 set_focus('reconcile_date');
40                 return false;
41         }
42         return true;
43 }
44 //
45 //      This function can be used directly in table pager 
46 //      if we would like to change page layout.
47 //
48 function rec_checkbox($row)
49 {
50         $name = "rec_" .$row['id'];
51         $hidden = 'last['.$row['id'].']';
52         $value = $row['reconciled'] != '';
53
54 // save also in hidden field for testing during 'Reconcile'
55         return is_closed_trans($row['type'], $row['trans_no']) ? "--" : checkbox(null, $name, $value, true, _('Reconcile this transaction'))
56                 . hidden($hidden, $value, false);
57 }
58
59 function systype_name($dummy, $type)
60 {
61         global $systypes_array;
62         
63         return $systypes_array[$type];
64 }
65
66 function trans_view($trans)
67 {
68         return get_trans_view_str($trans["type"], $trans["trans_no"]);
69 }
70
71 function gl_view($row)
72 {
73         return get_gl_view_str($row["type"], $row["trans_no"]);
74 }
75
76 function fmt_debit($row)
77 {
78         $value = $row["amount"];
79         return $value>=0 ? price_format($value) : '';
80 }
81
82 function fmt_credit($row)
83 {
84         $value = -$row["amount"];
85         return $value>0 ? price_format($value) : '';
86 }
87
88 function bank_fee($row)
89 {
90         $value = -$row["charge"];
91         return $value > 0 ? price_format($value) : '';
92 }
93
94 function fmt_person($trans)
95 {
96         return get_counterparty_name($trans["type"], $trans["trans_no"]);
97 }
98
99 function fmt_memo($row)
100 {
101         $value = $row["memo_"];
102         return $value;
103 }
104
105 function update_data()
106 {
107         global $Ajax;
108         
109         unset($_POST["beg_balance"]);
110         unset($_POST["end_balance"]);
111         $Ajax->activate('summary');
112 }
113 //---------------------------------------------------------------------------------------------
114 // Update db record if respective checkbox value has changed.
115 //
116 function change_tpl_flag($reconcile_id)
117 {
118         global  $Ajax;
119
120         if (!check_date() 
121                 && check_value("rec_".$reconcile_id)) // temporary fix
122                 return false;
123
124         if (get_post('bank_date')=='')  // new reconciliation
125                 $Ajax->activate('bank_date');
126
127         $_POST['bank_date'] = date2sql(get_post('reconcile_date'));
128         $reconcile_value = check_value("rec_".$reconcile_id) 
129                                                 ? ("'".$_POST['bank_date'] ."'") : 'NULL';
130         
131         update_reconciled_values($reconcile_id, $reconcile_value, $_POST['reconcile_date'],
132                 input_num('end_balance'), $_POST['bank_account']);
133                 
134         $Ajax->activate('reconciled');
135         $Ajax->activate('difference');
136         return true;
137 }
138
139 function set_tpl_flag($reconcile_id)
140 {
141         global  $Ajax;
142
143         if (check_value("rec_".$reconcile_id))
144                 return;
145
146         if (get_post('bank_date')=='')  // new reconciliation
147                 $Ajax->activate('bank_date');
148
149         $_POST['bank_date'] = date2sql(get_post('reconcile_date'));
150         $reconcile_value =  ("'".$_POST['bank_date'] ."'");
151         
152         update_reconciled_values($reconcile_id, $reconcile_value, $_POST['reconcile_date'],
153                 input_num('end_balance'), $_POST['bank_account']);
154                 
155         $Ajax->activate('reconciled');
156         $Ajax->activate('difference');
157 }
158
159 if (!isset($_POST['reconcile_date'])) { // init page
160         $_POST['reconcile_date'] = new_doc_date();
161 //      $_POST['bank_date'] = date2sql(Today());
162 }
163
164 if (list_updated('bank_account')) {
165     $Ajax->activate('bank_date');
166         update_data();
167 }
168 if (list_updated('bank_date')) {
169         $_POST['reconcile_date'] = 
170                 get_post('bank_date')=='' ? Today() : sql2date($_POST['bank_date']);
171         update_data();
172 }
173 if (get_post('_reconcile_date_changed')) {
174         $_POST['bank_date'] = check_date() ? date2sql(get_post('reconcile_date')) : '';
175     $Ajax->activate('bank_date');
176         update_data();
177 }
178
179 $id = find_submit('_rec_');
180 if ($id != -1) 
181         change_tpl_flag($id);
182
183
184 if (isset($_POST['Reconcile'])) {
185         set_focus('bank_date');
186         foreach($_POST['last'] as $id => $value)
187                 if ($value != check_value('rec_'.$id))
188                         if(!change_tpl_flag($id)) break;
189
190     $Ajax->activate('_page_body');
191 }
192
193 if (isset($_POST['ReconcileAll'])) {
194         set_focus('bank_date');
195         foreach($_POST['last'] as $id => $value)
196                 set_tpl_flag($id);
197
198     $Ajax->activate('_page_body');
199 }
200
201 //------------------------------------------------------------------------------------------------
202 start_form();
203 start_table(TABLESTYLE_NOBORDER);
204 start_row();
205 bank_accounts_list_cells(_("Account:"), 'bank_account', null, true);
206
207 bank_reconciliation_list_cells(_("Bank Statement:"), get_post('bank_account'),
208         'bank_date', null, true, _("New"));
209 end_row();
210 end_table();
211
212 $result = get_max_reconciled(get_post('reconcile_date'), $_POST['bank_account']);
213
214 if ($row = db_fetch($result)) {
215         $_POST["reconciled"] = price_format($row["end_balance"]-$row["beg_balance"]);
216         $total = $row["total"];
217         if (!isset($_POST["beg_balance"])) { // new selected account/statement
218                 $_POST["last_date"] = sql2date($row["last_date"]);
219                 $_POST["beg_balance"] = price_format($row["beg_balance"]);
220                 $_POST["end_balance"] = price_format($row["end_balance"]);
221                 if (get_post('bank_date')) {
222                         // if it is the last updated bank statement retrieve ending balance
223
224                         $row = get_ending_reconciled($_POST['bank_account'], $_POST['bank_date']);
225                         if($row) {
226                                 $_POST["end_balance"] = price_format($row["ending_reconcile_balance"]);
227                         }
228                 }
229         } 
230 }
231
232 echo "<hr>";
233
234 div_start('summary');
235
236 start_table(TABLESTYLE);
237 $th = array(_("Reconcile Date"), _("Beginning<br>Balance"), 
238         _("Ending<br>Balance"), _("Account<br>Total"),_("Reconciled<br>Amount"), _("Difference"));
239 table_header($th);
240 start_row();
241
242 date_cells("", "reconcile_date", _('Date of bank statement to reconcile'), 
243         get_post('bank_date')=='', 0, 0, 0, null, true);
244
245 amount_cells_ex("", "beg_balance", 15);
246
247 amount_cells_ex("", "end_balance", 15);
248
249 $reconciled = input_num('reconciled');
250 $difference = input_num("end_balance") - input_num("beg_balance") - $reconciled;
251
252 amount_cell($total);
253 amount_cell($reconciled, false, '', "reconciled");
254 amount_cell($difference, false, '', "difference");
255
256 end_row();
257 end_table();
258 div_end();
259 echo "<hr>";
260 //------------------------------------------------------------------------------------------------
261
262 if (!isset($_POST['bank_account']))
263     $_POST['bank_account'] = "";
264
265 $sql = get_sql_for_bank_account_reconcile(get_post('bank_account'), get_post('reconcile_date'));
266
267 $act = get_bank_account($_POST["bank_account"]);
268 display_heading($act['bank_account_name']." - ".$act['bank_curr_code']);
269
270         $cols =
271         array(
272                 _("Type") => array('fun'=>'systype_name', 'ord'=>''),
273                 _("#") => array('fun'=>'trans_view', 'ord'=>''),
274                 _("Reference"), 
275                 _("Date") => 'date',
276                 _("Debit") => array('align'=>'right', 'fun'=>'fmt_debit'), 
277                 _("Credit") => array('align'=>'right','insert'=>true, 'fun'=>'fmt_credit'), 
278                 _("Bank fees") => array('align'=>'right','insert'=>true, 'fun'=>'bank_fee'),
279             _("Person/Item") => array('fun'=>'fmt_person'), 
280                 _("Memo") => array('fun'=>'fmt_memo'),
281                 array('insert'=>true, 'fun'=>'gl_view'),
282                 "X"=>array('insert'=>true, 'fun'=>'rec_checkbox')
283            );
284         $table =& new_db_pager('trans_tbl', $sql, $cols);
285
286         $table->width = "80%";
287         display_db_pager($table);
288
289 br(1);
290 echo '<center>';
291 submit('Reconcile', _("Reconcile"), true, '', null);
292 submit('ReconcileAll', _("Reconcile All"), true, '');
293 echo '</center>';
294 end_form();
295
296 //------------------------------------------------------------------------------------------------
297
298 end_page();
299