From f746007883028d2a8f65e490820a4ad7a448d4e3 Mon Sep 17 00:00:00 2001 From: David Bourgault Date: Wed, 13 Sep 2017 09:32:38 -0400 Subject: [PATCH] Bug 12532 - Send emails to guarantee and guarantor Fixed patch didnt apply. Adds setting "RedirectGuaranteeEmail". When setting is enabled, emails to patron with a guarantor will be send to both guarantor and guarantee. Does this by amending the Koha::Patrons::notice_email_address sub. It does not affect values in the message_queue table. Test plan: Before applying 1) Select patron with guarantor 2) Enable checkout emails 3) Checkout item. Email will be sent only to guarantee 4) Apply patch 5) Run updatedatabase.pl 6) Enable 'RedirectGuaranteeEmail' in system preferences 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 --- C4/Letters.pm | 4 ++- Koha/Patron.pm | 18 ++++++++-- .../bug_12532-RedirectGuaranteeEmail_syspref.sql | 2 ++ installer/data/mysql/sysprefs.sql | 1 + .../prog/en/modules/admin/preferences/patrons.pref | 6 ++++ t/db_dependent/Members.t | 38 +++++++++++++++++++++- 6 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_12532-RedirectGuaranteeEmail_syspref.sql diff --git a/C4/Letters.pm b/C4/Letters.pm index 219779b..fc754bf 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -1308,7 +1308,9 @@ sub _send_message_by_email { my $patron = Koha::Patrons->find( $message->{borrowernumber} ); my $to_address = $message->{'to_address'}; - unless ($to_address) { + + my $use_garantor = C4::Context->preference('RedirectGuaranteeEmail'); + if($use_garantor || !$to_address) { unless ($patron) { warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})"; _set_message_status( { message_id => $message->{'message_id'}, diff --git a/Koha/Patron.pm b/Koha/Patron.pm index d0be998..9393e9a 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -631,14 +631,28 @@ Returns the empty string if no email address. sub notice_email_address{ my ( $self ) = @_; + my $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 || ''; + my $use_guarantor = C4::Context->preference('RedirectGuaranteeEmail'); + if ($use_guarantor) { + my $guarantor = $self->guarantor; + if ($guarantor) { + my $guarantor_address = $guarantor->notice_email_address; + if ($address){ + $address .= ', '; + } + $address .= $guarantor_address if $guarantor_address; + } + } + return $address; } =head3 first_valid_email_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 0000000..e3de7b1 --- /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/sysprefs.sql b/installer/data/mysql/sysprefs.sql index 76bb2cc..2e6cc06 100644 --- a/installer/data/mysql/sysprefs.sql +++ b/installer/data/mysql/sysprefs.sql @@ -448,6 +448,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('QuoteOfTheDay','0',NULL,'Enable or disable display of Quote of the Day on the OPAC home page','YesNo'), ('RandomizeHoldsQueueWeight','0',NULL,'if ON, the holds queue in circulation will be randomized, either based on all location codes, or by the location codes specified in StaticHoldsQueueWeight','YesNo'), ('RecordLocalUseOnReturn','0',NULL,'If ON, statistically record returns of unissued items as local use, instead of return','YesNo'), +('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'), ('RenewalLog','0','','If ON, log information about renewals','YesNo'), ('RenewalPeriodBase','date_due','date_due|now','Set whether the renewal date should be counted from the date_due or from the moment the Patron asks for renewal ','Choice'), 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 fe4da67..9f66e29 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 @@ -205,6 +205,12 @@ Patrons: - pref: FailedLoginAttempts class: integer - failed login attempts. + - + - pref: RedirectGuaranteeEmail + choices: + yes: Enable + no: Disable + - sending emails to both guarantees and their guarantor. This does not affect patrons without guarantors. "Norwegian patron database": - - pref: NorwegianPatronDBEnable diff --git a/t/db_dependent/Members.t b/t/db_dependent/Members.t index c7bc6b3..9251d97 100755 --- a/t/db_dependent/Members.t +++ b/t/db_dependent/Members.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 60; +use Test::More tests => 65; use Test::MockModule; use Test::Exception; @@ -138,6 +138,42 @@ 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} = AddMember( %data ); +ModMember( %$member ); + +$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"); # Check_Userid tests %data = ( -- 2.7.4