Lines 76-81
chosen 'Digests only' on the advance messages.
Link Here
|
76 |
|
76 |
|
77 |
use Modern::Perl; |
77 |
use Modern::Perl; |
78 |
use Pod::Usage qw( pod2usage ); |
78 |
use Pod::Usage qw( pod2usage ); |
|
|
79 |
use Parallel::ForkManager; |
79 |
use Getopt::Long qw( GetOptions ); |
80 |
use Getopt::Long qw( GetOptions ); |
80 |
|
81 |
|
81 |
use Koha::Script -cron; |
82 |
use Koha::Script -cron; |
Lines 132-246
$verbose = 1 unless $verbose or $confirm;
Link Here
|
132 |
print "Test run only\n" unless $confirm; |
133 |
print "Test run only\n" unless $confirm; |
133 |
|
134 |
|
134 |
print "getting auto renewals\n" if $verbose; |
135 |
print "getting auto renewals\n" if $verbose; |
135 |
my $auto_renews = Koha::Checkouts->search({ auto_renew => 1, 'patron.autorenew_checkouts' => 1 },{ join => 'patron'}); |
136 |
my @auto_renews = Koha::Checkouts->search({ auto_renew => 1, 'patron.autorenew_checkouts' => 1 },{ join => 'patron', order_by=> 'me.borrowernumber' }); |
136 |
print "found " . $auto_renews->count . " auto renewals\n" if $verbose; |
137 |
print "found " . scalar @auto_renews . " auto renewals\n" if $verbose; |
137 |
|
138 |
|
138 |
my $renew_digest = {}; |
139 |
my $renew_digest = {}; |
139 |
my %report; |
140 |
my %report; |
140 |
while ( my $auto_renew = $auto_renews->next ) { |
|
|
141 |
print "examining item '" . $auto_renew->itemnumber . "' to auto renew\n" if $verbose; |
142 |
|
141 |
|
143 |
my $borrower_preferences; |
142 |
my $cron_options = C4::Context->config('auto_renew_cronjob'); |
144 |
$borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $auto_renew->borrowernumber, |
143 |
my $loops = $cron_options ? $cron_options->{parallel_loops_count} // 1 : 1; |
145 |
message_name => 'auto_renewals' } ) if $send_notices_pref eq 'preferences'; |
144 |
|
146 |
|
145 |
# Split the list of issues into chunks to run in parallel |
147 |
$send_notices = 1 if !$send_notices && $send_notices_pref eq 'preferences' && $borrower_preferences && $borrower_preferences->{transports} && $borrower_preferences->{transports}->{email}; |
146 |
my @chunks; |
148 |
|
147 |
if ( $loops > 1 ) { |
149 |
# CanBookBeRenewed returns 'auto_renew' when the renewal should be done by this script |
148 |
my $i = 0; |
150 |
my ( $ok, $error ) = CanBookBeRenewed( $auto_renew->borrowernumber, $auto_renew->itemnumber, undef, 1 ); |
149 |
my $borrowernumber = 0; |
151 |
if ( $error eq 'auto_renew' ) { |
150 |
while (@auto_renews){ |
152 |
if ($verbose) { |
151 |
my $auto_renew = pop(@auto_renews); |
153 |
say sprintf "Issue id: %s for borrower: %s and item: %s %s be renewed.", |
152 |
if( $borrowernumber != $auto_renew->borrowernumber ){ |
154 |
$auto_renew->issue_id, $auto_renew->borrowernumber, $auto_renew->itemnumber, $confirm ? 'will' : 'would'; |
153 |
$i++ if $borrowernumber; |
155 |
} |
154 |
$borrowernumber = $auto_renew->borrowernumber; |
156 |
if ($confirm){ |
|
|
157 |
my $date_due = AddRenewal( $auto_renew->borrowernumber, $auto_renew->itemnumber, $auto_renew->branchcode, undef, undef, undef, 0 ); |
158 |
$auto_renew->auto_renew_error(undef)->store; |
159 |
} |
160 |
push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew unless $send_notices_pref ne 'cron' && (!$borrower_preferences || !$borrower_preferences->{transports} || !$borrower_preferences->{transports}->{email} || $borrower_preferences->{'wants_digest'}); |
161 |
} elsif ( $error eq 'too_many' |
162 |
or $error eq 'on_reserve' |
163 |
or $error eq 'restriction' |
164 |
or $error eq 'overdue' |
165 |
or $error eq 'too_unseen' |
166 |
or $error eq 'auto_account_expired' |
167 |
or $error eq 'auto_too_late' |
168 |
or $error eq 'auto_too_much_oweing' |
169 |
or $error eq 'auto_too_soon' |
170 |
or $error eq 'item_denied_renewal' ) { |
171 |
if ( $verbose ) { |
172 |
say sprintf "Issue id: %s for borrower: %s and item: %s %s not be renewed. (%s)", |
173 |
$auto_renew->issue_id, $auto_renew->borrowernumber, $auto_renew->itemnumber, $confirm ? 'will' : 'would', $error; |
174 |
} |
175 |
if ( not $auto_renew->auto_renew_error or $error ne $auto_renew->auto_renew_error ) { |
176 |
$auto_renew->auto_renew_error($error)->store if $confirm; |
177 |
push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew |
178 |
if $error ne 'auto_too_soon' && ($send_notices_pref eq 'cron' || ($borrower_preferences && $borrower_preferences->{transports} && $borrower_preferences->{transports}->{email} && !$borrower_preferences->{'wants_digest'})); # Do not notify if it's too soon |
179 |
} |
155 |
} |
|
|
156 |
$i = 0 if $i >= $loops; |
157 |
push( @{ $chunks[$i] }, $auto_renew ); |
180 |
} |
158 |
} |
181 |
|
159 |
|
182 |
if ( $borrower_preferences && $borrower_preferences->{transports} && $borrower_preferences->{transports}->{email} && $borrower_preferences->{'wants_digest'} ) { |
160 |
my $pm = Parallel::ForkManager->new($loops); |
183 |
# cache this one to process after we've run through all of the items. |
161 |
|
184 |
if ($digest_per_branch) { |
162 |
DATA_LOOP: |
185 |
$renew_digest->{ $auto_renew->branchcode }->{ $auto_renew->borrowernumber }->{success}++ if $error eq 'auto_renew'; |
163 |
foreach my $chunk (@chunks) { |
186 |
$renew_digest->{ $auto_renew->branchcode }->{ $auto_renew->borrowernumber }->{error}++ unless $error eq 'auto_renew' || $error eq 'auto_too_soon' ; |
164 |
my $pid = $pm->start and next DATA_LOOP; |
187 |
push @{$renew_digest->{ $auto_renew->branchcode }->{ $auto_renew->borrowernumber }->{issues}}, $auto_renew->itemnumber; |
165 |
_ProcessRenewals( $chunk ); |
188 |
} else { |
166 |
$pm->finish; |
189 |
$renew_digest->{ $auto_renew->borrowernumber }->{success} ++ if $error eq 'auto_renew'; |
|
|
190 |
$renew_digest->{ $auto_renew->borrowernumber }->{error}++ unless $error eq 'auto_renew' || $error eq 'auto_too_soon' ; |
191 |
push @{$renew_digest->{ $auto_renew->borrowernumber }->{issues}}, $auto_renew->itemnumber; |
192 |
} |
193 |
} |
167 |
} |
194 |
|
168 |
|
|
|
169 |
$pm->wait_all_children; |
170 |
} else { |
171 |
_ProcessRenewals( \@auto_renews ); |
195 |
} |
172 |
} |
196 |
|
173 |
|
197 |
if ( $send_notices && $confirm ) { |
174 |
=head1 METHODS |
198 |
for my $borrowernumber ( keys %report ) { |
|
|
199 |
my $patron = Koha::Patrons->find($borrowernumber); |
200 |
for my $issue ( @{ $report{$borrowernumber} } ) { |
201 |
my $item = Koha::Items->find( $issue->itemnumber ); |
202 |
my $letter = C4::Letters::GetPreparedLetter( |
203 |
module => 'circulation', |
204 |
letter_code => 'AUTO_RENEWALS', |
205 |
tables => { |
206 |
borrowers => $patron->borrowernumber, |
207 |
issues => $issue->itemnumber, |
208 |
items => $issue->itemnumber, |
209 |
biblio => $item->biblionumber, |
210 |
}, |
211 |
lang => $patron->lang, |
212 |
); |
213 |
|
175 |
|
214 |
my $library = Koha::Libraries->find( $patron->branchcode ); |
176 |
=head2 _ProcessRenewals |
215 |
my $admin_email_address = $library->from_email_address; |
|
|
216 |
|
177 |
|
217 |
C4::Letters::EnqueueLetter( |
178 |
Internal method to process the queue in chunks |
218 |
{ letter => $letter, |
179 |
|
219 |
borrowernumber => $borrowernumber, |
180 |
=cut |
220 |
message_transport_type => 'email', |
181 |
|
221 |
from_address => $admin_email_address, |
182 |
sub _ProcessRenewals { |
222 |
} |
183 |
my $auto_renew_issues = shift; |
223 |
); |
184 |
|
|
|
185 |
foreach my $auto_renew (@$auto_renew_issues) { |
186 |
print "examining item '" . $auto_renew->itemnumber . "' to auto renew\n" if $verbose; |
187 |
|
188 |
my $borrower_preferences; |
189 |
$borrower_preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $auto_renew->borrowernumber, |
190 |
message_name => 'auto_renewals' } ) if $send_notices_pref eq 'preferences'; |
191 |
|
192 |
$send_notices = 1 if !$send_notices && $send_notices_pref eq 'preferences' && $borrower_preferences && $borrower_preferences->{transports} && $borrower_preferences->{transports}->{email}; |
193 |
|
194 |
# CanBookBeRenewed returns 'auto_renew' when the renewal should be done by this script |
195 |
my ( $ok, $error ) = CanBookBeRenewed( $auto_renew->borrowernumber, $auto_renew->itemnumber, undef, 1 ); |
196 |
if ( $error eq 'auto_renew' ) { |
197 |
if ($verbose) { |
198 |
say sprintf "Issue id: %s for borrower: %s and item: %s %s be renewed.", |
199 |
$auto_renew->issue_id, $auto_renew->borrowernumber, $auto_renew->itemnumber, $confirm ? 'will' : 'would'; |
200 |
} |
201 |
if ($confirm){ |
202 |
my $date_due = AddRenewal( $auto_renew->borrowernumber, $auto_renew->itemnumber, $auto_renew->branchcode, undef, undef, undef, 0 ); |
203 |
$auto_renew->auto_renew_error(undef)->store; |
204 |
} |
205 |
push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew unless $send_notices_pref ne 'cron' && (!$borrower_preferences || !$borrower_preferences->{transports} || !$borrower_preferences->{transports}->{email} || $borrower_preferences->{'wants_digest'}); |
206 |
} elsif ( $error eq 'too_many' |
207 |
or $error eq 'on_reserve' |
208 |
or $error eq 'restriction' |
209 |
or $error eq 'overdue' |
210 |
or $error eq 'too_unseen' |
211 |
or $error eq 'auto_account_expired' |
212 |
or $error eq 'auto_too_late' |
213 |
or $error eq 'auto_too_much_oweing' |
214 |
or $error eq 'auto_too_soon' |
215 |
or $error eq 'item_denied_renewal' ) { |
216 |
if ( $verbose ) { |
217 |
say sprintf "Issue id: %s for borrower: %s and item: %s %s not be renewed. (%s)", |
218 |
$auto_renew->issue_id, $auto_renew->borrowernumber, $auto_renew->itemnumber, $confirm ? 'will' : 'would', $error; |
219 |
} |
220 |
if ( not $auto_renew->auto_renew_error or $error ne $auto_renew->auto_renew_error ) { |
221 |
$auto_renew->auto_renew_error($error)->store if $confirm; |
222 |
push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew |
223 |
if $error ne 'auto_too_soon' && ($send_notices_pref eq 'cron' || ($borrower_preferences && $borrower_preferences->{transports} && $borrower_preferences->{transports}->{email} && !$borrower_preferences->{'wants_digest'})); # Do not notify if it's too soon |
224 |
} |
225 |
} |
226 |
|
227 |
if ( $borrower_preferences && $borrower_preferences->{transports} && $borrower_preferences->{transports}->{email} && $borrower_preferences->{'wants_digest'} ) { |
228 |
# cache this one to process after we've run through all of the items. |
229 |
if ($digest_per_branch) { |
230 |
$renew_digest->{ $auto_renew->branchcode }->{ $auto_renew->borrowernumber }->{success}++ if $error eq 'auto_renew'; |
231 |
$renew_digest->{ $auto_renew->branchcode }->{ $auto_renew->borrowernumber }->{error}++ unless $error eq 'auto_renew' || $error eq 'auto_too_soon' ; |
232 |
push @{$renew_digest->{ $auto_renew->branchcode }->{ $auto_renew->borrowernumber }->{issues}}, $auto_renew->itemnumber; |
233 |
} else { |
234 |
$renew_digest->{ $auto_renew->borrowernumber }->{success} ++ if $error eq 'auto_renew'; |
235 |
$renew_digest->{ $auto_renew->borrowernumber }->{error}++ unless $error eq 'auto_renew' || $error eq 'auto_too_soon' ; |
236 |
push @{$renew_digest->{ $auto_renew->borrowernumber }->{issues}}, $auto_renew->itemnumber; |
237 |
} |
224 |
} |
238 |
} |
|
|
239 |
|
225 |
} |
240 |
} |
226 |
|
241 |
|
227 |
if ($digest_per_branch) { |
242 |
if ( $send_notices && $confirm ) { |
228 |
while (my ($branchcode, $digests) = each %$renew_digest) { |
243 |
for my $borrowernumber ( keys %report ) { |
|
|
244 |
my $patron = Koha::Patrons->find($borrowernumber); |
245 |
for my $issue ( @{ $report{$borrowernumber} } ) { |
246 |
my $item = Koha::Items->find( $issue->itemnumber ); |
247 |
my $letter = C4::Letters::GetPreparedLetter( |
248 |
module => 'circulation', |
249 |
letter_code => 'AUTO_RENEWALS', |
250 |
tables => { |
251 |
borrowers => $patron->borrowernumber, |
252 |
issues => $issue->itemnumber, |
253 |
items => $issue->itemnumber, |
254 |
biblio => $item->biblionumber, |
255 |
}, |
256 |
lang => $patron->lang, |
257 |
); |
258 |
|
259 |
my $library = Koha::Libraries->find( $patron->branchcode ); |
260 |
my $admin_email_address = $library->from_email_address; |
261 |
|
262 |
C4::Letters::EnqueueLetter( |
263 |
{ letter => $letter, |
264 |
borrowernumber => $borrowernumber, |
265 |
message_transport_type => 'email', |
266 |
from_address => $admin_email_address, |
267 |
} |
268 |
); |
269 |
} |
270 |
} |
271 |
|
272 |
if ($digest_per_branch) { |
273 |
while (my ($branchcode, $digests) = each %$renew_digest) { |
274 |
send_digests({ |
275 |
digests => $digests, |
276 |
branchcode => $branchcode, |
277 |
letter_code => 'AUTO_RENEWALS_DGST', |
278 |
}); |
279 |
} |
280 |
} else { |
229 |
send_digests({ |
281 |
send_digests({ |
230 |
digests => $digests, |
282 |
digests => $renew_digest, |
231 |
branchcode => $branchcode, |
|
|
232 |
letter_code => 'AUTO_RENEWALS_DGST', |
283 |
letter_code => 'AUTO_RENEWALS_DGST', |
233 |
}); |
284 |
}); |
234 |
} |
285 |
} |
235 |
} else { |
|
|
236 |
send_digests({ |
237 |
digests => $renew_digest, |
238 |
letter_code => 'AUTO_RENEWALS_DGST', |
239 |
}); |
240 |
} |
286 |
} |
241 |
} |
|
|
242 |
|
287 |
|
243 |
=head1 METHODS |
288 |
} |
244 |
|
289 |
|
245 |
=head2 send_digests |
290 |
=head2 send_digests |
246 |
|
291 |
|
247 |
- |
|
|