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

(-)a/Koha/Biblio.pm (-7 / +18 lines)
Lines 375-390 sub host_record { Link Here
375
    return if C4::Context->preference('marcflavour') eq 'UNIMARC'; # TODO
375
    return if C4::Context->preference('marcflavour') eq 'UNIMARC'; # TODO
376
    return if $params->{no_items} && $self->items->count > 0;
376
    return if $params->{no_items} && $self->items->count > 0;
377
    my $marc = C4::Biblio::GetMarcBiblio($self->biblionumber);
377
    my $marc = C4::Biblio::GetMarcBiblio($self->biblionumber);
378
    my $hostfld = $marc->field('773');
378
    my @hostfld = $marc->field('773');
379
    return if !$hostfld;
379
    return if !@hostfld;
380
380
381
    # Extract record control number
381
    # Extract record control number
382
    my $rcn;
382
    # We pick the first $w with your MARCOrgCode or the first $w that has no
383
    if( $hostfld->subfield('w') =~ /\)\s*(\d+)/ ) {
383
    # code (between parentheses) at all.
384
        $rcn= $1;
384
    # Since $g is repeatable too, we join them together.
385
    my $orgcode = C4::Context->preference('MARCOrgCode') // q{};
386
    my $result = {};
387
    foreach my $f ( @hostfld ) {
388
        push my @subfields, $f->subfield('w');
389
        my @rcn = map { /^\($orgcode\)\s*(\d+)/i ? $1 : (); } @subfields;
390
        @rcn = map { /^\s*(\d+)/ ? $1 : (); } @subfields if !@rcn;
391
        if( @rcn ) {
392
            $result = { rcn => $rcn[0], subg => (join ';', $f->subfield('g')) };
393
            last;
394
        }
385
    }
395
    }
386
    my $host = $rcn ? Koha::Biblios->find($rcn) : undef;
396
387
    return wantarray ? ( $host, $hostfld->subfield('g') ) : $host;
397
    my $host = $result->{rcn} ? Koha::Biblios->find($result->{rcn}) : undef;
398
    return wantarray ? ( $host, $result->{subg} ) : $host;
388
}
399
}
389
400
390
=head3 type
401
=head3 type
(-)a/t/db_dependent/Koha/Biblio/host_record.t (-10 / +19 lines)
Lines 36-44 our $builder = t::lib::TestBuilder->new; Link Here
36
our $marc;
36
our $marc;
37
37
38
subtest 'host_record' => sub {
38
subtest 'host_record' => sub {
39
    plan tests => 9;
39
    plan tests => 10;
40
40
41
    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
41
    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
42
    t::lib::Mocks::mock_preference( 'MARCOrgCode', 'xyz' );
42
    my $mod = Test::MockModule->new('C4::Biblio');
43
    my $mod = Test::MockModule->new('C4::Biblio');
43
    $mod->mock( 'GetMarcBiblio', sub { return $marc; } );
44
    $mod->mock( 'GetMarcBiblio', sub { return $marc; } );
44
45
Lines 48-75 subtest 'host_record' => sub { Link Here
48
49
49
    is( $bib1->host_record, undef, 'Empty MARC record' );
50
    is( $bib1->host_record, undef, 'Empty MARC record' );
50
    $marc->append_fields(
51
    $marc->append_fields(
51
        MARC::Field->new( '773', '', '', g => 'relpart', w => 'xyz' ),
52
        MARC::Field->new( '773', '', '', g => 'g1', g => 'g2', w => '(xyz)' ),
52
    );
53
    );
53
    is( $bib1->host_record, undef, 'No control number found' );
54
    is( $bib1->host_record, undef, 'No control number found' );
54
    $marc->field('773')->update( w => 'xyz)' . ($bib2->biblionumber + 1) );
55
    $marc->field('773')->update( w => '(xyz)' . ($bib2->biblionumber + 1) );
55
    is( $bib1->host_record, undef, 'Control number does not exist' );
56
    is( $bib1->host_record, undef, 'Control number does not exist' );
56
    # Make it work now
57
57
    $marc->field('773')->update( w => 'xyz)' . $bib2->biblionumber );
58
    # Now replace by number without code
59
    $marc->field('773')->update( w => $bib2->biblionumber );
58
    my $host = $bib1->host_record;
60
    my $host = $bib1->host_record;
59
    is( ref( $host ), 'Koha::Biblio', 'Correct object returned' );
61
    is( ref( $host ), 'Koha::Biblio', 'Correct object returned' );
60
    is( $host->biblionumber, $bib2->biblionumber, 'Check biblionumber' );
62
    is( $host->biblionumber, $bib2->biblionumber, 'Check biblionumber' );
61
    # Add second 773
63
    # Replace first $w, add second 773 with code and wrong number
62
    $marc->append_fields( MARC::Field->new( '773', '', '', w => 'second' ) );
64
    $marc->field('773')->update( w => '(zzz)123' );
65
    $marc->append_fields( MARC::Field->new( '773', '', '', w => '(xyz)'. ( $bib2->biblionumber + 1 ) ) );
66
    is( $bib1->host_record, undef, 'No control number found' );
67
    # Change second 773 to a correct number (adding another $w)
68
    ($marc->field('773'))[1]->update( w => '(zzz)'.$bib2->biblionumber );
69
    ($marc->field('773'))[1]->add_subfields( w => '(xyz)'.$bib1->biblionumber );
63
    $host = $bib1->host_record;
70
    $host = $bib1->host_record;
64
    is( $host->biblionumber, $bib2->biblionumber, 'Two 773s, record still found' );
71
    is( $host->biblionumber, $bib1->biblionumber, 'Found last biblionumber' );
72
    $marc->delete_fields( ($marc->field('773'))[1] ); # remove second 773
73
65
    # Test no_items flag
74
    # Test no_items flag
75
    $marc->field('773')->update( w => '(xyz)'.$bib2->biblionumber );
66
    $host = $bib1->host_record({ no_items => 1 });
76
    $host = $bib1->host_record({ no_items => 1 });
67
    is( $host->biblionumber, $bib2->biblionumber, 'Record found with no_items' );
77
    is( $host->biblionumber, $bib2->biblionumber, 'Record found with no_items' );
68
    $builder->build({ source => 'Item', value => { biblionumber => $bib1->biblionumber } });
78
    $builder->build({ source => 'Item', value => { biblionumber => $bib1->biblionumber } });
69
    is( $bib1->host_record({ no_items => 1 }), undef, 'Record not found with no_items flag after adding one item' );
79
    is( $bib1->host_record({ no_items => 1 }), undef, 'Record not found with no_items flag after adding one item' );
70
    # Test list context
80
    # Test list context
71
    my @temp = $bib1->host_record;
81
    my @temp = $bib1->host_record;
72
    is( $temp[1], 'relpart', 'Return $g in list context' );
82
    is( $temp[1], 'g1;g2', 'Return $g in list context' );
73
};
83
};
74
84
75
$schema->storage->txn_rollback();
85
$schema->storage->txn_rollback();
76
- 

Return to bug 20310