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>.
29 use C4::Reserves qw(AddReserve GetReservesFromBiblionumber GetReservesFromBorrowernumber CanBookBeReserved CanItemBeReserved IsAvailableForItemLevelRequest);
31 use C4::AuthoritiesMarc;
37 use C4::Members::Attributes qw(GetBorrowerAttributes);
43 C4::ILS-DI::Services - ILS-DI Services
47 Each function in this module represents an ILS-DI service.
48 They all takes a CGI instance as argument and most of them return a
49 hashref that will be printed by XML::Simple in opac/ilsdi.pl
53 use C4::ILSDI::Services;
59 $out = LookupPatron($cgi);
61 print CGI::header('text/xml');
66 xmldecl => '<?xml version="1.0" encoding="UTF-8" ?>',
67 RootName => 'LookupPatron',
74 =head2 GetAvailability
76 Given a set of biblionumbers or itemnumbers, returns a list with
77 availability of the items associated with the identifiers.
83 list of either biblionumbers or itemnumbers
85 =head3 id_type (Required)
87 defines the type of record identifier being used in the request,
93 =head3 return_type (Optional)
95 requests a particular level of detail in reporting availability,
101 =head3 return_fmt (Optional)
103 requests a particular format or set of formats in reporting
108 sub GetAvailability {
111 my $out = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
112 $out .= "<dlf:collection\n";
113 $out .= " xmlns:dlf=\"http://diglib.org/ilsdi/1.1\"\n";
114 $out .= " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
115 $out .= " xsi:schemaLocation=\"http://diglib.org/ilsdi/1.1\n";
116 $out .= " http://diglib.org/architectures/ilsdi/schemas/1.1/dlfexpanded.xsd\">\n";
118 foreach my $id ( split( / /, $cgi->param('id') ) ) {
119 if ( $cgi->param('id_type') eq "item" ) {
120 my ( $biblionumber, $status, $msg, $location ) = _availability($id);
122 $out .= " <dlf:record>\n";
123 $out .= " <dlf:bibliographic id=\"" . ( $biblionumber || $id ) . "\" />\n";
124 $out .= " <dlf:items>\n";
125 $out .= " <dlf:item id=\"" . $id . "\">\n";
126 $out .= " <dlf:simpleavailability>\n";
127 $out .= " <dlf:identifier>" . $id . "</dlf:identifier>\n";
128 $out .= " <dlf:availabilitystatus>" . $status . "</dlf:availabilitystatus>\n";
129 if ($msg) { $out .= " <dlf:availabilitymsg>" . $msg . "</dlf:availabilitymsg>\n"; }
130 if ($location) { $out .= " <dlf:location>" . $location . "</dlf:location>\n"; }
131 $out .= " </dlf:simpleavailability>\n";
132 $out .= " </dlf:item>\n";
133 $out .= " </dlf:items>\n";
134 $out .= " </dlf:record>\n";
138 my $biblioitem = ( GetBiblioItemByBiblioNumber( $id, undef ) )[0];
143 $msg = "Error: could not retrieve availability for this ID";
145 $out .= " <dlf:record>\n";
146 $out .= " <dlf:bibliographic id=\"" . $id . "\" />\n";
147 $out .= " <dlf:simpleavailability>\n";
148 $out .= " <dlf:identifier>" . $id . "</dlf:identifier>\n";
149 $out .= " <dlf:availabilitystatus>" . $status . "</dlf:availabilitystatus>\n";
150 $out .= " <dlf:availabilitymsg>" . $msg . "</dlf:availabilitymsg>\n";
151 $out .= " </dlf:simpleavailability>\n";
152 $out .= " </dlf:record>\n";
155 $out .= "</dlf:collection>\n";
162 Given a list of biblionumbers, returns a list of record objects that
163 contain bibliographic information, as well as associated holdings and item
164 information. The caller may request a specific metadata schema for the
165 record objects to be returned.
167 This function behaves similarly to HarvestBibliographicRecords and
168 HarvestExpandedRecords in Data Aggregation, but allows quick, real time
169 lookup by bibliographic identifier.
171 You can use OAI-PMH ListRecords instead of this service.
176 list of system record identifiers
178 Defines the metadata schema in which the records are returned,
187 # Check if the schema is supported. For now, GetRecords only supports MARCXML
188 if ( $cgi->param('schema') and $cgi->param('schema') ne "MARCXML" ) {
189 return { code => 'UnsupportedSchema' };
194 # Loop over biblionumbers
195 foreach my $biblionumber ( split( / /, $cgi->param('id') ) ) {
197 # Get the biblioitem from the biblionumber
198 my $biblioitem = ( GetBiblioItemByBiblioNumber( $biblionumber, undef ) )[0];
199 if ( not $biblioitem->{'biblionumber'} ) {
200 $biblioitem->{code} = "RecordNotFound";
204 my $record = GetMarcBiblio($biblionumber, $embed_items);
206 $biblioitem->{marcxml} = $record->as_xml_record();
209 # We don't want MARC to be displayed
210 delete $biblioitem->{'marc'};
212 # Get most of the needed data
213 my $biblioitemnumber = $biblioitem->{'biblioitemnumber'};
214 my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber });
215 my $issues = GetBiblioIssues($biblionumber);
216 my $items = GetItemsByBiblioitemnumber($biblioitemnumber);
218 # We loop over the items to clean them
219 foreach my $item (@$items) {
221 # This hides additionnal XML subfields, we don't need these info
222 delete $item->{'more_subfields_xml'};
224 # Display branch names instead of branch codes
225 my $home_library = Koha::Libraries->find( $item->{homebranch} );
226 my $holding_library = Koha::Libraries->find( $item->{holdingbranch} );
227 $item->{'homebranchname'} = $home_library ? $home_library->branchname : '';
228 $item->{'holdingbranchname'} = $holding_library ? $holding_library->branchname : '';
231 # Hashref building...
232 $biblioitem->{'items'}->{'item'} = $items;
233 $biblioitem->{'reserves'}->{'reserve'} = $reserves;
234 $biblioitem->{'issues'}->{'issue'} = $issues;
236 push @records, $biblioitem;
239 return { record => \@records };
242 =head2 GetAuthorityRecords
244 Given a list of authority record identifiers, returns a list of record
245 objects that contain the authority records. The function user may request
246 a specific metadata schema for the record objects.
251 list of authority record identifiers
253 specifies the metadata schema of records to be returned, possible values:
258 sub GetAuthorityRecords {
261 # If the user asks for an unsupported schema, return an error code
262 if ( $cgi->param('schema') and $cgi->param('schema') ne "MARCXML" ) {
263 return { code => 'UnsupportedSchema' };
268 # Let's loop over the authority IDs
269 foreach my $authid ( split( / /, $cgi->param('id') ) ) {
271 # Get the record as XML string, or error code
272 push @records, GetAuthorityXML($authid) || { code => 'RecordNotFound' };
275 return { record => \@records };
280 Looks up a patron in the ILS by an identifier, and returns the borrowernumber.
285 an identifier used to look up the patron in Koha
287 the type of the identifier, possible values:
298 # Get the borrower...
299 my $borrower = GetMember($cgi->param('id_type') => $cgi->param('id'));
300 if ( not $borrower->{'borrowernumber'} ) {
301 return { message => 'PatronNotFound' };
305 my $patron->{'id'} = $borrower->{'borrowernumber'};
306 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
308 # ...and return his ID
312 =head2 AuthenticatePatron
314 Authenticates a user's login credentials and returns the identifier for
319 - username (Required)
320 user's login identifier (userid or cardnumber)
321 - password (Required)
326 sub AuthenticatePatron {
328 my $username = $cgi->param('username');
329 my $password = $cgi->param('password');
330 my ($status, $cardnumber, $userid) = C4::Auth::checkpw( C4::Context->dbh, $username, $password );
333 my $borrower = GetMember( cardnumber => $cardnumber );
334 my $patron->{'id'} = $borrower->{'borrowernumber'};
338 return { code => 'PatronNotFound' };
344 Returns specified information about the patron, based on options in the
345 request. This function can optionally return patron's contact information,
346 fine information, hold request information, and loan information.
350 - patron_id (Required)
352 - show_contact (Optional, default 1)
353 whether or not to return patron's contact information in the response
354 - show_fines (Optional, default 0)
355 whether or not to return fine information in the response
356 - show_holds (Optional, default 0)
357 whether or not to return hold request information in the response
358 - show_loans (Optional, default 0)
359 whether or not to return loan information request information in the response
367 my $borrowernumber = $cgi->param('patron_id');
368 my $borrower = GetMemberDetails( $borrowernumber );
369 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
371 # Cleaning the borrower hashref
372 $borrower->{'charges'} = $borrower->{'flags'}->{'CHARGES'}->{'amount'};
373 my $library = Koha::Libraries->find( $borrower->{branchcode} );
374 $borrower->{'branchname'} = $library ? $library->branchname : '';
375 delete $borrower->{'flags'};
376 delete $borrower->{'userid'};
377 delete $borrower->{'password'};
379 # Contact fields management
380 if ( defined $cgi->param('show_contact') && $cgi->param('show_contact') eq "0" ) {
382 # Define contact fields
383 my @contactfields = (
384 'email', 'emailpro', 'fax', 'mobile', 'phone', 'phonepro',
385 'streetnumber', 'zipcode', 'city', 'streettype', 'B_address', 'B_city',
386 'B_email', 'B_phone', 'B_zipcode', 'address', 'address2', 'altcontactaddress1',
387 'altcontactaddress2', 'altcontactaddress3', 'altcontactfirstname', 'altcontactphone', 'altcontactsurname', 'altcontactzipcode'
391 foreach my $field (@contactfields) {
392 delete $borrower->{$field};
397 if ( $cgi->param('show_fines') && $cgi->param('show_fines') eq "1" ) {
399 for ( my $i = 1 ; my @charge = getcharges( $borrowernumber, undef, $i ) ; $i++ ) {
400 push( @charges, @charge );
402 $borrower->{'fines'}->{'fine'} = \@charges;
405 # Reserves management
406 if ( $cgi->param('show_holds') && $cgi->param('show_holds') eq "1" ) {
408 # Get borrower's reserves
409 my @reserves = GetReservesFromBorrowernumber( $borrowernumber, undef );
410 foreach my $reserve (@reserves) {
412 # Get additional informations
413 my $item = GetBiblioFromItemNumber( $reserve->{'itemnumber'}, undef );
414 my $library = Koha::Libraries->find( $reserve->{branchcode} );
415 my $branchname = $library ? $library->branchname : '';
417 # Remove unwanted fields
418 delete $item->{'marc'};
419 delete $item->{'marcxml'};
420 delete $item->{'more_subfields_xml'};
422 # Add additional fields
423 $reserve->{'item'} = $item;
424 $reserve->{'branchname'} = $branchname;
425 $reserve->{'title'} = GetBiblio( $reserve->{'biblionumber'} )->{'title'};
427 $borrower->{'holds'}->{'hold'} = \@reserves;
431 if ( $cgi->param('show_loans') && $cgi->param('show_loans') eq "1" ) {
432 my $issues = GetPendingIssues($borrowernumber);
433 foreach my $issue ( @$issues ){
434 $issue->{'issuedate'} = $issue->{'issuedate'}->strftime('%Y-%m-%d %H:%M');
435 $issue->{'date_due'} = $issue->{'date_due'}->strftime('%Y-%m-%d %H:%M');
437 $borrower->{'loans'}->{'loan'} = $issues;
440 if ( $cgi->param('show_attributes') eq "1" ) {
441 my $attrs = GetBorrowerAttributes( $borrowernumber, 0, 1 );
442 $borrower->{'attributes'} = $attrs;
448 =head2 GetPatronStatus
450 Returns a patron's status information.
454 - patron_id (Required)
459 sub GetPatronStatus {
463 my $borrowernumber = $cgi->param('patron_id');
464 my $borrower = GetMemberDetails( $borrowernumber );
465 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
469 type => $$borrower{categorycode},
471 expiry => $$borrower{dateexpiry},
477 Returns information about the services available on a particular item for
482 - patron_id (Required)
491 # Get the member, or return an error code if not found
492 my $borrowernumber = $cgi->param('patron_id');
493 my $borrower = GetMemberDetails( $borrowernumber );
494 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
496 # Get the item, or return an error code if not found
497 my $itemnumber = $cgi->param('item_id');
498 my $item = GetItem( $itemnumber );
499 return { code => 'RecordNotFound' } unless $$item{itemnumber};
503 # Reserve level management
504 my $biblionumber = $item->{'biblionumber'};
505 my $canbookbereserved = CanBookBeReserved( $borrower, $biblionumber );
506 if ($canbookbereserved eq 'OK') {
507 push @availablefor, 'title level hold';
508 my $canitembereserved = IsAvailableForItemLevelRequest($item, $borrower);
509 if ($canitembereserved) {
510 push @availablefor, 'item level hold';
514 # Reserve cancellation management
515 my @reserves = GetReservesFromBorrowernumber( $borrowernumber, undef );
517 foreach my $reserve (@reserves) {
518 push @reserveditems, $reserve->{'itemnumber'};
520 if ( grep { $itemnumber eq $_ } @reserveditems ) {
521 push @availablefor, 'hold cancellation';
525 my @renewal = CanBookBeRenewed( $borrowernumber, $itemnumber );
527 push @availablefor, 'loan renewal';
531 my $barcode = $item->{'barcode'} || '';
532 $barcode = barcodedecode($barcode) if ( $barcode && C4::Context->preference('itemBarcodeInputFilter') );
534 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $borrower, $barcode );
536 # TODO push @availablefor, 'loan';
540 $out->{'AvailableFor'} = \@availablefor;
547 Extends the due date for a borrower's existing issue.
551 - patron_id (Required)
555 - desired_due_date (Required)
556 the date the patron would like the item returned by
563 # Get borrower infos or return an error code
564 my $borrowernumber = $cgi->param('patron_id');
565 my $borrower = GetMemberDetails( $borrowernumber );
566 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
568 # Get the item, or return an error code
569 my $itemnumber = $cgi->param('item_id');
570 my $item = GetItem( $itemnumber );
571 return { code => 'RecordNotFound' } unless $$item{itemnumber};
573 # Add renewal if possible
574 my @renewal = CanBookBeRenewed( $borrowernumber, $itemnumber );
575 if ( $renewal[0] ) { AddRenewal( $borrowernumber, $itemnumber ); }
577 my $issue = GetItemIssue($itemnumber);
581 $out->{'renewals'} = $issue->{'renewals'};
582 $out->{date_due} = $issue->{date_due}->strftime('%Y-%m-%d %H:%S');
583 $out->{'success'} = $renewal[0];
584 $out->{'error'} = $renewal[1];
591 Creates, for a borrower, a biblio-level hold reserve.
595 - patron_id (Required)
599 - request_location (Required)
600 IP address where the end user request is being placed
601 - pickup_location (Optional)
602 a branch code indicating the location to which to deliver the item for pickup
603 - needed_before_date (Optional)
604 date after which hold request is no longer needed
605 - pickup_expiry_date (Optional)
606 date after which item returned to shelf if item is not picked up
613 # Get the borrower or return an error code
614 my $borrowernumber = $cgi->param('patron_id');
615 my $borrower = GetMemberDetails( $borrowernumber );
616 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
618 # Get the biblio record, or return an error code
619 my $biblionumber = $cgi->param('bib_id');
620 my $biblio = GetBiblio( $biblionumber );
621 return { code => 'RecordNotFound' } unless $$biblio{biblionumber};
623 my $title = $$biblio{title};
625 # Check if the biblio can be reserved
626 return { code => 'NotHoldable' } unless CanBookBeReserved( $borrowernumber, $biblionumber ) eq 'OK';
630 # Pickup branch management
631 if ( $cgi->param('pickup_location') ) {
632 $branch = $cgi->param('pickup_location');
633 my $branches = GetBranches;
634 return { code => 'LocationNotFound' } unless $$branches{$branch};
635 } else { # if the request provide no branch, use the borrower's branch
636 $branch = $$borrower{branchcode};
640 # $branch, $borrowernumber, $biblionumber,
641 # $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
642 # $title, $checkitem, $found
643 my $priority= C4::Reserves::CalculatePriority( $biblionumber );
644 AddReserve( $branch, $borrowernumber, $biblionumber, undef, $priority, undef, undef, undef, $title, undef, undef );
648 $out->{'title'} = $title;
649 my $library = Koha::Libraries->find( $branch );
650 $out->{'pickup_location'} = $library ? $library->branchname : '';
652 # TODO $out->{'date_available'} = '';
659 Creates, for a borrower, an item-level hold request on a specific item of
660 a bibliographic record in Koha.
664 - patron_id (Required)
670 - pickup_location (Optional)
671 a branch code indicating the location to which to deliver the item for pickup
672 - needed_before_date (Optional)
673 date after which hold request is no longer needed
674 - pickup_expiry_date (Optional)
675 date after which item returned to shelf if item is not picked up
682 # Get the borrower or return an error code
683 my $borrowernumber = $cgi->param('patron_id');
684 my $borrower = GetMemberDetails( $borrowernumber );
685 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
687 # Get the biblio or return an error code
688 my $biblionumber = $cgi->param('bib_id');
689 my $biblio = GetBiblio($biblionumber);
690 return { code => 'RecordNotFound' } unless $$biblio{biblionumber};
692 my $title = $$biblio{title};
694 # Get the item or return an error code
695 my $itemnumber = $cgi->param('item_id');
696 my $item = GetItem( $itemnumber );
697 return { code => 'RecordNotFound' } unless $$item{itemnumber};
699 # If the biblio does not match the item, return an error code
700 return { code => 'RecordNotFound' } if $$item{biblionumber} ne $$biblio{biblionumber};
702 # Check for item disponibility
703 my $canitembereserved = C4::Reserves::CanItemBeReserved( $borrowernumber, $itemnumber );
704 my $canbookbereserved = C4::Reserves::CanBookBeReserved( $borrowernumber, $biblionumber );
705 return { code => 'NotHoldable' } unless $canbookbereserved eq 'OK' and $canitembereserved eq 'OK';
707 # Pickup branch management
709 if ( $cgi->param('pickup_location') ) {
710 $branch = $cgi->param('pickup_location');
711 my $branches = GetBranches();
712 return { code => 'LocationNotFound' } unless $$branches{$branch};
713 } else { # if the request provide no branch, use the borrower's branch
714 $branch = $$borrower{branchcode};
718 # $branch, $borrowernumber, $biblionumber,
719 # $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
720 # $title, $checkitem, $found
721 my $priority= C4::Reserves::CalculatePriority( $biblionumber );
722 AddReserve( $branch, $borrowernumber, $biblionumber, undef, $priority, undef, undef, undef, $title, $itemnumber, undef );
726 my $library = Koha::Libraries->find( $branch );
727 $out->{'pickup_location'} = $library ? $library->branchname : '';
729 # TODO $out->{'date_available'} = '';
736 Cancels an active reserve request for the borrower.
740 - patron_id (Required)
750 # Get the borrower or return an error code
751 my $borrowernumber = $cgi->param('patron_id');
752 my $borrower = GetMemberDetails( $borrowernumber );
753 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
755 # Get the reserve or return an error code
756 my $reserve_id = $cgi->param('item_id');
757 my $reserve = C4::Reserves::GetReserve($reserve_id);
758 return { code => 'RecordNotFound' } unless $reserve;
759 return { code => 'RecordNotFound' } unless ($reserve->{borrowernumber} == $borrowernumber);
761 C4::Reserves::CancelReserve({reserve_id => $reserve_id});
763 return { code => 'Canceled' };
768 Returns, for an itemnumber, an array containing availability information.
770 my ($biblionumber, $status, $msg, $location) = _availability($id);
775 my ($itemnumber) = @_;
776 my $item = GetItem( $itemnumber, undef, undef );
778 if ( not $item->{'itemnumber'} ) {
779 return ( undef, 'unknown', 'Error: could not retrieve availability for this ID', undef );
782 my $biblionumber = $item->{'biblioitemnumber'};
783 my $library = Koha::Libraries->find( $item->{holdingbranch} );
784 my $location = $library ? $library->branchname : '';
786 if ( $item->{'notforloan'} ) {
787 return ( $biblionumber, 'not available', 'Not for loan', $location );
788 } elsif ( $item->{'onloan'} ) {
789 return ( $biblionumber, 'not available', 'Checked out', $location );
790 } elsif ( $item->{'itemlost'} ) {
791 return ( $biblionumber, 'not available', 'Item lost', $location );
792 } elsif ( $item->{'withdrawn'} ) {
793 return ( $biblionumber, 'not available', 'Item withdrawn', $location );
794 } elsif ( $item->{'damaged'} ) {
795 return ( $biblionumber, 'not available', 'Item damaged', $location );
797 return ( $biblionumber, 'available', undef, $location );