From 567e901b176ac1852e5b3caa099a40fc66002d02 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 90557ea..042af54 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2211,7 +2211,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' }); @@ -2944,8 +2944,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'); @@ -3113,7 +3114,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 ef600e7..f24b3c1 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -94,7 +94,6 @@ BEGIN { &IssueSlip GetBorrowersWithEmail - HasOverdues GetOverduesForPatron ); @@ -2211,21 +2210,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 740f4a6..237102a 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -115,6 +115,20 @@ sub is_debarred { return; } +=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 fafd829..4aed7fe 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -19,8 +19,9 @@ use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 6; +use C4::Circulation; use Koha::Patron; use Koha::Patrons; use Koha::Database; @@ -51,6 +52,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 ); @@ -98,6 +102,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} ); +}; + $retrieved_patron_1->delete; is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' ); -- 2.8.1