1 package Koha::BackgroundJob::BatchDeleteItem;
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 Koha::BackgroundJob::BatchDeleteItem - Background job derived class to process item deletion in batch
25 use List::MoreUtils qw( uniq );
30 use base 'Koha::BackgroundJob';
38 Return the job type 'batch_item_record_deletion'.
43 return 'batch_item_record_deletion';
48 Koha::BackgroundJobs->find($id)->process(
50 record_ids => \@itemnumbers,
51 deleted_biblios => 0|1,
55 Will delete all the items that have been passed for deletion.
57 When deleted_biblios is passed, if we deleted the last item of a biblio,
58 the bibliographic record will be deleted as well.
60 The search engine's index will be updated according to the changes made
61 to the deleted bibliographic recods.
63 The generated report will be:
65 deleted_itemnumbers => \@list_of_itemnumbers,
66 not_deleted_itemnumbers => \@list_of_itemnumbers,
67 deleted_biblionumbers=> \@list_of_biblionumbers,
73 my ( $self, $args ) = @_;
75 if ( $self->status eq 'cancelled' ) {
79 # FIXME If the job has already been started, but started again (worker has been restart for instance)
80 # Then we will start from scratch and so double delete the same records
84 my @record_ids = @{ $args->{record_ids} };
85 my $delete_biblios = $args->{delete_biblios};
88 total_records => scalar @record_ids,
92 my $schema = Koha::Database->new->schema;
93 my ( @deleted_itemnumbers, @not_deleted_itemnumbers,
94 @deleted_biblionumbers );
97 my $schema = Koha::Database->new->schema;
101 for my $record_id ( sort { $a <=> $b } @record_ids ) {
103 last if $self->get_from_storage->status eq 'cancelled';
105 my $item = Koha::Items->find($record_id) || next;
107 my $return = $item->safe_delete({ skip_record_index => 1, skip_holds_queue => 1 });
110 # FIXME Do we need to rollback the whole transaction if a deletion failed?
111 push @not_deleted_itemnumbers, $item->itemnumber;
115 code => 'item_not_deleted',
116 itemnumber => $item->itemnumber,
117 biblionumber => $item->biblionumber,
118 barcode => $item->barcode,
119 title => $item->biblio->title,
120 reason => @{$return->messages}[0]->message,
126 push @deleted_itemnumbers, $item->itemnumber;
127 push @biblionumbers, $item->biblionumber;
129 $report->{total_success}++;
133 # If there are no items left, delete the biblio
134 my @updated_biblionumbers;
135 for my $biblionumber ( uniq @biblionumbers ) {
137 Koha::Biblios->find($biblionumber)->items->count;
138 if ( $delete_biblios && $items_count == 0 ) {
139 my $error = C4::Biblio::DelBiblio( $biblionumber,
140 { skip_record_index => 1, skip_holds_queue => 1 } );
142 push @deleted_biblionumbers, $biblionumber;
145 push @updated_biblionumbers, $biblionumber;
149 if (@deleted_biblionumbers) {
150 my $indexer = Koha::SearchEngine::Indexer->new(
151 { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
153 $indexer->index_records( \@deleted_biblionumbers,
154 'recordDelete', "biblioserver", undef );
156 Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
158 biblio_ids => \@deleted_biblionumbers
160 ) if C4::Context->preference('RealTimeHoldsQueue');
163 if (@updated_biblionumbers) {
164 my $indexer = Koha::SearchEngine::Indexer->new(
165 { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
167 $indexer->index_records( \@updated_biblionumbers,
168 'specialUpdate', "biblioserver", undef );
170 Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
172 biblio_ids => \@updated_biblionumbers
174 ) if C4::Context->preference('RealTimeHoldsQueue');
190 die "Something terrible has happened!"
191 if ( $_ =~ /Rollback failed/ ); # Rollback failed
194 $report->{deleted_itemnumbers} = \@deleted_itemnumbers;
195 $report->{not_deleted_itemnumbers} = \@not_deleted_itemnumbers;
196 $report->{deleted_biblionumbers} = \@deleted_biblionumbers;
198 my $data = $self->decoded_data;
199 $data->{messages} = \@messages;
200 $data->{report} = $report;
202 $self->finish( $data );
207 Koha::BackgroundJob::BatchDeleteItem->new->enqueue(
209 record_ids => \@itemnumbers,
210 deleted_biblios => 0|1,
219 my ( $self, $args ) = @_;
221 # TODO Raise exception instead
222 return unless exists $args->{record_ids};
224 my @record_ids = @{ $args->{record_ids} };
225 my $delete_biblios = $args->{delete_biblios} || 0;
227 $self->SUPER::enqueue(
229 job_size => scalar @record_ids,
231 record_ids => \@record_ids,
232 delete_biblios => $delete_biblios,
234 job_queue => 'long_tasks',