summaryrefslogtreecommitdiff
path: root/httemplate/edit/process/currency_exchange.html
blob: 1f68522992cd5be839ddd5fc686c74cf7da81745 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
%if ( $error ) {
%  errorpage($error); #also not super ideal
%} else { #or this
<% include('/elements/header.html', 'Exchange rates updated') %>
<% include('/elements/footer.html') %>
%}
<%init>

die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('Configuration');

my $conf = new FS::Conf;

my $to_currency = $conf->config('currency') || 'USD';

my @currencies = sort { $a cmp $b } $conf->config('currencies');

#in the best of all possible worlds, i would be a single database transaction
# but here it isn't terribly important other than offending my sense of elegance
my $error = '';
foreach my $currency (@currencies) {

  my %hash = ( 'from_currency' => $currency,
               'to_currency'   => $to_currency,
             );

  my $currency_exchange = qsearchs('currency_exchange', \%hash)
                         || new FS::currency_exchange   \%hash;

  $currency_exchange->rate( $cgi->param("$currency-$to_currency") );

  my $method = $currency_exchange->currencyratenum ? 'replace' : 'insert';
  $error = $currency_exchange->$method() and last;
}

</%init>