Replaced the global variables for table styles to defined CSS classes.
[fa-stable.git] / gl / manage / currencies.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_CURRENCY';
13 $path_to_root = "../..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 page(_($help_context = "Currencies"));
17
18 include_once($path_to_root . "/includes/ui.inc");
19 include_once($path_to_root . "/includes/banking.inc");
20
21 simple_page_mode(false);
22
23 //---------------------------------------------------------------------------------------------
24
25 function check_data()
26 {
27         if (strlen($_POST['Abbreviation']) == 0) 
28         {
29                 display_error( _("The currency abbreviation must be entered."));
30                 set_focus('Abbreviation');
31                 return false;
32         } 
33         elseif (strlen($_POST['CurrencyName']) == 0) 
34         {
35                 display_error( _("The currency name must be entered."));
36                 set_focus('CurrencyName');
37                 return false;           
38         } 
39         elseif (strlen($_POST['Symbol']) == 0) 
40         {
41                 display_error( _("The currency symbol must be entered."));
42                 set_focus('Symbol');
43                 return false;           
44         } 
45         elseif (strlen($_POST['hundreds_name']) == 0) 
46         {
47                 display_error( _("The hundredths name must be entered."));
48                 set_focus('hundreds_name');
49                 return false;           
50         }       
51         
52         return true;
53 }
54
55 //---------------------------------------------------------------------------------------------
56
57 function handle_submit()
58 {
59         global $selected_id, $Mode;
60         
61         if (!check_data())
62                 return false;
63                 
64         if ($selected_id != "") 
65         {
66
67                 update_currency($_POST['Abbreviation'], $_POST['Symbol'], $_POST['CurrencyName'], 
68                         $_POST['country'], $_POST['hundreds_name'], check_value('auto_update'));
69                 display_notification(_('Selected currency settings has been updated'));
70         } 
71         else 
72         {
73
74                 add_currency($_POST['Abbreviation'], $_POST['Symbol'], $_POST['CurrencyName'], 
75                         $_POST['country'], $_POST['hundreds_name'], check_value('auto_update'));
76                 display_notification(_('New currency has been added'));
77         }       
78         $Mode = 'RESET';
79 }
80
81 //---------------------------------------------------------------------------------------------
82
83 function check_can_delete()
84 {
85         global $selected_id;
86                 
87         if ($selected_id == "")
88                 return false;
89         $curr = db_escape($selected_id);
90
91         // PREVENT DELETES IF DEPENDENT RECORDS IN debtors_master
92         if (key_in_foreign_table($curr, 'debtors_master', 'curr_code', true))
93         {
94                 display_error(_("Cannot delete this currency, because customer accounts have been created referring to this currency."));
95                 return false;
96         }
97
98         if (key_in_foreign_table($curr, 'suppliers', 'curr_code', true))
99         {
100                 display_error(_("Cannot delete this currency, because supplier accounts have been created referring to this currency."));
101                 return false;
102         }
103
104         if (key_in_foreign_table($curr, 'company', 'curr_default', true))               
105         {
106                 display_error(_("Cannot delete this currency, because the company preferences uses this currency."));
107                 return false;
108         }
109         
110         // see if there are any bank accounts that use this currency
111         if (key_in_foreign_table($curr, 'bank_accounts', 'bank_curr_code', true))
112         {
113                 display_error(_("Cannot delete this currency, because thre are bank accounts that use this currency."));
114                 return false;
115         }
116         
117         return true;
118 }
119
120 //---------------------------------------------------------------------------------------------
121
122 function handle_delete()
123 {
124         global $selected_id, $Mode;
125         if (check_can_delete()) {
126         //only delete if used in neither customer or supplier, comp prefs, bank trans accounts
127                 delete_currency($selected_id);
128                 display_notification(_('Selected currency has been deleted'));
129         }
130         $Mode = 'RESET';
131 }
132
133 //---------------------------------------------------------------------------------------------
134
135 function display_currencies()
136 {
137         $company_currency = get_company_currency();
138         
139     $result = get_currencies(check_value('show_inactive'));
140     start_table(TABLESTYLE);
141     $th = array(_("Abbreviation"), _("Symbol"), _("Currency Name"),
142         _("Hundredths name"), _("Country"), _("Auto update"), "", "");
143         inactive_control_column($th);
144     table_header($th);  
145     
146     $k = 0; //row colour counter
147     
148     while ($myrow = db_fetch($result)) 
149     {
150         
151         if ($myrow[1] == $company_currency) 
152         {
153                 start_row("class='currencybg'");
154         } 
155         else
156                 alt_table_row_color($k);
157                 
158         label_cell($myrow["curr_abrev"]);
159                 label_cell($myrow["curr_symbol"]);
160                 label_cell($myrow["currency"]);
161                 label_cell($myrow["hundreds_name"]);
162                 label_cell($myrow["country"]);
163                 label_cell(     $myrow[1] == $company_currency ? '-' : 
164                         ($myrow["auto_update"] ? _('Yes') :_('No')), "align='center'");
165                 inactive_control_cell($myrow["curr_abrev"], $myrow["inactive"], 'currencies', 'curr_abrev');
166                 edit_button_cell("Edit".$myrow["curr_abrev"], _("Edit"));
167                 if ($myrow["curr_abrev"] != $company_currency)
168                         delete_button_cell("Delete".$myrow["curr_abrev"], _("Delete"));
169                 else
170                         label_cell('');
171                 end_row();
172                 
173     } //END WHILE LIST LOOP
174     
175         inactive_control_row($th);
176     end_table();
177     display_note(_("The marked currency is the home currency which cannot be deleted."), 0, 0, "class='currentfg'");
178 }
179
180 //---------------------------------------------------------------------------------------------
181
182 function display_currency_edit($selected_id)
183 {
184         global $Mode;
185         
186         start_table(TABLESTYLE2);
187
188         if ($selected_id != '') 
189         {
190                 if ($Mode == 'Edit') {
191                         //editing an existing currency
192                         $myrow = get_currency($selected_id);
193
194                         $_POST['Abbreviation'] = $myrow["curr_abrev"];
195                         $_POST['Symbol'] = $myrow["curr_symbol"];
196                         $_POST['CurrencyName']  = $myrow["currency"];
197                         $_POST['country']  = $myrow["country"];
198                         $_POST['hundreds_name']  = $myrow["hundreds_name"];
199                         $_POST['auto_update']  = $myrow["auto_update"];
200                 }
201                 hidden('Abbreviation');
202                 hidden('selected_id', $selected_id);
203                 label_row(_("Currency Abbreviation:"), $_POST['Abbreviation']);
204         } 
205         else 
206         { 
207                 $_POST['auto_update']  = 1;
208                 text_row_ex(_("Currency Abbreviation:"), 'Abbreviation', 4, 3);
209         }
210
211         text_row_ex(_("Currency Symbol:"), 'Symbol', 10);
212         text_row_ex(_("Currency Name:"), 'CurrencyName', 20);
213         text_row_ex(_("Hundredths Name:"), 'hundreds_name', 15);        
214         text_row_ex(_("Country:"), 'country', 40);      
215         check_row(_("Automatic exchange rate update:"), 'auto_update', get_post('auto_update'));
216         end_table(1);
217
218         submit_add_or_update_center($selected_id == '', '', 'both');
219 }
220
221 //---------------------------------------------------------------------------------------------
222
223 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
224         handle_submit();
225
226 //--------------------------------------------------------------------------------------------- 
227
228 if ($Mode == 'Delete')
229         handle_delete();
230
231 //---------------------------------------------------------------------------------------------
232 if ($Mode == 'RESET')
233 {
234                 $selected_id = '';
235                 $_POST['Abbreviation'] = $_POST['Symbol'] = '';
236                 $_POST['CurrencyName'] = $_POST['country']  = '';
237                 $_POST['hundreds_name']  = '';
238 }
239
240 start_form();
241 display_currencies();
242
243 display_currency_edit($selected_id);
244 end_form();
245 //---------------------------------------------------------------------------------------------
246
247 end_page();
248
249 ?>