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 =item B<--itemscontent>
98 comma separated list of fields that get substituted into templates in
99 places of the E<lt>E<lt>items.contentE<gt>E<gt> placeholder. This
100 defaults to due date,title,author,barcode
102 Other possible values come from fields in the biblios, items and
109 This script is designed to alert patrons when items are due, or coming due
113 This script pays attention to the advanced notice configuration
114 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
115 section of Koha is available in the Koha manual.
117 =head2 Outgoing emails
119 Typically, messages are prepared for each patron with due
120 items, and who have selected (or the library has elected for them) Advance or Due notices.
122 These emails are staged in the outgoing message queue, as are messages
123 produced by other features of Koha. This message queue must be
124 processed regularly by the
125 F<misc/cronjobs/process_message_queue.pl> program.
127 In the event that the C<-n> flag is passed to this program, no emails
128 are sent. Instead, messages are sent on standard output from this
129 program. They may be redirected to a file if desired.
133 Templates can contain variables enclosed in double angle brackets like
134 E<lt>E<lt>thisE<gt>E<gt>. Those variables will be replaced with values
135 specific to the overdue items or relevant patron. Available variables
140 =item E<lt>E<lt>items.contentE<gt>E<gt>
142 one line for each item, each line containing a tab separated list of
143 title, author, barcode, issuedate
145 =item E<lt>E<lt>borrowers.*E<gt>E<gt>
147 any field from the borrowers table
149 =item E<lt>E<lt>branches.*E<gt>E<gt>
151 any field from the branches table
157 The F<misc/cronjobs/overdue_notices.pl> program allows you to send
158 messages to patrons when their messages are overdue.
161 # These are defaults for command line options.
162 my $confirm; # -c: Confirm that the user has read and configured this script.
163 # my $confirm = 1; # -c: Confirm that the user has read and configured this script.
164 my $nomail; # -n: No mail. Will not send any emails.
165 my $mindays = 0; # -m: Maximum number of days in advance to send notices
166 my $maxdays = 30; # -e: the End of the time period
167 my $verbose = 0; # -v: verbose
168 my $itemscontent = join(',',qw( date_due title author barcode ));
180 'itemscontent=s' => \$itemscontent,
182 pod2usage(1) if $help;
183 pod2usage( -verbose => 2 ) if $man;;
185 # Since advance notice options are not visible in the web-interface
186 # unless EnhancedMessagingPreferences is on, let the user know that
187 # this script probably isn't going to do much
188 if ( ! C4::Context->preference('EnhancedMessagingPreferences') ) {
191 The "EnhancedMessagingPreferences" syspref is off.
192 Therefore, it is unlikely that this script will actually produce any messages to be sent.
193 To change this, edit the "EnhancedMessagingPreferences" syspref.
200 # The fields that will be substituted into <<items.content>>
201 my @item_content_fields = split(/,/,$itemscontent);
203 warn 'getting upcoming due issues' if $verbose;
204 my $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $maxdays } );
205 warn 'found ' . scalar( @$upcoming_dues ) . ' issues' if $verbose;
207 # hash of borrowernumber to number of items upcoming
208 # for patrons wishing digests only.
212 my $dbh = C4::Context->dbh();
213 my $sth = $dbh->prepare(<<'END_SQL');
214 SELECT biblio.*, items.*, issues.*
215 FROM issues,items,biblio
216 WHERE items.itemnumber=issues.itemnumber
217 AND biblio.biblionumber=items.biblionumber
218 AND issues.borrowernumber = ?
219 AND issues.itemnumber = ?
220 AND (TO_DAYS(date_due)-TO_DAYS(NOW()) = ?)
223 my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
226 UPCOMINGITEM: foreach my $upcoming ( @$upcoming_dues ) {
227 warn 'examining ' . $upcoming->{'itemnumber'} . ' upcoming due items' if $verbose;
228 # warn( Data::Dumper->Dump( [ $upcoming ], [ 'overdue' ] ) );
230 my $from_address = $upcoming->{branchemail} || $admin_adress;
232 my $borrower_preferences;
233 if ( 0 == $upcoming->{'days_until_due'} ) {
234 # This item is due today. Send an 'item due' message.
235 $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $upcoming->{'borrowernumber'},
236 message_name => 'item_due' } );
237 # warn( Data::Dumper->Dump( [ $borrower_preferences ], [ 'borrower_preferences' ] ) );
238 next unless $borrower_preferences;
240 if ( $borrower_preferences->{'wants_digest'} ) {
241 # cache this one to process after we've run through all of the items.
242 my $digest = $due_digest->{$upcoming->{'borrowernumber'}} ||= {};
243 $digest->{email} ||= $from_address;
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 die "no letter of type '$letter_type' found. Please see sample_notices.sql";
266 push @letters, $letter;
270 $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $upcoming->{'borrowernumber'},
271 message_name => 'advance_notice' } );
272 # warn( Data::Dumper->Dump( [ $borrower_preferences ], [ 'borrower_preferences' ] ) );
273 next UPCOMINGITEM unless $borrower_preferences && exists $borrower_preferences->{'days_in_advance'};
274 next UPCOMINGITEM unless $borrower_preferences->{'days_in_advance'} == $upcoming->{'days_until_due'};
276 if ( $borrower_preferences->{'wants_digest'} ) {
277 # cache this one to process after we've run through all of the items.
278 my $digest = $upcoming_digest->{$upcoming->{'borrowernumber'}} ||= {};
279 $digest->{email} ||= $from_address;
282 my $biblio = C4::Biblio::GetBiblioFromItemNumber( $upcoming->{'itemnumber'} );
283 my $letter_type = 'PREDUE';
284 $sth->execute($upcoming->{'borrowernumber'},$upcoming->{'itemnumber'},$borrower_preferences->{'days_in_advance'});
286 while ( my $item_info = $sth->fetchrow_hashref()) {
287 my @item_info = map { $_ =~ /^date|date$/ ? format_date($item_info->{$_}) : $item_info->{$_} || '' } @item_content_fields;
288 $titles .= join("\t",@item_info) . "\n";
291 ## Get branch info for borrowers home library.
292 foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) {
293 my $letter = parse_letter( { letter_code => $letter_type,
294 borrowernumber => $upcoming->{'borrowernumber'},
295 branchcode => $upcoming->{'branchcode'},
296 biblionumber => $biblio->{'biblionumber'},
297 itemnumber => $upcoming->{'itemnumber'},
298 substitute => { 'items.content' => $titles },
299 message_transport_type => $transport,
301 or die "no letter of type '$letter_type' found. Please see sample_notices.sql";
302 push @letters, $letter;
307 # If we have prepared a letter, send it.
310 for my $letter ( @letters ) {
312 print $letter->{'content'};
316 for my $letter ( @letters ) {
317 C4::Letters::EnqueueLetter( { letter => $letter,
318 borrowernumber => $upcoming->{'borrowernumber'},
319 from_address => $from_address,
320 message_transport_type => $letter->{message_transport_type} } );
327 # warn( Data::Dumper->Dump( [ $upcoming_digest ], [ 'upcoming_digest' ] ) );
329 # Now, run through all the people that want digests and send them
331 $sth = $dbh->prepare(<<'END_SQL');
332 SELECT biblio.*, items.*, issues.*
333 FROM issues,items,biblio
334 WHERE items.itemnumber=issues.itemnumber
335 AND biblio.biblionumber=items.biblionumber
336 AND issues.borrowernumber = ?
337 AND (TO_DAYS(date_due)-TO_DAYS(NOW()) = ?)
340 PATRON: while ( my ( $borrowernumber, $digest ) = each %$upcoming_digest ) {
341 my $count = $digest->{count};
342 my $from_address = $digest->{email};
344 my $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber,
345 message_name => 'advance_notice' } );
346 # warn( Data::Dumper->Dump( [ $borrower_preferences ], [ 'borrower_preferences' ] ) );
347 next PATRON unless $borrower_preferences; # how could this happen?
350 my $letter_type = 'PREDUEDGST';
352 $sth->execute($borrowernumber,$borrower_preferences->{'days_in_advance'});
354 while ( my $item_info = $sth->fetchrow_hashref()) {
355 my @item_info = map { $_ =~ /^date|date$/ ? format_date($item_info->{$_}) : $item_info->{$_} || '' } @item_content_fields;
356 $titles .= join("\t",@item_info) . "\n";
359 ## Get branch info for borrowers home library.
360 my %branch_info = get_branch_info( $borrowernumber );
362 foreach my $transport ( keys %{ $borrower_preferences->{'transports'} } ) {
363 my $letter = parse_letter(
365 letter_code => $letter_type,
366 borrowernumber => $borrowernumber,
369 'items.content' => $titles,
372 branchcode => $branch_info{"branches.branchcode"},
373 message_transport_type => $transport,
376 or die "no letter of type '$letter_type' found. Please see sample_notices.sql";
377 push @letters, $letter;
382 for my $letter ( @letters ) {
384 print $letter->{'content'};
388 for my $letter ( @letters ) {
389 C4::Letters::EnqueueLetter( { letter => $letter,
390 borrowernumber => $borrowernumber,
391 from_address => $from_address,
392 message_transport_type => $letter->{message_transport_type} } );
398 # Now, run through all the people that want digests and send them
400 PATRON: while ( my ( $borrowernumber, $digest ) = each %$due_digest ) {
401 my $count = $digest->{count};
402 my $from_address = $digest->{email};
404 my $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber,
405 message_name => 'item_due' } );
406 # warn( Data::Dumper->Dump( [ $borrower_preferences ], [ 'borrower_preferences' ] ) );
407 next PATRON unless $borrower_preferences; # how could this happen?
409 my $letter_type = 'DUEDGST';
410 $sth->execute($borrowernumber,'0');
412 while ( my $item_info = $sth->fetchrow_hashref()) {
413 my @item_info = map { $_ =~ /^date|date$/ ? format_date($item_info->{$_}) : $item_info->{$_} || '' } @item_content_fields;
414 $titles .= join("\t",@item_info) . "\n";
417 ## Get branch info for borrowers home library.
418 my %branch_info = get_branch_info( $borrowernumber );
420 for my $transport ( keys %{ $borrower_preferences->{'transports'} } ) {
421 my $letter = parse_letter(
423 letter_code => $letter_type,
424 borrowernumber => $borrowernumber,
427 'items.content' => $titles,
430 branchcode => $branch_info{"branches.branchcode"},
431 message_transport_type => $transport,
434 or die "no letter of type '$letter_type' found. Please see sample_notices.sql";
435 push @letters, $letter;
440 for my $letter ( @letters ) {
442 print $letter->{'content'};
446 for my $letter ( @letters ) {
447 C4::Letters::EnqueueLetter( { letter => $letter,
448 borrowernumber => $borrowernumber,
449 from_address => $from_address,
450 message_transport_type => $letter->{message_transport_type} } );
465 foreach my $required ( qw( letter_code borrowernumber ) ) {
466 return unless exists $params->{$required};
469 my %table_params = ( 'borrowers' => $params->{'borrowernumber'} );
471 if ( my $p = $params->{'branchcode'} ) {
472 $table_params{'branches'} = $p;
474 if ( my $p = $params->{'itemnumber'} ) {
475 $table_params{'issues'} = $p;
476 $table_params{'items'} = $p;
478 if ( my $p = $params->{'biblionumber'} ) {
479 $table_params{'biblio'} = $p;
480 $table_params{'biblioitems'} = $p;
483 return C4::Letters::GetPreparedLetter (
484 module => 'circulation',
485 letter_code => $params->{'letter_code'},
486 branchcode => $table_params{'branches'},
487 substitute => $params->{'substitute'},
488 tables => \%table_params,
489 message_transport_type => $params->{message_transport_type},
494 my $date_string = shift;
495 my $dt=dt_from_string($date_string);
496 return output_pref($dt);
499 =head2 get_branch_info
503 sub get_branch_info {
504 my ( $borrowernumber ) = @_;
506 ## Get branch info for borrowers home library.
507 my $borrower_details = C4::Members::GetMember( borrowernumber => $borrowernumber );
508 my $borrower_branchcode = $borrower_details->{'branchcode'};
509 my $branch = C4::Branch::GetBranchDetail( $borrower_branchcode );
511 foreach my $key( keys %$branch ) {
512 $branch_info{"branches.$key"} = $branch->{$key};