Bug 13014: Notify budget owner on new suggestion - cronjob
authorJonathan Druart <jonathan.druart@biblibre.com>
Tue, 30 Sep 2014 14:59:50 +0000 (16:59 +0200)
committerTomas Cohen Arazi <tomascohen@theke.io>
Wed, 24 Jun 2015 14:41:43 +0000 (11:41 -0300)
Test plan:
0/ Create a new notice suggestions > TO_PROCESS
You can use the one defined in the other patch.
1/ Create a suggestion and link it to a fund
2/ Add a owner to this fund and make sure this patron has an email
address (the email address used should be the one defined in the
AutoEmailPrimaryAddress syspref).
3/ Execute the cronjob script with the -v and without the -c argument
4/ The output should tell you that an email will be sent
5/ Execute the cronjob script with the -v and with the -c argument
6/ Verify the notice is generated in the message_queue table and it is
correctly formatted.

Signed-off-by: Frederic Demians <f.demians@tamil.fr>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
misc/cronjobs/notice_unprocessed_suggestions.pl [new file with mode: 0755]

diff --git a/misc/cronjobs/notice_unprocessed_suggestions.pl b/misc/cronjobs/notice_unprocessed_suggestions.pl
new file mode 100755 (executable)
index 0000000..29c8663
--- /dev/null
@@ -0,0 +1,139 @@
+#!/usr/bin/perl
+
+use Modern::Perl;
+
+use Pod::Usage;
+use Getopt::Long;
+
+use C4::Budgets qw( GetBudget );
+use C4::Members qw( GetMember );
+use C4::Suggestions qw( GetUnprocessedSuggestions );
+
+my ( $help, $verbose, $confirm, @days );
+GetOptions(
+    'h|help'    => \$help,
+    'v|verbose' => \$verbose,
+    'days:s'    => \@days,
+    'c|confirm' => \$confirm,
+) || pod2usage( verbose => 2 );
+
+if ($help) {
+    pod2usage( verbose => 2 );
+}
+
+unless (@days) {
+    pod2usage(q{At least one day parameter should be given});
+    exit;
+}
+
+unless ($confirm) {
+    say "Doing a dry run; no email will be sent.";
+    say "Run again with --confirm to sent emails.";
+    $verbose = 1 unless $verbose;
+}
+
+for my $number_of_days (@days) {
+    say "Searching suggestions suggested $number_of_days ago" if $verbose;
+
+    my $suggestions = C4::Suggestions::GetUnprocessedSuggestions($number_of_days);
+
+    say "No suggestion found" if $verbose and not @$suggestions;
+
+    for my $suggestion (@$suggestions) {
+
+        say "Suggestion $suggestion->{suggestionid} should be processed" if $verbose;
+
+        my $budget = C4::Budgets::GetBudget( $suggestion->{budgetid} );
+        my $patron = C4::Members::GetMember( borrowernumber => $budget->{budget_owner_id} );
+        my $email_address =
+          C4::Members::GetNoticeEmailAddress( $budget->{budget_owner_id} );
+        my $library = C4::Branch::GetBranchDetail( $patron->{branchcode} );
+        my $admin_email_address = $library->{branchemail}
+          || C4::Context->preference('KohaAdminEmailAddress');
+
+        if ($email_address) {
+            say "Patron $patron->{borrowernumber} is going to be notified" if $verbose;
+            my $letter = C4::Letters::GetPreparedLetter(
+                module      => 'suggestions',
+                letter_code => 'TO_PROCESS',
+                branchcode  => $patron->{branchcode},
+                tables      => {
+                    suggestions => $suggestion->{suggestionid},
+                    branches    => $patron->{branchcode},
+                    borrowers   => $patron->{borrowernumber},
+                },
+            );
+            if ( $confirm ) {
+                C4::Letters::EnqueueLetter(
+                    {
+                        letter                 => $letter,
+                        borrowernumber         => $patron->{borrowernumber},
+                        message_transport_type => 'email',
+                        from_address           => $admin_email_address,
+                    }
+                );
+            }
+        } else {
+            say "Patron $patron->{borrowernumber} does not have an email address" if $verbose;
+        }
+    }
+
+}
+
+=head1 NAME
+
+notice_unprocessed_suggestions.pl - Generate notification for unprocessed suggestions.
+
+The budget owner will be notified.
+
+The letter template 'TO_PROCESS' will be used.
+
+=head1 SYNOPSIS
+
+notice_unprocessed_suggestions.pl [-h|--help] [-v|--verbose] [-c|--confirm] [--days=NUMBER_OF_DAYS]
+
+=head1 OPTIONS
+
+=over
+
+=item B<-h|--help>
+
+Print a brief help message
+
+=item B<-c|--confirm>
+
+This flag must be provided in order for the script to actually
+generate notices.  If it is not supplied, the script will
+only report on the patron it would have noticed.
+
+=item B<--days>
+
+This parameter is mandatory.
+It must contain an integer representing the number of days elapsed since the last modification of suggestions to process.
+
+=item B<-v|--verbose>
+
+Verbose mode.
+
+=back
+
+=head1 AUTHOR
+
+Jonathan Druart <jonathan.druart@biblibre.com>
+
+=head1 COPYRIGHT
+
+Copyright 2014 BibLibre
+
+=head1 LICENSE
+
+This file is part of Koha.
+
+Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
+Foundation; either version 3 of the License, or (at your option) any later version.
+
+You should have received a copy of the GNU General Public License along
+with Koha; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+=cut