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