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};
54 @vendors = Koha::Acquisition::Booksellers->search($filter);
55 @vendors = map { _to_api($_->TO_JSON) } @vendors;
56 return $c->render( status => 200,
57 openapi => \@vendors );
60 if ( $_->isa('DBIx::Class::Exception') ) {
61 return $c->render( status => 500,
62 openapi => { error => $_->{msg} } );
65 return $c->render( status => 500,
66 openapi => { error => "Something went wrong, check the logs." } );
73 Controller function that handles retrieving a single Koha::Acquisition::Bookseller
78 my $c = shift->openapi->valid_input or return;
80 my $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
82 return $c->render( status => 404,
83 openapi => { error => "Vendor not found" } );
86 return $c->render( status => 200,
87 openapi => _to_api($vendor->TO_JSON) );
92 Controller function that handles adding a new Koha::Acquisition::Bookseller object
97 my $c = shift->openapi->valid_input or return;
99 my $vendor = Koha::Acquisition::Bookseller->new( _to_model( $c->validation->param('body') ) );
103 return $c->render( status => 200,
104 openapi => _to_api($vendor->TO_JSON) );
107 if ( $_->isa('DBIx::Class::Exception') ) {
108 return $c->render( status => 500,
109 openapi => { error => $_->msg } );
112 return $c->render( status => 500,
113 openapi => { error => "Something went wrong, check the logs." } );
120 Controller function that handles updating a Koha::Acquisition::Bookseller object
125 my $c = shift->openapi->valid_input or return;
130 $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
131 $vendor->set( _to_model( $c->validation->param('body') ) );
133 return $c->render( status => 200,
134 openapi => _to_api($vendor->TO_JSON) );
137 if ( not defined $vendor ) {
138 return $c->render( status => 404,
139 openapi => { error => "Object not found" } );
141 elsif ( $_->isa('Koha::Exceptions::Object') ) {
142 return $c->render( status => 500,
143 openapi => { error => $_->message } );
146 return $c->render( status => 500,
147 openapi => { error => "Something went wrong, check the logs." } );
155 Controller function that handles deleting a Koha::Acquisition::Bookseller object
160 my $c = shift->openapi->valid_input or return;
165 $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
167 return $c->render( status => 200,
171 if ( not defined $vendor ) {
172 return $c->render( status => 404,
173 openapi => { error => "Object not found" } );
175 elsif ( $_->isa('DBIx::Class::Exception') ) {
176 return $c->render( status => 500,
177 openapi => { error => $_->msg } );
180 return $c->render( status => 500,
181 openapi => { error => "Something went wrong, check the logs." } );
189 Helper function that maps a Koha::Acquisition::Bookseller object into
190 the attribute names the exposed REST api spec.
197 # Delete unused fields
198 delete $vendor->{booksellerfax};
199 delete $vendor->{bookselleremail};
200 delete $vendor->{booksellerurl};
201 delete $vendor->{currency};
202 delete $vendor->{othersupplier};
204 # Rename changed fields
205 $vendor->{list_currency} = delete $vendor->{listprice};
206 $vendor->{invoice_currency} = delete $vendor->{invoiceprice};
207 $vendor->{gst} = delete $vendor->{gstreg};
208 $vendor->{list_includes_gst} = delete $vendor->{listincgst};
209 $vendor->{invoice_includes_gst} = delete $vendor->{invoiceincgst};
216 Helper function that maps REST api objects into Koha::Acquisition::Bookseller
225 $vendor->{listprice} = delete $vendor->{list_currency};
226 $vendor->{invoiceprice} = delete $vendor->{invoice_currency};
227 $vendor->{gstreg} = delete $vendor->{gst};
228 $vendor->{listincgst} = delete $vendor->{list_includes_gst};
229 $vendor->{invoiceincgst} = delete $vendor->{invoice_includes_gst};