From 7e4e4c06e67c455c9b429d6b619ec502d17a753a Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 4 Jul 2016 17:56:54 +0100 Subject: [PATCH] Bug 16851: Move HasOverdues to Koha::Patron->has_overdues This patch just move C4::Members::HasOverdues to Koha::Patron->has_overdues and updated callers Test plan: No change in behavior is expected. 1/ If a patron is debarred and does not have overdues and AutoRemoveOverduesRestrictions is on, the debarment will be removed on checkin 2/ Add overdues and make sure the patron cannot renew Signed-off-by: Owen Leonard Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall --- C4/Circulation.pm | 9 ++++---- C4/Members.pm | 16 --------------- Koha/Patron.pm | 14 +++++++++++++ Koha/Template/Plugin/Borrowers.pm | 2 +- t/db_dependent/Koha/Patrons.t | 34 +++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 21 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 06dd764085..19f9a9a1a1 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2091,7 +2091,7 @@ sub AddReturn { if ( $borrowernumber && $borrower->{'debarred'} && C4::Context->preference('AutoRemoveOverduesRestrictions') - && !C4::Members::HasOverdues( $borrowernumber ) + && !Koha::Patrons->find( $borrowernumber )->has_overdues && @{ GetDebarments({ borrowernumber => $borrowernumber, type => 'OVERDUES' }) } ) { DelUniqueDebarment({ borrowernumber => $borrowernumber, type => 'OVERDUES' }); @@ -2824,8 +2824,9 @@ sub CanBookBeRenewed { my $overduesblockrenewing = C4::Context->preference('OverduesBlockRenewing'); my $restrictionblockrenewing = C4::Context->preference('RestrictionBlockRenewing'); - my $restricted = Koha::Patrons->find( $borrowernumber )->is_debarred; - my $hasoverdues = C4::Members::HasOverdues($borrowernumber); + my $patron = Koha::Patrons->find($borrowernumber); + my $restricted = $patron->is_debarred; + my $hasoverdues = $patron->has_overdues; if ( $restricted and $restrictionblockrenewing ) { return ( 0, 'restriction'); @@ -2998,7 +2999,7 @@ sub AddRenewal { $borrower = C4::Members::GetMember( borrowernumber => $borrowernumber ); if ( $borrowernumber && $borrower->{'debarred'} - && !C4::Members::HasOverdues( $borrowernumber ) + && !Koha::Patrons->find( $borrowernumber )->has_overdues && @{ GetDebarments({ borrowernumber => $borrowernumber, type => 'OVERDUES' }) } ) { DelUniqueDebarment({ borrowernumber => $borrowernumber, type => 'OVERDUES' }); diff --git a/C4/Members.pm b/C4/Members.pm index 03f0b209e9..63fe0c4c3f 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -90,7 +90,6 @@ BEGIN { &IssueSlip GetBorrowersWithEmail - HasOverdues GetOverduesForPatron ); @@ -1865,21 +1864,6 @@ sub AddEnrolmentFeeIfNeeded { } } -=head2 HasOverdues - -=cut - -sub HasOverdues { - my ( $borrowernumber ) = @_; - - my $sql = "SELECT COUNT(*) FROM issues WHERE date_due < NOW() AND borrowernumber = ?"; - my $sth = C4::Context->dbh->prepare( $sql ); - $sth->execute( $borrowernumber ); - my ( $count ) = $sth->fetchrow_array(); - - return $count; -} - =head2 DeleteExpiredOpacRegistrations Delete accounts that haven't been upgraded from the 'temporary' category diff --git a/Koha/Patron.pm b/Koha/Patron.pm index fc5496fb5a..29ab0d988d 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -235,6 +235,20 @@ sub renew_account { return dt_from_string( $expiry_date )->truncate( to => 'day' ); } +=head2 has_overdues + +my $has_overdues = $patron->has_overdues; + +Returns the number of patron's overdues + +=cut + +sub has_overdues { + my ($self) = @_; + my $dtf = Koha::Database->new->schema->storage->datetime_parser; + return $self->_result->issues->search({ date_due => { '<' => $dtf->format_datetime( dt_from_string() ) } })->count; +} + =head3 type =cut diff --git a/Koha/Template/Plugin/Borrowers.pm b/Koha/Template/Plugin/Borrowers.pm index dd99b9b2ca..cb50312d55 100644 --- a/Koha/Template/Plugin/Borrowers.pm +++ b/Koha/Template/Plugin/Borrowers.pm @@ -54,7 +54,7 @@ sub HasOverdues { return unless $borrowernumber; - return C4::Members::HasOverdues($borrowernumber); + return Koha::Patrons->find( $borrowernumber )->has_overdues; } diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index 0f71655e21..345f530584 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -22,6 +22,7 @@ use Modern::Perl; use Test::More tests => 7; use Test::Warn; +use C4::Circulation; use Koha::Patron; use Koha::Patrons; use Koha::Database; @@ -57,6 +58,9 @@ my $new_patron_2 = Koha::Patron->new( } )->store; +C4::Context->_new_userenv('xxx'); +C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', ''); + is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' ); my $retrieved_patron_1 = Koha::Patrons->find( $new_patron_1->borrowernumber ); @@ -104,6 +108,36 @@ subtest 'siblings' => sub { $retrieved_guarantee_1->delete; }; +subtest 'has_overdues' => sub { + plan tests => 3; + + my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } ); + my $item_1 = $builder->build( + { source => 'Item', + value => { + homebranch => $library->{branchcode}, + holdingbranch => $library->{branchcode}, + notforloan => 0, + itemlost => 0, + withdrawn => 0, + biblionumber => $biblioitem_1->{biblionumber} + } + } + ); + my $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber ); + is( $retrieved_patron->has_overdues, 0, ); + + my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 ); + my $issue = AddIssue( $new_patron_1->unblessed, $item_1->{barcode} ); + is( $retrieved_patron->has_overdues, 0, ); + AddReturn( $item_1->{barcode} ); + my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 ); + $issue = AddIssue( $new_patron_1->unblessed, $item_1->{barcode}, $yesterday ); + $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber ); + is( $retrieved_patron->has_overdues, 1, ); + AddReturn( $item_1->{barcode} ); +}; + subtest 'update_password' => sub { plan tests => 7; -- 2.39.5