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 # Merge sorting into query attributes
79 $c->dbic_merge_sorting(
81 attributes => $attributes,
82 params => $reserved_params,
83 result_set => $orders_rs,
87 # If no pagination parameters are passed, default
88 $reserved_params->{_per_page} //= C4::Context->preference('RESTdefaultPageSize');
89 $reserved_params->{_page} //= 1;
91 unless ( $reserved_params->{_per_page} == -1 ) {
92 # Merge pagination into query attributes
93 $c->dbic_merge_pagination(
95 filter => $attributes,
96 params => $reserved_params
101 # Generate prefetches for embedded stuff
102 $c->dbic_merge_prefetch(
104 attributes => $attributes,
105 result_set => $orders_rs
109 # Call the to_model function by reference, if defined
110 if ( defined $filtered_params ) {
112 # Apply the mapping function to the passed params
113 $filtered_params = $orders_rs->attributes_from_api($filtered_params);
114 $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
117 if ( defined $path_params ) {
119 # Apply the mapping function to the passed params
120 $filtered_params //= {};
121 $path_params = $orders_rs->attributes_from_api($path_params);
122 foreach my $param (keys %{$path_params}) {
123 $filtered_params->{$param} = $path_params->{$param};
127 if ( defined $reserved_params->{q} || defined $reserved_params->{query} || defined $reserved_params->{'x-koha-query'}) {
128 $filtered_params //={};
129 my @query_params_array;
132 # FIXME The following lines are an ugly fix to deal with isbn and ean searches
133 # This must NOT be reused or extended
134 # Instead we need a better and global solution in a Koha::*Biblio method
135 for my $q ( qw( q query x-koha-query ) ) {
136 next unless $reserved_params->{$q};
137 for my $f ( qw( isbn ean publisher ) ) {
138 $reserved_params->{$q} =~ s|"biblio.$f":|"biblio.biblioitem.$f":|g;
140 push @query_params_array, $reserved_params->{$q};
143 if(scalar(@query_params_array) > 1) {
144 $query_params = {'-and' => \@query_params_array};
147 $query_params = $query_params_array[0];
150 $filtered_params = $c->merge_q_params( $filtered_params, $query_params, $orders_rs );
154 my $orders = $orders_rs->search( $filtered_params, $attributes );
155 my $total = $orders_rs->search->count;
157 $c->add_pagination_headers(
159 total => ($orders->is_paged ? $orders->pager->total_entries : $orders->count),
160 base_total => $total,
167 openapi => $orders->to_api({ embed => $embed })
171 $c->unhandled_exception($_);
177 Controller function that handles retrieving a single Koha::Acquisition::Order object
182 my $c = shift->openapi->valid_input or return;
184 my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
189 openapi => { error => "Order not found" }
194 my $embed = $c->stash('koha.embed');
198 openapi => $order->to_api({ embed => $embed })
202 $c->unhandled_exception($_);
208 Controller function that handles adding a new Koha::Acquisition::Order object
213 my $c = shift->openapi->valid_input or return;
216 my $order = Koha::Acquisition::Order->new_from_api( $c->validation->param('body') );
217 $order->store->discard_changes;
219 $c->res->headers->location(
220 $c->req->url->to_string . '/' . $order->ordernumber
225 openapi => $order->to_api
229 if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
232 openapi => { error => $_->error, conflict => $_->duplicate_id }
236 $c->unhandled_exception($_);
242 Controller function that handles updating a Koha::Acquisition::Order object
247 my $c = shift->openapi->valid_input or return;
249 my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
254 openapi => { error => "Order not found" }
259 $order->set_from_api( $c->validation->param('body') );
260 $order->store()->discard_changes;
264 openapi => $order->to_api
268 $c->unhandled_exception($_);
274 Controller function that handles deleting a Koha::Patron object
279 my $c = shift->openapi->valid_input or return;
281 my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
286 openapi => { error => 'Order not found' }
300 $c->unhandled_exception($_);