94748d2f0631daf471765a1d3edd563aec46dfcc
[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 $path_to_root="..";
13 $page_security = 5;
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 ($use_date_picker)
27         $js .= get_js_date_picker();
28 page(_("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 = systypes::bank_transfer();
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         safeExit();
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 $table_style2;
57         $home_currency = get_company_currency();
58
59         start_form();
60
61         start_outer_table($table_style2, 5);
62
63         table_section(1);
64
65         bank_accounts_list_row(_("From Account:"), 'FromBankAccount', null, true);
66
67     bank_accounts_list_row(_("To Account:"), 'ToBankAccount', null, true);
68
69     date_row(_("Transfer Date:"), 'DatePaid', '', null, 0, 0, 0, null, true);
70
71         $from_currency = get_bank_account_currency($_POST['FromBankAccount']);
72         $to_currency = get_bank_account_currency($_POST['ToBankAccount']);
73         if ($from_currency != "" && $to_currency != "" && $from_currency != $to_currency) 
74         {
75                 amount_row(_("Amount:"), 'amount', null, null, $from_currency);
76                 amount_row(_("Bank Charge:"), 'charge', null, null, $from_currency);
77
78                 exchange_rate_display($from_currency, $to_currency, $_POST['DatePaid']);
79         } 
80         else 
81         {
82                 amount_row(_("Amount:"), 'amount');
83                 amount_row(_("Bank Charge:"), 'charge');
84         }
85
86         table_section(2);
87
88     ref_row(_("Reference:"), 'ref', '', references::get_next(systypes::bank_transfer()));
89
90     textarea_row(_("Memo:"), 'memo_', null, 40,4);
91
92         end_outer_table(1); // outer table
93
94     submit_center('AddPayment',_("Enter Transfer"), true, '', 'default');
95
96         end_form();
97 }
98
99 //----------------------------------------------------------------------------------------
100
101 function check_valid_entries()
102 {
103         if (!is_date($_POST['DatePaid'])) 
104         {
105                 display_error(_("The entered date is invalid."));
106                 set_focus('DatePaid');
107                 return false;
108         }
109         if (!is_date_in_fiscalyear($_POST['DatePaid']))
110         {
111                 display_error(_("The entered date is not in fiscal year."));
112                 set_focus('DatePaid');
113                 return false;
114         }
115
116         if (!check_num('amount', 0)) 
117         {
118                 display_error(_("The entered amount is invalid or less than zero."));
119                 set_focus('amount');
120                 return false;
121         }
122
123         if (isset($_POST['charge']) && !check_num('charge', 0)) 
124         {
125                 display_error(_("The entered amount is invalid or less than zero."));
126                 set_focus('charge');
127                 return false;
128         }
129         if (!references::is_valid($_POST['ref'])) 
130         {
131                 display_error(_("You must enter a reference."));
132                 set_focus('ref');
133                 return false;
134         }
135
136         if (!is_new_reference($_POST['ref'], systypes::bank_transfer())) 
137         {
138                 display_error(_("The entered reference is already in use."));
139                 set_focus('ref');
140                 return false;
141         }
142
143         if ($_POST['FromBankAccount'] == $_POST['ToBankAccount']) 
144         {
145                 display_error(_("The source and destination bank accouts cannot be the same."));
146                 set_focus('ToBankAccount');
147                 return false;
148         }
149
150     return true;
151 }
152
153 //----------------------------------------------------------------------------------------
154
155 function handle_add_deposit()
156 {
157         $trans_no = add_bank_transfer($_POST['FromBankAccount'], $_POST['ToBankAccount'],
158                 $_POST['DatePaid'], input_num('amount'), $_POST['ref'], $_POST['memo_'], input_num('charge'));
159
160         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
161 }
162
163 //----------------------------------------------------------------------------------------
164
165 function safeExit()
166 {
167         echo "<br><br>";
168         end_page();
169         exit;
170 }
171
172 //----------------------------------------------------------------------------------------
173
174 if (isset($_POST['AddPayment']))
175 {
176         if (check_valid_entries() == true) 
177         {
178                 handle_add_deposit();
179                 safeExit();
180         }
181 }
182
183 gl_payment_controls();
184
185 end_page();
186 ?>