From 86fc12ebfd3489747ac3b59a63949c843a542734 Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Wed, 11 Dec 2013 10:45:59 -0500
Subject: [PATCH] Bug 6427 [Part 3] - Add new TT plugin to deal with currency

---
 Koha/Template/Plugin/Currency.pm |   91 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 91 insertions(+), 0 deletions(-)
 create mode 100644 Koha/Template/Plugin/Currency.pm

diff --git a/Koha/Template/Plugin/Currency.pm b/Koha/Template/Plugin/Currency.pm
new file mode 100644
index 0000000..de22dfe
--- /dev/null
+++ b/Koha/Template/Plugin/Currency.pm
@@ -0,0 +1,91 @@
+package Koha::Template::Plugin::Currency;
+
+# Copyright ByWater Solutions 2013
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha 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 GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use base qw( Template::Plugin::Filter );
+
+use Locale::Currency::Format;
+
+use C4::Context;
+use Koha::DateUtils;
+
+sub init {
+    my $self = shift;
+    $self->{ _DYNAMIC } = 1;
+
+    my $active_currency = C4::Context->dbh->selectrow_hashref(
+        'SELECT * FROM currency WHERE active = 1', {} );
+    $self->{active_currency} = $active_currency;
+
+    return $self;
+}
+
+sub filter {
+    my ( $self, $amount, $args, $conf ) = @_;
+
+    return $self->format( $amount, undef, $conf->{highlight} );
+}
+
+sub format {
+    my ( $self, $amount, $format, $highlight ) = @_;
+
+    $amount ||= 0;
+    my $is_negative = $amount < 0;
+    $amount = abs( $amount ) if $highlight;
+
+    # A negative debit is a credit and visa versa
+    if ($highlight) {
+        if ( $highlight eq 'debit' ) {
+            if ($is_negative) {
+                $highlight = 'credit';
+            }
+        }
+        elsif ( $highlight eq 'credit' ) {
+            if ($is_negative) {
+                $highlight = 'debit';
+            }
+
+        }
+        elsif ( $highlight eq 'offset' ) {
+            $highlight = $is_negative ? 'credit' : 'debit';
+        }
+    }
+
+    my $formatted = currency_format( $self->{active_currency}->{currency},
+        $amount, $format || FMT_HTML );
+
+    $formatted = "<span class='$highlight'>$formatted</span>" if ( $highlight && $amount );
+
+    return $formatted;
+}
+
+sub format_without_symbol {
+    my ( $self, $amount ) = @_;
+
+    return substr( $self->format( $amount, FMT_SYMBOL ), 1 );
+}
+
+sub symbol {
+    my ($self) = @_;
+
+    return currency_symbol( $self->{active_currency}->{'currency'}, SYM_HTML );
+}
+
+1;
-- 
1.7.2.5