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_EXCHANGERATE';
13 $path_to_root = "../..";
14 include($path_to_root . "/includes/db_pager.inc");
15 include_once($path_to_root . "/includes/session.inc");
17 include_once($path_to_root . "/includes/date_functions.inc");
18 include_once($path_to_root . "/includes/ui.inc");
19 include_once($path_to_root . "/includes/banking.inc");
22 if (user_use_date_picker())
23 $js .= get_js_date_picker();
24 page(_($help_context = "Exchange Rates"), false, false, "", $js);
26 simple_page_mode(false);
28 //---------------------------------------------------------------------------------------------
29 function check_data($selected_id)
31 if (!is_date($_POST['date_']))
33 display_error( _("The entered date is invalid."));
37 if (input_num('BuyRate') <= 0)
39 display_error( _("The exchange rate cannot be zero or a negative number."));
43 if (!$selected_id && get_date_exchange_rate($_POST['curr_abrev'], $_POST['date_']))
45 display_error( _("The exchange rate for the date is already there."));
52 //---------------------------------------------------------------------------------------------
54 function handle_submit()
58 if (!check_data($selected_id))
61 if ($selected_id != "")
64 update_exchange_rate($_POST['curr_abrev'], $_POST['date_'],
65 input_num('BuyRate'), input_num('BuyRate'));
70 add_exchange_rate($_POST['curr_abrev'], $_POST['date_'],
71 input_num('BuyRate'), input_num('BuyRate'));
78 //---------------------------------------------------------------------------------------------
80 function handle_delete()
84 if ($selected_id == "")
86 delete_exchange_rate($selected_id);
91 //---------------------------------------------------------------------------------------------
92 function edit_link($row)
94 return button('Edit'.$row["id"], _("Edit"), true, ICON_EDIT);
97 function del_link($row)
99 return button('Delete'.$row["id"], _("Delete"), true, ICON_DELETE);
102 function display_rates($curr_code)
107 //---------------------------------------------------------------------------------------------
109 function display_rate_edit()
111 global $selected_id, $Ajax, $SysPrefs;
112 $xchg_rate_provider = ((isset($SysPrefs->xr_providers) && isset($SysPrefs->dflt_xr_provider))
113 ? $SysPrefs->xr_providers[$SysPrefs->dflt_xr_provider] : 'ECB');
114 start_table(TABLESTYLE2);
116 if ($selected_id != "")
118 //editing an existing exchange rate
120 $myrow = get_exchange_rate($selected_id);
122 $_POST['date_'] = sql2date($myrow["date_"]);
123 $_POST['BuyRate'] = maxprec_format($myrow["rate_buy"]);
125 hidden('selected_id', $selected_id);
126 hidden('date_', $_POST['date_']);
128 label_row(_("Date to Use From:"), $_POST['date_']);
132 $_POST['date_'] = Today();
133 $_POST['BuyRate'] = '';
134 date_row(_("Date to Use From:"), 'date_');
136 if (isset($_POST['get_rate']))
139 maxprec_format(retrieve_exrate($_POST['curr_abrev'], $_POST['date_']));
140 $Ajax->activate('BuyRate');
142 amount_row(_("Exchange Rate:"), 'BuyRate', null, '',
143 submit('get_rate',_("Get"), false, _('Get current rate from') . ' ' . $xchg_rate_provider , true), 'max');
147 submit_add_or_update_center($selected_id == '', '', 'both');
149 display_note(_("Exchange rates are entered against the company currency."), 1);
152 //---------------------------------------------------------------------------------------------
154 function clear_data()
156 unset($_POST['selected_id']);
157 unset($_POST['date_']);
158 unset($_POST['BuyRate']);
161 //---------------------------------------------------------------------------------------------
163 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
166 //---------------------------------------------------------------------------------------------
168 if ($Mode == 'Delete')
172 //---------------------------------------------------------------------------------------------
176 if (!isset($_POST['curr_abrev']))
177 $_POST['curr_abrev'] = get_global_curr_code();
180 echo _("Select a currency :") . " ";
181 echo currencies_list('curr_abrev', null, true, true);
184 // if currency sel has changed, clear the form
185 if ($_POST['curr_abrev'] != get_global_curr_code())
191 set_global_curr_code(get_post('curr_abrev'));
193 $sql = get_sql_for_exchange_rates(get_post('curr_abrev'));
196 _("Date to Use From") => 'date',
197 _("Exchange Rate") => 'rate',
198 array('insert'=>true, 'fun'=>'edit_link'),
199 array('insert'=>true, 'fun'=>'del_link'),
201 $table =& new_db_pager('orders_tbl', $sql, $cols);
203 if (is_company_currency(get_post('curr_abrev')))
206 display_note(_("The selected currency is the company currency."), 2);
207 display_note(_("The company currency is the base currency so exchange rates cannot be set for it."), 1);
213 $table->width = "40%";
214 if ($table->rec_count == 0)
215 $table->ready = false;
216 display_db_pager($table);