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