3 # Copyright 2008 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 advance_notices.pl - cron script to put item due reminders into message queue
26 ./advance_notices.pl -c
29 0 1 * * * advance_notices.pl -c
33 This script prepares pre-due and item due reminders to be sent to
34 patrons. It queues them in the message queue, which is processed by
35 the process_message_queue.pl cronjob. The type and timing of the
36 messages can be configured by the patrons in their "My Alerts" tab in
46 # find Koha's Perl modules
47 # test carefully before changing this
49 eval { require "$FindBin::Bin/../kohalib.pl" };
55 use C4::Members::Messaging;
57 use C4::Dates qw/format_date/;
60 # These are defaults for command line options.
61 my $confirm; # -c: Confirm that the user has read and configured this script.
62 # my $confirm = 1; # -c: Confirm that the user has read and configured this script.
63 my $nomail; # -n: No mail. Will not send any emails.
64 my $mindays = 0; # -m: Maximum number of days in advance to send notices
65 my $maxdays = 30; # -e: the End of the time period
66 my $verbose = 0; # -v: verbose
67 my $itemscontent = join(',',qw( issuedate title barcode author ));
69 GetOptions( 'c' => \$confirm,
73 'itemscontent=s' => \$itemscontent,
75 my $usage = << 'ENDUSAGE';
77 This script prepares pre-due and item due reminders to be sent to
78 patrons. It queues them in the message queue, which is processed by
79 the process_message_queue.pl cronjob.
80 See the comments in the script for directions on changing the script.
81 This script has the following parameters :
82 -c Confirm and remove this help & warning
83 -m maximum number of days in advance to send advance notices.
84 -n send No mail. Instead, all mail messages are printed on screen. Usefull for testing purposes.
88 # Since advance notice options are not visible in the web-interface
89 # unless EnhancedMessagingPreferences is on, let the user know that
90 # this script probably isn't going to do much
91 if ( ! C4::Context->preference('EnhancedMessagingPreferences') ) {
94 The "EnhancedMessagingPreferences" syspref is off.
95 Therefore, it is unlikely that this script will actually produce any messages to be sent.
96 To change this, edit the "EnhancedMessagingPreferences" syspref.
103 print "Do you wish to continue? (y/n)";
109 # The fields that will be substituted into <<items.content>>
110 my @item_content_fields = split(/,/,$itemscontent);
112 warn 'getting upcoming due issues' if $verbose;
113 my $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $maxdays } );
114 warn 'found ' . scalar( @$upcoming_dues ) . ' issues' if $verbose;
116 # hash of borrowernumber to number of items upcoming
117 # for patrons wishing digests only.
121 my $dbh = C4::Context->dbh();
122 my $sth = $dbh->prepare(<<'END_SQL');
123 SELECT biblio.*, items.*, issues.*
124 FROM issues,items,biblio
125 WHERE items.itemnumber=issues.itemnumber
126 AND biblio.biblionumber=items.biblionumber
127 AND issues.borrowernumber = ?
128 AND issues.itemnumber = ?
129 AND (TO_DAYS(date_due)-TO_DAYS(NOW()) = ?)
132 my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
134 UPCOMINGITEM: foreach my $upcoming ( @$upcoming_dues ) {
135 warn 'examining ' . $upcoming->{'itemnumber'} . ' upcoming due items' if $verbose;
136 # warn( Data::Dumper->Dump( [ $upcoming ], [ 'overdue' ] ) );
138 my $from_address = $upcoming->{branchemail} || $admin_adress;
141 my $borrower_preferences;
142 if ( 0 == $upcoming->{'days_until_due'} ) {
143 # This item is due today. Send an 'item due' message.
144 $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $upcoming->{'borrowernumber'},
145 message_name => 'item_due' } );
146 # warn( Data::Dumper->Dump( [ $borrower_preferences ], [ 'borrower_preferences' ] ) );
147 next unless $borrower_preferences;
149 if ( $borrower_preferences->{'wants_digest'} ) {
150 # cache this one to process after we've run through all of the items.
151 my $digest = $due_digest->{$upcoming->{'borrowernumber'}} ||= {};
152 $digest->{email} ||= $from_address;
155 my $biblio = C4::Biblio::GetBiblioFromItemNumber( $upcoming->{'itemnumber'} );
156 my $letter_type = 'DUE';
157 $sth->execute($upcoming->{'borrowernumber'},$upcoming->{'itemnumber'},'0');
159 while ( my $item_info = $sth->fetchrow_hashref()) {
160 my @item_info = map { $_ =~ /^date|date$/ ? format_date($item_info->{$_}) : $item_info->{$_} || '' } @item_content_fields;
161 $titles .= join("\t",@item_info) . "\n";
164 $letter = parse_letter( { letter_code => $letter_type,
165 borrowernumber => $upcoming->{'borrowernumber'},
166 branchcode => $upcoming->{'branchcode'},
167 biblionumber => $biblio->{'biblionumber'},
168 itemnumber => $upcoming->{'itemnumber'},
169 substitute => { 'items.content' => $titles }
171 or die "no letter of type '$letter_type' found. Please see sample_notices.sql";
174 $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $upcoming->{'borrowernumber'},
175 message_name => 'advance_notice' } );
176 # warn( Data::Dumper->Dump( [ $borrower_preferences ], [ 'borrower_preferences' ] ) );
177 next UPCOMINGITEM unless $borrower_preferences && exists $borrower_preferences->{'days_in_advance'};
178 next UPCOMINGITEM unless $borrower_preferences->{'days_in_advance'} == $upcoming->{'days_until_due'};
180 if ( $borrower_preferences->{'wants_digest'} ) {
181 # cache this one to process after we've run through all of the items.
182 my $digest = $upcoming_digest->{$upcoming->{'borrowernumber'}} ||= {};
183 $digest->{email} ||= $from_address;
186 my $biblio = C4::Biblio::GetBiblioFromItemNumber( $upcoming->{'itemnumber'} );
187 my $letter_type = 'PREDUE';
188 $sth->execute($upcoming->{'borrowernumber'},$upcoming->{'itemnumber'},$borrower_preferences->{'days_in_advance'});
190 while ( my $item_info = $sth->fetchrow_hashref()) {
191 my @item_info = map { $_ =~ /^date|date$/ ? format_date($item_info->{$_}) : $item_info->{$_} || '' } @item_content_fields;
192 $titles .= join("\t",@item_info) . "\n";
195 $letter = parse_letter( { letter_code => $letter_type,
196 borrowernumber => $upcoming->{'borrowernumber'},
197 branchcode => $upcoming->{'branchcode'},
198 biblionumber => $biblio->{'biblionumber'},
199 itemnumber => $upcoming->{'itemnumber'},
200 substitute => { 'items.content' => $titles }
202 or die "no letter of type '$letter_type' found. Please see sample_notices.sql";
206 # If we have prepared a letter, send it.
210 print $letter->{'content'};
213 foreach my $transport ( @{$borrower_preferences->{'transports'}} ) {
214 C4::Letters::EnqueueLetter( { letter => $letter,
215 borrowernumber => $upcoming->{'borrowernumber'},
216 from_address => $from_address,
217 message_transport_type => $transport } );
224 # warn( Data::Dumper->Dump( [ $upcoming_digest ], [ 'upcoming_digest' ] ) );
226 # Now, run through all the people that want digests and send them
228 $sth = $dbh->prepare(<<'END_SQL');
229 SELECT biblio.*, items.*, issues.*
230 FROM issues,items,biblio
231 WHERE items.itemnumber=issues.itemnumber
232 AND biblio.biblionumber=items.biblionumber
233 AND issues.borrowernumber = ?
234 AND (TO_DAYS(date_due)-TO_DAYS(NOW()) = ?)
237 PATRON: while ( my ( $borrowernumber, $digest ) = each %$upcoming_digest ) {
238 my $count = $digest->{count};
239 my $from_address = $digest->{email};
241 my $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber,
242 message_name => 'advance_notice' } );
243 # warn( Data::Dumper->Dump( [ $borrower_preferences ], [ 'borrower_preferences' ] ) );
244 next PATRON unless $borrower_preferences; # how could this happen?
247 my $letter_type = 'PREDUEDGST';
249 $sth->execute($borrowernumber,$borrower_preferences->{'days_in_advance'});
251 while ( my $item_info = $sth->fetchrow_hashref()) {
252 my @item_info = map { $_ =~ /^date|date$/ ? format_date($item_info->{$_}) : $item_info->{$_} || '' } @item_content_fields;
253 $titles .= join("\t",@item_info) . "\n";
255 my $letter = parse_letter( { letter_code => $letter_type,
256 borrowernumber => $borrowernumber,
257 substitute => { count => $count,
258 'items.content' => $titles
261 or die "no letter of type '$letter_type' found. Please see sample_notices.sql";
264 print $letter->{'content'};
267 foreach my $transport ( @{$borrower_preferences->{'transports'}} ) {
268 C4::Letters::EnqueueLetter( { letter => $letter,
269 borrowernumber => $borrowernumber,
270 from_address => $from_address,
271 message_transport_type => $transport } );
276 # Now, run through all the people that want digests and send them
277 PATRON: while ( my ( $borrowernumber, $digest ) = each %$due_digest ) {
278 my $count = $digest->{count};
279 my $from_address = $digest->{email};
281 my $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber,
282 message_name => 'item_due' } );
283 # warn( Data::Dumper->Dump( [ $borrower_preferences ], [ 'borrower_preferences' ] ) );
284 next PATRON unless $borrower_preferences; # how could this happen?
286 my $letter_type = 'DUEDGST';
287 $sth->execute($borrowernumber,'0');
289 while ( my $item_info = $sth->fetchrow_hashref()) {
290 my @item_info = map { $_ =~ /^date|date$/ ? format_date($item_info->{$_}) : $item_info->{$_} || '' } @item_content_fields;
291 $titles .= join("\t",@item_info) . "\n";
293 my $letter = parse_letter( { letter_code => $letter_type,
294 borrowernumber => $borrowernumber,
295 substitute => { count => $count,
296 'items.content' => $titles
299 or die "no letter of type '$letter_type' found. Please see sample_notices.sql";
303 print $letter->{'content'};
306 foreach my $transport ( @{$borrower_preferences->{'transports'}} ) {
307 C4::Letters::EnqueueLetter( { letter => $letter,
308 borrowernumber => $borrowernumber,
309 from_address => $from_address,
310 message_transport_type => $transport } );
323 foreach my $required ( qw( letter_code borrowernumber ) ) {
324 return unless exists $params->{$required};
327 my %table_params = ( 'borrowers' => $params->{'borrowernumber'} );
329 if ( my $p = $params->{'branchcode'} ) {
330 $table_params{'branches'} = $p;
332 if ( my $p = $params->{'itemnumber'} ) {
333 $table_params{'issues'} = $p;
334 $table_params{'items'} = $p;
336 if ( my $p = $params->{'biblionumber'} ) {
337 $table_params{'biblio'} = $p;
338 $table_params{'biblioitems'} = $p;
341 return C4::Letters::GetPreparedLetter (
342 module => 'circulation',
343 letter_code => $params->{'letter_code'},
344 branchcode => $table_params{'branches'},
345 substitute => $params->{'substitute'},
346 tables => \%table_params,