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>.
20 use Koha::DateUtils qw( dt_from_string );
25 use base 'Koha::BackgroundJob';
29 Koha::BackgroundJob::BatchCancelHold - Batch cancel holds
31 This is a subclass of Koha::BackgroundJob.
39 Define the job type of this job: batch_hold_cancel
44 return 'batch_hold_cancel';
49 Process the modification.
54 my ( $self, $args ) = @_;
56 if ( $self->status eq 'cancelled' ) {
61 $self->started_on(dt_from_string)->progress($job_progress)
62 ->status('started')->store;
64 my @hold_ids = @{ $args->{hold_ids} };
67 total_holds => scalar @hold_ids,
71 HOLD_IDS: for my $hold_id ( sort { $a <=> $b } @hold_ids ) {
75 my ( $hold, $patron, $biblio );
76 $hold = Koha::Holds->find($hold_id);
79 $patron = $hold->patron;
80 $biblio = $hold->biblio;
81 $hold->cancel( { cancellation_reason => $args->{reason} } );
84 if ( $error and $error != $hold or $@ ) {
88 code => 'hold_not_cancelled',
89 patron_id => defined $patron ? $patron->borrowernumber : '',
90 biblio_id => defined $biblio ? $biblio->biblionumber : '',
92 error => defined $hold
101 code => 'hold_cancelled',
102 patron_id => $patron->borrowernumber,
103 biblio_id => $biblio->biblionumber,
106 $report->{total_success}++;
108 $self->progress( ++$job_progress )->store;
111 my $json = $self->json;
112 my $job_data = $json->decode($self->data);
113 $job_data->{messages} = \@messages;
114 $job_data->{report} = $report;
116 $self->ended_on(dt_from_string)->data($json->encode($job_data));
117 $self->status('finished') if $self->status ne 'cancelled';
129 my ( $self, $args ) = @_;
131 # TODO Raise exception instead
132 return unless exists $args->{hold_ids};
134 my @hold_ids = @{ $args->{hold_ids} };
136 $self->SUPER::enqueue(
138 job_size => scalar @hold_ids,
139 job_args => { hold_ids => \@hold_ids, reason => $args->{reason} },
140 queue => 'long_tasks',
145 =head3 additional_report
147 Pass the biblio's title and patron's name
151 sub additional_report {
152 my ( $self, $args ) = @_;
154 my $messages = $self->messages;
155 for my $m ( @$messages ) {
156 $m->{patron} = Koha::Patrons->find($m->{patron_id});
157 $m->{biblio} = Koha::Biblios->find($m->{biblio_id});
159 return { report_messages => $messages };