View | Details | Raw Unified | Return to bug 35165
Collapse All | Expand All

(-)a/Koha/BackgroundJob/BatchDeleteItem.pm (-79 / +66 lines)
Lines 94-194 sub process { Link Here
94
        @deleted_biblionumbers );
94
        @deleted_biblionumbers );
95
95
96
    try {
96
    try {
97
        my $schema = Koha::Database->new->schema;
97
        my (@biblionumbers);
98
        $schema->txn_do(
98
        for my $record_id ( sort { $a <=> $b } @record_ids ) {
99
            sub {
100
                my (@biblionumbers);
101
                for my $record_id ( sort { $a <=> $b } @record_ids ) {
102
103
                    last if $self->get_from_storage->status eq 'cancelled';
104
105
                    my $item = Koha::Items->find($record_id) || next;
106
107
                    my $return = $item->safe_delete({ skip_record_index => 1, skip_holds_queue => 1 });
108
                    unless ( $return ) {
109
110
                        # FIXME Do we need to rollback the whole transaction if a deletion failed?
111
                        push @not_deleted_itemnumbers, $item->itemnumber;
112
                        push @messages,
113
                          {
114
                            type         => 'error',
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,
121
                          };
122
123
                        next;
124
                    }
125
126
                    push @deleted_itemnumbers, $item->itemnumber;
127
                    push @biblionumbers,       $item->biblionumber;
128
129
                    $report->{total_success}++;
130
                    $self->step;
131
                }
132
99
133
                # If there are no items left, delete the biblio
100
            last if $self->get_from_storage->status eq 'cancelled';
134
                my @updated_biblionumbers;
135
                for my $biblionumber ( uniq @biblionumbers ) {
136
                    my $items_count =
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 } );
141
                        unless ($error) {
142
                            push @deleted_biblionumbers, $biblionumber;
143
                        }
144
                    } else {
145
                        push @updated_biblionumbers, $biblionumber;
146
                    }
147
                }
148
101
149
                if (@deleted_biblionumbers) {
102
            my $item = Koha::Items->find($record_id) || next;
150
                    my $indexer = Koha::SearchEngine::Indexer->new(
151
                        { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
152
103
153
                    $indexer->index_records( \@deleted_biblionumbers,
104
            my $return = $item->safe_delete( { skip_record_index => 1, skip_holds_queue => 1 } );
154
                        'recordDelete', "biblioserver", undef );
105
            unless ($return) {
155
106
156
                    Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
107
                push @not_deleted_itemnumbers, $item->itemnumber;
157
                        {
108
                push @messages,
158
                            biblio_ids => \@deleted_biblionumbers
109
                    {
159
                        }
110
                    type         => 'error',
160
                    ) if C4::Context->preference('RealTimeHoldsQueue');
111
                    code         => 'item_not_deleted',
161
                }
112
                    itemnumber   => $item->itemnumber,
113
                    biblionumber => $item->biblionumber,
114
                    barcode      => $item->barcode,
115
                    title        => $item->biblio->title,
116
                    reason       => @{ $return->messages }[0]->message,
117
                    };
118
119
                next;
120
            }
162
121
163
                if (@updated_biblionumbers) {
122
            push @deleted_itemnumbers, $item->itemnumber;
164
                    my $indexer = Koha::SearchEngine::Indexer->new(
123
            push @biblionumbers,       $item->biblionumber;
165
                        { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
166
124
167
                    $indexer->index_records( \@updated_biblionumbers,
125
            $report->{total_success}++;
168
                        'specialUpdate', "biblioserver", undef );
126
            $self->step;
127
        }
169
128
170
                    Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
129
        # If there are no items left, delete the biblio
171
                        {
130
        my @updated_biblionumbers;
172
                            biblio_ids => \@updated_biblionumbers
131
        for my $biblionumber ( uniq @biblionumbers ) {
173
                        }
132
            my $items_count = Koha::Biblios->find($biblionumber)->items->count;
174
                    ) if C4::Context->preference('RealTimeHoldsQueue');
133
            if ( $delete_biblios && $items_count == 0 ) {
134
                my $error = C4::Biblio::DelBiblio(
135
                    $biblionumber,
136
                    { skip_record_index => 1, skip_holds_queue => 1 }
137
                );
138
                unless ($error) {
139
                    push @deleted_biblionumbers, $biblionumber;
175
                }
140
                }
141
            } else {
142
                push @updated_biblionumbers, $biblionumber;
176
            }
143
            }
177
        );
144
        }
178
    }
145
179
    catch {
146
        if (@deleted_biblionumbers) {
147
            my $indexer = Koha::SearchEngine::Indexer->new( { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
148
149
            $indexer->index_records(
150
                \@deleted_biblionumbers,
151
                'recordDelete', "biblioserver", undef
152
            );
153
154
            Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue( { biblio_ids => \@deleted_biblionumbers } )
155
                if C4::Context->preference('RealTimeHoldsQueue');
156
        }
157
158
        if (@updated_biblionumbers) {
159
            my $indexer = Koha::SearchEngine::Indexer->new( { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
160
161
            $indexer->index_records(
162
                \@updated_biblionumbers,
163
                'specialUpdate', "biblioserver", undef
164
            );
165
166
            Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue( { biblio_ids => \@updated_biblionumbers } )
167
                if C4::Context->preference('RealTimeHoldsQueue');
168
        }
169
    } catch {
180
170
181
        warn $_;
171
        warn $_;
182
172
183
        push @messages,
173
        push @messages,
184
          {
174
            {
185
            type  => 'error',
175
            type  => 'error',
186
            code  => 'unknown',
176
            code  => 'unknown',
187
            error => $_,
177
            error => $_,
188
          };
178
            };
189
179
190
        die "Something terrible has happened!"
191
          if ( $_ =~ /Rollback failed/ );    # Rollback failed
192
    };
180
    };
193
181
194
    $report->{deleted_itemnumbers}     = \@deleted_itemnumbers;
182
    $report->{deleted_itemnumbers}     = \@deleted_itemnumbers;
195
- 

Return to bug 35165