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 <mikasmith@catalyst.net.nz>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
This commit is contained in:
Jonathan Druart 2016-11-25 14:16:26 +01:00 committed by Kyle M Hall
parent 19ee95a6d5
commit 7ecd9e3304
2 changed files with 59 additions and 2 deletions

View File

@ -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 <kyle@bywatersolutions.com>
Jonathan Druart <jonathan.druart@bugs.koha-community.org>
=cut
1;

View File

@ -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' );