1 package C4::ILSDI::Services;
3 # Copyright 2009 SARL Biblibre
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
28 use C4::Reserves qw(AddReserve CanBookBeReserved CanItemBeReserved IsAvailableForItemLevelRequest);
30 use C4::AuthoritiesMarc;
36 use C4::Members::Attributes qw(GetBorrowerAttributes);
44 C4::ILS-DI::Services - ILS-DI Services
48 Each function in this module represents an ILS-DI service.
49 They all takes a CGI instance as argument and most of them return a
50 hashref that will be printed by XML::Simple in opac/ilsdi.pl
54 use C4::ILSDI::Services;
60 $out = LookupPatron($cgi);
62 print CGI::header('text/xml');
67 xmldecl => '<?xml version="1.0" encoding="UTF-8" ?>',
68 RootName => 'LookupPatron',
75 =head2 GetAvailability
77 Given a set of biblionumbers or itemnumbers, returns a list with
78 availability of the items associated with the identifiers.
84 list of either biblionumbers or itemnumbers
86 =head3 id_type (Required)
88 defines the type of record identifier being used in the request,
94 =head3 return_type (Optional)
96 requests a particular level of detail in reporting availability,
102 =head3 return_fmt (Optional)
104 requests a particular format or set of formats in reporting
109 sub GetAvailability {
112 my $out = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
113 $out .= "<dlf:collection\n";
114 $out .= " xmlns:dlf=\"http://diglib.org/ilsdi/1.1\"\n";
115 $out .= " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
116 $out .= " xsi:schemaLocation=\"http://diglib.org/ilsdi/1.1\n";
117 $out .= " http://diglib.org/architectures/ilsdi/schemas/1.1/dlfexpanded.xsd\">\n";
119 foreach my $id ( split( / /, $cgi->param('id') ) ) {
120 if ( $cgi->param('id_type') eq "item" ) {
121 my ( $biblionumber, $status, $msg, $location ) = _availability($id);
123 $out .= " <dlf:record>\n";
124 $out .= " <dlf:bibliographic id=\"" . ( $biblionumber || $id ) . "\" />\n";
125 $out .= " <dlf:items>\n";
126 $out .= " <dlf:item id=\"" . $id . "\">\n";
127 $out .= " <dlf:simpleavailability>\n";
128 $out .= " <dlf:identifier>" . $id . "</dlf:identifier>\n";
129 $out .= " <dlf:availabilitystatus>" . $status . "</dlf:availabilitystatus>\n";
130 if ($msg) { $out .= " <dlf:availabilitymsg>" . $msg . "</dlf:availabilitymsg>\n"; }
131 if ($location) { $out .= " <dlf:location>" . $location . "</dlf:location>\n"; }
132 $out .= " </dlf:simpleavailability>\n";
133 $out .= " </dlf:item>\n";
134 $out .= " </dlf:items>\n";
135 $out .= " </dlf:record>\n";
139 my $items = GetItemnumbersForBiblio($id);
142 $out .= " <dlf:record>\n";
143 $out .= " <dlf:bibliographic id=\"" .$id. "\" />\n";
144 $out .= " <dlf:items>\n";
145 # We loop over the items to clean them
146 foreach my $itemnumber (@$items) {
147 my ( $biblionumber, $status, $msg, $location ) = _availability($itemnumber);
148 $out .= " <dlf:item id=\"" . $itemnumber . "\">\n";
149 $out .= " <dlf:simpleavailability>\n";
150 $out .= " <dlf:identifier>" . $itemnumber . "</dlf:identifier>\n";
151 $out .= " <dlf:availabilitystatus>" . $status . "</dlf:availabilitystatus>\n";
152 if ($msg) { $out .= " <dlf:availabilitymsg>" . $msg . "</dlf:availabilitymsg>\n"; }
153 if ($location) { $out .= " <dlf:location>" . $location . "</dlf:location>\n"; }
154 $out .= " </dlf:simpleavailability>\n";
155 $out .= " </dlf:item>\n";
158 $out .= " </dlf:items>\n";
159 $out .= " </dlf:record>\n";
162 $msg = "Error: could not retrieve availability for this ID";
166 $out .= "</dlf:collection>\n";
173 Given a list of biblionumbers, returns a list of record objects that
174 contain bibliographic information, as well as associated holdings and item
175 information. The caller may request a specific metadata schema for the
176 record objects to be returned.
178 This function behaves similarly to HarvestBibliographicRecords and
179 HarvestExpandedRecords in Data Aggregation, but allows quick, real time
180 lookup by bibliographic identifier.
182 You can use OAI-PMH ListRecords instead of this service.
187 list of system record identifiers
189 Defines the metadata schema in which the records are returned,
198 # Check if the schema is supported. For now, GetRecords only supports MARCXML
199 if ( $cgi->param('schema') and $cgi->param('schema') ne "MARCXML" ) {
200 return { code => 'UnsupportedSchema' };
205 # Loop over biblionumbers
206 foreach my $biblionumber ( split( / /, $cgi->param('id') ) ) {
208 # Get the biblioitem from the biblionumber
209 my $biblioitem = ( GetBiblioItemByBiblioNumber( $biblionumber, undef ) )[0];
210 if ( not $biblioitem->{'biblionumber'} ) {
211 $biblioitem->{code} = "RecordNotFound";
215 my $record = GetMarcBiblio($biblionumber, $embed_items);
217 $biblioitem->{marcxml} = $record->as_xml_record();
220 # Get most of the needed data
221 my $biblioitemnumber = $biblioitem->{'biblioitemnumber'};
222 my $biblio = Koha::Biblios->find( $biblionumber );
223 my $holds = $biblio->current_holds->unblessed;
224 my $issues = GetBiblioIssues($biblionumber);
225 my $items = GetItemsByBiblioitemnumber($biblioitemnumber);
227 # We loop over the items to clean them
228 foreach my $item (@$items) {
230 # This hides additionnal XML subfields, we don't need these info
231 delete $item->{'more_subfields_xml'};
233 # Display branch names instead of branch codes
234 my $home_library = Koha::Libraries->find( $item->{homebranch} );
235 my $holding_library = Koha::Libraries->find( $item->{holdingbranch} );
236 $item->{'homebranchname'} = $home_library ? $home_library->branchname : '';
237 $item->{'holdingbranchname'} = $holding_library ? $holding_library->branchname : '';
240 # Hashref building...
241 $biblioitem->{'items'}->{'item'} = $items;
242 $biblioitem->{'reserves'}->{'reserve'} = $holds;
243 $biblioitem->{'issues'}->{'issue'} = $issues;
245 push @records, $biblioitem;
248 return { record => \@records };
251 =head2 GetAuthorityRecords
253 Given a list of authority record identifiers, returns a list of record
254 objects that contain the authority records. The function user may request
255 a specific metadata schema for the record objects.
260 list of authority record identifiers
262 specifies the metadata schema of records to be returned, possible values:
267 sub GetAuthorityRecords {
270 # If the user asks for an unsupported schema, return an error code
271 if ( $cgi->param('schema') and $cgi->param('schema') ne "MARCXML" ) {
272 return { code => 'UnsupportedSchema' };
277 # Let's loop over the authority IDs
278 foreach my $authid ( split( / /, $cgi->param('id') ) ) {
280 # Get the record as XML string, or error code
281 push @records, GetAuthorityXML($authid) || { code => 'RecordNotFound' };
284 return { record => \@records };
289 Looks up a patron in the ILS by an identifier, and returns the borrowernumber.
294 an identifier used to look up the patron in Koha
296 the type of the identifier, possible values:
307 # Get the borrower...
308 my $borrower = GetMember($cgi->param('id_type') => $cgi->param('id'));
309 if ( not $borrower->{'borrowernumber'} ) {
310 return { message => 'PatronNotFound' };
314 my $patron->{'id'} = $borrower->{'borrowernumber'};
315 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
317 # ...and return his ID
321 =head2 AuthenticatePatron
323 Authenticates a user's login credentials and returns the identifier for
328 - username (Required)
329 user's login identifier (userid or cardnumber)
330 - password (Required)
335 sub AuthenticatePatron {
337 my $username = $cgi->param('username');
338 my $password = $cgi->param('password');
339 my ($status, $cardnumber, $userid) = C4::Auth::checkpw( C4::Context->dbh, $username, $password );
342 my $borrower = GetMember( cardnumber => $cardnumber );
343 my $patron->{'id'} = $borrower->{'borrowernumber'};
347 return { code => 'PatronNotFound' };
353 Returns specified information about the patron, based on options in the
354 request. This function can optionally return patron's contact information,
355 fine information, hold request information, and loan information.
359 - patron_id (Required)
361 - show_contact (Optional, default 1)
362 whether or not to return patron's contact information in the response
363 - show_fines (Optional, default 0)
364 whether or not to return fine information in the response
365 - show_holds (Optional, default 0)
366 whether or not to return hold request information in the response
367 - show_loans (Optional, default 0)
368 whether or not to return loan information request information in the response
376 my $borrowernumber = $cgi->param('patron_id');
377 my $borrower = GetMember( borrowernumber => $borrowernumber );
378 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
379 my $patron = Koha::Patrons->find( $borrowernumber );
381 # Cleaning the borrower hashref
382 my $flags = C4::Members::patronflags( $borrower );
383 $borrower->{'charges'} = $flags->{'CHARGES'}->{'amount'};
384 my $library = Koha::Libraries->find( $borrower->{branchcode} );
385 $borrower->{'branchname'} = $library ? $library->branchname : '';
386 delete $borrower->{'userid'};
387 delete $borrower->{'password'};
389 # Contact fields management
390 if ( defined $cgi->param('show_contact') && $cgi->param('show_contact') eq "0" ) {
392 # Define contact fields
393 my @contactfields = (
394 'email', 'emailpro', 'fax', 'mobile', 'phone', 'phonepro',
395 'streetnumber', 'zipcode', 'city', 'streettype', 'B_address', 'B_city',
396 'B_email', 'B_phone', 'B_zipcode', 'address', 'address2', 'altcontactaddress1',
397 'altcontactaddress2', 'altcontactaddress3', 'altcontactfirstname', 'altcontactphone', 'altcontactsurname', 'altcontactzipcode'
401 foreach my $field (@contactfields) {
402 delete $borrower->{$field};
407 if ( $cgi->param('show_fines') && $cgi->param('show_fines') eq "1" ) {
409 for ( my $i = 1 ; my @charge = getcharges( $borrowernumber, undef, $i ) ; $i++ ) {
410 push( @charges, @charge );
412 $borrower->{'fines'}->{'fine'} = \@charges;
415 # Reserves management
416 if ( $cgi->param('show_holds') && $cgi->param('show_holds') eq "1" ) {
418 # Get borrower's reserves
419 my $holds = $patron->holds;
420 while ( my $hold = $holds->next ) {
422 my $unblessed_hold = $hold->unblessed;
423 # Get additional informations
424 my $item = GetBiblioFromItemNumber( $hold->itemnumber, undef );
425 my $library = Koha::Libraries->find( $hold->branchcode ); # Should $hold->get_library
426 my $branchname = $library ? $library->branchname : '';
428 # Remove unwanted fields
429 delete $item->{'more_subfields_xml'};
431 # Add additional fields
432 $unblessed_hold->{item} = $item;
433 $unblessed_hold->{branchname} = $branchname;
434 $unblessed_hold->{title} = GetBiblio( $hold->biblionumber )->{'title'}; # Should be $hold->get_biblio
436 push @{ $borrower->{holds}{hold} }, $unblessed_hold;
441 if ( $cgi->param('show_loans') && $cgi->param('show_loans') eq "1" ) {
442 my $issues = GetPendingIssues($borrowernumber);
443 foreach my $issue ( @$issues ){
444 $issue->{'issuedate'} = $issue->{'issuedate'}->strftime('%Y-%m-%d %H:%M');
445 $issue->{'date_due'} = $issue->{'date_due'}->strftime('%Y-%m-%d %H:%M');
447 $borrower->{'loans'}->{'loan'} = $issues;
450 if ( $cgi->param('show_attributes') eq "1" ) {
451 my $attrs = GetBorrowerAttributes( $borrowernumber, 0, 1 );
452 $borrower->{'attributes'} = $attrs;
458 =head2 GetPatronStatus
460 Returns a patron's status information.
464 - patron_id (Required)
469 sub GetPatronStatus {
473 my $borrowernumber = $cgi->param('patron_id');
474 my $borrower = GetMember( borrowernumber => $borrowernumber );
475 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
479 type => $$borrower{categorycode},
481 expiry => $$borrower{dateexpiry},
487 Returns information about the services available on a particular item for
492 - patron_id (Required)
502 # Get the member, or return an error code if not found
503 my $borrowernumber = $cgi->param('patron_id');
504 my $borrower = GetMember( borrowernumber => $borrowernumber );
505 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
507 my $patron = Koha::Patrons->find( $borrowernumber );
509 # Get the item, or return an error code if not found
510 my $itemnumber = $cgi->param('item_id');
511 my $item = GetItem( $itemnumber );
512 return { code => 'RecordNotFound' } unless $$item{itemnumber};
516 # Reserve level management
517 my $biblionumber = $item->{'biblionumber'};
518 my $canbookbereserved = CanBookBeReserved( $borrower, $biblionumber );
519 if ($canbookbereserved eq 'OK') {
520 push @availablefor, 'title level hold';
521 my $canitembereserved = IsAvailableForItemLevelRequest($item, $borrower);
522 if ($canitembereserved) {
523 push @availablefor, 'item level hold';
527 # Reserve cancellation management
528 my $holds = $patron->holds;
530 while ( my $hold = $holds->next ) { # FIXME This could be improved
531 push @reserveditems, $hold->itemnumber;
533 if ( grep { $itemnumber eq $_ } @reserveditems ) {
534 push @availablefor, 'hold cancellation';
538 my @renewal = CanBookBeRenewed( $borrowernumber, $itemnumber );
540 push @availablefor, 'loan renewal';
544 my $barcode = $item->{'barcode'} || '';
545 $barcode = barcodedecode($barcode) if ( $barcode && C4::Context->preference('itemBarcodeInputFilter') );
547 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $borrower, $barcode );
549 # TODO push @availablefor, 'loan';
553 $out->{'AvailableFor'} = \@availablefor;
560 Extends the due date for a borrower's existing issue.
564 - patron_id (Required)
568 - desired_due_date (Required)
569 the date the patron would like the item returned by
576 # Get borrower infos or return an error code
577 my $borrowernumber = $cgi->param('patron_id');
578 my $borrower = GetMember( borrowernumber => $borrowernumber );
579 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
581 # Get the item, or return an error code
582 my $itemnumber = $cgi->param('item_id');
583 my $item = GetItem( $itemnumber );
584 return { code => 'RecordNotFound' } unless $$item{itemnumber};
586 # Add renewal if possible
587 my @renewal = CanBookBeRenewed( $borrowernumber, $itemnumber );
588 if ( $renewal[0] ) { AddRenewal( $borrowernumber, $itemnumber ); }
590 my $issue = GetItemIssue($itemnumber);
594 $out->{'renewals'} = $issue->{'renewals'};
595 $out->{date_due} = $issue->{date_due}->strftime('%Y-%m-%d %H:%S');
596 $out->{'success'} = $renewal[0];
597 $out->{'error'} = $renewal[1];
604 Creates, for a borrower, a biblio-level hold reserve.
608 - patron_id (Required)
612 - request_location (Required)
613 IP address where the end user request is being placed
614 - pickup_location (Optional)
615 a branch code indicating the location to which to deliver the item for pickup
616 - needed_before_date (Optional)
617 date after which hold request is no longer needed
618 - pickup_expiry_date (Optional)
619 date after which item returned to shelf if item is not picked up
626 # Get the borrower or return an error code
627 my $borrowernumber = $cgi->param('patron_id');
628 my $borrower = GetMember( borrowernumber => $borrowernumber );
629 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
631 # Get the biblio record, or return an error code
632 my $biblionumber = $cgi->param('bib_id');
633 my $biblio = GetBiblio( $biblionumber );
634 return { code => 'RecordNotFound' } unless $$biblio{biblionumber};
636 my $title = $$biblio{title};
638 # Check if the biblio can be reserved
639 return { code => 'NotHoldable' } unless CanBookBeReserved( $borrowernumber, $biblionumber ) eq 'OK';
643 # Pickup branch management
644 if ( $cgi->param('pickup_location') ) {
645 $branch = $cgi->param('pickup_location');
646 return { code => 'LocationNotFound' } unless Koha::Libraries->find($branch);
647 } else { # if the request provide no branch, use the borrower's branch
648 $branch = $$borrower{branchcode};
652 # $branch, $borrowernumber, $biblionumber,
653 # $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
654 # $title, $checkitem, $found
655 my $priority= C4::Reserves::CalculatePriority( $biblionumber );
656 AddReserve( $branch, $borrowernumber, $biblionumber, undef, $priority, undef, undef, undef, $title, undef, undef );
660 $out->{'title'} = $title;
661 my $library = Koha::Libraries->find( $branch );
662 $out->{'pickup_location'} = $library ? $library->branchname : '';
664 # TODO $out->{'date_available'} = '';
671 Creates, for a borrower, an item-level hold request on a specific item of
672 a bibliographic record in Koha.
676 - patron_id (Required)
682 - pickup_location (Optional)
683 a branch code indicating the location to which to deliver the item for pickup
684 - needed_before_date (Optional)
685 date after which hold request is no longer needed
686 - pickup_expiry_date (Optional)
687 date after which item returned to shelf if item is not picked up
694 # Get the borrower or return an error code
695 my $borrowernumber = $cgi->param('patron_id');
696 my $borrower = GetMember( borrowernumber => $borrowernumber );
697 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
699 # Get the biblio or return an error code
700 my $biblionumber = $cgi->param('bib_id');
701 my $biblio = GetBiblio($biblionumber);
702 return { code => 'RecordNotFound' } unless $$biblio{biblionumber};
704 my $title = $$biblio{title};
706 # Get the item or return an error code
707 my $itemnumber = $cgi->param('item_id');
708 my $item = GetItem( $itemnumber );
709 return { code => 'RecordNotFound' } unless $$item{itemnumber};
711 # If the biblio does not match the item, return an error code
712 return { code => 'RecordNotFound' } if $$item{biblionumber} ne $$biblio{biblionumber};
714 # Check for item disponibility
715 my $canitembereserved = C4::Reserves::CanItemBeReserved( $borrowernumber, $itemnumber );
716 my $canbookbereserved = C4::Reserves::CanBookBeReserved( $borrowernumber, $biblionumber );
717 return { code => 'NotHoldable' } unless $canbookbereserved eq 'OK' and $canitembereserved eq 'OK';
719 # Pickup branch management
721 if ( $cgi->param('pickup_location') ) {
722 $branch = $cgi->param('pickup_location');
723 return { code => 'LocationNotFound' } unless Koha::Libraries->find($branch);
724 } else { # if the request provide no branch, use the borrower's branch
725 $branch = $$borrower{branchcode};
729 # $branch, $borrowernumber, $biblionumber,
730 # $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
731 # $title, $checkitem, $found
732 my $priority= C4::Reserves::CalculatePriority( $biblionumber );
733 AddReserve( $branch, $borrowernumber, $biblionumber, undef, $priority, undef, undef, undef, $title, $itemnumber, undef );
737 my $library = Koha::Libraries->find( $branch );
738 $out->{'pickup_location'} = $library ? $library->branchname : '';
740 # TODO $out->{'date_available'} = '';
747 Cancels an active reserve request for the borrower.
751 - patron_id (Required)
761 # Get the borrower or return an error code
762 my $borrowernumber = $cgi->param('patron_id');
763 my $borrower = GetMember( borrowernumber => $borrowernumber );
764 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
766 # Get the reserve or return an error code
767 my $reserve_id = $cgi->param('item_id');
768 my $reserve = C4::Reserves::GetReserve($reserve_id);
769 return { code => 'RecordNotFound' } unless $reserve;
770 return { code => 'RecordNotFound' } unless ($reserve->{borrowernumber} == $borrowernumber);
772 C4::Reserves::CancelReserve({reserve_id => $reserve_id});
774 return { code => 'Canceled' };
779 Returns, for an itemnumber, an array containing availability information.
781 my ($biblionumber, $status, $msg, $location) = _availability($id);
786 my ($itemnumber) = @_;
787 my $item = GetItem( $itemnumber, undef, undef );
789 if ( not $item->{'itemnumber'} ) {
790 return ( undef, 'unknown', 'Error: could not retrieve availability for this ID', undef );
793 my $biblionumber = $item->{'biblioitemnumber'};
794 my $library = Koha::Libraries->find( $item->{holdingbranch} );
795 my $location = $library ? $library->branchname : '';
797 if ( $item->{'notforloan'} ) {
798 return ( $biblionumber, 'not available', 'Not for loan', $location );
799 } elsif ( $item->{'onloan'} ) {
800 return ( $biblionumber, 'not available', 'Checked out', $location );
801 } elsif ( $item->{'itemlost'} ) {
802 return ( $biblionumber, 'not available', 'Item lost', $location );
803 } elsif ( $item->{'withdrawn'} ) {
804 return ( $biblionumber, 'not available', 'Item withdrawn', $location );
805 } elsif ( $item->{'damaged'} ) {
806 return ( $biblionumber, 'not available', 'Item damaged', $location );
808 return ( $biblionumber, 'available', undef, $location );