From cc189185462e00e6a00f708ce918a28052e29067 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 1 Oct 2021 10:40:50 +0100 Subject: [PATCH] Bug 29005: Add option to send welcome email from patron imports This patch adds the ability to send the ACCTDETAILS notice for new users added using the patron import tool. Test plan 1. Create a valid csv for patron import that includes some new users, ensuring you add a valid email address for which you have access. 2. Import the users using the patron import tool and select the new 'Send email to new patrons' checkbox. 3. Check that the notice appears in the new patrons notices 4. Check that you received a welcome email for the user. Signed-off-by: Kyle M Hall --- Koha/Patrons/Import.pm | 50 ++++++++++++++++++- .../prog/en/modules/tools/import_borrowers.tt | 11 ++++ tools/import_borrowers.pl | 2 + 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/Koha/Patrons/Import.pm b/Koha/Patrons/Import.pm index fe3fd80c11..ae5ebe6058 100644 --- a/Koha/Patrons/Import.pm +++ b/Koha/Patrons/Import.pm @@ -24,6 +24,7 @@ use Encode qw( decode_utf8 ); use Try::Tiny qw( catch try ); use C4::Members qw( checkcardnumber ); +use C4::Letters qw( GetPreparedLetter EnqueueLetter ); use Koha::Libraries; use Koha::Patrons; @@ -74,6 +75,7 @@ sub import_patrons { my $overwrite_cardnumber = $params->{overwrite_cardnumber}; my $overwrite_passwords = $params->{overwrite_passwords}; my $dry_run = $params->{dry_run}; + my $send_welcome = $params->{send_welcome}; my $extended = C4::Context->preference('ExtendedPatronAttributes'); my $set_messaging_prefs = C4::Context->preference('EnhancedMessagingPreferences'); @@ -196,11 +198,13 @@ sub import_patrons { } } + my $is_new = 0; if ($patron) { $member = $patron->unblessed; $borrowernumber = $member->{'borrowernumber'}; } else { $member = {}; + $is_new = 1; } if ( C4::Members::checkcardnumber( $borrower{cardnumber}, $borrowernumber ) ) { @@ -277,7 +281,6 @@ sub import_patrons { } } - my $patron = Koha::Patrons->find( $borrowernumber ); try { $schema->storage->txn_do(sub { $patron->set(\%borrower)->store; @@ -373,7 +376,7 @@ sub import_patrons { else { try { $schema->storage->txn_do(sub { - my $patron = Koha::Patron->new(\%borrower)->store; + $patron = Koha::Patron->new(\%borrower)->store; $borrowernumber = $patron->id; if ( $patron->is_debarred ) { @@ -439,6 +442,49 @@ sub import_patrons { next LINE unless $success; + # Send ACCTDETAILS welcome email is the user is new and we're set to send mail + if ($send_welcome && $is_new) { + my $emailaddr = $patron->notice_email_address; + + # if we manage to find a valid email address, send notice + if ($emailaddr) { + eval { + my $letter = GetPreparedLetter( + module => 'members', + letter_code => 'ACCTDETAILS', + branchcode => $patron->branchcode,, + lang => $patron->lang || 'default', + tables => { + 'branches' => $patron->branchcode, + 'borrowers' => $patron->borrowernumber, + }, + want_librarian => 1, + ) or return; + + my $message_id = EnqueueLetter( + { + letter => $letter, + borrowernumber => $patron->id, + to_address => $emailaddr, + message_transport_type => 'email' + } + ); + }; + if ($@) { + push @errors, { welcome_email_err => 1, borrowernumber => $borrowernumber }; + } else { + push( + @feedback, + { + feedback => 1, + name => 'welcome_sent', + value => $borrower{'surname'} . ' / ' . $borrowernumber . ' / ' . $emailaddr + } + ); + } + } + } + # Add a guarantor if we are given a relationship if ( $guarantor_id ) { my $relationship = Koha::Patron::Relationships->find( diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/import_borrowers.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/import_borrowers.tt index a0bb8409b7..42c6718337 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/import_borrowers.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/import_borrowers.tt @@ -344,6 +344,17 @@ [% END %] +
+ Welcome email +
    +
  • + + + ACCTDETAILS notice is used +
  • +
+
+
diff --git a/tools/import_borrowers.pl b/tools/import_borrowers.pl index b635b46e0a..3488bdbf44 100755 --- a/tools/import_borrowers.pl +++ b/tools/import_borrowers.pl @@ -97,6 +97,7 @@ my @preserve_fields = $input->multi_param('preserve_existing'); my $uploadborrowers = $input->param('uploadborrowers'); my $matchpoint = $input->param('matchpoint'); +my $welcome_new = $input->param('welcome_new'); if ($matchpoint) { $matchpoint =~ s/^patron_attribute_//; } @@ -128,6 +129,7 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) { overwrite_passwords => $overwrite_passwords, preserve_extended_attributes => scalar $input->param( 'ext_preserve' ) || 0, preserve_fields => \@preserve_fields, + send_welcome => $welcome_new, } ); -- 2.30.2