From 4879b9fa9c601ec7c84979f8270cfb396ae963cb Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Wed, 1 Apr 2020 16:01:37 +0000 Subject: [PATCH] Bug 25037: Add Koha::Checkouts checkout_type constants and TT plugin Also make it available for templates. To test: 1. prove t/db_dependent/Koha/Checkouts.t 2. prove t/db_dependent/Template/Plugin/Checkouts.t Sponsored-by: The National Library of Finland --- Koha/Checkout.pm | 13 +++++ Koha/Checkouts.pm | 13 +++++ Koha/Template/Plugin/Checkouts.pm | 61 ++++++++++++++++++++++ t/db_dependent/Koha/Checkouts.t | 21 +++++++- t/db_dependent/Template/Plugin/Checkouts.t | 31 +++++++++++ 5 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 Koha/Template/Plugin/Checkouts.pm create mode 100644 t/db_dependent/Template/Plugin/Checkouts.t diff --git a/Koha/Checkout.pm b/Koha/Checkout.pm index e256b6c051..39eb1e119d 100644 --- a/Koha/Checkout.pm +++ b/Koha/Checkout.pm @@ -41,6 +41,19 @@ Koha::Checkout - Koha Checkout object class =cut +=head3 is_onsite_checkout + +my is_onsite_checkout = $checkout->is_onsite_checkout(); + +Return 1 if the checkout is an on-site checkout. + +=cut + +sub is_onsite_checkout { + my ( $self ) = @_; + return $self->checkout_type eq $Koha::Checkouts::type->{onsite_checkout}; +} + =head3 is_overdue my $is_overdue = $checkout->is_overdue( [ $reference_dt ] ); diff --git a/Koha/Checkouts.pm b/Koha/Checkouts.pm index ccfeb36e10..0bad230b9e 100644 --- a/Koha/Checkouts.pm +++ b/Koha/Checkouts.pm @@ -54,6 +54,19 @@ sub calculate_dropbox_date { return $dropbox_date; } +=cut + +=head2 Name to code mappings + +=head3 $checkout_type + +=cut + +our $type = { + 'checkout' => 'C', + 'onsite_checkout' => 'OS', +}; + =head3 type =cut diff --git a/Koha/Template/Plugin/Checkouts.pm b/Koha/Template/Plugin/Checkouts.pm new file mode 100644 index 0000000000..755663e82a --- /dev/null +++ b/Koha/Template/Plugin/Checkouts.pm @@ -0,0 +1,61 @@ +package Koha::Template::Plugin::Checkouts; + +# Copyright 2020 Hypernova Oy +# +# 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, see . + +=head1 NAME + +Koha::Template::Plugin::Checkouts + +=head1 DESCRIPTION + +The Checkouts plugin is a helper for using Koha::Checkouts in templates. + +=head1 SYNOPSYS + + [% USE Checkouts %] + +=cut + +use Modern::Perl; + +use Template::Plugin; +use base qw( Template::Plugin ); + +use C4::Koha; +use C4::Context; +use Koha::Checkouts; + +=head1 FUNCTIONS + +=head2 checkout_type + +Returns $Koha::Checkouts::type HASHref. + +Usage: + + [% IF (checkout_type == Checkouts.type.onsite_checkout %] + ... + [% END %] + +=cut + +sub type { + return $Koha::Checkouts::type; +} + +1; diff --git a/t/db_dependent/Koha/Checkouts.t b/t/db_dependent/Koha/Checkouts.t index dfb90e42a8..e584dae1e6 100644 --- a/t/db_dependent/Koha/Checkouts.t +++ b/t/db_dependent/Koha/Checkouts.t @@ -19,7 +19,8 @@ use Modern::Perl; -use Test::More tests => 7; +use Test::More tests => 9; +use Test::Exception; use C4::Circulation; use Koha::Checkouts; @@ -56,6 +57,24 @@ is( Koha::Checkouts->search->count, $nb_of_checkouts + 2, 'The 2 checkouts shoul my $retrieved_checkout_1 = Koha::Checkouts->find( $new_checkout_1->issue_id ); is( $retrieved_checkout_1->itemnumber, $new_checkout_1->itemnumber, 'Find a checkout by id should return the correct checkout' ); +subtest 'Koha::Checkouts checkout_codes' => sub { + plan tests => 2; + is( $Koha::Checkouts::type->{checkout}, 'C' ); + is( $Koha::Checkouts::type->{onsite_checkout}, 'OS' ); +}; + +subtest 'is_onsite_checkout' => sub { + plan tests => 2; + + my $old_checkout_type = $retrieved_checkout_1->checkout_type; + $new_checkout_1->checkout_type($Koha::Checkouts::type->{checkout})->store; + ok( !$new_checkout_1->is_onsite_checkout, 'It is not on-site checkout' ); + $new_checkout_1->checkout_type($Koha::Checkouts::type->{onsite_checkout})->store; + is( $new_checkout_1->is_onsite_checkout, + 1, 'It is an on-site checkout' ); + $new_checkout_1->checkout_type($old_checkout_type)->store; +}; + subtest 'is_overdue' => sub { plan tests => 6; my $ten_days_ago = dt_from_string->add( days => -10 ); diff --git a/t/db_dependent/Template/Plugin/Checkouts.t b/t/db_dependent/Template/Plugin/Checkouts.t new file mode 100644 index 0000000000..890b856da2 --- /dev/null +++ b/t/db_dependent/Template/Plugin/Checkouts.t @@ -0,0 +1,31 @@ +#!/usr/bin/perl + +# 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, see . + +use Modern::Perl; + +use Test::More tests => 1; +use t::lib::Mocks; +use t::lib::TestBuilder; + +use Koha::Template::Plugin::Checkouts; + +subtest 'type() tests' => sub { + plan tests => 2; + + is( Koha::Template::Plugin::Checkouts->new->type->{checkout}, 'C' ); + is( Koha::Template::Plugin::Checkouts->new->type->{onsite_checkout}, 'OS' ); +}; -- 2.17.1