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

(-)a/C4/Letters.pm (-1 / +1 lines)
Lines 48-54 BEGIN { Link Here
48
    require Exporter;
48
    require Exporter;
49
    @ISA = qw(Exporter);
49
    @ISA = qw(Exporter);
50
    @EXPORT = qw(
50
    @EXPORT = qw(
51
        &GetLetters &GetLettersAvailableForALibrary &GetLetterTemplates &DelLetter &GetPreparedLetter &GetWrappedLetter &SendAlerts &GetPrintMessages &GetMessageTransportTypes
51
        &EnqueueLetter &GetLetters &GetLettersAvailableForALibrary &GetLetterTemplates &DelLetter &GetPreparedLetter &GetWrappedLetter &SendAlerts &GetPrintMessages &GetMessageTransportTypes
52
    );
52
    );
53
}
53
}
54
54
(-)a/Koha/Patron.pm (+70 lines)
Lines 28-33 use Unicode::Normalize; Link Here
28
use C4::Context;
28
use C4::Context;
29
use C4::Log;
29
use C4::Log;
30
use Koha::Account;
30
use Koha::Account;
31
use C4::Letters qw( GetPreparedLetter EnqueueLetter );
31
use Koha::AuthUtils;
32
use Koha::AuthUtils;
32
use Koha::Checkouts;
33
use Koha::Checkouts;
33
use Koha::Club::Enrollments;
34
use Koha::Club::Enrollments;
Lines 1632-1637 sub to_api_mapping { Link Here
1632
    };
1633
    };
1633
}
1634
}
1634
1635
1636
=head3 send_notice
1637
1638
    Koha::Patrons->send_notice({ letter_params => $letter_params, message_name => 'DUE'});
1639
    Koha::Patrons->send_notice({ letter_params => $letter_params, message_transports => \@message_transports });
1640
    Koha::Patrons->send_notice({ letter_params => $letter_params, message_transports => \@message_transports, test_mode => 1 });
1641
1642
    Queue messages to a patron. Can pass a message that is part of the message_attributes
1643
    table or supply the transport to use.
1644
1645
    If passed a message name we retrieve the patrons preferences for transports
1646
    Otherwise we use the supplied transport. In the case of email or sms we fall back to print if
1647
    we have no address/number for sending
1648
1649
    $letter_params is a hashref of the values to be passed to GetPreparedLetter
1650
1651
    test_mode will only report which notices would be sent, but nothign will be queued
1652
1653
=cut
1654
1655
sub send_notice {
1656
    my ( $self, $params ) = @_;
1657
    my $letter_params = $params->{letter_params};
1658
    my $test_mode = $params->{test_mode};
1659
1660
    return unless $letter_params;
1661
    return unless exists $params->{message_name} xor $params->{message_transports}; # We only want one of these
1662
1663
    my $library = Koha::Libraries->find( $letter_params->{branchcode} )->unblessed;
1664
    my $admin_email_address = $library->{branchemail} || C4::Context->preference('KohaAdminEmailAddress');
1665
1666
    my @message_transports;
1667
    my $letter_code;
1668
    $letter_code = $letter_params->{letter_code};
1669
    if( $params->{message_name} ){
1670
        my $messaging_prefs = C4::Members::Messaging::GetMessagingPreferences( {
1671
                borrowernumber => $letter_params->{borrowernumber},
1672
                message_name => $params->{message_name}
1673
        } );
1674
        @message_transports = ( keys %{ $messaging_prefs->{transports} } );
1675
        $letter_code = $messaging_prefs->{transports}->{$message_transports[0]} unless $letter_code;
1676
    } else {
1677
        @message_transports = @{$params->{message_transports}};
1678
    }
1679
    return unless defined $letter_code;
1680
    $letter_params->{letter_code} = $letter_code;
1681
    my $print_sent = 0;
1682
    my %return;
1683
    foreach my $mtt (@message_transports){
1684
        next if ($mtt eq 'phone' and C4::Context->preference('TalkingTechItivaPhoneNotification') );
1685
        # Phone notices are handled by TalkingTech_itiva_outbound.pl
1686
        if( ($mtt eq 'email' and not $self->notice_email_address) or ($mtt eq 'sms' and not $self->smsalertnumber) ){
1687
            push @{$return{fallback}}, $mtt;
1688
            $mtt = 'print';
1689
        }
1690
        next if $mtt eq 'print' && $print_sent;
1691
        $letter_params->{message_transport_type} = $mtt;
1692
        my $letter = C4::Letters::GetPreparedLetter( %$letter_params );
1693
        C4::Letters::EnqueueLetter({
1694
            letter => $letter,
1695
            borrowernumber => $self->borrowernumber,
1696
            from_address   => $admin_email_address,
1697
            message_transport_type => $mtt
1698
        }) unless $test_mode;
1699
        push @{$return{sent}}, $mtt;
1700
        $print_sent = 1 if $mtt eq 'print';
1701
    }
1702
    return \%return;
1703
}
1704
1635
=head2 Internal methods
1705
=head2 Internal methods
1636
1706
1637
=head3 _type
1707
=head3 _type
(-)a/misc/cronjobs/holds_reminder.pl (+293 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
BEGIN {
21
22
    # find Koha's Perl modules
23
    # test carefully before changing this
24
    use FindBin;
25
    eval { require "$FindBin::Bin/../kohalib.pl" };
26
}
27
28
use Getopt::Long;
29
use Pod::Usage;
30
use Text::CSV_XS;
31
use DateTime;
32
use DateTime::Duration;
33
34
use C4::Context;
35
use C4::Letters;
36
use C4::Log;
37
use Koha::DateUtils;
38
use Koha::Calendar;
39
use Koha::Libraries;
40
use Koha::Script -cron;
41
42
=head1 NAME
43
44
holds_reminder.pl - prepare reminder messages to be sent to patrons with waiting holds
45
46
=head1 SYNOPSIS
47
48
holds_reminder.pl
49
  [ -n ][ -library <branchcode> ][ -library <branchcode> ... ]
50
  [ -days <number of days> ][ -csv [<filename>] ][ -itemscontent <field list> ]
51
  [ -email <email_type> ... ]
52
53
 Options:
54
   -help                          brief help message
55
   -man                           full documentation
56
   -v                             verbose
57
   -n                             No email will be sent
58
   -days          <days>          days waiting to deal with
59
   -lettercode   <lettercode>     predefined notice to use
60
   -library      <branchname>     only deal with holds from this library (repeatable : several libraries can be given)
61
   -holidays                      use the calendar to not count holidays as waiting days
62
   -mtt          <message_transport_type> type of messages to send, default is to use patrons messaging preferences for Hold filled
63
                                  populating this will force send even if patron has not chosen to receive hold notices
64
                                  email and sms will fallback to print if borrower does not have an address/phone
65
   -date                          Send notices as would have been sent on a specific date
66
67
=head1 OPTIONS
68
69
=over 8
70
71
=item B<-help>
72
73
Print a brief help message and exits.
74
75
=item B<-man>
76
77
Prints the manual page and exits.
78
79
=item B<-v>
80
81
Verbose. Without this flag set, only fatal errors are reported.
82
83
=item B<-n>
84
85
Do not send any email (test-mode) . If verbose a list of notices that would have been sent to
86
the patrons are printed to standard out.
87
88
=item B<-days>
89
90
Optional parameter, number of days an items has been 'waiting' on hold
91
to send a message for. If not included a notice will be sent to all
92
patrons with waiting holds.
93
94
=item B<-library>
95
96
select notices for one specific library. Use the value in the
97
branches.branchcode table. This option can be repeated in order
98
to select notices for a group of libraries.
99
100
=item B<-holidays>
101
102
This option determines whether library holidays are used when calculating how
103
long an item has been waiting. If enabled the count will skip closed days.
104
105
=item B<-date>
106
107
use it in order to send notices on a specific date and not Now. Format: YYYY-MM-DD.
108
109
=item B<-mtt>
110
111
send a notices via a specific transport, this can be repeated to send various notices.
112
If omitted the patron's messaging preferences for Hold notices will be used.
113
If supplied the notice types will be force sent even if patron has not selected hold notices
114
Email and SMS will fall back to print if there is no valid info in the patron's account
115
116
117
=back
118
119
=head1 DESCRIPTION
120
121
This script is designed to alert patrons of waiting
122
holds.
123
124
=head2 Configuration
125
126
This script sends reminders to patrons with waiting holds using a notice
127
defined in the Tools->Notices & slips module within Koha. The lettercode
128
is passed into this script and, along with other options, determine the content
129
of the notices sent to patrons.
130
131
132
=head1 USAGE EXAMPLES
133
134
C<holds_reminder.pl> - With no arguments the simple help is printed
135
136
C<holds_reminder.pl -lettercode CODE > In this most basic usage all
137
libraries are processed individually, and notices are prepared for
138
all patrons with waiting holds for whom we have email addresses.
139
Messages for those patrons for whom we have no email
140
address are sent in a single attachment to the library administrator's
141
email address, or to the address in the KohaAdminEmailAddress system
142
preference.
143
144
C<holds_reminder.pl -lettercode CODE -n -csv /tmp/holds_reminder.csv> - sends no email and
145
populates F</tmp/holds_reminder.csv> with information about all waiting holds
146
items.
147
148
C<holds_reminder.pl -lettercode CODE -library MAIN -days 14> - prepare notices of
149
holds waiting for 2 weeks for the MAIN library.
150
151
C<holds_reminder.pl -library MAIN -days 14 -list-all> - prepare notices
152
of holds waiting for 2 weeks for the MAIN library and include all the
153
patron's waiting hold
154
155
=cut
156
157
# These variables are set by command line options.
158
# They are initially set to default values.
159
my $dbh = C4::Context->dbh();
160
my $help    = 0;
161
my $man     = 0;
162
my $verbose = 0;
163
my $nomail  = 0;
164
my $days    ;
165
my $lettercode;
166
my @branchcodes; # Branch(es) passed as parameter
167
my $use_calendar = 0;
168
my $date_input;
169
my $opt_out = 0;
170
my @mtts;
171
172
GetOptions(
173
    'help|?'         => \$help,
174
    'man'            => \$man,
175
    'v'              => \$verbose,
176
    'n'              => \$nomail,
177
    'days=s'         => \$days,
178
    'lettercode=s'   => \$lettercode,
179
    'library=s'      => \@branchcodes,
180
    'date=s'         => \$date_input,
181
    'holidays'       => \$use_calendar,
182
    'mtt=s'          => \@mtts
183
);
184
pod2usage(1) if $help;
185
pod2usage( -verbose => 2 ) if $man;
186
187
if ( !$lettercode ) {
188
    pod2usage({
189
        -exitval => 1,
190
        -msg => qq{\nError: You must specify a lettercode to send reminders.\n},
191
    });
192
}
193
194
195
cronlogaction();
196
197
# Unless a delay is specified by the user we target all waiting holds
198
unless (defined $days) {
199
    $days=0;
200
}
201
202
# Unless one ore more branchcodes are passed we use all the branches
203
if (scalar @branchcodes > 0) {
204
    my $branchcodes_word = scalar @branchcodes > 1 ? 'branches' : 'branch';
205
    $verbose and warn "$branchcodes_word @branchcodes passed on parameter\n";
206
}
207
else {
208
    @branchcodes = Koha::Libraries->search()->get_column('branchcode');
209
}
210
211
# If provided we run the report as if it had run on a specified date
212
my $date_to_run;
213
if ( $date_input ){
214
    eval {
215
        $date_to_run = dt_from_string( $date_input, 'iso' );
216
    };
217
    die "$date_input is not a valid date, aborting! Use a date in format YYYY-MM-DD."
218
        if $@ or not $date_to_run;
219
}
220
else {
221
    $date_to_run = dt_from_string();
222
}
223
224
# Loop through each branch
225
foreach my $branchcode (@branchcodes) { #BEGIN BRANCH LOOP
226
    # Check that this branch has the letter code specified or skip this branch
227
    my $letter = C4::Letters::getletter( 'reserves', $lettercode , $branchcode );
228
    unless ($letter) {
229
        $verbose and print qq|Message '$lettercode' content not found for $branchcode\n|;
230
        next;
231
    }
232
233
    # If respecting calendar get the correct waiting since date
234
    my $waiting_date;
235
    if( $use_calendar ){
236
        my $calendar = Koha::Calendar->new( branchcode => $branchcode );
237
        $waiting_date = $calendar->addDays($date_to_run,-$days); #Add negative of days
238
    } else {
239
        $waiting_date = $date_to_run->subtract( days => $days );
240
    }
241
242
    # Find all the holds waiting since this date for the current branch
243
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
244
    my $waiting_since = $dtf->format_date( $waiting_date );
245
    my $reserves = Koha::Holds->search({
246
        waitingdate => {'<=' => $waiting_since },
247
        branchcode  => $branchcode,
248
    });
249
250
    $verbose and warn "No reserves found for $branchcode\n" unless $reserves->count;
251
    next unless $reserves->count;
252
    $verbose and warn $reserves->count . " reserves waiting since $waiting_since for $branchcode\n";
253
254
    # We only want to send one notice per patron per branch - this variable will hold the completed borrowers
255
    my %done;
256
257
    # If passed message transports we force use those, otherwise we will use the patrons preferences
258
    # for the 'Hold_Filled' notice
259
    my $sending_params = @mtts ? { message_transports => \@mtts } : { message_name => "Hold_Filled" };
260
261
262
    while ( my $reserve = $reserves->next ) {
263
264
        my $patron = $reserve->borrower;
265
        # Skip if we already dealt with this borrower
266
        next if ( $done{$patron->borrowernumber} );
267
        $verbose and print "  borrower " . $patron->surname . ", " . $patron->firstname . " has holds triggering notice.\n";
268
269
        # Setup the notice information
270
        my $letter_params = {
271
            module          => 'reserves',
272
            letter_code     => $lettercode,
273
            borrowernumber  => $patron->borrowernumber,
274
            branchcode      => $branchcode,
275
            tables          => {
276
                 borrowers  => $patron->borrowernumber,
277
                 branches   => $reserve->branchcode,
278
                 reserves   => $reserve->unblessed
279
            },
280
        };
281
        $sending_params->{letter_params} = $letter_params;
282
        $sending_params->{test_mode} = $nomail;
283
        my $result_text = $nomail ? "would have been sent" : "was sent";
284
        # send_notice queues the notices, falling back to print for email or SMS, and ignores phone (they are handled by Itiva)
285
        my $result = $patron->send_notice( $sending_params );
286
        $verbose and print "   borrower " . $patron->surname . ", " . $patron->firstname . " $result_text notices via: @{$result->{sent}}\n" if defined $result->{sent};
287
        $verbose and print "   borrower " . $patron->surname . ", " . $patron->firstname . " $result_text print fallback for: @{$result->{fallback}}\n" if defined $result->{fallback};
288
        # Mark this borrower as completed
289
        $done{$patron->borrowernumber} = 1;
290
    }
291
292
293
} #END BRANCH LOOP
(-)a/t/db_dependent/Koha/Patrons.t (-2 / +94 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 4;
22
use Test::More tests => 5;
23
use Test::Warn;
23
use Test::Warn;
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
Lines 43-48 use Koha::Patron::Relationship; Link Here
43
use Koha::Database;
43
use Koha::Database;
44
use Koha::DateUtils;
44
use Koha::DateUtils;
45
use Koha::Virtualshelves;
45
use Koha::Virtualshelves;
46
use Koha::Notice::Messages;
46
47
47
use t::lib::TestBuilder;
48
use t::lib::TestBuilder;
48
use t::lib::Mocks;
49
use t::lib::Mocks;
Lines 2066-2069 subtest 'anonymize' => sub { Link Here
2066
    $patron2->discard_changes; # refresh
2067
    $patron2->discard_changes; # refresh
2067
    is( $patron2->firstname, undef, 'First name patron2 cleared' );
2068
    is( $patron2->firstname, undef, 'First name patron2 cleared' );
2068
};
2069
};
2070
2071
subtest 'send_notice' => sub {
2072
    plan tests => 11;
2073
2074
    my $dbh = C4::Context->dbh;
2075
    t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'email' );
2076
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2077
    my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
2078
    my $letter_e = $builder->build_object( {
2079
        class => 'Koha::Notice::Templates',
2080
        value => {
2081
            branchcode => $branch->branchcode,
2082
            message_transport_type => 'email',
2083
            lang => 'default'
2084
        }
2085
    });
2086
    my $letter_p = $builder->build_object( {
2087
        class => 'Koha::Notice::Templates',
2088
        value => {
2089
            code => $letter_e->code,
2090
            module => $letter_e->module,
2091
            branchcode => $branch->branchcode,
2092
            message_transport_type => 'print',
2093
            lang => 'default'
2094
        }
2095
    });
2096
    my $letter_s = $builder->build_object( {
2097
        class => 'Koha::Notice::Templates',
2098
        value => {
2099
            code => $letter_e->code,
2100
            module => $letter_e->module,
2101
            branchcode => $branch->branchcode,
2102
            message_transport_type => 'sms',
2103
            lang => 'default'
2104
        }
2105
    });
2106
2107
    my $letter_params = {
2108
        letter_code => $letter_e->code,
2109
        branchcode  => $letter_e->branchcode,
2110
        module      => $letter_e->module,
2111
        borrowernumber => $patron->borrowernumber,
2112
        tables => {
2113
            borrowers => $patron->borrowernumber,
2114
        }
2115
    };
2116
    my @mtts = ('email');
2117
2118
    is( $patron->send_notice(), undef, "Nothing is done if no params passed");
2119
    is( $patron->send_notice({ letter_params => $letter_params }),undef, "Nothing done if only letter");
2120
    is_deeply(
2121
        $patron->send_notice({ letter_params => $letter_params, message_transports => \@mtts }),
2122
        {sent => ['email'] }, "Email sent"
2123
    );
2124
    $patron->email("")->store;
2125
    is_deeply(
2126
        $patron->send_notice({ letter_params => $letter_params, message_transports => \@mtts }),
2127
        {sent => ['print'],fallback => ['email']}, "Email fallsback to print if no email"
2128
    );
2129
    push @mtts, 'sms';
2130
    is_deeply(
2131
        $patron->send_notice({ letter_params => $letter_params, message_transports => \@mtts }),
2132
        {sent => ['print','sms'],fallback => ['email']}, "Email fallsback to print if no email, sms sent"
2133
    );
2134
    $patron->smsalertnumber("")->store;
2135
    my $counter = Koha::Notice::Messages->search({borrowernumber => $patron->borrowernumber })->count;
2136
    is_deeply(
2137
        $patron->send_notice({ letter_params => $letter_params, message_transports => \@mtts }),
2138
        {sent => ['print'],fallback => ['email','sms']}, "Email fallsback to print if no emai, sms fallsback to print if no sms, only one print sent"
2139
    );
2140
    is( Koha::Notice::Messages->search({borrowernumber => $patron->borrowernumber })->count, $counter+1,"Count of queued notices went up by one");
2141
2142
    # Enable notification for Hold_Filled - Things are hardcoded here but should work with default data
2143
    $dbh->do(q|INSERT INTO borrower_message_preferences( borrowernumber, message_attribute_id ) VALUES ( ?, ?)|, undef, $patron->borrowernumber, 4 );
2144
    my $borrower_message_preference_id = $dbh->last_insert_id(undef, undef, "borrower_message_preferences", undef);
2145
    $dbh->do(q|INSERT INTO borrower_message_transport_preferences( borrower_message_preference_id, message_transport_type) VALUES ( ?, ? )|, undef, $borrower_message_preference_id, 'email' );
2146
2147
    is( $patron->send_notice({ letter_params => $letter_params, message_transports => \@mtts, message_name => 'Hold_Filled' }),undef, "Nothing done if transports and name sent");
2148
2149
    $patron->email(q|awesome@ismymiddle.name|)->store;
2150
    is_deeply(
2151
        $patron->send_notice({ letter_params => $letter_params, message_name => 'Hold_Filled' }),
2152
        {sent => ['email'] }, "Email sent when using borrower preferences"
2153
    );
2154
    $counter = Koha::Notice::Messages->search({borrowernumber => $patron->borrowernumber })->count;
2155
    is_deeply(
2156
        $patron->send_notice({ letter_params => $letter_params, message_name => 'Hold_Filled', test_mode => 1 }),
2157
        {sent => ['email'] }, "Report that email sent when using borrower preferences in test_mode"
2158
    );
2159
    is( Koha::Notice::Messages->search({borrowernumber => $patron->borrowernumber })->count, $counter,"Count of queued notices not increased in test mode");
2160
};
2161
2069
$schema->storage->txn_rollback;
2162
$schema->storage->txn_rollback;
2070
- 

Return to bug 15986