bb83098ad3922c3242a64feb18f136b83b833187
[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(_("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         $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtors_master WHERE curr_code = $curr";
93         $result = db_query($sql);
94         $myrow = db_fetch_row($result);
95         if ($myrow[0] > 0) 
96         {
97                 display_error(_("Cannot delete this currency, because customer accounts have been created referring to this currency."));
98                 return false;
99         }
100
101         $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE curr_code = $curr";
102         $result = db_query($sql);
103         $myrow = db_fetch_row($result);
104         if ($myrow[0] > 0) 
105         {
106                 display_error(_("Cannot delete this currency, because supplier accounts have been created referring to this currency."));
107                 return false;
108         }
109                 
110         $sql= "SELECT COUNT(*) FROM ".TB_PREF."company WHERE curr_default = $curr";
111         $result = db_query($sql);
112         $myrow = db_fetch_row($result);
113         if ($myrow[0] > 0) 
114         {
115                 display_error(_("Cannot delete this currency, because the company preferences uses this currency."));
116                 return false;
117         }
118         
119         // see if there are any bank accounts that use this currency
120         $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE bank_curr_code = $curr";
121         $result = db_query($sql);
122         $myrow = db_fetch_row($result);
123         if ($myrow[0] > 0) 
124         {
125                 display_error(_("Cannot delete this currency, because thre are bank accounts that use this currency."));
126                 return false;
127         }
128         
129         return true;
130 }
131
132 //---------------------------------------------------------------------------------------------
133
134 function handle_delete()
135 {
136         global $selected_id, $Mode;
137         if (check_can_delete()) {
138         //only delete if used in neither customer or supplier, comp prefs, bank trans accounts
139                 delete_currency($selected_id);
140                 display_notification(_('Selected currency has been deleted'));
141         }
142         $Mode = 'RESET';
143 }
144
145 //---------------------------------------------------------------------------------------------
146
147 function display_currencies()
148 {
149         global $table_style;
150
151         $company_currency = get_company_currency();
152         
153     $result = get_currencies(check_value('show_inactive'));
154     start_table($table_style);
155     $th = array(_("Abbreviation"), _("Symbol"), _("Currency Name"),
156         _("Hundredths name"), _("Country"), _("Auto update"), "", "");
157         inactive_control_column($th);
158     table_header($th);  
159     
160     $k = 0; //row colour counter
161     
162     while ($myrow = db_fetch($result)) 
163     {
164         
165         if ($myrow[1] == $company_currency) 
166         {
167                 start_row("class='currencybg'");
168         } 
169         else
170                 alt_table_row_color($k);
171                 
172         label_cell($myrow["curr_abrev"]);
173                 label_cell($myrow["curr_symbol"]);
174                 label_cell($myrow["currency"]);
175                 label_cell($myrow["hundreds_name"]);
176                 label_cell($myrow["country"]);
177                 label_cell(     $myrow[1] == $company_currency ? '-' : 
178                         ($myrow["auto_update"] ? _('Yes') :_('No')), "align='center'");
179                 inactive_control_cell($myrow["curr_abrev"], $myrow["inactive"], 'currencies', 'curr_abrev');
180                 edit_button_cell("Edit".$myrow["curr_abrev"], _("Edit"));
181                 if ($myrow["curr_abrev"] != $company_currency)
182                         delete_button_cell("Delete".$myrow["curr_abrev"], _("Delete"));
183                 else
184                         label_cell('');
185                 end_row();
186                 
187     } //END WHILE LIST LOOP
188     
189         inactive_control_row($th);
190     end_table();
191     display_note(_("The marked currency is the home currency which cannot be deleted."), 0, 0, "class='currentfg'");
192 }
193
194 //---------------------------------------------------------------------------------------------
195
196 function display_currency_edit($selected_id)
197 {
198         global $table_style2, $Mode;
199         
200         start_table($table_style2);
201
202         if ($selected_id != '') 
203         {
204                 if ($Mode == 'Edit') {
205                         //editing an existing currency
206                         $myrow = get_currency($selected_id);
207
208                         $_POST['Abbreviation'] = $myrow["curr_abrev"];
209                         $_POST['Symbol'] = $myrow["curr_symbol"];
210                         $_POST['CurrencyName']  = $myrow["currency"];
211                         $_POST['country']  = $myrow["country"];
212                         $_POST['hundreds_name']  = $myrow["hundreds_name"];
213                         $_POST['auto_update']  = $myrow["auto_update"];
214                 }
215                 hidden('Abbreviation');
216                 hidden('selected_id', $selected_id);
217                 label_row(_("Currency Abbreviation:"), $_POST['Abbreviation']);
218         } 
219         else 
220         { 
221                 $_POST['auto_update']  = 1;
222                 text_row_ex(_("Currency Abbreviation:"), 'Abbreviation', 4, 3);
223         }
224
225         text_row_ex(_("Currency Symbol:"), 'Symbol', 10);
226         text_row_ex(_("Currency Name:"), 'CurrencyName', 20);
227         text_row_ex(_("Hundredths Name:"), 'hundreds_name', 15);        
228         text_row_ex(_("Country:"), 'country', 40);      
229         check_row(_("Automatic exchange rate update:"), 'auto_update', get_post('auto_update'));
230         end_table(1);
231
232         submit_add_or_update_center($selected_id == '', '', 'both');
233 }
234
235 //---------------------------------------------------------------------------------------------
236
237 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
238         handle_submit();
239
240 //--------------------------------------------------------------------------------------------- 
241
242 if ($Mode == 'Delete')
243         handle_delete();
244
245 //---------------------------------------------------------------------------------------------
246 if ($Mode == 'RESET')
247 {
248                 $selected_id = '';
249                 $_POST['Abbreviation'] = $_POST['Symbol'] = '';
250                 $_POST['CurrencyName'] = $_POST['country']  = '';
251                 $_POST['hundreds_name']  = '';
252 }
253
254 start_form();
255 display_currencies();
256
257 display_currency_edit($selected_id);
258 end_form();
259 //---------------------------------------------------------------------------------------------
260
261 end_page();
262
263 ?>