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 <josef.moravec@gmail.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
This commit is contained in:
Jonathan Druart 2016-11-07 14:38:17 +00:00 committed by Kyle M Hall
parent c1e62ceaf7
commit 4f48dcc9f8
2 changed files with 41 additions and 8 deletions

View File

@ -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

View File

@ -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',
});