Added check for POS relations before account removal.
[fa-stable.git] / gl / manage / bank_accounts.php
1 <?php
2
3 $page_security = 10;
4 $path_to_root="../..";
5 include($path_to_root . "/includes/session.inc");
6
7 page(_("Bank Accounts"));
8
9 include($path_to_root . "/includes/ui.inc");
10
11 simple_page_mode();
12 //-----------------------------------------------------------------------------------
13
14 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
15 {
16
17         //initialise no input errors assumed initially before we test
18         $input_error = 0;
19
20         //first off validate inputs sensible
21         if (strlen($_POST['bank_account_name']) == 0) 
22         {
23                 $input_error = 1;
24                 display_error(_("The bank account name cannot be empty."));
25                 set_focus('bank_account_name');
26         } 
27         
28         if ($input_error != 1)
29         {
30         if ($selected_id != -1) 
31         {
32                 
33                 update_bank_account($selected_id, $_POST['account_code'],
34                                 $_POST['account_type'], $_POST['bank_account_name'], 
35                                 $_POST['bank_name'], $_POST['bank_account_number'], 
36                         $_POST['bank_address'], $_POST['BankAccountCurrency']);         
37                         display_notification(_('Bank account has been updated'));
38         } 
39         else 
40         {
41     
42                 add_bank_account($_POST['account_code'], $_POST['account_type'], 
43                                 $_POST['bank_account_name'], $_POST['bank_name'], 
44                         $_POST['bank_account_number'],  $_POST['bank_address'], 
45                                 $_POST['BankAccountCurrency']);
46                         display_notification(_('New bank account has been added'));
47         }
48                 $Mode = 'RESET';
49         }
50
51 elseif( $Mode == 'Delete')
52 {
53         //the link to delete a selected record was clicked instead of the submit button
54
55         $cancel_delete = 0;
56
57         // PREVENT DELETES IF DEPENDENT RECORDS IN 'bank_trans'
58
59         $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_trans WHERE bank_act='$selected_id'";
60         $result = db_query($sql,"check failed");
61         $myrow = db_fetch_row($result);
62         if ($myrow[0] > 0) 
63         {
64                 $cancel_delete = 1;
65                 display_error(_("Cannot delete this bank account because transactions have been created using this account."));
66         }
67         $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_pos WHERE pos_account='$selected_id'";
68         $result = db_query($sql,"check failed");
69         $myrow = db_fetch_row($result);
70         if ($myrow[0] > 0) 
71         {
72                 $cancel_delete = 1;
73                 display_error(_("Cannot delete this bank account because POS definitions have been created using this account."));
74         }
75         if (!$cancel_delete) 
76         {
77                 delete_bank_account($selected_id);
78                 display_notification(_('Selected bank account has been deleted'));
79         } //end if Delete bank account
80         $Mode = 'RESET';
81
82
83 if ($Mode == 'RESET')
84 {
85         $selected_id = -1;
86         $_POST['bank_name']  =  $_POST['bank_account_name']  = '';
87         $_POST['bank_account_number'] = $_POST['bank_address'] = '';
88 }
89
90 /* Always show the list of accounts */
91
92 $sql = "SELECT account.*, gl_account.account_name 
93         FROM ".TB_PREF."bank_accounts account, ".TB_PREF."chart_master gl_account 
94         WHERE account.account_code = gl_account.account_code"
95         ." ORDER BY account_code, bank_curr_code";
96
97 $result = db_query($sql,"could not get bank accounts");
98
99 check_db_error("The bank accounts set up could not be retreived", $sql);
100
101 start_form();
102 start_table("$table_style width='80%'");
103
104 $th = array(_("Account Name"), _("Type"), _("Currency"), _("GL Account"), 
105         _("Bank"), _("Number"), _("Bank Address"),'','');
106 table_header($th);      
107
108 $k = 0; 
109 while ($myrow = db_fetch($result)) 
110 {
111         
112         alt_table_row_color($k);
113
114     label_cell($myrow["bank_account_name"], "nowrap");
115         label_cell(bank_account_types::name($myrow["account_type"]), "nowrap");
116     label_cell($myrow["bank_curr_code"], "nowrap");
117     label_cell($myrow["account_code"] . " " . $myrow["account_name"], "nowrap");
118     label_cell($myrow["bank_name"], "nowrap");
119     label_cell($myrow["bank_account_number"], "nowrap");
120     label_cell($myrow["bank_address"]);
121         edit_button_cell("Edit".$myrow["id"], _("Edit"));
122         edit_button_cell("Delete".$myrow["id"], _("Delete"));
123     end_row(); 
124 }
125
126 end_table();
127 end_form();
128 echo '<br>';
129 start_form();
130
131 $is_editing = $selected_id != -1; 
132
133 start_table($table_style2);
134
135 if ($is_editing) 
136 {
137   if ($Mode == 'Edit') {        
138         $myrow = get_bank_account($selected_id);
139
140         $_POST['account_code'] = $myrow["account_code"];
141         $_POST['account_type'] = $myrow["account_type"];
142         $_POST['bank_name']  = $myrow["bank_name"];
143         $_POST['bank_account_name']  = $myrow["bank_account_name"];
144         $_POST['bank_account_number'] = $myrow["bank_account_number"];
145         $_POST['bank_address'] = $myrow["bank_address"];
146         $_POST['BankAccountCurrency'] = $myrow["bank_curr_code"];
147   }
148         hidden('selected_id', $selected_id);
149         hidden('account_code');
150         hidden('BankAccountCurrency', $_POST['BankAccountCurrency']);   
151         set_focus('bank_account_name');
152
153
154 text_row(_("Bank Account Name:"), 'bank_account_name', null, 50, 100);
155
156 bank_account_types_list_row(_("Account Type:"), 'account_type', null); 
157
158 if ($is_editing) 
159 {
160         label_row(_("Bank Account Currency:"), $_POST['BankAccountCurrency']);
161
162 else 
163 {
164         currencies_list_row(_("Bank Account Currency:"), 'BankAccountCurrency', null);
165 }       
166
167 if($is_editing)
168         label_row(_("Bank Account GL Code:"), $_POST['account_code']);
169 else 
170         gl_all_accounts_list_row(_("Bank Account GL Code:"), 'account_code', null, false);
171
172 text_row(_("Bank Name:"), 'bank_name', null, 50, 60);
173 text_row(_("Bank Account Number:"), 'bank_account_number', null, 30, 60);
174 textarea_row(_("Bank Address:"), 'bank_address', null, 40, 5);
175
176 end_table(1);
177
178 submit_add_or_update_center($selected_id == -1, '', true);
179
180 end_form();
181
182 end_page();
183 ?>