7b987636a16162d005e06a133bba6b247720013e
[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);
91                         $_POST['memo_'] = '';
92                         $_POST['FromBankAccount'] = 0;
93                         $_POST['ToBankAccount'] = 0;
94                         $_POST['amount'] = 0;
95                 }
96         }
97         $home_currency = get_company_currency();
98
99         start_form();
100
101         start_outer_table(TABLESTYLE2);
102
103         table_section(1);
104
105         bank_accounts_list_row(_("From Account:"), 'FromBankAccount', null, true);
106
107         bank_balance_row($_POST['FromBankAccount']);
108
109     bank_accounts_list_row(_("To Account:"), 'ToBankAccount', null, true);
110
111         if (!isset($_POST['DatePaid'])) { // init page
112                 $_POST['DatePaid'] = new_doc_date();
113                 if (!is_date_in_fiscalyear($_POST['DatePaid']))
114                         $_POST['DatePaid'] = end_fiscalyear();
115         }
116     date_row(_("Transfer Date:"), 'DatePaid', '', true, 0, 0, 0, null, true);
117
118         ref_row(_("Reference:"), 'ref', '', $_POST['ref']);
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         if ($trans_no) {
187                 $problemTransaction = check_bank_transfer( $trans_no, $_POST['FromBankAccount'], $_POST['ToBankAccount'], $_POST['DatePaid'],
188                         $amnt_tr, input_num('target_amount', $amnt_tr));
189
190         if ($problemTransaction != null ) {
191                 if (!array_key_exists('trans_no', $problemTransaction)) {
192                         display_error(sprintf(
193                                 _("This bank transfer change would result in exceeding authorized overdraft limit (%s) of the account '%s'"),
194                                 price_format(-$problemTransaction['amount']), $problemTransaction['bank_account_name']
195                         ));
196                 } else {
197                         display_error(sprintf(
198                                 _("This bank transfer change would result in exceeding authorized overdraft limit on '%s' for transaction: %s #%s on %s."),
199                                 $problemTransaction['bank_account_name'], $systypes_array[$problemTransaction['type']],
200                                 $problemTransaction['trans_no'], sql2date($problemTransaction['trans_date'])
201                         ));
202                 }
203                 set_focus('amount');
204                 return false;
205                 }
206         } else {
207                 if (null != ($problemTransaction = check_bank_account_history(-$amnt_tr, $_POST['FromBankAccount'], $_POST['DatePaid']))) {
208                         if (!array_key_exists('trans_no', $problemTransaction)) {
209                                 display_error(sprintf(
210                                         _("This bank transfer would result in exceeding authorized overdraft limit of the account (%s)"),
211                                         price_format(-$problemTransaction['amount'])
212                                 ));
213                         } else {
214                                 display_error(sprintf(
215                                         _("This bank transfer would result in exceeding authorized overdraft limit for transaction: %s #%s on %s."),
216                                         $systypes_array[$problemTransaction['type']], $problemTransaction['trans_no'], sql2date($problemTransaction['trans_date'])
217                                 ));
218                         }
219                         set_focus('amount');
220                         return false;
221                 }
222         }
223
224         if (isset($_POST['charge']) && !check_num('charge', 0)) 
225         {
226                 display_error(_("The entered amount is invalid or less than zero."));
227                 set_focus('charge');
228                 return false;
229         }
230         if (isset($_POST['charge']) && input_num('charge') > 0 && get_bank_charge_account($_POST['FromBankAccount']) == '') {
231                 display_error(_("The Bank Charge Account has not been set in System and General GL Setup."));
232                 set_focus('charge');
233                 return false;
234         }
235         if (!$Refs->is_valid($_POST['ref'])) 
236         {
237                 display_error(_("You must enter a reference."));
238                 set_focus('ref');
239                 return false;
240         }
241
242         if (! $trans_no && ! is_new_reference($_POST['ref'], ST_BANKTRANSFER)) {
243                 display_error(_("The entered reference is already in use."));
244                 set_focus('ref');
245                 return false;
246         }
247
248         if ($_POST['FromBankAccount'] == $_POST['ToBankAccount']) 
249         {
250                 display_error(_("The source and destination bank accouts cannot be the same."));
251                 set_focus('ToBankAccount');
252                 return false;
253         }
254
255         if (isset($_POST['target_amount']) && !check_num('target_amount', 0)) 
256         {
257                 display_error(_("The entered amount is invalid or less than zero."));
258                 set_focus('target_amount');
259                 return false;
260         }
261         if (isset($_POST['target_amount']) && input_num('target_amount') == 0) {
262                 display_error(_("The incomming bank amount cannot be 0."));
263                 set_focus('target_amount');
264                 return false;
265         }
266
267         if (!db_has_currency_rates(get_bank_account_currency($_POST['FromBankAccount']), $_POST['DatePaid']))
268                 return false;
269
270         if (!db_has_currency_rates(get_bank_account_currency($_POST['ToBankAccount']), $_POST['DatePaid']))
271                 return false;
272
273     return true;
274 }
275
276 //----------------------------------------------------------------------------------------
277
278 function bank_transfer_handle_submit()
279 {
280         $trans_no = array_key_exists('_trans_no', $_POST) ?  $_POST['_trans_no'] : null;
281         if ($trans_no) {
282                 $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'));
283         } else {
284                 new_doc_date($_POST['DatePaid']);
285                 $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'));
286         }
287
288         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
289 }
290
291 //----------------------------------------------------------------------------------------
292
293 $trans_no = '';
294 if (!$trans_no && isset($_POST['_trans_no'])) {
295         $trans_no = $_POST['_trans_no'];
296 }
297 if (!$trans_no && isset($_GET['trans_no'])) {
298         $trans_no = $_GET["trans_no"];
299 }
300
301 if (isset($_POST['submit'])) {
302     if (check_valid_entries($trans_no) == true) {
303         bank_transfer_handle_submit();
304         }
305 }
306
307 gl_payment_controls($trans_no);
308
309 end_page();