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);
31 use C4::AuthoritiesMarc;
32 use C4::ILSDI::Utility;
40 C4::ILS-DI::Services - ILS-DI Services
44 Each function in this module represents an ILS-DI service.
45 They all takes a CGI instance as argument and most of them return a
46 hashref that will be printed by XML::Simple in opac/ilsdi.pl
50 use C4::ILSDI::Services;
56 $out = LookupPatron($cgi);
58 print CGI::header('text/xml');
63 xmldecl => '<?xml version="1.0" encoding="ISO-8859-1" ?>',
64 RootName => 'LookupPatron',
71 =head2 GetAvailability
73 Given a set of biblionumbers or itemnumbers, returns a list with
74 availability of the items associated with the identifiers.
80 list of either biblionumbers or itemnumbers
82 =head3 id_type (Required)
84 defines the type of record identifier being used in the request,
90 =head3 return_type (Optional)
92 requests a particular level of detail in reporting availability,
98 =head3 return_fmt (Optional)
100 requests a particular format or set of formats in reporting
105 sub GetAvailability {
108 my $out = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
109 $out .= "<dlf:collection\n";
110 $out .= " xmlns:dlf=\"http://diglib.org/ilsdi/1.1\"\n";
111 $out .= " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
112 $out .= " xsi:schemaLocation=\"http://diglib.org/ilsdi/1.1\n";
113 $out .= " http://diglib.org/architectures/ilsdi/schemas/1.1/dlfexpanded.xsd\">\n";
115 foreach my $id ( split( / /, $cgi->param('id') ) ) {
116 if ( $cgi->param('id_type') eq "item" ) {
117 my ( $biblionumber, $status, $msg, $location ) = Availability($id);
119 $out .= " <dlf:record>\n";
120 $out .= " <dlf:bibliographic id=\"" . ( $biblionumber || $id ) . "\" />\n";
121 $out .= " <dlf:items>\n";
122 $out .= " <dlf:item id=\"" . $id . "\">\n";
123 $out .= " <dlf:simpleavailability>\n";
124 $out .= " <dlf:identifier>" . $id . "</dlf:identifier>\n";
125 $out .= " <dlf:availabilitystatus>" . $status . "</dlf:availabilitystatus>\n";
126 if ($msg) { $out .= " <dlf:availabilitymsg>" . $msg . "</dlf:availabilitymsg>\n"; }
127 if ($location) { $out .= " <dlf:location>" . $location . "</dlf:location>\n"; }
128 $out .= " </dlf:simpleavailability>\n";
129 $out .= " </dlf:item>\n";
130 $out .= " </dlf:items>\n";
131 $out .= " </dlf:record>\n";
135 my $biblioitem = ( GetBiblioItemByBiblioNumber( $id, undef ) )[0];
140 $msg = "Error: could not retrieve availability for this ID";
142 $out .= " <dlf:record>\n";
143 $out .= " <dlf:bibliographic id=\"" . $id . "\" />\n";
144 $out .= " <dlf:simpleavailability>\n";
145 $out .= " <dlf:identifier>" . $id . "</dlf:identifier>\n";
146 $out .= " <dlf:availabilitystatus>" . $status . "</dlf:availabilitystatus>\n";
147 $out .= " <dlf:availabilitymsg>" . $msg . "</dlf:availabilitymsg>\n";
148 $out .= " </dlf:simpleavailability>\n";
149 $out .= " </dlf:record>\n";
152 $out .= "</dlf:collection>\n";
159 Given a list of biblionumbers, returns a list of record objects that
160 contain bibliographic information, as well as associated holdings and item
161 information. The caller may request a specific metadata schema for the
162 record objects to be returned.
164 This function behaves similarly to HarvestBibliographicRecords and
165 HarvestExpandedRecords in Data Aggregation, but allows quick, real time
166 lookup by bibliographic identifier.
168 You can use OAI-PMH ListRecords instead of this service.
173 list of system record identifiers
175 Defines the metadata schema in which the records are returned,
184 # Check if the schema is supported. For now, GetRecords only supports MARCXML
185 if ( $cgi->param('schema') and $cgi->param('schema') ne "MARCXML" ) {
186 return { code => 'UnsupportedSchema' };
191 # Loop over biblionumbers
192 foreach my $biblionumber ( split( / /, $cgi->param('id') ) ) {
194 # Get the biblioitem from the biblionumber
195 my $biblioitem = ( GetBiblioItemByBiblioNumber( $biblionumber, undef ) )[0];
196 if ( not $biblioitem->{'biblionumber'} ) {
197 $biblioitem->{code} = "RecordNotFound";
200 # We don't want MARC to be displayed
201 delete $biblioitem->{'marc'};
203 # Get most of the needed data
204 my $biblioitemnumber = $biblioitem->{'biblioitemnumber'};
205 my @reserves = GetReservesFromBiblionumber( $biblionumber, undef, undef );
206 my $issues = GetBiblioIssues($biblionumber);
207 my $items = GetItemsByBiblioitemnumber($biblioitemnumber);
209 # We loop over the items to clean them
210 foreach my $item (@$items) {
212 # This hides additionnal XML subfields, we don't need these info
213 delete $item->{'more_subfields_xml'};
215 # Display branch names instead of branch codes
216 $item->{'homebranchname'} = GetBranchName( $item->{'homebranch'} );
217 $item->{'holdingbranchname'} = GetBranchName( $item->{'holdingbranch'} );
220 # Hashref building...
221 $biblioitem->{'items'}->{'item'} = $items;
222 $biblioitem->{'reserves'}->{'reserve'} = $reserves[1];
223 $biblioitem->{'issues'}->{'issue'} = $issues;
225 push @records, $biblioitem;
228 return { record => \@records };
231 =head2 GetAuthorityRecords
233 Given a list of authority record identifiers, returns a list of record
234 objects that contain the authority records. The function user may request
235 a specific metadata schema for the record objects.
240 list of authority record identifiers
242 specifies the metadata schema of records to be returned, possible values:
247 sub GetAuthorityRecords {
250 # If the user asks for an unsupported schema, return an error code
251 if ( $cgi->param('schema') and $cgi->param('schema') ne "MARCXML" ) {
252 return { code => 'UnsupportedSchema' };
257 # Let's loop over the authority IDs
258 foreach my $authid ( split( / /, $cgi->param('id') ) ) {
260 # Get the record as XML string, or error code
261 push @records, GetAuthorityXML($authid) || { code => 'RecordNotFound' };
264 return { record => \@records };
269 Looks up a patron in the ILS by an identifier, and returns the borrowernumber.
274 an identifier used to look up the patron in Koha
276 the type of the identifier, possible values:
287 # Get the borrower...
288 my $borrower = GetMember($cgi->param('id_type') => $cgi->param('id'));
289 if ( not $borrower->{'borrowernumber'} ) {
290 return { message => 'PatronNotFound' };
294 my $patron->{'id'} = $borrower->{'borrowernumber'};
295 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
297 # ...and return his ID
301 =head2 AuthenticatePatron
303 Authenticates a user's login credentials and returns the identifier for
308 - username (Required)
309 user's login identifier
310 - password (Required)
315 sub AuthenticatePatron {
318 # Check if borrower exists, using a C4::Auth function...
319 unless( C4::Auth::checkpw( C4::Context->dbh, $cgi->param('username'), $cgi->param('password') ) ) {
320 return { code => 'PatronNotFound' };
324 my $borrower = GetMember( userid => $cgi->param('username') );
327 my $patron->{'id'} = $borrower->{'borrowernumber'};
329 # ... and return his ID
335 Returns specified information about the patron, based on options in the
336 request. This function can optionally return patron's contact information,
337 fine information, hold request information, and loan information.
341 - patron_id (Required)
343 - show_contact (Optional, default 1)
344 whether or not to return patron's contact information in the response
345 - show_fines (Optional, default 0)
346 whether or not to return fine information in the response
347 - show_holds (Optional, default 0)
348 whether or not to return hold request information in the response
349 - show_loans (Optional, default 0)
350 whether or not to return loan information request information in the response
358 my $borrowernumber = $cgi->param('patron_id');
359 my $borrower = GetMemberDetails( $borrowernumber );
360 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
362 # Cleaning the borrower hashref
363 $borrower->{'charges'} = $borrower->{'flags'}->{'CHARGES'}->{'amount'};
364 $borrower->{'branchname'} = GetBranchName( $borrower->{'branchcode'} );
365 delete $borrower->{'flags'};
366 delete $borrower->{'userid'};
367 delete $borrower->{'password'};
369 # Contact fields management
370 if ( $cgi->param('show_contact') eq "0" ) {
372 # Define contact fields
373 my @contactfields = (
374 'email', 'emailpro', 'fax', 'mobile', 'phone', 'phonepro',
375 'streetnumber', 'zipcode', 'city', 'streettype', 'B_address', 'B_city',
376 'B_email', 'B_phone', 'B_zipcode', 'address', 'address2', 'altcontactaddress1',
377 'altcontactaddress2', 'altcontactaddress3', 'altcontactfirstname', 'altcontactphone', 'altcontactsurname', 'altcontactzipcode'
381 foreach my $field (@contactfields) {
382 delete $borrower->{$field};
387 if ( $cgi->param('show_fines') eq "1" ) {
389 for ( my $i = 1 ; my @charge = getcharges( $borrowernumber, undef, $i ) ; $i++ ) {
390 push( @charges, @charge );
392 $borrower->{'fines'}->{'fine'} = \@charges;
395 # Reserves management
396 if ( $cgi->param('show_holds') eq "1" ) {
398 # Get borrower's reserves
399 my @reserves = GetReservesFromBorrowernumber( $borrowernumber, undef );
400 foreach my $reserve (@reserves) {
402 # Get additional informations
403 my $item = GetBiblioFromItemNumber( $reserve->{'itemnumber'}, undef );
404 my $branchname = GetBranchName( $reserve->{'branchcode'} );
406 # Remove unwanted fields
407 delete $item->{'marc'};
408 delete $item->{'marcxml'};
409 delete $item->{'more_subfields_xml'};
411 # Add additional fields
412 $reserve->{'item'} = $item;
413 $reserve->{'branchname'} = $branchname;
414 $reserve->{'title'} = ( GetBiblio( $reserve->{'biblionumber'} ) )[1]->{'title'};
416 $borrower->{'holds'}->{'hold'} = \@reserves;
420 if ( $cgi->param('show_loans') eq "1" ) {
421 my $issues = GetPendingIssues($borrowernumber);
422 $borrower->{'loans'}->{'loan'} = $issues;
428 =head2 GetPatronStatus
430 Returns a patron's status information.
434 - patron_id (Required)
439 sub GetPatronStatus {
443 my $borrowernumber = $cgi->param('patron_id');
444 my $borrower = GetMemberDetails( $borrowernumber );
445 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
449 type => $$borrower{categorycode},
451 expiry => $$borrower{dateexpiry},
457 Returns information about the services available on a particular item for
462 - patron_id (Required)
471 # Get the member, or return an error code if not found
472 my $borrowernumber = $cgi->param('patron_id');
473 my $borrower = GetMemberDetails( $borrowernumber );
474 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
476 # Get the item, or return an error code if not found
477 my $itemnumber = $cgi->param('item_id');
478 my $item = GetItem( $itemnumber );
479 return { code => 'RecordNotFound' } unless $$item{itemnumber};
483 # Reserve level management
484 my $biblionumber = $item->{'biblionumber'};
485 my $canbookbereserved = CanBookBeReserved( $borrower, $biblionumber );
486 if ($canbookbereserved) {
487 push @availablefor, 'title level hold';
488 my $canitembereserved = IsAvailableForItemLevelRequest($itemnumber);
489 if ($canitembereserved) {
490 push @availablefor, 'item level hold';
494 # Reserve cancellation management
495 my @reserves = GetReservesFromBorrowernumber( $borrowernumber, undef );
497 foreach my $reserve (@reserves) {
498 push @reserveditems, $reserve->{'itemnumber'};
500 if ( grep { $itemnumber eq $_ } @reserveditems ) {
501 push @availablefor, 'hold cancellation';
505 my @renewal = CanBookBeRenewed( $borrowernumber, $itemnumber );
507 push @availablefor, 'loan renewal';
511 my $barcode = $item->{'barcode'} || '';
512 $barcode = barcodedecode($barcode) if ( $barcode && C4::Context->preference('itemBarcodeInputFilter') );
514 my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $borrower, $barcode );
516 # TODO push @availablefor, 'loan';
520 $out->{'AvailableFor'} = \@availablefor;
527 Extends the due date for a borrower's existing issue.
531 - patron_id (Required)
535 - desired_due_date (Required)
536 the date the patron would like the item returned by
543 # Get borrower infos or return an error code
544 my $borrowernumber = $cgi->param('patron_id');
545 my $borrower = GetMemberDetails( $borrowernumber );
546 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
548 # Get the item, or return an error code
549 my $itemnumber = $cgi->param('item_id');
550 my $item = GetItem( $itemnumber );
551 return { code => 'RecordNotFound' } unless $$item{itemnumber};
553 # Add renewal if possible
554 my @renewal = CanBookBeRenewed( $borrowernumber, $itemnumber );
555 if ( $renewal[0] ) { AddRenewal( $borrowernumber, $itemnumber ); }
557 my $issue = GetItemIssue($itemnumber);
561 $out->{'renewals'} = $issue->{'renewals'};
562 $out->{date_due} = $issue->{date_due}->strftime('%Y-%m-%d %H:%S');
563 $out->{'success'} = $renewal[0];
564 $out->{'error'} = $renewal[1];
571 Creates, for a borrower, a biblio-level hold reserve.
575 - patron_id (Required)
579 - request_location (Required)
580 IP address where the end user request is being placed
581 - pickup_location (Optional)
582 a branch code indicating the location to which to deliver the item for pickup
583 - needed_before_date (Optional)
584 date after which hold request is no longer needed
585 - pickup_expiry_date (Optional)
586 date after which item returned to shelf if item is not picked up
593 # Get the borrower or return an error code
594 my $borrowernumber = $cgi->param('patron_id');
595 my $borrower = GetMemberDetails( $borrowernumber );
596 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
598 # Get the biblio record, or return an error code
599 my $biblionumber = $cgi->param('bib_id');
600 my ( $count, $biblio ) = GetBiblio( $biblionumber );
601 return { code => 'RecordNotFound' } unless $$biblio{biblionumber};
603 my $title = $$biblio{title};
605 # Check if the biblio can be reserved
606 return { code => 'NotHoldable' } unless CanBookBeReserved( $borrowernumber, $biblionumber );
610 # Pickup branch management
611 if ( $cgi->param('pickup_location') ) {
612 $branch = $cgi->param('pickup_location');
613 my $branches = GetBranches;
614 return { code => 'LocationNotFound' } unless $$branches{$branch};
615 } else { # if the request provide no branch, use the borrower's branch
616 $branch = $$borrower{branchcode};
620 # $branch, $borrowernumber, $biblionumber, $constraint, $bibitems, $priority, $notes, $title, $checkitem, $found
621 AddReserve( $branch, $borrowernumber, $biblionumber, 'a', undef, 0, undef, $title, undef, undef );
625 $out->{'title'} = $title;
626 $out->{'pickup_location'} = GetBranchName($branch);
628 # TODO $out->{'date_available'} = '';
635 Creates, for a borrower, an item-level hold request on a specific item of
636 a bibliographic record in Koha.
640 - patron_id (Required)
646 - pickup_location (Optional)
647 a branch code indicating the location to which to deliver the item for pickup
648 - needed_before_date (Optional)
649 date after which hold request is no longer needed
650 - pickup_expiry_date (Optional)
651 date after which item returned to shelf if item is not picked up
658 # Get the borrower or return an error code
659 my $borrowernumber = $cgi->param('patron_id');
660 my $borrower = GetMemberDetails( $borrowernumber );
661 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
663 # Get the biblio or return an error code
664 my $biblionumber = $cgi->param('bib_id');
665 my ( $count, $biblio ) = GetBiblio($biblionumber);
666 return { code => 'RecordNotFound' } unless $$biblio{biblionumber};
668 my $title = $$biblio{title};
670 # Get the item or return an error code
671 my $itemnumber = $cgi->param('item_id');
672 my $item = GetItem( $itemnumber );
673 return { code => 'RecordNotFound' } unless $$item{itemnumber};
675 # If the biblio does not match the item, return an error code
676 return { code => 'RecordNotFound' } if $$item{biblionumber} ne $$biblio{biblionumber};
678 # Check for item disponibility
679 my $canitembereserved = CanItemBeReserved( $borrowernumber, $itemnumber );
680 my $canbookbereserved = CanBookBeReserved( $borrowernumber, $biblionumber );
681 return { code => 'NotHoldable' } unless $canbookbereserved and $canitembereserved;
685 # Pickup branch management
686 if ( $cgi->param('pickup_location') ) {
687 $branch = $cgi->param('pickup_location');
688 my $branches = GetBranches();
689 return { code => 'LocationNotFound' } unless $$branches{$branch};
690 } else { # if the request provide no branch, use the borrower's branch
691 $branch = $$borrower{branchcode};
698 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
699 if ( $item->{'holdingbranch'} eq $branch ) {
700 $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
704 # $branch, $borrowernumber, $biblionumber, $constraint, $bibitems, $priority, $notes, $title, $checkitem, $found
705 AddReserve( $branch, $borrowernumber, $biblionumber, 'a', undef, $rank, undef, $title, $itemnumber, $found );
709 $out->{'pickup_location'} = GetBranchName($branch);
711 # TODO $out->{'date_available'} = '';
718 Cancels an active reserve request for the borrower.
722 - patron_id (Required)
732 # Get the borrower or return an error code
733 my $borrowernumber = $cgi->param('patron_id');
734 my $borrower = GetMemberDetails( $borrowernumber );
735 return { code => 'PatronNotFound' } unless $$borrower{borrowernumber};
737 # Get the item or return an error code
738 my $itemnumber = $cgi->param('item_id');
739 my $item = GetItem( $itemnumber );
740 return { code => 'RecordNotFound' } unless $$item{itemnumber};
742 # Get borrower's reserves
743 my @reserves = GetReservesFromBorrowernumber( $borrowernumber, undef );
746 # ...and loop over it to build an array of reserved itemnumbers
747 foreach my $reserve (@reserves) {
748 push @reserveditems, $reserve->{'itemnumber'};
751 # if the item was not reserved by the borrower, returns an error code
752 return { code => 'NotCanceled' } unless any { $itemnumber eq $_ } @reserveditems;
755 CancelReserve( $itemnumber, undef, $borrowernumber );
757 return { code => 'Canceled' };