[0004216] Print Work Orders: database error fixed when voided WO is in selected range.
[fa-stable.git] / gl / manage / exchange_rates.php
index 5381831a52025e5c70eeb655f21fc37decaee129..6d13b06bf90f3bd77b712eed49eaff45115ea39f 100644 (file)
@@ -1,7 +1,16 @@
 <?php
-
-$page_security = 9;
-$path_to_root="../..";
+/**********************************************************************
+    Copyright (C) FrontAccounting, LLC.
+       Released under the terms of the GNU General Public License, GPL, 
+       as published by the Free Software Foundation, either version 3 
+       of the License, or (at your option) any later version.
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
+    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
+***********************************************************************/
+$page_security = 'SA_EXCHANGERATE';
+$path_to_root = "../..";
 include($path_to_root . "/includes/db_pager.inc");
 include_once($path_to_root . "/includes/session.inc");
 
@@ -10,14 +19,14 @@ include_once($path_to_root . "/includes/ui.inc");
 include_once($path_to_root . "/includes/banking.inc");
 
 $js = "";
-if ($use_date_picker)
+if (user_use_date_picker())
        $js .= get_js_date_picker();
-page(_("Exchange Rates"), false, false, "", $js);
+page(_($help_context = "Exchange Rates"), false, false, "", $js);
 
 simple_page_mode(false);
 
 //---------------------------------------------------------------------------------------------
-function check_data()
+function check_data($selected_id)
 {
        if (!is_date($_POST['date_']))
        {
@@ -25,19 +34,18 @@ function check_data()
                set_focus('date_');
                return false;
        }
-       if (!check_num('BuyRate', 0))
+       if (input_num('BuyRate') <= 0)
        {
-               display_error( _("The exchange rate must be numeric and greater than zero."));
+               display_error( _("The exchange rate cannot be zero or a negative number."));
                set_focus('BuyRate');
                return false;
        }
-       if ($_POST['BuyRate'] <= 0)
+       if (!$selected_id && get_date_exchange_rate($_POST['curr_abrev'], $_POST['date_']))
        {
-               display_error( _("The exchange rate cannot be zero or a negative number."));
-               set_focus('BuyRate');
+               display_error( _("The exchange rate for the date is already there."));
+               set_focus('date_');
                return false;
        }
-
        return true;
 }
 
@@ -47,7 +55,7 @@ function handle_submit()
 {
        global $selected_id;
 
-       if (!check_data())
+       if (!check_data($selected_id))
                return false;
 
        if ($selected_id != "")
@@ -64,6 +72,7 @@ function handle_submit()
        }
 
        $selected_id = '';
+       clear_data();
 }
 
 //---------------------------------------------------------------------------------------------
@@ -76,6 +85,7 @@ function handle_delete()
                return;
        delete_exchange_rate($selected_id);
        $selected_id = '';
+       clear_data();
 }
 
 //---------------------------------------------------------------------------------------------
@@ -91,35 +101,17 @@ function del_link($row)
 
 function display_rates($curr_code)
 {
-       global $table_style;
-
-       $sql = "SELECT date_, rate_buy, id FROM "
-               .TB_PREF."exchange_rates "
-               ."WHERE curr_code='$curr_code'
-                ORDER BY date_ DESC";
-
-       $cols = array(
-               _("Date to Use From") => 'date', 
-               _("Exchange Rate") => 'rate',
-               array('insert'=>true, 'fun'=>'edit_link'),
-               array('insert'=>true, 'fun'=>'del_link'),
-       );
-       $table =& new_db_pager('orders_tbl', $sql, $cols);
-       if (list_updated('curr_abrev')) {
-               $table->set_sql($sql);
-               $table->set_columns($cols);
-       }
-       $table->width = "40%";
-       display_db_pager($table);
+
 }
 
 //---------------------------------------------------------------------------------------------
 
 function display_rate_edit()
 {
-       global $selected_id, $table_style2, $Ajax;
-
-       start_table($table_style2);
+       global $selected_id, $Ajax, $SysPrefs;
+       $xchg_rate_provider = ((isset($SysPrefs->xr_providers) && isset($SysPrefs->dflt_xr_provider))
+               ? $SysPrefs->xr_providers[$SysPrefs->dflt_xr_provider] : 'ECB');
+       start_table(TABLESTYLE2);
 
        if ($selected_id != "")
        {
@@ -128,7 +120,7 @@ function display_rate_edit()
                $myrow = get_exchange_rate($selected_id);
 
                $_POST['date_'] = sql2date($myrow["date_"]);
-               $_POST['BuyRate'] = exrate_format($myrow["rate_buy"]);
+               $_POST['BuyRate'] = maxprec_format($myrow["rate_buy"]);
 
                hidden('selected_id', $selected_id);
                hidden('date_', $_POST['date_']);
@@ -143,19 +135,16 @@ function display_rate_edit()
        }
        if (isset($_POST['get_rate']))
        {
-               $_POST['BuyRate'] = exrate_format(get_ecb_rate($_POST['curr_abrev']));
-               if ($_POST['BuyRate'] == 0) {
-                       display_error(_('This currency is not listed by ECB. Enter exchange rate manually.'));
-               }
+               $_POST['BuyRate'] = 
+                       maxprec_format(retrieve_exrate($_POST['curr_abrev'], $_POST['date_']));
                $Ajax->activate('BuyRate');
        }
-       small_amount_row(_("Exchange Rate:"), 'BuyRate', null, '',
-               submit('get_rate',_("Get"), false, _('Get current ECB rate') , true),
-               user_exrate_dec());
+       amount_row(_("Exchange Rate:"), 'BuyRate', null, '',
+               submit('get_rate',_("Get"), false, _('Get current rate from') . ' ' . $xchg_rate_provider , true), 'max');
 
        end_table(1);
 
-       submit_add_or_update_center($selected_id == '', '', true);
+       submit_add_or_update_center($selected_id == '', '', 'both');
 
        display_note(_("Exchange rates are entered against the company currency."), 1);
 }
@@ -182,14 +171,14 @@ if ($Mode == 'Delete')
 
 //---------------------------------------------------------------------------------------------
 
-start_form(false, true);
+start_form();
 
 if (!isset($_POST['curr_abrev']))
        $_POST['curr_abrev'] = get_global_curr_code();
 
 echo "<center>";
 echo _("Select a currency :") . "  ";
-currencies_list('curr_abrev', null, true);
+echo currencies_list('curr_abrev', null, true, true);
 echo "</center>";
 
 // if currency sel has changed, clear the form
@@ -199,9 +188,19 @@ if ($_POST['curr_abrev'] != get_global_curr_code())
        $selected_id = "";
 }
 
-set_global_curr_code($_POST['curr_abrev']);
+set_global_curr_code(get_post('curr_abrev'));
 
-if (is_company_currency($_POST['curr_abrev']))
+$sql = get_sql_for_exchange_rates(get_post('curr_abrev'));
+
+$cols = array(
+       _("Date to Use From") => 'date', 
+       _("Exchange Rate") => 'rate',
+       array('insert'=>true, 'fun'=>'edit_link'),
+       array('insert'=>true, 'fun'=>'del_link'),
+);
+$table =& new_db_pager('orders_tbl', $sql, $cols);
+
+if (is_company_currency(get_post('curr_abrev')))
 {
 
        display_note(_("The selected currency is the company currency."), 2);
@@ -211,7 +210,10 @@ else
 {
 
        br(1);
-    display_rates($_POST['curr_abrev']);
+       $table->width = "40%";
+       if ($table->rec_count == 0)
+               $table->ready = false;
+       display_db_pager($table);
        br(1);
     display_rate_edit();
 }
@@ -220,4 +222,3 @@ end_form();
 
 end_page();
 
-?>