1 package Koha::REST::V1::Acquisitions::Vendors;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Mojo::Base 'Mojolicious::Controller';
22 use Koha::Acquisition::Booksellers;
28 Koha::REST::V1::Acquisitions::Vendors
36 Controller function that handles listing Koha::Acquisition::Bookseller objects
41 my $c = shift->openapi->valid_input or return;
43 my $args = _to_model($c->req->params->to_hash);
46 for my $filter_param ( keys %$args ) {
47 $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" }
48 if $args->{$filter_param};
52 my $vendors = Koha::Acquisition::Booksellers->search($filter);
55 openapi => $vendors->to_api
59 if ( $_->isa('DBIx::Class::Exception') ) {
60 return $c->render( status => 500,
61 openapi => { error => $_->{msg} } );
64 return $c->render( status => 500,
65 openapi => { error => "Something went wrong, check the logs." } );
72 Controller function that handles retrieving a single Koha::Acquisition::Bookseller
77 my $c = shift->openapi->valid_input or return;
79 my $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
81 return $c->render( status => 404,
82 openapi => { error => "Vendor not found" } );
87 openapi => $vendor->to_api
93 Controller function that handles adding a new Koha::Acquisition::Bookseller object
98 my $c = shift->openapi->valid_input or return;
100 my $vendor = Koha::Acquisition::Bookseller->new( _to_model( $c->validation->param('body') ) );
104 $c->res->headers->location($c->req->url->to_string . '/' . $vendor->id );
107 openapi => $vendor->to_api
111 if ( $_->isa('DBIx::Class::Exception') ) {
112 return $c->render( status => 500,
113 openapi => { error => $_->msg } );
116 return $c->render( status => 500,
117 openapi => { error => "Something went wrong, check the logs." } );
124 Controller function that handles updating a Koha::Acquisition::Bookseller object
129 my $c = shift->openapi->valid_input or return;
134 $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
135 $vendor->set( _to_model( $c->validation->param('body') ) );
139 openapi => $vendor->to_api
143 if ( not defined $vendor ) {
144 return $c->render( status => 404,
145 openapi => { error => "Object not found" } );
147 elsif ( $_->isa('Koha::Exceptions::Object') ) {
148 return $c->render( status => 500,
149 openapi => { error => $_->message } );
152 return $c->render( status => 500,
153 openapi => { error => "Something went wrong, check the logs." } );
161 Controller function that handles deleting a Koha::Acquisition::Bookseller object
166 my $c = shift->openapi->valid_input or return;
171 $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
173 return $c->render( status => 200,
177 if ( not defined $vendor ) {
178 return $c->render( status => 404,
179 openapi => { error => "Object not found" } );
181 elsif ( $_->isa('DBIx::Class::Exception') ) {
182 return $c->render( status => 500,
183 openapi => { error => $_->msg } );
186 return $c->render( status => 500,
187 openapi => { error => "Something went wrong, check the logs." } );
195 Helper function that maps a Koha::Acquisition::Bookseller object into
196 the attribute names the exposed REST api spec.
203 # Delete unused fields
204 delete $vendor->{booksellerfax};
205 delete $vendor->{bookselleremail};
206 delete $vendor->{booksellerurl};
207 delete $vendor->{currency};
208 delete $vendor->{othersupplier};
210 # Rename changed fields
211 $vendor->{list_currency} = delete $vendor->{listprice};
212 $vendor->{invoice_currency} = delete $vendor->{invoiceprice};
213 $vendor->{gst} = delete $vendor->{gstreg};
214 $vendor->{list_includes_gst} = delete $vendor->{listincgst};
215 $vendor->{invoice_includes_gst} = delete $vendor->{invoiceincgst};
222 Helper function that maps REST api objects into Koha::Acquisition::Bookseller
231 $vendor->{listprice} = delete $vendor->{list_currency};
232 $vendor->{invoiceprice} = delete $vendor->{invoice_currency};
233 $vendor->{gstreg} = delete $vendor->{gst};
234 $vendor->{listincgst} = delete $vendor->{list_includes_gst};
235 $vendor->{invoiceincgst} = delete $vendor->{invoice_includes_gst};