From 0ce8a224b380eb718aac758f28f09fbb11325815 Mon Sep 17 00:00:00 2001
From: Robert Lange <robert.lange@uni-leipzig.de>
Date: Thu, 4 Nov 2021 10:20:20 +0100
Subject: [PATCH] refs #20842 [finc] refactor getPickUpLocations

* move getWachtlPickUpLocations from de_zwi2, de_d13 to LiberoWachtlTrait for PAIA usage to fetch pickUpLocations
* move getPickUpLocations from de_105, de_l152, de_15, de_ch1 to FincLibero for DAIA usage to fetch pickUpLocations
** check for config var "useWachtlPickupLocations" to use LiberoWachtlTrait
** check for useDaiaLocationId to use FincIls getPickUpLocations
** otherwise use customData from getCustomData

* refactor parseDaiaArray for getPickUpLocations
** set item_id for non-title-hold orders (storage retrieval request)
** also refs #20223
---
 local/config/vufind/FincLibero.ini            |  5 +++
 .../finc/src/finc/ILS/Driver/FincLibero.php   | 45 +++++++++++++++++++
 .../src/finc/ILS/Driver/LiberoWachtlTrait.php | 43 ++++++++++++++++++
 3 files changed, 93 insertions(+)

diff --git a/local/config/vufind/FincLibero.ini b/local/config/vufind/FincLibero.ini
index 26fb4a666a5..3b73011fd7f 100644
--- a/local/config/vufind/FincLibero.ini
+++ b/local/config/vufind/FincLibero.ini
@@ -50,3 +50,8 @@ relative_path=FincILS.ini
 
 ; URIs that will be used for reading room views
 ;readingRoomURIs[] = "http://data.ub.uni-leipzig.de/resource/DE-15/pickup/zw01thekfo"
+
+; use Wachtl service for departments instead of DAIA details
+;useWachtlPickupLocations = false
+; use locationid and not customData
+;useDaiaLocationId = true
diff --git a/module/finc/src/finc/ILS/Driver/FincLibero.php b/module/finc/src/finc/ILS/Driver/FincLibero.php
index 91d958f1bf6..ec2aed3e4ee 100644
--- a/module/finc/src/finc/ILS/Driver/FincLibero.php
+++ b/module/finc/src/finc/ILS/Driver/FincLibero.php
@@ -328,6 +328,8 @@ class FincLibero extends FincILS implements TranslatorAwareInterface
                     )
                 ) {
                     $result_item['item_id'] = $titleHoldId;
+                } else {
+                    $result_item['item_id'] = $item['id'];
                 }
 
                 // custom DAIA field used in getHoldLink()
@@ -1269,4 +1271,47 @@ class FincLibero extends FincILS implements TranslatorAwareInterface
     {
         return '';
     }
+
+    /**
+     * This method returns a list of locations where a user may collect a hold.
+     *
+     * @param array $patron  Patron array returned by patronLogin method
+     * @param array $details Hold information array similar to placeHold's input
+     * @return array Array of associative arrays containing these keys:
+     *      locationID - A pick up location id or code (string)
+     *      locationDisplay – The text to display for the location (string)
+     *
+     * @link https://vufind.org/wiki/development:plugins:ils_drivers#getpickuplocations
+     */
+    public function getPickUpLocations($patron = [], $details = [])
+    {
+        // use departments of Wachtl, not DAIA
+        if ($this->config['General']['useWachtlPickupLocations'] ?? false) {
+            return $this->getWachtlPickUpLocations($details);
+        }
+
+        // use location and locationid, not customData of DAIA
+        if ($this->config['General']['useDaiaLocationId'] ?? false) {
+            return parent::getPickUpLocations($patron, $details);
+        }
+
+        // use DAIA customData
+        if ($details != [] && isset($details['id'])) {
+            $statusItems = $this->getStatus($details['id']);
+            foreach ($statusItems as $statusItem) {
+                if ($statusItem['item_id'] == $details['item_id']) {
+                    // our pickUpLocations are stored in the customData array upon
+                    // processing the DAIA availability information
+                    if (isset($statusItem['customData'])
+                        && isset($statusItem['customData']['pickUpLocations'])
+                    ) {
+                        return $statusItem['customData']['pickUpLocations'];
+                    } else {
+                        return $this->stackURIs ?? [];
+                    }
+                }
+            }
+        }
+        return [];
+    }
 }
diff --git a/module/finc/src/finc/ILS/Driver/LiberoWachtlTrait.php b/module/finc/src/finc/ILS/Driver/LiberoWachtlTrait.php
index 343989de538..ea403ca867f 100644
--- a/module/finc/src/finc/ILS/Driver/LiberoWachtlTrait.php
+++ b/module/finc/src/finc/ILS/Driver/LiberoWachtlTrait.php
@@ -442,6 +442,49 @@ trait LiberoWachtlTrait
         return $this->getLiberoWachtlResult($result, 'getMyProfile');
     }
 
+    /**
+     * This method returns a list of locations where a user may collect a hold.
+     *
+     * @param array $details Hold information array similar to placeHold's input - needed for overriden method in de_zi4
+     *
+     * @return array $locations Array of associative arrays containing these keys:
+     *      locationID - A pick up location id or code (string)
+     *      locationDisplay – The text to display for the location (string)
+     * @throws \Exception
+     * @throws \ILSException
+     *
+     * @link https://vufind.org/wiki/development:plugins:ils_drivers#getpickuplocations
+     */
+    public function getWachtlPickUpLocations($details = [])
+    {
+        $locations = [];
+
+        try {
+            $client = $this->httpService->createClient(
+                $this->baseUrl .
+                $this->config['General']['liberoDbName'] .
+                '/departments'
+            );
+            $result = $client->send();
+        } catch (\Exception $e) {
+            throw new ILSException($e->getMessage());
+        }
+
+        if ($result->isSuccess()) {
+            $arr = json_decode($result->getBody(), true);
+            $i = 0;
+            foreach ($arr as $code => $name) {
+                $locations[]
+                    = ['locationID' => $code, 'locationDisplay' => $name]
+                ;
+            }
+        } else {
+            $this->debug(
+                "No list of pickup locations found."
+            );
+        }
+        return $locations;
+    }
 
     /**
      * This method sends a PIN changing request to the LiberoWachtl.
-- 
GitLab