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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 use C4::Reserves qw(AddReserve CancelReserve GetReservesFromBiblionumber GetReservesFromBorrowernumber CanBookBeReserved CanItemBeReserved);
31 use C4::AuthoritiesMarc;
32 use C4::ILSDI::Utility;
41 C4::ILS-DI::Services - ILS-DI Services
45 Each function in this module represents an ILS-DI service.
46 They all takes a CGI instance as argument and most of them return a
47 hashref that will be printed by XML::Simple in opac/ilsdi.pl
51 use C4::ILSDI::Services;
57 $out = LookupPatron($cgi);
59 print CGI::header('text/xml');
64 xmldecl => '<?xml version="1.0" encoding="UTF-8" ?>',
65 RootName => 'LookupPatron',
72 =head2 GetAvailability
74 Given a set of biblionumbers or itemnumbers, returns a list with
75 availability of the items associated with the identifiers.
81 list of either biblionumbers or itemnumbers
83 =head3 id_type (Required)
85 defines the type of record identifier being used in the request,
91 =head3 return_type (Optional)
93 requests a particular level of detail in reporting availability,
99 =head3 return_fmt (Optional)
101 requests a particular format or set of formats in reporting
106 sub GetAvailability {
109 my $out = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
110 $out .= "<dlf:collection\n";
111 $out .= " xmlns:dlf=\"http://diglib.org/ilsdi/1.1\"\n";
112 $out .= " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
113 $out .= " xsi:schemaLocation=\"http://diglib.org/ilsdi/1.1\n";
114 $out .= " http://diglib.org/architectures/ilsdi/schemas/1.1/dlfexpanded.xsd\">\n";
116 foreach my $id ( split( / /, $cgi->param('id') ) ) {
117 if ( $cgi->param('id_type') eq "item" ) {
118 my ( $biblionumber, $status, $msg, $location ) = Availability($id);
120 $out .= " <dlf:record>\n";
121 $out .= " <dlf:bibliographic id=\"" . ( $biblionumber || $id ) . "\" />\n";
122 $out .= " <dlf:items>\n";
123 $out .= " <dlf:item id=\"" . $id . "\">\n";
124 $out .= " <dlf:simpleavailability>\n";
125 $out .= " <dlf:identifier>" . $id . "</dlf:identifier>\n";
126 $out .= " <dlf:availabilitystatus>" . $status . "</dlf:availabilitystatus>\n";
127 if ($msg) { $out .= " <dlf:availabilitymsg>" . $msg . "</dlf:availabilitymsg>\n"; }
128 if ($location) { $out .= " <dlf:location>" . $location . "</dlf:location>\n"; }
129 $out .= " </dlf:simpleavailability>\n";
130 $out .= " </dlf:item>\n";
131 $out .= " </dlf:items>\n";
132 $out .= " </dlf:record>\n";
136 my $biblioitem = ( GetBiblioItemByBiblioNumber( $id, undef ) )[0];
141 $msg = "Error: could not retrieve availability for this ID";
143 $out .= " <dlf:record>\n";
144 $out .= " <dlf:bibliographic id=\"" . $id . "\" />\n";
145 $out .= " <dlf:simpleavailability>\n";
146 $out .= " <dlf:identifier>" . $id . "</dlf:identifier>\n";
147 $out .= " <dlf:availabilitystatus>" . $status . "</dlf:availabilitystatus>\n";
148 $out .= " <dlf:availabilitymsg>" . $msg . "</dlf:availabilitymsg>\n";
149 $out .= " </dlf:simpleavailability>\n";
150 $out .= " </dlf:record>\n";
153 $out .= "</dlf:collection>\n";
160 Given a list of biblionumbers, returns a list of record objects that
161 contain bibliographic information, as well as associated holdings and item
162 information. The caller may request a specific metadata schema for the
163 record objects to be returned.
165 This function behaves similarly to HarvestBibliographicRecords and
166 HarvestExpandedRecords in Data Aggregation, but allows quick, real time
167 lookup by bibliographic identifier.
169 You can use OAI-PMH ListRecords instead of this service.
174 list of system record identifiers
176 Defines the metadata schema in which the records are returned,
185 # Check if the schema is supported. For now, GetRecords only supports MARCXML
186 if ( $cgi->param('schema') and $cgi->param('schema') ne "MARCXML" ) {
187 return { code => 'UnsupportedSchema' };
192 # Loop over biblionumbers
193 foreach my $biblionumber ( split( / /, $cgi->param('id') ) ) {
195 # Get the biblioitem from the biblionumber
196 my $biblioitem = ( GetBiblioItemByBiblioNumber( $biblionumber, undef ) )[0];
197 if ( not $biblioitem->{'biblionumber'} ) {
198 $biblioitem->{code} = "RecordNotFound";
202 my $record = GetMarcBiblio($biblionumber, $embed_items);
204 $biblioitem->{marcxml} = $record->as_xml_record();
207 # We don't want MARC to be displayed
208 delete $biblioitem->{'marc'};
210 # Get most of the needed data
211 my $biblioitemnumber = $biblioitem->{'biblioitemnumber'};
212 my @reserves = GetReservesFromBiblionumber( $biblionumber, undef, undef );
213 my $issues = GetBiblioIssues($biblionumber);
214 my $items = GetItemsByBiblioitemnumber($biblioitemnumber);
216 # We loop over the items to clean them
217 foreach my $item (@$items) {
219 # This hides additionnal XML subfields, we don't need these info
220 delete $item->{'more_subfields_xml'};
222 # Display branch names instead of branch codes
223 $item->{'homebranchname'} = GetBranchName( $item->{'homebranch'} );
224 $item->{'holdingbranchname'} = GetBranchName( $item->{'holdingbranch'} );
227 # Hashref building...
228 $biblioitem->{'items'}->{'item'} = $items;
229 $biblioitem->{'reserves'}->{'reserve'} = $reserves[1];
230 $biblioitem->{'issues'}->{'issue'} = $issues;
232 push @records, $biblioitem;
235 return { record => \@records };
238 =head2 GetAuthorityRecords
240 Given a list of authority record identifiers, returns a list of record
241 objects that contain the authority records. The function user may request
242 a specific metadata schema for the record objects.
247 list of authority record identifiers
249 specifies the metadata schema of records to be returned, possible values:
254 sub GetAuthorityRecords {
257 # If the user asks for an unsupported schema, return an error code
258 if ( $cgi->param('schema') and $cgi->param('schema') ne "MARCXML" ) {
259 return { code => 'UnsupportedSchema' };
264 # Let's loop over the authority IDs
265 foreach my $authid ( split( / /, $cgi->param('id') ) ) {
267 # Get the record as XML string, or error code
268 push @records, GetAuthorityXML($authid) || { code => 'RecordNotFound' };
271 return { record => \@records };
276 Looks up a patron in the ILS by an identifier, and returns the borrowernumber.
281 an identifier used to look up the patron in Koha
283 the type of the identifier, possible values:
294 # Get the borrower...
295 my $borrower = GetMember($cgi->param('id_type') => $cgi->param('id'));
296 if ( not $borrower->{'borrowernumber'} ) {
297 return { message => 'PatronNotFound' };
301 my $patron->{'id'} = $borrower->{'borrowernumber'};
302 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
304 # ...and return his ID
308 =head2 AuthenticatePatron
310 Authenticates a user's login credentials and returns the identifier for
315 - username (Required)
316 user's login identifier (userid or cardnumber)
317 - password (Required)
322 sub AuthenticatePatron {
324 my ($status, $cardnumber, $userid) = C4::Auth::checkpw( C4::Context->dbh, $cgi->param('username'), $cgi->param('password') );
327 my $borrower = GetMember( cardnumber => $cardnumber );
328 my $patron->{'id'} = $borrower->{'borrowernumber'};
332 return { code => 'PatronNotFound' };
338 Returns specified information about the patron, based on options in the
339 request. This function can optionally return patron's contact information,
340 fine information, hold request information, and loan information.
344 - patron_id (Required)
346 - show_contact (Optional, default 1)
347 whether or not to return patron's contact information in the response
348 - show_fines (Optional, default 0)
349 whether or not to return fine information in the response
350 - show_holds (Optional, default 0)
351 whether or not to return hold request information in the response
352 - show_loans (Optional, default 0)
353 whether or not to return loan information request information in the response
361 my $borrowernumber = $cgi->param('patron_id');
362 my $borrower = GetMemberDetails( $borrowernumber );
363 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
365 # Cleaning the borrower hashref
366 $borrower->{'charges'} = $borrower->{'flags'}->{'CHARGES'}->{'amount'};
367 $borrower->{'branchname'} = GetBranchName( $borrower->{'branchcode'} );
368 delete $borrower->{'flags'};
369 delete $borrower->{'userid'};
370 delete $borrower->{'password'};
372 # Contact fields management
373 if ( $cgi->param('show_contact') eq "0" ) {
375 # Define contact fields
376 my @contactfields = (
377 'email', 'emailpro', 'fax', 'mobile', 'phone', 'phonepro',
378 'streetnumber', 'zipcode', 'city', 'streettype', 'B_address', 'B_city',
379 'B_email', 'B_phone', 'B_zipcode', 'address', 'address2', 'altcontactaddress1',
380 'altcontactaddress2', 'altcontactaddress3', 'altcontactfirstname', 'altcontactphone', 'altcontactsurname', 'altcontactzipcode'
384 foreach my $field (@contactfields) {
385 delete $borrower->{$field};
390 if ( $cgi->param('show_fines') eq "1" ) {
392 for ( my $i = 1 ; my @charge = getcharges( $borrowernumber, undef, $i ) ; $i++ ) {
393 push( @charges, @charge );
395 $borrower->{'fines'}->{'fine'} = \@charges;
398 # Reserves management
399 if ( $cgi->param('show_holds') eq "1" ) {
401 # Get borrower's reserves
402 my @reserves = GetReservesFromBorrowernumber( $borrowernumber, undef );
403 foreach my $reserve (@reserves) {
405 # Get additional informations
406 my $item = GetBiblioFromItemNumber( $reserve->{'itemnumber'}, undef );
407 my $branchname = GetBranchName( $reserve->{'branchcode'} );
409 # Remove unwanted fields
410 delete $item->{'marc'};
411 delete $item->{'marcxml'};
412 delete $item->{'more_subfields_xml'};
414 # Add additional fields
415 $reserve->{'item'} = $item;
416 $reserve->{'branchname'} = $branchname;
417 $reserve->{'title'} = GetBiblio( $reserve->{'biblionumber'} )->{'title'};
419 $borrower->{'holds'}->{'hold'} = \@reserves;
423 if ( $cgi->param('show_loans') eq "1" ) {
424 my $issues = GetPendingIssues($borrowernumber);
425 foreach my $issue ( @$issues ){
426 $issue->{'issuedate'} = $issue->{'issuedate'}->strftime('%Y-%m-%d %H:%M');
427 $issue->{'date_due'} = $issue->{'date_due'}->strftime('%Y-%m-%d %H:%M');
429 $borrower->{'loans'}->{'loan'} = $issues;
435 =head2 GetPatronStatus
437 Returns a patron's status information.
441 - patron_id (Required)
446 sub GetPatronStatus {
450 my $borrowernumber = $cgi->param('patron_id');
451 my $borrower = GetMemberDetails( $borrowernumber );
452 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
456 type => $$borrower{categorycode},
458 expiry => $$borrower{dateexpiry},
464 Returns information about the services available on a particular item for
469 - patron_id (Required)
478 # Get the member, or return an error code if not found
479 my $borrowernumber = $cgi->param('patron_id');
480 my $borrower = GetMemberDetails( $borrowernumber );
481 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
483 # Get the item, or return an error code if not found
484 my $itemnumber = $cgi->param('item_id');
485 my $item = GetItem( $itemnumber );
486 return { code => 'RecordNotFound' } unless $$item{itemnumber};
490 # Reserve level management
491 my $biblionumber = $item->{'biblionumber'};
492 my $canbookbereserved = CanBookBeReserved( $borrower, $biblionumber );
493 if ($canbookbereserved) {
494 push @availablefor, 'title level hold';
495 my $canitembereserved = IsAvailableForItemLevelRequest($itemnumber);
496 if ($canitembereserved) {
497 push @availablefor, 'item level hold';
501 # Reserve cancellation management
502 my @reserves = GetReservesFromBorrowernumber( $borrowernumber, undef );
504 foreach my $reserve (@reserves) {
505 push @reserveditems, $reserve->{'itemnumber'};
507 if ( grep { $itemnumber eq $_ } @reserveditems ) {
508 push @availablefor, 'hold cancellation';
512 my @renewal = CanBookBeRenewed( $borrowernumber, $itemnumber );
514 push @availablefor, 'loan renewal';
518 my $barcode = $item->{'barcode'} || '';
519 $barcode = barcodedecode($barcode) if ( $barcode && C4::Context->preference('itemBarcodeInputFilter') );
521 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $borrower, $barcode );
523 # TODO push @availablefor, 'loan';
527 $out->{'AvailableFor'} = \@availablefor;
534 Extends the due date for a borrower's existing issue.
538 - patron_id (Required)
542 - desired_due_date (Required)
543 the date the patron would like the item returned by
550 # Get borrower infos or return an error code
551 my $borrowernumber = $cgi->param('patron_id');
552 my $borrower = GetMemberDetails( $borrowernumber );
553 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
555 # Get the item, or return an error code
556 my $itemnumber = $cgi->param('item_id');
557 my $item = GetItem( $itemnumber );
558 return { code => 'RecordNotFound' } unless $$item{itemnumber};
560 # Add renewal if possible
561 my @renewal = CanBookBeRenewed( $borrowernumber, $itemnumber );
562 if ( $renewal[0] ) { AddRenewal( $borrowernumber, $itemnumber ); }
564 my $issue = GetItemIssue($itemnumber);
568 $out->{'renewals'} = $issue->{'renewals'};
569 $out->{date_due} = $issue->{date_due}->strftime('%Y-%m-%d %H:%S');
570 $out->{'success'} = $renewal[0];
571 $out->{'error'} = $renewal[1];
578 Creates, for a borrower, a biblio-level hold reserve.
582 - patron_id (Required)
586 - request_location (Required)
587 IP address where the end user request is being placed
588 - pickup_location (Optional)
589 a branch code indicating the location to which to deliver the item for pickup
590 - needed_before_date (Optional)
591 date after which hold request is no longer needed
592 - pickup_expiry_date (Optional)
593 date after which item returned to shelf if item is not picked up
600 # Get the borrower or return an error code
601 my $borrowernumber = $cgi->param('patron_id');
602 my $borrower = GetMemberDetails( $borrowernumber );
603 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
605 # Get the biblio record, or return an error code
606 my $biblionumber = $cgi->param('bib_id');
607 my $biblio = GetBiblio( $biblionumber );
608 return { code => 'RecordNotFound' } unless $$biblio{biblionumber};
610 my $title = $$biblio{title};
612 # Check if the biblio can be reserved
613 return { code => 'NotHoldable' } unless CanBookBeReserved( $borrowernumber, $biblionumber );
617 # Pickup branch management
618 if ( $cgi->param('pickup_location') ) {
619 $branch = $cgi->param('pickup_location');
620 my $branches = GetBranches;
621 return { code => 'LocationNotFound' } unless $$branches{$branch};
622 } else { # if the request provide no branch, use the borrower's branch
623 $branch = $$borrower{branchcode};
627 # $branch, $borrowernumber, $biblionumber, $constraint, $bibitems, $priority, $notes, $title, $checkitem, $found
628 AddReserve( $branch, $borrowernumber, $biblionumber, 'a', undef, 0, undef, $title, undef, undef );
632 $out->{'title'} = $title;
633 $out->{'pickup_location'} = GetBranchName($branch);
635 # TODO $out->{'date_available'} = '';
642 Creates, for a borrower, an item-level hold request on a specific item of
643 a bibliographic record in Koha.
647 - patron_id (Required)
653 - pickup_location (Optional)
654 a branch code indicating the location to which to deliver the item for pickup
655 - needed_before_date (Optional)
656 date after which hold request is no longer needed
657 - pickup_expiry_date (Optional)
658 date after which item returned to shelf if item is not picked up
665 # Get the borrower or return an error code
666 my $borrowernumber = $cgi->param('patron_id');
667 my $borrower = GetMemberDetails( $borrowernumber );
668 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
670 # Get the biblio or return an error code
671 my $biblionumber = $cgi->param('bib_id');
672 my $biblio = GetBiblio($biblionumber);
673 return { code => 'RecordNotFound' } unless $$biblio{biblionumber};
675 my $title = $$biblio{title};
677 # Get the item or return an error code
678 my $itemnumber = $cgi->param('item_id');
679 my $item = GetItem( $itemnumber );
680 return { code => 'RecordNotFound' } unless $$item{itemnumber};
682 # If the biblio does not match the item, return an error code
683 return { code => 'RecordNotFound' } if $$item{biblionumber} ne $$biblio{biblionumber};
685 # Check for item disponibility
686 my $canitembereserved = C4::Reserves::CanItemBeReserved( $borrowernumber, $itemnumber );
687 my $canbookbereserved = C4::Reserves::CanBookBeReserved( $borrowernumber, $biblionumber );
688 return { code => 'NotHoldable' } unless $canbookbereserved and $canitembereserved;
692 # Pickup branch management
693 if ( $cgi->param('pickup_location') ) {
694 $branch = $cgi->param('pickup_location');
695 my $branches = GetBranches();
696 return { code => 'LocationNotFound' } unless $$branches{$branch};
697 } else { # if the request provide no branch, use the borrower's branch
698 $branch = $$borrower{branchcode};
705 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
706 if ( $item->{'holdingbranch'} eq $branch ) {
707 $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
711 # $branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$resdate,$expdate,$notes,$title,$checkitem,$found
712 AddReserve( $branch, $borrowernumber, $biblionumber, 'a', undef, $rank, '', '', '', $title, $itemnumber, $found );
716 $out->{'pickup_location'} = GetBranchName($branch);
718 # TODO $out->{'date_available'} = '';
725 Cancels an active reserve request for the borrower.
729 - patron_id (Required)
739 # Get the borrower or return an error code
740 my $borrowernumber = $cgi->param('patron_id');
741 my $borrower = GetMemberDetails( $borrowernumber );
742 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
744 # Get the item or return an error code
745 my $itemnumber = $cgi->param('item_id');
746 my $item = GetItem( $itemnumber );
747 return { code => 'RecordNotFound' } unless $$item{itemnumber};
749 # Get borrower's reserves
750 my @reserves = GetReservesFromBorrowernumber( $borrowernumber, undef );
753 # ...and loop over it to build an array of reserved itemnumbers
754 foreach my $reserve (@reserves) {
755 push @reserveditems, $reserve->{'itemnumber'};
758 # if the item was not reserved by the borrower, returns an error code
759 return { code => 'NotCanceled' } unless any { $itemnumber eq $_ } @reserveditems;
762 CancelReserve( $itemnumber, undef, $borrowernumber );
764 return { code => 'Canceled' };