09f07b04b91121b9d1f1d9409ceca0f77c01316c
[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         $curr = get_company_currency();
29         $sql = "SELECT account_code     FROM ".TB_PREF."bank_accounts
30                 WHERE account_code = '".$_POST['account_code']."'";
31         if ($curr == $_POST['BankAccountCurrency'])
32                 $sql .= " AND bank_curr_code <> '".$_POST['BankAccountCurrency']."'";
33         $sql .= " AND id <> $selected_id";
34         $result = db_query($sql,"could not get bank accounts");
35         if (db_num_rows($result) > 0)
36         {
37                 $input_error = 1;
38                 display_error(_("The GL account is already in use."));
39                 set_focus('account_code');
40         }
41         //first off validate inputs sensible
42         if ($input_error == 0 && strlen($_POST['bank_account_name']) == 0) 
43         {
44                 $input_error = 1;
45                 display_error(_("The bank account name cannot be empty."));
46                 set_focus('bank_account_name');
47         } 
48         
49         if ($input_error != 1)
50         {
51         if ($selected_id != -1) 
52         {
53                 
54                 update_bank_account($selected_id, $_POST['account_code'],
55                                 $_POST['account_type'], $_POST['bank_account_name'], 
56                                 $_POST['bank_name'], $_POST['bank_account_number'], 
57                         $_POST['bank_address'], $_POST['BankAccountCurrency'],
58                         $_POST['dflt_curr_act']);
59                         display_notification(_('Bank account has been updated'));
60         } 
61         else 
62         {
63     
64                 add_bank_account($_POST['account_code'], $_POST['account_type'], 
65                                 $_POST['bank_account_name'], $_POST['bank_name'], 
66                         $_POST['bank_account_number'], $_POST['bank_address'], 
67                                 $_POST['BankAccountCurrency'], $_POST['dflt_curr_act']);
68                         display_notification(_('New bank account has been added'));
69         }
70                 $Mode = 'RESET';
71         }
72
73 elseif( $Mode == 'Delete')
74 {
75         //the link to delete a selected record was clicked instead of the submit button
76
77         $cancel_delete = 0;
78         $acc = db_escape($selected_id);
79         // PREVENT DELETES IF DEPENDENT RECORDS IN 'bank_trans'
80
81         $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_trans WHERE bank_act=$acc";
82         $result = db_query($sql,"check failed");
83         $myrow = db_fetch_row($result);
84         if ($myrow[0] > 0) 
85         {
86                 $cancel_delete = 1;
87                 display_error(_("Cannot delete this bank account because transactions have been created using this account."));
88         }
89         $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_pos WHERE pos_account=$acc";
90         $result = db_query($sql,"check failed");
91         $myrow = db_fetch_row($result);
92         if ($myrow[0] > 0) 
93         {
94                 $cancel_delete = 1;
95                 display_error(_("Cannot delete this bank account because POS definitions have been created using this account."));
96         }
97         if (!$cancel_delete) 
98         {
99                 delete_bank_account($selected_id);
100                 display_notification(_('Selected bank account has been deleted'));
101         } //end if Delete bank account
102         $Mode = 'RESET';
103
104
105 if ($Mode == 'RESET')
106 {
107         $selected_id = -1;
108         $_POST['bank_name']  =  $_POST['bank_account_name']  = '';
109         $_POST['bank_account_number'] = $_POST['bank_address'] = '';
110 }
111
112 /* Always show the list of accounts */
113
114 $sql = "SELECT account.*, gl_account.account_name 
115         FROM ".TB_PREF."bank_accounts account, ".TB_PREF."chart_master gl_account 
116         WHERE account.account_code = gl_account.account_code";
117 if (!check_value('show_inactive')) $sql .= " AND !account.inactive";
118 $sql .= " ORDER BY account_code, bank_curr_code";
119
120 $result = db_query($sql,"could not get bank accounts");
121
122 check_db_error("The bank accounts set up could not be retreived", $sql);
123
124 start_form();
125 start_table("$table_style width='80%'");
126
127 $th = array(_("Account Name"), _("Type"), _("Currency"), _("GL Account"), 
128         _("Bank"), _("Number"), _("Bank Address"), _("Dflt"), '','');
129 inactive_control_column($th);
130 table_header($th);      
131
132 $k = 0; 
133 while ($myrow = db_fetch($result)) 
134 {
135         
136         alt_table_row_color($k);
137
138     label_cell($myrow["bank_account_name"], "nowrap");
139         label_cell($bank_account_types[$myrow["account_type"]], "nowrap");
140     label_cell($myrow["bank_curr_code"], "nowrap");
141     label_cell($myrow["account_code"] . " " . $myrow["account_name"], "nowrap");
142     label_cell($myrow["bank_name"], "nowrap");
143     label_cell($myrow["bank_account_number"], "nowrap");
144     label_cell($myrow["bank_address"]);
145     if ($myrow["dflt_curr_act"])
146                 label_cell(_("Yes"));
147         else
148                 label_cell(_("No"));
149
150         inactive_control_cell($myrow["id"], $myrow["inactive"], 'bank_accounts', 'id');
151         edit_button_cell("Edit".$myrow["id"], _("Edit"));
152         delete_button_cell("Delete".$myrow["id"], _("Delete"));
153     end_row(); 
154 }
155
156 inactive_control_row($th);
157 end_table(1);
158
159 $is_editing = $selected_id != -1; 
160
161 start_table($table_style2);
162
163 if ($is_editing) 
164 {
165   if ($Mode == 'Edit') {        
166         $myrow = get_bank_account($selected_id);
167
168         $_POST['account_code'] = $myrow["account_code"];
169         $_POST['account_type'] = $myrow["account_type"];
170         $_POST['bank_name']  = $myrow["bank_name"];
171         $_POST['bank_account_name']  = $myrow["bank_account_name"];
172         $_POST['bank_account_number'] = $myrow["bank_account_number"];
173         $_POST['bank_address'] = $myrow["bank_address"];
174         $_POST['BankAccountCurrency'] = $myrow["bank_curr_code"];
175         $_POST['dflt_curr_act'] = $myrow["dflt_curr_act"];
176   }
177         hidden('selected_id', $selected_id);
178         hidden('account_code');
179         hidden('account_type');
180         hidden('BankAccountCurrency', $_POST['BankAccountCurrency']);   
181         set_focus('bank_account_name');
182
183
184 text_row(_("Bank Account Name:"), 'bank_account_name', null, 50, 100);
185
186 if ($is_editing) 
187 {
188         label_row(_("Account Type:"), $bank_account_types[$_POST['account_type']]);
189
190 else 
191 {
192         bank_account_types_list_row(_("Account Type:"), 'account_type', null); 
193 }
194 if ($is_editing) 
195 {
196         label_row(_("Bank Account Currency:"), $_POST['BankAccountCurrency']);
197
198 else 
199 {
200         currencies_list_row(_("Bank Account Currency:"), 'BankAccountCurrency', null);
201 }       
202
203 yesno_list_row(_("Default currency account:"), 'dflt_curr_act');
204
205 $curr = get_company_currency();
206 if ($is_editing && $curr == $_POST['BankAccountCurrency'])
207         label_row(_("Bank Account GL Code:"), $_POST['account_code']);
208 else
209         gl_all_accounts_list_row(_("Bank Account GL Code:"), 'account_code', null);
210
211 text_row(_("Bank Name:"), 'bank_name', null, 50, 60);
212 text_row(_("Bank Account Number:"), 'bank_account_number', null, 30, 60);
213 textarea_row(_("Bank Address:"), 'bank_address', null, 40, 5);
214
215 end_table(1);
216
217 submit_add_or_update_center($selected_id == -1, '', 'both');
218
219 end_form();
220
221 end_page();
222 ?>