1 package Koha::BackgroundJob::BatchCancelHold;
3 # This file is part of Koha.
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.
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.
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>.
19 use JSON qw( encode_json decode_json );
21 use Koha::BackgroundJobs;
22 use Koha::DateUtils qw( dt_from_string );
27 use base 'Koha::BackgroundJob';
31 Koha::BackgroundJob::BatchCancelHold - Batch cancel holds
33 This is a subclass of Koha::BackgroundJob.
41 Define the job type of this job: batch_hold_cancel
46 return 'batch_hold_cancel';
51 Process the modification.
56 my ( $self, $args ) = @_;
58 my $job = Koha::BackgroundJobs->find( $args->{job_id} );
60 if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
65 $job->started_on(dt_from_string)->progress($job_progress)
66 ->status('started')->store;
68 my @hold_ids = @{ $args->{hold_ids} };
71 total_holds => scalar @hold_ids,
75 HOLD_IDS: for my $hold_id ( sort { $a <=> $b } @hold_ids ) {
79 my ( $hold, $patron, $biblio );
80 $hold = Koha::Holds->find($hold_id);
83 $patron = $hold->patron;
84 $biblio = $hold->biblio;
85 $hold->cancel( { cancellation_reason => $args->{reason} } );
88 if ( $error and $error != $hold or $@ ) {
92 code => 'hold_not_cancelled',
93 patron_id => defined $patron ? $patron->borrowernumber : '',
94 biblio_id => defined $biblio ? $biblio->biblionumber : '',
96 error => defined $hold
105 code => 'hold_cancelled',
106 patron_id => $patron->borrowernumber,
107 biblio_id => $biblio->biblionumber,
110 $report->{total_success}++;
112 $job->progress( ++$job_progress )->store;
115 my $job_data = decode_json $job->data;
116 $job_data->{messages} = \@messages;
117 $job_data->{report} = $report;
119 $job->ended_on(dt_from_string)->data( encode_json $job_data);
120 $job->status('finished') if $job->status ne 'cancelled';
132 my ( $self, $args ) = @_;
134 # TODO Raise exception instead
135 return unless exists $args->{hold_ids};
137 my @hold_ids = @{ $args->{hold_ids} };
139 $self->SUPER::enqueue(
141 job_size => scalar @hold_ids,
142 job_args => { hold_ids => \@hold_ids, reason => $args->{reason} }
147 =head3 additional_report
149 Pass the biblio's title and patron's name
153 sub additional_report {
154 my ( $self, $args ) = @_;
156 my $job = Koha::BackgroundJobs->find( $args->{job_id} );
157 my $messages = $job->messages;
158 for my $m ( @$messages ) {
159 $m->{patron} = Koha::Patrons->find($m->{patron_id});
160 $m->{biblio} = Koha::Biblios->find($m->{biblio_id});
162 return { report_messages => $messages };