@@ -, +, @@ --- C4/Letters.pm | 4 +- C4/Members.pm | 45 ++++++++++++++++------ .../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 | 25 +++++++++++- 6 files changed, 70 insertions(+), 13 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_12532-RedirectGuaranteeEmail_syspref.sql --- a/C4/Letters.pm +++ a/C4/Letters.pm @@ -1304,7 +1304,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'}, --- a/C4/Members.pm +++ a/C4/Members.pm @@ -949,20 +949,43 @@ sub GetNoticeEmailAddress { my $borrowernumber = shift; my $which_address = C4::Context->preference("AutoEmailPrimaryAddress"); + my $address; + # if syspref is set to 'first valid' (value == OFF), look up email address if ( $which_address eq 'OFF' ) { - return GetFirstValidEmailAddress($borrowernumber); + $address = GetFirstValidEmailAddress($borrowernumber); } - # specified email address field - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare( qq{ - SELECT $which_address AS primaryemail - FROM borrowers - WHERE borrowernumber=? - } ); - $sth->execute($borrowernumber); - my $data = $sth->fetchrow_hashref; - return $data->{'primaryemail'} || ''; + else { + # specified address address field + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare( qq{ + SELECT $which_address AS primaryaddress + FROM borrowers + WHERE borrowernumber=? + } ); + $sth->execute($borrowernumber); + $address = $sth->fetchrow_hashref->{'primaryaddress'}; + } + + # If RedirectGuaranteeEmail is enabled, (and user as guarantor) append guarantor's email + my $use_guarantor = C4::Context->preference('RedirectGuaranteeEmail'); + if ($use_guarantor) { + my ($guarantor_id, $guarantor_address ); + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare( qq{ + SELECT guarantorid + FROM borrowers + WHERE borrowernumber=? + }); + $sth->execute($borrowernumber); + $guarantor_id = $sth->fetchrow_hashref->{'guarantorid'}; + + if ($guarantor_id) { + $guarantor_address = GetNoticeEmailAddress($guarantor_id); + $address .= ', ' . $guarantor_address if $guarantor_address; + } + } + return $address || ''; } =head2 GetBorrowersToExpunge --- a/installer/data/mysql/atomicupdate/bug_12532-RedirectGuaranteeEmail_syspref.sql +++ a/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'); --- a/installer/data/mysql/sysprefs.sql +++ a/installer/data/mysql/sysprefs.sql @@ -439,6 +439,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'), --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref @@ -193,6 +193,12 @@ Patrons: no: "Don't" - track last patron activity. - Everytime a patron will connect, the borrowers.lastseen will be updated with the current time. + - + - 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 --- a/t/db_dependent/Members.t +++ a/t/db_dependent/Members.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 63; +use Test::More tests => 64; use Test::MockModule; use Data::Dumper qw/Dumper/; use C4::Context; @@ -137,7 +137,24 @@ $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, ""); is ($checkcardnum, "2", "Card number is too long"); +# Test GetNoticeEmailAddress +# 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 ); +t::lib::Mocks::mock_preference( 'RedirectGuaranteeEmail', '0' ); t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'OFF' ); C4::Context->clear_syspref_cache(); @@ -150,6 +167,12 @@ C4::Context->clear_syspref_cache(); $notice_email = GetNoticeEmailAddress($member->{'borrowernumber'}); is ($notice_email, $EMAILPRO, "GetNoticeEmailAddress 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 = GetNoticeEmailAddress($member->{'borrowernumber'}); +is ($notice_email, $EMAIL . ", " . $GUARANTOR_EMAIL, "GetNoticeEmailAddress returns correct value when RedirectGuaranteeEmail is enabled"); + # Check_Userid tests %data = ( cardnumber => "123456789", --