View | Details | Raw Unified | Return to bug 18731
Collapse All | Expand All

(-)a/Koha/REST/V1/Acquisitions/Orders.pm (+308 lines)
Line 0 Link Here
1
package Koha::REST::V1::Acquisitions::Orders;
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::Orders;
23
24
use Try::Tiny;
25
26
sub list {
27
28
    my $c = shift->openapi->valid_input or return;
29
30
    return try {
31
        my $orders = $c->objects->search( Koha::Acquisition::Orders->new, \&_to_model, \&_to_api );
32
33
        return $c->render(
34
            status  => 200,
35
            openapi => $orders
36
        );
37
    }
38
    catch {
39
        if ( $_->isa('DBIx::Class::Exception') ) {
40
            return $c->render(
41
                status  => 500,
42
                openapi => { error => $_->{msg} }
43
            );
44
        }
45
        else {
46
            return $c->render(
47
                status  => 500,
48
                openapi => { error => "Something went wrong, check the logs." }
49
            );
50
        }
51
    };
52
}
53
54
sub get {
55
    my $c = shift->openapi->valid_input or return;
56
57
    my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
58
59
    unless ($order) {
60
        return $c->render(
61
            status  => 404,
62
            openapi => { error => "Order not found" }
63
        );
64
    }
65
66
    return $c->render(
67
        status  => 200,
68
        openapi => _to_api( $order->TO_JSON )
69
    );
70
}
71
72
sub add {
73
    my $c = shift->openapi->valid_input or return;
74
75
    my $order = Koha::Acquisition::Order->new( _to_model($c->validation->param('body')) );
76
77
    return try {
78
        $order->store;
79
        return $c->render(
80
            status  => 200,
81
            openapi => $order
82
        );
83
    }
84
    catch {
85
        if ( $_->isa('DBIx::Class::Exception') ) {
86
            return $c->render(
87
                status  => 500,
88
                openapi => { error => $_->msg }
89
            );
90
        }
91
        else {
92
            return $c->render(
93
                status  => 500,
94
                openapi => { error => "Something went wrong, check the logs." }
95
            );
96
        }
97
    };
98
}
99
100
sub update {
101
    my $c = shift->openapi->valid_input or return;
102
103
    my $order;
104
105
    return try {
106
        $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
107
        $order->set( _to_model($c->validation->param('body')) );
108
        $order->store();
109
        return $c->render(
110
            status  => 200,
111
            openapi => _to_api( $order->TO_JSON )
112
        );
113
    }
114
    catch {
115
        if ( not defined $order ) {
116
            return $c->render(
117
                status  => 404,
118
                openapi => { error => "Object not found" }
119
            );
120
        }
121
        elsif ( $_->isa('Koha::Exceptions::Object') ) {
122
            return $c->render(
123
                status  => 500,
124
                openapi => { error => $_->message }
125
            );
126
        }
127
        else {
128
            return $c->render(
129
                status  => 500,
130
                openapi => { error => "Something went wrong, check the logs." }
131
            );
132
        }
133
    };
134
}
135
136
sub delete {
137
    my $c = shift->openapi->valid_input or return;
138
139
    my $order;
140
141
    return try {
142
        $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
143
        $order->delete;
144
        return $c->render(
145
            status  => 200,
146
            openapi => q{}
147
        );
148
    }
149
    catch {
150
        if ( not defined $order ) {
151
            return $c->render(
152
                status  => 404,
153
                openapi => { error => "Object not found" }
154
            );
155
        }
156
        elsif ( $_->isa('DBIx::Class::Exception') ) {
157
            return $c->render(
158
                status  => 500,
159
                openapi => { error => $_->msg }
160
            );
161
        }
162
        else {
163
            return $c->render(
164
                status  => 500,
165
                openapi => { error => "Something went wrong, check the logs." }
166
            );
167
        }
168
    };
169
}
170
171
=head3 _to_api
172
173
Helper function that maps unblessed Koha::Acquisition::Order objects into REST api
174
attribute names.
175
176
=cut
177
178
sub _to_api {
179
    my $order = shift;
180
181
    # Rename attributes
182
    foreach my $column ( keys %{ $Koha::REST::V1::Acquisitions::Orders::to_api_mapping } ) {
183
        my $mapped_column = $Koha::REST::V1::Acquisitions::Orders::to_api_mapping->{$column};
184
        if (    exists $order->{ $column }
185
             && defined $mapped_column )
186
        {
187
            # key != undef
188
            $order->{ $mapped_column } = delete $order->{ $column };
189
        }
190
        elsif (    exists $order->{ $column }
191
                && !defined $mapped_column )
192
        {
193
            # key == undef
194
            delete $order->{ $column };
195
        }
196
    }
197
198
    return $order;
199
}
200
201
=head3 _to_model
202
203
Helper function that maps REST api objects into Koha::Acquisition::Order
204
attribute names.
205
206
=cut
207
208
sub _to_model {
209
    my $order = shift;
210
211
    foreach my $attribute ( keys %{ $Koha::REST::V1::Acquisitions::Orders::to_model_mapping } ) {
212
        my $mapped_attribute = $Koha::REST::V1::Acquisitions::Orders::to_model_mapping->{$attribute};
213
        if (    exists $order->{ $attribute }
214
             && defined $mapped_attribute )
215
        {
216
            # key => !undef
217
            $order->{ $mapped_attribute } = delete $order->{ $attribute };
218
        }
219
        elsif (    exists $order->{ $attribute }
220
                && !defined $mapped_attribute )
221
        {
222
            # key => undef / to be deleted
223
            delete $order->{ $attribute };
224
        }
225
    }
226
227
    return $order;
228
}
229
230
=head2 Global variables
231
232
=head3 $to_api_mapping
233
234
=cut
235
236
our $to_api_mapping = {
237
    basketno                      => 'basket_id',
238
    biblionumber                  => 'biblio_id',
239
    budgetdate                    => undef,                       # unused
240
    cancellationreason            => 'cancellation_reason',
241
    claimed_date                  => 'last_claim_date',
242
    datecacellationprinted        => 'cancellation_date',
243
    datereceived                  => 'date_received',
244
    discount                      => 'discount_rate',
245
    entrydate                     => 'entry_date',
246
    freight                       => 'shipping_cost',
247
    invoiceid                     => 'invoice_id',
248
    line_item_id                  => undef,                       # EDIFACT related
249
    listprice                     => 'list_price',
250
    order_internalnote            => 'internal_note',
251
    order_vendornote              => 'vendor_note',
252
    ordernumber                   => 'order_id',
253
    orderstatus                   => 'status',
254
    parent_ordernumber            => 'parent_order_id',
255
    purchaseordernumber           => undef,                       # obsolete
256
    quantityreceived              => 'quantity_received',
257
    sort1                         => 'statistics_1',
258
    sort1_authcat                 => 'statistics_1_authcat',
259
    sort2                         => 'statistics_2',
260
    sort2_authcat                 => 'statistics_2_authcat',
261
    subscriptionid                => 'subscription_id',
262
    suppliers_reference_number    => undef,                       # EDIFACT related
263
    suppliers_reference_qualifier => undef,                       # EDIFACT related
264
    suppliers_report              => undef,                       # EDIFACT related
265
    tax_rate_bak                  => undef,                       # unused
266
    tax_value_bak                 => undef,                       # unused
267
    uncertainprice                => 'uncertain_price',
268
    unitprice                     => 'unit_price',
269
    unitprice_tax_excluded        => 'unit_price_tax_excluded',
270
    unitprice_tax_included        => 'unit_price_tax_included'
271
};
272
273
274
=head3 $to_model_mapping
275
276
=cut
277
278
our $to_model_mapping = {
279
    basket_id               => 'basketno',
280
    biblio_id               => 'biblionumber',
281
    last_claim_date         => 'claimed_date',
282
    cancellation_date       => 'datecacellationprinted',
283
    discount_rate           => 'discount',
284
    cancellation_reason     => 'cancellationreason',
285
    date_received           => 'datereceived',
286
    entry_date              => 'entrydate',
287
    shipping_cost           => 'freight',
288
    invoice_id              => 'invoiceid',
289
    list_price              => 'listprice',
290
    internal_note           => 'order_internalnote',
291
    vendor_note             => 'order_vendornote',
292
    order_id                => 'ordernumber',
293
    status                  => 'orderstatus',
294
    parent_order_id         => 'parent_ordernumber',
295
    quantity_received       => 'quantityreceived',
296
    statistics_1            => 'sort1',
297
    statistics_1_authcat    => 'sort1_authcat',
298
    statistics_2            => 'sort2',
299
    statistics_2_authcat    => 'sort2_authcat',
300
    subscription_id         => 'subscriptionid',
301
    uncertain_price         => 'uncertainprice',
302
    unit_price              => 'unitprice',
303
    unit_price_tax_excluded => 'unitprice_tax_excluded',
304
    unit_price_tax_included => 'unitprice_tax_included'
305
};
306
307
308
1;
(-)a/Koha/Schema/Result/Aqorder.pm (-1 / +3 lines)
Lines 670-674 sub koha_objects_class { Link Here
670
sub koha_object_class {
670
sub koha_object_class {
671
    'Koha::Acquisition::Order';
671
    'Koha::Acquisition::Order';
672
}
672
}
673
__PACKAGE__->add_columns(
674
    '+uncertainprice' => { is_boolean => 1 }
675
);
673
676
674
1;
677
1;
675
- 

Return to bug 18731