[0004212] Work Order Entry: fixed error when voided WO refence is reused.
[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         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
236         if (!check_reference($_POST['ref'], ST_BANKTRANSFER, $trans_no)) {
237                 set_focus('ref');
238                 return false;
239         }
240
241         if ($_POST['FromBankAccount'] == $_POST['ToBankAccount']) 
242         {
243                 display_error(_("The source and destination bank accouts cannot be the same."));
244                 set_focus('ToBankAccount');
245                 return false;
246         }
247
248         if (isset($_POST['target_amount']) && !check_num('target_amount', 0)) 
249         {
250                 display_error(_("The entered amount is invalid or less than zero."));
251                 set_focus('target_amount');
252                 return false;
253         }
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');
257                 return false;
258         }
259
260         if (!db_has_currency_rates(get_bank_account_currency($_POST['FromBankAccount']), $_POST['DatePaid']))
261                 return false;
262
263         if (!db_has_currency_rates(get_bank_account_currency($_POST['ToBankAccount']), $_POST['DatePaid']))
264                 return false;
265
266     return true;
267 }
268
269 //----------------------------------------------------------------------------------------
270
271 function bank_transfer_handle_submit()
272 {
273         $trans_no = array_key_exists('_trans_no', $_POST) ?  $_POST['_trans_no'] : null;
274         if ($trans_no) {
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'));
276         } else {
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'));
279         }
280
281         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
282 }
283
284 //----------------------------------------------------------------------------------------
285
286 $trans_no = '';
287 if (!$trans_no && isset($_POST['_trans_no'])) {
288         $trans_no = $_POST['_trans_no'];
289 }
290 if (!$trans_no && isset($_GET['trans_no'])) {
291         $trans_no = $_GET["trans_no"];
292 }
293
294 if (isset($_POST['submit'])) {
295     if (check_valid_entries($trans_no) == true) {
296         bank_transfer_handle_submit();
297         }
298 }
299
300 gl_payment_controls($trans_no);
301
302 end_page();