Bug 10955 - Add ability to skip deletions in zebraqueue
[koha_fer] / C4 / ILSDI / Services.pm
index be093f0..bd201d3 100644 (file)
@@ -26,13 +26,14 @@ use C4::Circulation;
 use C4::Branch;
 use C4::Accounts;
 use C4::Biblio;
-use C4::Reserves;
+use C4::Reserves qw(AddReserve CancelReserve GetReservesFromBiblionumber GetReservesFromBorrowernumber CanBookBeReserved CanItemBeReserved);
 use C4::Context;
 use C4::AuthoritiesMarc;
-use C4::ILSDI::Utility;
 use XML::Simple;
 use HTML::Entities;
 use CGI;
+use DateTime;
+use C4::Auth;
 
 =head1 NAME
 
@@ -40,9 +41,9 @@ C4::ILS-DI::Services - ILS-DI Services
 
 =head1 DESCRIPTION
 
-       Each function in this module represents an ILS-DI service.
-       They all takes a CGI instance as argument and most of them return a 
-       hashref that will be printed by XML::Simple in opac/ilsdi.pl
+Each function in this module represents an ILS-DI service.
+They all takes a CGI instance as argument and most of them return a 
+hashref that will be printed by XML::Simple in opac/ilsdi.pl
 
 =head1 SYNOPSIS
 
@@ -59,41 +60,52 @@ C4::ILS-DI::Services - ILS-DI Services
                noattr => 1, 
                noescape => 1,
                nosort => 1,
-               xmldecl => '<?xml version="1.0" encoding="ISO-8859-1" ?>', 
+                xmldecl => '<?xml version="1.0" encoding="UTF-8" ?>',
                RootName => 'LookupPatron', 
                SuppressEmpty => 1);
 
 =cut
 
+=head1 FUNCTIONS
+
 =head2 GetAvailability
-    
-       Given a set of biblionumbers or itemnumbers, returns a list with 
-       availability of the items associated with the identifiers.
-       
-       Parameters :
-
-       - id (Required)
-               list of either biblionumbers or itemnumbers
-       - id_type (Required)
-               defines the type of record identifier being used in the request, 
-               possible values:
-                       - bib
-                       - item
-       - return_type (Optional)
-               requests a particular level of detail in reporting availability, 
-               possible values:
-                       - bib
-                       - item
-       - return_fmt (Optional)
-               requests a particular format or set of formats in reporting 
-               availability 
+
+Given a set of biblionumbers or itemnumbers, returns a list with 
+availability of the items associated with the identifiers.
+
+Parameters:
+
+=head3 id (Required)
+
+list of either biblionumbers or itemnumbers
+
+=head3 id_type (Required)
+
+defines the type of record identifier being used in the request, 
+possible values:
+
+  - bib
+  - item
+
+=head3 return_type (Optional)
+
+requests a particular level of detail in reporting availability, 
+possible values:
+
+  - bib
+  - item
+
+=head3 return_fmt (Optional)
+
+requests a particular format or set of formats in reporting 
+availability 
 
 =cut
 
 sub GetAvailability {
     my ($cgi) = @_;
 
-    my $out = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
+    my $out = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
     $out .= "<dlf:collection\n";
     $out .= "  xmlns:dlf=\"http://diglib.org/ilsdi/1.1\"\n";
     $out .= "  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
@@ -102,7 +114,7 @@ sub GetAvailability {
 
     foreach my $id ( split( / /, $cgi->param('id') ) ) {
         if ( $cgi->param('id_type') eq "item" ) {
-            my ( $biblionumber, $status, $msg, $location ) = Availability($id);
+            my ( $biblionumber, $status, $msg, $location ) = _availability($id);
 
             $out .= "  <dlf:record>\n";
             $out .= "    <dlf:bibliographic id=\"" . ( $biblionumber || $id ) . "\" />\n";
@@ -143,25 +155,26 @@ sub GetAvailability {
 }
 
 =head2 GetRecords
-    
-       Given a list of biblionumbers, returns a list of record objects that 
-       contain bibliographic information, as well as associated holdings and item
-       information. The caller may request a specific metadata schema for the 
-       record objects to be returned.
-       This function behaves similarly to HarvestBibliographicRecords and 
-       HarvestExpandedRecords in Data Aggregation, but allows quick, real time 
-       lookup by bibliographic identifier.
-
-       You can use OAI-PMH ListRecords instead of this service.
-       
-       Parameters:
-
-       - id (Required)
-               list of system record identifiers
-       - id_type (Optional)
-               Defines the metadata schema in which the records are returned, 
-               possible values:
-                       - MARCXML
+
+Given a list of biblionumbers, returns a list of record objects that 
+contain bibliographic information, as well as associated holdings and item
+information. The caller may request a specific metadata schema for the 
+record objects to be returned.
+
+This function behaves similarly to HarvestBibliographicRecords and 
+HarvestExpandedRecords in Data Aggregation, but allows quick, real time 
+lookup by bibliographic identifier.
+
+You can use OAI-PMH ListRecords instead of this service.
+
+Parameters:
+
+  - id (Required)
+       list of system record identifiers
+  - id_type (Optional)
+       Defines the metadata schema in which the records are returned, 
+       possible values:
+         - MARCXML
 
 =cut
 
@@ -181,18 +194,21 @@ sub GetRecords {
         # Get the biblioitem from the biblionumber
         my $biblioitem = ( GetBiblioItemByBiblioNumber( $biblionumber, undef ) )[0];
         if ( not $biblioitem->{'biblionumber'} ) {
-            $biblioitem = "RecordNotFound";
+            $biblioitem->{code} = "RecordNotFound";
+        }
+
+        my $embed_items = 1;
+        my $record = GetMarcBiblio($biblionumber, $embed_items);
+        if ($record) {
+            $biblioitem->{marcxml} = $record->as_xml_record();
         }
 
         # We don't want MARC to be displayed
         delete $biblioitem->{'marc'};
 
-        # nor the XML declaration of MARCXML
-        $biblioitem->{'marcxml'} =~ s/<\?xml version="1.0" encoding="UTF-8"\?>//go;
-
         # Get most of the needed data
         my $biblioitemnumber = $biblioitem->{'biblioitemnumber'};
-        my @reserves         = GetReservesFromBiblionumber( $biblionumber, undef, undef );
+        my $reserves         = GetReservesFromBiblionumber({ biblionumber => $biblionumber });
         my $issues           = GetBiblioIssues($biblionumber);
         my $items            = GetItemsByBiblioitemnumber($biblioitemnumber);
 
@@ -209,11 +225,9 @@ sub GetRecords {
 
         # Hashref building...
         $biblioitem->{'items'}->{'item'}       = $items;
-        $biblioitem->{'reserves'}->{'reserve'} = $reserves[1];
+        $biblioitem->{'reserves'}->{'reserve'} = $reserves;
         $biblioitem->{'issues'}->{'issue'}     = $issues;
 
-        map { $biblioitem->{$_} = encode_entities( $biblioitem->{$_}, '&' ) } grep( !/marcxml/, keys %$biblioitem );
-        
         push @records, $biblioitem;
     }
 
@@ -221,18 +235,18 @@ sub GetRecords {
 }
 
 =head2 GetAuthorityRecords
-    
-       Given a list of authority record identifiers, returns a list of record 
-       objects that contain the authority records. The function user may request 
-       a specific metadata schema for the record objects.
 
-       Parameters:
+Given a list of authority record identifiers, returns a list of record 
+objects that contain the authority records. The function user may request 
+a specific metadata schema for the record objects.
+
+Parameters:
 
-       - id (Required)
-           list of authority record identifiers
-       - schema (Optional)
-           specifies the metadata schema of records to be returned, possible values:
-                 - MARCXML
+  - id (Required)
+    list of authority record identifiers
+  - schema (Optional)
+    specifies the metadata schema of records to be returned, possible values:
+      - MARCXML
 
 =cut
 
@@ -244,34 +258,32 @@ sub GetAuthorityRecords {
         return { code => 'UnsupportedSchema' };
     }
 
-    my $records;
+    my @records;
 
     # Let's loop over the authority IDs
     foreach my $authid ( split( / /, $cgi->param('id') ) ) {
 
         # Get the record as XML string, or error code
-        my $record = GetAuthorityXML($authid) || "<record><code>RecordNotFound</code></record>";
-        $record =~ s/<\?xml(.*)\?>//go;
-        $records .= $record;
+        push @records, GetAuthorityXML($authid) || { code => 'RecordNotFound' };
     }
 
-    return $records;
+    return { record => \@records };
 }
 
 =head2 LookupPatron
-    
-       Looks up a patron in the ILS by an identifier, and returns the borrowernumber.
-       
-       Parameters:
-
-       - id (Required)
-               an identifier used to look up the patron in Koha
-       - id_type (Optional)
-               the type of the identifier, possible values:
-                       - cardnumber
-                       - firstname
-                       - userid
-                       - borrowernumber
+
+Looks up a patron in the ILS by an identifier, and returns the borrowernumber.
+
+Parameters:
+
+  - id (Required)
+       an identifier used to look up the patron in Koha
+  - id_type (Optional)
+       the type of the identifier, possible values:
+       - cardnumber
+       - firstname
+       - userid
+       - borrowernumber
 
 =cut
 
@@ -294,55 +306,51 @@ sub LookupPatron {
 
 =head2 AuthenticatePatron
 
-       Authenticates a user's login credentials and returns the identifier for 
-       the patron.
-       
-       Parameters:
+Authenticates a user's login credentials and returns the identifier for 
+the patron.
+
+Parameters:
+
+  - username (Required)
+    user's login identifier (userid or cardnumber)
+  - password (Required)
+    user's password
 
-       - username (Required)
-               user's login identifier
-       - password (Required)
-               user's password
-               
 =cut
 
 sub AuthenticatePatron {
     my ($cgi) = @_;
-
-    # Check if borrower exists, using a C4::Auth function...
-    unless( checkpw( C4::Context->dbh, $cgi->param('username'), $cgi->param('password') ) ) {
+    my ($status, $cardnumber, $userid) = C4::Auth::checkpw( C4::Context->dbh, $cgi->param('username'), $cgi->param('password') );
+    if ( $status ) {
+        # Get the borrower
+        my $borrower = GetMember( cardnumber => $cardnumber );
+        my $patron->{'id'} = $borrower->{'borrowernumber'};
+        return $patron;
+    }
+    else {
         return { code => 'PatronNotFound' };
     }
+}
 
-    # Get the borrower
-    my $borrower = GetMember( userid => $cgi->param('username') );
+=head2 GetPatronInfo
 
-    # Build the hashref
-    my $patron->{'id'} = $borrower->{'borrowernumber'};
+Returns specified information about the patron, based on options in the 
+request. This function can optionally return patron's contact information, 
+fine information, hold request information, and loan information.
 
-    # ... and return his ID
-    return $patron;
-}
+Parameters:
 
-=head2 GetPatronInfo
+  - patron_id (Required)
+       the borrowernumber
+  - show_contact (Optional, default 1)
+       whether or not to return patron's contact information in the response
+  - show_fines (Optional, default 0)
+       whether or not to return fine information in the response
+  - show_holds (Optional, default 0)
+       whether or not to return hold request information in the response
+  - show_loans (Optional, default 0)
+       whether or not to return loan information request information in the response 
 
-       Returns specified information about the patron, based on options in the 
-       request. This function can optionally return patron's contact information, 
-       fine information, hold request information, and loan information.
-       
-       Parameters:
-
-       - patron_id (Required)
-               the borrowernumber
-       - show_contact (Optional, default 1)
-               whether or not to return patron's contact information in the response
-       - show_fines (Optional, default 0)
-               whether or not to return fine information in the response
-       - show_holds (Optional, default 0)
-               whether or not to return hold request information in the response
-       - show_loans (Optional, default 0)
-               whether or not to return loan information request information in the response 
-               
 =cut
 
 sub GetPatronInfo {
@@ -405,7 +413,7 @@ sub GetPatronInfo {
             # Add additional fields
             $reserve->{'item'}       = $item;
             $reserve->{'branchname'} = $branchname;
-            $reserve->{'title'}      = ( GetBiblio( $reserve->{'biblionumber'} ) )[1]->{'title'};
+            $reserve->{'title'}      = GetBiblio( $reserve->{'biblionumber'} )->{'title'};
         }
         $borrower->{'holds'}->{'hold'} = \@reserves;
     }
@@ -413,6 +421,10 @@ sub GetPatronInfo {
     # Issues management
     if ( $cgi->param('show_loans') eq "1" ) {
         my $issues = GetPendingIssues($borrowernumber);
+        foreach my $issue ( @$issues ){
+            $issue->{'issuedate'} = $issue->{'issuedate'}->strftime('%Y-%m-%d %H:%M');
+            $issue->{'date_due'} = $issue->{'date_due'}->strftime('%Y-%m-%d %H:%M');
+        }
         $borrower->{'loans'}->{'loan'} = $issues;
     }
 
@@ -421,12 +433,12 @@ sub GetPatronInfo {
 
 =head2 GetPatronStatus
 
-       Returns a patron's status information.
-       
-       Parameters:
+Returns a patron's status information.
+
+Parameters:
 
-       - patron_id (Required)
-               the borrower ID
+  - patron_id (Required)
+       the borrower ID
 
 =cut
 
@@ -448,15 +460,15 @@ sub GetPatronStatus {
 
 =head2 GetServices
 
-       Returns information about the services available on a particular item for 
-       a particular patron.
-       
-       Parameters:
+Returns information about the services available on a particular item for 
+a particular patron.
 
-       - patron_id (Required)
-               a borrowernumber
-       - item_id (Required)
-               an itemnumber
+Parameters:
+
+  - patron_id (Required)
+       a borrowernumber
+  - item_id (Required)
+       an itemnumber
 =cut
 
 sub GetServices {
@@ -518,16 +530,16 @@ sub GetServices {
 
 =head2 RenewLoan
 
-       Extends the due date for a borrower's existing issue.
-       
-       Parameters:
+Extends the due date for a borrower's existing issue.
+
+Parameters:
 
-       - patron_id (Required)
-               a borrowernumber
-       - item_id (Required)
-               an itemnumber
-       - desired_due_date (Required)
-               the date the patron would like the item returned by 
+  - patron_id (Required)
+       a borrowernumber
+  - item_id (Required)
+       an itemnumber
+  - desired_due_date (Required)
+       the date the patron would like the item returned by 
 
 =cut
 
@@ -553,7 +565,7 @@ sub RenewLoan {
     # Hashref building
     my $out;
     $out->{'renewals'} = $issue->{'renewals'};
-    $out->{'date_due'} = $issue->{'date_due'};
+    $out->{date_due}   = $issue->{date_due}->strftime('%Y-%m-%d %H:%S');
     $out->{'success'}  = $renewal[0];
     $out->{'error'}    = $renewal[1];
 
@@ -562,22 +574,22 @@ sub RenewLoan {
 
 =head2 HoldTitle
 
-       Creates, for a borrower, a biblio-level hold reserve.
-       
-       Parameters:
-
-       - patron_id (Required)
-               a borrowernumber
-       - bib_id (Required)
-               a biblionumber
-       - request_location (Required)
-               IP address where the end user request is being placed
-       - pickup_location (Optional)
-               a branch code indicating the location to which to deliver the item for pickup
-       - needed_before_date (Optional)
-               date after which hold request is no longer needed
-       - pickup_expiry_date (Optional)
-               date after which item returned to shelf if item is not picked up 
+Creates, for a borrower, a biblio-level hold reserve.
+
+Parameters:
+
+  - patron_id (Required)
+       a borrowernumber
+  - bib_id (Required)
+       a biblionumber
+  - request_location (Required)
+       IP address where the end user request is being placed
+  - pickup_location (Optional)
+       a branch code indicating the location to which to deliver the item for pickup
+  - needed_before_date (Optional)
+       date after which hold request is no longer needed
+  - pickup_expiry_date (Optional)
+       date after which item returned to shelf if item is not picked up 
 
 =cut
 
@@ -591,7 +603,7 @@ sub HoldTitle {
 
     # Get the biblio record, or return an error code
     my $biblionumber = $cgi->param('bib_id');
-    my ( $count, $biblio ) = GetBiblio( $biblionumber );
+    my $biblio = GetBiblio( $biblionumber );
     return { code => 'RecordNotFound' } unless $$biblio{biblionumber};
     
     my $title = $$biblio{title};
@@ -611,8 +623,11 @@ sub HoldTitle {
     }
 
     # Add the reserve
-    #          $branch, $borrowernumber, $biblionumber, $constraint, $bibitems,  $priority, $notes, $title, $checkitem,  $found
-    AddReserve( $branch, $borrowernumber, $biblionumber, 'a', undef, 0, undef, $title, undef, undef );
+    #    $branch,    $borrowernumber, $biblionumber,
+    #    $constraint, $bibitems,  $priority, $resdate, $expdate, $notes,
+    #    $title,      $checkitem, $found
+    my $priority= C4::Reserves::CalculatePriority( $biblionumber );
+    AddReserve( $branch, $borrowernumber, $biblionumber, 'a', undef, $priority, undef, undef, undef, $title, undef, undef );
 
     # Hashref building
     my $out;
@@ -626,23 +641,23 @@ sub HoldTitle {
 
 =head2 HoldItem
 
-       Creates, for a borrower, an item-level hold request on a specific item of 
-       a bibliographic record in Koha.
+Creates, for a borrower, an item-level hold request on a specific item of 
+a bibliographic record in Koha.
 
-       Parameters:
+Parameters:
 
-       - patron_id (Required)
-               a borrowernumber
-       - bib_id (Required)
-               a biblionumber
-       - item_id (Required)
-               an itemnumber
-       - pickup_location (Optional)
-               a branch code indicating the location to which to deliver the item for pickup
-       - needed_before_date (Optional)
-               date after which hold request is no longer needed
-       - pickup_expiry_date (Optional)
-               date after which item returned to shelf if item is not picked up 
+  - patron_id (Required)
+       a borrowernumber
+  - bib_id (Required)
+       a biblionumber
+  - item_id (Required)
+       an itemnumber
+  - pickup_location (Optional)
+       a branch code indicating the location to which to deliver the item for pickup
+  - needed_before_date (Optional)
+       date after which hold request is no longer needed
+  - pickup_expiry_date (Optional)
+       date after which item returned to shelf if item is not picked up 
 
 =cut
 
@@ -656,7 +671,7 @@ sub HoldItem {
 
     # Get the biblio or return an error code
     my $biblionumber = $cgi->param('bib_id');
-    my ( $count, $biblio ) = GetBiblio($biblionumber);
+    my $biblio = GetBiblio($biblionumber);
     return { code => 'RecordNotFound' } unless $$biblio{biblionumber};
 
     my $title = $$biblio{title};
@@ -670,13 +685,12 @@ sub HoldItem {
     return { code => 'RecordNotFound' } if $$item{biblionumber} ne $$biblio{biblionumber};
 
     # Check for item disponibility
-    my $canitembereserved = CanItemBeReserved( $borrowernumber, $itemnumber );
-    my $canbookbereserved = CanBookBeReserved( $borrowernumber, $biblionumber );
+    my $canitembereserved = C4::Reserves::CanItemBeReserved( $borrowernumber, $itemnumber );
+    my $canbookbereserved = C4::Reserves::CanBookBeReserved( $borrowernumber, $biblionumber );
     return { code => 'NotHoldable' } unless $canbookbereserved and $canitembereserved;
 
-    my $branch;
-
     # Pickup branch management
+    my $branch;
     if ( $cgi->param('pickup_location') ) {
         $branch = $cgi->param('pickup_location');
         my $branches = GetBranches();
@@ -685,18 +699,12 @@ sub HoldItem {
         $branch = $$borrower{branchcode};
     }
 
-    my $rank;
-    my $found;
-
-    # Get rank and found
-    $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
-    if ( $item->{'holdingbranch'} eq $branch ) {
-        $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
-    }
-
     # Add the reserve
-    #          $branch, $borrowernumber, $biblionumber, $constraint, $bibitems,  $priority, $notes, $title, $checkitem,  $found
-    AddReserve( $branch, $borrowernumber, $biblionumber, 'a', undef, $rank, undef, $title, $itemnumber, $found );
+    #    $branch,    $borrowernumber, $biblionumber,
+    #    $constraint, $bibitems,  $priority, $resdate, $expdate, $notes,
+    #    $title,      $checkitem, $found
+    my $priority= C4::Reserves::CalculatePriority( $biblionumber );
+    AddReserve( $branch, $borrowernumber, $biblionumber, 'a', undef, $priority, undef, undef, undef, $title, $itemnumber, undef );
 
     # Hashref building
     my $out;
@@ -709,14 +717,14 @@ sub HoldItem {
 
 =head2 CancelHold
 
-       Cancels an active reserve request for the borrower.
-       
-       Parameters:
+Cancels an active reserve request for the borrower.
 
-       - patron_id (Required)
-               a borrowernumber
-       - item_id (Required)
-               an itemnumber 
+Parameters:
+
+  - patron_id (Required)
+       a borrowernumber
+  - item_id (Required)
+       an itemnumber 
 
 =cut
 
@@ -746,9 +754,43 @@ sub CancelHold {
     return { code => 'NotCanceled' } unless any { $itemnumber eq $_ } @reserveditems;
 
     # Cancel the reserve
-    CancelReserve( $itemnumber, undef, $borrowernumber );
+    CancelReserve({ itemnumber => $itemnumber, borrowernumber => $borrowernumber });
 
     return { code => 'Canceled' };
 }
 
+=head2 _availability
+
+Returns, for an itemnumber, an array containing availability information.
+
+ my ($biblionumber, $status, $msg, $location) = _availability($id);
+
+=cut
+
+sub _availability {
+    my ($itemnumber) = @_;
+    my $item = GetItem( $itemnumber, undef, undef );
+
+    if ( not $item->{'itemnumber'} ) {
+        return ( undef, 'unknown', 'Error: could not retrieve availability for this ID', undef );
+    }
+
+    my $biblionumber = $item->{'biblioitemnumber'};
+    my $location     = GetBranchName( $item->{'holdingbranch'} );
+
+    if ( $item->{'notforloan'} ) {
+        return ( $biblionumber, 'not available', 'Not for loan', $location );
+    } elsif ( $item->{'onloan'} ) {
+        return ( $biblionumber, 'not available', 'Checked out', $location );
+    } elsif ( $item->{'itemlost'} ) {
+        return ( $biblionumber, 'not available', 'Item lost', $location );
+    } elsif ( $item->{'wthdrawn'} ) {
+        return ( $biblionumber, 'not available', 'Item withdrawn', $location );
+    } elsif ( $item->{'damaged'} ) {
+        return ( $biblionumber, 'not available', 'Item damaged', $location );
+    } else {
+        return ( $biblionumber, 'available', undef, $location );
+    }
+}
+
 1;