Bug 18309: UNIMARC update from IFLA - authority (fr) (STU)
[koha.git] / Koha / Filter / MARC / EmbedItemsAvailability.pm
blobc3bb4a3d16cf9ac6c6684b0fb027a4420cca96ff
1 package Koha::Filter::MARC::EmbedItemsAvailability;
3 # This file is part of Koha.
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.
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.
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>.
18 =head1 NAME
20 Koha::Filter::MARC::EmbedItemsAvailability - calculates item availability and embeds
21 it in a fixed MARC subfield for indexing.
23 =head1 SYNOPSIS
25 my $processor = Koha::RecordProcessor->new({ filters => ('EmbedItemsAvailability') });
27 =head1 DESCRIPTION
29 Filter to embed items not on loan count information into MARC records.
31 =cut
33 use Modern::Perl;
35 use C4::Biblio qw/GetMarcFromKohaField/;
36 use Koha::Items;
38 use base qw(Koha::RecordProcessor::Base);
39 our $NAME = 'EmbedItemsAvailability';
41 =head2 filter
43 my $newrecord = $filter->filter($record);
44 my $newrecords = $filter->filter(\@records);
46 Embed not on loan items count into the specified record(s) and return the result.
48 =cut
50 sub filter {
51 my $self = shift;
52 my $record = shift;
53 my $newrecord;
55 return unless defined $record;
57 if (ref $record eq 'ARRAY') {
58 my @recarray;
59 foreach my $thisrec (@$record) {
60 push @recarray, _processrecord($thisrec);
62 $newrecord = \@recarray;
63 } elsif (ref $record eq 'MARC::Record') {
64 $newrecord = _processrecord($record);
67 return $newrecord;
70 sub _processrecord {
72 my $record = shift;
74 my ($biblionumber_field, $biblionumber_subfield) = GetMarcFromKohaField( "biblio.biblionumber" );
75 my $biblionumber = ( $biblionumber_field > 9 )
76 ? $record->field($biblionumber_field)->subfield($biblionumber_subfield)
77 : $record->field($biblionumber_field)->data();
79 my $not_onloan_items = Koha::Items->search({
80 biblionumber => $biblionumber,
81 onloan => undef,
82 })->count;
84 # check for field 999
85 my $destination_field = $record->field('999');
86 if (defined $destination_field) {
87 # we have a field, add what we need
88 $destination_field->update( x => $not_onloan_items );
90 else {
91 # no field, create one
92 $destination_field = MARC::Field->new( '999', '', '', x => $not_onloan_items );
93 $record->append_fields($destination_field);
96 return $record;