Line 0
Link Here
|
|
|
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::Bookseller; |
23 |
use Koha::Acquisition::Booksellers; |
24 |
|
25 |
use Try::Tiny; |
26 |
|
27 |
sub list { |
28 |
my ( $c, $args, $cb ) = @_; |
29 |
|
30 |
my @vendors; |
31 |
|
32 |
return try { |
33 |
@vendors = map { _to_api($_) } Koha::Acquisition::Booksellers->search(); |
34 |
return $c->$cb( \@vendors, 200 ); |
35 |
} |
36 |
catch { |
37 |
if ( $_->isa('DBIx::Class::Exception') ) { |
38 |
return $c->$cb( { error => $_->{msg} }, 500 ); |
39 |
} |
40 |
else { |
41 |
return $c->$cb( { error => "Something went wrong, check the logs." }, 500 ); |
42 |
} |
43 |
}; |
44 |
} |
45 |
|
46 |
sub get { |
47 |
my ( $c, $args, $cb ) = @_; |
48 |
|
49 |
my $vendor = Koha::Acquisition::Booksellers->find( $args->{vendor_id} ); |
50 |
unless ($vendor) { |
51 |
return $c->$cb( { error => "Vendor not found" }, 404 ); |
52 |
} |
53 |
|
54 |
return $c->$cb( _to_api($vendor), 200 ); |
55 |
} |
56 |
|
57 |
sub add { |
58 |
my ( $c, $args, $cb ) = @_; |
59 |
|
60 |
my $vendor = Koha::Acquisition::Bookseller->new( _to_model( $args->{body} ) ); |
61 |
|
62 |
return try { |
63 |
$vendor->store; |
64 |
return $c->$cb( _to_api($vendor), 200 ); |
65 |
} |
66 |
catch { |
67 |
if ( $_->isa('DBIx::Class::Exception') ) { |
68 |
return $c->$cb( { error => $_->msg }, 500 ); |
69 |
} |
70 |
else { |
71 |
return $c->$cb( { error => "Something went wrong, check the logs." }, 500 ); |
72 |
} |
73 |
}; |
74 |
} |
75 |
|
76 |
sub update { |
77 |
my ( $c, $args, $cb ) = @_; |
78 |
|
79 |
my $vendor; |
80 |
|
81 |
return try { |
82 |
$vendor = Koha::Acquisition::Booksellers->find( $args->{vendor_id} ); |
83 |
$vendor->set( _to_model( $args->{body} ) ); |
84 |
$vendor->store(); |
85 |
return $c->$cb( _to_api($vendor), 200 ); |
86 |
} |
87 |
catch { |
88 |
if ( not defined $vendor ) { |
89 |
return $c->$cb( { error => "Object not found" }, 404 ); |
90 |
} |
91 |
elsif ( $_->isa('Koha::Exceptions::Object') ) { |
92 |
return $c->$cb( { error => $_->message }, 500 ); |
93 |
} |
94 |
else { |
95 |
return $c->$cb( { error => "Something went wrong, check the logs." }, 500 ); |
96 |
} |
97 |
}; |
98 |
|
99 |
} |
100 |
|
101 |
sub delete { |
102 |
my ( $c, $args, $cb ) = @_; |
103 |
|
104 |
my $vendor; |
105 |
|
106 |
return try { |
107 |
$vendor = Koha::Acquisition::Booksellers->find( $args->{vendor_id} ); |
108 |
$vendor->delete; |
109 |
return $c->$cb( q{}, 200 ); |
110 |
} |
111 |
catch { |
112 |
if ( not defined $vendor ) { |
113 |
return $c->$cb( { error => "Object not found" }, 404 ); |
114 |
} |
115 |
elsif ( $_->isa('DBIx::Class::Exception') ) { |
116 |
return $c->$cb( { error => $_->msg }, 500 ); |
117 |
} |
118 |
else { |
119 |
return $c->$cb( { error => "Something went wrong, check the logs." }, 500 ); |
120 |
} |
121 |
}; |
122 |
|
123 |
} |
124 |
|
125 |
sub _to_api { |
126 |
|
127 |
my $vendor_param = shift; |
128 |
|
129 |
my $vendor = $vendor_param->TO_JSON; |
130 |
|
131 |
# Delete unused fields |
132 |
delete $vendor->{booksellerfax}; |
133 |
delete $vendor->{bookselleremail}; |
134 |
delete $vendor->{booksellerurl}; |
135 |
delete $vendor->{currency}; |
136 |
delete $vendor->{othersupplier}; |
137 |
|
138 |
# Rename changed fields |
139 |
$vendor->{list_currency} = $vendor->{listprice}; |
140 |
delete $vendor->{listprice}; |
141 |
$vendor->{invoice_currency} = $vendor->{invoiceprice}; |
142 |
delete $vendor->{invoiceprice}; |
143 |
$vendor->{gst} = $vendor->{gstreg}; |
144 |
delete $vendor->{gstreg}; |
145 |
$vendor->{list_includes_gst} = $vendor->{listincgst}; |
146 |
delete $vendor->{listincgst}; |
147 |
$vendor->{invoice_includes_gst} = $vendor->{invoiceincgst}; |
148 |
delete $vendor->{invoiceincgst}; |
149 |
|
150 |
return $vendor; |
151 |
} |
152 |
|
153 |
sub _to_model { |
154 |
my $vendor_param = shift; |
155 |
|
156 |
my $vendor = $vendor_param; |
157 |
|
158 |
# Rename back |
159 |
$vendor->{listprice} = $vendor->{list_currency}; |
160 |
delete $vendor->{list_currency}; |
161 |
$vendor->{invoiceprice} = $vendor->{invoice_currency}; |
162 |
delete $vendor->{invoice_currency}; |
163 |
$vendor->{gstreg} = $vendor->{gst}; |
164 |
delete $vendor->{gst}; |
165 |
$vendor->{listincgst} = $vendor->{list_includes_gst}; |
166 |
delete $vendor->{list_includes_gst}; |
167 |
$vendor->{invoiceincgst} = $vendor->{invoice_includes_gst}; |
168 |
delete $vendor->{invoice_includes_gst}; |
169 |
|
170 |
return $vendor; |
171 |
} |
172 |
|
173 |
1; |