Bug 28854: (follow-up) Use barcodedecode in Koha::REST::V1::Items
[koha.git] / Koha / REST / V1 / Items.pm
1 package Koha::REST::V1::Items;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use C4::Circulation qw( barcodedecode );
23
24 use Koha::Items;
25
26 use List::MoreUtils qw( any );
27 use Try::Tiny qw( catch try );
28
29 =head1 NAME
30
31 Koha::REST::V1::Items - Koha REST API for handling items (V1)
32
33 =head1 API
34
35 =head2 Methods
36
37 =cut
38
39 =head3 list
40
41 Controller function that handles listing Koha::Item objects
42
43 =cut
44
45 sub list {
46     my $c = shift->openapi->valid_input or return;
47
48     return try {
49         my $items_set = Koha::Items->new;
50         my $items     = $c->objects->search( $items_set );
51         return $c->render(
52             status  => 200,
53             openapi => $items
54         );
55     }
56     catch {
57         $c->unhandled_exception($_);
58     };
59 }
60
61
62 =head3 get
63
64 Controller function that handles retrieving a single Koha::Item
65
66 =cut
67
68 sub get {
69     my $c = shift->openapi->valid_input or return;
70
71     try {
72         my $item = Koha::Items->find($c->validation->param('item_id'));
73         unless ( $item ) {
74             return $c->render(
75                 status => 404,
76                 openapi => { error => 'Item not found'}
77             );
78         }
79         return $c->render( status => 200, openapi => $item->to_api );
80     }
81     catch {
82         $c->unhandled_exception($_);
83     };
84 }
85
86 =head3 pickup_locations
87
88 Method that returns the possible pickup_locations for a given item
89 used for building the dropdown selector
90
91 =cut
92
93 sub pickup_locations {
94     my $c = shift->openapi->valid_input or return;
95
96     my $item_id = $c->validation->param('item_id');
97     my $item = Koha::Items->find( $item_id );
98
99     unless ($item) {
100         return $c->render(
101             status  => 404,
102             openapi => { error => "Item not found" }
103         );
104     }
105
106     my $patron_id = delete $c->validation->output->{patron_id};
107     my $patron    = Koha::Patrons->find( $patron_id );
108
109     unless ($patron) {
110         return $c->render(
111             status  => 400,
112             openapi => { error => "Patron not found" }
113         );
114     }
115
116     return try {
117
118         my $pl_set = $item->pickup_locations( { patron => $patron } );
119
120         my @response = ();
121         if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
122
123             my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
124             my $libraries    = $c->objects->search($libraries_rs);
125
126             @response = map {
127                 my $library = $_;
128                 $library->{needs_override} = (
129                     any { $_->branchcode eq $library->{library_id} }
130                     @{ $pl_set->as_list }
131                   )
132                   ? Mojo::JSON->false
133                   : Mojo::JSON->true;
134                 $library;
135             } @{$libraries};
136         }
137         else {
138
139             my $pickup_locations = $c->objects->search($pl_set);
140             @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
141         }
142
143         return $c->render(
144             status  => 200,
145             openapi => \@response
146         );
147     }
148     catch {
149         $c->unhandled_exception($_);
150     };
151 }
152
153 =head3 bundled_items
154
155 Controller function that handles bundled_items Koha::Item objects
156
157 =cut
158
159 sub bundled_items {
160     my $c = shift->openapi->valid_input or return;
161
162     my $item_id = $c->validation->param('item_id');
163     my $item = Koha::Items->find( $item_id );
164
165     unless ($item) {
166         return $c->render(
167             status  => 404,
168             openapi => { error => "Item not found" }
169         );
170     }
171
172     return try {
173         my $items_set = $item->bundle_items;
174         my $items     = $c->objects->search( $items_set );
175         return $c->render(
176             status  => 200,
177             openapi => $items
178         );
179     }
180     catch {
181         $c->unhandled_exception($_);
182     };
183 }
184
185 =head3 add_to_bundle
186
187 Controller function that handles adding items to this bundle
188
189 =cut
190
191 sub add_to_bundle {
192     my $c = shift->openapi->valid_input or return;
193
194     my $item_id = $c->validation->param('item_id');
195     my $item = Koha::Items->find( $item_id );
196
197     unless ($item) {
198         return $c->render(
199             status  => 404,
200             openapi => { error => "Item not found" }
201         );
202     }
203
204     my $bundle_item_id = $c->validation->param('body')->{'external_id'};
205     $bundle_item_id = barcodedecode($bundle_item_id);
206     my $bundle_item = Koha::Items->find( { barcode => $bundle_item_id } );
207
208     unless ($bundle_item) {
209         return $c->render(
210             status  => 404,
211             openapi => { error => "Bundle item not found" }
212         );
213     }
214
215     return try {
216         my $link = $item->add_to_bundle($bundle_item);
217         return $c->render(
218             status  => 201,
219             openapi => $bundle_item
220         );
221     }
222     catch {
223         if ( ref($_) eq 'Koha::Exceptions::Object::DuplicateID' ) {
224             return $c->render(
225                 status  => 409,
226                 openapi => {
227                     error => 'Item is already bundled',
228                     key   => $_->duplicate_id
229                 }
230             );
231         }
232         else {
233             $c->unhandled_exception($_);
234         }
235     };
236 }
237
238 =head3 remove_from_bundle
239
240 Controller function that handles removing items from this bundle
241
242 =cut
243
244 sub remove_from_bundle {
245     my $c = shift->openapi->valid_input or return;
246
247     my $item_id = $c->validation->param('item_id');
248     my $item = Koha::Items->find( $item_id );
249
250     unless ($item) {
251         return $c->render(
252             status  => 404,
253             openapi => { error => "Item not found" }
254         );
255     }
256
257     my $bundle_item_id = $c->validation->param('bundled_item_id');
258     $bundle_item_id = barcodedecode($bundle_item_id);
259     my $bundle_item = Koha::Items->find( { itemnumber => $bundle_item_id } );
260
261     unless ($bundle_item) {
262         return $c->render(
263             status  => 404,
264             openapi => { error => "Bundle item not found" }
265         );
266     }
267
268     $bundle_item->remove_from_bundle;
269     return $c->render(
270         status  => 204,
271         openapi => q{}
272     );
273 }
274
275 1;