Cleanup: removed all closing tags in php files.
[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 ($use_popup_windows)
25         $js .= get_js_open_window(800, 500);
26 if (user_use_date_picker())
27         $js .= get_js_date_picker();
28 page(_($help_context = "Transfer between Bank Accounts"), false, false, "", $js);
29
30 check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));
31
32 //----------------------------------------------------------------------------------------
33
34 if (isset($_GET['AddedID'])) 
35 {
36         $trans_no = $_GET['AddedID'];
37         $trans_type = ST_BANKTRANSFER;
38
39         display_notification_centered( _("Transfer has been entered"));
40
41         display_note(get_gl_view_str($trans_type, $trans_no, _("&View the GL Journal Entries for this Transfer")));
42
43         hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter &Another Transfer"));
44
45         display_footer_exit();
46 }
47
48 if (isset($_POST['_DatePaid_changed'])) {
49         $Ajax->activate('_ex_rate');
50 }
51
52 //----------------------------------------------------------------------------------------
53
54 function gl_payment_controls()
55 {
56         global $Refs;
57         
58         $home_currency = get_company_currency();
59
60         start_form();
61
62         start_outer_table(TABLESTYLE2);
63
64         table_section(1);
65
66         bank_accounts_list_row(_("From Account:"), 'FromBankAccount', null, true);
67
68         bank_balance_row($_POST['FromBankAccount']);
69
70     bank_accounts_list_row(_("To Account:"), 'ToBankAccount', null, true);
71
72         if (!isset($_POST['DatePaid'])) { // init page
73                 $_POST['DatePaid'] = new_doc_date();
74                 if (!is_date_in_fiscalyear($_POST['DatePaid']))
75                         $_POST['DatePaid'] = end_fiscalyear();
76         }
77     date_row(_("Transfer Date:"), 'DatePaid', '', true, 0, 0, 0, null, true);
78
79     ref_row(_("Reference:"), 'ref', '', $Refs->get_next(ST_BANKTRANSFER));
80
81         table_section(2);
82
83         $from_currency = get_bank_account_currency($_POST['FromBankAccount']);
84         $to_currency = get_bank_account_currency($_POST['ToBankAccount']);
85         if ($from_currency != "" && $to_currency != "" && $from_currency != $to_currency) 
86         {
87                 amount_row(_("Amount:"), 'amount', null, null, $from_currency);
88                 amount_row(_("Bank Charge:"), 'charge', null, null, $from_currency);
89
90                 amount_row(_("Incoming Amount:"), 'target_amount', null, '', $to_currency, 2);
91         } 
92         else 
93         {
94                 amount_row(_("Amount:"), 'amount');
95                 amount_row(_("Bank Charge:"), 'charge');
96         }
97
98     textarea_row(_("Memo:"), 'memo_', null, 40,4);
99
100         end_outer_table(1); // outer table
101
102     submit_center('AddPayment',_("Enter Transfer"), true, '', 'default');
103
104         end_form();
105 }
106
107 //----------------------------------------------------------------------------------------
108
109 function check_valid_entries()
110 {
111         global $Refs;
112         
113         if (!is_date($_POST['DatePaid'])) 
114         {
115                 display_error(_("The entered date is invalid."));
116                 set_focus('DatePaid');
117                 return false;
118         }
119         if (!is_date_in_fiscalyear($_POST['DatePaid']))
120         {
121                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
122                 set_focus('DatePaid');
123                 return false;
124         }
125
126         if (!check_num('amount', 0)) 
127         {
128                 display_error(_("The entered amount is invalid or less than zero."));
129                 set_focus('amount');
130                 return false;
131         }
132         if (input_num('amount') == 0) {
133                 display_error(_("The total bank amount cannot be 0."));
134                 set_focus('amount');
135                 return false;
136         }
137
138         $limit = get_bank_account_limit($_POST['FromBankAccount'], $_POST['DatePaid']);
139
140         $amnt_tr = input_num('charge') + input_num('amount');
141
142         if ($limit !== null && floatcmp($limit, $amnt_tr) < 0)
143         {
144                 display_error(sprintf(_("The total bank amount exceeds allowed limit (%s) for source account."), price_format($limit)));
145                 set_focus('amount');
146                 return false;
147         }
148         if ($trans = check_bank_account_history(-$amnt_tr, $_POST['FromBankAccount'], $_POST['DatePaid'])) {
149
150                 display_error(sprintf(_("The bank transaction would result in exceed of authorized overdraft limit for transaction: %s #%s on %s."),
151                         $systypes_array[$trans['type']], $trans['trans_no'], sql2date($trans['trans_date'])));
152                 set_focus('amount');
153                 $input_error = 1;
154         }
155
156         if (isset($_POST['charge']) && !check_num('charge', 0)) 
157         {
158                 display_error(_("The entered amount is invalid or less than zero."));
159                 set_focus('charge');
160                 return false;
161         }
162         if (isset($_POST['charge']) && input_num('charge') > 0 && get_bank_charge_account($_POST['FromBankAccount']) == '') {
163                 display_error(_("The Bank Charge Account has not been set in System and General GL Setup."));
164                 set_focus('charge');
165                 return false;
166         }
167         if (!$Refs->is_valid($_POST['ref'])) 
168         {
169                 display_error(_("You must enter a reference."));
170                 set_focus('ref');
171                 return false;
172         }
173
174         if (!is_new_reference($_POST['ref'], ST_BANKTRANSFER)) 
175         {
176                 display_error(_("The entered reference is already in use."));
177                 set_focus('ref');
178                 return false;
179         }
180
181         if ($_POST['FromBankAccount'] == $_POST['ToBankAccount']) 
182         {
183                 display_error(_("The source and destination bank accouts cannot be the same."));
184                 set_focus('ToBankAccount');
185                 return false;
186         }
187
188         if (isset($_POST['target_amount']) && !check_num('target_amount', 0)) 
189         {
190                 display_error(_("The entered amount is invalid or less than zero."));
191                 set_focus('target_amount');
192                 return false;
193         }
194         if (isset($_POST['target_amount']) && input_num('target_amount') == 0) {
195                 display_error(_("The incomming bank amount cannot be 0."));
196                 set_focus('target_amount');
197                 return false;
198         }
199
200         if (!db_has_currency_rates(get_bank_account_currency($_POST['FromBankAccount']), $_POST['DatePaid']))
201                 return false;
202
203         if (!db_has_currency_rates(get_bank_account_currency($_POST['ToBankAccount']), $_POST['DatePaid']))
204                 return false;
205
206     return true;
207 }
208
209 //----------------------------------------------------------------------------------------
210
211 function handle_add_deposit()
212 {
213         new_doc_date($_POST['DatePaid']);
214         $trans_no = add_bank_transfer($_POST['FromBankAccount'], $_POST['ToBankAccount'],
215                 $_POST['DatePaid'], input_num('amount'), $_POST['ref'], $_POST['memo_'], input_num('charge'), input_num('target_amount'));
216
217         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
218 }
219
220 //----------------------------------------------------------------------------------------
221
222 if (isset($_POST['AddPayment']))
223 {
224         if (check_valid_entries() == true) 
225         {
226                 handle_add_deposit();
227         }
228 }
229
230 gl_payment_controls();
231
232 end_page();