Bug 20212: Make all biblioitems.* fields searchable
[koha.git] / Koha / REST / V1 / Acquisitions / Orders.pm
1 package Koha::REST::V1::Acquisitions::Orders;
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 Koha::Acquisition::Orders;
23 use Koha::DateUtils;
24
25 use Clone 'clone';
26 use JSON qw(decode_json);
27 use Scalar::Util qw( blessed );
28 use Try::Tiny;
29
30 =head1 NAME
31
32 Koha::REST::V1::Acquisitions::Orders
33
34 =head1 API
35
36 =head2 Methods
37
38 =head3 list
39
40 Controller function that handles listing Koha::Acquisition::Order objects
41
42 =cut
43
44 sub list {
45
46     my $c = shift->openapi->valid_input or return;
47
48     return try {
49
50         my $only_active = delete $c->validation->output->{only_active};
51         my $order_id    = delete $c->validation->output->{order_id};
52
53         my $orders_rs;
54
55         if ( $only_active ) {
56             $orders_rs = Koha::Acquisition::Orders->filter_by_active;
57         }
58         else {
59             $orders_rs = Koha::Acquisition::Orders->new;
60         }
61
62         $orders_rs = $orders_rs->filter_by_id_including_transfers({ ordernumber => $order_id })
63             if $order_id;
64
65         my $args = $c->validation->output;
66         my $attributes = {};
67
68         # Extract reserved params
69         my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
70         # Look for embeds
71         my $embed = $c->stash('koha.embed');
72         my $fixed_embed = clone($embed);
73         if ( exists $fixed_embed->{biblio} ) {
74             # Add biblioitems to prefetch
75             # FIXME remove if we merge biblio + biblioitems
76             $fixed_embed->{biblio}->{children}->{biblioitem} = {};
77             $c->stash('koha.embed', $fixed_embed);
78         }
79
80         # If no pagination parameters are passed, default
81         $reserved_params->{_per_page} //= C4::Context->preference('RESTdefaultPageSize');
82         $reserved_params->{_page}     //= 1;
83
84         unless ( $reserved_params->{_per_page} == -1 ) {
85             # Merge pagination into query attributes
86             $c->dbic_merge_pagination(
87                 {
88                     filter => $attributes,
89                     params => $reserved_params
90                 }
91             );
92         }
93
94         # Generate prefetches for embedded stuff
95         $c->dbic_merge_prefetch(
96             {
97                 attributes => $attributes,
98                 result_set => $orders_rs
99             }
100         );
101
102         # Call the to_model function by reference, if defined
103         if ( defined $filtered_params ) {
104
105             # Apply the mapping function to the passed params
106             $filtered_params = $orders_rs->attributes_from_api($filtered_params);
107             $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
108         }
109
110         if ( defined $path_params ) {
111
112             # Apply the mapping function to the passed params
113             $filtered_params //= {};
114             $path_params = $orders_rs->attributes_from_api($path_params);
115             foreach my $param (keys %{$path_params}) {
116                 $filtered_params->{$param} = $path_params->{$param};
117             }
118         }
119
120         if ( defined $reserved_params->{q} || defined $reserved_params->{query} || defined $reserved_params->{'x-koha-query'}) {
121             $filtered_params //={};
122             my @query_params_array;
123             my $query_params;
124             if ( exists $reserved_params->{query} and defined $reserved_params->{query} ) {
125                 push @query_params_array, fix_query({ query => $reserved_params->{query} });
126             }
127             if ( exists $reserved_params->{q} and defined $reserved_params->{q}) {
128                 push @query_params_array, fix_query({ query => decode_json($reserved_params->{q}) });
129             }
130             if ( exists $reserved_params->{'x-koha-query'} and defined $reserved_params->{'x-koha-query'} ) {
131                 push @query_params_array, fix_query({ query => decode_json($reserved_params->{'x-koha-query'}) });;
132             }
133
134             if(scalar(@query_params_array) > 1) {
135                 $query_params = {'-and' => \@query_params_array};
136             }
137             else {
138                 $query_params = $query_params_array[0];
139             }
140
141             $filtered_params = $c->merge_q_params( $filtered_params, $query_params, $orders_rs );
142         }
143
144         # Perform search
145         my $orders = $orders_rs->search( $filtered_params, $attributes );
146
147         if ($orders->is_paged) {
148             $c->add_pagination_headers({
149                 total => $orders->pager->total_entries,
150                 params => $args,
151             });
152         }
153         else {
154             $c->add_pagination_headers({
155                 total => $orders->count,
156                 params => $args,
157             });
158         }
159
160         return $c->render(
161             status  => 200,
162             openapi => $orders->to_api({ embed => $embed })
163         );
164     }
165     catch {
166         $c->unhandled_exception($_);
167     };
168 }
169
170 =head3 get
171
172 Controller function that handles retrieving a single Koha::Acquisition::Order object
173
174 =cut
175
176 sub get {
177     my $c = shift->openapi->valid_input or return;
178
179     my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
180
181     unless ($order) {
182         return $c->render(
183             status  => 404,
184             openapi => { error => "Order not found" }
185         );
186     }
187
188     return try {
189         my $embed = $c->stash('koha.embed');
190
191         return $c->render(
192             status  => 200,
193             openapi => $order->to_api({ embed => $embed })
194         );
195     }
196     catch {
197         $c->unhandled_exception($_);
198     };
199 }
200
201 =head3 add
202
203 Controller function that handles adding a new Koha::Acquisition::Order object
204
205 =cut
206
207 sub add {
208     my $c = shift->openapi->valid_input or return;
209
210     return try {
211         my $order = Koha::Acquisition::Order->new_from_api( $c->validation->param('body') );
212         $order->store->discard_changes;
213
214         $c->res->headers->location(
215             $c->req->url->to_string . '/' . $order->ordernumber
216         );
217
218         return $c->render(
219             status  => 201,
220             openapi => $order->to_api
221         );
222     }
223     catch {
224         if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
225             return $c->render(
226                 status  => 409,
227                 openapi => { error => $_->error, conflict => $_->duplicate_id }
228             );
229         }
230
231         $c->unhandled_exception($_);
232     };
233 }
234
235 =head3 update
236
237 Controller function that handles updating a Koha::Acquisition::Order object
238
239 =cut
240
241 sub update {
242     my $c = shift->openapi->valid_input or return;
243
244     my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
245
246     unless ($order) {
247         return $c->render(
248             status  => 404,
249             openapi => { error => "Order not found" }
250         );
251     }
252
253     return try {
254         $order->set_from_api( $c->validation->param('body') );
255         $order->store()->discard_changes;
256
257         return $c->render(
258             status  => 200,
259             openapi => $order->to_api
260         );
261     }
262     catch {
263         $c->unhandled_exception($_);
264     };
265 }
266
267 =head3 delete
268
269 Controller function that handles deleting a Koha::Patron object
270
271 =cut
272
273 sub delete {
274     my $c = shift->openapi->valid_input or return;
275
276     my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
277
278     unless ($order) {
279         return $c->render(
280             status  => 404,
281             openapi => { error => 'Order not found' }
282         );
283     }
284
285     return try {
286
287         $order->delete;
288
289         return $c->render(
290             status  => 204,
291             openapi => q{}
292         );
293     }
294     catch {
295         $c->unhandled_exception($_);
296     };
297 }
298
299 =head2 Internal methods
300
301 =head3 fix_query
302
303     my $query = fix_query($query);
304
305 This method takes care of recursively fixing queries that should be done
306 against biblioitems (instead if biblio as exposed on the API)
307
308 =cut
309
310 sub fix_query {
311     my ($args) = @_;
312
313     my $query = $args->{query};
314     my $biblioitem_fields = {
315         'biblio.age_restriction'     => 'biblio.biblioitem.age_restriction',
316         'biblio.cn_class'            => 'biblio.biblioitem.cn_class',
317         'biblio.cn_item'             => 'biblio.biblioitem.cn_item',
318         'biblio.cn_sort'             => 'biblio.biblioitem.cn_sort',
319         'biblio.cn_source'           => 'biblio.biblioitem.cn_source',
320         'biblio.cn_suffix'           => 'biblio.biblioitem.cn_suffix',
321         'biblio.collection_issn'     => 'biblio.biblioitem.collection_issn',
322         'biblio.collection_title'    => 'biblio.biblioitem.collection_title',
323         'biblio.collection_volume'   => 'biblio.biblioitem.collection_volume',
324         'biblio.ean'                 => 'biblio.biblioitem.ean',
325         'biblio.edition_statement'   => 'biblio.biblioitem.edition_statement',
326         'biblio.illustrations'       => 'biblio.biblioitem.illustrations',
327         'biblio.isbn'                => 'biblio.biblioitem.isbn',
328         'biblio.issn'                => 'biblio.biblioitem.issn',
329         'biblio.item_type'           => 'biblio.biblioitem.item_type',
330         'biblio.lc_control_number'   => 'biblio.biblioitem.lc_control_number',
331         'biblio.material_size'       => 'biblio.biblioitem.material_size',
332         'biblio.notes'               => 'biblio.biblioitem.notes',
333         'biblio.number'              => 'biblio.biblioitem.number',
334         'biblio.pages'               => 'biblio.biblioitem.pages',
335         'biblio.publication_place'   => 'biblio.biblioitem.publication_place',
336         'biblio.publication_year'    => 'biblio.biblioitem.publication_year',
337         'biblio.publisher'           => 'biblio.biblioitem.publisher',
338         'biblio.serial_total_issues' => 'biblio.biblioitem.serial_total_issues'
339         'biblio.url'                 => 'biblio.biblioitem.url',
340         'biblio.volume'              => 'biblio.biblioitem.volume',
341         'biblio.volume_date'         => 'biblio.biblioitem.volume_date',
342         'biblio.volume_description'  => 'biblio.biblioitem.volume_description',
343     };
344
345     if ( ref($query) eq 'HASH' ) {
346         foreach my $key (keys %{$query}) {
347             if ( exists $biblioitem_fields->{$key}) {
348                 my $subq = delete $query->{$key};
349                 $query->{$biblioitem_fields->{$key}} = (ref($subq) eq 'HASH')
350                         ? fix_query({ query => $subq })
351                         : $subq;
352             }
353             else {
354                 $query->{$key} = fix_query({ query => $query->{$key} });
355             }
356         }
357     }
358     elsif ( ref($query) eq 'ARRAY' ) {
359         my @accum;
360         foreach my $item (@{$query}) {
361             push @accum, fix_query({ query => $item });
362         }
363         $query = \@accum;
364     }
365     else { # scalar
366         $query = $biblioitem_fields->{$query}
367             if exists $biblioitem_fields->{$query};
368     }
369
370     return $query;
371 }
372
373 1;