Moved all SQL statements from PHP files into relevant *_db.inc files.
[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         
37         if ($input_error != 1)
38         {
39         if ($selected_id != -1) 
40         {
41                 
42                 update_bank_account($selected_id, $_POST['account_code'],
43                                 $_POST['account_type'], $_POST['bank_account_name'], 
44                                 $_POST['bank_name'], $_POST['bank_account_number'], 
45                         $_POST['bank_address'], $_POST['BankAccountCurrency'],
46                         $_POST['dflt_curr_act']);
47                         display_notification(_('Bank account has been updated'));
48         } 
49         else 
50         {
51     
52                 add_bank_account($_POST['account_code'], $_POST['account_type'], 
53                                 $_POST['bank_account_name'], $_POST['bank_name'], 
54                         $_POST['bank_account_number'], $_POST['bank_address'], 
55                                 $_POST['BankAccountCurrency'], $_POST['dflt_curr_act']);
56                         display_notification(_('New bank account has been added'));
57         }
58                 $Mode = 'RESET';
59         }
60
61 elseif( $Mode == 'Delete')
62 {
63         //the link to delete a selected record was clicked instead of the submit button
64
65         $cancel_delete = 0;
66         $acc = db_escape($selected_id);
67         // PREVENT DELETES IF DEPENDENT RECORDS IN 'bank_trans'
68
69         if (key_in_foreign_table($acc, 'bank_trans', 'bank_act', true))
70         {
71                 $cancel_delete = 1;
72                 display_error(_("Cannot delete this bank account because transactions have been created using this account."));
73         }
74
75         if (key_in_foreign_table($acc, 'sales_pos', 'pos_account', true))
76         {
77                 $cancel_delete = 1;
78                 display_error(_("Cannot delete this bank account because POS definitions have been created using this account."));
79         }
80         if (!$cancel_delete) 
81         {
82                 delete_bank_account($selected_id);
83                 display_notification(_('Selected bank account has been deleted'));
84         } //end if Delete bank account
85         $Mode = 'RESET';
86
87
88 if ($Mode == 'RESET')
89 {
90         $selected_id = -1;
91         $_POST['bank_name']  =  $_POST['bank_account_name']  = '';
92         $_POST['bank_account_number'] = $_POST['bank_address'] = '';
93 }
94
95 /* Always show the list of accounts */
96
97 $result = get_bank_accounts(check_value('show_inactive'));
98
99 start_form();
100 start_table("$table_style width='80%'");
101
102 $th = array(_("Account Name"), _("Type"), _("Currency"), _("GL Account"), 
103         _("Bank"), _("Number"), _("Bank Address"), _("Dflt"), '','');
104 inactive_control_column($th);
105 table_header($th);      
106
107 $k = 0; 
108 while ($myrow = db_fetch($result)) 
109 {
110         
111         alt_table_row_color($k);
112
113     label_cell($myrow["bank_account_name"], "nowrap");
114         label_cell($bank_account_types[$myrow["account_type"]], "nowrap");
115     label_cell($myrow["bank_curr_code"], "nowrap");
116     label_cell($myrow["account_code"] . " " . $myrow["account_name"], "nowrap");
117     label_cell($myrow["bank_name"], "nowrap");
118     label_cell($myrow["bank_account_number"], "nowrap");
119     label_cell($myrow["bank_address"]);
120     if ($myrow["dflt_curr_act"])
121                 label_cell(_("Yes"));
122         else
123                 label_cell(_("No"));
124
125         inactive_control_cell($myrow["id"], $myrow["inactive"], 'bank_accounts', 'id');
126         edit_button_cell("Edit".$myrow["id"], _("Edit"));
127         delete_button_cell("Delete".$myrow["id"], _("Delete"));
128     end_row(); 
129 }
130
131 inactive_control_row($th);
132 end_table(1);
133
134 $is_editing = $selected_id != -1; 
135
136 start_table($table_style2);
137
138 if ($is_editing) 
139 {
140   if ($Mode == 'Edit') {        
141         $myrow = get_bank_account($selected_id);
142
143         $_POST['account_code'] = $myrow["account_code"];
144         $_POST['account_type'] = $myrow["account_type"];
145         $_POST['bank_name']  = $myrow["bank_name"];
146         $_POST['bank_account_name']  = $myrow["bank_account_name"];
147         $_POST['bank_account_number'] = $myrow["bank_account_number"];
148         $_POST['bank_address'] = $myrow["bank_address"];
149         $_POST['BankAccountCurrency'] = $myrow["bank_curr_code"];
150         $_POST['dflt_curr_act'] = $myrow["dflt_curr_act"];
151   }
152         hidden('selected_id', $selected_id);
153         hidden('account_code');
154         hidden('account_type');
155         hidden('BankAccountCurrency', $_POST['BankAccountCurrency']);   
156         set_focus('bank_account_name');
157
158
159 text_row(_("Bank Account Name:"), 'bank_account_name', null, 50, 100);
160
161 if ($is_editing) 
162 {
163         label_row(_("Account Type:"), $bank_account_types[$_POST['account_type']]);
164
165 else 
166 {
167         bank_account_types_list_row(_("Account Type:"), 'account_type', null); 
168 }
169 if ($is_editing) 
170 {
171         label_row(_("Bank Account Currency:"), $_POST['BankAccountCurrency']);
172
173 else 
174 {
175         currencies_list_row(_("Bank Account Currency:"), 'BankAccountCurrency', null);
176 }       
177
178 yesno_list_row(_("Default currency account:"), 'dflt_curr_act');
179
180 if($is_editing)
181         label_row(_("Bank Account GL Code:"), $_POST['account_code']);
182 else 
183         gl_all_accounts_list_row(_("Bank Account GL Code:"), 'account_code', null);
184
185 text_row(_("Bank Name:"), 'bank_name', null, 50, 60);
186 text_row(_("Bank Account Number:"), 'bank_account_number', null, 30, 60);
187 textarea_row(_("Bank Address:"), 'bank_address', null, 40, 5);
188
189 end_table(1);
190
191 submit_add_or_update_center($selected_id == -1, '', 'both');
192
193 end_form();
194
195 end_page();
196 ?>