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
45 # find Koha's Perl modules
46 # test carefully before changing this
48 eval { require "$FindBin::Bin/../kohalib.pl" };
50 use Koha::Script -cron;
55 use C4::Members::Messaging;
69 Print a brief help message and exits.
73 Prints the manual page and exits.
77 Verbose. Without this flag set, only fatal errors are reported.
81 Do not send any email. Advanced or due notices that would have been sent to
82 the patrons are printed to standard out.
86 Defines the maximum number of days in advance to send advance notices.
90 Confirm flag: Add this option. The script will only print a usage
93 =item B<--itemscontent>
95 comma separated list of fields that get substituted into templates in
96 places of the E<lt>E<lt>items.contentE<gt>E<gt> placeholder. This
97 defaults to date_due,title,author,barcode
99 Other possible values come from fields in the biblios, items and
102 =item B<--digest-per-branch>
104 Flag to indicate that generation of message digests should be
105 performed separately for each branch.
107 A patron could potentially have loans at several different branches
108 There is no natural branch to set as the sender on the aggregated
109 message in this situation so the default behavior is to use the
110 borrowers home branch. This could surprise to the borrower when
111 message sender is a library where they have not borrowed anything.
113 Enabling this flag ensures that the issuing library is the sender of
114 the digested message. It has no effect unless the borrower has
115 chosen 'Digests only' on the advance messages.
119 select notices for one specific library. Use the value in the
120 branches.branchcode table. This option can be repeated in order
121 to select notices for a group of libraries.
123 =item B<--frombranch>
125 Set the from address for the notice to one of 'item-homebranch' or 'item-issuebranch'.
127 Defaults to 'item-issuebranch'
133 This script pays attention to the advanced notice configuration
134 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
135 section of Koha is available in the Koha manual.
137 =head2 Outgoing emails
139 Typically, messages are prepared for each patron with due
140 items, and who have selected (or the library has elected for them) Advance or Due notices.
142 These emails are staged in the outgoing message queue, as are messages
143 produced by other features of Koha. This message queue must be
144 processed regularly by the
145 F<misc/cronjobs/process_message_queue.pl> program.
147 In the event that the C<-n> flag is passed to this program, no emails
148 are sent. Instead, messages are sent on standard output from this
149 program. They may be redirected to a file if desired.
153 Templates can contain variables enclosed in double angle brackets like
154 E<lt>E<lt>thisE<gt>E<gt>. Those variables will be replaced with values
155 specific to the overdue items or relevant patron. Available variables
160 =item E<lt>E<lt>items.contentE<gt>E<gt>
162 one line for each item, each line containing a tab separated list of
163 date due, title, author, barcode
165 =item E<lt>E<lt>borrowers.*E<gt>E<gt>
167 any field from the borrowers table
169 =item E<lt>E<lt>branches.*E<gt>E<gt>
171 any field from the branches table
177 The F<misc/cronjobs/overdue_notices.pl> program allows you to send
178 messages to patrons when their messages are overdue.
182 binmode( STDOUT, ':encoding(UTF-8)' );
184 # These are defaults for command line options.
185 my $confirm; # -c: Confirm that the user has read and configured this script.
186 my $nomail; # -n: No mail. Will not send any emails.
187 my $mindays = 0; # -m: Maximum number of days in advance to send notices
188 my $maxdays = 30; # -e: the End of the time period
189 my $verbose = 0; # -v: verbose
190 my $digest_per_branch = 0; # -digest-per-branch: Prepare and send digests per branch
191 my @branchcodes; # Branch(es) passed as parameter
192 my $frombranch = 'item-issuebranch';
193 my $itemscontent = join(',',qw( date_due title author barcode ));
201 'library=s' => \@branchcodes,
202 'frombranch=s' => \$frombranch,
207 'digest-per-branch' => \$digest_per_branch,
208 'itemscontent=s' => \$itemscontent,
210 pod2usage(1) if $help;
211 pod2usage( -verbose => 2 ) if $man;
213 # Since advance notice options are not visible in the web-interface
214 # unless EnhancedMessagingPreferences is on, let the user know that
215 # this script probably isn't going to do much
216 if ( ! C4::Context->preference('EnhancedMessagingPreferences') ) {
219 The "EnhancedMessagingPreferences" syspref is off.
220 Therefore, it is unlikely that this script will actually produce any messages to be sent.
221 To change this, edit the "EnhancedMessagingPreferences" syspref.
232 %branches = map { $_ => 1 } @branchcodes;
235 die "--frombranch takes item-homebranch or item-issuebranch only"
236 unless ( $frombranch eq 'item-issuebranch'
237 || $frombranch eq 'item-homebranch' );
238 my $owning_library = ( $frombranch eq 'item-homebranch' ) ? 1 : 0;
240 # The fields that will be substituted into <<items.content>>
241 my @item_content_fields = split(/,/,$itemscontent);
243 warn 'getting upcoming due issues' if $verbose;
244 my $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( {
245 days_in_advance => $maxdays,
246 owning_library => $owning_library
248 warn 'found ' . scalar( @$upcoming_dues ) . ' issues' if $verbose;
250 # hash of borrowernumber to number of items upcoming
251 # for patrons wishing digests only.
252 my $upcoming_digest = {};
255 my $dbh = C4::Context->dbh();
256 my $sth = $dbh->prepare(<<'END_SQL');
257 SELECT biblio.*, items.*, issues.*
258 FROM issues,items,biblio
259 WHERE items.itemnumber=issues.itemnumber
260 AND biblio.biblionumber=items.biblionumber
261 AND issues.borrowernumber = ?
262 AND issues.itemnumber = ?
263 AND (TO_DAYS(date_due)-TO_DAYS(NOW()) = ?)
266 my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
269 UPCOMINGITEM: foreach my $upcoming ( @$upcoming_dues ) {
271 warn 'examining ' . $upcoming->{'itemnumber'} . ' upcoming due items' if $verbose;
273 my $from_address = $upcoming->{branchemail} || $admin_adress;
275 my $borrower_preferences;
276 if ( 0 == $upcoming->{'days_until_due'} ) {
277 # This item is due today. Send an 'item due' message.
278 $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $upcoming->{'borrowernumber'},
279 message_name => 'item_due' } );
280 next unless $borrower_preferences;
282 if ( $borrower_preferences->{'wants_digest'} ) {
283 # cache this one to process after we've run through all of the items.
284 if ($digest_per_branch) {
285 $due_digest->{ $upcoming->{branchcode} }->{ $upcoming->{borrowernumber} }->{email} = $from_address;
286 $due_digest->{ $upcoming->{branchcode} }->{ $upcoming->{borrowernumber} }->{count}++;
288 $due_digest->{ $upcoming->{borrowernumber} }->{email} = $from_address;
289 $due_digest->{ $upcoming->{borrowernumber} }->{count}++;
293 if($owning_library) {
294 $branchcode = $upcoming->{'homebranch'};
296 $branchcode = $upcoming->{'branchcode'};
298 # Skip this DUE if we specify list of libraries and this one is not part of it
299 next if (@branchcodes && !$branches{$branchcode});
301 my $item = Koha::Items->find( $upcoming->{itemnumber} );
302 my $letter_type = 'DUE';
303 $sth->execute($upcoming->{'borrowernumber'},$upcoming->{'itemnumber'},'0');
305 while ( my $item_info = $sth->fetchrow_hashref()) {
306 $titles .= C4::Letters::get_item_content( { item => $item_info, item_content_fields => \@item_content_fields } );
309 ## Get branch info for borrowers home library.
310 foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) {
311 my $letter = parse_letter( { letter_code => $letter_type,
312 borrowernumber => $upcoming->{'borrowernumber'},
313 branchcode => $branchcode,
314 biblionumber => $item->biblionumber,
315 itemnumber => $upcoming->{'itemnumber'},
316 substitute => { 'items.content' => $titles },
317 message_transport_type => $transport,
319 or warn "no letter of type '$letter_type' found for borrowernumber ".$upcoming->{'borrowernumber'}.". Please see sample_notices.sql";
320 push @letters, $letter if $letter;
324 $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $upcoming->{'borrowernumber'},
325 message_name => 'advance_notice' } );
326 next UPCOMINGITEM unless $borrower_preferences && exists $borrower_preferences->{'days_in_advance'};
327 next UPCOMINGITEM unless $borrower_preferences->{'days_in_advance'} == $upcoming->{'days_until_due'};
329 if ( $borrower_preferences->{'wants_digest'} ) {
330 # cache this one to process after we've run through all of the items.
331 if ($digest_per_branch) {
332 $upcoming_digest->{ $upcoming->{branchcode} }->{ $upcoming->{borrowernumber} }->{email} = $from_address;
333 $upcoming_digest->{ $upcoming->{branchcode} }->{ $upcoming->{borrowernumber} }->{count}++;
335 $upcoming_digest->{ $upcoming->{borrowernumber} }->{email} = $from_address;
336 $upcoming_digest->{ $upcoming->{borrowernumber} }->{count}++;
340 if($owning_library) {
341 $branchcode = $upcoming->{'homebranch'};
343 $branchcode = $upcoming->{'branchcode'};
345 # Skip this PREDUE if we specify list of libraries and this one is not part of it
346 next if (@branchcodes && !$branches{$branchcode});
348 my $item = Koha::Items->find( $upcoming->{itemnumber} );
349 my $letter_type = 'PREDUE';
350 $sth->execute($upcoming->{'borrowernumber'},$upcoming->{'itemnumber'},$borrower_preferences->{'days_in_advance'});
352 while ( my $item_info = $sth->fetchrow_hashref()) {
353 $titles .= C4::Letters::get_item_content( { item => $item_info, item_content_fields => \@item_content_fields } );
356 ## Get branch info for borrowers home library.
357 foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) {
358 my $letter = parse_letter( { letter_code => $letter_type,
359 borrowernumber => $upcoming->{'borrowernumber'},
360 branchcode => $branchcode,
361 biblionumber => $item->biblionumber,
362 itemnumber => $upcoming->{'itemnumber'},
363 substitute => { 'items.content' => $titles },
364 message_transport_type => $transport,
366 or warn "no letter of type '$letter_type' found for borrowernumber ".$upcoming->{'borrowernumber'}.". Please see sample_notices.sql";
367 push @letters, $letter if $letter;
372 # If we have prepared a letter, send it.
375 for my $letter ( @letters ) {
377 print $letter->{'content'}."\n";
381 for my $letter ( @letters ) {
382 C4::Letters::EnqueueLetter( { letter => $letter,
383 borrowernumber => $upcoming->{'borrowernumber'},
384 from_address => $from_address,
385 message_transport_type => $letter->{message_transport_type} } );
393 # Now, run through all the people that want digests and send them
395 my $sth_digest = $dbh->prepare(<<'END_SQL');
396 SELECT biblio.*, items.*, issues.*
397 FROM issues,items,biblio
398 WHERE items.itemnumber=issues.itemnumber
399 AND biblio.biblionumber=items.biblionumber
400 AND issues.borrowernumber = ?
401 AND (TO_DAYS(date_due)-TO_DAYS(NOW()) = ?)
404 if ($digest_per_branch) {
405 while (my ($branchcode, $digests) = each %$upcoming_digest) {
409 letter_code => 'PREDUEDGST',
410 message_name => 'advance_notice',
411 branchcode => $branchcode,
412 get_item_info => sub {
414 $params->{sth}->execute($params->{borrowernumber},
415 $params->{borrower_preferences}->{'days_in_advance'});
417 $params->{sth}->fetchrow_hashref;
423 while (my ($branchcode, $digests) = each %$due_digest) {
426 digests => $due_digest,
427 letter_code => 'DUEDGST',
428 branchcode => $branchcode,
429 message_name => 'item_due',
430 get_item_info => sub {
432 $params->{sth}->execute($params->{borrowernumber}, 0);
434 $params->{sth}->fetchrow_hashref;
442 digests => $upcoming_digest,
443 letter_code => 'PREDUEDGST',
444 message_name => 'advance_notice',
445 get_item_info => sub {
447 $params->{sth}->execute($params->{borrowernumber},
448 $params->{borrower_preferences}->{'days_in_advance'});
450 $params->{sth}->fetchrow_hashref;
457 digests => $due_digest,
458 letter_code => 'DUEDGST',
459 message_name => 'item_due',
460 get_item_info => sub {
462 $params->{sth}->execute($params->{borrowernumber}, 0);
464 $params->{sth}->fetchrow_hashref;
479 foreach my $required ( qw( letter_code borrowernumber ) ) {
480 return unless exists $params->{$required};
482 my $patron = Koha::Patrons->find( $params->{borrowernumber} );
484 my %table_params = ( 'borrowers' => $params->{'borrowernumber'} );
486 if ( my $p = $params->{'branchcode'} ) {
487 $table_params{'branches'} = $p;
489 if ( my $p = $params->{'itemnumber'} ) {
490 $table_params{'issues'} = $p;
491 $table_params{'items'} = $p;
493 if ( my $p = $params->{'biblionumber'} ) {
494 $table_params{'biblio'} = $p;
495 $table_params{'biblioitems'} = $p;
498 return C4::Letters::GetPreparedLetter (
499 module => 'circulation',
500 letter_code => $params->{'letter_code'},
501 branchcode => $table_params{'branches'},
502 lang => $patron->lang,
503 substitute => $params->{'substitute'},
504 tables => \%table_params,
505 message_transport_type => $params->{message_transport_type},
509 =head2 get_branch_info
513 sub get_branch_info {
514 my ( $borrowernumber ) = @_;
516 ## Get branch info for borrowers home library.
517 my $patron = Koha::Patrons->find( $borrowernumber );
518 my $branch = $patron->library->unblessed;
520 foreach my $key( keys %$branch ) {
521 $branch_info{"branches.$key"} = $branch->{$key};
533 get_item_info => ...,
536 Enqueue digested letters (or print them if -n was passed at command line).
544 Reference to the array of digested messages.
548 Prepared statement handle for fetching overdue issues.
550 =item C<$letter_code>
552 String that denote the letter code.
554 =item C<$get_item_info>
556 Subroutine for executing prepared statement. Takes parameters $sth,
557 $borrowernumber and $borrower_parameters and return a generator
558 function that produce the matching rows.
567 PATRON: while ( my ( $borrowernumber, $digest ) = each %{$params->{digests}} ) {
569 my $count = $digest->{count};
570 my $from_address = $digest->{email};
575 if (defined($params->{branchcode})) {
577 $branchcode = $params->{branchcode};
579 ## Get branch info for borrowers home library.
580 %branch_info = get_branch_info( $borrowernumber );
581 $branchcode = $branch_info{'branches.branchcode'};
584 my $borrower_preferences =
585 C4::Members::Messaging::GetMessagingPreferences(
587 borrowernumber => $borrowernumber,
588 message_name => $params->{message_name}
592 next PATRON unless $borrower_preferences; # how could this happen?
594 my $next_item_info = $params->{get_item_info}->({
595 sth => $params->{sth},
596 borrowernumber => $borrowernumber,
597 borrower_preferences => $borrower_preferences
600 while ( my $item_info = $next_item_info->()) {
601 $titles .= C4::Letters::get_item_content( { item => $item_info, item_content_fields => \@item_content_fields } );
604 foreach my $transport ( keys %{ $borrower_preferences->{'transports'} } ) {
605 my $letter = parse_letter(
607 letter_code => $params->{letter_code},
608 borrowernumber => $borrowernumber,
611 'items.content' => $titles,
614 branchcode => $branchcode,
615 message_transport_type => $transport
619 warn "no letter of type '$params->{letter_type}' found for borrowernumber $borrowernumber. Please see sample_notices.sql";
622 push @letters, $letter if $letter;
627 for my $letter ( @letters ) {
629 print $letter->{'content'};
633 for my $letter ( @letters ) {
634 C4::Letters::EnqueueLetter( { letter => $letter,
635 borrowernumber => $borrowernumber,
636 from_address => $from_address,
637 message_transport_type => $letter->{message_transport_type} } );