1 package Koha::BackgroundJob::BatchDeleteBiblio;
4 use JSON qw( encode_json decode_json );
6 use Koha::BackgroundJobs;
7 use Koha::DateUtils qw( dt_from_string );
10 use base 'Koha::BackgroundJob';
14 Koha::BackgroundJob::BatchDeleteBiblio - Batch delete bibliographic records
16 This is a subclass of Koha::BackgroundJob.
24 Define the job type of this job: batch_biblio_record_deletion
29 return 'batch_biblio_record_deletion';
39 my ( $self, $args ) = @_;
41 my $job_type = $args->{job_type};
43 my $job = Koha::BackgroundJobs->find( $args->{job_id} );
45 if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
49 # FIXME If the job has already been started, but started again (worker has been restart for instance)
50 # Then we will start from scratch and so double delete the same records
53 $job->started_on(dt_from_string)
54 ->progress($job_progress)
58 my $mmtid = $args->{mmtid};
59 my @record_ids = @{ $args->{record_ids} };
62 total_records => scalar @record_ids,
66 my $schema = Koha::Database->new->schema;
67 RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
69 last if $job->get_from_storage->status eq 'cancelled';
71 next unless $record_id;
73 $schema->storage->txn_begin;
75 my $biblionumber = $record_id;
76 # First, checking if issues exist.
77 # If yes, nothing to do
78 my $biblio = Koha::Biblios->find( $biblionumber );
80 # TODO Replace with $biblio->get_issues->count
81 if ( C4::Biblio::CountItemsIssued( $biblionumber ) ) {
84 code => 'item_issued',
85 biblionumber => $biblionumber,
87 $schema->storage->txn_rollback;
88 $job->progress( ++$job_progress )->store;
93 my $holds = $biblio->holds;
94 while ( my $hold = $holds->next ) {
101 code => 'reserve_not_cancelled',
102 biblionumber => $biblionumber,
103 reserve_id => $hold->reserve_id,
106 $schema->storage->txn_rollback;
107 $job->progress( ++$job_progress )->store;
113 my $items = Koha::Items->search({ biblionumber => $biblionumber });
114 while ( my $item = $items->next ) {
115 my $error = $item->safe_delete;
116 if(ref($error) ne 'Koha::Item'){
119 code => 'item_not_deleted',
120 biblionumber => $biblionumber,
121 itemnumber => $item->itemnumber,
124 $schema->storage->txn_rollback;
125 $job->progress( ++$job_progress )->store;
130 # Finally, delete the biblio
132 C4::Biblio::DelBiblio( $biblionumber );
134 if ( $error or $@ ) {
137 code => 'biblio_not_deleted',
138 biblionumber => $biblionumber,
139 error => ($@ ? $@ : $error),
141 $schema->storage->txn_rollback;
142 $job->progress( ++$job_progress )->store;
148 code => 'biblio_deleted',
149 biblionumber => $biblionumber,
151 $report->{total_success}++;
152 $schema->storage->txn_commit;
153 $job->progress( ++$job_progress )->store;
156 my $job_data = decode_json $job->data;
157 $job_data->{messages} = \@messages;
158 $job_data->{report} = $report;
160 $job->ended_on(dt_from_string)
161 ->data(encode_json $job_data);
162 $job->status('finished') if $job->status ne 'cancelled';
173 my ( $self, $args) = @_;
175 # TODO Raise exception instead
176 return unless exists $args->{record_ids};
178 my @record_ids = @{ $args->{record_ids} };
180 $self->SUPER::enqueue({
181 job_size => scalar @record_ids,
182 job_args => {record_ids => \@record_ids,}