Bug 28581: Use 'from_email_address' where appropriate
[koha.git] / misc / cronjobs / notice_unprocessed_suggestions.pl
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Pod::Usage;
6 use Getopt::Long;
7
8 use Koha::Script -cron;
9 use C4::Budgets qw( GetBudget );
10 use C4::Suggestions qw( GetUnprocessedSuggestions );
11 use Koha::Libraries;
12 use Koha::Patrons;
13
14 my ( $help, $verbose, $confirm, @days );
15 GetOptions(
16     'h|help'    => \$help,
17     'v|verbose' => \$verbose,
18     'days:s'    => \@days,
19     'c|confirm' => \$confirm,
20 ) || pod2usage( verbose => 2 );
21
22 if ($help) {
23     pod2usage( verbose => 2 );
24 }
25
26 unless (@days) {
27     pod2usage(q{At least one day parameter should be given});
28     exit;
29 }
30
31 unless ($confirm) {
32     say "Doing a dry run; no email will be sent.";
33     say "Run again with --confirm to send emails.";
34     $verbose = 1 unless $verbose;
35 }
36
37 for my $number_of_days (@days) {
38     say "Searching suggestions suggested $number_of_days days ago" if $verbose;
39
40     my $suggestions = C4::Suggestions::GetUnprocessedSuggestions($number_of_days);
41
42     say "No suggestion found" if $verbose and not @$suggestions;
43
44     for my $suggestion (@$suggestions) {
45
46         say "Suggestion $suggestion->{suggestionid} should be processed" if $verbose;
47
48         my $budget = C4::Budgets::GetBudget( $suggestion->{budgetid} );
49         my $patron = Koha::Patrons->find( $budget->{budget_owner_id} );
50         my $email_address = $patron->notice_email_address;
51
52         if ($email_address) {
53             say "Patron " . $patron->borrowernumber . " is going to be notified" if $verbose;
54             my $letter = C4::Letters::GetPreparedLetter(
55                 module      => 'suggestions',
56                 letter_code => 'TO_PROCESS',
57                 branchcode  => $patron->branchcode,
58                 lang        => $patron->lang,
59                 tables      => {
60                     suggestions => $suggestion->{suggestionid},
61                     branches    => $patron->branchcode,
62                     borrowers   => $patron->borrowernumber,
63                 },
64             );
65             if ( $confirm ) {
66                 C4::Letters::EnqueueLetter(
67                     {
68                         letter                 => $letter,
69                         borrowernumber         => $patron->borrowernumber,
70                         message_transport_type => 'email'
71                     }
72                 );
73             }
74         } else {
75             say "Patron " . $patron->borrowernumber . " does not have an email address" if $verbose;
76         }
77     }
78
79 }
80
81 =head1 NAME
82
83 notice_unprocessed_suggestions.pl - Generate notification for unprocessed suggestions.
84
85 The budget owner will be notified.
86
87 The letter template 'TO_PROCESS' will be used.
88
89 =head1 SYNOPSIS
90
91 notice_unprocessed_suggestions.pl [-h|--help] [-v|--verbose] [-c|--confirm] [--days=NUMBER_OF_DAYS]
92
93 =head1 OPTIONS
94
95 =over
96
97 =item B<-h|--help>
98
99 Print a brief help message
100
101 =item B<-c|--confirm>
102
103 This flag must be provided in order for the script to actually
104 generate notices.  If it is not supplied, the script will
105 only report on the patron it would have noticed.
106
107 =item B<--days>
108
109 This parameter is mandatory.
110 It must contain an integer representing the number of days elapsed since the last modification of suggestions to process.
111
112 =item B<-v|--verbose>
113
114 Verbose mode.
115
116 =back
117
118 =head1 AUTHOR
119
120 Jonathan Druart <jonathan.druart@biblibre.com>
121
122 =head1 COPYRIGHT
123
124 Copyright 2014 BibLibre
125
126 =head1 LICENSE
127
128 This file is part of Koha.
129
130 # Koha is free software; you can redistribute it and/or modify it
131 # under the terms of the GNU General Public License as published by
132 # the Free Software Foundation; either version 3 of the License, or
133 # (at your option) any later version.
134 #
135 # Koha is distributed in the hope that it will be useful, but
136 # WITHOUT ANY WARRANTY; without even the implied warranty of
137 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138 # GNU General Public License for more details.
139 #
140 # You should have received a copy of the GNU General Public License
141 # along with Koha; if not, see <http://www.gnu.org/licenses>.
142
143 =cut