From 708de015bbe772de44952c537cc023a90a15d5e9 Mon Sep 17 00:00:00 2001 From: Lyon3 Team Date: Fri, 3 May 2013 11:42:21 +0200 Subject: [PATCH] Bug 9593 Prices not imported correctly from a staged file When there's a round price with no decimals after it, or when the symbol is after the digits, the price is not captured by regular expression and the variable is not initialized. --- C4/Biblio.pm | 56 +++++++++----------- admin/currency.pl | 7 ++- installer/data/mysql/updatedatabase.pl | 7 +++ .../prog/en/modules/admin/currency.tt | 7 ++- 4 files changed, 44 insertions(+), 33 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 7af8619..96b5cd1 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -1466,6 +1466,11 @@ sub GetMarcPrice { for my $subfield_value ($field->subfield($subfield)){ #check value $subfield_value = MungeMarcPrice( $subfield_value ); + ( $subfield_value ) = $subfield_value =~ m/(\d[\d\,\. ]*\d{0,2})/; + # remove comma,dot or space when used as separators from hundreds + $subfield_value =~s/[\,\. ](\d{3})/$1/; + # convert comma to dot to ensure correct display of decimals if existing + $subfield_value =~s/,/./; return $subfield_value if ($subfield_value); } } @@ -1479,39 +1484,30 @@ Return the best guess at what the actual price is from a price field. sub MungeMarcPrice { my ( $price ) = @_; - return unless ( $price =~ m/\d/ ); ## No digits means no price. - - ## Look for the currency symbol of the active currency, if it's there, - ## start the price string right after the symbol. This allows us to prefer - ## this native currency price over other currency prices, if possible. - my $active_currency = C4::Context->dbh->selectrow_hashref( 'SELECT * FROM currency WHERE active = 1', {} ); - my $symbol = quotemeta( $active_currency->{'symbol'} ); - if ( $price =~ m/$symbol/ ) { - my @parts = split(/$symbol/, $price ); - $price = $parts[1]; - } - - ## Grab the first number in the string ( can use commas or periods for thousands separator and/or decimal separator ) - ( $price ) = $price =~ m/([\d\,\.]+[[\,\.]\d\d]?)/; - - ## Split price into array on periods and commas - my @parts = split(/[\,\.]/, $price); - - ## If the last grouping of digits is more than 2 characters, assume there is no decimal value and put it back. - my $decimal = pop( @parts ); - if ( length( $decimal ) > 2 ) { - push( @parts, $decimal ); - $decimal = ''; + # Look for the currency symbol and the normalized code of the active currency, if it's there, + my $active_currency = C4::Budgets->GetCurrency(); + my $symbol = $active_currency->{'symbol'}; + my $isocode = $active_currency->{'isocode'}; + my $localprice; + if ( $isocode and $price=~/[A-Z]{3}/ ) { + $price =~ s/\p{Currency_Symbol}//g; # in case price look like '$800 LRD, $10 USD'. + # find if the currency code precedes or follows the numeric part of price + if ( substr($price,0,1) =~/\s*[0-9]/ ) { + ($localprice)=$price =~ /([\p{P}\d\s]+$isocode)/; + } else { + ($localprice)=$price =~ /($isocode[\p{P}\d\s]+)/; + } } - - $price = join('', @parts ); - - if ( $decimal ) { - $price .= ".$decimal"; + if ( !$localprice and $symbol ) { + if ( substr($price,0,1) =~/\s*[0-9]/ ) { + ($localprice)=$price =~ /([\p{P}\d\s]+\Q$symbol\E)/; + } else { + ($localprice)=$price =~ /(\Q$symbol\E[\p{P}\d\s]+)/; + } } - - return $price; + $localprice||= $price; + return $localprice; } diff --git a/admin/currency.pl b/admin/currency.pl index ea22431..abbdca2 100755 --- a/admin/currency.pl +++ b/admin/currency.pl @@ -185,6 +185,7 @@ sub add_validate { my $rec = { rate => $input->param('rate'), symbol => $input->param('symbol') || q{}, + isocode => $input->param('isocode') || q{}, active => $input->param('active') || 0, currency => $input->param('currency'), }; @@ -198,20 +199,22 @@ sub add_validate { {}, $input->param('currency') ); if ($row_count) { $dbh->do( -q|UPDATE currency SET rate = ?, symbol = ?, active = ? WHERE currency = ? |, +q|UPDATE currency SET rate = ?, symbol = ?, isocode = ?, active = ? WHERE currency = ? |, {}, $rec->{rate}, $rec->{symbol}, + $rec->{isocode}, $rec->{active}, $rec->{currency} ); } else { $dbh->do( -q|INSERT INTO currency (currency, rate, symbol, active) VALUES (?,?,?,?) |, +q|INSERT INTO currency (currency, rate, symbol, isocode, active) VALUES (?,?,?,?,?) |, {}, $rec->{currency}, $rec->{rate}, $rec->{symbol}, + $rec->{isocode}, $rec->{active} ); diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index be4dfa2..d286df0 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -6784,6 +6784,13 @@ if ( CheckVersion($DBversion) ) { SetVersion ($DBversion); } +$DBversion = "3.11.00.XXX"; +if ( CheckVersion($DBversion) ) { + $dbh->do("ALTER TABLE currency ADD isocode VARCHAR(5) default NULL AFTER symbol;"); + print "Upgrade to $DBversion done (Added isocode to the currency table)\n"; + SetVersion($DBversion); +} + =head1 FUNCTIONS diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/currency.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/currency.tt index 99f693b..dae3838 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/currency.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/currency.tt @@ -107,7 +107,10 @@ - +
  • + + +
  • Last updated: [% timestamp %]
  • @@ -187,6 +190,7 @@ Currency Rate Symbol + Code iso Last updated Active Actions  @@ -200,6 +204,7 @@ [% loo.currency %] [% loo.rate %] [% loo.symbol |html %] + [% loo.isocode |html %] [% loo.timestamp %] [% IF ( loo.active ) %]✓[% END %] Edit -- 1.7.2.5