Bug 17600: Standardize our EXPORT_OK
[srvgit] / misc / cronjobs / automatic_renewals.pl
index a02bdf9..cb45e68 100755 (executable)
@@ -43,6 +43,8 @@ and the renewal isn't premature (No Renewal before) the issue is renewed.
 
 =item B<-s|--send-notices>
 
+DEPRECATED: The system preference AutoRenewalNotices should be used to determine
+whether notices are sent or not
 Send AUTO_RENEWALS notices to patrons if the auto renewal has been done.
 
 =item B<-v|--verbose>
@@ -53,14 +55,10 @@ Print report to standard out.
 
 Without this parameter no changes will be made
 
-=item B<-d|--digest>
-
-Flag to indicate that this script should process digest messages.
-
 =item B<-b|--digest-per-branch>
 
 Flag to indicate that generation of message digests should be
-performed separately for each branch. Needs --digest option.
+performed separately for each branch.
 
 A patron could potentially have loans at several different branches
 There is no natural branch to set as the sender on the aggregated
@@ -77,30 +75,44 @@ chosen 'Digests only' on the advance messages.
 =cut
 
 use Modern::Perl;
-use Pod::Usage;
-use Getopt::Long;
+use Pod::Usage qw( pod2usage );
+use Getopt::Long qw( GetOptions );
 
 use Koha::Script -cron;
-use C4::Circulation;
+use C4::Circulation qw( CanBookBeRenewed AddRenewal );
 use C4::Context;
-use C4::Log;
+use C4::Log qw( cronlogaction );
 use C4::Letters;
 use Koha::Checkouts;
 use Koha::Libraries;
 use Koha::Patrons;
 
-my ( $help, $send_notices, $verbose, $confirm, $digest, $digest_per_branch );
+my ( $help, $send_notices, $verbose, $confirm, $digest_per_branch );
 GetOptions(
     'h|help' => \$help,
     's|send-notices' => \$send_notices,
     'v|verbose'    => \$verbose,
     'c|confirm'     => \$confirm,
-    'd|digest|' => \$digest,
     'b|digest-per-branch' => \$digest_per_branch,
 ) || pod2usage(1);
 
 pod2usage(0) if $help;
 
+my $send_notices_pref = C4::Context->preference('AutoRenewalNotices');
+if ( $send_notices_pref eq 'cron' ) {
+    warn <<'END_WARN';
+
+The "AutoRenewalNotices" syspref is set to 'Follow the cron switch'.
+The send_notices switch for this script is deprecated, you should either set the preference
+to 'Never send emails' or 'Follow patron messaging preferences'
+
+END_WARN
+} else {
+    # If not following cron then we should not send if set to never
+    # and always send any generated according to preferences if following those
+    $send_notices = $send_notices_pref eq 'never' ? 0 : 1;
+}
+
 # Since advance notice options are not visible in the web-interface
 # unless EnhancedMessagingPreferences is on, let the user know that
 # this script probably isn't going to do much
@@ -128,10 +140,11 @@ my %report;
 while ( my $auto_renew = $auto_renews->next ) {
     print "examining item '" . $auto_renew->itemnumber . "' to auto renew\n" if $verbose;
 
-    my $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $auto_renew->borrowernumber,
-                                                                                   message_name   => 'auto_renewals' } );
+    my $borrower_preferences;
+    $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $auto_renew->borrowernumber,
+                                                                                   message_name   => 'auto_renewals' } ) if $send_notices_pref eq 'preferences';
 
-    next if !$digest && $borrower_preferences && $borrower_preferences->{'wants_digest'};
+    $send_notices = 1 if !$send_notices && $send_notices_pref eq 'preferences' && $borrower_preferences && $borrower_preferences->{transports} && $borrower_preferences->{transports}->{email};
 
     # CanBookBeRenewed returns 'auto_renew' when the renewal should be done by this script
     my ( $ok, $error ) = CanBookBeRenewed( $auto_renew->borrowernumber, $auto_renew->itemnumber, undef, 1 );
@@ -144,7 +157,7 @@ while ( my $auto_renew = $auto_renews->next ) {
             my $date_due = AddRenewal( $auto_renew->borrowernumber, $auto_renew->itemnumber, $auto_renew->branchcode, undef, undef, undef, 0 );
             $auto_renew->auto_renew_error(undef)->store;
         }
-        push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew unless $borrower_preferences && (!$borrower_preferences->{transports} || !$borrower_preferences->{transports}->{email} || $borrower_preferences->{'wants_digest'});
+        push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew unless $send_notices_pref ne 'cron' && (!$borrower_preferences || !$borrower_preferences->{transports} || !$borrower_preferences->{transports}->{email} || $borrower_preferences->{'wants_digest'});
     } elsif ( $error eq 'too_many'
         or $error eq 'on_reserve'
         or $error eq 'restriction'
@@ -162,7 +175,7 @@ while ( my $auto_renew = $auto_renews->next ) {
         if ( not $auto_renew->auto_renew_error or $error ne $auto_renew->auto_renew_error ) {
             $auto_renew->auto_renew_error($error)->store if $confirm;
             push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew
-              if $error ne 'auto_too_soon' && (!$borrower_preferences || ($borrower_preferences->{transports} && $borrower_preferences->{transports}->{email} && !$borrower_preferences->{'wants_digest'}));    # Do not notify if it's too soon
+              if $error ne 'auto_too_soon' && ($send_notices_pref eq 'cron' || ($borrower_preferences && $borrower_preferences->{transports} && $borrower_preferences->{transports}->{email} && !$borrower_preferences->{'wants_digest'}));    # Do not notify if it's too soon
         }
     }
 
@@ -170,10 +183,12 @@ while ( my $auto_renew = $auto_renews->next ) {
         # cache this one to process after we've run through all of the items.
         if ($digest_per_branch) {
             $renew_digest->{ $auto_renew->branchcode }->{ $auto_renew->borrowernumber }->{success}++ if $error eq 'auto_renew';
-            $renew_digest->{ $auto_renew->branchcode }->{ $auto_renew->borrowernumber }->{error}++ unless $error eq 'auto_renew' || $error == 'auto_too_soon' ;
+            $renew_digest->{ $auto_renew->branchcode }->{ $auto_renew->borrowernumber }->{error}++ unless $error eq 'auto_renew' || $error eq 'auto_too_soon' ;
+            push @{$renew_digest->{ $auto_renew->branchcode }->{ $auto_renew->borrowernumber }->{issues}}, $auto_renew->itemnumber;
         } else {
             $renew_digest->{ $auto_renew->borrowernumber }->{success} ++ if $error eq 'auto_renew';
             $renew_digest->{ $auto_renew->borrowernumber }->{error}++ unless $error eq 'auto_renew' || $error eq 'auto_too_soon' ;
+            push @{$renew_digest->{ $auto_renew->borrowernumber }->{issues}}, $auto_renew->itemnumber;
         }
     }
 
@@ -197,7 +212,7 @@ if ( $send_notices && $confirm ) {
             );
 
             my $library = Koha::Libraries->find( $patron->branchcode );
-            my $admin_email_address = $library->branchemail || C4::Context->preference('KohaAdminEmailAddress');
+            my $admin_email_address = $library->from_email_address;
 
             C4::Letters::EnqueueLetter(
                 {   letter                 => $letter,
@@ -255,8 +270,6 @@ String that denote the letter code.
 sub send_digests {
     my $params = shift;
 
-    my $admin_email_address = C4::Context->preference('KohaAdminEmailAddress');
-
     PATRON: while ( my ( $borrowernumber, $digest ) = each %{$params->{digests}} ) {
         my $borrower_preferences =
             C4::Members::Messaging::GetMessagingPreferences(
@@ -270,7 +283,7 @@ sub send_digests {
 
         my $patron = Koha::Patrons->find( $borrowernumber );
         my $library = Koha::Libraries->find( $params->{branchcode} );
-        my $from_address = $library->{branchemail} || $admin_email_address;
+        my $from_address = $library->from_email_address;
 
         foreach my $transport ( keys %{ $borrower_preferences->{'transports'} } ) {
             my $letter = C4::Letters::GetPreparedLetter (
@@ -282,6 +295,7 @@ sub send_digests {
                     error => $digest->{error}||0,
                     success => $digest->{success}||0,
                 },
+                loops => { issues => \@{$digest->{issues}} },
                 tables      => {
                     borrowers => $patron->borrowernumber,
                 },