3 # Copyright 2008 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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
47 # find Koha's Perl modules
48 # test carefully before changing this
50 eval { require "$FindBin::Bin/../kohalib.pl" };
56 use C4::Members::Messaging;
66 advance_notices.pl - prepare messages to be sent to patrons for nearly due, or due, items
71 [ -n ][ -m <number of days> ][ --itemscontent <comma separated field list> ][ -c ]
79 Print a brief help message and exits.
83 Prints the manual page and exits.
87 Verbose. Without this flag set, only fatal errors are reported.
91 Do not send any email. Advanced or due notices that would have been sent to
92 the patrons are printed to standard out.
96 Defines the maximum number of days in advance to send advance notices.
100 Confirm flag: Add this option. The script will only print a usage
103 =item B<--itemscontent>
105 comma separated list of fields that get substituted into templates in
106 places of the E<lt>E<lt>items.contentE<gt>E<gt> placeholder. This
107 defaults to date_due,title,author,barcode
109 Other possible values come from fields in the biblios, items and
116 This script is designed to alert patrons when items are due, or coming due
120 This script pays attention to the advanced notice configuration
121 performed by borrowers in the OPAC, or by staff in the patron detail page of the intranet. The content of the messages is configured in Tools -> Notices and slips. Advanced notices use the PREDUE template, due notices use DUE. More information about the use of this
122 section of Koha is available in the Koha manual.
124 =head2 Outgoing emails
126 Typically, messages are prepared for each patron with due
127 items, and who have selected (or the library has elected for them) Advance or Due notices.
129 These emails are staged in the outgoing message queue, as are messages
130 produced by other features of Koha. This message queue must be
131 processed regularly by the
132 F<misc/cronjobs/process_message_queue.pl> program.
134 In the event that the C<-n> flag is passed to this program, no emails
135 are sent. Instead, messages are sent on standard output from this
136 program. They may be redirected to a file if desired.
140 Templates can contain variables enclosed in double angle brackets like
141 E<lt>E<lt>thisE<gt>E<gt>. Those variables will be replaced with values
142 specific to the overdue items or relevant patron. Available variables
147 =item E<lt>E<lt>items.contentE<gt>E<gt>
149 one line for each item, each line containing a tab separated list of
150 date due, title, author, barcode
152 =item E<lt>E<lt>borrowers.*E<gt>E<gt>
154 any field from the borrowers table
156 =item E<lt>E<lt>branches.*E<gt>E<gt>
158 any field from the branches table
164 The F<misc/cronjobs/overdue_notices.pl> program allows you to send
165 messages to patrons when their messages are overdue.
169 binmode( STDOUT, ':encoding(UTF-8)' );
171 # These are defaults for command line options.
172 my $confirm; # -c: Confirm that the user has read and configured this script.
173 my $nomail; # -n: No mail. Will not send any emails.
174 my $mindays = 0; # -m: Maximum number of days in advance to send notices
175 my $maxdays = 30; # -e: the End of the time period
176 my $verbose = 0; # -v: verbose
177 my $itemscontent = join(',',qw( date_due title author barcode ));
189 'itemscontent=s' => \$itemscontent,
191 pod2usage(1) if $help;
192 pod2usage( -verbose => 2 ) if $man;
194 # Since advance notice options are not visible in the web-interface
195 # unless EnhancedMessagingPreferences is on, let the user know that
196 # this script probably isn't going to do much
197 if ( ! C4::Context->preference('EnhancedMessagingPreferences') ) {
200 The "EnhancedMessagingPreferences" syspref is off.
201 Therefore, it is unlikely that this script will actually produce any messages to be sent.
202 To change this, edit the "EnhancedMessagingPreferences" syspref.
212 # The fields that will be substituted into <<items.content>>
213 my @item_content_fields = split(/,/,$itemscontent);
215 warn 'getting upcoming due issues' if $verbose;
216 my $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $maxdays } );
217 warn 'found ' . scalar( @$upcoming_dues ) . ' issues' if $verbose;
219 # hash of borrowernumber to number of items upcoming
220 # for patrons wishing digests only.
224 my $dbh = C4::Context->dbh();
225 my $sth = $dbh->prepare(<<'END_SQL');
226 SELECT biblio.*, items.*, issues.*
227 FROM issues,items,biblio
228 WHERE items.itemnumber=issues.itemnumber
229 AND biblio.biblionumber=items.biblionumber
230 AND issues.borrowernumber = ?
231 AND issues.itemnumber = ?
232 AND (TO_DAYS(date_due)-TO_DAYS(NOW()) = ?)
235 my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
238 UPCOMINGITEM: foreach my $upcoming ( @$upcoming_dues ) {
240 warn 'examining ' . $upcoming->{'itemnumber'} . ' upcoming due items' if $verbose;
242 my $from_address = $upcoming->{branchemail} || $admin_adress;
244 my $borrower_preferences;
245 if ( 0 == $upcoming->{'days_until_due'} ) {
246 # This item is due today. Send an 'item due' message.
247 $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $upcoming->{'borrowernumber'},
248 message_name => 'item_due' } );
249 next unless $borrower_preferences;
251 if ( $borrower_preferences->{'wants_digest'} ) {
252 # cache this one to process after we've run through all of the items.
253 $due_digest->{ $upcoming->{borrowernumber} }->{email} = $from_address;
254 $due_digest->{ $upcoming->{borrowernumber} }->{count}++;
256 my $item = Koha::Items->find( $upcoming->{itemnumber} );
257 my $letter_type = 'DUE';
258 $sth->execute($upcoming->{'borrowernumber'},$upcoming->{'itemnumber'},'0');
260 while ( my $item_info = $sth->fetchrow_hashref()) {
261 my @item_info = map { $_ =~ /^date|date$/ ? format_date($item_info->{$_}) : $item_info->{$_} || '' } @item_content_fields;
262 $titles .= join("\t",@item_info) . "\n";
265 ## Get branch info for borrowers home library.
266 foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) {
267 my $letter = parse_letter( { letter_code => $letter_type,
268 borrowernumber => $upcoming->{'borrowernumber'},
269 branchcode => $upcoming->{'branchcode'},
270 biblionumber => $item->biblionumber,
271 itemnumber => $upcoming->{'itemnumber'},
272 substitute => { 'items.content' => $titles },
273 message_transport_type => $transport,
275 or warn "no letter of type '$letter_type' found for borrowernumber ".$upcoming->{'borrowernumber'}.". Please see sample_notices.sql";
276 push @letters, $letter if $letter;
280 $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $upcoming->{'borrowernumber'},
281 message_name => 'advance_notice' } );
282 next UPCOMINGITEM unless $borrower_preferences && exists $borrower_preferences->{'days_in_advance'};
283 next UPCOMINGITEM unless $borrower_preferences->{'days_in_advance'} == $upcoming->{'days_until_due'};
285 if ( $borrower_preferences->{'wants_digest'} ) {
286 # cache this one to process after we've run through all of the items.
287 $upcoming_digest->{ $upcoming->{borrowernumber} }->{email} = $from_address;
288 $upcoming_digest->{ $upcoming->{borrowernumber} }->{count}++;
290 my $item = Koha::Items->find( $upcoming->{itemnumber} );
291 my $letter_type = 'PREDUE';
292 $sth->execute($upcoming->{'borrowernumber'},$upcoming->{'itemnumber'},$borrower_preferences->{'days_in_advance'});
294 while ( my $item_info = $sth->fetchrow_hashref()) {
295 my @item_info = map { $_ =~ /^date|date$/ ? format_date($item_info->{$_}) : $item_info->{$_} || '' } @item_content_fields;
296 $titles .= join("\t",@item_info) . "\n";
299 ## Get branch info for borrowers home library.
300 foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) {
301 my $letter = parse_letter( { letter_code => $letter_type,
302 borrowernumber => $upcoming->{'borrowernumber'},
303 branchcode => $upcoming->{'branchcode'},
304 biblionumber => $item->biblionumber,
305 itemnumber => $upcoming->{'itemnumber'},
306 substitute => { 'items.content' => $titles },
307 message_transport_type => $transport,
309 or warn "no letter of type '$letter_type' found for borrowernumber ".$upcoming->{'borrowernumber'}.". Please see sample_notices.sql";
310 push @letters, $letter if $letter;
315 # If we have prepared a letter, send it.
318 for my $letter ( @letters ) {
320 print $letter->{'content'};
324 for my $letter ( @letters ) {
325 C4::Letters::EnqueueLetter( { letter => $letter,
326 borrowernumber => $upcoming->{'borrowernumber'},
327 from_address => $from_address,
328 message_transport_type => $letter->{message_transport_type} } );
336 # Now, run through all the people that want digests and send them
338 $sth = $dbh->prepare(<<'END_SQL');
339 SELECT biblio.*, items.*, issues.*
340 FROM issues,items,biblio
341 WHERE items.itemnumber=issues.itemnumber
342 AND biblio.biblionumber=items.biblionumber
343 AND issues.borrowernumber = ?
344 AND (TO_DAYS(date_due)-TO_DAYS(NOW()) = ?)
346 PATRON: while ( my ( $borrowernumber, $digest ) = each %$upcoming_digest ) {
348 my $count = $digest->{count};
349 my $from_address = $digest->{email};
351 my $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber,
352 message_name => 'advance_notice' } );
353 next PATRON unless $borrower_preferences; # how could this happen?
356 my $letter_type = 'PREDUEDGST';
358 $sth->execute($borrowernumber,$borrower_preferences->{'days_in_advance'});
360 while ( my $item_info = $sth->fetchrow_hashref()) {
361 my @item_info = map { $_ =~ /^date|date$/ ? format_date($item_info->{$_}) : $item_info->{$_} || '' } @item_content_fields;
362 $titles .= join("\t",@item_info) . "\n";
365 ## Get branch info for borrowers home library.
366 my %branch_info = get_branch_info( $borrowernumber );
368 foreach my $transport ( keys %{ $borrower_preferences->{'transports'} } ) {
369 my $letter = parse_letter(
371 letter_code => $letter_type,
372 borrowernumber => $borrowernumber,
375 'items.content' => $titles,
378 branchcode => $branch_info{"branches.branchcode"},
379 message_transport_type => $transport,
382 or warn "no letter of type '$letter_type' found for borrowernumber $borrowernumber. Please see sample_notices.sql";
383 push @letters, $letter if $letter;
388 for my $letter ( @letters ) {
390 print $letter->{'content'};
394 for my $letter ( @letters ) {
395 C4::Letters::EnqueueLetter( { letter => $letter,
396 borrowernumber => $borrowernumber,
397 from_address => $from_address,
398 message_transport_type => $letter->{message_transport_type} } );
404 # Now, run through all the people that want digests and send them
405 PATRON: while ( my ( $borrowernumber, $digest ) = each %$due_digest ) {
407 my $count = $digest->{count};
408 my $from_address = $digest->{email};
410 my $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber,
411 message_name => 'item_due' } );
412 next PATRON unless $borrower_preferences; # how could this happen?
414 my $letter_type = 'DUEDGST';
415 $sth->execute($borrowernumber,'0');
417 while ( my $item_info = $sth->fetchrow_hashref()) {
418 my @item_info = map { $_ =~ /^date|date$/ ? format_date($item_info->{$_}) : $item_info->{$_} || '' } @item_content_fields;
419 $titles .= join("\t",@item_info) . "\n";
422 ## Get branch info for borrowers home library.
423 my %branch_info = get_branch_info( $borrowernumber );
425 for my $transport ( keys %{ $borrower_preferences->{'transports'} } ) {
426 my $letter = parse_letter(
428 letter_code => $letter_type,
429 borrowernumber => $borrowernumber,
432 'items.content' => $titles,
435 branchcode => $branch_info{"branches.branchcode"},
436 message_transport_type => $transport,
439 or warn "no letter of type '$letter_type' found for borrowernumber $borrowernumber. Please see sample_notices.sql";
440 push @letters, $letter if $letter;
445 for my $letter ( @letters ) {
447 print $letter->{'content'};
451 for my $letter ( @letters ) {
452 C4::Letters::EnqueueLetter( { letter => $letter,
453 borrowernumber => $borrowernumber,
454 from_address => $from_address,
455 message_transport_type => $letter->{message_transport_type} } );
470 foreach my $required ( qw( letter_code borrowernumber ) ) {
471 return unless exists $params->{$required};
473 my $patron = Koha::Patrons->find( $params->{borrowernumber} );
475 my %table_params = ( 'borrowers' => $params->{'borrowernumber'} );
477 if ( my $p = $params->{'branchcode'} ) {
478 $table_params{'branches'} = $p;
480 if ( my $p = $params->{'itemnumber'} ) {
481 $table_params{'issues'} = $p;
482 $table_params{'items'} = $p;
484 if ( my $p = $params->{'biblionumber'} ) {
485 $table_params{'biblio'} = $p;
486 $table_params{'biblioitems'} = $p;
489 return C4::Letters::GetPreparedLetter (
490 module => 'circulation',
491 letter_code => $params->{'letter_code'},
492 branchcode => $table_params{'branches'},
493 lang => $patron->lang,
494 substitute => $params->{'substitute'},
495 tables => \%table_params,
496 message_transport_type => $params->{message_transport_type},
501 my $date_string = shift;
502 my $dt=dt_from_string($date_string);
503 return output_pref($dt);
506 =head2 get_branch_info
510 sub get_branch_info {
511 my ( $borrowernumber ) = @_;
513 ## Get branch info for borrowers home library.
514 my $patron = Koha::Patrons->find( $borrowernumber );
515 my $branch = $patron->library->unblessed;
517 foreach my $key( keys %$branch ) {
518 $branch_info{"branches.$key"} = $branch->{$key};