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

(-)a/Koha/Filter/MARC/EmbedItemsAvailability.pm (+98 lines)
Line 0 Link Here
1
package Koha::Filter::MARC::EmbedItemsAvailability;
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
=head1 NAME
19
20
Koha::Filter::MARC::EmbedItemsAvailability - calculates item availability and embeds
21
it in a fixed MARC subfield for indexing.
22
23
=head1 SYNOPSIS
24
25
my $processor = Koha::RecordProcessor->new({ filters => ('EmbedItemsAvailability') });
26
27
=head1 DESCRIPTION
28
29
Filter to embed items not on loan count information into MARC records.
30
31
=cut
32
33
use Modern::Perl;
34
35
use C4::Biblio qw/GetMarcFromKohaField/;
36
use Koha::Items;
37
38
use base qw(Koha::RecordProcessor::Base);
39
our $NAME = 'EmbedItemsAvailability';
40
41
=head2 filter
42
43
    my $newrecord = $filter->filter($record);
44
    my $newrecords = $filter->filter(\@records);
45
46
Embed not on loan items count into the specified record(s) and return the result.
47
48
=cut
49
50
sub filter {
51
    my $self = shift;
52
    my $record = shift;
53
    my $newrecord;
54
55
    return unless defined $record;
56
57
    if (ref $record eq 'ARRAY') {
58
        my @recarray;
59
        foreach my $thisrec (@$record) {
60
            push @recarray, _processrecord($thisrec);
61
        }
62
        $newrecord = \@recarray;
63
    } elsif (ref $record eq 'MARC::Record') {
64
        $newrecord = _processrecord($record);
65
    }
66
67
    return $newrecord;
68
}
69
70
sub _processrecord {
71
72
    my $record = shift;
73
74
    my ($biblionumber_field, $biblionumber_subfield) = GetMarcFromKohaField("biblio.biblionumber", '');
75
    my $biblionumber = $record->field($biblionumber_field)->subfield($biblionumber_subfield);
76
77
    my $not_onloan_items = 0;
78
    my $items = Koha::Items->search({ biblionumber => $biblionumber });
79
80
    while ( my $item = $items->next ) {
81
        $not_onloan_items++
82
            if not $item->onloan;
83
    }
84
85
    # check for field 999
86
    my $destination_field = $record->field('999');
87
    if (defined $destination_field) {
88
        # we have a field, add what we need
89
        $destination_field->update( x => $not_onloan_items );
90
    }
91
    else {
92
        # no field, create one
93
        $destination_field = MARC::Field->new( '999', '', '', x => $not_onloan_items );
94
        $record->append_fields([$destination_field]);
95
    }
96
97
    return $record;
98
}
(-)a/t/db_dependent/Koha/Filter/EmbedItemsAvailability.t (-1 / +133 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 t::lib::Mocks;
22
use t::lib::TestBuilder;
23
24
use MARC::Record;
25
26
use C4::Biblio qw/AddBiblio GetMarcBiblio/;
27
use Koha::Database;
28
use Koha::RecordProcessor;
29
30
my $schema  = Koha::Database->schema();
31
my $builder = t::lib::TestBuilder->new();
32
33
subtest 'EmbedItemsAvailability tests' => sub {
34
35
    plan tests => 6;
36
37
    $schema->storage->txn_begin();
38
39
    # MARC21 tests
40
    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
41
    # Create a dummy record
42
    my ( $biblionumber, $biblioitemnumber ) = AddBiblio(MARC::Record->new(), '');
43
44
    # Add some items with different onloan values
45
    $builder->build(
46
        {   source => 'Item',
47
            value  => {
48
                biblionumber     => $biblionumber,
49
                biblioitemnumber => $biblioitemnumber,
50
                onloan           => '2017-01-01'
51
            }
52
        }
53
    );
54
    $builder->build(
55
        {   source => 'Item',
56
            value  => {
57
                biblionumber     => $biblionumber,
58
                biblioitemnumber => $biblioitemnumber,
59
                onloan           => undef
60
            }
61
        }
62
    );
63
    $builder->build(
64
        {   source => 'Item',
65
            value  => {
66
                biblionumber     => $biblionumber,
67
                biblioitemnumber => $biblioitemnumber,
68
                onloan           => '2017-01-02'
69
            }
70
        }
71
    );
72
73
    my $processor = Koha::RecordProcessor->new( { filters => ('EmbedItemsAvailability') } );
74
    is( ref($processor), 'Koha::RecordProcessor', 'Created record processor' );
75
76
    my $record = GetMarcBiblio($biblionumber);
77
    ok( !defined $record->field('999')->subfield('x'), q{The record doesn't originally contain 999$x} );
78
    # Apply filter
79
    $processor->process($record);
80
    is($record->field('999')->subfield('x'), 1, 'There is only one item with undef onloan');
81
82
    $schema->storage->txn_rollback();
83
84
    # UNIMARC tests (999 is not created)
85
    t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
86
87
    $schema->storage->txn_begin();
88
89
    # Create a dummy record
90
    ( $biblionumber, $biblioitemnumber ) = AddBiblio(MARC::Record->new(), '');
91
92
    # Add some items with different onloan values
93
    $builder->build(
94
        {   source => 'Item',
95
            value  => {
96
                biblionumber     => $biblionumber,
97
                biblioitemnumber => $biblioitemnumber,
98
                onloan           => '2017-01-01'
99
            }
100
        }
101
    );
102
    $builder->build(
103
        {   source => 'Item',
104
            value  => {
105
                biblionumber     => $biblionumber,
106
                biblioitemnumber => $biblioitemnumber,
107
                onloan           => undef
108
            }
109
        }
110
    );
111
    $builder->build(
112
        {   source => 'Item',
113
            value  => {
114
                biblionumber     => $biblionumber,
115
                biblioitemnumber => $biblioitemnumber,
116
                onloan           => '2017-01-02'
117
            }
118
        }
119
    );
120
121
    $processor = Koha::RecordProcessor->new( { filters => ('EmbedItemsAvailability') } );
122
    is( ref($processor), 'Koha::RecordProcessor', 'Created record processor' );
123
124
    $record = GetMarcBiblio($biblionumber);
125
    ok( !defined $record->field('999')->subfield('x'), q{The record doesn't originally contain 999$x} );
126
    # Apply filter
127
    $processor->process($record);
128
    is($record->field('999')->subfield('x'), 1, 'There is only one item with undef onloan');
129
130
    $schema->storage->txn_rollback();
131
};
132
133
1;

Return to bug 18208