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;
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;
133 push @query_params_array, $reserved_params->{$q};
136 if(scalar(@query_params_array) > 1) {
137 $query_params = {'-and' => \@query_params_array};
140 $query_params = $query_params_array[0];
143 $filtered_params = $c->merge_q_params( $filtered_params, $query_params, $orders_rs );
147 my $orders = $orders_rs->search( $filtered_params, $attributes );
148 my $total = $orders_rs->search->count;
150 $c->add_pagination_headers(
152 total => ($orders->is_paged ? $orders->pager->total_entries : $orders->count),
153 base_total => $total,
160 openapi => $orders->to_api({ embed => $embed })
164 $c->unhandled_exception($_);
170 Controller function that handles retrieving a single Koha::Acquisition::Order object
175 my $c = shift->openapi->valid_input or return;
177 my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
182 openapi => { error => "Order not found" }
187 my $embed = $c->stash('koha.embed');
191 openapi => $order->to_api({ embed => $embed })
195 $c->unhandled_exception($_);
201 Controller function that handles adding a new Koha::Acquisition::Order object
206 my $c = shift->openapi->valid_input or return;
209 my $order = Koha::Acquisition::Order->new_from_api( $c->validation->param('body') );
210 $order->store->discard_changes;
212 $c->res->headers->location(
213 $c->req->url->to_string . '/' . $order->ordernumber
218 openapi => $order->to_api
222 if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
225 openapi => { error => $_->error, conflict => $_->duplicate_id }
229 $c->unhandled_exception($_);
235 Controller function that handles updating a Koha::Acquisition::Order object
240 my $c = shift->openapi->valid_input or return;
242 my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
247 openapi => { error => "Order not found" }
252 $order->set_from_api( $c->validation->param('body') );
253 $order->store()->discard_changes;
257 openapi => $order->to_api
261 $c->unhandled_exception($_);
267 Controller function that handles deleting a Koha::Patron object
272 my $c = shift->openapi->valid_input or return;
274 my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
279 openapi => { error => 'Order not found' }
293 $c->unhandled_exception($_);