Bug 23820: Add checkout to default to patron's home branch on club hold
authorAgustin Moyano <agustinmoyano@theke.io>
Mon, 30 Mar 2020 18:56:18 +0000 (18:56 +0000)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 20 Jul 2020 15:45:30 +0000 (17:45 +0200)
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>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Koha/Club/Hold.pm
Koha/REST/V1/Clubs/Holds.pm
api/v1/swagger/paths/clubs.json
koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt

index 567a314..39a58dc 100644 (file)
@@ -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;
index 6acaced..78f741a 100644 (file)
@@ -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(
index 2f4498a..fb66e41 100644 (file)
                     "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" ]
index 44c46d2..8473127 100644 (file)
                                     [% 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>
                 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('/')