From d8c53e02431f328165cc59dc996934e298014412 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 29 Jan 2016 16:57:21 +0000 Subject: [PATCH] Bug 15705: Notify the user on auto renewing When an issue is auto renewed, a notice will be sent to the patron. The notice will tell if the renewed has been successfully done. Note that this patch is not ready to be pushed yet, as it has some defects: - 1 notice per issue - no way to disable the notice generation - no way to specify patron categories to enable/disable the notifications Test plan: Use the automatic_renewals.pl script to auto renew issues. If the auto renew has failed or succeeded, a notice will be generated in the message_queue table. If the error is "too_soon" or is the same as the previous error, the notice won't be generated (we do not want to spam the patron). --- misc/cronjobs/automatic_renewals.pl | 68 ++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/misc/cronjobs/automatic_renewals.pl b/misc/cronjobs/automatic_renewals.pl index 73aad5f..de6a3c9 100755 --- a/misc/cronjobs/automatic_renewals.pl +++ b/misc/cronjobs/automatic_renewals.pl @@ -45,21 +45,63 @@ use Modern::Perl; use C4::Circulation; use C4::Context; use C4::Log; +use C4::Letters; +use Koha::Checkouts; +use Koha::Libraries; +use Koha::Patrons; cronlogaction(); -my $dbh = C4::Context->dbh; -my ( $borrowernumber, $itemnumber, $branch, $ok, $error ); - -my $query = -"SELECT borrowernumber, itemnumber, branchcode FROM issues WHERE auto_renew = 1"; -my $sth = $dbh->prepare($query); -$sth->execute(); - -while ( ( $borrowernumber, $itemnumber, $branch ) = $sth->fetchrow_array ) { +my $auto_renews = Koha::Checkouts->search({ auto_renew => 1 }); + +my %report; +while ( my $auto_renew = $auto_renews->next ) { + + # CanBookBeRenewed returns 'auto_renew' when the renewal should be done by this script + my ( $ok, $error ) = CanBookBeRenewed( $auto_renew->borrowernumber, $auto_renew->itemnumber ); + if ( $error eq 'auto_renew' ) { + my $date_due = AddRenewal( $auto_renew->borrowernumber, $auto_renew->itemnumber, $auto_renew->branchcode ); + push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew; + } elsif ( $error eq 'too_many' + or $error eq 'on_reserve' + or $error eq 'restriction' + or $error eq 'overdue' + or $error eq 'auto_too_late' + or $error eq 'auto_too_much_oweing' + or $error eq 'auto_too_soon' ) { + if ( not $auto_renew->auto_renew_error or $error ne $auto_renew->auto_renew_error ) { + $auto_renew->auto_renew_error($error)->store; + push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew + if $error ne 'auto_too_soon'; # Do not notify if it's too soon + } + } +} -# CanBookBeRenewed returns 'auto_renew' when the renewal should be done by this script - ( $ok, $error ) = CanBookBeRenewed( $borrowernumber, $itemnumber ); - AddRenewal( $borrowernumber, $itemnumber, $branch ) - if ( $error eq "auto_renew" ); +for my $borrowernumber ( keys %report ) { + my $patron = Koha::Patrons->find($borrowernumber); + my @issues; + for my $issue ( @{ $report{$borrowernumber} } ) { + my $item = Koha::Items->find( $issue->itemnumber ); + my $letter = C4::Letters::GetPreparedLetter( + module => 'circulation', + letter_code => 'AUTO_RENEWALS', + tables => { + borrowers => $patron->borrowernumber, + issues => $issue->itemnumber, + items => $issue->itemnumber, + biblio => $item->biblionumber, + }, + ); + + my $library = Koha::Libraries->find( $patron->branchcode ); + my $admin_email_address = $library->branchemail || C4::Context->preference('KohaAdminEmailAddress'); + + C4::Letters::EnqueueLetter( + { letter => $letter, + borrowernumber => $borrowernumber, + message_transport_type => 'email', + from_address => $admin_email_address, + } + ); + } } -- 2.1.4