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

(-)a/C4/ILSDI/Services.pm (-1 / +141 lines)
Lines 34-39 use HTML::Entities; Link Here
34
use CGI qw ( -utf8 );
34
use CGI qw ( -utf8 );
35
use DateTime;
35
use DateTime;
36
use C4::Auth;
36
use C4::Auth;
37
use MARC::Record;
38
use XML::Writer;
37
39
38
=head1 NAME
40
=head1 NAME
39
41
Lines 154-159 sub GetAvailability { Link Here
154
    return $out;
156
    return $out;
155
}
157
}
156
158
159
=head2 GetLibrisAvailability
160
161
Returns item status for items from the Swedish national
162
catalogue LIBRIS. Given a unique id stored in control-field 001,
163
item status for all copies are returned as XML.
164
165
=head3 id (Required)
166
167
The LIBRIS id of the bibliographic item.
168
This number is completely separate from ISBN or internal Koha ids.
169
It can be found in MARC control-field 001, indexed by Zebra.
170
171
=cut
172
173
sub GetLibrisAvailability {
174
    my ( $cgi ) = @_;
175
176
    # Prepare XML header and output.
177
    my $xsi_ns  = 'http://www.w3.org/2001/XMLSchema-instance';
178
    my $item_ns = 'http://appl.libris.kb.se/LIBRISItem.xsd';
179
180
    my $writer = XML::Writer->new( OUTPUT   => 'self',
181
                                   ENCODING => 'utf-8' );
182
    $writer->xmlDecl();
183
184
    my $libris_id = $cgi->param( 'id' );
185
186
    # Retrieve availability info:
187
    my ( $info, $error ) = _libris_availability( $libris_id );
188
    if ( not defined $info ) {
189
        $writer->dataElement( 'Error', $error );
190
    } else {
191
        foreach my $item (@{ $info->items }) {
192
            $writer->startTag(
193
                'Item_Information',
194
                'xmlns:xsi'             => $xsi_ns,
195
                'xsi:noNamespaceSchema' => $item_ns );
196
            $writer->dataElement( 'Item_No',                 $item->{'item_no'} );
197
            $writer->dataElement( 'Location',                $item->{'location'} );
198
            $writer->dataElement( 'Call_No',                 $item->{'call_no'} );
199
            $writer->dataElement( 'Status',                  $item->{'status'} );
200
            $writer->dataElement( 'Status_Date_Description', $item->{'status_date_desc'} );
201
            $writer->dataElement( 'Status_Date',             $item->{'status_date'} );
202
            $writer->dataElement( 'UniqueItemId',            $item->{'unique_id'} );
203
            $writer->endTag();
204
        }
205
    }
206
    return $writer->end(); # Calling 'end' returns the XML output.
207
}
208
157
=head2 GetRecords
209
=head2 GetRecords
158
210
159
Given a list of biblionumbers, returns a list of record objects that 
211
Given a list of biblionumbers, returns a list of record objects that 
Lines 781-784 sub _availability { Link Here
781
    }
833
    }
782
}
834
}
783
835
836
=head2 _libris_availability
837
838
Returns, for an LIBRIS (Swedish national catalogue) ID,
839
an array containing availability information.
840
841
my ( $result, $errmsg ) = _libris_availability;
842
$result will be undefined on error, $errmsg will be set.
843
844
=cut
845
846
sub _libris_availability
847
{
848
    my ( $libris_id ) = @_;
849
850
    return ( undef, "Missing parameter: Libris id" ) unless defined $libris_id;
851
852
    my ($error, $results, $count) = C4::Search::SimpleSearch( $libris_id, 0, 1 );
853
854
    if ($count < 1 || scalar @{ $results } < 1) {
855
        return ( undef, "No search results for Libris id $libris_id" );
856
    }
857
858
    my $xml = @{ $results }[0];
859
    my $marcflavor = C4::Context->preference( 'marcflavour' );
860
    my $record = MARC::Record->new_from_xml( $xml, 'utf8', $marcflavor );
861
    my $biblionumber = $record->subfield( '999', 'c' );
862
863
    if (defined $biblionumber) {
864
        # Possible enhancement; verify ontrol-field 003 is either 'LIBRIS' or 'SE-LIBR'.
865
        my @kohaitems = GetItemsInfo( $biblionumber );
866
867
        my ( $status, $status_date, $status_date_desc );
868
869
        my @items;
870
        foreach my $kohaitem ( @kohaitems ) {
871
            # Convert Koha status to Libris equivalent.
872
            if ( exists $kohaitem->{'notforloan'} ) {
873
                $status = 'Not for loan';
874
875
            } elsif ( exists $kohaitem->{'onloan'} ) {
876
                $status = 'On loan';
877
                if ( defined $kohaitem->{'datelastborrowed'} ) {
878
                    $status_date      = $kohaitem->{'datedue'};
879
                    $status_date_desc = 'Due on';
880
                }
881
882
            } elsif ( exists $kohaitem->{'itemlost'} ) {
883
                $status = 'Item lost';
884
                if ( defined $kohaitem->{'itemlost_on'} ) {
885
                    $status_date      = $kohaitem->{'itemlost_on'};
886
                    $status_date_desc = 'Item lost on';
887
                }
888
889
            } elsif ( exists $kohaitem->{'withdrawn'} ) {
890
                $status = 'Withdrawn';
891
                if (defined $kohaitem->{'withdrawn_om'} ) {
892
                    $status_date      = $kohaitem->{'withdrawn_on'};
893
                    $status_date_desc = 'Withdrawn on';
894
                }
895
            } elsif ( exists $kohaitem->{'damaged'} ) {
896
                $status = 'Damaged';
897
            } else {
898
                $status = 'Available';
899
            }
900
901
            # No date set yet, fall back to 'date last seen':
902
            if ( not defined $status_date and defined $kohaitem->{'datelastseen'} ) {
903
                $status_date_desc = 'Last seen on';
904
                $status_date = $kohaitem->{'datelastseen'};
905
            }
906
            my $item = {
907
                location         => $kohaitem->{'branchname'},
908
                status           => $status,
909
                status_date      => $status_date,
910
                status_date_desc => $status_date_desc,
911
                call_number      => $kohaitem->{'itemcallnumber'},
912
                item_id          => $kohaitem->{'copynumber'},
913
                unique_item_id   => $kohaitem->{'barcode'},
914
            };
915
            push( @items, $item );
916
        }
917
918
        my $result = { 'items' => @items };
919
        return ( $result, undef );
920
    }
921
922
    return ( undef, "Mo item information for $libris_id with biblionumber $biblionumber" );
923
}
924
784
1;
925
1;
785
- 

Return to bug 15292