From 76dde86fcea815565399cb3d6ac4ed3a653ba60a Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 6 Sep 2013 11:39:51 +0200 Subject: [PATCH] Bug 10833: Multi transport types for advanced notices Test plan: - Choose 2 patrons P1 and P2 - Edit "Patron messaging preferences" and check SMS + email with 2 days in advance for P1 check email with 5 days in advance for P2 - defined a message for the letter code PREDUE for sms and email (tools/letters.pl). - select 2 barcodes (B1, B2). * checkout B1 to P1 with a due date = NOW - 2 days * checkout B2 to P2 with a due date = NOW - 5 days - into the mysql cli, note the value of unsent message: select count(*) from message_queue where status != "send"; - launch the cronjob: perl misc/cronjobs/advance_notices.pl -c - retry the previous sql query, you should have X + 3 unsent messages (depending of current checkouts in your DB!). - view all unsent message: select borrowernumber, letter_code, message_transport_type, content from message_queue where status != "send"; You should see: 2 messages for P1, 1 for sms, 1 for email and the letter code PREDUE 1 message for P2, email and the letter code PREDUE Signed-off-by: Olli-Antti Kivilahti Signed-off-by: Marcel de Rooy Signed-off-by: Galen Charlton --- misc/cronjobs/advance_notices.pl | 146 +++++++++++++++++++------------ 1 file changed, 88 insertions(+), 58 deletions(-) diff --git a/misc/cronjobs/advance_notices.pl b/misc/cronjobs/advance_notices.pl index 4e7149d6e1..d81ad89786 100755 --- a/misc/cronjobs/advance_notices.pl +++ b/misc/cronjobs/advance_notices.pl @@ -222,13 +222,13 @@ END_SQL my $admin_adress = C4::Context->preference('KohaAdminEmailAddress'); +my @letters; UPCOMINGITEM: foreach my $upcoming ( @$upcoming_dues ) { warn 'examining ' . $upcoming->{'itemnumber'} . ' upcoming due items' if $verbose; # warn( Data::Dumper->Dump( [ $upcoming ], [ 'overdue' ] ) ); my $from_address = $upcoming->{branchemail} || $admin_adress; - my $letter; my $borrower_preferences; if ( 0 == $upcoming->{'days_until_due'} ) { # This item is due today. Send an 'item due' message. @@ -253,14 +253,18 @@ UPCOMINGITEM: foreach my $upcoming ( @$upcoming_dues ) { } ## Get branch info for borrowers home library. - $letter = parse_letter( { letter_code => $letter_type, + foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) { + my $letter = parse_letter( { letter_code => $letter_type, borrowernumber => $upcoming->{'borrowernumber'}, branchcode => $upcoming->{'branchcode'}, biblionumber => $biblio->{'biblionumber'}, itemnumber => $upcoming->{'itemnumber'}, - substitute => { 'items.content' => $titles } + substitute => { 'items.content' => $titles }, + message_transport_type => $transport, } ) - or die "no letter of type '$letter_type' found. Please see sample_notices.sql"; + or die "no letter of type '$letter_type' found. Please see sample_notices.sql"; + push @letters, $letter; + } } } else { $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $upcoming->{'borrowernumber'}, @@ -285,29 +289,35 @@ UPCOMINGITEM: foreach my $upcoming ( @$upcoming_dues ) { } ## Get branch info for borrowers home library. - $letter = parse_letter( { letter_code => $letter_type, + foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) { + my $letter = parse_letter( { letter_code => $letter_type, borrowernumber => $upcoming->{'borrowernumber'}, branchcode => $upcoming->{'branchcode'}, biblionumber => $biblio->{'biblionumber'}, itemnumber => $upcoming->{'itemnumber'}, - substitute => { 'items.content' => $titles } + substitute => { 'items.content' => $titles }, + message_transport_type => $transport, } ) - or die "no letter of type '$letter_type' found. Please see sample_notices.sql"; + or die "no letter of type '$letter_type' found. Please see sample_notices.sql"; + push @letters, $letter; + } } } # If we have prepared a letter, send it. - if ($letter) { + if ( @letters ) { if ($nomail) { - local $, = "\f"; - print $letter->{'content'}; + for my $letter ( @letters ) { + local $, = "\f"; + print $letter->{'content'}; + } } else { - foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) { + for my $letter ( @letters ) { C4::Letters::EnqueueLetter( { letter => $letter, borrowernumber => $upcoming->{'borrowernumber'}, from_address => $from_address, - message_transport_type => $transport } ); + message_transport_type => $letter->{message_transport_type} } ); } } } @@ -326,7 +336,7 @@ SELECT biblio.*, items.*, issues.* AND issues.borrowernumber = ? AND (TO_DAYS(date_due)-TO_DAYS(NOW()) = ?) END_SQL - +@letters = (); PATRON: while ( my ( $borrowernumber, $digest ) = each %$upcoming_digest ) { my $count = $digest->{count}; my $from_address = $digest->{email}; @@ -349,34 +359,44 @@ PATRON: while ( my ( $borrowernumber, $digest ) = each %$upcoming_digest ) { ## Get branch info for borrowers home library. my %branch_info = get_branch_info( $borrowernumber ); - my $letter = parse_letter( - { - letter_code => $letter_type, - borrowernumber => $borrowernumber, - substitute => { - count => $count, - 'items.content' => $titles, - %branch_info, - }, - branchcode => $branch_info{ "branches.branchcode" }, - } - ) - or die "no letter of type '$letter_type' found. Please see sample_notices.sql"; - if ($nomail) { - local $, = "\f"; - print $letter->{'content'}; + foreach my $transport ( keys %{ $borrower_preferences->{'transports'} } ) { + my $letter = parse_letter( + { + letter_code => $letter_type, + borrowernumber => $borrowernumber, + substitute => { + count => $count, + 'items.content' => $titles, + %branch_info, + }, + branchcode => $branch_info{"branches.branchcode"}, + message_transport_type => $transport, + } + ) + or die "no letter of type '$letter_type' found. Please see sample_notices.sql"; + push @letters, $letter; } - else { - foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) { - C4::Letters::EnqueueLetter( { letter => $letter, - borrowernumber => $borrowernumber, - from_address => $from_address, - message_transport_type => $transport } ); + + if ( @letters ) { + if ($nomail) { + for my $letter ( @letters ) { + local $, = "\f"; + print $letter->{'content'}; + } + } + else { + for my $letter ( @letters ) { + C4::Letters::EnqueueLetter( { letter => $letter, + borrowernumber => $borrowernumber, + from_address => $from_address, + message_transport_type => $letter->{message_transport_type} } ); + } } } } # Now, run through all the people that want digests and send them +@letters = (); PATRON: while ( my ( $borrowernumber, $digest ) = each %$due_digest ) { my $count = $digest->{count}; my $from_address = $digest->{email}; @@ -397,32 +417,41 @@ PATRON: while ( my ( $borrowernumber, $digest ) = each %$due_digest ) { ## Get branch info for borrowers home library. my %branch_info = get_branch_info( $borrowernumber ); - my $letter = parse_letter( - { - letter_code => $letter_type, - borrowernumber => $borrowernumber, - substitute => { - count => $count, - 'items.content' => $titles, - %branch_info, - }, - branchcode => $branch_info{ "branches.branchcode" }, - } - ) - or die "no letter of type '$letter_type' found. Please see sample_notices.sql"; - - if ($nomail) { - local $, = "\f"; - print $letter->{'content'}; + for my $transport ( keys %{ $borrower_preferences->{'transports'} } ) { + my $letter = parse_letter( + { + letter_code => $letter_type, + borrowernumber => $borrowernumber, + substitute => { + count => $count, + 'items.content' => $titles, + %branch_info, + }, + branchcode => $branch_info{"branches.branchcode"}, + message_transport_type => $transport, + } + ) + or die "no letter of type '$letter_type' found. Please see sample_notices.sql"; + push @letters, $letter; } - else { - foreach my $transport ( keys %{$borrower_preferences->{'transports'}} ) { - C4::Letters::EnqueueLetter( { letter => $letter, - borrowernumber => $borrowernumber, - from_address => $from_address, - message_transport_type => $transport } ); + + if ( @letters ) { + if ($nomail) { + for my $letter ( @letters ) { + local $, = "\f"; + print $letter->{'content'}; + } + } + else { + for my $letter ( @letters ) { + C4::Letters::EnqueueLetter( { letter => $letter, + borrowernumber => $borrowernumber, + from_address => $from_address, + message_transport_type => $letter->{message_transport_type} } ); + } } } + } =head1 METHODS @@ -457,6 +486,7 @@ sub parse_letter { branchcode => $table_params{'branches'}, substitute => $params->{'substitute'}, tables => \%table_params, + message_transport_type => $params->{message_transport_type}, ); } -- 2.39.5