Moved all SQL statements from PHP files into relevant *_db.inc files.
[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 (currency_in_debtors($curr))
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 (currency_in_suppliers($curr))
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 (currency_in_company($curr))
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 (currenty_in_bank_accounts($curr))
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         global $table_style;
138
139         $company_currency = get_company_currency();
140         
141     $result = get_currencies(check_value('show_inactive'));
142     start_table($table_style);
143     $th = array(_("Abbreviation"), _("Symbol"), _("Currency Name"),
144         _("Hundredths name"), _("Country"), _("Auto update"), "", "");
145         inactive_control_column($th);
146     table_header($th);  
147     
148     $k = 0; //row colour counter
149     
150     while ($myrow = db_fetch($result)) 
151     {
152         
153         if ($myrow[1] == $company_currency) 
154         {
155                 start_row("class='currencybg'");
156         } 
157         else
158                 alt_table_row_color($k);
159                 
160         label_cell($myrow["curr_abrev"]);
161                 label_cell($myrow["curr_symbol"]);
162                 label_cell($myrow["currency"]);
163                 label_cell($myrow["hundreds_name"]);
164                 label_cell($myrow["country"]);
165                 label_cell(     $myrow[1] == $company_currency ? '-' : 
166                         ($myrow["auto_update"] ? _('Yes') :_('No')), "align='center'");
167                 inactive_control_cell($myrow["curr_abrev"], $myrow["inactive"], 'currencies', 'curr_abrev');
168                 edit_button_cell("Edit".$myrow["curr_abrev"], _("Edit"));
169                 if ($myrow["curr_abrev"] != $company_currency)
170                         delete_button_cell("Delete".$myrow["curr_abrev"], _("Delete"));
171                 else
172                         label_cell('');
173                 end_row();
174                 
175     } //END WHILE LIST LOOP
176     
177         inactive_control_row($th);
178     end_table();
179     display_note(_("The marked currency is the home currency which cannot be deleted."), 0, 0, "class='currentfg'");
180 }
181
182 //---------------------------------------------------------------------------------------------
183
184 function display_currency_edit($selected_id)
185 {
186         global $table_style2, $Mode;
187         
188         start_table($table_style2);
189
190         if ($selected_id != '') 
191         {
192                 if ($Mode == 'Edit') {
193                         //editing an existing currency
194                         $myrow = get_currency($selected_id);
195
196                         $_POST['Abbreviation'] = $myrow["curr_abrev"];
197                         $_POST['Symbol'] = $myrow["curr_symbol"];
198                         $_POST['CurrencyName']  = $myrow["currency"];
199                         $_POST['country']  = $myrow["country"];
200                         $_POST['hundreds_name']  = $myrow["hundreds_name"];
201                         $_POST['auto_update']  = $myrow["auto_update"];
202                 }
203                 hidden('Abbreviation');
204                 hidden('selected_id', $selected_id);
205                 label_row(_("Currency Abbreviation:"), $_POST['Abbreviation']);
206         } 
207         else 
208         { 
209                 $_POST['auto_update']  = 1;
210                 text_row_ex(_("Currency Abbreviation:"), 'Abbreviation', 4, 3);
211         }
212
213         text_row_ex(_("Currency Symbol:"), 'Symbol', 10);
214         text_row_ex(_("Currency Name:"), 'CurrencyName', 20);
215         text_row_ex(_("Hundredths Name:"), 'hundreds_name', 15);        
216         text_row_ex(_("Country:"), 'country', 40);      
217         check_row(_("Automatic exchange rate update:"), 'auto_update', get_post('auto_update'));
218         end_table(1);
219
220         submit_add_or_update_center($selected_id == '', '', 'both');
221 }
222
223 //---------------------------------------------------------------------------------------------
224
225 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
226         handle_submit();
227
228 //--------------------------------------------------------------------------------------------- 
229
230 if ($Mode == 'Delete')
231         handle_delete();
232
233 //---------------------------------------------------------------------------------------------
234 if ($Mode == 'RESET')
235 {
236                 $selected_id = '';
237                 $_POST['Abbreviation'] = $_POST['Symbol'] = '';
238                 $_POST['CurrencyName'] = $_POST['country']  = '';
239                 $_POST['hundreds_name']  = '';
240 }
241
242 start_form();
243 display_currencies();
244
245 display_currency_edit($selected_id);
246 end_form();
247 //---------------------------------------------------------------------------------------------
248
249 end_page();
250
251 ?>