Bug 17600: Standardize our EXPORT_OK
[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
24 use Clone qw( clone );
25 use Scalar::Util qw( blessed );
26 use Try::Tiny qw( catch try );
27
28 =head1 NAME
29
30 Koha::REST::V1::Acquisitions::Orders
31
32 =head1 API
33
34 =head2 Methods
35
36 =head3 list
37
38 Controller function that handles listing Koha::Acquisition::Order objects
39
40 =cut
41
42 sub list {
43
44     my $c = shift->openapi->valid_input or return;
45
46     return try {
47
48         my $only_active = delete $c->validation->output->{only_active};
49         my $order_id    = delete $c->validation->output->{order_id};
50
51         my $orders_rs;
52
53         if ( $only_active ) {
54             $orders_rs = Koha::Acquisition::Orders->filter_by_active;
55         }
56         else {
57             $orders_rs = Koha::Acquisition::Orders->new;
58         }
59
60         $orders_rs = $orders_rs->filter_by_id_including_transfers({ ordernumber => $order_id })
61             if $order_id;
62
63         my $args = $c->validation->output;
64         my $attributes = {};
65
66         # Extract reserved params
67         my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
68         # Look for embeds
69         my $embed = $c->stash('koha.embed');
70         my $fixed_embed = clone($embed);
71         if ( exists $fixed_embed->{biblio} ) {
72             # Add biblioitems to prefetch
73             # FIXME remove if we merge biblio + biblioitems
74             $fixed_embed->{biblio}->{children}->{biblioitem} = {};
75             $c->stash('koha.embed', $fixed_embed);
76         }
77
78         # If no pagination parameters are passed, default
79         $reserved_params->{_per_page} //= C4::Context->preference('RESTdefaultPageSize');
80         $reserved_params->{_page}     //= 1;
81
82         unless ( $reserved_params->{_per_page} == -1 ) {
83             # Merge pagination into query attributes
84             $c->dbic_merge_pagination(
85                 {
86                     filter => $attributes,
87                     params => $reserved_params
88                 }
89             );
90         }
91
92         # Generate prefetches for embedded stuff
93         $c->dbic_merge_prefetch(
94             {
95                 attributes => $attributes,
96                 result_set => $orders_rs
97             }
98         );
99
100         # Call the to_model function by reference, if defined
101         if ( defined $filtered_params ) {
102
103             # Apply the mapping function to the passed params
104             $filtered_params = $orders_rs->attributes_from_api($filtered_params);
105             $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
106         }
107
108         if ( defined $path_params ) {
109
110             # Apply the mapping function to the passed params
111             $filtered_params //= {};
112             $path_params = $orders_rs->attributes_from_api($path_params);
113             foreach my $param (keys %{$path_params}) {
114                 $filtered_params->{$param} = $path_params->{$param};
115             }
116         }
117
118         if ( defined $reserved_params->{q} || defined $reserved_params->{query} || defined $reserved_params->{'x-koha-query'}) {
119             $filtered_params //={};
120             my @query_params_array;
121             my $query_params;
122
123             # FIXME The following lines are an ugly fix to deal with isbn and ean searches
124             # This must NOT be reused or extended
125             # Instead we need a better and global solution in a Koha::*Biblio method
126             for my $q ( qw( q query x-koha-query ) ) {
127                 next unless $reserved_params->{$q};
128                 for my $f ( qw( isbn ean publisher ) ) {
129                     $reserved_params->{$q} =~ s|"biblio.$f":|"biblio.biblioitem.$f":|g;
130                 }
131                 push @query_params_array, $reserved_params->{$q};
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         my $total  = $orders_rs->search->count;
147
148         $c->add_pagination_headers(
149             {
150                 total      => ($orders->is_paged ? $orders->pager->total_entries : $orders->count),
151                 base_total => $total,
152                 params     => $args,
153             }
154         );
155
156         return $c->render(
157             status  => 200,
158             openapi => $orders->to_api({ embed => $embed })
159         );
160     }
161     catch {
162         $c->unhandled_exception($_);
163     };
164 }
165
166 =head3 get
167
168 Controller function that handles retrieving a single Koha::Acquisition::Order object
169
170 =cut
171
172 sub get {
173     my $c = shift->openapi->valid_input or return;
174
175     my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
176
177     unless ($order) {
178         return $c->render(
179             status  => 404,
180             openapi => { error => "Order not found" }
181         );
182     }
183
184     return try {
185         my $embed = $c->stash('koha.embed');
186
187         return $c->render(
188             status  => 200,
189             openapi => $order->to_api({ embed => $embed })
190         );
191     }
192     catch {
193         $c->unhandled_exception($_);
194     };
195 }
196
197 =head3 add
198
199 Controller function that handles adding a new Koha::Acquisition::Order object
200
201 =cut
202
203 sub add {
204     my $c = shift->openapi->valid_input or return;
205
206     return try {
207         my $order = Koha::Acquisition::Order->new_from_api( $c->validation->param('body') );
208         $order->store->discard_changes;
209
210         $c->res->headers->location(
211             $c->req->url->to_string . '/' . $order->ordernumber
212         );
213
214         return $c->render(
215             status  => 201,
216             openapi => $order->to_api
217         );
218     }
219     catch {
220         if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
221             return $c->render(
222                 status  => 409,
223                 openapi => { error => $_->error, conflict => $_->duplicate_id }
224             );
225         }
226
227         $c->unhandled_exception($_);
228     };
229 }
230
231 =head3 update
232
233 Controller function that handles updating a Koha::Acquisition::Order object
234
235 =cut
236
237 sub update {
238     my $c = shift->openapi->valid_input or return;
239
240     my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
241
242     unless ($order) {
243         return $c->render(
244             status  => 404,
245             openapi => { error => "Order not found" }
246         );
247     }
248
249     return try {
250         $order->set_from_api( $c->validation->param('body') );
251         $order->store()->discard_changes;
252
253         return $c->render(
254             status  => 200,
255             openapi => $order->to_api
256         );
257     }
258     catch {
259         $c->unhandled_exception($_);
260     };
261 }
262
263 =head3 delete
264
265 Controller function that handles deleting a Koha::Patron object
266
267 =cut
268
269 sub delete {
270     my $c = shift->openapi->valid_input or return;
271
272     my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
273
274     unless ($order) {
275         return $c->render(
276             status  => 404,
277             openapi => { error => 'Order not found' }
278         );
279     }
280
281     return try {
282
283         $order->delete;
284
285         return $c->render(
286             status  => 204,
287             openapi => q{}
288         );
289     }
290     catch {
291         $c->unhandled_exception($_);
292     };
293 }
294
295 1;