Bug 18208: Add RecordProcessor filter to inject not onloan count to MARC records
authorTomas Cohen Arazi <tomascohen@theke.io>
Fri, 3 Mar 2017 18:30:22 +0000 (15:30 -0300)
committerKyle M Hall <kyle@bywatersolutions.com>
Thu, 13 Apr 2017 12:51:04 +0000 (08:51 -0400)
This patch adds a new filter for MARC records to be used with
Koha::RecordProcessor. It's purpose is to inject the information about
items not-onloan in a fixed subfield, 999$x.

To test:
- Apply the patch
- Run:
  $ prove t/db_dependent/Koha/Filter/EmbedItemsAvailability.t
=> SUCCESS: Tests pass!
- Sign off :-D

Sponsored-by: ByWater Solutions
Followed test plan, test passes OK
Signed-off-by: Marc VĂ©ron <veron@veron.ch>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Koha/Filter/MARC/EmbedItemsAvailability.pm [new file with mode: 0644]
t/db_dependent/Koha/Filter/EmbedItemsAvailability.t [new file with mode: 0755]

diff --git a/Koha/Filter/MARC/EmbedItemsAvailability.pm b/Koha/Filter/MARC/EmbedItemsAvailability.pm
new file mode 100644 (file)
index 0000000..db8b04a
--- /dev/null
@@ -0,0 +1,98 @@
+package Koha::Filter::MARC::EmbedItemsAvailability;
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+=head1 NAME
+
+Koha::Filter::MARC::EmbedItemsAvailability - calculates item availability and embeds
+it in a fixed MARC subfield for indexing.
+
+=head1 SYNOPSIS
+
+my $processor = Koha::RecordProcessor->new({ filters => ('EmbedItemsAvailability') });
+
+=head1 DESCRIPTION
+
+Filter to embed items not on loan count information into MARC records.
+
+=cut
+
+use Modern::Perl;
+
+use C4::Biblio qw/GetMarcFromKohaField/;
+use Koha::Items;
+
+use base qw(Koha::RecordProcessor::Base);
+our $NAME = 'EmbedItemsAvailability';
+
+=head2 filter
+
+    my $newrecord = $filter->filter($record);
+    my $newrecords = $filter->filter(\@records);
+
+Embed not on loan items count into the specified record(s) and return the result.
+
+=cut
+
+sub filter {
+    my $self = shift;
+    my $record = shift;
+    my $newrecord;
+
+    return unless defined $record;
+
+    if (ref $record eq 'ARRAY') {
+        my @recarray;
+        foreach my $thisrec (@$record) {
+            push @recarray, _processrecord($thisrec);
+        }
+        $newrecord = \@recarray;
+    } elsif (ref $record eq 'MARC::Record') {
+        $newrecord = _processrecord($record);
+    }
+
+    return $newrecord;
+}
+
+sub _processrecord {
+
+    my $record = shift;
+
+    my ($biblionumber_field, $biblionumber_subfield) = GetMarcFromKohaField("biblio.biblionumber", '');
+    my $biblionumber = $record->field($biblionumber_field)->subfield($biblionumber_subfield);
+
+    my $not_onloan_items = 0;
+    my $items = Koha::Items->search({ biblionumber => $biblionumber });
+
+    while ( my $item = $items->next ) {
+        $not_onloan_items++
+            if not $item->onloan;
+    }
+
+    # check for field 999
+    my $destination_field = $record->field('999');
+    if (defined $destination_field) {
+        # we have a field, add what we need
+        $destination_field->update( x => $not_onloan_items );
+    }
+    else {
+        # no field, create one
+        $destination_field = MARC::Field->new( '999', '', '', x => $not_onloan_items );
+        $record->append_fields([$destination_field]);
+    }
+
+    return $record;
+}
diff --git a/t/db_dependent/Koha/Filter/EmbedItemsAvailability.t b/t/db_dependent/Koha/Filter/EmbedItemsAvailability.t
new file mode 100755 (executable)
index 0000000..7110bf9
--- /dev/null
@@ -0,0 +1,133 @@
+#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Test::More tests => 1;
+use t::lib::Mocks;
+use t::lib::TestBuilder;
+
+use MARC::Record;
+
+use C4::Biblio qw/AddBiblio GetMarcBiblio/;
+use Koha::Database;
+use Koha::RecordProcessor;
+
+my $schema  = Koha::Database->schema();
+my $builder = t::lib::TestBuilder->new();
+
+subtest 'EmbedItemsAvailability tests' => sub {
+
+    plan tests => 6;
+
+    $schema->storage->txn_begin();
+
+    # MARC21 tests
+    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
+    # Create a dummy record
+    my ( $biblionumber, $biblioitemnumber ) = AddBiblio(MARC::Record->new(), '');
+
+    # Add some items with different onloan values
+    $builder->build(
+        {   source => 'Item',
+            value  => {
+                biblionumber     => $biblionumber,
+                biblioitemnumber => $biblioitemnumber,
+                onloan           => '2017-01-01'
+            }
+        }
+    );
+    $builder->build(
+        {   source => 'Item',
+            value  => {
+                biblionumber     => $biblionumber,
+                biblioitemnumber => $biblioitemnumber,
+                onloan           => undef
+            }
+        }
+    );
+    $builder->build(
+        {   source => 'Item',
+            value  => {
+                biblionumber     => $biblionumber,
+                biblioitemnumber => $biblioitemnumber,
+                onloan           => '2017-01-02'
+            }
+        }
+    );
+
+    my $processor = Koha::RecordProcessor->new( { filters => ('EmbedItemsAvailability') } );
+    is( ref($processor), 'Koha::RecordProcessor', 'Created record processor' );
+
+    my $record = GetMarcBiblio($biblionumber);
+    ok( !defined $record->field('999')->subfield('x'), q{The record doesn't originally contain 999$x} );
+    # Apply filter
+    $processor->process($record);
+    is($record->field('999')->subfield('x'), 1, 'There is only one item with undef onloan');
+
+    $schema->storage->txn_rollback();
+
+    # UNIMARC tests (999 is not created)
+    t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
+
+    $schema->storage->txn_begin();
+
+    # Create a dummy record
+    ( $biblionumber, $biblioitemnumber ) = AddBiblio(MARC::Record->new(), '');
+
+    # Add some items with different onloan values
+    $builder->build(
+        {   source => 'Item',
+            value  => {
+                biblionumber     => $biblionumber,
+                biblioitemnumber => $biblioitemnumber,
+                onloan           => '2017-01-01'
+            }
+        }
+    );
+    $builder->build(
+        {   source => 'Item',
+            value  => {
+                biblionumber     => $biblionumber,
+                biblioitemnumber => $biblioitemnumber,
+                onloan           => undef
+            }
+        }
+    );
+    $builder->build(
+        {   source => 'Item',
+            value  => {
+                biblionumber     => $biblionumber,
+                biblioitemnumber => $biblioitemnumber,
+                onloan           => '2017-01-02'
+            }
+        }
+    );
+
+    $processor = Koha::RecordProcessor->new( { filters => ('EmbedItemsAvailability') } );
+    is( ref($processor), 'Koha::RecordProcessor', 'Created record processor' );
+
+    $record = GetMarcBiblio($biblionumber);
+    ok( !defined $record->field('999')->subfield('x'), q{The record doesn't originally contain 999$x} );
+    # Apply filter
+    $processor->process($record);
+    is($record->field('999')->subfield('x'), 1, 'There is only one item with undef onloan');
+
+    $schema->storage->txn_rollback();
+};
+
+1;