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 JSON qw( encode_json decode_json );
26 use List::MoreUtils qw( uniq );
29 use Koha::BackgroundJobs;
30 use Koha::DateUtils qw( dt_from_string );
33 use base 'Koha::BackgroundJob';
41 Return the job type 'batch_item_record_deletion'.
46 return 'batch_item_record_deletion';
51 Koha::BackgroundJobs->find($id)->process(
53 record_ids => \@itemnumbers,
54 deleted_biblios => 0|1,
58 Will delete all the items that have been passed for deletion.
60 When deleted_biblios is passed, if we deleted the last item of a biblio,
61 the bibliographic record will be deleted as well.
63 The search engine's index will be updated according to the changes made
64 to the deleted bibliographic recods.
66 The generated report will be:
68 deleted_itemnumbers => \@list_of_itemnumbers,
69 not_deleted_itemnumbers => \@list_of_itemnumbers,
70 deleted_biblionumbers=> \@list_of_biblionumbers,
76 my ( $self, $args ) = @_;
78 my $job_type = $args->{job_type};
80 my $job = Koha::BackgroundJobs->find( $args->{job_id} );
82 if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
86 # FIXME If the job has already been started, but started again (worker has been restart for instance)
87 # Then we will start from scratch and so double delete the same records
90 $job->started_on(dt_from_string)->progress($job_progress)
91 ->status('started')->store;
93 my @record_ids = @{ $args->{record_ids} };
94 my $delete_biblios = $args->{delete_biblios};
97 total_records => scalar @record_ids,
101 my $schema = Koha::Database->new->schema;
102 my ( @deleted_itemnumbers, @not_deleted_itemnumbers,
103 @deleted_biblionumbers );
106 my $schema = Koha::Database->new->schema;
110 for my $record_id ( sort { $a <=> $b } @record_ids ) {
112 last if $job->get_from_storage->status eq 'cancelled';
114 my $item = Koha::Items->find($record_id) || next;
116 my $return = $item->safe_delete;
119 # FIXME Do we need to rollback the whole transaction if a deletion failed?
120 push @not_deleted_itemnumbers, $item->itemnumber;
124 code => 'item_not_deleted',
125 itemnumber => $item->itemnumber,
126 biblionumber => $item->biblionumber,
127 barcode => $item->barcode,
128 title => $item->biblio->title,
129 reason => @{$return->messages}[0]->message,
135 push @deleted_itemnumbers, $item->itemnumber;
136 push @biblionumbers, $item->biblionumber;
138 $report->{total_success}++;
139 $job->progress( ++$job_progress )->store;
142 # If there are no items left, delete the biblio
143 if ( $delete_biblios && @biblionumbers ) {
144 for my $biblionumber ( uniq @biblionumbers ) {
146 Koha::Biblios->find($biblionumber)->items->count;
147 if ( $items_count == 0 ) {
148 my $error = C4::Biblio::DelBiblio( $biblionumber,
149 { skip_record_index => 1 } );
151 push @deleted_biblionumbers, $biblionumber;
156 if (@deleted_biblionumbers) {
157 my $indexer = Koha::SearchEngine::Indexer->new(
158 { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
160 $indexer->index_records( \@deleted_biblionumbers,
161 'recordDelete', "biblioserver", undef );
178 die "Something terrible has happened!"
179 if ( $_ =~ /Rollback failed/ ); # Rollback failed
182 $report->{deleted_itemnumbers} = \@deleted_itemnumbers;
183 $report->{not_deleted_itemnumbers} = \@not_deleted_itemnumbers;
184 $report->{deleted_biblionumbers} = \@deleted_biblionumbers;
186 my $job_data = decode_json $job->data;
187 $job_data->{messages} = \@messages;
188 $job_data->{report} = $report;
190 $job->ended_on(dt_from_string)->data( encode_json $job_data);
191 $job->status('finished') if $job->status ne 'cancelled';
197 Koha::BackgroundJob::BatchDeleteItem->new->enqueue(
199 record_ids => \@itemnumbers,
200 deleted_biblios => 0|1,
209 my ( $self, $args ) = @_;
211 # TODO Raise exception instead
212 return unless exists $args->{record_ids};
214 my @record_ids = @{ $args->{record_ids} };
215 my $delete_biblios = $args->{delete_biblios} || 0;
217 $self->SUPER::enqueue(
219 job_size => scalar @record_ids,
221 record_ids => \@record_ids,
222 delete_biblios => $delete_biblios,