Bug 18120: Add '/acquisitions/vendors' endpoint
[koha.git] / Koha / REST / V1 / Acquisitions / Vendors.pm
1 package Koha::REST::V1::Acquisitions::Vendors;
2
3 # This file is part of Koha.
4 #
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
8 # version.
9 #
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.
13 #
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.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::Acquisition::Booksellers;
23
24 use Try::Tiny;
25
26 sub list_vendors {
27     my $c = shift->openapi->valid_input or return;
28
29     my $args = _to_model($c->req->params->to_hash);
30     my $filter;
31
32     for my $filter_param ( keys %$args ) {
33         $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" }
34             if $args->{$filter_param};
35     }
36
37     my @vendors;
38
39     return try {
40         @vendors = Koha::Acquisition::Booksellers->search($filter);
41         @vendors = map { _to_api($_->TO_JSON) } @vendors;
42         return $c->render( status  => 200,
43                            openapi => \@vendors );
44     }
45     catch {
46         if ( $_->isa('DBIx::Class::Exception') ) {
47             return $c->render( status  => 500,
48                                openapi => { error => $_->{msg} } );
49         }
50         else {
51             return $c->render( status  => 500,
52                                openapi => { error => "Something went wrong, check the logs." } );
53         }
54     };
55 }
56
57 sub get_vendor {
58     my $c = shift->openapi->valid_input or return;
59
60     my $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
61     unless ($vendor) {
62         return $c->render( status  => 404,
63                            openapi => { error => "Vendor not found" } );
64     }
65
66     return $c->render( status  => 200,
67                        openapi => _to_api($vendor->TO_JSON) );
68 }
69
70 sub add_vendor {
71     my $c = shift->openapi->valid_input or return;
72
73     my $vendor = Koha::Acquisition::Bookseller->new( _to_model( $c->validation->param('body') ) );
74
75     return try {
76         $vendor->store;
77         return $c->render( status  => 200,
78                            openapi => _to_api($vendor->TO_JSON) );
79     }
80     catch {
81         if ( $_->isa('DBIx::Class::Exception') ) {
82             return $c->render( status  => 500,
83                                openapi => { error => $_->msg } );
84         }
85         else {
86             return $c->render( status  => 500,
87                                openapi => { error => "Something went wrong, check the logs." } );
88         }
89     };
90 }
91
92 sub update_vendor {
93     my $c = shift->openapi->valid_input or return;
94
95     my $vendor;
96
97     return try {
98         $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
99         $vendor->set( _to_model( $c->validation->param('body') ) );
100         $vendor->store();
101         return $c->render( status  => 200,
102                            openapi => _to_api($vendor->TO_JSON) );
103     }
104     catch {
105         if ( not defined $vendor ) {
106             return $c->render( status  => 404,
107                                openapi => { error => "Object not found" } );
108         }
109         elsif ( $_->isa('Koha::Exceptions::Object') ) {
110             return $c->render( status  => 500,
111                                openapi => { error => $_->message } );
112         }
113         else {
114             return $c->render( status  => 500,
115                                openapi => { error => "Something went wrong, check the logs." } );
116         }
117     };
118
119 }
120
121 sub delete_vendor {
122     my $c = shift->openapi->valid_input or return;
123
124     my $vendor;
125
126     return try {
127         $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
128         $vendor->delete;
129         return $c->render( status => 200,
130                            openapi => q{} );
131     }
132     catch {
133         if ( not defined $vendor ) {
134             return $c->render( status  => 404,
135                                openapi => { error => "Object not found" } );
136         }
137         elsif ( $_->isa('DBIx::Class::Exception') ) {
138             return $c->render( status  => 500,
139                                openapi => { error => $_->msg } );
140         }
141         else {
142             return $c->render( status  => 500,
143                                openapi => { error => "Something went wrong, check the logs." } );
144         }
145     };
146
147 }
148
149 sub _to_api {
150
151     my $vendor = shift;
152
153     #my $vendor = $vendor_param->TO_JSON;
154
155     # Delete unused fields
156     delete $vendor->{booksellerfax};
157     delete $vendor->{bookselleremail};
158     delete $vendor->{booksellerurl};
159     delete $vendor->{currency};
160     delete $vendor->{othersupplier};
161
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};
173
174     return $vendor;
175 }
176
177 sub _to_model {
178     my $vendor_param = shift;
179
180     my $vendor = $vendor_param;
181
182     # Rename back
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};
193
194     return $vendor;
195 }
196
197 1;