Incorrect Journal Balance (sales invoice) when many decimals in tax and price.
[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                         $_POST['dimension_id'] = $to_trans['dimension_id'];
90                         $_POST['dimension2_id'] = $to_trans['dimension2_id'];
91                 } else {
92                         $_POST['ref'] = $Refs->get_next(ST_BANKTRANSFER, null, get_post('DatePaid'));
93                         $_POST['memo_'] = '';
94                         $_POST['FromBankAccount'] = 0;
95                         $_POST['ToBankAccount'] = 0;
96                         $_POST['amount'] = 0;
97                         $_POST['dimension_id'] = 0;
98                         $_POST['dimension2_id'] = 0;
99                 }
100         }
101
102         start_form();
103
104         start_outer_table(TABLESTYLE2);
105
106         table_section(1);
107
108         bank_accounts_list_row(_("From Account:"), 'FromBankAccount', null, true);
109
110         bank_balance_row($_POST['FromBankAccount']);
111
112     bank_accounts_list_row(_("To Account:"), 'ToBankAccount', null, true);
113
114         if (!isset($_POST['DatePaid'])) { // init page
115                 $_POST['DatePaid'] = new_doc_date();
116                 if (!is_date_in_fiscalyear($_POST['DatePaid']))
117                         $_POST['DatePaid'] = end_fiscalyear();
118         }
119     date_row(_("Transfer Date:"), 'DatePaid', '', true, 0, 0, 0, null, true);
120
121     ref_row(_("Reference:"), 'ref', '', $Refs->get_next(ST_BANKTRANSFER, null, get_post('DatePaid')), false, ST_BANKTRANSFER,
122         array('date' => get_post('DatePaid')));
123         $dim = get_company_pref('use_dimension');
124         if ($dim > 0)
125                 dimensions_list_row(_("Dimension").":", 'dimension_id', 
126                         null, true, ' ', false, 1, false);
127         else
128                 hidden('dimension_id', 0);
129
130         table_section(2);
131
132         $from_currency = get_bank_account_currency($_POST['FromBankAccount']);
133         $to_currency = get_bank_account_currency($_POST['ToBankAccount']);
134         if ($from_currency != "" && $to_currency != "" && $from_currency != $to_currency) 
135         {
136                 amount_row(_("Amount:"), 'amount', null, null, $from_currency);
137                 amount_row(_("Bank Charge:"), 'charge', null, null, $from_currency);
138
139                 amount_row(_("Incoming Amount:"), 'target_amount', null, '', $to_currency, 2);
140         } 
141         else 
142         {
143                 amount_row(_("Amount:"), 'amount');
144                 amount_row(_("Bank Charge:"), 'charge');
145         }
146         if ($dim > 1)
147                 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', 
148                         null, true, ' ', false, 2, false);
149         else
150                 hidden('dimension2_id', 0);
151
152     textarea_row(_("Memo:"), 'memo_', null, 40,4);
153
154         end_outer_table(1); // outer table
155
156         if ($trans_no) {
157                 hidden('_trans_no', $trans_no);
158                 submit_center('submit', _("Modify Transfer"), true, '', 'default');
159         } else {
160                 submit_center('submit', _("Enter Transfer"), true, '', 'default');
161         }
162
163         end_form();
164 }
165
166 //----------------------------------------------------------------------------------------
167
168 function check_valid_entries($trans_no)
169 {
170         global $Refs, $systypes_array;
171         
172         if (!is_date($_POST['DatePaid'])) 
173         {
174                 display_error(_("The entered date is invalid."));
175                 set_focus('DatePaid');
176                 return false;
177         }
178         if (!is_date_in_fiscalyear($_POST['DatePaid']))
179         {
180                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
181                 set_focus('DatePaid');
182                 return false;
183         }
184
185         if (!check_num('amount', 0)) 
186         {
187                 display_error(_("The entered amount is invalid or less than zero."));
188                 set_focus('amount');
189                 return false;
190         }
191         if (input_num('amount') == 0) {
192                 display_error(_("The total bank amount cannot be 0."));
193                 set_focus('amount');
194                 return false;
195         }
196
197         $limit = get_bank_account_limit($_POST['FromBankAccount'], $_POST['DatePaid']);
198
199         $amnt_tr = input_num('charge') + input_num('amount');
200
201         $problemTransaction = null;
202         if ($trans_no) {
203                 $problemTransaction = check_bank_transfer( $trans_no, $_POST['FromBankAccount'], $_POST['ToBankAccount'], $_POST['DatePaid'],
204                         $amnt_tr, input_num('target_amount', $amnt_tr));
205
206         if ($problemTransaction != null ) {
207                 if (!array_key_exists('trans_no', $problemTransaction)) {
208                         display_error(sprintf(
209                                 _("This bank transfer change would result in exceeding authorized overdraft limit (%s) of the account '%s'"),
210                                 price_format(-$problemTransaction['amount']), $problemTransaction['bank_account_name']
211                         ));
212                 } else {
213                         display_error(sprintf(
214                                 _("This bank transfer change would result in exceeding authorized overdraft limit on '%s' for transaction: %s #%s on %s."),
215                                 $problemTransaction['bank_account_name'], $systypes_array[$problemTransaction['type']],
216                                 $problemTransaction['trans_no'], sql2date($problemTransaction['trans_date'])
217                         ));
218                 }
219                 set_focus('amount');
220                 return false;
221                 }
222         } else {
223                 if (null != ($problemTransaction = check_bank_account_history(-$amnt_tr, $_POST['FromBankAccount'], $_POST['DatePaid']))) {
224                         if (!array_key_exists('trans_no', $problemTransaction)) {
225                                 display_error(sprintf(
226                                         _("This bank transfer would result in exceeding authorized overdraft limit of the account (%s)"),
227                                         price_format(-$problemTransaction['amount'])
228                                 ));
229                         } else {
230                                 display_error(sprintf(
231                                         _("This bank transfer would result in exceeding authorized overdraft limit for transaction: %s #%s on %s."),
232                                         $systypes_array[$problemTransaction['type']], $problemTransaction['trans_no'], sql2date($problemTransaction['trans_date'])
233                                 ));
234                         }
235                         set_focus('amount');
236                         return false;
237                 }
238         }
239
240         if (isset($_POST['charge']) && !check_num('charge', 0)) 
241         {
242                 display_error(_("The entered amount is invalid or less than zero."));
243                 set_focus('charge');
244                 return false;
245         }
246         if (isset($_POST['charge']) && input_num('charge') > 0 && get_bank_charge_account($_POST['FromBankAccount']) == '') {
247                 display_error(_("The Bank Charge Account has not been set in System and General GL Setup."));
248                 set_focus('charge');
249                 return false;
250         }
251
252         if (!check_reference($_POST['ref'], ST_BANKTRANSFER, $trans_no)) {
253                 set_focus('ref');
254                 return false;
255         }
256
257         if ($_POST['FromBankAccount'] == $_POST['ToBankAccount']) 
258         {
259                 display_error(_("The source and destination bank accouts cannot be the same."));
260                 set_focus('ToBankAccount');
261                 return false;
262         }
263
264         if (isset($_POST['target_amount']) && !check_num('target_amount', 0)) 
265         {
266                 display_error(_("The entered amount is invalid or less than zero."));
267                 set_focus('target_amount');
268                 return false;
269         }
270         if (isset($_POST['target_amount']) && input_num('target_amount') == 0) {
271                 display_error(_("The incomming bank amount cannot be 0."));
272                 set_focus('target_amount');
273                 return false;
274         }
275
276         if (!db_has_currency_rates(get_bank_account_currency($_POST['FromBankAccount']), $_POST['DatePaid']))
277                 return false;
278
279         if (!db_has_currency_rates(get_bank_account_currency($_POST['ToBankAccount']), $_POST['DatePaid']))
280                 return false;
281
282     return true;
283 }
284
285 //----------------------------------------------------------------------------------------
286
287 function bank_transfer_handle_submit()
288 {
289         $trans_no = array_key_exists('_trans_no', $_POST) ?  $_POST['_trans_no'] : null;
290         if ($trans_no) {
291                 $trans_no = update_bank_transfer($trans_no, $_POST['FromBankAccount'], $_POST['ToBankAccount'], $_POST['DatePaid'],     input_num('amount'), 
292                         $_POST['ref'], $_POST['memo_'], $_POST['dimension_id'], $_POST['dimension2_id'], input_num('charge'), input_num('target_amount'));
293         } else {
294                 new_doc_date($_POST['DatePaid']);
295                 $trans_no = add_bank_transfer($_POST['FromBankAccount'], $_POST['ToBankAccount'], $_POST['DatePaid'], input_num('amount'), $_POST['ref'], 
296                         $_POST['memo_'], $_POST['dimension_id'], $_POST['dimension2_id'], input_num('charge'), input_num('target_amount'));
297         }
298
299         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
300 }
301
302 //----------------------------------------------------------------------------------------
303
304 $trans_no = '';
305 if (!$trans_no && isset($_POST['_trans_no'])) {
306         $trans_no = $_POST['_trans_no'];
307 }
308 if (!$trans_no && isset($_GET['trans_no'])) {
309         $trans_no = $_GET["trans_no"];
310 }
311
312 if (isset($_POST['submit'])) {
313     if (check_valid_entries($trans_no) == true) {
314         bank_transfer_handle_submit();
315         }
316 }
317
318 gl_payment_controls($trans_no);
319
320 end_page();