Bug 17600: Standardize our EXPORT_OK
[srvgit] / Koha / REST / V1 / Holds.pm
index 09b7b7e..7541a5c 100644 (file)
@@ -19,15 +19,17 @@ use Modern::Perl;
 
 use Mojo::Base 'Mojolicious::Controller';
 
-use C4::Biblio;
+use Mojo::JSON;
+
 use C4::Reserves;
 
 use Koha::Items;
 use Koha::Patrons;
 use Koha::Holds;
-use Koha::DateUtils;
+use Koha::DateUtils qw( dt_from_string output_pref );
 
-use Try::Tiny;
+use List::MoreUtils qw( any );
+use Try::Tiny qw( catch try );
 
 =head1 API
 
@@ -35,7 +37,7 @@ use Try::Tiny;
 
 =head3 list
 
-Mehtod that handles listing Koha::Hold objects
+Method that handles listing Koha::Hold objects
 
 =cut
 
@@ -65,6 +67,7 @@ sub add {
         my $body = $c->validation->param('body');
 
         my $biblio;
+        my $item;
 
         my $biblio_id         = $body->{biblio_id};
         my $pickup_library_id = $body->{pickup_library_id};
@@ -74,6 +77,10 @@ sub add {
         my $expiration_date   = $body->{expiration_date};
         my $notes             = $body->{notes};
         my $hold_date         = $body->{hold_date};
+        my $non_priority      = $body->{non_priority};
+
+        my $overrides = $c->stash('koha.overrides');
+        my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
 
         if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
             return $c->render(
@@ -98,7 +105,7 @@ sub add {
             }
         }
         elsif ($item_id) {
-            my $item = Koha::Items->find($item_id);
+            $item = Koha::Items->find($item_id);
 
             unless ($item) {
                 return $c->render(
@@ -135,6 +142,28 @@ sub add {
             );
         }
 
+        # Validate pickup location
+        my $valid_pickup_location;
+        if ($item) {    # item-level hold
+            $valid_pickup_location =
+              any { $_->branchcode eq $pickup_library_id }
+            $item->pickup_locations(
+                { patron => $patron } );
+        }
+        else {
+            $valid_pickup_location =
+              any { $_->branchcode eq $pickup_library_id }
+            $biblio->pickup_locations(
+                { patron => $patron } );
+        }
+
+        return $c->render(
+            status  => 400,
+            openapi => {
+                error => 'The supplied pickup location is not valid'
+            }
+        ) unless $valid_pickup_location || $can_override;
+
         my $can_place_hold
             = $item_id
             ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
@@ -144,9 +173,7 @@ sub add {
             $can_place_hold->{status} = 'tooManyReserves';
         }
 
-        my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
-
-        unless ($can_override || $can_place_hold->{status} eq 'OK' ) {
+        unless ( $can_override || $can_place_hold->{status} eq 'OK' ) {
             return $c->render(
                 status => 403,
                 openapi =>
@@ -174,6 +201,7 @@ sub add {
                 itemnumber       => $item_id,
                 found            => undef,                # TODO: Why not?
                 itemtype         => $item_type,
+                non_priority     => $non_priority,
             }
         );
 
@@ -223,14 +251,34 @@ sub edit {
         my $hold = Koha::Holds->find( $hold_id );
 
         unless ($hold) {
-            return $c->render( status  => 404,
-                            openapi => {error => "Hold not found"} );
+            return $c->render(
+                status  => 404,
+                openapi => { error => "Hold not found" }
+            );
         }
 
-        my $body = $c->req->json;
+        my $overrides = $c->stash('koha.overrides');
+        my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
 
-        my $pickup_library_id = $body->{pickup_library_id} // $hold->branchcode;
-        my $priority          = $body->{priority} // $hold->priority;
+        my $body = $c->validation->output->{body};
+
+        my $pickup_library_id = $body->{pickup_library_id};
+
+        if (
+            defined $pickup_library_id
+            && ( !$hold->is_pickup_location_valid({ library_id => $pickup_library_id }) && !$can_override )
+          )
+        {
+            return $c->render(
+                status  => 400,
+                openapi => {
+                    error => 'The supplied pickup location is not valid'
+                }
+            );
+        }
+
+        $pickup_library_id //= $hold->branchcode;
+        my $priority         = $body->{priority} // $hold->priority;
         # suspended_until can also be set to undef
         my $suspended_until   = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
 
@@ -393,4 +441,128 @@ sub update_priority {
     };
 }
 
+=head3 pickup_locations
+
+Method that returns the possible pickup_locations for a given hold
+used for building the dropdown selector
+
+=cut
+
+sub pickup_locations {
+    my $c = shift->openapi->valid_input or return;
+
+    my $hold_id = $c->validation->param('hold_id');
+    my $hold = Koha::Holds->find( $hold_id, { prefetch => [ 'patron' ] } );
+
+    unless ($hold) {
+        return $c->render(
+            status  => 404,
+            openapi => { error => "Hold not found" }
+        );
+    }
+
+    return try {
+        my $ps_set;
+
+        if ( $hold->itemnumber ) {
+            $ps_set = $hold->item->pickup_locations( { patron => $hold->patron } );
+        }
+        else {
+            $ps_set = $hold->biblio->pickup_locations( { patron => $hold->patron } );
+        }
+
+        my $pickup_locations = $c->objects->search( $ps_set );
+        my @response = ();
+
+        if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
+
+            my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
+            my $libraries    = $c->objects->search($libraries_rs);
+
+            @response = map {
+                my $library = $_;
+                $library->{needs_override} = (
+                    any { $_->{library_id} eq $library->{library_id} }
+                    @{$pickup_locations}
+                  )
+                  ? Mojo::JSON->false
+                  : Mojo::JSON->true;
+                $library;
+            } @{$libraries};
+
+            return $c->render(
+                status  => 200,
+                openapi => \@response
+            );
+        }
+
+        @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
+
+        return $c->render(
+            status  => 200,
+            openapi => \@response
+        );
+    }
+    catch {
+        $c->unhandled_exception($_);
+    };
+}
+
+=head3 update_pickup_location
+
+Method that handles modifying the pickup location of a Koha::Hold object
+
+=cut
+
+sub update_pickup_location {
+    my $c = shift->openapi->valid_input or return;
+
+    my $hold_id = $c->validation->param('hold_id');
+    my $body    = $c->validation->param('body');
+    my $pickup_library_id = $body->{pickup_library_id};
+
+    my $hold = Koha::Holds->find($hold_id);
+
+    unless ($hold) {
+        return $c->render(
+            status  => 404,
+            openapi => { error => "Hold not found" }
+        );
+    }
+
+    return try {
+
+        my $overrides    = $c->stash('koha.overrides');
+        my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
+
+        $hold->set_pickup_location(
+            {
+                library_id => $pickup_library_id,
+                force      => $can_override
+            }
+        );
+
+        return $c->render(
+            status  => 200,
+            openapi => {
+                pickup_library_id => $pickup_library_id
+            }
+        );
+    }
+    catch {
+
+        if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::InvalidPickupLocation') ) {
+            return $c->render(
+                status  => 400,
+                openapi => {
+                    error => "$_"
+                }
+            );
+        }
+
+        $c->unhandled_exception($_);
+    };
+}
+
+
 1;