From 38b12baadfbd7cdc932bae0463ad99080ecfc004 Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Fri, 17 Oct 2025 06:58:49 +0300 Subject: [PATCH] Bug 29326 - _ALL_ should not override other rules in UpdateItemLocationOnCheckin. Backend. Refactored for a more efficient testing of rules. Added _DEFAULT_ to fall back to if _ALL_ or a specific location rule is not defined. --- Koha/Item.pm | 102 +++++++++++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 56 deletions(-) diff --git a/Koha/Item.pm b/Koha/Item.pm index 05d2e97570e..96a97f9f6d8 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -2783,6 +2783,27 @@ FIXME: It should return I<$self>. See bug 35270. sub location_update_trigger { my ( $self, $action ) = @_; + if (my $new_location = $self->_check_location_update_rules($action)) { + my $messages = { + 'ItemLocationUpdated' => { + from => $self->location, + to => $new_location, + } + }; + $self->location($new_location)->store( + { + log_action => 0, + skip_record_index => 1, + skip_holds_queue => 1 + } + ); + return $messages; + } + return undef; +} + +sub _check_location_update_rules { + my ($self, $action) = @_; my ( $update_loc_rules, $messages ); if ( $action eq 'checkin' ) { $update_loc_rules = C4::Context->yaml_preference('UpdateItemLocationOnCheckin'); @@ -2791,64 +2812,33 @@ sub location_update_trigger { } if ($update_loc_rules) { - if ( defined $update_loc_rules->{_ALL_} ) { - if ( $update_loc_rules->{_ALL_} eq '_PERM_' ) { - $update_loc_rules->{_ALL_} = $self->permanent_location; - } - if ( $update_loc_rules->{_ALL_} eq '_BLANK_' ) { - $update_loc_rules->{_ALL_} = ''; - } - if ( - ( defined $self->location && $self->location ne $update_loc_rules->{_ALL_} ) - || ( !defined $self->location - && $update_loc_rules->{_ALL_} ne "" ) - ) - { - $messages->{'ItemLocationUpdated'} = - { from => $self->location, to => $update_loc_rules->{_ALL_} }; - $self->location( $update_loc_rules->{_ALL_} )->store( - { - log_action => 0, - skip_record_index => 1, - skip_holds_queue => 1 - } - ); - } - } else { - foreach my $key ( keys %$update_loc_rules ) { - if ( $update_loc_rules->{$key} eq '_PERM_' ) { - $update_loc_rules->{$key} = $self->permanent_location; - } elsif ( $update_loc_rules->{$key} eq '_BLANK_' ) { - $update_loc_rules->{$key} = ''; - } - if ( - ( - defined $self->location - && $self->location eq $key - && $self->location ne $update_loc_rules->{$key} - ) - || ( $key eq '_BLANK_' - && ( !defined $self->location || $self->location eq '' ) - && $update_loc_rules->{$key} ne '' ) - ) - { - $messages->{'ItemLocationUpdated'} = { - from => $self->location, - to => $update_loc_rules->{$key} - }; - $self->location( $update_loc_rules->{$key} )->store( - { - log_action => 0, - skip_record_index => 1, - skip_holds_queue => 1 - } - ); - last; - } - } + if (defined($update_loc_rules->{'_ALL_'})) { + return $self->_check_location_update_rule($update_loc_rules->{'_ALL_'}); + } + elsif (defined($update_loc_rules->{$self->location//''})) { + return $self->_check_location_update_rule($update_loc_rules->{$self->location}); } + elsif (defined($update_loc_rules->{'_DEFAULT_'})) { + return $self->_check_location_update_rule($update_loc_rules->{'_DEFAULT_'}); + } + } +} + + +sub _check_location_update_rule { + my ($self, $update_loc_rule) = @_; + + if ($update_loc_rule eq '_PERM_') { + $update_loc_rule = $self->permanent_location; + } + if ($update_loc_rule eq '_BLANK_') { + $update_loc_rule = ''; + } + if ((defined($self->location) && $self->location ne $update_loc_rule) || + (not(defined($self->location)) && $update_loc_rule ne "" )) { + return $update_loc_rule; } - return $messages; + return undef; } =head3 z3950_status -- 2.34.1