@@ -, +, @@ code --- Koha/Biblio.pm | 25 ++++++++++++++++++------- t/db_dependent/Koha/Biblio/host_record.t | 28 +++++++++++++++++++--------- 2 files changed, 37 insertions(+), 16 deletions(-) --- a/Koha/Biblio.pm +++ a/Koha/Biblio.pm @@ -375,16 +375,27 @@ sub host_record { return if C4::Context->preference('marcflavour') eq 'UNIMARC'; # TODO return if $params->{no_items} && $self->items->count > 0; my $marc = C4::Biblio::GetMarcBiblio({ biblionumber => $self->biblionumber }); - my $hostfld = $marc->field('773'); - return if !$hostfld; + my @hostfld = $marc->field('773'); + return if !@hostfld; # Extract record control number - my $rcn; - if( $hostfld->subfield('w') =~ /\)\s*(\d+)/ ) { - $rcn= $1; + # We pick the first $w with your MARCOrgCode or the first $w that has no + # code (between parentheses) at all. + # Since $g is repeatable too, we join them together. + my $orgcode = C4::Context->preference('MARCOrgCode') // q{}; + my $result = {}; + foreach my $f ( @hostfld ) { + push my @subfields, $f->subfield('w'); + my @rcn = map { /^\($orgcode\)\s*(\d+)/i ? $1 : (); } @subfields; + @rcn = map { /^\s*(\d+)/ ? $1 : (); } @subfields if !@rcn; + if( @rcn ) { + $result = { rcn => $rcn[0], subg => (join ';', $f->subfield('g')) }; + last; + } } - my $host = $rcn ? Koha::Biblios->find($rcn) : undef; - return wantarray ? ( $host, $hostfld->subfield('g') ) : $host; + + my $host = $result->{rcn} ? Koha::Biblios->find($result->{rcn}) : undef; + return wantarray ? ( $host, $result->{subg} ) : $host; } =head3 type --- a/t/db_dependent/Koha/Biblio/host_record.t +++ a/t/db_dependent/Koha/Biblio/host_record.t @@ -36,9 +36,10 @@ our $builder = t::lib::TestBuilder->new; our $marc; subtest 'host_record' => sub { - plan tests => 9; + plan tests => 10; t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' ); + t::lib::Mocks::mock_preference( 'MARCOrgCode', 'xyz' ); my $mod = Test::MockModule->new('C4::Biblio'); $mod->mock( 'GetMarcBiblio', sub { return $marc; } ); @@ -48,28 +49,37 @@ subtest 'host_record' => sub { is( $bib1->host_record, undef, 'Empty MARC record' ); $marc->append_fields( - MARC::Field->new( '773', '', '', g => 'relpart', w => 'xyz' ), + MARC::Field->new( '773', '', '', g => 'g1', g => 'g2', w => '(xyz)' ), ); is( $bib1->host_record, undef, 'No control number found' ); - $marc->field('773')->update( w => 'xyz)' . ($bib2->biblionumber + 1) ); + $marc->field('773')->update( w => '(xyz)' . ($bib2->biblionumber + 1) ); is( $bib1->host_record, undef, 'Control number does not exist' ); - # Make it work now - $marc->field('773')->update( w => 'xyz)' . $bib2->biblionumber ); + + # Now replace by number without code + $marc->field('773')->update( w => $bib2->biblionumber ); my $host = $bib1->host_record; is( ref( $host ), 'Koha::Biblio', 'Correct object returned' ); is( $host->biblionumber, $bib2->biblionumber, 'Check biblionumber' ); - # Add second 773 - $marc->append_fields( MARC::Field->new( '773', '', '', w => 'second' ) ); + # Replace first $w, add second 773 with code and wrong number + $marc->field('773')->update( w => '(zzz)123' ); + $marc->append_fields( MARC::Field->new( '773', '', '', w => '(xyz)'. ( $bib2->biblionumber + 1 ) ) ); + is( $bib1->host_record, undef, 'No control number found' ); + # Change second 773 to a correct number (adding another $w) + ($marc->field('773'))[1]->update( w => '(zzz)'.$bib2->biblionumber ); + ($marc->field('773'))[1]->add_subfields( w => '(xyz)'.$bib1->biblionumber ); $host = $bib1->host_record; - is( $host->biblionumber, $bib2->biblionumber, 'Two 773s, record still found' ); + is( $host->biblionumber, $bib1->biblionumber, 'Found last biblionumber' ); + $marc->delete_fields( ($marc->field('773'))[1] ); # remove second 773 + # Test no_items flag + $marc->field('773')->update( w => '(xyz)'.$bib2->biblionumber ); $host = $bib1->host_record({ no_items => 1 }); is( $host->biblionumber, $bib2->biblionumber, 'Record found with no_items' ); $builder->build({ source => 'Item', value => { biblionumber => $bib1->biblionumber } }); is( $bib1->host_record({ no_items => 1 }), undef, 'Record not found with no_items flag after adding one item' ); # Test list context my @temp = $bib1->host_record; - is( $temp[1], 'relpart', 'Return $g in list context' ); + is( $temp[1], 'g1;g2', 'Return $g in list context' ); }; $schema->storage->txn_rollback(); --