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

(-)a/Koha/Biblio.pm (+32 lines)
Lines 355-360 sub has_items_waiting_or_intransit { Link Here
355
    return 0;
355
    return 0;
356
}
356
}
357
357
358
=head3 host_record
359
360
    $host = $biblio->host_record;
361
    ( $host, $relatedparts ) = $biblio->host_record;
362
363
    Returns host biblio record from MARC21 773 (undef if no 773 present).
364
    Currently, looks only at the first 773 field.
365
    The optional parameter no_items triggers a check if $biblio has items.
366
    If there are, the sub returns undef.
367
    Called in list context, it also returns 773$g (related parts).
368
369
=cut
370
371
sub host_record {
372
    my ($self, $params) = @_;
373
    my $no_items = $params->{no_items};
374
375
    return if C4::Context->preference('marcflavour') eq 'UNIMARC'; # TODO
376
    return if $params->{no_items} && $self->items->count > 0;
377
    my $marc = C4::Biblio::GetMarcBiblio($self->biblionumber);
378
    my $hostfld = $marc->field('773');
379
    return if !$hostfld;
380
381
    # Extract record control number
382
    my $rcn;
383
    if( $hostfld->subfield('w') =~ /\)\s*(\d+)/ ) {
384
        $rcn= $1;
385
    }
386
    my $host = $rcn ? Koha::Biblios->find($rcn) : undef;
387
    return wantarray ? ( $host, $hostfld->subfield('g') ) : $host;
388
}
389
358
=head3 type
390
=head3 type
359
391
360
=cut
392
=cut
(-)a/t/db_dependent/Koha/Biblio/host_record.t (-1 / +75 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 1;
21
use Data::Dumper qw/Dumper/;
22
use MARC::Field;
23
use MARC::Record;
24
use Test::MockModule;
25
26
use t::lib::TestBuilder;
27
use t::lib::Mocks;
28
use Koha::Database;
29
use Koha::Biblios;
30
use C4::Biblio;
31
32
33
my $schema = Koha::Database->new->schema;
34
$schema->storage->txn_begin;
35
our $builder = t::lib::TestBuilder->new;
36
our $marc;
37
38
subtest 'host_record' => sub {
39
    plan tests => 9;
40
41
    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
42
    my $mod = Test::MockModule->new('C4::Biblio');
43
    $mod->mock( 'GetMarcBiblio', sub { return $marc; } );
44
45
    my $bib1 = $builder->build_object({ class => 'Koha::Biblios' });
46
    my $bib2 = $builder->build_object({ class => 'Koha::Biblios' });
47
    $marc = MARC::Record->new;
48
49
    is( $bib1->host_record, undef, 'Empty MARC record' );
50
    $marc->append_fields(
51
        MARC::Field->new( '773', '', '', g => 'relpart', w => 'xyz' ),
52
    );
53
    is( $bib1->host_record, undef, 'No control number found' );
54
    $marc->field('773')->update( w => 'xyz)' . ($bib2->biblionumber + 1) );
55
    is( $bib1->host_record, undef, 'Control number does not exist' );
56
    # Make it work now
57
    $marc->field('773')->update( w => 'xyz)' . $bib2->biblionumber );
58
    my $host = $bib1->host_record;
59
    is( ref( $host ), 'Koha::Biblio', 'Correct object returned' );
60
    is( $host->biblionumber, $bib2->biblionumber, 'Check biblionumber' );
61
    # Add second 773
62
    $marc->append_fields( MARC::Field->new( '773', '', '', w => 'second' ) );
63
    $host = $bib1->host_record;
64
    is( $host->biblionumber, $bib2->biblionumber, 'Two 773s, record still found' );
65
    # Test no_items flag
66
    $host = $bib1->host_record({ no_items => 1 });
67
    is( $host->biblionumber, $bib2->biblionumber, 'Record found with no_items' );
68
    $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' );
70
    # Test list context
71
    my @temp = $bib1->host_record;
72
    is( $temp[1], 'relpart', 'Return $g in list context' );
73
};
74
75
$schema->storage->txn_rollback();

Return to bug 20310