e104ce5dea5534b7e2ca416364a28cf15fd53eff
[srvgit] / 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
125             # FIXME The following lines are an ugly fix to deal with isbn and ean searches
126             # This must NOT be reused or extended
127             # Instead we need a better and global solution in a Koha::*Biblio method
128             for my $q ( qw( q query x-koha-query ) ) {
129                 next unless $reserved_params->{$q};
130                 for my $f ( qw( isbn ean publisher ) ) {
131                     $reserved_params->{$q} =~ s|"biblio.$f":|"biblio.biblioitem.$f":|g;
132                 }
133                 push @query_params_array, $reserved_params->{$q};
134             }
135
136             if(scalar(@query_params_array) > 1) {
137                 $query_params = {'-and' => \@query_params_array};
138             }
139             else {
140                 $query_params = $query_params_array[0];
141             }
142
143             $filtered_params = $c->merge_q_params( $filtered_params, $query_params, $orders_rs );
144         }
145
146         # Perform search
147         my $orders = $orders_rs->search( $filtered_params, $attributes );
148         my $total  = $orders_rs->search->count;
149
150         $c->add_pagination_headers(
151             {
152                 total      => ($orders->is_paged ? $orders->pager->total_entries : $orders->count),
153                 base_total => $total,
154                 params     => $args,
155             }
156         );
157
158         return $c->render(
159             status  => 200,
160             openapi => $orders->to_api({ embed => $embed })
161         );
162     }
163     catch {
164         $c->unhandled_exception($_);
165     };
166 }
167
168 =head3 get
169
170 Controller function that handles retrieving a single Koha::Acquisition::Order object
171
172 =cut
173
174 sub get {
175     my $c = shift->openapi->valid_input or return;
176
177     my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
178
179     unless ($order) {
180         return $c->render(
181             status  => 404,
182             openapi => { error => "Order not found" }
183         );
184     }
185
186     return try {
187         my $embed = $c->stash('koha.embed');
188
189         return $c->render(
190             status  => 200,
191             openapi => $order->to_api({ embed => $embed })
192         );
193     }
194     catch {
195         $c->unhandled_exception($_);
196     };
197 }
198
199 =head3 add
200
201 Controller function that handles adding a new Koha::Acquisition::Order object
202
203 =cut
204
205 sub add {
206     my $c = shift->openapi->valid_input or return;
207
208     return try {
209         my $order = Koha::Acquisition::Order->new_from_api( $c->validation->param('body') );
210         $order->store->discard_changes;
211
212         $c->res->headers->location(
213             $c->req->url->to_string . '/' . $order->ordernumber
214         );
215
216         return $c->render(
217             status  => 201,
218             openapi => $order->to_api
219         );
220     }
221     catch {
222         if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
223             return $c->render(
224                 status  => 409,
225                 openapi => { error => $_->error, conflict => $_->duplicate_id }
226             );
227         }
228
229         $c->unhandled_exception($_);
230     };
231 }
232
233 =head3 update
234
235 Controller function that handles updating a Koha::Acquisition::Order object
236
237 =cut
238
239 sub update {
240     my $c = shift->openapi->valid_input or return;
241
242     my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
243
244     unless ($order) {
245         return $c->render(
246             status  => 404,
247             openapi => { error => "Order not found" }
248         );
249     }
250
251     return try {
252         $order->set_from_api( $c->validation->param('body') );
253         $order->store()->discard_changes;
254
255         return $c->render(
256             status  => 200,
257             openapi => $order->to_api
258         );
259     }
260     catch {
261         $c->unhandled_exception($_);
262     };
263 }
264
265 =head3 delete
266
267 Controller function that handles deleting a Koha::Patron object
268
269 =cut
270
271 sub delete {
272     my $c = shift->openapi->valid_input or return;
273
274     my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
275
276     unless ($order) {
277         return $c->render(
278             status  => 404,
279             openapi => { error => 'Order not found' }
280         );
281     }
282
283     return try {
284
285         $order->delete;
286
287         return $c->render(
288             status  => 204,
289             openapi => q{}
290         );
291     }
292     catch {
293         $c->unhandled_exception($_);
294     };
295 }
296
297 1;