Bug 31203: Add End action to cronjobs and log viewer
[koha.git] / misc / cronjobs / advance_notices.pl
1 #!/usr/bin/perl
2
3 # Copyright 2008 LibLime
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 =head1 NAME
21
22 advance_notices.pl - prepare messages to be sent to patrons for nearly due, or due, items
23
24 =head1 SYNOPSIS
25
26        advance_notices.pl
27          [ -n ][ -m <number of days> ][ --itemscontent <comma separated field list> ][ -c ]
28
29 =head1 DESCRIPTION
30
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
35 the OPAC.
36
37 =cut
38
39 use strict;
40 use warnings;
41 use Getopt::Long qw( GetOptions );
42 use Pod::Usage qw( pod2usage );
43 use Koha::Script -cron;
44 use C4::Context;
45 use C4::Letters;
46 use C4::Members;
47 use C4::Members::Messaging;
48 use C4::Log qw( cronlogaction );
49 use Koha::Items;
50 use Koha::Libraries;
51 use Koha::Patrons;
52
53 =head1 OPTIONS
54
55 =over 8
56
57 =item B<--help>
58
59 Print a brief help message and exits.
60
61 =item B<--man>
62
63 Prints the manual page and exits.
64
65 =item B<-v>
66
67 Verbose. Without this flag set, only fatal errors are reported.
68
69 =item B<-n>
70
71 Do not send any email. Advanced or due notices that would have been sent to
72 the patrons are printed to standard out.
73
74 =item B<-m>
75
76 Defines the maximum number of days in advance to send advance notices.
77
78 =item B<-c>
79
80 Confirm flag: Add this option. The script will only print a usage
81 statement otherwise.
82
83 =item B<--itemscontent>
84
85 comma separated list of fields that get substituted into templates in
86 places of the E<lt>E<lt>items.contentE<gt>E<gt> placeholder. This
87 defaults to date_due,title,author,barcode
88
89 Other possible values come from fields in the biblios, items and
90 issues tables.
91
92 =item B<--digest-per-branch>
93
94 Flag to indicate that generation of message digests should be
95 performed separately for each branch.
96
97 A patron could potentially have loans at several different branches
98 There is no natural branch to set as the sender on the aggregated
99 message in this situation so the default behavior is to use the
100 borrowers home branch.  This could surprise to the borrower when
101 message sender is a library where they have not borrowed anything.
102
103 Enabling this flag ensures that the issuing library is the sender of
104 the digested message.  It has no effect unless the borrower has
105 chosen 'Digests only' on the advance messages.
106
107 =item B<--library>
108
109 select notices for one specific library. Use the value in the
110 branches.branchcode table. This option can be repeated in order
111 to select notices for a group of libraries.
112
113 =item B<--frombranch>
114
115 Set the from address for the notice to one of 'item-homebranch' or 'item-issuebranch'.
116
117 Defaults to 'item-issuebranch'
118
119 =back
120
121 =head2 Configuration
122
123 This script pays attention to the advanced notice configuration
124 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
125 section of Koha is available in the Koha manual.
126
127 =head2 Outgoing emails
128
129 Typically, messages are prepared for each patron with due
130 items, and who have selected (or the library has elected for them) Advance or Due notices.
131
132 These emails are staged in the outgoing message queue, as are messages
133 produced by other features of Koha. This message queue must be
134 processed regularly by the
135 F<misc/cronjobs/process_message_queue.pl> program.
136
137 In the event that the C<-n> flag is passed to this program, no emails
138 are sent. Instead, messages are sent on standard output from this
139 program. They may be redirected to a file if desired.
140
141 =head2 Templates
142
143 Templates can contain variables enclosed in double angle brackets like
144 E<lt>E<lt>thisE<gt>E<gt>. Those variables will be replaced with values
145 specific to the overdue items or relevant patron. Available variables
146 are:
147
148 =over
149
150 =item E<lt>E<lt>items.contentE<gt>E<gt>
151
152 one line for each item, each line containing a tab separated list of
153 date due, title, author, barcode
154
155 =item E<lt>E<lt>borrowers.*E<gt>E<gt>
156
157 any field from the borrowers table
158
159 =item E<lt>E<lt>branches.*E<gt>E<gt>
160
161 any field from the branches table
162
163 =back
164
165 =head1 SEE ALSO
166
167 The F<misc/cronjobs/overdue_notices.pl> program allows you to send
168 messages to patrons when their messages are overdue.
169
170 =cut
171
172 binmode( STDOUT, ':encoding(UTF-8)' );
173
174 # These are defaults for command line options.
175 my $confirm;                                                        # -c: Confirm that the user has read and configured this script.
176 my $nomail;                                                         # -n: No mail. Will not send any emails.
177 my $mindays     = 0;                                                # -m: Maximum number of days in advance to send notices
178 my $maxdays     = 30;                                               # -e: the End of the time period
179 my $verbose     = 0;                                                # -v: verbose
180 my $digest_per_branch = 0;                                          # -digest-per-branch: Prepare and send digests per branch
181 my @branchcodes; # Branch(es) passed as parameter
182 my $frombranch   = 'item-issuebranch';
183 my $itemscontent = join(',',qw( date_due title author barcode ));
184
185 my $help    = 0;
186 my $man     = 0;
187
188 my $command_line_options = join(" ",@ARGV);
189
190 GetOptions(
191             'help|?'         => \$help,
192             'man'            => \$man,
193             'library=s'      => \@branchcodes,
194             'frombranch=s'   => \$frombranch,
195             'c'              => \$confirm,
196             'n'              => \$nomail,
197             'm:i'            => \$maxdays,
198             'v'              => \$verbose,
199             'digest-per-branch' => \$digest_per_branch,
200             'itemscontent=s' => \$itemscontent,
201        )or pod2usage(2);
202 pod2usage(1) if $help;
203 pod2usage( -verbose => 2 ) if $man;
204
205 # Since advance notice options are not visible in the web-interface
206 # unless EnhancedMessagingPreferences is on, let the user know that
207 # this script probably isn't going to do much
208 if ( ! C4::Context->preference('EnhancedMessagingPreferences') && $verbose ) {
209     warn <<'END_WARN';
210
211 The "EnhancedMessagingPreferences" syspref is off.
212 Therefore, it is unlikely that this script will actually produce any messages to be sent.
213 To change this, edit the "EnhancedMessagingPreferences" syspref.
214
215 END_WARN
216 }
217 unless ($confirm) {
218      pod2usage(1);
219 }
220 cronlogaction({ info => $command_line_options });
221
222 my %branches = ();
223 if (@branchcodes) {
224     %branches = map { $_ => 1 } @branchcodes;
225 }
226
227 die "--frombranch takes item-homebranch or item-issuebranch only"
228   unless ( $frombranch eq 'item-issuebranch'
229     || $frombranch eq 'item-homebranch' );
230 my $owning_library = ( $frombranch eq 'item-homebranch' ) ? 1 : 0;
231
232 # The fields that will be substituted into <<items.content>>
233 my @item_content_fields = split(/,/,$itemscontent);
234
235 warn 'getting upcoming due issues' if $verbose;
236 my $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( {
237     days_in_advance => $maxdays,
238     owning_library => $owning_library
239  } );
240 warn 'found ' . scalar( @$upcoming_dues ) . ' issues' if $verbose;
241
242 # hash of borrowernumber to number of items upcoming
243 # for patrons wishing digests only.
244 my $upcoming_digest = {};
245 my $due_digest = {};
246
247 my $dbh = C4::Context->dbh();
248 my $sth = $dbh->prepare(<<'END_SQL');
249 SELECT biblio.*, items.*, issues.*
250   FROM issues,items,biblio
251   WHERE items.itemnumber=issues.itemnumber
252     AND biblio.biblionumber=items.biblionumber
253     AND issues.borrowernumber = ?
254     AND issues.itemnumber = ?
255     AND (TO_DAYS(date_due)-TO_DAYS(NOW()) = ?)
256 END_SQL
257
258 my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
259
260 my @letters;
261 UPCOMINGITEM: foreach my $upcoming ( @$upcoming_dues ) {
262     @letters = ();
263     warn 'examining ' . $upcoming->{'itemnumber'} . ' upcoming due items' if $verbose;
264
265     my $from_address = $upcoming->{branchemail} || $admin_adress;
266
267     my $borrower_preferences;
268     if ( 0 == $upcoming->{'days_until_due'} ) {
269         # This item is due today. Send an 'item due' message.
270         $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $upcoming->{'borrowernumber'},
271                                                                                    message_name   => 'item_due' } );
272         next unless $borrower_preferences;
273         
274         if ( $borrower_preferences->{'wants_digest'} ) {
275             # cache this one to process after we've run through all of the items.
276             if ($digest_per_branch) {
277                 $due_digest->{ $upcoming->{branchcode} }->{ $upcoming->{borrowernumber} }->{email} = $from_address;
278                 $due_digest->{ $upcoming->{branchcode} }->{ $upcoming->{borrowernumber} }->{count}++;
279             } else {
280                 $due_digest->{ $upcoming->{borrowernumber} }->{email} = $from_address;
281                 $due_digest->{ $upcoming->{borrowernumber} }->{count}++;
282             }
283         } else {
284             my $branchcode;
285             if($owning_library) {
286                 $branchcode = $upcoming->{'homebranch'};
287             } else {
288                 $branchcode = $upcoming->{'branchcode'};
289             }
290             # Skip this DUE if we specify list of libraries and this one is not part of it
291             next if (@branchcodes && !$branches{$branchcode});
292
293             my $item = Koha::Items->find( $upcoming->{itemnumber} );
294             my $letter_type = 'DUE';
295             $sth->execute($upcoming->{'borrowernumber'},$upcoming->{'itemnumber'},'0');
296             my $titles = "";
297             while ( my $item_info = $sth->fetchrow_hashref()) {
298                 $titles .= C4::Letters::get_item_content( { item => $item_info, item_content_fields => \@item_content_fields } );
299             }
300
301             ## Get branch info for borrowers home library.
302             foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) {
303                 next if $transport eq 'itiva';
304                 my $letter = parse_letter( { letter_code    => $letter_type,
305                                       borrowernumber => $upcoming->{'borrowernumber'},
306                                       branchcode     => $branchcode,
307                                       biblionumber   => $item->biblionumber,
308                                       itemnumber     => $upcoming->{'itemnumber'},
309                                       substitute     => { 'items.content' => $titles },
310                                       message_transport_type => $transport,
311                                     } )
312                     or warn "no letter of type '$letter_type' found for borrowernumber ".$upcoming->{'borrowernumber'}.". Please see sample_notices.sql";
313                 push @letters, $letter if $letter;
314             }
315         }
316     } else {
317         $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $upcoming->{'borrowernumber'},
318                                                                                    message_name   => 'advance_notice' } );
319         next UPCOMINGITEM unless $borrower_preferences && exists $borrower_preferences->{'days_in_advance'};
320         next UPCOMINGITEM unless $borrower_preferences->{'days_in_advance'} == $upcoming->{'days_until_due'};
321
322         if ( $borrower_preferences->{'wants_digest'} ) {
323             # cache this one to process after we've run through all of the items.
324             if ($digest_per_branch) {
325                 $upcoming_digest->{ $upcoming->{branchcode} }->{ $upcoming->{borrowernumber} }->{email} = $from_address;
326                 $upcoming_digest->{ $upcoming->{branchcode} }->{ $upcoming->{borrowernumber} }->{count}++;
327             } else {
328                 $upcoming_digest->{ $upcoming->{borrowernumber} }->{email} = $from_address;
329                 $upcoming_digest->{ $upcoming->{borrowernumber} }->{count}++;
330             }
331         } else {
332             my $branchcode;
333             if($owning_library) {
334             $branchcode = $upcoming->{'homebranch'};
335             } else {
336             $branchcode = $upcoming->{'branchcode'};
337             }
338             # Skip this PREDUE if we specify list of libraries and this one is not part of it
339             next if (@branchcodes && !$branches{$branchcode});
340
341             my $item = Koha::Items->find( $upcoming->{itemnumber} );
342             my $letter_type = 'PREDUE';
343             $sth->execute($upcoming->{'borrowernumber'},$upcoming->{'itemnumber'},$borrower_preferences->{'days_in_advance'});
344             my $titles = "";
345             while ( my $item_info = $sth->fetchrow_hashref()) {
346                 $titles .= C4::Letters::get_item_content( { item => $item_info, item_content_fields => \@item_content_fields } );
347             }
348
349             ## Get branch info for borrowers home library.
350             foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) {
351                 next if $transport eq 'itiva';
352                 my $letter = parse_letter( { letter_code    => $letter_type,
353                                       borrowernumber => $upcoming->{'borrowernumber'},
354                                       branchcode     => $branchcode,
355                                       biblionumber   => $item->biblionumber,
356                                       itemnumber     => $upcoming->{'itemnumber'},
357                                       substitute     => { 'items.content' => $titles },
358                                       message_transport_type => $transport,
359                                     } )
360                     or warn "no letter of type '$letter_type' found for borrowernumber ".$upcoming->{'borrowernumber'}.". Please see sample_notices.sql";
361                 push @letters, $letter if $letter;
362             }
363         }
364     }
365
366     # If we have prepared a letter, send it.
367     if ( @letters ) {
368       if ($nomail) {
369         for my $letter ( @letters ) {
370             local $, = "\f";
371             print $letter->{'content'}."\n";
372         }
373       }
374       else {
375         for my $letter ( @letters ) {
376             C4::Letters::EnqueueLetter( { letter                 => $letter,
377                                           borrowernumber         => $upcoming->{'borrowernumber'},
378                                           from_address           => $from_address,
379                                           message_transport_type => $letter->{message_transport_type} } );
380         }
381       }
382     }
383 }
384
385
386
387 # Now, run through all the people that want digests and send them
388
389 my $sth_digest = $dbh->prepare(<<'END_SQL');
390 SELECT biblio.*, items.*, issues.*
391   FROM issues,items,biblio
392   WHERE items.itemnumber=issues.itemnumber
393     AND biblio.biblionumber=items.biblionumber
394     AND issues.borrowernumber = ?
395     AND (TO_DAYS(date_due)-TO_DAYS(NOW()) = ?)
396 END_SQL
397
398 if ($digest_per_branch) {
399     while (my ($branchcode, $digests) = each %$upcoming_digest) {
400         send_digests({
401             sth => $sth_digest,
402             digests => $digests,
403             letter_code => 'PREDUEDGST',
404             message_name => 'advance_notice',
405             branchcode => $branchcode,
406             get_item_info => sub {
407                 my $params = shift;
408                 $params->{sth}->execute($params->{borrowernumber},
409                                         $params->{borrower_preferences}->{'days_in_advance'});
410                 return sub {
411                     $params->{sth}->fetchrow_hashref;
412                 };
413             }
414         });
415     }
416
417     while (my ($branchcode, $digests) = each %$due_digest) {
418         send_digests({
419             sth => $sth_digest,
420             digests => $due_digest,
421             letter_code => 'DUEDGST',
422             branchcode => $branchcode,
423             message_name => 'item_due',
424             get_item_info => sub {
425                 my $params = shift;
426                 $params->{sth}->execute($params->{borrowernumber}, 0);
427                 return sub {
428                     $params->{sth}->fetchrow_hashref;
429                 };
430             }
431         });
432     }
433 } else {
434     send_digests({
435         sth => $sth_digest,
436         digests => $upcoming_digest,
437         letter_code => 'PREDUEDGST',
438         message_name => 'advance_notice',
439         get_item_info => sub {
440             my $params = shift;
441             $params->{sth}->execute($params->{borrowernumber},
442                                     $params->{borrower_preferences}->{'days_in_advance'});
443             return sub {
444                 $params->{sth}->fetchrow_hashref;
445             };
446         }
447     });
448
449     send_digests({
450         sth => $sth_digest,
451         digests => $due_digest,
452         letter_code => 'DUEDGST',
453         message_name => 'item_due',
454         get_item_info => sub {
455             my $params = shift;
456             $params->{sth}->execute($params->{borrowernumber}, 0);
457             return sub {
458                 $params->{sth}->fetchrow_hashref;
459             };
460         }
461     });
462 }
463
464 =head1 METHODS
465
466 =head2 parse_letter
467
468 =cut
469
470 sub parse_letter {
471     my $params = shift;
472
473     foreach my $required ( qw( letter_code borrowernumber ) ) {
474         return unless exists $params->{$required};
475     }
476     my $patron = Koha::Patrons->find( $params->{borrowernumber} );
477
478     my %table_params = ( 'borrowers' => $params->{'borrowernumber'} );
479
480     if ( my $p = $params->{'branchcode'} ) {
481         $table_params{'branches'} = $p;
482     }
483     if ( my $p = $params->{'itemnumber'} ) {
484         $table_params{'issues'} = $p;
485         $table_params{'items'} = $p;
486     }
487     if ( my $p = $params->{'biblionumber'} ) {
488         $table_params{'biblio'} = $p;
489         $table_params{'biblioitems'} = $p;
490     }
491
492     return C4::Letters::GetPreparedLetter (
493         module => 'circulation',
494         letter_code => $params->{'letter_code'},
495         branchcode => $table_params{'branches'},
496         lang => $patron->lang,
497         substitute => $params->{'substitute'},
498         tables     => \%table_params,
499         ( $params->{itemnumbers} ? ( loops => { items => $params->{itemnumbers} } ) : () ),
500         message_transport_type => $params->{message_transport_type},
501     );
502 }
503
504 =head2 get_branch_info
505
506 =cut
507
508 sub get_branch_info {
509     my ( $borrowernumber ) = @_;
510
511     ## Get branch info for borrowers home library.
512     my $patron = Koha::Patrons->find( $borrowernumber );
513     my $branch = $patron->library->unblessed;
514     my %branch_info;
515     foreach my $key( keys %$branch ) {
516         $branch_info{"branches.$key"} = $branch->{$key};
517     }
518
519     return %branch_info;
520 }
521
522 =head2 send_digests
523
524     send_digests({
525         digests => ...,
526         sth => ...,
527         letter_code => ...,
528         get_item_info => ...,
529     })
530
531 Enqueue digested letters (or print them if -n was passed at command line).
532
533 Parameters:
534
535 =over 4
536
537 =item C<$digests>
538
539 Reference to the array of digested messages.
540
541 =item C<$sth>
542
543 Prepared statement handle for fetching overdue issues.
544
545 =item C<$letter_code>
546
547 String that denote the letter code.
548
549 =item C<$get_item_info>
550
551 Subroutine for executing prepared statement.  Takes parameters $sth,
552 $borrowernumber and $borrower_parameters and return a generator
553 function that produce the matching rows.
554
555 =back
556
557 =cut
558
559 sub send_digests {
560     my $params = shift;
561
562     PATRON: while ( my ( $borrowernumber, $digest ) = each %{$params->{digests}} ) {
563         @letters = ();
564         my $count = $digest->{count};
565         my $from_address = $digest->{email};
566
567         my %branch_info;
568         my $branchcode;
569
570         if (defined($params->{branchcode})) {
571             %branch_info = ();
572             $branchcode = $params->{branchcode};
573         } else {
574             ## Get branch info for borrowers home library.
575             %branch_info = get_branch_info( $borrowernumber );
576             $branchcode = $branch_info{'branches.branchcode'};
577         }
578
579         my $borrower_preferences =
580             C4::Members::Messaging::GetMessagingPreferences(
581                 {
582                     borrowernumber => $borrowernumber,
583                     message_name   => $params->{message_name}
584                 }
585             );
586
587         next PATRON unless $borrower_preferences; # how could this happen?
588
589         my $next_item_info = $params->{get_item_info}->({
590             sth => $params->{sth},
591             borrowernumber => $borrowernumber,
592             borrower_preferences => $borrower_preferences
593         });
594         my $titles = "";
595         my @itemnumbers;
596         while ( my $item_info = $next_item_info->()) {
597             push @itemnumbers, $item_info->{itemnumber};
598             $titles .= C4::Letters::get_item_content( { item => $item_info, item_content_fields => \@item_content_fields } );
599         }
600
601         foreach my $transport ( keys %{ $borrower_preferences->{'transports'} } ) {
602             next if $transport eq 'itiva';
603             my $letter = parse_letter(
604                 {
605                     letter_code    => $params->{letter_code},
606                     borrowernumber => $borrowernumber,
607                     substitute     => {
608                         count           => $count,
609                         'items.content' => $titles,
610                         %branch_info
611                     },
612                     itemnumbers    => \@itemnumbers,
613                     branchcode     => $branchcode,
614                     message_transport_type => $transport
615                 }
616             );
617             unless ( $letter ){
618                 warn "no letter of type '$params->{letter_type}' found for borrowernumber $borrowernumber. Please see sample_notices.sql";
619                 next;
620             }
621             push @letters, $letter if $letter;
622         }
623
624         if ( @letters ) {
625             if ($nomail) {
626                 for my $letter ( @letters ) {
627                     local $, = "\f";
628                     print $letter->{'content'};
629                 }
630             }
631             else {
632                 for my $letter ( @letters ) {
633                     C4::Letters::EnqueueLetter( { letter                 => $letter,
634                                                   borrowernumber         => $borrowernumber,
635                                                   from_address           => $from_address,
636                                                   message_transport_type => $letter->{message_transport_type} } );
637                 }
638             }
639         }
640     }
641 }
642
643 cronlogaction({ action => 'End', info => "COMPLETED" });
644
645 1;
646
647 __END__