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 - prepare messages to be sent to patrons for nearly due, or due, items
27 [ -n ][ -m <number of days> ][ --itemscontent <comma separated field list> ][ -c ]
31 This script prepares pre-due and item due reminders to be sent to
32 patrons. It queues them in the message queue, which is processed by
33 the process_message_queue.pl cronjob. The type and timing of the
34 messages can be configured by the patrons in their "My Alerts" tab in
41 use Getopt::Long qw( GetOptions );
42 use Pod::Usage qw( pod2usage );
44 # find Koha's Perl modules
45 # test carefully before changing this
47 eval { require "$FindBin::Bin/../kohalib.pl" };
49 use Koha::Script -cron;
53 use C4::Members::Messaging;
54 use C4::Log qw( cronlogaction );
65 Print a brief help message and exits.
69 Prints the manual page and exits.
73 Verbose. Without this flag set, only fatal errors are reported.
77 Do not send any email. Advanced or due notices that would have been sent to
78 the patrons are printed to standard out.
82 Defines the maximum number of days in advance to send advance notices.
86 Confirm flag: Add this option. The script will only print a usage
89 =item B<--itemscontent>
91 comma separated list of fields that get substituted into templates in
92 places of the E<lt>E<lt>items.contentE<gt>E<gt> placeholder. This
93 defaults to date_due,title,author,barcode
95 Other possible values come from fields in the biblios, items and
98 =item B<--digest-per-branch>
100 Flag to indicate that generation of message digests should be
101 performed separately for each branch.
103 A patron could potentially have loans at several different branches
104 There is no natural branch to set as the sender on the aggregated
105 message in this situation so the default behavior is to use the
106 borrowers home branch. This could surprise to the borrower when
107 message sender is a library where they have not borrowed anything.
109 Enabling this flag ensures that the issuing library is the sender of
110 the digested message. It has no effect unless the borrower has
111 chosen 'Digests only' on the advance messages.
115 select notices for one specific library. Use the value in the
116 branches.branchcode table. This option can be repeated in order
117 to select notices for a group of libraries.
119 =item B<--frombranch>
121 Set the from address for the notice to one of 'item-homebranch' or 'item-issuebranch'.
123 Defaults to 'item-issuebranch'
129 This script pays attention to the advanced notice configuration
130 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
131 section of Koha is available in the Koha manual.
133 =head2 Outgoing emails
135 Typically, messages are prepared for each patron with due
136 items, and who have selected (or the library has elected for them) Advance or Due notices.
138 These emails are staged in the outgoing message queue, as are messages
139 produced by other features of Koha. This message queue must be
140 processed regularly by the
141 F<misc/cronjobs/process_message_queue.pl> program.
143 In the event that the C<-n> flag is passed to this program, no emails
144 are sent. Instead, messages are sent on standard output from this
145 program. They may be redirected to a file if desired.
149 Templates can contain variables enclosed in double angle brackets like
150 E<lt>E<lt>thisE<gt>E<gt>. Those variables will be replaced with values
151 specific to the overdue items or relevant patron. Available variables
156 =item E<lt>E<lt>items.contentE<gt>E<gt>
158 one line for each item, each line containing a tab separated list of
159 date due, title, author, barcode
161 =item E<lt>E<lt>borrowers.*E<gt>E<gt>
163 any field from the borrowers table
165 =item E<lt>E<lt>branches.*E<gt>E<gt>
167 any field from the branches table
173 The F<misc/cronjobs/overdue_notices.pl> program allows you to send
174 messages to patrons when their messages are overdue.
178 binmode( STDOUT, ':encoding(UTF-8)' );
180 # These are defaults for command line options.
181 my $confirm; # -c: Confirm that the user has read and configured this script.
182 my $nomail; # -n: No mail. Will not send any emails.
183 my $mindays = 0; # -m: Maximum number of days in advance to send notices
184 my $maxdays = 30; # -e: the End of the time period
185 my $verbose = 0; # -v: verbose
186 my $digest_per_branch = 0; # -digest-per-branch: Prepare and send digests per branch
187 my @branchcodes; # Branch(es) passed as parameter
188 my $frombranch = 'item-issuebranch';
189 my $itemscontent = join(',',qw( date_due title author barcode ));
197 'library=s' => \@branchcodes,
198 'frombranch=s' => \$frombranch,
203 'digest-per-branch' => \$digest_per_branch,
204 'itemscontent=s' => \$itemscontent,
206 pod2usage(1) if $help;
207 pod2usage( -verbose => 2 ) if $man;
209 # Since advance notice options are not visible in the web-interface
210 # unless EnhancedMessagingPreferences is on, let the user know that
211 # this script probably isn't going to do much
212 if ( ! C4::Context->preference('EnhancedMessagingPreferences') ) {
215 The "EnhancedMessagingPreferences" syspref is off.
216 Therefore, it is unlikely that this script will actually produce any messages to be sent.
217 To change this, edit the "EnhancedMessagingPreferences" syspref.
228 %branches = map { $_ => 1 } @branchcodes;
231 die "--frombranch takes item-homebranch or item-issuebranch only"
232 unless ( $frombranch eq 'item-issuebranch'
233 || $frombranch eq 'item-homebranch' );
234 my $owning_library = ( $frombranch eq 'item-homebranch' ) ? 1 : 0;
236 # The fields that will be substituted into <<items.content>>
237 my @item_content_fields = split(/,/,$itemscontent);
239 warn 'getting upcoming due issues' if $verbose;
240 my $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( {
241 days_in_advance => $maxdays,
242 owning_library => $owning_library
244 warn 'found ' . scalar( @$upcoming_dues ) . ' issues' if $verbose;
246 # hash of borrowernumber to number of items upcoming
247 # for patrons wishing digests only.
248 my $upcoming_digest = {};
251 my $dbh = C4::Context->dbh();
252 my $sth = $dbh->prepare(<<'END_SQL');
253 SELECT biblio.*, items.*, issues.*
254 FROM issues,items,biblio
255 WHERE items.itemnumber=issues.itemnumber
256 AND biblio.biblionumber=items.biblionumber
257 AND issues.borrowernumber = ?
258 AND issues.itemnumber = ?
259 AND (TO_DAYS(date_due)-TO_DAYS(NOW()) = ?)
262 my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
265 UPCOMINGITEM: foreach my $upcoming ( @$upcoming_dues ) {
267 warn 'examining ' . $upcoming->{'itemnumber'} . ' upcoming due items' if $verbose;
269 my $from_address = $upcoming->{branchemail} || $admin_adress;
271 my $borrower_preferences;
272 if ( 0 == $upcoming->{'days_until_due'} ) {
273 # This item is due today. Send an 'item due' message.
274 $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $upcoming->{'borrowernumber'},
275 message_name => 'item_due' } );
276 next unless $borrower_preferences;
278 if ( $borrower_preferences->{'wants_digest'} ) {
279 # cache this one to process after we've run through all of the items.
280 if ($digest_per_branch) {
281 $due_digest->{ $upcoming->{branchcode} }->{ $upcoming->{borrowernumber} }->{email} = $from_address;
282 $due_digest->{ $upcoming->{branchcode} }->{ $upcoming->{borrowernumber} }->{count}++;
284 $due_digest->{ $upcoming->{borrowernumber} }->{email} = $from_address;
285 $due_digest->{ $upcoming->{borrowernumber} }->{count}++;
289 if($owning_library) {
290 $branchcode = $upcoming->{'homebranch'};
292 $branchcode = $upcoming->{'branchcode'};
294 # Skip this DUE if we specify list of libraries and this one is not part of it
295 next if (@branchcodes && !$branches{$branchcode});
297 my $item = Koha::Items->find( $upcoming->{itemnumber} );
298 my $letter_type = 'DUE';
299 $sth->execute($upcoming->{'borrowernumber'},$upcoming->{'itemnumber'},'0');
301 while ( my $item_info = $sth->fetchrow_hashref()) {
302 $titles .= C4::Letters::get_item_content( { item => $item_info, item_content_fields => \@item_content_fields } );
305 ## Get branch info for borrowers home library.
306 foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) {
307 my $letter = parse_letter( { letter_code => $letter_type,
308 borrowernumber => $upcoming->{'borrowernumber'},
309 branchcode => $branchcode,
310 biblionumber => $item->biblionumber,
311 itemnumber => $upcoming->{'itemnumber'},
312 substitute => { 'items.content' => $titles },
313 message_transport_type => $transport,
315 or warn "no letter of type '$letter_type' found for borrowernumber ".$upcoming->{'borrowernumber'}.". Please see sample_notices.sql";
316 push @letters, $letter if $letter;
320 $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $upcoming->{'borrowernumber'},
321 message_name => 'advance_notice' } );
322 next UPCOMINGITEM unless $borrower_preferences && exists $borrower_preferences->{'days_in_advance'};
323 next UPCOMINGITEM unless $borrower_preferences->{'days_in_advance'} == $upcoming->{'days_until_due'};
325 if ( $borrower_preferences->{'wants_digest'} ) {
326 # cache this one to process after we've run through all of the items.
327 if ($digest_per_branch) {
328 $upcoming_digest->{ $upcoming->{branchcode} }->{ $upcoming->{borrowernumber} }->{email} = $from_address;
329 $upcoming_digest->{ $upcoming->{branchcode} }->{ $upcoming->{borrowernumber} }->{count}++;
331 $upcoming_digest->{ $upcoming->{borrowernumber} }->{email} = $from_address;
332 $upcoming_digest->{ $upcoming->{borrowernumber} }->{count}++;
336 if($owning_library) {
337 $branchcode = $upcoming->{'homebranch'};
339 $branchcode = $upcoming->{'branchcode'};
341 # Skip this PREDUE if we specify list of libraries and this one is not part of it
342 next if (@branchcodes && !$branches{$branchcode});
344 my $item = Koha::Items->find( $upcoming->{itemnumber} );
345 my $letter_type = 'PREDUE';
346 $sth->execute($upcoming->{'borrowernumber'},$upcoming->{'itemnumber'},$borrower_preferences->{'days_in_advance'});
348 while ( my $item_info = $sth->fetchrow_hashref()) {
349 $titles .= C4::Letters::get_item_content( { item => $item_info, item_content_fields => \@item_content_fields } );
352 ## Get branch info for borrowers home library.
353 foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) {
354 my $letter = parse_letter( { letter_code => $letter_type,
355 borrowernumber => $upcoming->{'borrowernumber'},
356 branchcode => $branchcode,
357 biblionumber => $item->biblionumber,
358 itemnumber => $upcoming->{'itemnumber'},
359 substitute => { 'items.content' => $titles },
360 message_transport_type => $transport,
362 or warn "no letter of type '$letter_type' found for borrowernumber ".$upcoming->{'borrowernumber'}.". Please see sample_notices.sql";
363 push @letters, $letter if $letter;
368 # If we have prepared a letter, send it.
371 for my $letter ( @letters ) {
373 print $letter->{'content'}."\n";
377 for my $letter ( @letters ) {
378 C4::Letters::EnqueueLetter( { letter => $letter,
379 borrowernumber => $upcoming->{'borrowernumber'},
380 from_address => $from_address,
381 message_transport_type => $letter->{message_transport_type} } );
389 # Now, run through all the people that want digests and send them
391 my $sth_digest = $dbh->prepare(<<'END_SQL');
392 SELECT biblio.*, items.*, issues.*
393 FROM issues,items,biblio
394 WHERE items.itemnumber=issues.itemnumber
395 AND biblio.biblionumber=items.biblionumber
396 AND issues.borrowernumber = ?
397 AND (TO_DAYS(date_due)-TO_DAYS(NOW()) = ?)
400 if ($digest_per_branch) {
401 while (my ($branchcode, $digests) = each %$upcoming_digest) {
405 letter_code => 'PREDUEDGST',
406 message_name => 'advance_notice',
407 branchcode => $branchcode,
408 get_item_info => sub {
410 $params->{sth}->execute($params->{borrowernumber},
411 $params->{borrower_preferences}->{'days_in_advance'});
413 $params->{sth}->fetchrow_hashref;
419 while (my ($branchcode, $digests) = each %$due_digest) {
422 digests => $due_digest,
423 letter_code => 'DUEDGST',
424 branchcode => $branchcode,
425 message_name => 'item_due',
426 get_item_info => sub {
428 $params->{sth}->execute($params->{borrowernumber}, 0);
430 $params->{sth}->fetchrow_hashref;
438 digests => $upcoming_digest,
439 letter_code => 'PREDUEDGST',
440 message_name => 'advance_notice',
441 get_item_info => sub {
443 $params->{sth}->execute($params->{borrowernumber},
444 $params->{borrower_preferences}->{'days_in_advance'});
446 $params->{sth}->fetchrow_hashref;
453 digests => $due_digest,
454 letter_code => 'DUEDGST',
455 message_name => 'item_due',
456 get_item_info => sub {
458 $params->{sth}->execute($params->{borrowernumber}, 0);
460 $params->{sth}->fetchrow_hashref;
475 foreach my $required ( qw( letter_code borrowernumber ) ) {
476 return unless exists $params->{$required};
478 my $patron = Koha::Patrons->find( $params->{borrowernumber} );
480 my %table_params = ( 'borrowers' => $params->{'borrowernumber'} );
482 if ( my $p = $params->{'branchcode'} ) {
483 $table_params{'branches'} = $p;
485 if ( my $p = $params->{'itemnumber'} ) {
486 $table_params{'issues'} = $p;
487 $table_params{'items'} = $p;
489 if ( my $p = $params->{'biblionumber'} ) {
490 $table_params{'biblio'} = $p;
491 $table_params{'biblioitems'} = $p;
494 return C4::Letters::GetPreparedLetter (
495 module => 'circulation',
496 letter_code => $params->{'letter_code'},
497 branchcode => $table_params{'branches'},
498 lang => $patron->lang,
499 substitute => $params->{'substitute'},
500 tables => \%table_params,
501 message_transport_type => $params->{message_transport_type},
505 =head2 get_branch_info
509 sub get_branch_info {
510 my ( $borrowernumber ) = @_;
512 ## Get branch info for borrowers home library.
513 my $patron = Koha::Patrons->find( $borrowernumber );
514 my $branch = $patron->library->unblessed;
516 foreach my $key( keys %$branch ) {
517 $branch_info{"branches.$key"} = $branch->{$key};
529 get_item_info => ...,
532 Enqueue digested letters (or print them if -n was passed at command line).
540 Reference to the array of digested messages.
544 Prepared statement handle for fetching overdue issues.
546 =item C<$letter_code>
548 String that denote the letter code.
550 =item C<$get_item_info>
552 Subroutine for executing prepared statement. Takes parameters $sth,
553 $borrowernumber and $borrower_parameters and return a generator
554 function that produce the matching rows.
563 PATRON: while ( my ( $borrowernumber, $digest ) = each %{$params->{digests}} ) {
565 my $count = $digest->{count};
566 my $from_address = $digest->{email};
571 if (defined($params->{branchcode})) {
573 $branchcode = $params->{branchcode};
575 ## Get branch info for borrowers home library.
576 %branch_info = get_branch_info( $borrowernumber );
577 $branchcode = $branch_info{'branches.branchcode'};
580 my $borrower_preferences =
581 C4::Members::Messaging::GetMessagingPreferences(
583 borrowernumber => $borrowernumber,
584 message_name => $params->{message_name}
588 next PATRON unless $borrower_preferences; # how could this happen?
590 my $next_item_info = $params->{get_item_info}->({
591 sth => $params->{sth},
592 borrowernumber => $borrowernumber,
593 borrower_preferences => $borrower_preferences
596 while ( my $item_info = $next_item_info->()) {
597 $titles .= C4::Letters::get_item_content( { item => $item_info, item_content_fields => \@item_content_fields } );
600 foreach my $transport ( keys %{ $borrower_preferences->{'transports'} } ) {
601 my $letter = parse_letter(
603 letter_code => $params->{letter_code},
604 borrowernumber => $borrowernumber,
607 'items.content' => $titles,
610 branchcode => $branchcode,
611 message_transport_type => $transport
615 warn "no letter of type '$params->{letter_type}' found for borrowernumber $borrowernumber. Please see sample_notices.sql";
618 push @letters, $letter if $letter;
623 for my $letter ( @letters ) {
625 print $letter->{'content'};
629 for my $letter ( @letters ) {
630 C4::Letters::EnqueueLetter( { letter => $letter,
631 borrowernumber => $borrowernumber,
632 from_address => $from_address,
633 message_transport_type => $letter->{message_transport_type} } );