Lines 1477-1514
Return the best guess at what the actual price is from a price field.
Link Here
|
1477 |
|
1477 |
|
1478 |
sub MungeMarcPrice { |
1478 |
sub MungeMarcPrice { |
1479 |
my ( $price ) = @_; |
1479 |
my ( $price ) = @_; |
1480 |
|
|
|
1481 |
return unless ( $price =~ m/\d/ ); ## No digits means no price. |
1480 |
return unless ( $price =~ m/\d/ ); ## No digits means no price. |
1482 |
|
1481 |
# Look for the currency symbol and the normalized code of the active currency, if it's there, |
1483 |
## Look for the currency symbol of the active currency, if it's there, |
1482 |
my $active_currency = C4::Budgets->GetCurrency(); |
1484 |
## start the price string right after the symbol. This allows us to prefer |
1483 |
my $symbol = $active_currency->{'symbol'}; |
1485 |
## this native currency price over other currency prices, if possible. |
1484 |
my $isocode = $active_currency->{'isocode'}; |
1486 |
my $active_currency = C4::Context->dbh->selectrow_hashref( 'SELECT * FROM currency WHERE active = 1', {} ); |
1485 |
my $localprice; |
1487 |
my $symbol = quotemeta( $active_currency->{'symbol'} ); |
1486 |
if ( $symbol ) { |
1488 |
if ( $price =~ m/$symbol/ ) { |
1487 |
my @matches =($price=~ / |
1489 |
my @parts = split(/$symbol/, $price ); |
1488 |
\s? |
1490 |
$price = $parts[1]; |
1489 |
( # start of capturing parenthesis |
1491 |
} |
1490 |
(?: |
1492 |
|
1491 |
(?:\p{Sc}|[a-zA-Z]){1,3} # currency sign (unicode propriety) or alphabetical character whitin 1 to 3 occurrences : call this whole block 'symbol block' |
1493 |
## Grab the first number in the string ( can use commas or periods for thousands separator and/or decimal separator ) |
1492 |
|(?:\d+[\p{P}\s]?){1,4} # or else at least one digit followed or not by a punctuation sign or whitespace, all theese within 1 to 4 occurrences : call this whole block 'digits block' |
1494 |
( $price ) = $price =~ m/([\d\,\.]+[[\,\.]\d\d]?)/; |
1493 |
) |
1495 |
|
1494 |
\s?\p{Sc}?\s? # followed or not by a whitespace. \p{Sc}?\s? are for cases like '25$ USD' |
1496 |
## Split price into array on periods and commas |
1495 |
(?: |
1497 |
my @parts = split(/[\,\.]/, $price); |
1496 |
(?:\p{Sc}|[a-zA-Z]){1,3} # followed by same block as symbol block |
1498 |
|
1497 |
|(?:\d+[\p{P}\s]?){1,4} # or by same block as digits block |
1499 |
## If the last grouping of digits is more than 2 characters, assume there is no decimal value and put it back. |
1498 |
) |
1500 |
my $decimal = pop( @parts ); |
1499 |
\s? # followed or not by a whitespace |
1501 |
if ( length( $decimal ) > 2 ) { |
1500 |
) # end of capturing parenthesis |
1502 |
push( @parts, $decimal ); |
1501 |
(?:\p{P}|\z) # followed by a punctuation sign or by the end of the string |
1503 |
$decimal = ''; |
1502 |
/gx); |
1504 |
} |
1503 |
|
1505 |
|
1504 |
if ( @matches ) { |
1506 |
$price = join('', @parts ); |
1505 |
foreach ( @matches ) { |
1507 |
|
1506 |
$localprice = $_ and last if index($_, $isocode)>=0; |
1508 |
if ( $decimal ) { |
1507 |
} |
1509 |
$price .= ".$decimal"; |
1508 |
if ( !$localprice ) { |
|
|
1509 |
foreach ( @matches ) { |
1510 |
$localprice = $_ and last if $_=~ /(^|[^A-Za-z\p{Sc}])\Q$symbol\E([^A-Za-z\p{Sc}]|\z)/; |
1511 |
} |
1512 |
} |
1513 |
} |
1514 |
$price=$localprice; |
1515 |
if ($price) { |
1516 |
# eliminate symbol/isocode from the string |
1517 |
( $price ) = $price =~ m/(\d[\d\,\. ]*\d{0,2})/; |
1518 |
# remove comma,dot or space when used as separators from hundreds |
1519 |
$price =~s/[\,\. ](\d{3})/$1/g; |
1520 |
# convert comma to dot to ensure correct display of decimals if existing |
1521 |
$price =~s/,/./; |
1522 |
} |
1510 |
} |
1523 |
} |
1511 |
|
|
|
1512 |
return $price; |
1524 |
return $price; |
1513 |
} |
1525 |
} |
1514 |
|
1526 |
|