View | Details | Raw Unified | Return to bug 5770
Collapse All | Expand All

(-)a/C4/Letters.pm (+5 lines)
Lines 1077-1082 ENDSQL Link Here
1077
        push @query_params, $params->{'borrowernumber'};
1077
        push @query_params, $params->{'borrowernumber'};
1078
    }
1078
    }
1079
1079
1080
    if ( exists $params->{'letter_code'} ) {
1081
        push @whereclauses, ' letter_code = ? ';
1082
        push @query_params, $params->{'letter_code'};
1083
    }
1084
1080
    if ( @whereclauses ) {
1085
    if ( @whereclauses ) {
1081
        $statement .= ' WHERE ' . join( 'AND', @whereclauses );
1086
        $statement .= ' WHERE ' . join( 'AND', @whereclauses );
1082
    }
1087
    }
(-)a/C4/Suggestions.pm (-1 / +1 lines)
Lines 471-477 sub NewSuggestion { Link Here
471
        my $emailpurchasesuggestions = C4::Context->preference("EmailPurchaseSuggestions");
471
        my $emailpurchasesuggestions = C4::Context->preference("EmailPurchaseSuggestions");
472
472
473
        my $toaddress = ( $emailpurchasesuggestions eq "BranchEmailAddress" )
473
        my $toaddress = ( $emailpurchasesuggestions eq "BranchEmailAddress" )
474
            ? Koha::Libraries->find($full_suggestion->{branchcode})->branchemail
474
            ? ( Koha::Libraries->find($full_suggestion->{branchcode})->branchemail || C4::Context->preference('KohaAdminEmailAddress') )
475
            : C4::Context->preference( $emailpurchasesuggestions ) ;
475
            : C4::Context->preference( $emailpurchasesuggestions ) ;
476
476
477
        C4::Letters::EnqueueLetter(
477
        C4::Letters::EnqueueLetter(
(-)a/installer/data/mysql/atomicupdate/bug_5770.perl (+39 lines)
Line 0 Link Here
1
$DBversion = '18.12.00.XXX'; # will be replaced by the RM
2
if( CheckVersion( $DBversion ) ) {
3
    $dbh->do( q{
4
        INSERT IGNORE INTO systempreferences (variable,value,options,explanation,type) VALUES
5
            ('EmailPurchaseSuggestions','KohaAdminEmailAddress','EmailAddressForSuggestions|BranchEmailAddress|KohaAdminEmailAddress','Choose email address that will be sent new purchase suggestions','Choice'),
6
            ('EmailAddressForSuggestions','','','If you choose EmailAddressForSuggestions you should enter a valid email address','free')
7
    });
8
    $dbh->do( q{
9
        INSERT IGNORE INTO `letter` (module, code, name, title, content, is_html, message_transport_type) VALUES
10
        ('suggestions','NEW_SUGGESTION','New suggestion','New suggestion','<h3>Suggestion pending approval</h3>
11
        <p><h4>Suggested by</h4>
12
            <ul>
13
                <li><<borrowers.firstname>> <<borrowers.surname>></li>
14
                <li><<borrowers.cardnumber>></li>
15
                <li><<borrowers.phone>></li>
16
                <li><<borrowers.email>></li>
17
            </ul>
18
        </p>
19
        <p><h4>Title suggested</h4>
20
             <ul>
21
                <li><b>Library:</b> <<branches.branchname>></li>
22
                <li><b>Title:</b> <<suggestions.title>></li>
23
                <li><b>Author:</b> <<suggestions.author>></li>
24
                <li><b>Copyright date:</b> <<suggestions.copyrightdate>></li>
25
                <li><b>Standard number (ISBN, ISSN or other):</b> <<suggestions.isbn>></li>
26
                <li><b>Publisher:</b> <<suggestions.publishercode>></li>
27
                <li><b>Collection title:</b> <<suggestions.collectiontitle>></li>
28
                <li><b>Publication place:</b> <<suggestions.place>></li>
29
                <li><b>Quantity:</b> <<suggestions.quantity>></li>
30
                <li><b>Item type:</b> <<suggestions.itemtype>></li>
31
                <li><b>Reason for suggestion:</b> <<suggestions.patronreason>></li>
32
                <li><b>Notes:</b> <<suggestions.note>></li>
33
            </ul>
34
        </p>',1, 'email')
35
    });
36
37
    SetVersion( $DBversion );
38
    print "Upgrade to $DBversion done (Bug 5770 - Email librarian when purchase suggestion made)\n";
39
}
(-)a/installer/data/mysql/atomicupdate/bug_5770.sql (-30 lines)
Lines 1-30 Link Here
1
INSERT IGNORE INTO systempreferences (variable,value,options,explanation,type) VALUES
2
('EmailPurchaseSuggestions','KohaAdminEmailAddress','EmailAddressForSuggestions|BranchEmailAddress|KohaAdminEmailAddress','Choose email address that will be sent new purchase suggestions','Choice'),
3
('EmailAddressForSuggestions','','','If you choose EmailAddressForSuggestions you should enter a valid email address','free');
4
5
INSERT IGNORE INTO `letter` (module, code, name, title, content, is_html, message_transport_type)
6
VALUES ('suggestions','NEW_SUGGESTION','New suggestion','New suggestion','<h3>Suggestion pending approval</h3>
7
<p><h4>Suggested by</h4>
8
 <ul>
9
   <li><<borrowers.firstname>> <<borrowers.surname>></li>
10
   <li><<borrowers.cardnumber>></li>
11
   <li><<borrowers.phone>></li>
12
   <li><<borrowers.email>></li>
13
 </ul>
14
</p>
15
<p><h4>Title suggested</h4>
16
 <ul>
17
   <li><b>Library:</b> <<branches.branchname>></li>
18
   <li><b>Title:</b> <<suggestions.title>></li>
19
   <li><b>Author:</b> <<suggestions.author>></li>
20
   <li><b>Copyright date:</b> <<suggestions.copyrightdate>></li>
21
   <li><b>Standard number (ISBN, ISSN or other):</b> <<suggestions.isbn>></li>
22
   <li><b>Publisher:</b> <<suggestions.publishercode>></li>
23
   <li><b>Collection title:</b> <<suggestions.collectiontitle>></li>
24
   <li><b>Publication place:</b> <<suggestions.place>></li>
25
   <li><b>Quantity:</b> <<suggestions.quantity>></li>
26
   <li><b>Item type:</b> <<suggestions.itemtype>></li>
27
   <li><b>Reason for suggestion:</b> <<suggestions.patronreason>></li>
28
   <li><b>Notes:</b> <<suggestions.note>></li>
29
 </ul>
30
</p>',1, 'email');
(-)a/t/db_dependent/Suggestions.t (-3 / +56 lines)
Lines 18-24 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use DateTime::Duration;
20
use DateTime::Duration;
21
use Test::More tests => 106;
21
use Test::More tests => 107;
22
use Test::Warn;
22
use Test::Warn;
23
23
24
use t::lib::Mocks;
24
use t::lib::Mocks;
Lines 32-38 use Koha::DateUtils qw( dt_from_string output_pref ); Link Here
32
use Koha::Libraries;
32
use Koha::Libraries;
33
use Koha::Patrons;
33
use Koha::Patrons;
34
use Koha::Suggestions;
34
use Koha::Suggestions;
35
36
BEGIN {
35
BEGIN {
37
    use_ok('C4::Suggestions');
36
    use_ok('C4::Suggestions');
38
}
37
}
Lines 61-66 $dbh->do(q|DELETE FROM borrowers|); Link Here
61
$dbh->do(q|DELETE FROM letter|);
60
$dbh->do(q|DELETE FROM letter|);
62
$dbh->do(q|DELETE FROM message_queue|);
61
$dbh->do(q|DELETE FROM message_queue|);
63
$dbh->do(q|INSERT INTO letter(module, code, content) VALUES ('suggestions', 'CHECKED', 'my content')|);
62
$dbh->do(q|INSERT INTO letter(module, code, content) VALUES ('suggestions', 'CHECKED', 'my content')|);
63
$dbh->do(q|INSERT INTO letter(module, code, content) VALUES ('suggestions', 'NEW_SUGGESTION', 'Content for new suggestion')|);
64
64
65
# Add CPL if missing.
65
# Add CPL if missing.
66
if (not defined Koha::Libraries->find('CPL')) {
66
if (not defined Koha::Libraries->find('CPL')) {
Lines 95-100 my $my_suggestion = { Link Here
95
    publishercode => 'my publishercode',
95
    publishercode => 'my publishercode',
96
    suggestedby   => $borrowernumber,
96
    suggestedby   => $borrowernumber,
97
    biblionumber  => $biblionumber1,
97
    biblionumber  => $biblionumber1,
98
    branchcode    => 'CPL',
98
    managedby     => '',
99
    managedby     => '',
99
    manageddate   => '',
100
    manageddate   => '',
100
    accepteddate  => dt_from_string,
101
    accepteddate  => dt_from_string,
Lines 190-195 is( $suggestion->{note}, 'my note', 'ModSuggestion should not erase data if not Link Here
190
191
191
my $messages = C4::Letters::GetQueuedMessages({
192
my $messages = C4::Letters::GetQueuedMessages({
192
    borrowernumber => $borrowernumber,
193
    borrowernumber => $borrowernumber,
194
    letter_code => 'CHECKED',
193
});
195
});
194
is( @$messages, 0, 'ModSuggestions does not send an email if the status is not updated' );
196
is( @$messages, 0, 'ModSuggestions does not send an email if the status is not updated' );
195
197
Lines 217-222 $suggestion = GetSuggestion($my_suggestionid); Link Here
217
is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'ModSuggestion modifies the status correctly' );
219
is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'ModSuggestion modifies the status correctly' );
218
$messages = C4::Letters::GetQueuedMessages({
220
$messages = C4::Letters::GetQueuedMessages({
219
    borrowernumber => $borrowernumber,
221
    borrowernumber => $borrowernumber,
222
    letter_code => 'CHECKED',
220
});
223
});
221
is( @$messages, 1, 'ModSuggestion sends an email if the status is updated' );
224
is( @$messages, 1, 'ModSuggestion sends an email if the status is updated' );
222
is ($messages->[0]->{message_transport_type}, 'email', 'When FallbackToSMSIfNoEmail syspref is disabled the suggestion message_transport_type is always email');
225
is ($messages->[0]->{message_transport_type}, 'email', 'When FallbackToSMSIfNoEmail syspref is disabled the suggestion message_transport_type is always email');
Lines 227-232 t::lib::Mocks::mock_preference( 'FallbackToSMSIfNoEmail', 1 ); Link Here
227
ModSuggestion($mod_suggestion3);
230
ModSuggestion($mod_suggestion3);
228
$messages = C4::Letters::GetQueuedMessages({
231
$messages = C4::Letters::GetQueuedMessages({
229
    borrowernumber => $borrowernumber,
232
    borrowernumber => $borrowernumber,
233
    letter_code => 'CHECKED',
230
});
234
});
231
is ($messages->[1]->{message_transport_type}, 'sms', 'When FallbackToSMSIfNoEmail syspref is enabled the suggestion message_transport_type is sms if the borrower has no email');
235
is ($messages->[1]->{message_transport_type}, 'sms', 'When FallbackToSMSIfNoEmail syspref is enabled the suggestion message_transport_type is sms if the borrower has no email');
232
236
Lines 242-247 my $mod_suggestion4 = { Link Here
242
ModSuggestion($mod_suggestion4);
246
ModSuggestion($mod_suggestion4);
243
$messages = C4::Letters::GetQueuedMessages({
247
$messages = C4::Letters::GetQueuedMessages({
244
    borrowernumber => $borrowernumber2,
248
    borrowernumber => $borrowernumber2,
249
    letter_code => 'CHECKED',
245
});
250
});
246
is ($messages->[0]->{message_transport_type}, 'email', 'When FallbackToSMSIfNoEmail syspref is enabled the suggestion message_transport_type is email if the borrower has an email');
251
is ($messages->[0]->{message_transport_type}, 'email', 'When FallbackToSMSIfNoEmail syspref is enabled the suggestion message_transport_type is email if the borrower has an email');
247
252
Lines 462-465 subtest 'DelSuggestionsOlderThan' => sub { Link Here
462
    is( Koha::Suggestions->count, 2, '1 suggestions>1d deleted' );
467
    is( Koha::Suggestions->count, 2, '1 suggestions>1d deleted' );
463
};
468
};
464
469
470
subtest 'EmailPurchaseSuggestions' => sub {
471
    plan tests => 5;
472
473
    $dbh->do(q|DELETE FROM message_queue|);
474
475
    Koha::Libraries->find('CPL')->update({ branchemail => 'branchemail@b.c' });
476
    t::lib::Mocks::mock_preference( "KohaAdminEmailAddress", 'root@localhost');
477
    t::lib::Mocks::mock_preference( "EmailAddressForSuggestions", 'a@b.c');
478
    t::lib::Mocks::mock_preference( "EmailPurchaseSuggestions", "KohaAdminEmailAddress"); # EmailAddressForSuggestions or BranchEmailAddress or KohaAdminEmailAddress
479
480
    NewSuggestion($my_suggestion);
481
    my $newsuggestions_messages = C4::Letters::GetQueuedMessages({
482
            borrowernumber => $borrowernumber,
483
            letter_code => 'NEW_SUGGESTION',
484
    });
485
486
    is( @$newsuggestions_messages, 1, 'NewSuggestions sends an email' );
487
    my $message1 = C4::Letters::GetMessage( $newsuggestions_messages->[0]->{message_id});
488
    is ($message1->{to_address}, 'root@localhost', 'to_address is KohaAdminEmailAddress');
489
490
    t::lib::Mocks::mock_preference( "EmailPurchaseSuggestions", "EmailAddressForSuggestions");
491
    NewSuggestion($my_suggestion);
492
    $newsuggestions_messages = C4::Letters::GetQueuedMessages({
493
        borrowernumber => $borrowernumber,
494
        letter_code => 'NEW_SUGGESTION',
495
    });
496
    my $message2 = C4::Letters::GetMessage( $newsuggestions_messages->[1]->{message_id});
497
    is ($message2->{to_address}, 'a@b.c', 'to_address is EmailAddressForSuggestions');
498
499
    t::lib::Mocks::mock_preference( "EmailPurchaseSuggestions", "BranchEmailAddress");
500
    NewSuggestion($my_suggestion);
501
    $newsuggestions_messages = C4::Letters::GetQueuedMessages({
502
        borrowernumber => $borrowernumber,
503
        letter_code => 'NEW_SUGGESTION',
504
    });
505
    my $message3 = C4::Letters::GetMessage( $newsuggestions_messages->[2]->{message_id});
506
    is ($message3->{to_address}, 'branchemail@b.c', 'to_address is BranchEmailAddress');
507
508
    Koha::Libraries->find('CPL')->update({ branchemail => undef });
509
    NewSuggestion($my_suggestion);
510
    $newsuggestions_messages = C4::Letters::GetQueuedMessages({
511
        borrowernumber => $borrowernumber,
512
        letter_code => 'NEW_SUGGESTION',
513
    });
514
    my $message4 = C4::Letters::GetMessage( $newsuggestions_messages->[3]->{message_id});
515
    isnt ($message4->{to_address}, 'branchemail@b.c', 'to_address is KohaAdminEmailAddress. Because branchemail is undef');
516
517
};
518
465
$schema->storage->txn_rollback;
519
$schema->storage->txn_rollback;
466
- 

Return to bug 5770