Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[fa-stable.git] / gl / bank_transfer.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_BANKTRANSFER';
13 $path_to_root = "..";
14
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/data_checks.inc");
19
20 include_once($path_to_root . "/gl/includes/gl_db.inc");
21 include_once($path_to_root . "/gl/includes/gl_ui.inc");
22
23 $js = "";
24 if ($SysPrefs->use_popup_windows)
25         $js .= get_js_open_window(800, 500);
26 if (user_use_date_picker())
27         $js .= get_js_date_picker();
28
29 if (isset($_GET['ModifyTransfer'])) {
30         $_SESSION['page_title'] = _($help_context = "Modify Bank Account Transfer");
31 } else {
32         $_SESSION['page_title'] = _($help_context = "Bank Account Transfer Entry");
33 }
34
35 page($_SESSION['page_title'], false, false, "", $js);
36
37 check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));
38
39 //----------------------------------------------------------------------------------------
40
41 if (isset($_GET['AddedID'])) 
42 {
43         $trans_no = $_GET['AddedID'];
44         $trans_type = ST_BANKTRANSFER;
45
46         display_notification_centered( _("Transfer has been entered"));
47
48         display_note(get_gl_view_str($trans_type, $trans_no, _("&View the GL Journal Entries for this Transfer")));
49
50         hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter &Another Transfer"));
51
52         display_footer_exit();
53 }
54
55 if (isset($_POST['_DatePaid_changed'])) {
56         $Ajax->activate('_ex_rate');
57 }
58
59 //----------------------------------------------------------------------------------------
60
61 function gl_payment_controls($trans_no)
62 {
63         global $Refs;
64         
65         if (!in_ajax()) {
66                 if ($trans_no) {
67                         $result = get_bank_trans(ST_BANKTRANSFER, $trans_no);
68
69                         if (db_num_rows($result) != 2)
70                                 display_db_error("Bank transfer does not contain two records");
71
72                         $trans1 = db_fetch($result);
73                         $trans2 = db_fetch($result);
74
75                         if ($trans1["amount"] < 0) {
76                                 $from_trans = $trans1; // from trans is the negative one
77                                 $to_trans = $trans2;
78                         } else {
79                         $from_trans = $trans2;
80                                 $to_trans = $trans1;
81                         }
82                         $_POST['DatePaid'] = sql2date($to_trans['trans_date']);
83                         $_POST['ref'] = $to_trans['ref'];
84                         $_POST['memo_'] = get_comments_string($to_trans['type'], $trans_no);
85                         $_POST['FromBankAccount'] = $from_trans['bank_act'];
86                         $_POST['ToBankAccount'] = $to_trans['bank_act'];
87                         $_POST['target_amount'] = price_format($to_trans['amount']);
88                         $_POST['amount'] = price_format(-$from_trans['amount']);
89                 } else {
90                         $_POST['ref'] = $Refs->get_next(ST_BANKTRANSFER, null, get_post('DatePaid'));
91                         $_POST['memo_'] = '';
92                         $_POST['FromBankAccount'] = 0;
93                         $_POST['ToBankAccount'] = 0;
94                         $_POST['amount'] = 0;
95                 }
96         }
97
98         start_form();
99
100         start_outer_table(TABLESTYLE2);
101
102         table_section(1);
103
104         bank_accounts_list_row(_("From Account:"), 'FromBankAccount', null, true);
105
106         bank_balance_row($_POST['FromBankAccount']);
107
108     bank_accounts_list_row(_("To Account:"), 'ToBankAccount', null, true);
109
110         if (!isset($_POST['DatePaid'])) { // init page
111                 $_POST['DatePaid'] = new_doc_date();
112                 if (!is_date_in_fiscalyear($_POST['DatePaid']))
113                         $_POST['DatePaid'] = end_fiscalyear();
114         }
115     date_row(_("Transfer Date:"), 'DatePaid', '', true, 0, 0, 0, null, true);
116
117     ref_row(_("Reference:"), 'ref', '', $Refs->get_next(ST_BANKTRANSFER, null, get_post('DatePaid')), false, ST_BANKTRANSFER,
118         array('date' => get_post('DatePaid')));
119
120         table_section(2);
121
122         $from_currency = get_bank_account_currency($_POST['FromBankAccount']);
123         $to_currency = get_bank_account_currency($_POST['ToBankAccount']);
124         if ($from_currency != "" && $to_currency != "" && $from_currency != $to_currency) 
125         {
126                 amount_row(_("Amount:"), 'amount', null, null, $from_currency);
127                 amount_row(_("Bank Charge:"), 'charge', null, null, $from_currency);
128
129                 amount_row(_("Incoming Amount:"), 'target_amount', null, '', $to_currency, 2);
130         } 
131         else 
132         {
133                 amount_row(_("Amount:"), 'amount');
134                 amount_row(_("Bank Charge:"), 'charge');
135         }
136
137     textarea_row(_("Memo:"), 'memo_', null, 40,4);
138
139         end_outer_table(1); // outer table
140
141         if ($trans_no) {
142                 hidden('_trans_no', $trans_no);
143                 submit_center('submit', _("Modify Transfer"), true, '', 'default');
144         } else {
145                 submit_center('submit', _("Enter Transfer"), true, '', 'default');
146         }
147
148         end_form();
149 }
150
151 //----------------------------------------------------------------------------------------
152
153 function check_valid_entries($trans_no)
154 {
155         global $Refs, $systypes_array;
156         
157         if (!is_date($_POST['DatePaid'])) 
158         {
159                 display_error(_("The entered date is invalid."));
160                 set_focus('DatePaid');
161                 return false;
162         }
163         if (!is_date_in_fiscalyear($_POST['DatePaid']))
164         {
165                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
166                 set_focus('DatePaid');
167                 return false;
168         }
169
170         if (!check_num('amount', 0)) 
171         {
172                 display_error(_("The entered amount is invalid or less than zero."));
173                 set_focus('amount');
174                 return false;
175         }
176         if (input_num('amount') == 0) {
177                 display_error(_("The total bank amount cannot be 0."));
178                 set_focus('amount');
179                 return false;
180         }
181
182         $limit = get_bank_account_limit($_POST['FromBankAccount'], $_POST['DatePaid']);
183
184         $amnt_tr = input_num('charge') + input_num('amount');
185
186         $problemTransaction = null;
187         if ($trans_no) {
188                 $problemTransaction = check_bank_transfer( $trans_no, $_POST['FromBankAccount'], $_POST['ToBankAccount'], $_POST['DatePaid'],
189                         $amnt_tr, input_num('target_amount', $amnt_tr));
190
191         if ($problemTransaction != null ) {
192                 if (!array_key_exists('trans_no', $problemTransaction)) {
193                         display_error(sprintf(
194                                 _("This bank transfer change would result in exceeding authorized overdraft limit (%s) of the account '%s'"),
195                                 price_format(-$problemTransaction['amount']), $problemTransaction['bank_account_name']
196                         ));
197                 } else {
198                         display_error(sprintf(
199                                 _("This bank transfer change would result in exceeding authorized overdraft limit on '%s' for transaction: %s #%s on %s."),
200                                 $problemTransaction['bank_account_name'], $systypes_array[$problemTransaction['type']],
201                                 $problemTransaction['trans_no'], sql2date($problemTransaction['trans_date'])
202                         ));
203                 }
204                 set_focus('amount');
205                 return false;
206                 }
207         } else {
208                 if (null != ($problemTransaction = check_bank_account_history(-$amnt_tr, $_POST['FromBankAccount'], $_POST['DatePaid']))) {
209                         if (!array_key_exists('trans_no', $problemTransaction)) {
210                                 display_error(sprintf(
211                                         _("This bank transfer would result in exceeding authorized overdraft limit of the account (%s)"),
212                                         price_format(-$problemTransaction['amount'])
213                                 ));
214                         } else {
215                                 display_error(sprintf(
216                                         _("This bank transfer would result in exceeding authorized overdraft limit for transaction: %s #%s on %s."),
217                                         $systypes_array[$problemTransaction['type']], $problemTransaction['trans_no'], sql2date($problemTransaction['trans_date'])
218                                 ));
219                         }
220                         set_focus('amount');
221                         return false;
222                 }
223         }
224
225         if (isset($_POST['charge']) && !check_num('charge', 0)) 
226         {
227                 display_error(_("The entered amount is invalid or less than zero."));
228                 set_focus('charge');
229                 return false;
230         }
231         if (isset($_POST['charge']) && input_num('charge') > 0 && get_bank_charge_account($_POST['FromBankAccount']) == '') {
232                 display_error(_("The Bank Charge Account has not been set in System and General GL Setup."));
233                 set_focus('charge');
234                 return false;
235         }
236
237         if (!check_reference($_POST['ref'], ST_BANKTRANSFER, $trans_no)) {
238                 set_focus('ref');
239                 return false;
240         }
241
242         if ($_POST['FromBankAccount'] == $_POST['ToBankAccount']) 
243         {
244                 display_error(_("The source and destination bank accouts cannot be the same."));
245                 set_focus('ToBankAccount');
246                 return false;
247         }
248
249         if (isset($_POST['target_amount']) && !check_num('target_amount', 0)) 
250         {
251                 display_error(_("The entered amount is invalid or less than zero."));
252                 set_focus('target_amount');
253                 return false;
254         }
255         if (isset($_POST['target_amount']) && input_num('target_amount') == 0) {
256                 display_error(_("The incomming bank amount cannot be 0."));
257                 set_focus('target_amount');
258                 return false;
259         }
260
261         if (!db_has_currency_rates(get_bank_account_currency($_POST['FromBankAccount']), $_POST['DatePaid']))
262                 return false;
263
264         if (!db_has_currency_rates(get_bank_account_currency($_POST['ToBankAccount']), $_POST['DatePaid']))
265                 return false;
266
267     return true;
268 }
269
270 //----------------------------------------------------------------------------------------
271
272 function bank_transfer_handle_submit()
273 {
274         $trans_no = array_key_exists('_trans_no', $_POST) ?  $_POST['_trans_no'] : null;
275         if ($trans_no) {
276                 $trans_no = update_bank_transfer($trans_no, $_POST['FromBankAccount'], $_POST['ToBankAccount'], $_POST['DatePaid'], input_num('amount'), $_POST['ref'], $_POST['memo_'], input_num('charge'), input_num('target_amount'));
277         } else {
278                 new_doc_date($_POST['DatePaid']);
279                 $trans_no = add_bank_transfer($_POST['FromBankAccount'], $_POST['ToBankAccount'], $_POST['DatePaid'], input_num('amount'), $_POST['ref'], $_POST['memo_'], input_num('charge'), input_num('target_amount'));
280         }
281
282         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
283 }
284
285 //----------------------------------------------------------------------------------------
286
287 $trans_no = '';
288 if (!$trans_no && isset($_POST['_trans_no'])) {
289         $trans_no = $_POST['_trans_no'];
290 }
291 if (!$trans_no && isset($_GET['trans_no'])) {
292         $trans_no = $_GET["trans_no"];
293 }
294
295 if (isset($_POST['submit'])) {
296     if (check_valid_entries($trans_no) == true) {
297         bank_transfer_handle_submit();
298         }
299 }
300
301 gl_payment_controls($trans_no);
302
303 end_page();