From 51f0d2d403986cad9b9cd33be314872d34aee887 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 --- C4/Circulation.pm | 9 +++++---- C4/Members.pm | 16 ---------------- Koha/Patron.pm | 14 ++++++++++++++ Koha/Template/Plugin/Borrowers.pm | 2 +- t/db_dependent/Koha/Patrons.t | 36 +++++++++++++++++++++++++++++++++++- 5 files changed, 55 insertions(+), 22 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 9b95bbf..2947c6e 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2243,7 +2243,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' }); @@ -2976,8 +2976,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'); @@ -3145,7 +3146,7 @@ sub AddRenewal { my $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 63e3f36..443fc41 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -94,7 +94,6 @@ BEGIN { &IssueSlip GetBorrowersWithEmail - HasOverdues GetOverduesForPatron ); @@ -2060,21 +2059,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 42b066b..886f4d3 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -208,6 +208,20 @@ sub update_password { return 1; } +=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 dd99b9b..cb50312 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 980bf84..f5b2e56 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -19,9 +19,10 @@ use Modern::Perl; -use Test::More tests => 6; +use Test::More tests => 7; use Test::Warn; +use C4::Circulation; use Koha::Patron; use Koha::Patrons; use Koha::Database; @@ -55,6 +56,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 ); @@ -102,6 +106,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.8.1