1 package Koha::REST::V1::Acquisitions::Orders;
3 # This file is part of Koha.
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.
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.
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>.
20 use Mojo::Base 'Mojolicious::Controller';
22 use Koha::Acquisition::Orders;
26 use JSON qw(decode_json);
27 use Scalar::Util qw( blessed );
32 Koha::REST::V1::Acquisitions::Orders
40 Controller function that handles listing Koha::Acquisition::Order objects
46 my $c = shift->openapi->valid_input or return;
50 my $only_active = delete $c->validation->output->{only_active};
51 my $order_id = delete $c->validation->output->{order_id};
56 $orders_rs = Koha::Acquisition::Orders->filter_by_active;
59 $orders_rs = Koha::Acquisition::Orders->new;
62 $orders_rs = $orders_rs->filter_by_id_including_transfers({ ordernumber => $order_id })
65 my $args = $c->validation->output;
68 # Extract reserved params
69 my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
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);
80 # If no pagination parameters are passed, default
81 $reserved_params->{_per_page} //= C4::Context->preference('RESTdefaultPageSize');
82 $reserved_params->{_page} //= 1;
84 unless ( $reserved_params->{_per_page} == -1 ) {
85 # Merge pagination into query attributes
86 $c->dbic_merge_pagination(
88 filter => $attributes,
89 params => $reserved_params
94 # Generate prefetches for embedded stuff
95 $c->dbic_merge_prefetch(
97 attributes => $attributes,
98 result_set => $orders_rs
102 # Call the to_model function by reference, if defined
103 if ( defined $filtered_params ) {
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 );
110 if ( defined $path_params ) {
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};
120 if ( defined $reserved_params->{q} || defined $reserved_params->{query} || defined $reserved_params->{'x-koha-query'}) {
121 $filtered_params //={};
122 my @query_params_array;
124 if ( exists $reserved_params->{query} and defined $reserved_params->{query} ) {
125 push @query_params_array, fix_query({ query => $reserved_params->{query} });
127 if ( exists $reserved_params->{q} and defined $reserved_params->{q}) {
128 push @query_params_array, fix_query({ query => decode_json($reserved_params->{q}) });
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'}) });;
134 if(scalar(@query_params_array) > 1) {
135 $query_params = {'-and' => \@query_params_array};
138 $query_params = $query_params_array[0];
141 $filtered_params = $c->merge_q_params( $filtered_params, $query_params, $orders_rs );
145 my $orders = $orders_rs->search( $filtered_params, $attributes );
147 if ($orders->is_paged) {
148 $c->add_pagination_headers({
149 total => $orders->pager->total_entries,
154 $c->add_pagination_headers({
155 total => $orders->count,
162 openapi => $orders->to_api({ embed => $embed })
166 $c->unhandled_exception($_);
172 Controller function that handles retrieving a single Koha::Acquisition::Order object
177 my $c = shift->openapi->valid_input or return;
179 my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
184 openapi => { error => "Order not found" }
189 my $embed = $c->stash('koha.embed');
193 openapi => $order->to_api({ embed => $embed })
197 $c->unhandled_exception($_);
203 Controller function that handles adding a new Koha::Acquisition::Order object
208 my $c = shift->openapi->valid_input or return;
211 my $order = Koha::Acquisition::Order->new_from_api( $c->validation->param('body') );
212 $order->store->discard_changes;
214 $c->res->headers->location(
215 $c->req->url->to_string . '/' . $order->ordernumber
220 openapi => $order->to_api
224 if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
227 openapi => { error => $_->error, conflict => $_->duplicate_id }
231 $c->unhandled_exception($_);
237 Controller function that handles updating a Koha::Acquisition::Order object
242 my $c = shift->openapi->valid_input or return;
244 my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
249 openapi => { error => "Order not found" }
254 $order->set_from_api( $c->validation->param('body') );
255 $order->store()->discard_changes;
259 openapi => $order->to_api
263 $c->unhandled_exception($_);
269 Controller function that handles deleting a Koha::Patron object
274 my $c = shift->openapi->valid_input or return;
276 my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
281 openapi => { error => 'Order not found' }
295 $c->unhandled_exception($_);
299 =head2 Internal methods
303 my $query = fix_query($query);
305 This method takes care of recursively fixing queries that should be done
306 against biblioitems (instead if biblio as exposed on the API)
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',
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 })
354 $query->{$key} = fix_query({ query => $query->{$key} });
358 elsif ( ref($query) eq 'ARRAY' ) {
360 foreach my $item (@{$query}) {
361 push @accum, fix_query({ query => $item });
366 $query = $biblioitem_fields->{$query}
367 if exists $biblioitem_fields->{$query};