From 7ecd9e330481389d523da27f962e8e5242c1bbfa Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 25 Nov 2016 14:16:26 +0100 Subject: [PATCH] Bug 17689: Add the Koha::Checkout->is_overdue method This patch adds a new method Koha::Checkout->is_overdue and provide tests to cover it. The goal is to behave like GetItemIssues set the 'overdue' flag to issues. I don't understand why the existing GetItemIssues truncate dates to minutes, so I did not recreate this behavior. Test plan: prove t/db_dependent/Koha/Checkouts.t should return green Signed-off-by: Mika Smith Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall --- Koha/Checkout.pm | 26 ++++++++++++++++++++++++++ t/db_dependent/Koha/Checkouts.t | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/Koha/Checkout.pm b/Koha/Checkout.pm index 358f41c356..a1a67d66bb 100644 --- a/Koha/Checkout.pm +++ b/Koha/Checkout.pm @@ -1,6 +1,7 @@ package Koha::Checkout; # Copyright ByWater Solutions 2015 +# Copyright 2016 Koha Development Team # # This file is part of Koha. # @@ -22,6 +23,8 @@ use Modern::Perl; use Carp; use Koha::Database; +use DateTime; +use Koha::DateUtils; use base qw(Koha::Object); @@ -35,6 +38,27 @@ Koha::Checkout - Koha Checkout object class =cut +=head3 is_overdue + +my $is_overdue = $checkout->is_overdue( [ $reference_dt ] ); + +Return 1 if the checkout is overdue. + +A reference date can be passed, in this case it will be used, otherwise today +will be the reference date. + +=cut + +sub is_overdue { + my ( $self, $dt ) = @_; + $dt ||= DateTime->now( time_zone => C4::Context->tz ); + my $is_overdue = + DateTime->compare( dt_from_string( $self->date_due, 'sql' ), $dt ) == -1 + ? 1 + : 0; + return $is_overdue; +} + =head3 type =cut @@ -47,6 +71,8 @@ sub _type { Kyle M Hall +Jonathan Druart + =cut 1; diff --git a/t/db_dependent/Koha/Checkouts.t b/t/db_dependent/Koha/Checkouts.t index e8b1341a36..ba85bb2e1a 100644 --- a/t/db_dependent/Koha/Checkouts.t +++ b/t/db_dependent/Koha/Checkouts.t @@ -19,11 +19,11 @@ use Modern::Perl; -use Test::More tests => 4; +use Test::More tests => 5; -use Koha::Checkout; use Koha::Checkouts; use Koha::Database; +use Koha::DateUtils qw( dt_from_string ); use t::lib::TestBuilder; @@ -55,6 +55,37 @@ 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 'is_overdue' => sub { + plan tests => 6; + my $ten_days_ago = dt_from_string->add( days => -10 ); + my $ten_days_later = dt_from_string->add( days => 10 ); + my $yesterday = dt_from_string->add( days => -1 ); + my $tomorrow = dt_from_string->add( days => 1 ); + + $retrieved_checkout_1->date_due($ten_days_ago)->store; + is( $retrieved_checkout_1->is_overdue, + 1, 'The item should have been returned 10 days ago' ); + + $retrieved_checkout_1->date_due($ten_days_later)->store; + is( $retrieved_checkout_1->is_overdue, 0, 'The item is due in 10 days' ); + + $retrieved_checkout_1->date_due($tomorrow)->store; + is( $retrieved_checkout_1->is_overdue($ten_days_later), + 1, 'The item should have been returned yesterday' ); + + $retrieved_checkout_1->date_due($yesterday)->store; + is( $retrieved_checkout_1->is_overdue($ten_days_ago), + 0, 'Ten days ago the item due yesterday was not late' ); + + $retrieved_checkout_1->date_due($tomorrow)->store; + is( $retrieved_checkout_1->is_overdue($ten_days_later), + 1, 'In Ten days, the item due tomorrow will be late' ); + + $retrieved_checkout_1->date_due($yesterday)->store; + is( $retrieved_checkout_1->is_overdue($ten_days_ago), + 0, 'In Ten days, the item due yesterday will still be late' ); +}; + $retrieved_checkout_1->delete; is( Koha::Checkouts->search->count, $nb_of_checkouts + 1, 'Delete should have deleted the checkout' ); -- 2.11.0