Fixed bug [0000139] in bank account edition.
[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 = 10;
13 $path_to_root="../..";
14 include($path_to_root . "/includes/session.inc");
15
16 page(_("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                         display_notification(_('Bank account has been updated'));
47         } 
48         else 
49         {
50     
51                 add_bank_account($_POST['account_code'], $_POST['account_type'], 
52                                 $_POST['bank_account_name'], $_POST['bank_name'], 
53                         $_POST['bank_account_number'],  $_POST['bank_address'], 
54                                 $_POST['BankAccountCurrency']);
55                         display_notification(_('New bank account has been added'));
56         }
57                 $Mode = 'RESET';
58         }
59
60 elseif( $Mode == 'Delete')
61 {
62         //the link to delete a selected record was clicked instead of the submit button
63
64         $cancel_delete = 0;
65
66         // PREVENT DELETES IF DEPENDENT RECORDS IN 'bank_trans'
67
68         $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_trans WHERE bank_act='$selected_id'";
69         $result = db_query($sql,"check failed");
70         $myrow = db_fetch_row($result);
71         if ($myrow[0] > 0) 
72         {
73                 $cancel_delete = 1;
74                 display_error(_("Cannot delete this bank account because transactions have been created using this account."));
75         }
76         $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_pos WHERE pos_account='$selected_id'";
77         $result = db_query($sql,"check failed");
78         $myrow = db_fetch_row($result);
79         if ($myrow[0] > 0) 
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 $sql = "SELECT account.*, gl_account.account_name 
102         FROM ".TB_PREF."bank_accounts account, ".TB_PREF."chart_master gl_account 
103         WHERE account.account_code = gl_account.account_code"
104         ." ORDER BY account_code, bank_curr_code";
105
106 $result = db_query($sql,"could not get bank accounts");
107
108 check_db_error("The bank accounts set up could not be retreived", $sql);
109
110 start_form();
111 start_table("$table_style width='80%'");
112
113 $th = array(_("Account Name"), _("Type"), _("Currency"), _("GL Account"), 
114         _("Bank"), _("Number"), _("Bank Address"),'','');
115 table_header($th);      
116
117 $k = 0; 
118 while ($myrow = db_fetch($result)) 
119 {
120         
121         alt_table_row_color($k);
122
123     label_cell($myrow["bank_account_name"], "nowrap");
124         label_cell(bank_account_types::name($myrow["account_type"]), "nowrap");
125     label_cell($myrow["bank_curr_code"], "nowrap");
126     label_cell($myrow["account_code"] . " " . $myrow["account_name"], "nowrap");
127     label_cell($myrow["bank_name"], "nowrap");
128     label_cell($myrow["bank_account_number"], "nowrap");
129     label_cell($myrow["bank_address"]);
130         edit_button_cell("Edit".$myrow["id"], _("Edit"));
131         delete_button_cell("Delete".$myrow["id"], _("Delete"));
132     end_row(); 
133 }
134
135 end_table();
136 end_form();
137 echo '<br>';
138 start_form();
139
140 $is_editing = $selected_id != -1; 
141
142 start_table($table_style2);
143
144 if ($is_editing) 
145 {
146   if ($Mode == 'Edit') {        
147         $myrow = get_bank_account($selected_id);
148
149         $_POST['account_code'] = $myrow["account_code"];
150         $_POST['account_type'] = $myrow["account_type"];
151         $_POST['bank_name']  = $myrow["bank_name"];
152         $_POST['bank_account_name']  = $myrow["bank_account_name"];
153         $_POST['bank_account_number'] = $myrow["bank_account_number"];
154         $_POST['bank_address'] = $myrow["bank_address"];
155         $_POST['BankAccountCurrency'] = $myrow["bank_curr_code"];
156   }
157         hidden('selected_id', $selected_id);
158         hidden('account_code');
159         hidden('account_type');
160         hidden('BankAccountCurrency', $_POST['BankAccountCurrency']);   
161         set_focus('bank_account_name');
162
163
164 text_row(_("Bank Account Name:"), 'bank_account_name', null, 50, 100);
165
166 if ($is_editing) 
167 {
168         label_row(_("Account Type:"), bank_account_types::name($_POST['account_type']));
169
170 else 
171 {
172         bank_account_types_list_row(_("Account Type:"), 'account_type', null); 
173 }
174 if ($is_editing) 
175 {
176         label_row(_("Bank Account Currency:"), $_POST['BankAccountCurrency']);
177
178 else 
179 {
180         currencies_list_row(_("Bank Account Currency:"), 'BankAccountCurrency', null);
181 }       
182
183 if($is_editing)
184         label_row(_("Bank Account GL Code:"), $_POST['account_code']);
185 else 
186         gl_all_accounts_list_row(_("Bank Account GL Code:"), 'account_code', null);
187
188 text_row(_("Bank Name:"), 'bank_name', null, 50, 60);
189 text_row(_("Bank Account Number:"), 'bank_account_number', null, 30, 60);
190 textarea_row(_("Bank Address:"), 'bank_address', null, 40, 5);
191
192 end_table(1);
193
194 submit_add_or_update_center($selected_id == -1, '', true);
195
196 end_form();
197
198 end_page();
199 ?>