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

(-)a/misc/cronjobs/automatic_renewals.pl (-31 / +53 lines)
Lines 133-139 $verbose = 1 unless $verbose or $confirm; Link Here
133
print "Test run only\n" unless $confirm;
133
print "Test run only\n" unless $confirm;
134
134
135
print "getting auto renewals\n" if $verbose;
135
print "getting auto renewals\n" if $verbose;
136
my $auto_renews = Koha::Checkouts->search({ auto_renew => 1, 'patron.autorenew_checkouts' => 1 },{ join => 'patron'});
136
my $auto_renews = Koha::Checkouts->search(
137
    {
138
        auto_renew                   => 1,
139
        'patron.autorenew_checkouts' => 1,
140
    },
141
    {
142
        join => 'patron',
143
        join => 'item',
144
    }
145
);
137
print "found " . $auto_renews->count . " auto renewals\n" if $verbose;
146
print "found " . $auto_renews->count . " auto renewals\n" if $verbose;
138
147
139
my $renew_digest = {};
148
my $renew_digest = {};
Lines 141-147 my %report; Link Here
141
while ( my $auto_renew = $auto_renews->next ) {
150
while ( my $auto_renew = $auto_renews->next ) {
142
    print "examining item '" . $auto_renew->itemnumber . "' to auto renew\n" if $verbose;
151
    print "examining item '" . $auto_renew->itemnumber . "' to auto renew\n" if $verbose;
143
152
144
    my ( $borrower_preferences, $wants_email, $wants_digest ) = ( undef, 0, 0 );
153
    my ( $borrower_preferences, $wants_messages, $wants_digest ) = ( undef, 0, 0 );
145
    if ( $send_notices_pref eq 'preferences' ){
154
    if ( $send_notices_pref eq 'preferences' ){
146
        $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences(
155
        $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences(
147
            {
156
            {
Lines 149-163 while ( my $auto_renew = $auto_renews->next ) { Link Here
149
                message_name   => 'auto_renewals'
158
                message_name   => 'auto_renewals'
150
            }
159
            }
151
        );
160
        );
152
        $wants_email = 1
161
        $wants_messages = 1
153
            if $borrower_preferences
162
            if $borrower_preferences
154
            && $borrower_preferences->{transports}
163
            && $borrower_preferences->{transports};
155
            && $borrower_preferences->{transports}->{email};
156
        $wants_digest = 1
164
        $wants_digest = 1
157
            if $wants_email
165
            if $wants_messages
158
            && $borrower_preferences->{wants_digest};
166
            && $borrower_preferences->{wants_digest};
159
    } else { # Preference is never or cron
167
    } else { # Preference is never or cron
160
        $wants_email = $send_notices;
168
        $wants_messages = $send_notices;
161
        $wants_digest = 0;
169
        $wants_digest = 0;
162
    }
170
    }
163
171
Lines 175-181 while ( my $auto_renew = $auto_renews->next ) { Link Here
175
            $auto_renew->auto_renew_error(undef)->store;
183
            $auto_renew->auto_renew_error(undef)->store;
176
        }
184
        }
177
        push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew
185
        push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew
178
            if ( $wants_email ) && !$wants_digest;
186
            if ( $wants_messages ) && !$wants_digest;
179
    } elsif ( $error eq 'too_many'
187
    } elsif ( $error eq 'too_many'
180
        or $error eq 'on_reserve'
188
        or $error eq 'on_reserve'
181
        or $error eq 'restriction'
189
        or $error eq 'restriction'
Lines 194-200 while ( my $auto_renew = $auto_renews->next ) { Link Here
194
        if ( $updated ) {
202
        if ( $updated ) {
195
            $auto_renew->auto_renew_error($error)->store if $confirm;
203
            $auto_renew->auto_renew_error($error)->store if $confirm;
196
            push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew
204
            push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew
197
              if $error ne 'auto_too_soon' && ( $wants_email  && !$wants_digest );    # Do not notify if it's too soon
205
              if $error ne 'auto_too_soon' && ( $wants_messages  && !$wants_digest );    # Do not notify if it's too soon
198
        }
206
        }
199
    }
207
    }
200
208
Lines 218-247 while ( my $auto_renew = $auto_renews->next ) { Link Here
218
if ( $send_notices && $confirm ) {
226
if ( $send_notices && $confirm ) {
219
    for my $borrowernumber ( keys %report ) {
227
    for my $borrowernumber ( keys %report ) {
220
        my $patron = Koha::Patrons->find($borrowernumber);
228
        my $patron = Koha::Patrons->find($borrowernumber);
221
        for my $issue ( @{ $report{$borrowernumber} } ) {
229
        my $borrower_preferences =
222
            my $item   = Koha::Items->find( $issue->itemnumber );
230
            C4::Members::Messaging::GetMessagingPreferences(
223
            my $letter = C4::Letters::GetPreparedLetter(
231
                {
224
                module      => 'circulation',
232
                    borrowernumber => $borrowernumber,
225
                letter_code => 'AUTO_RENEWALS',
233
                    message_name   => 'auto_renewals'
226
                tables      => {
234
                }
227
                    borrowers => $patron->borrowernumber,
228
                    issues    => $issue->itemnumber,
229
                    items     => $issue->itemnumber,
230
                    biblio    => $item->biblionumber,
231
                },
232
                lang => $patron->lang,
233
            );
235
            );
236
        for my $issue ( @{ $report{$borrowernumber} } ) {
237
            my $item = $issue->item;
238
            # Force sending of email and only email if pref is set to "cron"
239
            my @transports = $send_notices_pref eq 'preferences' ? keys %{ $borrower_preferences->{'transports'} } : 'sms';
240
            foreach my $transport ( @transports ) {
241
                my $letter = C4::Letters::GetPreparedLetter (
242
                    module      => 'circulation',
243
                    letter_code => 'AUTO_RENEWALS',
244
                    tables      => {
245
                        borrowers => $patron->borrowernumber,
246
                        issues    => $issue->itemnumber,
247
                        items     => $issue->itemnumber,
248
                        biblio    => $item->biblionumber,
249
                    },
250
                    lang => $patron->lang,
251
                    message_transport_type => $transport,
252
                );
234
253
235
            my $library = Koha::Libraries->find( $patron->branchcode );
254
                if ($letter) {
236
            my $admin_email_address = $library->from_email_address;
255
                    my $library = $patron->library;
237
256
                    my $admin_email_address = $library->from_email_address;
238
            C4::Letters::EnqueueLetter(
257
239
                {   letter                 => $letter,
258
                    C4::Letters::EnqueueLetter(
240
                    borrowernumber         => $borrowernumber,
259
                        {
241
                    message_transport_type => 'email',
260
                            letter                 => $letter,
242
                    from_address           => $admin_email_address,
261
                            borrowernumber         => $borrowernumber,
262
                            from_address           => $admin_email_address,
263
                            message_transport_type => $transport
264
                        }
265
                    );
243
                }
266
                }
244
            );
267
            }
245
        }
268
        }
246
    }
269
    }
247
270
248
- 

Return to bug 30355