Bug 29859: Use iterator instead of as_list
[srvgit] / misc / cronjobs / advance_notices.pl
index d8dcf73..96029dc 100755 (executable)
 
 =head1 NAME
 
-advance_notices.pl - cron script to put item due reminders into message queue
+advance_notices.pl - prepare messages to be sent to patrons for nearly due, or due, items
 
 =head1 SYNOPSIS
 
-./advance_notices.pl -c
-
-or, in crontab:
-0 1 * * * advance_notices.pl -c
+       advance_notices.pl
+         [ -n ][ -m <number of days> ][ --itemscontent <comma separated field list> ][ -c ]
 
 =head1 DESCRIPTION
 
@@ -40,37 +38,18 @@ the OPAC.
 
 use strict;
 use warnings;
-use Getopt::Long;
-use Pod::Usage;
-use Data::Dumper;
-BEGIN {
-    # find Koha's Perl modules
-    # test carefully before changing this
-    use FindBin;
-    eval { require "$FindBin::Bin/../kohalib.pl" };
-}
+use Getopt::Long qw( GetOptions );
+use Pod::Usage qw( pod2usage );
 use Koha::Script -cron;
-use C4::Biblio;
 use C4::Context;
 use C4::Letters;
 use C4::Members;
 use C4::Members::Messaging;
-use C4::Overdues;
-use Koha::DateUtils;
-use C4::Log;
+use C4::Log qw( cronlogaction );
 use Koha::Items;
 use Koha::Libraries;
 use Koha::Patrons;
 
-=head1 NAME
-
-advance_notices.pl - prepare messages to be sent to patrons for nearly due, or due, items
-
-=head1 SYNOPSIS
-
-advance_notices.pl
-  [ -n ][ -m <number of days> ][ --itemscontent <comma separated field list> ][ -c ]
-
 =head1 OPTIONS
 
 =over 8
@@ -125,11 +104,19 @@ Enabling this flag ensures that the issuing library is the sender of
 the digested message.  It has no effect unless the borrower has
 chosen 'Digests only' on the advance messages.
 
-=back
+=item B<--library>
 
-=head1 DESCRIPTION
+select notices for one specific library. Use the value in the
+branches.branchcode table. This option can be repeated in order
+to select notices for a group of libraries.
+
+=item B<--frombranch>
 
-This script is designed to alert patrons when items are due, or coming due
+Set the from address for the notice to one of 'item-homebranch' or 'item-issuebranch'.
+
+Defaults to 'item-issuebranch'
+
+=back
 
 =head2 Configuration
 
@@ -191,6 +178,8 @@ my $mindays     = 0;                                                # -m: Maximu
 my $maxdays     = 30;                                               # -e: the End of the time period
 my $verbose     = 0;                                                # -v: verbose
 my $digest_per_branch = 0;                                          # -digest-per-branch: Prepare and send digests per branch
+my @branchcodes; # Branch(es) passed as parameter
+my $frombranch   = 'item-issuebranch';
 my $itemscontent = join(',',qw( date_due title author barcode ));
 
 my $help    = 0;
@@ -199,6 +188,8 @@ my $man     = 0;
 GetOptions(
             'help|?'         => \$help,
             'man'            => \$man,
+            'library=s'      => \@branchcodes,
+            'frombranch=s'   => \$frombranch,
             'c'              => \$confirm,
             'n'              => \$nomail,
             'm:i'            => \$maxdays,
@@ -212,7 +203,7 @@ pod2usage( -verbose => 2 ) if $man;
 # 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
-if ( ! C4::Context->preference('EnhancedMessagingPreferences') ) {
+if ( ! C4::Context->preference('EnhancedMessagingPreferences') && $verbose ) {
     warn <<'END_WARN';
 
 The "EnhancedMessagingPreferences" syspref is off.
@@ -226,11 +217,24 @@ unless ($confirm) {
 }
 cronlogaction();
 
+my %branches = ();
+if (@branchcodes) {
+    %branches = map { $_ => 1 } @branchcodes;
+}
+
+die "--frombranch takes item-homebranch or item-issuebranch only"
+  unless ( $frombranch eq 'item-issuebranch'
+    || $frombranch eq 'item-homebranch' );
+my $owning_library = ( $frombranch eq 'item-homebranch' ) ? 1 : 0;
+
 # The fields that will be substituted into <<items.content>>
 my @item_content_fields = split(/,/,$itemscontent);
 
 warn 'getting upcoming due issues' if $verbose;
-my $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $maxdays } );
+my $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( {
+    days_in_advance => $maxdays,
+    owning_library => $owning_library
+ } );
 warn 'found ' . scalar( @$upcoming_dues ) . ' issues' if $verbose;
 
 # hash of borrowernumber to number of items upcoming
@@ -275,6 +279,15 @@ UPCOMINGITEM: foreach my $upcoming ( @$upcoming_dues ) {
                 $due_digest->{ $upcoming->{borrowernumber} }->{count}++;
             }
         } else {
+            my $branchcode;
+            if($owning_library) {
+                $branchcode = $upcoming->{'homebranch'};
+            } else {
+                $branchcode = $upcoming->{'branchcode'};
+            }
+            # Skip this DUE if we specify list of libraries and this one is not part of it
+            next if (@branchcodes && !$branches{$branchcode});
+
             my $item = Koha::Items->find( $upcoming->{itemnumber} );
             my $letter_type = 'DUE';
             $sth->execute($upcoming->{'borrowernumber'},$upcoming->{'itemnumber'},'0');
@@ -287,7 +300,7 @@ UPCOMINGITEM: foreach my $upcoming ( @$upcoming_dues ) {
             foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) {
                 my $letter = parse_letter( { letter_code    => $letter_type,
                                       borrowernumber => $upcoming->{'borrowernumber'},
-                                      branchcode     => $upcoming->{'branchcode'},
+                                      branchcode     => $branchcode,
                                       biblionumber   => $item->biblionumber,
                                       itemnumber     => $upcoming->{'itemnumber'},
                                       substitute     => { 'items.content' => $titles },
@@ -313,6 +326,15 @@ UPCOMINGITEM: foreach my $upcoming ( @$upcoming_dues ) {
                 $upcoming_digest->{ $upcoming->{borrowernumber} }->{count}++;
             }
         } else {
+            my $branchcode;
+            if($owning_library) {
+            $branchcode = $upcoming->{'homebranch'};
+            } else {
+            $branchcode = $upcoming->{'branchcode'};
+            }
+            # Skip this PREDUE if we specify list of libraries and this one is not part of it
+            next if (@branchcodes && !$branches{$branchcode});
+
             my $item = Koha::Items->find( $upcoming->{itemnumber} );
             my $letter_type = 'PREDUE';
             $sth->execute($upcoming->{'borrowernumber'},$upcoming->{'itemnumber'},$borrower_preferences->{'days_in_advance'});
@@ -325,7 +347,7 @@ UPCOMINGITEM: foreach my $upcoming ( @$upcoming_dues ) {
             foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) {
                 my $letter = parse_letter( { letter_code    => $letter_type,
                                       borrowernumber => $upcoming->{'borrowernumber'},
-                                      branchcode     => $upcoming->{'branchcode'},
+                                      branchcode     => $branchcode,
                                       biblionumber   => $item->biblionumber,
                                       itemnumber     => $upcoming->{'itemnumber'},
                                       substitute     => { 'items.content' => $titles },
@@ -342,7 +364,7 @@ UPCOMINGITEM: foreach my $upcoming ( @$upcoming_dues ) {
       if ($nomail) {
         for my $letter ( @letters ) {
             local $, = "\f";
-            print $letter->{'content'};
+            print $letter->{'content'}."\n";
         }
       }
       else {