@@ -, +, @@ - Add an email address - Update the 'Patron messaging preferences' section so that an email is sent for item checkouts --- C4/Letters.pm | 13 +++- Koha/Email.pm | 12 ++-- Koha/Patron.pm | 25 +++++++- ...ug_12532-RedirectGuaranteeEmail_syspref.pl | 14 +++++ installer/data/mysql/mandatory/sysprefs.sql | 1 + .../en/modules/admin/preferences/patrons.pref | 6 ++ t/db_dependent/Letters.t | 1 + t/db_dependent/Members.t | 60 +++++++++++++++---- 8 files changed, 110 insertions(+), 22 deletions(-) create mode 100755 installer/data/mysql/atomicupdate/bug_12532-RedirectGuaranteeEmail_syspref.pl --- a/C4/Letters.pm +++ a/C4/Letters.pm @@ -1319,9 +1319,10 @@ sub _send_message_by_email { my $patron; my $to_address = $message->{'to_address'}; - unless ($to_address) { + my $use_garantor = C4::Context->preference('RedirectGuaranteeEmail'); + if($use_garantor eq 'yes' || !$to_address) { $patron = Koha::Patrons->find( $message->{borrowernumber} ); - unless ($patron) { + unless ($patron or $to_address) { warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})"; _set_message_status( { @@ -1332,7 +1333,9 @@ sub _send_message_by_email { ); return; } - $to_address = $patron->notice_email_address; + 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? @@ -1470,6 +1473,10 @@ sub _send_message_by_email { $smtp_transports->{ $smtp_server->id // 'default' } ||= $smtp_server->transport; my $smtp_transport = $smtp_transports->{ $smtp_server->id // 'default' }; + _update_message_from_address($message->{'message_id'},$email->email->header('From') ) + if !$message->{from_address} + || $message->{from_address} ne $email->email->header('From'); + try { $email->send_or_die({ transport => $smtp_transport }); --- a/Koha/Email.pm +++ a/Koha/Email.pm @@ -118,10 +118,14 @@ sub create { $args->{to} = $params->{to}; } - Koha::Exceptions::BadParameter->throw( - error => "Invalid 'to' parameter: " . $args->{to}, - parameter => 'to' - ) unless Koha::Email->is_valid( $args->{to} ); # to is mandatory + my @emails = split(',', $args->{to}); + foreach my $email (@emails) { + $email =~ s/ //g; + Koha::Exceptions::BadParameter->throw( + error => "Invalid 'to' parameter: ".$email, + parameter => 'to' + ) unless Koha::Email->is_valid($email); + } my $addresses = {}; $addresses->{reply_to} = $params->{reply_to}; --- a/Koha/Patron.pm +++ a/Koha/Patron.pm @@ -1428,14 +1428,35 @@ Returns the empty string if no email address. sub notice_email_address{ my ( $self ) = @_; + my $address; + my $guarantor_address; my $which_address = C4::Context->preference("EmailFieldPrimary"); # 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 @guarantors = map { $_->guarantors->as_list } $self->guarantor_relationships(); + if (@guarantors) { + foreach my $guarantor (@guarantors) { + if ( $which_address eq 'OFF' ) { + $guarantor_address = $guarantor->first_valid_email_address; + } else { + $guarantor_address = $guarantor->$which_address || ''; + } + if ($address){ + $address .= ', '; + } + $address .= $guarantor_address if $guarantor_address; + } + } + } + return $address; } =head3 first_valid_email_address --- a/installer/data/mysql/atomicupdate/bug_12532-RedirectGuaranteeEmail_syspref.pl +++ a/installer/data/mysql/atomicupdate/bug_12532-RedirectGuaranteeEmail_syspref.pl @@ -0,0 +1,14 @@ +use Modern::Perl; + +return { + bug_number => "12532", + description => "Add new system preference RedirectGuaranteeEmail", + up => sub { + my ($args) = @_; + my ($dbh, $out) = @$args{qw(dbh out)}; + + $dbh->do(q{INSERT IGNORE INTO systempreferences (variable,value,options,explanation,type) VALUES ('RedirectGuaranteeEmail', '0', 'Enable the ability to redirect guarantee email messages to guarantor.', NULL, 'YesNo') }); + + say $out "Added system preference 'RedirectGuaranteeEmail'"; + }, +}; --- a/installer/data/mysql/mandatory/sysprefs.sql +++ a/installer/data/mysql/mandatory/sysprefs.sql @@ -602,6 +602,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('RecallsMaxPickUpDelay','7',NULL,'Define the maximum time a recall can be awaiting pickup','Integer'), ('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'), --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref @@ -357,6 +357,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: --- a/t/db_dependent/Letters.t +++ a/t/db_dependent/Letters.t @@ -869,6 +869,7 @@ subtest 'Test SMS handling in SendQueuedMessages' => sub { t::lib::Mocks::mock_preference( 'SMSSendDriver', 'Email' ); t::lib::Mocks::mock_preference('EmailSMSSendDriverFromAddress', ''); + t::lib::Mocks::mock_preference('RedirectGuaranteeEmail', '0'); my $patron = Koha::Patrons->find($borrowernumber); $dbh->do(q| INSERT INTO message_queue(borrowernumber, subject, content, message_transport_type, status, letter_code) --- a/t/db_dependent/Members.t +++ a/t/db_dependent/Members.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 50; +use Test::More tests => 57; use Test::MockModule; use Test::Exception; @@ -101,21 +101,55 @@ ok ( $changedmember->{firstname} eq $CHANGED_FIRSTNAME && , "Member Changed") or diag("Mismatching member details: ".Dumper($member, $changedmember)); -# Add a new borrower +# Test notice_email_address +# Add Guarantor for testing +my $GUARANTOR_EMAIL = "Robert\@email.com"; %data = ( - cardnumber => "123456789", - firstname => "Tomasito", - surname => "None", + cardnumber => "2997924548", + firstname => "Robert", + surname => "Tables", categorycode => $patron_category->{categorycode}, - branchcode => $library2->{branchcode}, - dateofbirth => '', - debarred => '', - dateexpiry => '', - dateenrolled => '', + branchcode => $BRANCHCODE, + dateofbirth => '', + dateexpiry => '9999-12-31', + userid => 'bobbytables', + email => $GUARANTOR_EMAIL ); -my $borrowernumber = Koha::Patron->new( \%data )->store->borrowernumber; -$patron = Koha::Patrons->find( $borrowernumber ); -my $borrower = $patron->unblessed; + +$addmem=Koha::Patron->new(\%data)->store->borrowernumber; +ok($addmem, "Koha::Patron->store()"); + +my $patron_guarantor = Koha::Patrons->find( { cardnumber => (\%data)->{'cardnumber'} } ) + or BAIL_OUT("Cannot read member with card (\%data)->{'cardnumber'}"); +my $member_guarantor = $patron_guarantor->unblessed; + +my %data2 = ( + guarantor_id => $member_guarantor->{borrowernumber}, + guarantee_id => $member->{borrowernumber}, + relationship => "father" +); +Koha::Patron::Relationship->new(\%data2)->store; + +$member = Koha::Patrons->find( { cardnumber => $CARDNUMBER } ); +t::lib::Mocks::mock_preference( 'RedirectGuaranteeEmail', '0' ); +t::lib::Mocks::mock_preference( 'EmailFieldPrimary', 'OFF' ); +C4::Context->clear_syspref_cache(); + +my $notice_email = $member->notice_email_address; +is ($notice_email, $EMAIL, "notice_email_address returns correct value when EmailFieldPrimary is off"); + +t::lib::Mocks::mock_preference( 'EmailFieldPrimary', 'emailpro' ); +C4::Context->clear_syspref_cache(); + +$notice_email = $member->notice_email_address; +is ($notice_email, $EMAILPRO, "notice_email_address returns correct value when EmailFieldPrimary is emailpro"); + +t::lib::Mocks::mock_preference( 'EmailFieldPrimary', '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"); + is( $borrower->{dateofbirth}, undef, 'Koha::Patron->store should undef dateofbirth if empty string is given'); is( $borrower->{debarred}, undef, 'Koha::Patron->store should undef debarred if empty string is given'); isnt( $borrower->{dateexpiry}, '0000-00-00', 'Koha::Patron->store should not set dateexpiry to 0000-00-00 if empty string is given'); --