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 );
26 use Scalar::Util qw( blessed );
27 use Try::Tiny qw( catch try );
31 Koha::REST::V1::Acquisitions::Orders
39 Controller function that handles listing Koha::Acquisition::Order objects
45 my $c = shift->openapi->valid_input or return;
49 my $only_active = delete $c->validation->output->{only_active};
50 my $order_id = delete $c->validation->output->{order_id};
55 $orders_rs = Koha::Acquisition::Orders->filter_by_active;
58 $orders_rs = Koha::Acquisition::Orders->new;
61 $orders_rs = $orders_rs->filter_by_id_including_transfers({ ordernumber => $order_id })
67 my $embed = $c->stash('koha.embed');
68 if ( exists $embed->{biblio} ) { # asked to embed biblio
69 my $fixed_embed = clone($embed);
70 # Add biblioitems to prefetch
71 # FIXME remove if we merge biblio + biblioitems
72 $fixed_embed->{biblio}->{children}->{biblioitem} = {};
73 $c->stash('koha.embed', $fixed_embed);
74 push @query_fixers, (sub{ Koha::Biblios->new->api_query_fixer( $_[0], 'biblio', $_[1] ) });
79 openapi => $c->objects->search( $orders_rs, \@query_fixers ),
83 $c->unhandled_exception($_);
89 Controller function that handles retrieving a single Koha::Acquisition::Order object
94 my $c = shift->openapi->valid_input or return;
96 my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
101 openapi => { error => "Order not found" }
106 my $embed = $c->stash('koha.embed');
110 openapi => $order->to_api({ embed => $embed })
114 $c->unhandled_exception($_);
120 Controller function that handles adding a new Koha::Acquisition::Order object
125 my $c = shift->openapi->valid_input or return;
128 my $order = Koha::Acquisition::Order->new_from_api( $c->validation->param('body') );
129 $order->store->discard_changes;
131 $c->res->headers->location(
132 $c->req->url->to_string . '/' . $order->ordernumber
137 openapi => $order->to_api
141 if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
144 openapi => { error => $_->error, conflict => $_->duplicate_id }
148 $c->unhandled_exception($_);
154 Controller function that handles updating a Koha::Acquisition::Order object
159 my $c = shift->openapi->valid_input or return;
161 my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
166 openapi => { error => "Order not found" }
171 $order->set_from_api( $c->validation->param('body') );
172 $order->store()->discard_changes;
176 openapi => $order->to_api
180 $c->unhandled_exception($_);
186 Controller function that handles deleting a Koha::Patron object
191 my $c = shift->openapi->valid_input or return;
193 my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
198 openapi => { error => 'Order not found' }
212 $c->unhandled_exception($_);
216 =head2 Internal methods
218 =head3 table_name_fixer
220 $q = $c->table_name_fixer( $q );
222 The Koha::Biblio representation includes the biblioitem.* attributes. This is handy
223 for API consumers but as they are different tables, converting the queries that mention
224 biblioitem columns can be tricky. This method renames known column names as used on Koha's
229 sub table_name_fixer {
230 my ( $self, $q ) = @_;
231 $q =~ s/biblio\.(?=isbn|ean|publisher)/biblio.biblioitem./g;