|
Line 0
Link Here
|
| 0 |
- |
1 |
package Koha::Template::Plugin::Currency; |
|
|
2 |
|
| 3 |
# Copyright ByWater Solutions 2013 |
| 4 |
|
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 8 |
# terms of the GNU General Public License as published by the Free Software |
| 9 |
# Foundation; either version 3 of the License, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along |
| 17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use base qw( Template::Plugin::Filter ); |
| 23 |
|
| 24 |
use Locale::Currency::Format; |
| 25 |
|
| 26 |
use C4::Context; |
| 27 |
use Koha::DateUtils; |
| 28 |
|
| 29 |
sub init { |
| 30 |
my $self = shift; |
| 31 |
$self->{ _DYNAMIC } = 1; |
| 32 |
|
| 33 |
my $active_currency = C4::Context->dbh->selectrow_hashref( |
| 34 |
'SELECT * FROM currency WHERE active = 1', {} ); |
| 35 |
$self->{active_currency} = $active_currency; |
| 36 |
|
| 37 |
return $self; |
| 38 |
} |
| 39 |
|
| 40 |
sub filter { |
| 41 |
my ( $self, $amount, $args, $conf ) = @_; |
| 42 |
|
| 43 |
return $self->format( $amount, undef, $conf->{highlight} ); |
| 44 |
} |
| 45 |
|
| 46 |
sub format { |
| 47 |
my ( $self, $amount, $format, $highlight ) = @_; |
| 48 |
|
| 49 |
$amount ||= 0; |
| 50 |
my $is_negative = $amount < 0; |
| 51 |
$amount = abs( $amount ) if $highlight; |
| 52 |
|
| 53 |
# A negative debit is a credit and visa versa |
| 54 |
if ($highlight) { |
| 55 |
if ( $highlight eq 'debit' ) { |
| 56 |
if ($is_negative) { |
| 57 |
$highlight = 'credit'; |
| 58 |
} |
| 59 |
} |
| 60 |
elsif ( $highlight eq 'credit' ) { |
| 61 |
if ($is_negative) { |
| 62 |
$highlight = 'debit'; |
| 63 |
} |
| 64 |
|
| 65 |
} |
| 66 |
elsif ( $highlight eq 'offset' ) { |
| 67 |
$highlight = $is_negative ? 'credit' : 'debit'; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
my $formatted = currency_format( $self->{active_currency}->{currency}, |
| 72 |
$amount, $format || FMT_HTML ); |
| 73 |
|
| 74 |
$formatted = "<span class='$highlight'>$formatted</span>" if ( $highlight && $amount ); |
| 75 |
|
| 76 |
return $formatted; |
| 77 |
} |
| 78 |
|
| 79 |
sub format_without_symbol { |
| 80 |
my ( $self, $amount ) = @_; |
| 81 |
|
| 82 |
return substr( |
| 83 |
$self->format( $amount, FMT_SYMBOL ), |
| 84 |
length( |
| 85 |
currency_symbol( $self->{active_currency}->{'currency'} ) |
| 86 |
) |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
sub symbol { |
| 91 |
my ($self) = @_; |
| 92 |
|
| 93 |
return currency_symbol( $self->{active_currency}->{'currency'}, SYM_HTML ); |
| 94 |
} |
| 95 |
|
| 96 |
1; |