From dd3da0fe0af84cc5d2298a95db67d08d761a2bcf Mon Sep 17 00:00:00 2001
From: Arthur Suzuki <arthur.suzuki@biblibre.com>
Date: Thu, 30 May 2019 01:16:55 +0200
Subject: [PATCH] Bug 23015: Implements a new ILS-DI service to get available
 pickup libraries for an item or record

test plan :
1 / Set a library A as available pickup location in the intranet
2 / query ILS-DI service with any record which has at least an item in
A or an item which can be transfered to A => should send all
informations about A

3 / Remove A from available pickup locations from the item in the intranet then repeat 2 => should
give empty reply

4 / Try with unexisting patron, unexisting item or unexisting record,
should throw an explicit error

Signed-off-by: Christophe Croullebois <christophe.croullebois@biblibre.com>
---
 C4/ILSDI/Services.pm                          | 46 +++++++++++++++++++
 .../opac-tmpl/bootstrap/en/modules/ilsdi.tt   | 37 +++++++++++++++
 opac/ilsdi.pl                                 |  3 ++
 3 files changed, 86 insertions(+)

diff --git a/C4/ILSDI/Services.pm b/C4/ILSDI/Services.pm
index 049ddcf600..09911d781f 100644
--- a/C4/ILSDI/Services.pm
+++ b/C4/ILSDI/Services.pm
@@ -614,6 +614,52 @@ sub GetServices {
     return $out;
 }
 
+=head2 GetPickupLocation
+
+Returns availables pickup locations given a particular item or record and a particular patron.
+
+Parameters:
+
+  - patron_id (Required)
+    a borrowernumber
+  - id (Required)
+    an itemnumber or recordnumber
+  - id_type (Required)
+    either item or biblio (defines the id given above).
+
+=cut
+
+sub GetPickupLocation {
+    my ($cgi) = @_;
+
+    # Get the borrower or return an error code
+    my $borrowernumber = $cgi->param('patron_id');
+    my $patron = Koha::Patrons->find( $borrowernumber );
+    return { error => 'PatronNotFound' } unless $patron;
+
+    # Get all pickup locations
+    my @pickup_locations;
+    my $id_type = $cgi->param('id_type');
+    if ( $id_type eq 'item' ) {
+    my $itemnumber = $cgi->param('id');
+        my $item = Koha::Items->find( $itemnumber );
+    return { error => 'ItemNotFound' } unless $item;
+    @pickup_locations = Koha::Libraries->pickup_locations( { item => $item } );
+    }
+
+    elsif ( $id_type eq 'bib' ) {
+    my $biblionumber = $cgi->param('id');
+        my $biblio = Koha::Biblios->find( $biblionumber );
+    return { error => 'RecordNotFound' } unless $biblio;
+    @pickup_locations = Koha::Libraries->pickup_locations( { biblio => $biblio } );
+    }
+
+    else { return { error => 'MissingParameter' }; }
+
+    # Might be nice to filter against those in which the patron can actually pickup the book.
+    return { library => \@pickup_locations };
+}
+
 =head2 RenewLoan
 
 Extends the due date for a borrower's existing issue.
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/ilsdi.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/ilsdi.tt
index 7c882d4b3b..28374734f5 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/ilsdi.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/ilsdi.tt
@@ -17,6 +17,8 @@
         ILS-DI &rsaquo; GetPatronStatus
     [% ELSIF ( GetServices ) %]
         ILS-DI &rsaquo; GetServices
+    [% ELSIF ( GetPickupLocation ) %]
+        ILS-DI &rsaquo; GetPickupLocation
     [% ELSIF ( RenewLoan ) %]
         ILS-DI &rsaquo; RenewLoan
     [% ELSIF ( HoldTitle ) %]
@@ -61,6 +63,9 @@
             [% ELSIF ( GetServices ) %]
                 <li><a href="/cgi-bin/koha/ilsdi.pl">ILS-DI</a> <span class="divider">&rsaquo;</span></li>
                 <li><a href="#">GetServices</a></li>
+            [% ELSIF ( GetPickupLocation ) %]
+                <li><a href="/cgi-bin/koha/ilsdi.pl">ILS-DI</a> <span class="divider">&rsaquo;</span></li>
+                <li><a href="#">GetPickupLocation</a></li>
             [% ELSIF ( RenewLoan ) %]
                 <li><a href="/cgi-bin/koha/ilsdi.pl">ILS-DI</a> <span class="divider">&rsaquo;</span></li>
                 <li><a href="#">RenewLoan</a></li>
@@ -674,6 +679,38 @@
   &lt;AvailableFor&gt;item level hold&lt;/AvailableFor&gt;
 &lt;/GetServices&gt;</pre>
 
+                            [% ELSIF ( GetPickupLocation ) %]
+
+                                <h2>GetPickupLocation</h2>
+                                <p>Returns available pickup locations given a  particular item or record and a particular patron.</p>
+                                <h4>Parameters</h4>
+                                <dl>
+                                    <dt><strong>patron_id</strong> (Required)</dt>
+                                    <dd>the unique patron identifier in the ILS; the same identifier returned by LookupPatron or AuthenticatePatron</dd>
+                                    <dt><strong>id</strong> (Required)</dt>
+                                    <dd>system item identifier</dd>
+                                    <dt><strong>id</strong> (Required)</dt>
+                                    <dd>system identifier type (either bib for biblio or item)</dd>
+                                </dl>
+                                <h4>Example call</h4>
+                                <a href="ilsdi.pl?service=GetPickupLocation&amp;patron_id=1&amp;id=1&amp;id_type=item">
+                                ilsdi.pl?service=GetPickupLocation&amp;patron_id=1&amp;id=1&amp;id_type=item
+                                </a>
+                                <h4>Example response</h4>
+<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
+&lt;GetPickupLocation&gt;
+&lt;pickup_locations&gt;
+              &lt;pickup_location&gt;
+                &lt;pickup_location_label&gt;First library&lt;/pickup_location_label&gt;
+                &lt;pickup_location_id&gt;1&lt;/pickup_location_id&gt;
+              &lt;/pickup_location&gt;
+              &lt;pickup_location&gt;
+                &lt;pickup_location_label&gt;Second library&lt;/pickup_location_label&gt;
+                &lt;pickup_location_id&gt;2&lt;/pickup_location_id&gt;
+              &lt;/pickup_location&gt;
+&lt;/pickup_locations&gt;
+&lt;/GetPickupLocation&gt;
+
                             [% ELSIF ( RenewLoan ) %]
 
                                 <h2>RenewLoan</h2>
diff --git a/opac/ilsdi.pl b/opac/ilsdi.pl
index 346f23e4ed..013f032e6e 100755
--- a/opac/ilsdi.pl
+++ b/opac/ilsdi.pl
@@ -67,6 +67,7 @@ my @services = (
     'GetPatronInfo',
     'GetPatronStatus',
     'GetServices',    # FIXME Loans
+    'GetPickupLocation',
     'RenewLoan',
     'HoldTitle',      # FIXME Add dates support
     'HoldItem',       # FIXME Add dates support
@@ -90,6 +91,7 @@ my %required = (
     'GetPatronInfo'       => ['patron_id'],
     'GetPatronStatus'     => ['patron_id'],
     'GetServices'         => [ 'patron_id', 'item_id' ],
+    'GetPickupLocation'   => [ 'patron_id', 'id', 'id_type' ],
     'RenewLoan'           => [ 'patron_id', 'item_id' ],
     'HoldTitle'           => [ 'patron_id', 'bib_id', 'request_location' ],
     'HoldItem'            => [ 'patron_id', 'bib_id', 'item_id' ],
@@ -107,6 +109,7 @@ my %optional = (
     'GetPatronInfo'       => [ 'show_contact', 'show_fines', 'show_holds', 'show_loans', 'loans_per_page', 'loans_page', 'show_attributes' ],
     'GetPatronStatus'     => [],
     'GetServices'         => [],
+    'GetPickupLocation'   => [],
     'RenewLoan'           => ['desired_due_date'],
     'HoldTitle'  => [ 'pickup_location', 'needed_before_date', 'pickup_expiry_date' ],
     'HoldItem'   => [ 'pickup_location', 'needed_before_date', 'pickup_expiry_date' ],
-- 
2.20.1