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

(-)a/misc/cronjobs/membership_expiry.pl (-105 / +61 lines)
Lines 19-25 Link Here
19
19
20
=head1 NAME
20
=head1 NAME
21
21
22
membership_expiry.pl - cron script to put membership expiry reminder into message queues
22
membership_expiry.pl - cron script to put membership expiry reminders into the message queue
23
23
24
=head1 SYNOPSIS
24
=head1 SYNOPSIS
25
25
Lines 35-68 This script sends membership expiry reminder notices to patrons. Link Here
35
It queues them in the message queue, which is processed by
35
It queues them in the message queue, which is processed by
36
the process_message_queue.pl cronjob.
36
the process_message_queue.pl cronjob.
37
37
38
=cut
39
40
41
use Modern::Perl;
42
use Getopt::Long;
43
use Pod::Usage;
44
use Data::Dumper;
45
BEGIN {
46
    # find Koha's Perl modules
47
    # test carefully before changing this
48
    use FindBin;
49
    eval { require "$FindBin::Bin/../kohalib.pl" };
50
}
51
52
use C4::Context;
53
use C4::Letters;
54
use C4::Dates qw/format_date/;
55
use C4::Log;
56
57
58
=head1 NAME
59
60
membership_expiry.pl - prepare messages to be sent to membership expiry reminder notices to patrons.
61
62
=head1 SYNOPSIS
63
64
membership_expiry.pl [-c]
65
66
=head1 OPTIONS
38
=head1 OPTIONS
67
39
68
=over 8
40
=over 8
Lines 91-109 statement otherwise. Link Here
91
63
92
=back
64
=back
93
65
94
=head1 DESCRIPTION
66
=head1 CONFIGURATION
95
96
This script is designed to alert send membership expire notices
97
98
=head2 Configuration
99
100
This script pays attention to send the membership expire notices
101
The content of the messages is configured in Tools -> Notices and slips. Use the MEMBERSHIP_EXPIRY template
102
67
103
=head2 Outgoing emails
68
The content of the messages is configured in Tools -> Notices and slips. Use the MEMBERSHIP_EXPIRY notice.
104
105
Typically, messages are prepared for each patron when the memberships are going to expire
106
69
70
Typically, messages are prepared for each patron when the memberships are going to expire.
107
71
108
These emails are staged in the outgoing message queue, as are messages
72
These emails are staged in the outgoing message queue, as are messages
109
produced by other features of Koha. This message queue must be
73
produced by other features of Koha. This message queue must be
Lines 114-124 In the event that the C<-n> flag is passed to this program, no emails Link Here
114
are sent. Instead, messages are sent on standard output from this
78
are sent. Instead, messages are sent on standard output from this
115
program.
79
program.
116
80
117
=head2 Templates
81
Notices can contain variables enclosed in double angle brackets like
118
119
Templates can contain variables enclosed in double angle brackets like
120
E<lt>E<lt>thisE<gt>E<gt>. Those variables will be replaced with values
82
E<lt>E<lt>thisE<gt>E<gt>. Those variables will be replaced with values
121
specific to the members there membership expiry date is coming.
83
specific to the soon expiring members.
122
Available variables are:
84
Available variables are:
123
85
124
=over
86
=over
Lines 133-216 any field from the branches table Link Here
133
95
134
=back
96
=back
135
97
136
=head1 SEE ALSO
137
138
The F<misc/cronjobs/membership_expiry.pl> program allows you to send
139
messages to patrons when the membership are going to expires.
140
=cut
98
=cut
141
99
142
# These are defaults for command line options.
100
use Modern::Perl;
101
use Getopt::Long;
102
use Pod::Usage;
103
use Data::Dumper;
104
BEGIN {
105
    # find Koha's Perl modules
106
    # test carefully before changing this
107
    use FindBin;
108
    eval { require "$FindBin::Bin/../kohalib.pl" };
109
}
143
110
111
use C4::Context;
112
use C4::Letters;
113
use C4::Log;
114
115
# These are defaults for command line options.
144
my $confirm;                              # -c: Confirm that the user has read and configured this script.
116
my $confirm;                              # -c: Confirm that the user has read and configured this script.
145
my $nomail;                               # -n: No mail. Will not send any emails.
117
my $nomail;                               # -n: No mail. Will not send any emails.
146
my $verbose= 0;                           # -v: verbose
118
my $verbose= 0;                           # -v: verbose
147
148
my $help    = 0;
119
my $help    = 0;
149
my $man     = 0;
120
my $man     = 0;
150
121
151
GetOptions(
122
GetOptions(
152
            'help|?'         => \$help,
123
    'help|?'         => \$help,
153
            'man'            => \$man,
124
    'man'            => \$man,
154
            'c'              => \$confirm,
125
    'c'              => \$confirm,
155
            'n'              => \$nomail,
126
    'n'              => \$nomail,
156
            'v'              => \$verbose,
127
    'v'              => \$verbose,
157
       ) or pod2usage(2);
128
) or pod2usage(2);
158
pod2usage(1) if $help;
129
159
pod2usage( -verbose => 2 ) if $man;;
130
pod2usage( -verbose => 2 ) if $man;
160
131
pod2usage(1) if $help || !$confirm;
161
my $usage = << 'ENDUSAGE';
162
This script prepares for membership expiry reminders to be sent to
163
patrons. It queues them in the message queue, which is processed by
164
the process_message_queue.pl cronjob.
165
See the comments in the script for directions on changing the script.
166
This script has the following parameters :
167
    -c Confirm and remove this help & warning
168
    -n send No mail. Instead, all mail messages are printed on screen. Useful for testing purposes.
169
    -v verbose
170
ENDUSAGE
171
172
unless ($confirm) {
173
    print $usage;
174
    print "Do you wish to continue? (y/n)";
175
    chomp($_ = <STDIN>);
176
    exit unless (/^y/i);
177
}
178
132
179
cronlogaction();
133
cronlogaction();
180
134
181
my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
135
my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
182
warn 'getting upcoming membership expires' if $verbose;
136
warn 'getting upcoming membership expires' if $verbose;
183
my $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires();
137
my $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires();
184
warn 'found ' . scalar( @$upcoming_mem_expires ) . ' issues' if $verbose;
138
warn 'found ' . scalar( @$upcoming_mem_expires ) . ' soon expiring members'
139
    if $verbose;
185
140
186
141
# main loop
187
UPCOMINGMEMEXP: foreach my $recent ( @$upcoming_mem_expires ) {
142
foreach my $recent ( @$upcoming_mem_expires ) {
188
    my $from_address = $recent->{'branchemail'} || $admin_adress;
143
    my $from_address = $recent->{'branchemail'} || $admin_adress;
189
    my $letter_type = 'MEMBERSHIP_EXPIRY';
144
    my $letter_type = 'MEMBERSHIP_EXPIRY';
190
    my $letter = C4::Letters::getletter( 'members', $letter_type, $recent->{'branchcode'} );
145
    my $letter = C4::Letters::getletter( 'members', $letter_type,
191
    die "no letter of type '$letter_type' found. Please see sample_notices.sql" unless $letter;
146
        $recent->{'branchcode'} );
192
147
    die "no letter of type '$letter_type' found. Please see sample_notices.sql"
193
    $letter = parse_letter({  letter    => $letter,
148
        unless $letter;
194
                              borrowernumber => $recent->{'borrowernumber'},
149
195
                              firstname => $recent->{'firstname'},
150
    $letter = parse_letter({
196
                              categorycode  => $recent->{'categorycode'},
151
        letter         => $letter,
197
                              branchcode => $recent->{'branchcode'},
152
        borrowernumber => $recent->{'borrowernumber'},
198
                          });
153
        firstname      => $recent->{'firstname'},
154
        categorycode   => $recent->{'categorycode'},
155
        branchcode     => $recent->{'branchcode'},
156
    });
199
    if ($letter) {
157
    if ($letter) {
200
        if ($nomail) {
158
        if ($nomail) {
201
            print $letter->{'content'};
159
            print $letter->{'content'};
202
        } else {
160
        } else {
203
             C4::Letters::EnqueueLetter( {  letter               => $letter,
161
            C4::Letters::EnqueueLetter({
204
                                            borrowernumber       =>  $recent->{'borrowernumber'},
162
                letter                 => $letter,
205
                                            from_address           => $from_address,
163
                borrowernumber         =>  $recent->{'borrowernumber'},
206
                                            message_transport_type => 'email',
164
                from_address           => $from_address,
207
                                        } );
165
                message_transport_type => 'email',
208
         }
166
            });
209
       }
167
        }
210
    }
168
    }
169
}
211
170
212
171
=head1 SUBROUTINES
213
=head1 METHODS
214
172
215
=head2 parse_letter
173
=head2 parse_letter
216
174
Lines 222-233 sub parse_letter { Link Here
222
        return unless exists $params->{$required};
180
        return unless exists $params->{$required};
223
    }
181
    }
224
    my $letter =  C4::Letters::GetPreparedLetter (
182
    my $letter =  C4::Letters::GetPreparedLetter (
225
            module => 'members',
183
        module => 'members',
226
            letter_code => 'MEMBERSHIP_EXPIRY',
184
        letter_code => 'MEMBERSHIP_EXPIRY',
227
            tables => {'borrowers', $params->{'borrowernumber'}, 'branches', $params->{'branchcode'}},
185
        tables => {
186
            'borrowers', $params->{'borrowernumber'},
187
            'branches', $params->{'branchcode'}
188
        },
228
    );
189
    );
229
}
190
}
230
231
1;
232
233
__END__
234
- 

Return to bug 6810