Bug 16039: Remove useless exit
[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 C4::Budgets qw( GetBudget );
9 use C4::Members qw( GetMember );
10 use C4::Suggestions qw( GetUnprocessedSuggestions );
11 use Koha::Libraries;
12
13 my ( $help, $verbose, $confirm, @days );
14 GetOptions(
15     'h|help'    => \$help,
16     'v|verbose' => \$verbose,
17     'days:s'    => \@days,
18     'c|confirm' => \$confirm,
19 ) || pod2usage( verbose => 2 );
20
21 if ($help) {
22     pod2usage( verbose => 2 );
23 }
24
25 unless (@days) {
26     pod2usage(q{At least one day parameter should be given});
27     exit;
28 }
29
30 unless ($confirm) {
31     say "Doing a dry run; no email will be sent.";
32     say "Run again with --confirm to send emails.";
33     $verbose = 1 unless $verbose;
34 }
35
36 for my $number_of_days (@days) {
37     say "Searching suggestions suggested $number_of_days days ago" if $verbose;
38
39     my $suggestions = C4::Suggestions::GetUnprocessedSuggestions($number_of_days);
40
41     say "No suggestion found" if $verbose and not @$suggestions;
42
43     for my $suggestion (@$suggestions) {
44
45         say "Suggestion $suggestion->{suggestionid} should be processed" if $verbose;
46
47         my $budget = C4::Budgets::GetBudget( $suggestion->{budgetid} );
48         my $patron = C4::Members::GetMember( borrowernumber => $budget->{budget_owner_id} );
49         my $email_address =
50           C4::Members::GetNoticeEmailAddress( $budget->{budget_owner_id} );
51         my $library = Koha::Libraries->find( $patron->{branchcode} );
52         my $admin_email_address = $library->branchemail
53           || C4::Context->preference('KohaAdminEmailAddress');
54
55         if ($email_address) {
56             say "Patron $patron->{borrowernumber} is going to be notified" if $verbose;
57             my $letter = C4::Letters::GetPreparedLetter(
58                 module      => 'suggestions',
59                 letter_code => 'TO_PROCESS',
60                 branchcode  => $patron->{branchcode},
61                 tables      => {
62                     suggestions => $suggestion->{suggestionid},
63                     branches    => $patron->{branchcode},
64                     borrowers   => $patron->{borrowernumber},
65                 },
66             );
67             if ( $confirm ) {
68                 C4::Letters::EnqueueLetter(
69                     {
70                         letter                 => $letter,
71                         borrowernumber         => $patron->{borrowernumber},
72                         message_transport_type => 'email',
73                         from_address           => $admin_email_address,
74                     }
75                 );
76             }
77         } else {
78             say "Patron $patron->{borrowernumber} does not have an email address" if $verbose;
79         }
80     }
81
82 }
83
84 =head1 NAME
85
86 notice_unprocessed_suggestions.pl - Generate notification for unprocessed suggestions.
87
88 The budget owner will be notified.
89
90 The letter template 'TO_PROCESS' will be used.
91
92 =head1 SYNOPSIS
93
94 notice_unprocessed_suggestions.pl [-h|--help] [-v|--verbose] [-c|--confirm] [--days=NUMBER_OF_DAYS]
95
96 =head1 OPTIONS
97
98 =over
99
100 =item B<-h|--help>
101
102 Print a brief help message
103
104 =item B<-c|--confirm>
105
106 This flag must be provided in order for the script to actually
107 generate notices.  If it is not supplied, the script will
108 only report on the patron it would have noticed.
109
110 =item B<--days>
111
112 This parameter is mandatory.
113 It must contain an integer representing the number of days elapsed since the last modification of suggestions to process.
114
115 =item B<-v|--verbose>
116
117 Verbose mode.
118
119 =back
120
121 =head1 AUTHOR
122
123 Jonathan Druart <jonathan.druart@biblibre.com>
124
125 =head1 COPYRIGHT
126
127 Copyright 2014 BibLibre
128
129 =head1 LICENSE
130
131 This file is part of Koha.
132
133 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
134 Foundation; either version 3 of the License, or (at your option) any later version.
135
136 You should have received a copy of the GNU General Public License along
137 with Koha; if not, write to the Free Software Foundation, Inc.,
138 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
139
140 =cut