Bug 33974: (QA follow-up) Remove superflous import
[koha.git] / Koha / BackgroundJob / BatchDeleteBiblio.pm
1 package Koha::BackgroundJob::BatchDeleteBiblio;
2
3 use Modern::Perl;
4
5 use C4::Biblio;
6
7 use Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue;
8 use Koha::SearchEngine;
9 use Koha::SearchEngine::Indexer;
10
11 use base 'Koha::BackgroundJob';
12
13 =head1 NAME
14
15 Koha::BackgroundJob::BatchDeleteBiblio - Batch delete bibliographic records
16
17 This is a subclass of Koha::BackgroundJob.
18
19 =head1 API
20
21 =head2 Class methods
22
23 =head3 job_type
24
25 Define the job type of this job: batch_biblio_record_deletion
26
27 =cut
28
29 sub job_type {
30     return 'batch_biblio_record_deletion';
31 }
32
33 =head3 process
34
35 Process the job.
36
37 =cut
38
39 sub process {
40     my ( $self, $args ) = @_;
41
42     if ( $self->status eq 'cancelled' ) {
43         return;
44     }
45
46     # FIXME If the job has already been started, but started again (worker has been restart for instance)
47     # Then we will start from scratch and so double delete the same records
48
49     $self->start;
50
51     my $mmtid = $args->{mmtid};
52     my @record_ids = @{ $args->{record_ids} };
53
54     my $report = {
55         total_records => scalar @record_ids,
56         total_success => 0,
57     };
58     my @messages;
59     my $schema = Koha::Database->new->schema;
60     RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
61
62         last if $self->get_from_storage->status eq 'cancelled';
63
64         next unless $record_id;
65
66         $schema->storage->txn_begin;
67
68         my $biblionumber = $record_id;
69         # First, checking if issues exist.
70         # If yes, nothing to do
71         my $biblio = Koha::Biblios->find( $biblionumber );
72
73         # TODO Replace with $biblio->get_issues->count
74         if ( C4::Biblio::CountItemsIssued( $biblionumber ) ) {
75             push @messages, {
76                 type => 'warning',
77                 code => 'item_issued',
78                 biblionumber => $biblionumber,
79             };
80             $schema->storage->txn_rollback;
81
82             $self->step;
83             next;
84         }
85
86         # Cancel reserves
87         my $holds = $biblio->holds;
88         while ( my $hold = $holds->next ) {
89             eval{
90                 $hold->cancel({ skip_holds_queue => 1 });
91             };
92             if ( $@ ) {
93                 push @messages, {
94                     type => 'error',
95                     code => 'reserve_not_cancelled',
96                     biblionumber => $biblionumber,
97                     reserve_id => $hold->reserve_id,
98                     error => "$@",
99                 };
100                 $schema->storage->txn_rollback;
101
102                 $self->step;
103                 next RECORD_IDS;
104             }
105         }
106
107         # Delete items
108         my $items = Koha::Items->search({ biblionumber => $biblionumber });
109         while ( my $item = $items->next ) {
110             my $deleted = $item->safe_delete({ skip_record_index => 1, skip_holds_queue => 1 });
111             unless ( $deleted ) {
112                 push @messages, {
113                     type => 'error',
114                     code => 'item_not_deleted',
115                     biblionumber => $biblionumber,
116                     itemnumber => $item->itemnumber,
117                     error => @{$deleted->messages}[0]->message,
118                 };
119                 $schema->storage->txn_rollback;
120
121                 $self->step;
122                 next RECORD_IDS;
123             }
124         }
125
126         # Finally, delete the biblio
127         my $error = eval {
128             C4::Biblio::DelBiblio( $biblionumber, { skip_record_index => 1, skip_holds_queue => 1 } );
129         };
130         if ( $error or $@ ) {
131             push @messages, {
132                 type => 'error',
133                 code => 'biblio_not_deleted',
134                 biblionumber => $biblionumber,
135                 error => ($@ ? $@ : $error),
136             };
137             $schema->storage->txn_rollback;
138
139             $self->step;
140             next;
141         }
142
143         push @messages, {
144             type => 'success',
145             code => 'biblio_deleted',
146             biblionumber => $biblionumber,
147         };
148         $report->{total_success}++;
149         $schema->storage->txn_commit;
150
151         $self->step;
152     }
153
154     my @deleted_biblionumbers =
155       map { $_->{code} eq 'biblio_deleted' ? $_->{biblionumber} : () }
156           @messages;
157
158     if ( @deleted_biblionumbers ) {
159         my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
160         $indexer->index_records( \@deleted_biblionumbers, "recordDelete", "biblioserver" );
161
162         Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
163             {
164                 biblio_ids => \@deleted_biblionumbers
165             }
166         ) if C4::Context->preference('RealTimeHoldsQueue');
167     }
168
169     my $data = $self->decoded_data;
170     $data->{messages} = \@messages;
171     $data->{report} = $report;
172
173     $self->finish( $data );
174 }
175
176 =head3 enqueue
177
178 Enqueue the new job
179
180 =cut
181
182 sub enqueue {
183     my ( $self, $args) = @_;
184
185     # TODO Raise exception instead
186     return unless exists $args->{record_ids};
187
188     my @record_ids = @{ $args->{record_ids} };
189
190     $self->SUPER::enqueue({
191         job_size  => scalar @record_ids,
192         job_args  => {record_ids => \@record_ids,},
193         job_queue => 'long_tasks',
194     });
195 }
196
197 1;