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;
24 use Clone qw( clone );
25 use Scalar::Util qw( blessed );
26 use Try::Tiny qw( catch try );
30 Koha::REST::V1::Acquisitions::Orders
38 Controller function that handles listing Koha::Acquisition::Order objects
44 my $c = shift->openapi->valid_input or return;
48 my $only_active = delete $c->validation->output->{only_active};
49 my $order_id = delete $c->validation->output->{order_id};
54 $orders_rs = Koha::Acquisition::Orders->filter_by_active;
57 $orders_rs = Koha::Acquisition::Orders->new;
60 $orders_rs = $orders_rs->filter_by_id_including_transfers({ ordernumber => $order_id })
63 my $args = $c->validation->output;
66 # Extract reserved params
67 my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
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);
78 # If no pagination parameters are passed, default
79 $reserved_params->{_per_page} //= C4::Context->preference('RESTdefaultPageSize');
80 $reserved_params->{_page} //= 1;
82 unless ( $reserved_params->{_per_page} == -1 ) {
83 # Merge pagination into query attributes
84 $c->dbic_merge_pagination(
86 filter => $attributes,
87 params => $reserved_params
92 # Generate prefetches for embedded stuff
93 $c->dbic_merge_prefetch(
95 attributes => $attributes,
96 result_set => $orders_rs
100 # Call the to_model function by reference, if defined
101 if ( defined $filtered_params ) {
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 );
108 if ( defined $path_params ) {
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};
118 if ( defined $reserved_params->{q} || defined $reserved_params->{query} || defined $reserved_params->{'x-koha-query'}) {
119 $filtered_params //={};
120 my @query_params_array;
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;
131 push @query_params_array, $reserved_params->{$q};
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 );
146 my $total = $orders_rs->search->count;
148 $c->add_pagination_headers(
150 total => ($orders->is_paged ? $orders->pager->total_entries : $orders->count),
151 base_total => $total,
158 openapi => $orders->to_api({ embed => $embed })
162 $c->unhandled_exception($_);
168 Controller function that handles retrieving a single Koha::Acquisition::Order object
173 my $c = shift->openapi->valid_input or return;
175 my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
180 openapi => { error => "Order not found" }
185 my $embed = $c->stash('koha.embed');
189 openapi => $order->to_api({ embed => $embed })
193 $c->unhandled_exception($_);
199 Controller function that handles adding a new Koha::Acquisition::Order object
204 my $c = shift->openapi->valid_input or return;
207 my $order = Koha::Acquisition::Order->new_from_api( $c->validation->param('body') );
208 $order->store->discard_changes;
210 $c->res->headers->location(
211 $c->req->url->to_string . '/' . $order->ordernumber
216 openapi => $order->to_api
220 if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
223 openapi => { error => $_->error, conflict => $_->duplicate_id }
227 $c->unhandled_exception($_);
233 Controller function that handles updating a Koha::Acquisition::Order object
238 my $c = shift->openapi->valid_input or return;
240 my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
245 openapi => { error => "Order not found" }
250 $order->set_from_api( $c->validation->param('body') );
251 $order->store()->discard_changes;
255 openapi => $order->to_api
259 $c->unhandled_exception($_);
265 Controller function that handles deleting a Koha::Patron object
270 my $c = shift->openapi->valid_input or return;
272 my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
277 openapi => { error => 'Order not found' }
291 $c->unhandled_exception($_);