Focus set to invalid field after submit check fail
[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 if (isset($_GET['selected_id'])) 
12 {
13         $selected_id = $_GET['selected_id'];
14
15 elseif (isset($_POST['selected_id'])) 
16 {
17         $selected_id = $_POST['selected_id'];
18 }
19
20 if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM'])) 
21 {
22
23         //initialise no input errors assumed initially before we test
24         $input_error = 0;
25
26         //first off validate inputs sensible
27         if (strlen($_POST['bank_account_name']) == 0) 
28         {
29                 $input_error = 1;
30                 display_error(_("The bank account name cannot be empty."));
31                 set_focus('bank_account_name');
32         } 
33         
34         if ($input_error != 1)
35         {
36         if (isset($selected_id)) 
37         {
38                 
39                 update_bank_account($selected_id, $_POST['account_type'], $_POST['bank_account_name'], $_POST['bank_name'], 
40                         $_POST['bank_account_number'], 
41                         $_POST['bank_address'], $_POST['BankAccountCurrency']);         
42         } 
43         else 
44         {
45     
46                 add_bank_account($_POST['account_code'], $_POST['account_type'], $_POST['bank_account_name'], $_POST['bank_name'], 
47                         $_POST['bank_account_number'], 
48                         $_POST['bank_address'], $_POST['BankAccountCurrency']);
49         }
50                 
51                 meta_forward($_SERVER['PHP_SELF']); 
52         }
53
54
55 elseif (isset($_GET['delete'])) 
56 {
57         //the link to delete a selected record was clicked instead of the submit button
58
59         $cancel_delete = 0;
60
61         // PREVENT DELETES IF DEPENDENT RECORDS IN 'bank_trans'
62
63         $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_trans WHERE bank_act='$selected_id'";
64         $result = db_query($sql,"check failed");
65         $myrow = db_fetch_row($result);
66         if ($myrow[0] > 0) 
67         {
68                 $cancel_delete = 1;
69                 display_error(_("Cannot delete this bank account because transactions have been created using this account."));
70         }
71         if (!$cancel_delete) 
72         {
73                 delete_bank_account($selected_id);
74                 meta_forward($_SERVER['PHP_SELF']);
75         } //end if Delete bank account
76 }
77
78 /* Always show the list of accounts */
79
80 $sql = "SELECT ".TB_PREF."bank_accounts.*, ".TB_PREF."chart_master.account_name FROM ".TB_PREF."bank_accounts, ".TB_PREF."chart_master 
81         WHERE ".TB_PREF."bank_accounts.account_code = ".TB_PREF."chart_master.account_code";
82 $result = db_query($sql,"could not get bank accounts");
83
84 check_db_error("The bank accounts set up could not be retreived", $sql);
85
86 start_table("$table_style width='80%'");
87
88 $th = array(_("GL Account"), _("Bank"), _("Account Name"),
89         _("Type"), _("Number"), _("Currency"), _("Bank Address"));
90 table_header($th);      
91
92 $k = 0; 
93 while ($myrow = db_fetch($result)) 
94 {
95         
96         alt_table_row_color($k);
97
98     label_cell($myrow["account_code"] . " " . $myrow["account_name"], "nowrap");
99     label_cell($myrow["bank_name"], "nowrap");
100     label_cell($myrow["bank_account_name"], "nowrap");
101         label_cell(bank_account_types::name($myrow["account_type"]), "nowrap");
102     label_cell($myrow["bank_account_number"], "nowrap");
103     label_cell($myrow["bank_curr_code"], "nowrap");
104     label_cell($myrow["bank_address"]);
105     edit_link_cell("selected_id=" . $myrow["account_code"]);
106     delete_link_cell("selected_id=" . $myrow["account_code"]. "&delete=1");
107     end_row(); 
108 }
109 //END WHILE LIST LOOP
110
111
112 end_table();
113
114 hyperlink_no_params($_SERVER['PHP_SELF'], _("New Bank Account"));
115
116 start_form();
117
118 $is_editing = (isset($selected_id) && !isset($_GET['delete'])); 
119
120 start_table($table_style2);
121
122 if ($is_editing) 
123 {
124         
125         $myrow = get_bank_account($selected_id);
126
127         $_POST['account_code'] = $myrow["account_code"];
128         $_POST['account_type'] = $myrow["account_type"];
129         $_POST['bank_name']  = $myrow["bank_name"];
130         $_POST['bank_account_name']  = $myrow["bank_account_name"];
131         $_POST['bank_account_number'] = $myrow["bank_account_number"];
132         $_POST['bank_address'] = $myrow["bank_address"];
133         $_POST['BankAccountCurrency'] = $myrow["bank_curr_code"];
134
135         hidden('selected_id', $selected_id);
136         hidden('account_code', $_POST['account_code']);
137         hidden('BankAccountCurrency', $_POST['BankAccountCurrency']);   
138         label_row(_("Bank Account GL Code:"), $_POST['account_code']);
139
140 else 
141 {
142         gl_all_accounts_list_row(_("Bank Account GL Code:"), 'account_code', null, true);       
143 }
144
145 bank_account_types_list_row(_("Account Type:"), 'account_type', null); 
146
147 text_row(_("Bank Name:"), 'bank_name', null, 50, 50);
148 text_row(_("Bank Account Name:"), 'bank_account_name', null, 50, 50);
149 text_row(_("Bank Account Number:"), 'bank_account_number', null, 30, 30);
150
151 if ($is_editing) 
152 {
153         label_row(_("Bank Account Currency:"), $_POST['BankAccountCurrency']);
154
155 else 
156 {
157         currencies_list_row(_("Bank Account Currency:"), 'BankAccountCurrency', null);
158 }       
159
160 textarea_row(_("Bank Address:"), 'bank_address', null, 40, 5);
161 //text_row(_("Bank Address:"), 'bank_address', null, 70, 70);
162
163 end_table(1);
164
165 submit_add_or_update_center(!isset($selected_id));
166
167 end_form();
168
169 end_page();
170 ?>