@@ -, +, @@ prove t/db_dependent/Koha/Checkouts.t --- Koha/Checkout.pm | 26 ++++++++++++++++++++++++++ t/db_dependent/Koha/Checkouts.t | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-) --- a/Koha/Checkout.pm +++ a/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; --- a/t/db_dependent/Koha/Checkouts.t +++ a/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' ); --