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

(-)a/Koha/BackgroundJob/BatchUpdateItem.pm (-20 / +16 lines)
Lines 111-137 sub process { Link Here
111
    };
111
    };
112
112
113
    try {
113
    try {
114
        my $schema = Koha::Database->new->schema;
114
        my ($results) =
115
        $schema->txn_do(
115
          Koha::Items->search( { itemnumber => \@record_ids } )
116
            sub {
116
          ->batch_update(
117
                my ($results) =
117
            {
118
                  Koha::Items->search( { itemnumber => \@record_ids } )
118
                regex_mod  => $regex_mod,
119
                  ->batch_update(
119
                new_values => $new_values,
120
                    {
120
                exclude_from_local_holds_priority =>
121
                        regex_mod  => $regex_mod,
121
                  $exclude_from_local_holds_priority,
122
                        new_values => $new_values,
122
                callback => sub {
123
                        exclude_from_local_holds_priority =>
123
                    my ($progress) = @_;
124
                          $exclude_from_local_holds_priority,
124
                    $job->progress($progress)->store;
125
                        callback => sub {
125
                },
126
                            my ($progress) = @_;
127
                            $job->progress($progress)->store;
128
                        },
129
                    }
130
                  );
131
                $report->{modified_itemnumbers} = $results->{modified_itemnumbers};
132
                $report->{modified_fields}      = $results->{modified_fields};
133
            }
126
            }
134
        );
127
          );
128
        $report->{modified_itemnumbers} = $results->{modified_itemnumbers};
129
        $report->{modified_fields}      = $results->{modified_fields};
135
    }
130
    }
136
    catch {
131
    catch {
137
        warn $_;
132
        warn $_;
Lines 139-144 sub process { Link Here
139
          if ( $_ =~ /Rollback failed/ );    # Rollback failed
134
          if ( $_ =~ /Rollback failed/ );    # Rollback failed
140
    };
135
    };
141
136
137
    $job->discard_changes;
142
    my $job_data = decode_json encode_utf8 $job->data;
138
    my $job_data = decode_json encode_utf8 $job->data;
143
    $job_data->{report} = $report;
139
    $job_data->{report} = $report;
144
140
(-)a/Koha/Items.pm (-77 / +80 lines)
Lines 20-25 package Koha::Items; Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
use Array::Utils qw( array_minus );
21
use Array::Utils qw( array_minus );
22
use List::MoreUtils qw( uniq );
22
use List::MoreUtils qw( uniq );
23
use Try::Tiny;
23
24
24
use C4::Context;
25
use C4::Context;
25
use C4::Biblio qw( GetMarcStructure GetMarcFromKohaField );
26
use C4::Biblio qw( GetMarcStructure GetMarcFromKohaField );
Lines 242-339 sub batch_update { Link Here
242
243
243
    my (@modified_itemnumbers, $modified_fields);
244
    my (@modified_itemnumbers, $modified_fields);
244
    my $i;
245
    my $i;
246
    my $schema = Koha::Database->new->schema;
245
    while ( my $item = $self->next ) {
247
    while ( my $item = $self->next ) {
246
248
247
        my $modified_holds_priority = 0;
249
        try {$schema->txn_do(sub {
248
        if ( defined $exclude_from_local_holds_priority ) {
250
            my $modified_holds_priority = 0;
249
            if(!defined $item->exclude_from_local_holds_priority || $item->exclude_from_local_holds_priority != $exclude_from_local_holds_priority) {
251
            if ( defined $exclude_from_local_holds_priority ) {
250
                $item->exclude_from_local_holds_priority($exclude_from_local_holds_priority)->store;
252
                if(!defined $item->exclude_from_local_holds_priority || $item->exclude_from_local_holds_priority != $exclude_from_local_holds_priority) {
251
                $modified_holds_priority = 1;
253
                    $item->exclude_from_local_holds_priority($exclude_from_local_holds_priority)->store;
254
                    $modified_holds_priority = 1;
255
                }
252
            }
256
            }
253
        }
254
255
        my $modified = 0;
256
        my $new_values = {%$new_values};    # Don't modify the original
257
258
        my $old_values = $item->unblessed;
259
        if ( $item->more_subfields_xml ) {
260
            $old_values = {
261
                %$old_values,
262
                %{$item->additional_attributes->to_hashref},
263
            };
264
        }
265
257
266
        for my $attr ( keys %$regex_mod ) {
258
            my $modified = 0;
267
            my $old_value = $old_values->{$attr};
259
            my $new_values = {%$new_values};    # Don't modify the original
268
260
269
            next unless $old_value;
261
            my $old_values = $item->unblessed;
262
            if ( $item->more_subfields_xml ) {
263
                $old_values = {
264
                    %$old_values,
265
                    %{$item->additional_attributes->to_hashref},
266
                };
267
            }
270
268
271
            my $value = apply_regex(
269
            for my $attr ( keys %$regex_mod ) {
272
                {
270
                my $old_value = $old_values->{$attr};
273
                    %{ $regex_mod->{$attr} },
274
                    value => $old_value,
275
                }
276
            );
277
271
278
            $new_values->{$attr} = $value;
272
                next unless $old_value;
279
        }
280
273
281
        for my $attribute ( keys %$new_values ) {
274
                my $value = apply_regex(
282
            next if $attribute eq 'more_subfields_xml'; # Already counted before
275
                    {
276
                        %{ $regex_mod->{$attr} },
277
                        value => $old_value,
278
                    }
279
                );
283
280
284
            my $old = $old_values->{$attribute};
281
                $new_values->{$attr} = $value;
285
            my $new = $new_values->{$attribute};
282
            }
286
            $modified++
287
              if ( defined $old xor defined $new )
288
              || ( defined $old && defined $new && $new ne $old );
289
        }
290
283
291
        { # Dealing with more_subfields_xml
284
            for my $attribute ( keys %$new_values ) {
292
285
                next if $attribute eq 'more_subfields_xml'; # Already counted before
293
            my $frameworkcode = $item->biblio->frameworkcode;
294
            my $tagslib = C4::Biblio::GetMarcStructure( 1, $frameworkcode, { unsafe => 1 });
295
            my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
296
297
            my @more_subfield_tags = map {
298
                (
299
                         ref($_)
300
                      && %$_
301
                      && !$_->{kohafield}    # Get subfields that are not mapped
302
                  )
303
                  ? $_->{tagsubfield}
304
                  : ()
305
            } values %{ $tagslib->{$itemtag} };
306
307
            my $more_subfields_xml = Koha::Item::Attributes->new(
308
                {
309
                    map {
310
                        exists $new_values->{$_} ? ( $_ => $new_values->{$_} )
311
                          : exists $old_values->{$_}
312
                          ? ( $_ => $old_values->{$_} )
313
                          : ()
314
                    } @more_subfield_tags
315
                }
316
            )->to_marcxml($frameworkcode);
317
286
318
            $new_values->{more_subfields_xml} = $more_subfields_xml;
287
                my $old = $old_values->{$attribute};
288
                my $new = $new_values->{$attribute};
289
                $modified++
290
                  if ( defined $old xor defined $new )
291
                  || ( defined $old && defined $new && $new ne $old );
292
            }
319
293
320
            delete $new_values->{$_} for @more_subfield_tags; # Clean the hash
294
            { # Dealing with more_subfields_xml
295
296
                my $frameworkcode = $item->biblio->frameworkcode;
297
                my $tagslib = C4::Biblio::GetMarcStructure( 1, $frameworkcode, { unsafe => 1 });
298
                my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
299
300
                my @more_subfield_tags = map {
301
                    (
302
                             ref($_)
303
                          && %$_
304
                          && !$_->{kohafield}    # Get subfields that are not mapped
305
                      )
306
                      ? $_->{tagsubfield}
307
                      : ()
308
                } values %{ $tagslib->{$itemtag} };
309
310
                my $more_subfields_xml = Koha::Item::Attributes->new(
311
                    {
312
                        map {
313
                            exists $new_values->{$_} ? ( $_ => $new_values->{$_} )
314
                              : exists $old_values->{$_}
315
                              ? ( $_ => $old_values->{$_} )
316
                              : ()
317
                        } @more_subfield_tags
318
                    }
319
                )->to_marcxml($frameworkcode);
320
321
                $new_values->{more_subfields_xml} = $more_subfields_xml;
322
323
                delete $new_values->{$_} for @more_subfield_tags; # Clean the hash
321
324
322
        }
325
            }
323
326
324
        if ( $modified ) {
327
            if ( $modified ) {
325
            my $itemlost_pre = $item->itemlost;
328
                my $itemlost_pre = $item->itemlost;
326
            $item->set($new_values)->store({skip_record_index => 1});
329
                $item->set($new_values)->store({skip_record_index => 1});
327
330
328
            LostItem(
331
                LostItem(
329
                $item->itemnumber, 'batchmod', undef,
332
                    $item->itemnumber, 'batchmod', undef,
330
                { skip_record_index => 1 }
333
                    { skip_record_index => 1 }
331
            ) if $item->itemlost
334
                ) if $item->itemlost
332
                  and not $itemlost_pre;
335
                      and not $itemlost_pre;
333
336
334
            push @modified_itemnumbers, $item->itemnumber if $modified || $modified_holds_priority;
337
                push @modified_itemnumbers, $item->itemnumber if $modified || $modified_holds_priority;
335
            $modified_fields += $modified + $modified_holds_priority;
338
                $modified_fields += $modified + $modified_holds_priority;
336
        }
339
            }
340
        })};
337
341
338
        if ( $callback ) {
342
        if ( $callback ) {
339
            $callback->(++$i);
343
            $callback->(++$i);
340
- 

Return to bug 28445