5081e3b5d6140acd32a0f050d7f29fb58f8abcc5
[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_type'], $_POST['bank_account_name'], $_POST['bank_name'], 
34                         $_POST['bank_account_number'], 
35                         $_POST['bank_address'], $_POST['BankAccountCurrency']);         
36                         display_notification(_('Bank account has been updated'));
37         } 
38         else 
39         {
40     
41                 add_bank_account($_POST['account_code'], $_POST['account_type'], $_POST['bank_account_name'], $_POST['bank_name'], 
42                         $_POST['bank_account_number'], 
43                         $_POST['bank_address'], $_POST['BankAccountCurrency']);
44                         display_notification(_('New bank account has been added'));
45         }
46                 $Mode = 'RESET';
47         }
48
49 elseif( $Mode == 'Delete')
50 {
51         //the link to delete a selected record was clicked instead of the submit button
52
53         $cancel_delete = 0;
54
55         // PREVENT DELETES IF DEPENDENT RECORDS IN 'bank_trans'
56
57         $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_trans WHERE bank_act='$selected_id'";
58         $result = db_query($sql,"check failed");
59         $myrow = db_fetch_row($result);
60         if ($myrow[0] > 0) 
61         {
62                 $cancel_delete = 1;
63                 display_error(_("Cannot delete this bank account because transactions have been created using this account."));
64         }
65         if (!$cancel_delete) 
66         {
67                 delete_bank_account($selected_id);
68                 display_notification(_('Selected bank account has been deleted'));
69         } //end if Delete bank account
70         $Mode = 'RESET';
71
72
73 if ($Mode == 'RESET')
74 {
75         $selected_id = -1;
76         $_POST['bank_name']  =  $_POST['bank_account_name']  = '';
77         $_POST['bank_account_number'] = $_POST['bank_address'] = '';
78 }
79
80 /* Always show the list of accounts */
81
82 $sql = "SELECT ".TB_PREF."bank_accounts.*, ".TB_PREF."chart_master.account_name FROM ".TB_PREF."bank_accounts, ".TB_PREF."chart_master 
83         WHERE ".TB_PREF."bank_accounts.account_code = ".TB_PREF."chart_master.account_code";
84 $result = db_query($sql,"could not get bank accounts");
85
86 check_db_error("The bank accounts set up could not be retreived", $sql);
87
88 start_form();
89 start_table("$table_style width='80%'");
90
91 $th = array(_("GL Account"), _("Bank"), _("Account Name"),
92         _("Type"), _("Number"), _("Currency"), _("Bank Address"),'','');
93 table_header($th);      
94
95 $k = 0; 
96 while ($myrow = db_fetch($result)) 
97 {
98         
99         alt_table_row_color($k);
100
101     label_cell($myrow["account_code"] . " " . $myrow["account_name"], "nowrap");
102     label_cell($myrow["bank_name"], "nowrap");
103     label_cell($myrow["bank_account_name"], "nowrap");
104         label_cell(bank_account_types::name($myrow["account_type"]), "nowrap");
105     label_cell($myrow["bank_account_number"], "nowrap");
106     label_cell($myrow["bank_curr_code"], "nowrap");
107     label_cell($myrow["bank_address"]);
108         edit_button_cell("Edit".$myrow["account_code"], _("Edit"));
109         edit_button_cell("Delete".$myrow["account_code"], _("Delete"));
110     end_row(); 
111 }
112
113 end_table();
114 end_form();
115 echo '<br>';
116 start_form();
117
118 $is_editing = $selected_id != -1; 
119
120 start_table($table_style2);
121
122 if ($is_editing) 
123 {
124   if ($Mode == 'Edit') {        
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');
137         hidden('BankAccountCurrency', $_POST['BankAccountCurrency']);   
138         label_row(_("Bank Account GL Code:"), $_POST['account_code']);
139         set_focus('account_type');
140
141 else 
142 {
143         gl_all_accounts_list_row(_("Bank Account GL Code:"), 'account_code', null, true);       
144         set_focus('account_code');
145 }
146
147 bank_account_types_list_row(_("Account Type:"), 'account_type', null); 
148
149 text_row(_("Bank Name:"), 'bank_name', null, 50, 60);
150 text_row(_("Bank Account Name:"), 'bank_account_name', null, 50, 100);
151 text_row(_("Bank Account Number:"), 'bank_account_number', null, 30, 60);
152
153 if ($is_editing) 
154 {
155         label_row(_("Bank Account Currency:"), $_POST['BankAccountCurrency']);
156
157 else 
158 {
159         currencies_list_row(_("Bank Account Currency:"), 'BankAccountCurrency', null);
160 }       
161
162 textarea_row(_("Bank Address:"), 'bank_address', null, 40, 5);
163
164 end_table(1);
165
166 submit_add_or_update_center($selected_id == -1, '', true);
167
168 end_form();
169
170 end_page();
171 ?>