Fixed typo in last commit.
[fa-stable.git] / gl / manage / bank_accounts.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_BANKACCOUNT';
13 $path_to_root = "../..";
14 include($path_to_root . "/includes/session.inc");
15
16 page(_($help_context = "Bank Accounts"));
17
18 include($path_to_root . "/includes/ui.inc");
19
20 simple_page_mode();
21 //-----------------------------------------------------------------------------------
22
23 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
24 {
25
26         //initialise no input errors assumed initially before we test
27         $input_error = 0;
28
29         //first off validate inputs sensible
30         if (strlen($_POST['bank_account_name']) == 0) 
31         {
32                 $input_error = 1;
33                 display_error(_("The bank account name cannot be empty."));
34                 set_focus('bank_account_name');
35         } 
36         if ($Mode=='ADD_ITEM' && (gl_account_in_bank_accounts(get_post('account_code')) 
37                         || key_in_foreign_table(get_post('account_code'), 'gl_trans', 'account'))) {
38                 $input_error = 1;
39                 display_error(_("The GL account selected is already in use. Select another GL account."));
40                 set_focus('account_code');
41         }
42         if ($input_error != 1)
43         {
44         if ($selected_id != -1) 
45         {
46                 
47                 update_bank_account($selected_id, $_POST['account_code'],
48                                 $_POST['account_type'], $_POST['bank_account_name'], 
49                                 $_POST['bank_name'], $_POST['bank_account_number'], 
50                         $_POST['bank_address'], $_POST['BankAccountCurrency'],
51                         $_POST['dflt_curr_act']);
52                         display_notification(_('Bank account has been updated'));
53         } 
54         else 
55         {
56     
57                 add_bank_account($_POST['account_code'], $_POST['account_type'], 
58                                 $_POST['bank_account_name'], $_POST['bank_name'], 
59                         $_POST['bank_account_number'], $_POST['bank_address'], 
60                                 $_POST['BankAccountCurrency'], $_POST['dflt_curr_act']);
61                         display_notification(_('New bank account has been added'));
62         }
63                 $Mode = 'RESET';
64         }
65
66 elseif( $Mode == 'Delete')
67 {
68         //the link to delete a selected record was clicked instead of the submit button
69
70         $cancel_delete = 0;
71         // PREVENT DELETES IF DEPENDENT RECORDS IN 'bank_trans'
72
73         if (key_in_foreign_table($selected_id, 'bank_trans', 'bank_act') || key_in_foreign_table(get_post('account_code'), 'gl_trans', 'account'))
74         {
75                 $cancel_delete = 1;
76                 display_error(_("Cannot delete this bank account because transactions have been created using this account."));
77         }
78
79         if (key_in_foreign_table($selected_id, 'sales_pos', 'pos_account'))
80         {
81                 $cancel_delete = 1;
82                 display_error(_("Cannot delete this bank account because POS definitions have been created using this account."));
83         }
84         if (!$cancel_delete) 
85         {
86                 delete_bank_account($selected_id);
87                 display_notification(_('Selected bank account has been deleted'));
88         } //end if Delete bank account
89         $Mode = 'RESET';
90
91
92 if ($Mode == 'RESET')
93 {
94         $selected_id = -1;
95         $_POST['bank_name']  =  $_POST['bank_account_name']  = '';
96         $_POST['bank_account_number'] = $_POST['bank_address'] = '';
97 }
98
99 /* Always show the list of accounts */
100
101 $result = get_bank_accounts(check_value('show_inactive'));
102
103 start_form();
104 start_table(TABLESTYLE, "width='80%'");
105
106 $th = array(_("Account Name"), _("Type"), _("Currency"), _("GL Account"), 
107         _("Bank"), _("Number"), _("Bank Address"), _("Dflt"), '','');
108 inactive_control_column($th);
109 table_header($th);      
110
111 $k = 0; 
112 while ($myrow = db_fetch($result)) 
113 {
114         
115         alt_table_row_color($k);
116
117     label_cell($myrow["bank_account_name"], "nowrap");
118         label_cell($bank_account_types[$myrow["account_type"]], "nowrap");
119     label_cell($myrow["bank_curr_code"], "nowrap");
120     label_cell($myrow["account_code"] . " " . $myrow["account_name"], "nowrap");
121     label_cell($myrow["bank_name"], "nowrap");
122     label_cell($myrow["bank_account_number"], "nowrap");
123     label_cell($myrow["bank_address"]);
124     if ($myrow["dflt_curr_act"])
125                 label_cell(_("Yes"));
126         else
127                 label_cell(_("No"));
128
129         inactive_control_cell($myrow["id"], $myrow["inactive"], 'bank_accounts', 'id');
130         edit_button_cell("Edit".$myrow["id"], _("Edit"));
131         delete_button_cell("Delete".$myrow["id"], _("Delete"));
132     end_row(); 
133 }
134
135 inactive_control_row($th);
136 end_table(1);
137
138 $is_used = $selected_id != -1 && key_in_foreign_table($selected_id, 'bank_trans', 'bank_act');
139
140 start_table(TABLESTYLE2);
141
142 if ($selected_id != -1) 
143 {
144   if ($Mode == 'Edit') {        
145         $myrow = get_bank_account($selected_id);
146
147         $_POST['account_code'] = $myrow["account_code"];
148         $_POST['account_type'] = $myrow["account_type"];
149         $_POST['bank_name']  = $myrow["bank_name"];
150         $_POST['bank_account_name']  = $myrow["bank_account_name"];
151         $_POST['bank_account_number'] = $myrow["bank_account_number"];
152         $_POST['bank_address'] = $myrow["bank_address"];
153         $_POST['BankAccountCurrency'] = $myrow["bank_curr_code"];
154         $_POST['dflt_curr_act'] = $myrow["dflt_curr_act"];
155   }
156         hidden('selected_id', $selected_id);
157         set_focus('bank_account_name');
158
159
160 text_row(_("Bank Account Name:"), 'bank_account_name', null, 50, 100);
161
162 if ($is_used) 
163 {
164         label_row(_("Account Type:"), $bank_account_types[$_POST['account_type']]);
165         hidden('account_type');
166
167 else 
168 {
169         bank_account_types_list_row(_("Account Type:"), 'account_type', null); 
170 }
171 if ($is_used) 
172 {
173         label_row(_("Bank Account Currency:"), $_POST['BankAccountCurrency']);
174         hidden('BankAccountCurrency', $_POST['BankAccountCurrency']);
175
176 else 
177 {
178         currencies_list_row(_("Bank Account Currency:"), 'BankAccountCurrency', null);
179 }       
180
181 yesno_list_row(_("Default currency account:"), 'dflt_curr_act');
182
183 if($is_used)
184 {
185         label_row(_("Bank Account GL Code:"), $_POST['account_code']);
186         hidden('account_code');
187 } else 
188         gl_all_accounts_list_row(_("Bank Account GL Code:"), 'account_code', null);
189
190 text_row(_("Bank Name:"), 'bank_name', null, 50, 60);
191 text_row(_("Bank Account Number:"), 'bank_account_number', null, 30, 60);
192 textarea_row(_("Bank Address:"), 'bank_address', null, 40, 5);
193
194 end_table(1);
195
196 submit_add_or_update_center($selected_id == -1, '', 'both');
197
198 end_form();
199
200 end_page();
201 ?>