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
47 # find Koha's Perl modules
48 # test carefully before changing this
50 eval { require "$FindBin::Bin/../kohalib.pl" };
56 use C4::Members::Messaging;
62 advance_notices.pl - prepare messages to be sent to patrons for nearly due, or due, items
67 [ -n ][ -m <number of days> ][ --itemscontent <comma separated field list> ][ -c ]
75 Print a brief help message and exits.
79 Prints the manual page and exits.
83 Verbose. Without this flag set, only fatal errors are reported.
87 Do not send any email. Advanced or due notices that would have been sent to
88 the patrons are printed to standard out.
92 Defines the maximum number of days in advance to send advance notices.
96 Confirm flag: Add this option. The script will only print a usage
99 =item B<--itemscontent>
101 comma separated list of fields that get substituted into templates in
102 places of the E<lt>E<lt>items.contentE<gt>E<gt> placeholder. This
103 defaults to due date,title,author,barcode
105 Other possible values come from fields in the biblios, items and
112 This script is designed to alert patrons when items are due, or coming due
116 This script pays attention to the advanced notice configuration
117 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
118 section of Koha is available in the Koha manual.
120 =head2 Outgoing emails
122 Typically, messages are prepared for each patron with due
123 items, and who have selected (or the library has elected for them) Advance or Due notices.
125 These emails are staged in the outgoing message queue, as are messages
126 produced by other features of Koha. This message queue must be
127 processed regularly by the
128 F<misc/cronjobs/process_message_queue.pl> program.
130 In the event that the C<-n> flag is passed to this program, no emails
131 are sent. Instead, messages are sent on standard output from this
132 program. They may be redirected to a file if desired.
136 Templates can contain variables enclosed in double angle brackets like
137 E<lt>E<lt>thisE<gt>E<gt>. Those variables will be replaced with values
138 specific to the overdue items or relevant patron. Available variables
143 =item E<lt>E<lt>items.contentE<gt>E<gt>
145 one line for each item, each line containing a tab separated list of
146 title, author, barcode, issuedate
148 =item E<lt>E<lt>borrowers.*E<gt>E<gt>
150 any field from the borrowers table
152 =item E<lt>E<lt>branches.*E<gt>E<gt>
154 any field from the branches table
160 The F<misc/cronjobs/overdue_notices.pl> program allows you to send
161 messages to patrons when their messages are overdue.
164 # These are defaults for command line options.
165 my $confirm; # -c: Confirm that the user has read and configured this script.
166 my $nomail; # -n: No mail. Will not send any emails.
167 my $mindays = 0; # -m: Maximum number of days in advance to send notices
168 my $maxdays = 30; # -e: the End of the time period
169 my $verbose = 0; # -v: verbose
170 my $itemscontent = join(',',qw( date_due title author barcode ));
182 'itemscontent=s' => \$itemscontent,
184 pod2usage(1) if $help;
185 pod2usage( -verbose => 2 ) if $man;;
187 # Since advance notice options are not visible in the web-interface
188 # unless EnhancedMessagingPreferences is on, let the user know that
189 # this script probably isn't going to do much
190 if ( ! C4::Context->preference('EnhancedMessagingPreferences') ) {
193 The "EnhancedMessagingPreferences" syspref is off.
194 Therefore, it is unlikely that this script will actually produce any messages to be sent.
195 To change this, edit the "EnhancedMessagingPreferences" syspref.
202 # The fields that will be substituted into <<items.content>>
203 my @item_content_fields = split(/,/,$itemscontent);
205 warn 'getting upcoming due issues' if $verbose;
206 my $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $maxdays } );
207 warn 'found ' . scalar( @$upcoming_dues ) . ' issues' if $verbose;
209 # hash of borrowernumber to number of items upcoming
210 # for patrons wishing digests only.
214 my $dbh = C4::Context->dbh();
215 my $sth = $dbh->prepare(<<'END_SQL');
216 SELECT biblio.*, items.*, issues.*
217 FROM issues,items,biblio
218 WHERE items.itemnumber=issues.itemnumber
219 AND biblio.biblionumber=items.biblionumber
220 AND issues.borrowernumber = ?
221 AND issues.itemnumber = ?
222 AND (TO_DAYS(date_due)-TO_DAYS(NOW()) = ?)
225 my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
228 UPCOMINGITEM: foreach my $upcoming ( @$upcoming_dues ) {
230 warn 'examining ' . $upcoming->{'itemnumber'} . ' upcoming due items' if $verbose;
232 my $from_address = $upcoming->{branchemail} || $admin_adress;
234 my $borrower_preferences;
235 if ( 0 == $upcoming->{'days_until_due'} ) {
236 # This item is due today. Send an 'item due' message.
237 $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $upcoming->{'borrowernumber'},
238 message_name => 'item_due' } );
239 next unless $borrower_preferences;
241 if ( $borrower_preferences->{'wants_digest'} ) {
242 # cache this one to process after we've run through all of the items.
243 $due_digest->{$upcoming->{borrowernumber}}{email} = $from_address;
244 $due_digest->{$upcoming->{borrowernumber}}{count}++;
246 my $biblio = C4::Biblio::GetBiblioFromItemNumber( $upcoming->{'itemnumber'} );
247 my $letter_type = 'DUE';
248 $sth->execute($upcoming->{'borrowernumber'},$upcoming->{'itemnumber'},'0');
250 while ( my $item_info = $sth->fetchrow_hashref()) {
251 my @item_info = map { $_ =~ /^date|date$/ ? format_date($item_info->{$_}) : $item_info->{$_} || '' } @item_content_fields;
252 $titles .= join("\t",@item_info) . "\n";
255 ## Get branch info for borrowers home library.
256 foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) {
257 my $letter = parse_letter( { letter_code => $letter_type,
258 borrowernumber => $upcoming->{'borrowernumber'},
259 branchcode => $upcoming->{'branchcode'},
260 biblionumber => $biblio->{'biblionumber'},
261 itemnumber => $upcoming->{'itemnumber'},
262 substitute => { 'items.content' => $titles },
263 message_transport_type => $transport,
265 or warn "no letter of type '$letter_type' found. Please see sample_notices.sql";
266 push @letters, $letter if $letter;
270 $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $upcoming->{'borrowernumber'},
271 message_name => 'advance_notice' } );
272 next UPCOMINGITEM unless $borrower_preferences && exists $borrower_preferences->{'days_in_advance'};
273 next UPCOMINGITEM unless $borrower_preferences->{'days_in_advance'} == $upcoming->{'days_until_due'};
275 if ( $borrower_preferences->{'wants_digest'} ) {
276 # cache this one to process after we've run through all of the items.
277 $upcoming_digest->{$upcoming->{borrowernumber}}{email} = $from_address;
278 $upcoming_digest->{$upcoming->{borrowernumber}}{count}++;
280 my $biblio = C4::Biblio::GetBiblioFromItemNumber( $upcoming->{'itemnumber'} );
281 my $letter_type = 'PREDUE';
282 $sth->execute($upcoming->{'borrowernumber'},$upcoming->{'itemnumber'},$borrower_preferences->{'days_in_advance'});
284 while ( my $item_info = $sth->fetchrow_hashref()) {
285 my @item_info = map { $_ =~ /^date|date$/ ? format_date($item_info->{$_}) : $item_info->{$_} || '' } @item_content_fields;
286 $titles .= join("\t",@item_info) . "\n";
289 ## Get branch info for borrowers home library.
290 foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) {
291 my $letter = parse_letter( { letter_code => $letter_type,
292 borrowernumber => $upcoming->{'borrowernumber'},
293 branchcode => $upcoming->{'branchcode'},
294 biblionumber => $biblio->{'biblionumber'},
295 itemnumber => $upcoming->{'itemnumber'},
296 substitute => { 'items.content' => $titles },
297 message_transport_type => $transport,
299 or warn "no letter of type '$letter_type' found. Please see sample_notices.sql";
300 push @letters, $letter if $letter;
305 # If we have prepared a letter, send it.
308 for my $letter ( @letters ) {
310 print $letter->{'content'};
314 for my $letter ( @letters ) {
315 C4::Letters::EnqueueLetter( { letter => $letter,
316 borrowernumber => $upcoming->{'borrowernumber'},
317 from_address => $from_address,
318 message_transport_type => $letter->{message_transport_type} } );
326 # Now, run through all the people that want digests and send them
328 $sth = $dbh->prepare(<<'END_SQL');
329 SELECT biblio.*, items.*, issues.*
330 FROM issues,items,biblio
331 WHERE items.itemnumber=issues.itemnumber
332 AND biblio.biblionumber=items.biblionumber
333 AND issues.borrowernumber = ?
334 AND (TO_DAYS(date_due)-TO_DAYS(NOW()) = ?)
336 PATRON: while ( my ( $borrowernumber, $digest ) = each %$upcoming_digest ) {
338 my $count = $digest->{count};
339 my $from_address = $digest->{email};
341 my $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber,
342 message_name => 'advance_notice' } );
343 next PATRON unless $borrower_preferences; # how could this happen?
346 my $letter_type = 'PREDUEDGST';
348 $sth->execute($borrowernumber,$borrower_preferences->{'days_in_advance'});
350 while ( my $item_info = $sth->fetchrow_hashref()) {
351 my @item_info = map { $_ =~ /^date|date$/ ? format_date($item_info->{$_}) : $item_info->{$_} || '' } @item_content_fields;
352 $titles .= join("\t",@item_info) . "\n";
355 ## Get branch info for borrowers home library.
356 my %branch_info = get_branch_info( $borrowernumber );
358 foreach my $transport ( keys %{ $borrower_preferences->{'transports'} } ) {
359 my $letter = parse_letter(
361 letter_code => $letter_type,
362 borrowernumber => $borrowernumber,
365 'items.content' => $titles,
368 branchcode => $branch_info{"branches.branchcode"},
369 message_transport_type => $transport,
372 or warn "no letter of type '$letter_type' found. Please see sample_notices.sql";
373 push @letters, $letter if $letter;
378 for my $letter ( @letters ) {
380 print $letter->{'content'};
384 for my $letter ( @letters ) {
385 C4::Letters::EnqueueLetter( { letter => $letter,
386 borrowernumber => $borrowernumber,
387 from_address => $from_address,
388 message_transport_type => $letter->{message_transport_type} } );
394 # Now, run through all the people that want digests and send them
395 PATRON: while ( my ( $borrowernumber, $digest ) = each %$due_digest ) {
397 my $count = $digest->{count};
398 my $from_address = $digest->{email};
400 my $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber,
401 message_name => 'item_due' } );
402 next PATRON unless $borrower_preferences; # how could this happen?
404 my $letter_type = 'DUEDGST';
405 $sth->execute($borrowernumber,'0');
407 while ( my $item_info = $sth->fetchrow_hashref()) {
408 my @item_info = map { $_ =~ /^date|date$/ ? format_date($item_info->{$_}) : $item_info->{$_} || '' } @item_content_fields;
409 $titles .= join("\t",@item_info) . "\n";
412 ## Get branch info for borrowers home library.
413 my %branch_info = get_branch_info( $borrowernumber );
415 for my $transport ( keys %{ $borrower_preferences->{'transports'} } ) {
416 my $letter = parse_letter(
418 letter_code => $letter_type,
419 borrowernumber => $borrowernumber,
422 'items.content' => $titles,
425 branchcode => $branch_info{"branches.branchcode"},
426 message_transport_type => $transport,
429 or warn "no letter of type '$letter_type' found. Please see sample_notices.sql";
430 push @letters, $letter if $letter;
435 for my $letter ( @letters ) {
437 print $letter->{'content'};
441 for my $letter ( @letters ) {
442 C4::Letters::EnqueueLetter( { letter => $letter,
443 borrowernumber => $borrowernumber,
444 from_address => $from_address,
445 message_transport_type => $letter->{message_transport_type} } );
460 foreach my $required ( qw( letter_code borrowernumber ) ) {
461 return unless exists $params->{$required};
464 my %table_params = ( 'borrowers' => $params->{'borrowernumber'} );
466 if ( my $p = $params->{'branchcode'} ) {
467 $table_params{'branches'} = $p;
469 if ( my $p = $params->{'itemnumber'} ) {
470 $table_params{'issues'} = $p;
471 $table_params{'items'} = $p;
473 if ( my $p = $params->{'biblionumber'} ) {
474 $table_params{'biblio'} = $p;
475 $table_params{'biblioitems'} = $p;
478 return C4::Letters::GetPreparedLetter (
479 module => 'circulation',
480 letter_code => $params->{'letter_code'},
481 branchcode => $table_params{'branches'},
482 substitute => $params->{'substitute'},
483 tables => \%table_params,
484 message_transport_type => $params->{message_transport_type},
489 my $date_string = shift;
490 my $dt=dt_from_string($date_string);
491 return output_pref($dt);
494 =head2 get_branch_info
498 sub get_branch_info {
499 my ( $borrowernumber ) = @_;
501 ## Get branch info for borrowers home library.
502 my $borrower_details = C4::Members::GetMember( borrowernumber => $borrowernumber );
503 my $borrower_branchcode = $borrower_details->{'branchcode'};
504 my $branch = C4::Branch::GetBranchDetail( $borrower_branchcode );
506 foreach my $key( keys %$branch ) {
507 $branch_info{"branches.$key"} = $branch->{$key};