From 693a1f1f1055d84ce16371bfd6fd981111ace406 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 14 Dec 2017 10:31:17 -0500 Subject: [PATCH] Bug 19813 - MarcItemFieldsToOrder cannot handle a tag not existing MarcItemFieldsToOrder defines how Koha looks at tags in order records to generate item data. Let's look at a simplified case: homebranch: 955$a holdingbranch: 956$a So, here we are looking at 955 for the home branch, and 956 for the holding branch. So, it should make sense that Koha requires that these fields exist in equal number in the record. That is, for each 955, there should be a corresponding 956. Let's look at a different case: homebranch: 946$a|975$a holdingbranch: 946$a|975$a In this case, we are using the fallback behavior. VendorA stores the branch data in 946, and VendorB stores it in 975. This seems like it would work, but it won't! That's because Koha is expecting there to be the same number of 946's as there are 975's! In reality, the VendorA records will have a number of 946's, and *zero* 975's. The inverse will be true for VendorB. Koha should be able to skip those tags that simply don't exist in the record. Test Plan: 1) Set MarcItemFieldsToOrder to something like: homebranch: 946$a|975$a holdingbranch: 946$a|975$a budget_code: 946$f|975$f itype: 946$y|975$y notforloan: 946$l|975$l ccode: 946$t|975$c quantity: 946$q|975$q price: 946$p|975$p itemcallnumber: 946$n|975$n loc: 946$c|975$t 2) Create a record using only the 975 tag for item building data 3) Import the record into Koha 4) Create a basket 5) Attempt to add the record to the basket 6) Note the unequal fields error 7) Apply this patch 8) Reload the page 9) No error! Signed-off-by: Kyle M Hall Signed-off-by: Marci Chen --- acqui/addorderiso2709.pl | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/acqui/addorderiso2709.pl b/acqui/addorderiso2709.pl index 0e0793b..5b13afd 100755 --- a/acqui/addorderiso2709.pl +++ b/acqui/addorderiso2709.pl @@ -687,18 +687,21 @@ sub get_infos_syspref { sub equal_number_of_fields { my ($tags_list, $record) = @_; - my $refcount = 0; - my $count = 0; + my $tag_fields_count; for my $tag (@$tags_list) { - return -1 if $count != $refcount; - $count = 0; - for my $field ($record->field($tag)) { - $count++; + my @fields = $record->field($tag); + $tag_fields_count->{$tag} = scalar @fields; + } + + my $tags_count; + foreach my $key ( keys %$tag_fields_count ) { + if ( $tag_fields_count->{$key} > 0 ) { # Having 0 of a field is ok + $tags_count //= $tag_fields_count->{$key}; # Start with the count from the first occurrance + return -1 if $tag_fields_count->{$key} != $tags_count; # All counts of various fields should be equal if they exist } - $refcount = $count if ($refcount == 0); } - return -1 if $count != $refcount; - return $count; + + return $tags_count; } sub get_infos_syspref_on_item { @@ -749,7 +752,7 @@ sub get_infos_syspref_on_item { for my $field ( @fields ) { my ( $f, $sf ) = split /\$/, $field; next unless $f and $sf; - my $v = $fields_hash->{$f}[$i]->subfield( $sf ); + my $v = $fields_hash->{$f}[$i] ? $fields_hash->{$f}[$i]->subfield( $sf ) : undef; $r->{$field_name} = $v if (defined $v); last if $yaml->{$field}; } -- 2.10.2