From e69217913e4ef926fe45fcf3c14e4a70f7c9ed13 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Mon, 4 Jul 2016 11:29:16 +0100
Subject: [PATCH] Bug 16849: Move IsDebarred to Koha::Patron->is_debarred
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

In order to move IsMemberBlocked to Koha::Patron it makes sense to move
the code from Koha::Patron::Debarments::IsDebarred to
Koha::Patron->is_debarred.

Test plan:
1/ Add a restriction to a patron
2/ make sure he is not able to checkout items any more
3/ Make sure he cannot get a discharge
4/ Put a hold and make sure you get "Patron has restrictions"

Signed-off-by: Marc Véron <veron@veron.ch>
---
 C4/Circulation.pm                           |    7 ++++---
 C4/Members.pm                               |    4 ++--
 Koha/Patron.pm                              |   20 ++++++++++++++++++++
 Koha/Patron/Debarments.pm                   |   23 -----------------------
 Koha/Patron/Discharge.pm                    |    4 ++--
 Koha/Template/Plugin/Borrowers.pm           |    4 ++--
 circ/circulation.pl                         |    4 ++--
 members/moremember.pl                       |    4 ++--
 opac/opac-reserve.pl                        |    4 ++--
 opac/opac-user.pl                           |    3 +--
 reserve/request.pl                          |    4 ++--
 t/db_dependent/Patron/Borrower_Debarments.t |    7 ++++---
 t/db_dependent/Patron/Borrower_Discharge.t  |    4 ++--
 13 files changed, 45 insertions(+), 47 deletions(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index 2cb0930..90557ea 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -2380,8 +2380,9 @@ sub _debar_user_on_return {
                 type           => 'SUSPENSION',
             });
             # if borrower was already debarred but does not get an extra debarment
-            if ( $borrower->{debarred} eq Koha::Patron::Debarments::IsDebarred($borrower->{borrowernumber}) ) {
-                    return ($borrower->{debarred},1);
+            my $patron = Koha::Patrons->find( $borrower->{borrowernumber} );
+            if ( $borrower->{debarred} eq $patron->is_debarred ) {
+                return ($borrower->{debarred},1);
             }
             return $new_debar_dt->ymd();
         }
@@ -2943,7 +2944,7 @@ sub CanBookBeRenewed {
 
     my $overduesblockrenewing = C4::Context->preference('OverduesBlockRenewing');
     my $restrictionblockrenewing = C4::Context->preference('RestrictionBlockRenewing');
-    my $restricted = Koha::Patron::Debarments::IsDebarred($borrowernumber);
+    my $restricted = Koha::Patrons->find( $borrowernumber )->is_debarred;
     my $hasoverdues = C4::Members::HasOverdues($borrowernumber);
 
     if ( $restricted and $restrictionblockrenewing ) {
diff --git a/C4/Members.pm b/C4/Members.pm
index 383799d..ef600e7 100644
--- a/C4/Members.pm
+++ b/C4/Members.pm
@@ -37,11 +37,11 @@ use C4::NewsChannels; #get slip news
 use DateTime;
 use Koha::Database;
 use Koha::DateUtils;
-use Koha::Patron::Debarments qw(IsDebarred);
 use Text::Unaccent qw( unac_string );
 use Koha::AuthUtils qw(hash_password);
 use Koha::Database;
 use Koha::List::Patron;
+use Koha::Patrons;
 
 our (@ISA,@EXPORT,@EXPORT_OK,$debug);
 
@@ -491,7 +491,7 @@ sub IsMemberBlocked {
     my $borrowernumber = shift;
     my $dbh            = C4::Context->dbh;
 
-    my $blockeddate = Koha::Patron::Debarments::IsDebarred($borrowernumber);
+    my $blockeddate = Koha::Patrons->find( $borrowernumber )->is_debarred;
 
     return ( 1, $blockeddate ) if $blockeddate;
 
diff --git a/Koha/Patron.pm b/Koha/Patron.pm
index ac62594..740f4a6 100644
--- a/Koha/Patron.pm
+++ b/Koha/Patron.pm
@@ -22,6 +22,7 @@ use Modern::Perl;
 use Carp;
 
 use Koha::Database;
+use Koha::DateUtils;
 use Koha::Patrons;
 use Koha::Patron::Images;
 
@@ -95,6 +96,25 @@ sub siblings {
     );
 }
 
+=head2 is_debarred
+
+my $debarment_expiration = $patron->is_debarred;
+
+Returns the date a patron debarment will expire, or undef if the patron is not
+debarred
+
+=cut
+
+sub is_debarred {
+    my ($self) = @_;
+
+    return unless $self->debarred;
+    return $self->debarred
+      if $self->debarred =~ '^9999'
+      or dt_from_string( $self->debarred ) > dt_from_string;
+    return;
+}
+
 =head3 type
 
 =cut
diff --git a/Koha/Patron/Debarments.pm b/Koha/Patron/Debarments.pm
index c305ba9..9fc2724 100644
--- a/Koha/Patron/Debarments.pm
+++ b/Koha/Patron/Debarments.pm
@@ -33,7 +33,6 @@ our @EXPORT = qw(
   AddUniqueDebarment
   DelUniqueDebarment
 
-  IsDebarred
 );
 
 =head1 Koha::Patron::Debarments
@@ -169,28 +168,6 @@ sub ModDebarment {
     return $r;
 }
 
-=head2 IsDebarred
-
-my $debarment_expiration = IsDebarred( $borrowernumber );
-
-Returns the date a borrowers debarment will expire, or
-undef if the patron is not debarred
-
-=cut
-
-sub IsDebarred {
-    my ($borrowernumber) = @_;
-
-    return unless ($borrowernumber);
-
-    my $sql = "SELECT debarred FROM borrowers WHERE borrowernumber = ? AND debarred > CURRENT_DATE()";
-    my $sth = C4::Context->dbh->prepare($sql);
-    $sth->execute($borrowernumber);
-    my ($debarred) = $sth->fetchrow_array();
-
-    return $debarred;
-}
-
 =head2 AddUniqueDebarment
 
 my $success = AddUniqueDebarment({
diff --git a/Koha/Patron/Discharge.pm b/Koha/Patron/Discharge.pm
index 4b775dc..d519d40 100644
--- a/Koha/Patron/Discharge.pm
+++ b/Koha/Patron/Discharge.pm
@@ -11,6 +11,7 @@ use C4::Reserves qw( GetReservesFromBorrowernumber CancelReserve );
 
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string output_pref );
+use Koha::Patrons;
 
 my $rs = Koha::Database->new->schema->resultset('Discharge');
 
@@ -50,8 +51,7 @@ sub is_discharged {
     return unless $params->{borrowernumber};
     my $borrowernumber = $params->{borrowernumber};
 
-
-    my $restricted = Koha::Patron::Debarments::IsDebarred($borrowernumber);
+    my $restricted = Koha::Patrons->find( $borrowernumber )->is_debarred;
     my $validated = get_validated({borrowernumber => $borrowernumber});
 
     if ($restricted && $validated) {
diff --git a/Koha/Template/Plugin/Borrowers.pm b/Koha/Template/Plugin/Borrowers.pm
index d6b87d4..dd99b9b 100644
--- a/Koha/Template/Plugin/Borrowers.pm
+++ b/Koha/Template/Plugin/Borrowers.pm
@@ -23,7 +23,7 @@ use Modern::Perl;
 use base qw( Template::Plugin );
 
 use Koha::Patron::Debarments qw();
-use C4::Members qw();
+use Koha::Patrons;
 
 =pod
 
@@ -46,7 +46,7 @@ sub IsDebarred {
 
     return unless $borrower;
 
-    return Koha::Patron::Debarments::IsDebarred($borrower->{borrowernumber});
+    return Koha::Patrons->find( $borrower->{borrowernumber} )->is_debarred;
 }
 
 sub HasOverdues {
diff --git a/circ/circulation.pl b/circ/circulation.pl
index 24c8110..e177122 100755
--- a/circ/circulation.pl
+++ b/circ/circulation.pl
@@ -44,7 +44,7 @@ use C4::Context;
 use CGI::Session;
 use C4::Members::Attributes qw(GetBorrowerAttributes);
 use Koha::Patron;
-use Koha::Patron::Debarments qw(GetDebarments IsDebarred);
+use Koha::Patron::Debarments qw(GetDebarments);
 use Koha::DateUtils;
 use Koha::Database;
 use Koha::Patron::Messages;
@@ -297,7 +297,7 @@ if ($borrowernumber) {
         finetotal    => $fines
     );
 
-    if ( IsDebarred($borrowernumber) ) {
+    if ( Koha::Patrons->find( $borrowernumber )->is_debarred ) {
         $template->param(
             'userdebarred'    => $borrower->{debarred},
             'debarredcomment' => $borrower->{debarredcomment},
diff --git a/members/moremember.pl b/members/moremember.pl
index e7c54f4..98286a0 100755
--- a/members/moremember.pl
+++ b/members/moremember.pl
@@ -51,7 +51,7 @@ use C4::Branch; # GetBranchName
 use C4::Form::MessagingPreferences;
 use List::MoreUtils qw/uniq/;
 use C4::Members::Attributes qw(GetBorrowerAttributes);
-use Koha::Patron::Debarments qw(GetDebarments IsDebarred);
+use Koha::Patron::Debarments qw(GetDebarments);
 use Koha::Patron::Images;
 use Module::Load;
 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
@@ -144,7 +144,7 @@ for (qw(gonenoaddress lost borrowernotes)) {
 	 $data->{$_} and $template->param(flagged => 1) and last;
 }
 
-if ( IsDebarred($borrowernumber) ) {
+if ( Koha::Patrons->find( $borrowernumber )->is_debarred ) {
     $template->param( 'userdebarred' => 1, 'flagged' => 1 );
     my $debar = $data->{'debarred'};
     if ( $debar ne "9999-12-31" ) {
diff --git a/opac/opac-reserve.pl b/opac/opac-reserve.pl
index 3f77672..9e74784 100755
--- a/opac/opac-reserve.pl
+++ b/opac/opac-reserve.pl
@@ -35,7 +35,7 @@ use C4::Overdues;
 use C4::Debug;
 use Koha::DateUtils;
 use Koha::Libraries;
-use Koha::Patron::Debarments qw(IsDebarred);
+use Koha::Patrons;
 use Date::Calc qw/Today Date_to_Days/;
 use List::MoreUtils qw/uniq/;
 
@@ -333,7 +333,7 @@ if ( $borr->{lost} && ($borr->{lost} == 1) ) {
     );
 }
 
-if ( IsDebarred($borrowernumber) ) {
+if ( Koha::Patrons->find( $borrowernumber )->is_debarred ) {
     $noreserves = 1;
     $template->param(
         message          => 1,
diff --git a/opac/opac-user.pl b/opac/opac-user.pl
index 413c26d..c4e3b33 100755
--- a/opac/opac-user.pl
+++ b/opac/opac-user.pl
@@ -35,7 +35,6 @@ use C4::Items;
 use C4::Letters;
 use C4::Branch; # GetBranches
 use Koha::DateUtils;
-use Koha::Patron::Debarments qw(IsDebarred);
 use Koha::Holds;
 use Koha::Database;
 use Koha::Patron::Messages;
@@ -86,7 +85,7 @@ my ( $borr ) = GetMemberDetails( $borrowernumber );
 my (  $today_year,   $today_month,   $today_day) = Today();
 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
 
-my $debar = IsDebarred($borrowernumber);
+my $debar = Koha::Patrons->find( $borrowernumber )->is_debarred;
 my $userdebarred;
 
 if ($debar) {
diff --git a/reserve/request.pl b/reserve/request.pl
index 1826e00..8e1f458 100755
--- a/reserve/request.pl
+++ b/reserve/request.pl
@@ -44,7 +44,6 @@ use C4::Utils::DataTables::Members;
 use C4::Members;
 use C4::Search;		# enabled_staff_search_views
 use Koha::DateUtils;
-use Koha::Patron::Debarments qw(IsDebarred);
 use Koha::Holds;
 use Koha::Libraries;
 
@@ -181,6 +180,7 @@ if ($borrowernumber_hold && !$action) {
         $diffbranch = 1;
     }
 
+    my $is_debarred = Koha::Patrons->find( $borrowerinfo->{borrowernumber} )->is_debarred;
     $template->param(
                 borrowernumber      => $borrowerinfo->{'borrowernumber'},
                 borrowersurname     => $borrowerinfo->{'surname'},
@@ -199,7 +199,7 @@ if ($borrowernumber_hold && !$action) {
                 diffbranch          => $diffbranch,
                 messages            => $messages,
                 warnings            => $warnings,
-                restricted          => IsDebarred($borrowerinfo->{'borrowernumber'}),
+                restricted          => $is_debarred,
                 amount_outstanding  => GetMemberAccountRecords($borrowerinfo->{borrowernumber}),
     );
 }
diff --git a/t/db_dependent/Patron/Borrower_Debarments.t b/t/db_dependent/Patron/Borrower_Debarments.t
index bb92094f..c4e52d3 100755
--- a/t/db_dependent/Patron/Borrower_Debarments.t
+++ b/t/db_dependent/Patron/Borrower_Debarments.t
@@ -5,6 +5,7 @@ use Modern::Perl;
 use C4::Context;
 use C4::Members;
 use Koha::Database;
+use Koha::Patrons;
 
 use t::lib::TestBuilder;
 
@@ -154,10 +155,10 @@ $debarments = GetDebarments({ borrowernumber => $borrowernumber });
 is( @$debarments, 0, "DelDebarment functions correctly" );
 
 $dbh->do(q|UPDATE borrowers SET debarred = '1970-01-01'|);
-is( IsDebarred( $borrowernumber ), undef, 'A patron with a debarred date in the past is not debarred' );
+is( Koha::Patrons->find( $borrowernumber )->is_debarred, undef, 'A patron with a debarred date in the past is not debarred' );
 
 $dbh->do(q|UPDATE borrowers SET debarred = NULL|);
-is( IsDebarred( $borrowernumber ), undef, 'A patron without a debarred date is not debarred' );
+is( Koha::Patrons->find( $borrowernumber )->is_debarred, undef, 'A patron without a debarred date is not debarred' );
 
 $dbh->do(q|UPDATE borrowers SET debarred = '9999-12-31'|); # Note: Change this test before the first of January 10000!
-is( IsDebarred( $borrowernumber ), '9999-12-31', 'A patron with a debarred date in the future is debarred' );
+is( Koha::Patrons->find( $borrowernumber )->is_debarred, '9999-12-31', 'A patron with a debarred date in the future is debarred' );
diff --git a/t/db_dependent/Patron/Borrower_Discharge.t b/t/db_dependent/Patron/Borrower_Discharge.t
index 4090475..9fde49c 100644
--- a/t/db_dependent/Patron/Borrower_Discharge.t
+++ b/t/db_dependent/Patron/Borrower_Discharge.t
@@ -90,12 +90,12 @@ Koha::Patron::Discharge::discharge( { borrowernumber => $patron->{borrowernumber
 Koha::Patron::Discharge::discharge( { borrowernumber => $patron2->{borrowernumber} } );
 Koha::Patron::Discharge::discharge( { borrowernumber => $patron3->{borrowernumber} } );
 is( Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->{borrowernumber} } ), 1, 'The patron has been discharged' );
-is( Koha::Patron::Debarments::IsDebarred( $patron->{borrowernumber} ), '9999-12-31', 'The patron has been debarred after discharge' );
+is( Koha::Patrons->find( $patron->{borrowernumber} )->is_debarred, '9999-12-31', 'The patron has been debarred after discharge' );
 is( scalar( @{ Koha::Patron::Discharge::get_validated() } ),             3,            'There are 3 validated discharges' );
 is( scalar( @{ Koha::Patron::Discharge::get_validated( { borrowernumber => $patron->{borrowernumber} } ) } ), 1, 'There is 1 validated discharge for a given patron' );
 is( scalar( @{ Koha::Patron::Discharge::get_validated( { branchcode => $library->{branchcode} } ) } ), 2, 'There is 2 validated discharges for a given branchcode' );    # This is not used in the code yet
 Koha::Patron::Debarments::DelUniqueDebarment( { 'borrowernumber' => $patron->{borrowernumber}, 'type' => 'DISCHARGE' } );
-ok( !Koha::Patron::Debarments::IsDebarred( $patron->{borrowernumber} ), 'The debarment has been lifted' );
+ok( !Koha::Patrons->find( $patron->{borrowernumber} )->is_debarred, 'The debarment has been lifted' );
 ok( !Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->{borrowernumber} } ), 'The patron is not discharged after the restriction has been lifted' );
 
 # Verify that the discharge works multiple times
-- 
1.7.10.4