Bug 19813: Make MarcItemFieldsToOrder handle non-existing tags
authorKyle M Hall <kyle@bywatersolutions.com>
Thu, 14 Dec 2017 15:31:17 +0000 (10:31 -0500)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Fri, 22 Dec 2017 16:15:35 +0000 (13:15 -0300)
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 <kyle@bywatersolutions.com>
Signed-off-by: Marci Chen <mchen@mckinneytexas.org>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Amended: Fix typo occurrance and theses.

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

acqui/addorderiso2709.pl

index 0e0793b..3ce7e1e 100755 (executable)
@@ -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 occurrence
+            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 {
@@ -728,7 +731,7 @@ sub get_infos_syspref_on_item {
     @tags_list = List::MoreUtils::uniq(@tags_list);
 
     my $tags_count = equal_number_of_fields(\@tags_list, $record);
-    # Return if the number of theses fields in the record is not the same.
+    # Return if the number of these fields in the record is not the same.
     return -1 if $tags_count == -1;
 
     # Gather the fields
@@ -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};
             }