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" };
52 use Koha::Script -cron;
57 use C4::Members::Messaging;
67 advance_notices.pl - prepare messages to be sent to patrons for nearly due, or due, items
72 [ -n ][ -m <number of days> ][ --itemscontent <comma separated field list> ][ -c ]
80 Print a brief help message and exits.
84 Prints the manual page and exits.
88 Verbose. Without this flag set, only fatal errors are reported.
92 Do not send any email. Advanced or due notices that would have been sent to
93 the patrons are printed to standard out.
97 Defines the maximum number of days in advance to send advance notices.
101 Confirm flag: Add this option. The script will only print a usage
104 =item B<--itemscontent>
106 comma separated list of fields that get substituted into templates in
107 places of the E<lt>E<lt>items.contentE<gt>E<gt> placeholder. This
108 defaults to date_due,title,author,barcode
110 Other possible values come from fields in the biblios, items and
113 =item B<--digest-per-branch>
115 Flag to indicate that generation of message digests should be
116 performed separately for each branch.
118 A patron could potentially have loans at several different branches
119 There is no natural branch to set as the sender on the aggregated
120 message in this situation so the default behavior is to use the
121 borrowers home branch. This could surprise to the borrower when
122 message sender is a library where they have not borrowed anything.
124 Enabling this flag ensures that the issuing library is the sender of
125 the digested message. It has no effect unless the borrower has
126 chosen 'Digests only' on the advance messages.
130 select notices for one specific library. Use the value in the
131 branches.branchcode table. This option can be repeated in order
132 to select notices for a group of libraries.
136 Use the address information from the item homebranch library instead of the issuing library.
142 This script is designed to alert patrons when items are due, or coming due
146 This script pays attention to the advanced notice configuration
147 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
148 section of Koha is available in the Koha manual.
150 =head2 Outgoing emails
152 Typically, messages are prepared for each patron with due
153 items, and who have selected (or the library has elected for them) Advance or Due notices.
155 These emails are staged in the outgoing message queue, as are messages
156 produced by other features of Koha. This message queue must be
157 processed regularly by the
158 F<misc/cronjobs/process_message_queue.pl> program.
160 In the event that the C<-n> flag is passed to this program, no emails
161 are sent. Instead, messages are sent on standard output from this
162 program. They may be redirected to a file if desired.
166 Templates can contain variables enclosed in double angle brackets like
167 E<lt>E<lt>thisE<gt>E<gt>. Those variables will be replaced with values
168 specific to the overdue items or relevant patron. Available variables
173 =item E<lt>E<lt>items.contentE<gt>E<gt>
175 one line for each item, each line containing a tab separated list of
176 date due, title, author, barcode
178 =item E<lt>E<lt>borrowers.*E<gt>E<gt>
180 any field from the borrowers table
182 =item E<lt>E<lt>branches.*E<gt>E<gt>
184 any field from the branches table
190 The F<misc/cronjobs/overdue_notices.pl> program allows you to send
191 messages to patrons when their messages are overdue.
195 binmode( STDOUT, ':encoding(UTF-8)' );
197 # These are defaults for command line options.
198 my $confirm; # -c: Confirm that the user has read and configured this script.
199 my $nomail; # -n: No mail. Will not send any emails.
200 my $mindays = 0; # -m: Maximum number of days in advance to send notices
201 my $maxdays = 30; # -e: the End of the time period
202 my $verbose = 0; # -v: verbose
203 my $digest_per_branch = 0; # -digest-per-branch: Prepare and send digests per branch
204 my @branchcodes; # Branch(es) passed as parameter
205 my $owning_library = 0;
206 my $itemscontent = join(',',qw( date_due title author barcode ));
214 'library=s' => \@branchcodes,
215 'owning' => \$owning_library,
220 'digest-per-branch' => \$digest_per_branch,
221 'itemscontent=s' => \$itemscontent,
223 pod2usage(1) if $help;
224 pod2usage( -verbose => 2 ) if $man;
226 # Since advance notice options are not visible in the web-interface
227 # unless EnhancedMessagingPreferences is on, let the user know that
228 # this script probably isn't going to do much
229 if ( ! C4::Context->preference('EnhancedMessagingPreferences') ) {
232 The "EnhancedMessagingPreferences" syspref is off.
233 Therefore, it is unlikely that this script will actually produce any messages to be sent.
234 To change this, edit the "EnhancedMessagingPreferences" syspref.
243 my %branches = map { $_ => 1 } @branchcodes if @branchcodes;
245 # The fields that will be substituted into <<items.content>>
246 my @item_content_fields = split(/,/,$itemscontent);
248 warn 'getting upcoming due issues' if $verbose;
249 my $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( {
250 days_in_advance => $maxdays,
251 owning_library => $owning_library
253 warn 'found ' . scalar( @$upcoming_dues ) . ' issues' if $verbose;
255 # hash of borrowernumber to number of items upcoming
256 # for patrons wishing digests only.
257 my $upcoming_digest = {};
260 my $dbh = C4::Context->dbh();
261 my $sth = $dbh->prepare(<<'END_SQL');
262 SELECT biblio.*, items.*, issues.*
263 FROM issues,items,biblio
264 WHERE items.itemnumber=issues.itemnumber
265 AND biblio.biblionumber=items.biblionumber
266 AND issues.borrowernumber = ?
267 AND issues.itemnumber = ?
268 AND (TO_DAYS(date_due)-TO_DAYS(NOW()) = ?)
271 my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
274 UPCOMINGITEM: foreach my $upcoming ( @$upcoming_dues ) {
276 warn 'examining ' . $upcoming->{'itemnumber'} . ' upcoming due items' if $verbose;
278 my $from_address = $upcoming->{branchemail} || $admin_adress;
280 my $borrower_preferences;
281 if ( 0 == $upcoming->{'days_until_due'} ) {
282 # This item is due today. Send an 'item due' message.
283 $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $upcoming->{'borrowernumber'},
284 message_name => 'item_due' } );
285 next unless $borrower_preferences;
287 if ( $borrower_preferences->{'wants_digest'} ) {
288 # cache this one to process after we've run through all of the items.
289 if ($digest_per_branch) {
290 $due_digest->{ $upcoming->{branchcode} }->{ $upcoming->{borrowernumber} }->{email} = $from_address;
291 $due_digest->{ $upcoming->{branchcode} }->{ $upcoming->{borrowernumber} }->{count}++;
293 $due_digest->{ $upcoming->{borrowernumber} }->{email} = $from_address;
294 $due_digest->{ $upcoming->{borrowernumber} }->{count}++;
298 if($owning_library) {
299 $branchcode = $upcoming->{'homebranch'};
301 $branchcode = $upcoming->{'branchcode'};
303 # Skip this DUE if we specify list of libraries and this one is not part of it
304 next if (@branchcodes && !$branches{$branchcode});
306 my $item = Koha::Items->find( $upcoming->{itemnumber} );
307 my $letter_type = 'DUE';
308 $sth->execute($upcoming->{'borrowernumber'},$upcoming->{'itemnumber'},'0');
310 while ( my $item_info = $sth->fetchrow_hashref()) {
311 $titles .= C4::Letters::get_item_content( { item => $item_info, item_content_fields => \@item_content_fields } );
314 ## Get branch info for borrowers home library.
315 foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) {
316 my $letter = parse_letter( { letter_code => $letter_type,
317 borrowernumber => $upcoming->{'borrowernumber'},
318 branchcode => $branchcode,
319 biblionumber => $item->biblionumber,
320 itemnumber => $upcoming->{'itemnumber'},
321 substitute => { 'items.content' => $titles },
322 message_transport_type => $transport,
324 or warn "no letter of type '$letter_type' found for borrowernumber ".$upcoming->{'borrowernumber'}.". Please see sample_notices.sql";
325 push @letters, $letter if $letter;
329 $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $upcoming->{'borrowernumber'},
330 message_name => 'advance_notice' } );
331 next UPCOMINGITEM unless $borrower_preferences && exists $borrower_preferences->{'days_in_advance'};
332 next UPCOMINGITEM unless $borrower_preferences->{'days_in_advance'} == $upcoming->{'days_until_due'};
334 if ( $borrower_preferences->{'wants_digest'} ) {
335 # cache this one to process after we've run through all of the items.
336 if ($digest_per_branch) {
337 $upcoming_digest->{ $upcoming->{branchcode} }->{ $upcoming->{borrowernumber} }->{email} = $from_address;
338 $upcoming_digest->{ $upcoming->{branchcode} }->{ $upcoming->{borrowernumber} }->{count}++;
340 $upcoming_digest->{ $upcoming->{borrowernumber} }->{email} = $from_address;
341 $upcoming_digest->{ $upcoming->{borrowernumber} }->{count}++;
345 if($owning_library) {
346 $branchcode = $upcoming->{'homebranch'};
348 $branchcode = $upcoming->{'branchcode'};
350 # Skip this PREDUE if we specify list of libraries and this one is not part of it
351 next if (@branchcodes && !$branches{$branchcode});
353 my $item = Koha::Items->find( $upcoming->{itemnumber} );
354 my $letter_type = 'PREDUE';
355 $sth->execute($upcoming->{'borrowernumber'},$upcoming->{'itemnumber'},$borrower_preferences->{'days_in_advance'});
357 while ( my $item_info = $sth->fetchrow_hashref()) {
358 $titles .= C4::Letters::get_item_content( { item => $item_info, item_content_fields => \@item_content_fields } );
361 ## Get branch info for borrowers home library.
362 foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) {
363 my $letter = parse_letter( { letter_code => $letter_type,
364 borrowernumber => $upcoming->{'borrowernumber'},
365 branchcode => $branchcode,
366 biblionumber => $item->biblionumber,
367 itemnumber => $upcoming->{'itemnumber'},
368 substitute => { 'items.content' => $titles },
369 message_transport_type => $transport,
371 or warn "no letter of type '$letter_type' found for borrowernumber ".$upcoming->{'borrowernumber'}.". Please see sample_notices.sql";
372 push @letters, $letter if $letter;
377 # If we have prepared a letter, send it.
380 for my $letter ( @letters ) {
382 print $letter->{'content'};
386 for my $letter ( @letters ) {
387 C4::Letters::EnqueueLetter( { letter => $letter,
388 borrowernumber => $upcoming->{'borrowernumber'},
389 from_address => $from_address,
390 message_transport_type => $letter->{message_transport_type} } );
398 # Now, run through all the people that want digests and send them
400 my $sth_digest = $dbh->prepare(<<'END_SQL');
401 SELECT biblio.*, items.*, issues.*
402 FROM issues,items,biblio
403 WHERE items.itemnumber=issues.itemnumber
404 AND biblio.biblionumber=items.biblionumber
405 AND issues.borrowernumber = ?
406 AND (TO_DAYS(date_due)-TO_DAYS(NOW()) = ?)
409 if ($digest_per_branch) {
410 while (my ($branchcode, $digests) = each %$upcoming_digest) {
414 letter_code => 'PREDUEDGST',
415 message_name => 'advance_notice',
416 branchcode => $branchcode,
417 get_item_info => sub {
419 $params->{sth}->execute($params->{borrowernumber},
420 $params->{borrower_preferences}->{'days_in_advance'});
422 $params->{sth}->fetchrow_hashref;
428 while (my ($branchcode, $digests) = each %$due_digest) {
431 digests => $due_digest,
432 letter_code => 'DUEDGST',
433 branchcode => $branchcode,
434 message_name => 'item_due',
435 get_item_info => sub {
437 $params->{sth}->execute($params->{borrowernumber}, 0);
439 $params->{sth}->fetchrow_hashref;
447 digests => $upcoming_digest,
448 letter_code => 'PREDUEDGST',
449 message_name => 'advance_notice',
450 get_item_info => sub {
452 $params->{sth}->execute($params->{borrowernumber},
453 $params->{borrower_preferences}->{'days_in_advance'});
455 $params->{sth}->fetchrow_hashref;
462 digests => $due_digest,
463 letter_code => 'DUEDGST',
464 message_name => 'item_due',
465 get_item_info => sub {
467 $params->{sth}->execute($params->{borrowernumber}, 0);
469 $params->{sth}->fetchrow_hashref;
484 foreach my $required ( qw( letter_code borrowernumber ) ) {
485 return unless exists $params->{$required};
487 my $patron = Koha::Patrons->find( $params->{borrowernumber} );
489 my %table_params = ( 'borrowers' => $params->{'borrowernumber'} );
491 if ( my $p = $params->{'branchcode'} ) {
492 $table_params{'branches'} = $p;
494 if ( my $p = $params->{'itemnumber'} ) {
495 $table_params{'issues'} = $p;
496 $table_params{'items'} = $p;
498 if ( my $p = $params->{'biblionumber'} ) {
499 $table_params{'biblio'} = $p;
500 $table_params{'biblioitems'} = $p;
503 return C4::Letters::GetPreparedLetter (
504 module => 'circulation',
505 letter_code => $params->{'letter_code'},
506 branchcode => $table_params{'branches'},
507 lang => $patron->lang,
508 substitute => $params->{'substitute'},
509 tables => \%table_params,
510 message_transport_type => $params->{message_transport_type},
514 =head2 get_branch_info
518 sub get_branch_info {
519 my ( $borrowernumber ) = @_;
521 ## Get branch info for borrowers home library.
522 my $patron = Koha::Patrons->find( $borrowernumber );
523 my $branch = $patron->library->unblessed;
525 foreach my $key( keys %$branch ) {
526 $branch_info{"branches.$key"} = $branch->{$key};
538 get_item_info => ...,
541 Enqueue digested letters (or print them if -n was passed at command line).
549 Reference to the array of digested messages.
553 Prepared statement handle for fetching overdue issues.
555 =item C<$letter_code>
557 String that denote the letter code.
559 =item C<$get_item_info>
561 Subroutine for executing prepared statement. Takes parameters $sth,
562 $borrowernumber and $borrower_parameters and return a generator
563 function that produce the matching rows.
572 PATRON: while ( my ( $borrowernumber, $digest ) = each %{$params->{digests}} ) {
574 my $count = $digest->{count};
575 my $from_address = $digest->{email};
580 if (defined($params->{branchcode})) {
582 $branchcode = $params->{branchcode};
584 ## Get branch info for borrowers home library.
585 %branch_info = get_branch_info( $borrowernumber );
586 $branchcode = $branch_info{'branches.branchcode'};
589 my $borrower_preferences =
590 C4::Members::Messaging::GetMessagingPreferences(
592 borrowernumber => $borrowernumber,
593 message_name => $params->{message_name}
597 next PATRON unless $borrower_preferences; # how could this happen?
599 my $next_item_info = $params->{get_item_info}->({
600 sth => $params->{sth},
601 borrowernumber => $borrowernumber,
602 borrower_preferences => $borrower_preferences
605 while ( my $item_info = $next_item_info->()) {
606 $titles .= C4::Letters::get_item_content( { item => $item_info, item_content_fields => \@item_content_fields } );
609 foreach my $transport ( keys %{ $borrower_preferences->{'transports'} } ) {
610 my $letter = parse_letter(
612 letter_code => $params->{letter_code},
613 borrowernumber => $borrowernumber,
616 'items.content' => $titles,
619 branchcode => $branchcode,
620 message_transport_type => $transport
624 warn "no letter of type '$params->{letter_type}' found for borrowernumber $borrowernumber. Please see sample_notices.sql";
627 push @letters, $letter if $letter;
632 for my $letter ( @letters ) {
634 print $letter->{'content'};
638 for my $letter ( @letters ) {
639 C4::Letters::EnqueueLetter( { letter => $letter,
640 borrowernumber => $borrowernumber,
641 from_address => $from_address,
642 message_transport_type => $letter->{message_transport_type} } );