From 4f48dcc9f86e5fc3edbb4c22b7cc2ad2a87e1f04 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 7 Nov 2016 14:38:17 +0000 Subject: [PATCH] Bug 17569: Koha::Patrons - Move GetUpcomingMembershipExpires to search_upcoming_membership_expires This patchset moves the C4::Members::GetUpcomingMembershipExpires subroutine code to the Koha::Patrons->search_upcoming_membership_expires method. This subroutine was used from only 1 place, so it's an easier to move. Test plan: Use the membership_expiry.pl cronjob script using the different options. The behavior should be the same as prior to this patch. prove t/db_dependent/Koha/Patrons.t should return green Signed-off-by: Josef Moravec Signed-off-by: Marcel de Rooy Signed-off-by: Kyle M Hall --- Koha/Patrons.pm | 33 ++++++++++++++++++++++++++++++ misc/cronjobs/membership_expiry.pl | 16 +++++++-------- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/Koha/Patrons.pm b/Koha/Patrons.pm index 431817902f..d079df0414 100644 --- a/Koha/Patrons.pm +++ b/Koha/Patrons.pm @@ -22,6 +22,7 @@ use Modern::Perl; use Carp; use Koha::Database; +use Koha::DateUtils; use Koha::ArticleRequests; use Koha::ArticleRequest::Status; @@ -69,6 +70,38 @@ sub search_housebound_deliverers { return Koha::Patrons->_new_from_dbic($del); } +=head3 + +my $patrons = Koha::Patrons->search_upcoming_membership_expires(); + +The 'before' and 'after' represent the number of days before/after the date +that is set by the preference MembershipExpiryDaysNotice. +If the pref is 14, before 2 and after 3 then you will get all expires +from 12 to 17 days. + +=cut + +sub search_upcoming_membership_expires { + my ( $self, $params ) = @_; + my $before = $params->{before} || 0; + my $after = $params->{after} || 0; + delete $params->{before}; + delete $params->{after}; + + my $days = C4::Context->preference("MembershipExpiryDaysNotice") || 0; + my $date_before = dt_from_string->add( days => $days - $before ); + my $date_after = dt_from_string->add( days => $days + $after ); + my $dtf = Koha::Database->new->schema->storage->datetime_parser; + + $params->{dateexpiry} = { + ">=" => $dtf->format_date( $date_before ), + "<=" => $dtf->format_date( $date_after ), + }; + return $self->SUPER::search( + $params, { join => ['branchcode', 'categorycode'] } + ); +} + =head3 guarantor Returns a Koha::Patron object for this borrower's guarantor diff --git a/misc/cronjobs/membership_expiry.pl b/misc/cronjobs/membership_expiry.pl index 36c0329201..3b4b8858e8 100755 --- a/misc/cronjobs/membership_expiry.pl +++ b/misc/cronjobs/membership_expiry.pl @@ -167,21 +167,21 @@ if( !$expdays ) { my $admin_adress = C4::Context->preference('KohaAdminEmailAddress'); warn 'getting upcoming membership expires' if $verbose; -my $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires({ branch => $branch, before => $before, after => $after }); -warn 'found ' . scalar( @$upcoming_mem_expires ) . ' soon expiring members' +my $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $branch, before => $before, after => $after }); +warn 'found ' . $upcoming_mem_expires->count . ' soon expiring members' if $verbose; # main loop $letter_type = 'MEMBERSHIP_EXPIRY' if !$letter_type; -foreach my $recent ( @$upcoming_mem_expires ) { - my $from_address = $recent->{'branchemail'} || $admin_adress; +while ( my $recent = $upcoming_mem_expires->next ) { + my $from_address = $recent->library->branchemail || $admin_adress; my $letter = C4::Letters::GetPreparedLetter( module => 'members', letter_code => $letter_type, - branchcode => $recent->{'branchcode'}, + branchcode => $recent->branchcode, tables => { - borrowers => $recent->{'borrowernumber'}, - branches => $recent->{'branchcode'}, + borrowers => $recent->borrowernumber, + branches => $recent->branchcode, }, ); last if !$letter; # Letters.pm already warned, just exit @@ -190,7 +190,7 @@ foreach my $recent ( @$upcoming_mem_expires ) { } else { C4::Letters::EnqueueLetter({ letter => $letter, - borrowernumber => $recent->{'borrowernumber'}, + borrowernumber => $recent->borrowernumber, from_address => $from_address, message_transport_type => 'email', }); -- 2.39.5