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';
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/data_checks.inc");
20 include_once($path_to_root . "/gl/includes/gl_db.inc");
21 include_once($path_to_root . "/gl/includes/gl_ui.inc");
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();
29 if (isset($_GET['ModifyTransfer'])) {
30 $_SESSION['page_title'] = _($help_context = "Modify Bank Account Transfer");
32 $_SESSION['page_title'] = _($help_context = "Bank Account Transfer Entry");
35 page($_SESSION['page_title'], false, false, "", $js);
37 check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));
39 //----------------------------------------------------------------------------------------
41 if (isset($_GET['AddedID']))
43 $trans_no = $_GET['AddedID'];
44 $trans_type = ST_BANKTRANSFER;
46 display_notification_centered( _("Transfer has been entered"));
48 display_note(get_gl_view_str($trans_type, $trans_no, _("&View the GL Journal Entries for this Transfer")));
50 hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter &Another Transfer"));
52 display_footer_exit();
55 if (isset($_POST['_DatePaid_changed'])) {
56 $Ajax->activate('_ex_rate');
59 //----------------------------------------------------------------------------------------
61 function gl_payment_controls($trans_no)
67 $result = get_bank_trans(ST_BANKTRANSFER, $trans_no);
69 if (db_num_rows($result) != 2)
70 display_db_error("Bank transfer does not contain two records");
72 $trans1 = db_fetch($result);
73 $trans2 = db_fetch($result);
75 if ($trans1["amount"] < 0) {
76 $from_trans = $trans1; // from trans is the negative one
79 $from_trans = $trans2;
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']);
90 $_POST['ref'] = $Refs->get_next(ST_BANKTRANSFER, null, get_post('DatePaid'));
92 $_POST['FromBankAccount'] = 0;
93 $_POST['ToBankAccount'] = 0;
100 start_outer_table(TABLESTYLE2);
104 bank_accounts_list_row(_("From Account:"), 'FromBankAccount', null, true);
106 bank_balance_row($_POST['FromBankAccount']);
108 bank_accounts_list_row(_("To Account:"), 'ToBankAccount', null, true);
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();
115 date_row(_("Transfer Date:"), 'DatePaid', '', true, 0, 0, 0, null, true);
117 ref_row(_("Reference:"), 'ref', '', $Refs->get_next(ST_BANKTRANSFER, null, get_post('DatePaid')), false, ST_BANKTRANSFER,
118 array('date' => get_post('DatePaid')));
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)
126 amount_row(_("Amount:"), 'amount', null, null, $from_currency);
127 amount_row(_("Bank Charge:"), 'charge', null, null, $from_currency);
129 amount_row(_("Incoming Amount:"), 'target_amount', null, '', $to_currency, 2);
133 amount_row(_("Amount:"), 'amount');
134 amount_row(_("Bank Charge:"), 'charge');
137 textarea_row(_("Memo:"), 'memo_', null, 40,4);
139 end_outer_table(1); // outer table
142 hidden('_trans_no', $trans_no);
143 submit_center('submit', _("Modify Transfer"), true, '', 'default');
145 submit_center('submit', _("Enter Transfer"), true, '', 'default');
151 //----------------------------------------------------------------------------------------
153 function check_valid_entries($trans_no)
155 global $Refs, $systypes_array;
157 if (!is_date($_POST['DatePaid']))
159 display_error(_("The entered date is invalid."));
160 set_focus('DatePaid');
163 if (!is_date_in_fiscalyear($_POST['DatePaid']))
165 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
166 set_focus('DatePaid');
170 if (!check_num('amount', 0))
172 display_error(_("The entered amount is invalid or less than zero."));
176 if (input_num('amount') == 0) {
177 display_error(_("The total bank amount cannot be 0."));
182 $limit = get_bank_account_limit($_POST['FromBankAccount'], $_POST['DatePaid']);
184 $amnt_tr = input_num('charge') + input_num('amount');
187 $problemTransaction = check_bank_transfer( $trans_no, $_POST['FromBankAccount'], $_POST['ToBankAccount'], $_POST['DatePaid'],
188 $amnt_tr, input_num('target_amount', $amnt_tr));
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']
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'])
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'])
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'])
224 if (isset($_POST['charge']) && !check_num('charge', 0))
226 display_error(_("The entered amount is invalid or less than zero."));
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."));
236 if (!check_reference($_POST['ref'], ST_BANKTRANSFER, $trans_no)) {
241 if ($_POST['FromBankAccount'] == $_POST['ToBankAccount'])
243 display_error(_("The source and destination bank accouts cannot be the same."));
244 set_focus('ToBankAccount');
248 if (isset($_POST['target_amount']) && !check_num('target_amount', 0))
250 display_error(_("The entered amount is invalid or less than zero."));
251 set_focus('target_amount');
254 if (isset($_POST['target_amount']) && input_num('target_amount') == 0) {
255 display_error(_("The incomming bank amount cannot be 0."));
256 set_focus('target_amount');
260 if (!db_has_currency_rates(get_bank_account_currency($_POST['FromBankAccount']), $_POST['DatePaid']))
263 if (!db_has_currency_rates(get_bank_account_currency($_POST['ToBankAccount']), $_POST['DatePaid']))
269 //----------------------------------------------------------------------------------------
271 function bank_transfer_handle_submit()
273 $trans_no = array_key_exists('_trans_no', $_POST) ? $_POST['_trans_no'] : null;
275 $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 new_doc_date($_POST['DatePaid']);
278 $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'));
281 meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
284 //----------------------------------------------------------------------------------------
287 if (!$trans_no && isset($_POST['_trans_no'])) {
288 $trans_no = $_POST['_trans_no'];
290 if (!$trans_no && isset($_GET['trans_no'])) {
291 $trans_no = $_GET["trans_no"];
294 if (isset($_POST['submit'])) {
295 if (check_valid_entries($trans_no) == true) {
296 bank_transfer_handle_submit();
300 gl_payment_controls($trans_no);