From 56b3d1be01c4c131a6976eac589a23827832ed3c Mon Sep 17 00:00:00 2001 From: Charles Farmer Date: Sat, 5 May 2018 22:38:37 -0400 Subject: [PATCH] Bug 12532 - Send emails to guarantee and guarantor Checks for the syspref "RedirectGuaranteeEmail" in Koha::Patrons::notice_email_address. When enabled, emails to a patron with a guarantor will be sent to both guarantor and guarantee. It does not affect values in the message_queue table. Test plan: Before applying 1) Search, or create, a patron with guarantor 2) Enable checkout emails 3) Checkout an item. An email should be sent only to the guarantee 4) Apply this patch 5) Run updatedatabase.pl 6) Enable 'RedirectGuaranteeEmail' 7) Go through step 1) to 3). Email should now be sent to both. 8) Run t/db_dependant/Members.t Sponsored by: Ville de Victoriaville Bug 12532 - (follow-up) clean up --- C4/Letters.pm | 8 +++- Koha/Patron.pm | 4 +- ...g_12532-RedirectGuaranteeEmail_syspref.sql | 2 + installer/data/mysql/mandatory/sysprefs.sql | 1 + .../en/modules/admin/preferences/patrons.pref | 6 +++ t/db_dependent/Members.t | 37 +++++++++++++++++++ 6 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_12532-RedirectGuaranteeEmail_syspref.sql diff --git a/C4/Letters.pm b/C4/Letters.pm index c36553ac9a..4c21c62a1d 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -1333,8 +1333,10 @@ sub _send_message_by_email { my $patron = Koha::Patrons->find( $message->{borrowernumber} ); my $to_address = $message->{'to_address'}; - unless ($to_address) { - unless ($patron) { + + my $use_garantor = C4::Context->preference('RedirectGuaranteeEmail'); + if($use_garantor || !$to_address) { + unless ($patron or $to_address) { warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})"; _set_message_status( { @@ -1345,7 +1347,9 @@ sub _send_message_by_email { ); return; } + if ($patron) { $to_address = $patron->notice_email_address; + } unless ($to_address) { # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})"; # warning too verbose for this more common case? diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 9968d36350..2899062dd0 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -1329,7 +1329,9 @@ sub notice_email_address{ my $which_address = C4::Context->preference("AutoEmailPrimaryAddress"); # if syspref is set to 'first valid' (value == OFF), look up email address if ( $which_address eq 'OFF' ) { - return $self->first_valid_email_address; + $address = $self->first_valid_email_address; + } else { + $address = $self->$which_address || ''; } return $self->$which_address || ''; diff --git a/installer/data/mysql/atomicupdate/bug_12532-RedirectGuaranteeEmail_syspref.sql b/installer/data/mysql/atomicupdate/bug_12532-RedirectGuaranteeEmail_syspref.sql new file mode 100644 index 0000000000..e3de7b1303 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_12532-RedirectGuaranteeEmail_syspref.sql @@ -0,0 +1,2 @@ +INSERT IGNORE INTO systempreferences (variable, value, explanation, options, type) +VALUES ('RedirectGuaranteeEmail','0',NULL,'Enable the ability to redirect guarantee email messages to guarantor.','YesNo'); \ No newline at end of file diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index dd83a00fd8..d49992cc99 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -552,6 +552,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('RecordStaffUserOnCheckout','0',NULL,'If ON, when an item is checked out, the user who checked out the item is recorded','YesNo'), ('RecordLocalUseOnReturn','0',NULL,'If ON, statistically record returns of unissued items as local use, instead of return','YesNo'), ('Reference_NFL_Statuses','1|2',NULL,'Contains not for loan statuses considered as available for reference','Free'), +('RedirectGuaranteeEmail', '0', NULL, 'Enable the ability to redirect guarantee email messages to guarantor.', 'YesNo'), ('RefundLostOnReturnControl','CheckinLibrary','CheckinLibrary|ItemHomeBranch|ItemHoldingBranch','If a lost item is returned, choose which branch to pick rules for refunding.','Choice'), ('RenewAccruingItemWhenPaid','0','','If enabled, when the fines on an item accruing is paid off, attempt to renew that item. If the syspref "RenewalPeriodBase" is set to "due date", renewed items may still be overdue','YesNo'), ('RenewAccruingItemInOpac','0','','If enabled, when the fines on an item accruing is paid off in the OPAC via a payment plugin, attempt to renew that item. If the syspref "RenewalPeriodBase" is set to "due date", renewed items may still be overdue','YesNo'), diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref index 9ef410f24c..d604aa7ffe 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref @@ -320,6 +320,12 @@ Patrons: 1: Allow 0: "Don't allow" - staff to set the ability for a patron's checkouts to be viewed by linked patrons in the OPAC. + - + - pref: RedirectGuaranteeEmail + choices: + yes: Enable + no: Disable + - sending emails to both guarantees and their guarantor. This does not affect patrons without guarantors. - - pref: AllowStaffToSetFinesVisibilityForGuarantor choices: diff --git a/t/db_dependent/Members.t b/t/db_dependent/Members.t index f65a0e53ad..7a1a4ef995 100755 --- a/t/db_dependent/Members.t +++ b/t/db_dependent/Members.t @@ -119,6 +119,43 @@ C4::Context->clear_syspref_cache(); $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, ""); is ($checkcardnum, "2", "Card number is too long"); +# Test notice_email_address +# Add Guarantor for testing +my $GUARANTOR_EMAIL = "Robert\@email.com"; +%data = ( + cardnumber => "2997924548", + firstname => "Robert", + surname => "Tables", + categorycode => $patron_category->{categorycode}, + branchcode => $BRANCHCODE, + dateofbirth => '', + dateexpiry => '9999-12-31', + userid => 'bobbytables', + email => $GUARANTOR_EMAIL +); + +$member->{guarantorid} = Koha::Patron->new(\%data)->store->borrowernumber; +$patron->set($member)->store; + +$member = Koha::Patrons->find( { cardnumber => $CARDNUMBER } ); +t::lib::Mocks::mock_preference( 'RedirectGuaranteeEmail', '0' ); +t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'OFF' ); +C4::Context->clear_syspref_cache(); + +my $notice_email = $member->notice_email_address; +is ($notice_email, $EMAIL, "notice_email_address returns correct value when AutoEmailPrimaryAddress is off"); + +t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'emailpro' ); +C4::Context->clear_syspref_cache(); + +$notice_email = $member->notice_email_address; +is ($notice_email, $EMAILPRO, "notice_email_address returns correct value when AutoEmailPrimaryAddress is emailpro"); + +t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'OFF' ); +t::lib::Mocks::mock_preference( 'RedirectGuaranteeEmail', '1' ); +C4::Context->clear_syspref_cache(); +$notice_email = $member->notice_email_address; +is ($notice_email, $EMAIL . ", " . $GUARANTOR_EMAIL, "notice_email_address returns correct value when RedirectGuaranteeEmail is enabled"); # Add a new borrower %data = ( -- 2.25.1