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;
27 my $c = shift->openapi->valid_input or return;
29 my $args = _to_model($c->req->params->to_hash);
32 for my $filter_param ( keys %$args ) {
33 $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" }
34 if $args->{$filter_param};
40 @vendors = Koha::Acquisition::Booksellers->search($filter);
41 @vendors = map { _to_api($_->TO_JSON) } @vendors;
42 return $c->render( status => 200,
43 openapi => \@vendors );
46 if ( $_->isa('DBIx::Class::Exception') ) {
47 return $c->render( status => 500,
48 openapi => { error => $_->{msg} } );
51 return $c->render( status => 500,
52 openapi => { error => "Something went wrong, check the logs." } );
58 my $c = shift->openapi->valid_input or return;
60 my $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
62 return $c->render( status => 404,
63 openapi => { error => "Vendor not found" } );
66 return $c->render( status => 200,
67 openapi => _to_api($vendor->TO_JSON) );
71 my $c = shift->openapi->valid_input or return;
73 my $vendor = Koha::Acquisition::Bookseller->new( _to_model( $c->validation->param('body') ) );
77 return $c->render( status => 200,
78 openapi => _to_api($vendor->TO_JSON) );
81 if ( $_->isa('DBIx::Class::Exception') ) {
82 return $c->render( status => 500,
83 openapi => { error => $_->msg } );
86 return $c->render( status => 500,
87 openapi => { error => "Something went wrong, check the logs." } );
93 my $c = shift->openapi->valid_input or return;
98 $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
99 $vendor->set( _to_model( $c->validation->param('body') ) );
101 return $c->render( status => 200,
102 openapi => _to_api($vendor->TO_JSON) );
105 if ( not defined $vendor ) {
106 return $c->render( status => 404,
107 openapi => { error => "Object not found" } );
109 elsif ( $_->isa('Koha::Exceptions::Object') ) {
110 return $c->render( status => 500,
111 openapi => { error => $_->message } );
114 return $c->render( status => 500,
115 openapi => { error => "Something went wrong, check the logs." } );
122 my $c = shift->openapi->valid_input or return;
127 $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
129 return $c->render( status => 200,
133 if ( not defined $vendor ) {
134 return $c->render( status => 404,
135 openapi => { error => "Object not found" } );
137 elsif ( $_->isa('DBIx::Class::Exception') ) {
138 return $c->render( status => 500,
139 openapi => { error => $_->msg } );
142 return $c->render( status => 500,
143 openapi => { error => "Something went wrong, check the logs." } );
153 #my $vendor = $vendor_param->TO_JSON;
155 # Delete unused fields
156 delete $vendor->{booksellerfax};
157 delete $vendor->{bookselleremail};
158 delete $vendor->{booksellerurl};
159 delete $vendor->{currency};
160 delete $vendor->{othersupplier};
162 # Rename changed fields
163 $vendor->{list_currency} = $vendor->{listprice};
164 delete $vendor->{listprice};
165 $vendor->{invoice_currency} = $vendor->{invoiceprice};
166 delete $vendor->{invoiceprice};
167 $vendor->{gst} = $vendor->{gstreg};
168 delete $vendor->{gstreg};
169 $vendor->{list_includes_gst} = $vendor->{listincgst};
170 delete $vendor->{listincgst};
171 $vendor->{invoice_includes_gst} = $vendor->{invoiceincgst};
172 delete $vendor->{invoiceincgst};
178 my $vendor_param = shift;
180 my $vendor = $vendor_param;
183 $vendor->{listprice} = $vendor->{list_currency};
184 delete $vendor->{list_currency};
185 $vendor->{invoiceprice} = $vendor->{invoice_currency};
186 delete $vendor->{invoice_currency};
187 $vendor->{gstreg} = $vendor->{gst};
188 delete $vendor->{gst};
189 $vendor->{listincgst} = $vendor->{list_includes_gst};
190 delete $vendor->{list_includes_gst};
191 $vendor->{invoiceincgst} = $vendor->{invoice_includes_gst};
192 delete $vendor->{invoice_includes_gst};