From 172ad70e67e500e17517540eb9337dc630e56edd Mon Sep 17 00:00:00 2001
From: Agustin Moyano <agustinmoyano@theke.io>
Date: Mon, 30 Mar 2020 18:56:18 +0000
Subject: [PATCH] Bug 23820: Add checkout to default to patron's home branch on
 club hold

This patch adds a checkbox to make holds created by club hold to default to patron's home branch if possible.

To test:
1. Apply this patch
2. Create a club, and add two patrons (from now on called patron A and patron B) to it, each one form a different library..
3. Create a hold for the club, and in the details set pickup location different from any of the patrons.
4. Check "Pickup at patron's home library when possible" checkbox
SUCCESS => when submitted, pickup location of holds defaults to patron's home branch
5. Modify patron A's library and set pickup location to no.
6. Repeat steps 3 and 4.
SUCCESS => when submitted, patron A's hold now points to pickup location setted on step 3, and patron B's hold still points to his home branch.
7. Sign off

Sponsored-by: Southeast Kansas Library - SEKLS

Signed-off-by: Jason Robb <jrobb@sekls.org>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
---
 Koha/Club/Hold.pm                                  | 27 +++++++++++++++++-----
 Koha/REST/V1/Clubs/Holds.pm                        |  4 +++-
 api/v1/swagger/paths/clubs.json                    |  4 ++++
 .../prog/en/modules/reserve/request.tt             |  7 ++++++
 4 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/Koha/Club/Hold.pm b/Koha/Club/Hold.pm
index 567a3149bc..39a58dc7d4 100644
--- a/Koha/Club/Hold.pm
+++ b/Koha/Club/Hold.pm
@@ -29,6 +29,7 @@ use base qw(Koha::Object);
 use Koha::Exceptions::ClubHold;
 use Koha::Club::Hold::PatronHold;
 use Koha::Clubs;
+use Koha::Patrons;
 
 use List::Util 'shuffle';
 
@@ -72,11 +73,26 @@ sub add {
 
     foreach my $enrollment (@enrollments) {
         my $patron_id = $enrollment->borrowernumber;
+        my $pickup_id = $params->{pickup_library_id};
+
+        my $can_place_hold;
+        if($params->{default_patron_home}) {
+            my $patron = Koha::Patrons->find($patron_id);
+            my $patron_home = $patron->branchcode;
+            $can_place_hold = $params->{item_id}
+                ? C4::Reserves::CanItemBeReserved( $patron_id, $params->{item_id}, $patron_home )
+                : C4::Reserves::CanBookBeReserved( $patron_id, $params->{biblio_id}, $patron_home );
+            $pickup_id = $patron_home if $can_place_hold->{status} eq 'OK';
+            unless ( $can_place_hold->{status} eq 'OK' ) {
+                warn "Patron(".$patron_id.") Hold cannot be placed with patron's homebranch ($patron_home). Reason: " . $can_place_hold->{status};
+            }
+        }
 
-        my $can_place_hold
-        = $params->{item_id}
-        ? C4::Reserves::CanItemBeReserved( $patron_id, $params->{club_id} )
-        : C4::Reserves::CanBookBeReserved( $patron_id, $params->{biblio_id} );
+        unless ( defined $can_place_hold && $can_place_hold->{status} eq 'OK' ) {
+            $can_place_hold = $params->{item_id}
+                ? C4::Reserves::CanItemBeReserved( $patron_id, $params->{item_id}, $pickup_id )
+                : C4::Reserves::CanBookBeReserved( $patron_id, $params->{biblio_id}, $pickup_id );
+        }
 
         unless ( $can_place_hold->{status} eq 'OK' ) {
             warn "Patron(".$patron_id.") Hold cannot be placed. Reason: " . $can_place_hold->{status};
@@ -92,7 +108,7 @@ sub add {
 
         my $hold_id = C4::Reserves::AddReserve(
             {
-                branchcode      => $params->{pickup_library_id},
+                branchcode      => $pickup_id,
                 borrowernumber  => $patron_id,
                 biblionumber    => $params->{biblio_id},
                 priority        => $priority,
@@ -118,7 +134,6 @@ sub add {
                 error_message => "Could not create hold for Patron(".$patron_id.")"
             })->store();
         }
-
     }
 
     return $club_hold;
diff --git a/Koha/REST/V1/Clubs/Holds.pm b/Koha/REST/V1/Clubs/Holds.pm
index 6acaced0c3..78f741acd0 100644
--- a/Koha/REST/V1/Clubs/Holds.pm
+++ b/Koha/REST/V1/Clubs/Holds.pm
@@ -58,6 +58,7 @@ sub add {
         my $item_type         = $body->{item_type};
         my $expiration_date   = $body->{expiration_date};
         my $notes             = $body->{notes};
+        my $default_patron_home = $body->{default_patron_home};
 
         if ( $item_id and $biblio_id ) {
 
@@ -116,7 +117,8 @@ sub add {
             pickup_library_id => $pickup_library_id,
             expiration_date => $expiration_date,
             notes => $notes,
-            item_type => $item_type
+            item_type => $item_type,
+            default_patron_home => $default_patron_home
         });
 
         return $c->render(
diff --git a/api/v1/swagger/paths/clubs.json b/api/v1/swagger/paths/clubs.json
index 2f4498a442..fb66e4107f 100644
--- a/api/v1/swagger/paths/clubs.json
+++ b/api/v1/swagger/paths/clubs.json
@@ -38,6 +38,10 @@
                     "item_type": {
                       "description": "Limit hold on one itemtype (ignored for item-level holds)",
                       "type": [ "string", "null" ]
+                    },
+                    "default_patron_home": {
+                      "description": "For each patron, set pickup location to patron's home library if possible",
+                      "type": "integer"
                     }
                   },
                   "required": [ "pickup_library_id" ]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt
index 44c46d2395..8473127a39 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt
@@ -162,6 +162,10 @@
                                     [% PROCESS options_for_libraries libraries => Branches.all({ selected => club.branchcode, search_params => { pickup_location => 1 } }) %]
                                 </select>
                             </li>
+                            <li>
+                                <label for="default_patron_home">Pickup at patron's home library when possible</label>
+                                <input type="checkbox" name="default_patron_home"/>
+                            </li>
                         </ol>
                         <h2 style="padding: 0 1em;">Members</h2>
                         <ol>
@@ -1039,6 +1043,9 @@
                 if($('input[name="itemtype"]').length) {
                     data.item_type = $('input[name="itemtype"]').val()||null;
                 }
+                if($('input[name="default_patron_home"]:checked').length) {
+                    data.default_patron_home = 1;
+                }
                 if($('input[name="biblionumbers"]').length) {
                     biblionumbers_text = $('input[name="biblionumbers"]').val();
                     biblionumbers = biblionumbers_text.replace(/\/$/, '').split('/')
-- 
2.11.0