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

(-)a/Koha/Acquisition/Order.pm (+46 lines)
Lines 263-268 sub duplicate_to { Link Here
263
    return $new_order;
263
    return $new_order;
264
}
264
}
265
265
266
=head3 to_api_mapping
267
268
This method returns the mapping for representing a Koha::Acquisition::Order object
269
on the API.
270
271
=cut
272
273
sub to_api_mapping {
274
    return {
275
        basketno                      => 'basket_id',
276
        biblionumber                  => 'biblio_id',
277
        budgetdate                    => undef,                    # unused
278
        cancellationreason            => 'cancellation_reason',
279
        claimed_date                  => 'last_claim_date',
280
        datecancellationprinted       => 'cancellation_date',
281
        datereceived                  => 'date_received',
282
        discount                      => 'discount_rate',
283
        entrydate                     => 'entry_date',
284
        freight                       => 'shipping_cost',
285
        invoiceid                     => 'invoice_id',
286
        line_item_id                  => undef,                    # EDIFACT related
287
        listprice                     => 'list_price',
288
        order_internalnote            => 'internal_note',
289
        order_vendornote              => 'vendor_note',
290
        ordernumber                   => 'order_id',
291
        orderstatus                   => 'status',
292
        parent_ordernumber            => 'parent_order_id',
293
        purchaseordernumber           => undef,                    # obsolete
294
        quantityreceived              => 'quantity_received',
295
        replacementprice              => 'replacement_price',
296
        sort1                         => 'statistics_1',
297
        sort1_authcat                 => 'statistics_1_authcat',
298
        sort2                         => 'statistics_2',
299
        sort2_authcat                 => 'statistics_2_authcat',
300
        subscriptionid                => 'subscription_id',
301
        suppliers_reference_number    => undef,                    # EDIFACT related
302
        suppliers_reference_qualifier => undef,                    # EDIFACT related
303
        suppliers_report              => undef,                    # EDIFACT related
304
        tax_rate_bak                  => undef,                    # unused
305
        tax_value_bak                 => undef,                    # unused
306
        uncertainprice                => 'uncertain_price',
307
        unitprice                     => 'unit_price',
308
        unitprice_tax_excluded        => 'unit_price_tax_excluded',
309
        unitprice_tax_included        => 'unit_price_tax_included'
310
    };
311
}
266
312
267
=head2 Internal methods
313
=head2 Internal methods
268
314
(-)a/Koha/REST/V1/Acquisitions/Orders.pm (+379 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
use Koha::DateUtils;
24
25
use Scalar::Util qw( blessed );
26
use Try::Tiny;
27
28
=head1 NAME
29
30
Koha::REST::V1::Acquisitions::Orders
31
32
=head1 API
33
34
=head2 Methods
35
36
=head3 list
37
38
Controller function that handles listing Koha::Acquisition::Order objects
39
40
=cut
41
42
sub list {
43
44
    my $c = shift->openapi->valid_input or return;
45
46
    return try {
47
        my $orders = $c->objects->search( Koha::Acquisition::Orders->new, \&_to_model, \&_to_api );
48
49
        return $c->render(
50
            status  => 200,
51
            openapi => $orders
52
        );
53
    }
54
    catch {
55
        if ( $_->isa('Koha::Exceptions::Exception') ) {
56
            return $c->render(
57
                status  => 500,
58
                openapi => { error => "Unhandled exception ($_)" }
59
            );
60
        }
61
        else {
62
            return $c->render(
63
                status  => 500,
64
                openapi => { error => "Something went wrong, check the logs." }
65
            );
66
        }
67
    };
68
}
69
70
=head3 get
71
72
Controller function that handles retrieving a single Koha::Aquisition::Order object
73
74
=cut
75
76
sub get {
77
    my $c = shift->openapi->valid_input or return;
78
79
    my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
80
81
    unless ($order) {
82
        return $c->render(
83
            status  => 404,
84
            openapi => { error => "Order not found" }
85
        );
86
    }
87
88
    return $c->render(
89
        status  => 200,
90
        openapi => $order->to_api
91
    );
92
}
93
94
=head3 add
95
96
Controller function that handles adding a new Koha::Acquisition::Order object
97
98
=cut
99
100
sub add {
101
    my $c = shift->openapi->valid_input or return;
102
103
    return try {
104
        my $order = Koha::Acquisition::Order->new( _to_model( $c->validation->param('body') ) );
105
        $order->store->discard_changes;
106
107
        $c->res->headers->location(
108
            $c->req->url->to_string . '/' . $order->ordernumber
109
        );
110
111
        return $c->render(
112
            status  => 201,
113
            openapi => $order->to_api
114
        );
115
    }
116
    catch {
117
        unless ( blessed $_ && $_->can('rethrow') ) {
118
            return $c->render(
119
                status  => 500,
120
                openapi => {
121
                    error =>
122
                      "Something went wrong, check Koha logs for details."
123
                }
124
            );
125
        }
126
        if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
127
            return $c->render(
128
                status  => 409,
129
                openapi => { error => $_->error, conflict => $_->duplicate_id }
130
            );
131
        }
132
        else {
133
            return $c->render(
134
                status  => 500,
135
                openapi => { error => "$_" }
136
            );
137
        }
138
    };
139
}
140
141
=head3 update
142
143
Controller function that handles updating a Koha::Acquisition::Order object
144
145
=cut
146
147
sub update {
148
    my $c = shift->openapi->valid_input or return;
149
150
    my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
151
152
    unless ($order) {
153
        return $c->render(
154
            status  => 404,
155
            openapi => { error => "Order not found" }
156
        );
157
    }
158
159
    return try {
160
        $order->set( _to_model($c->validation->param('body')) );
161
        $order->store()->discard_changes;
162
163
        return $c->render(
164
            status  => 200,
165
            openapi => $order->to_api
166
        );
167
    }
168
    catch {
169
        if ( $_->isa('Koha::Exceptions::Exception') ) {
170
            return $c->render(
171
                status  => 500,
172
                openapi => { error => "Unhandled exception ($_)" }
173
            );
174
        }
175
        else {
176
            return $c->render(
177
                status  => 500,
178
                openapi => { error => "Something went wrong, check the logs." }
179
            );
180
        }
181
    };
182
}
183
184
=head3 delete
185
186
Controller function that handles deleting a Koha::Patron object
187
188
=cut
189
190
sub delete {
191
    my $c = shift->openapi->valid_input or return;
192
193
    my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
194
195
    unless ($order) {
196
        return $c->render(
197
            status  => 404,
198
            openapi => { error => 'Order not found' }
199
        );
200
    }
201
202
    return try {
203
204
        $order->delete;
205
206
        return $c->render(
207
            status  => 204,
208
            openapi => q{}
209
        );
210
    }
211
    catch {
212
        if ( $_->isa('Koha::Exceptions::Exception') ) {
213
            return $c->render(
214
                status  => 500,
215
                openapi => { error => "Unhandled exception ($_)" }
216
            );
217
        }
218
        else {
219
            return $c->render(
220
                status  => 500,
221
                openapi => { error => "Something went wrong, check the logs." }
222
            );
223
        }
224
    };
225
}
226
227
=head3 _to_api
228
229
Helper function that maps unblessed Koha::Acquisition::Order objects into REST api
230
attribute names.
231
232
=cut
233
234
sub _to_api {
235
    my $order = shift;
236
237
    # Rename attributes
238
    foreach my $column ( keys %{ $Koha::REST::V1::Acquisitions::Orders::to_api_mapping } ) {
239
        my $mapped_column = $Koha::REST::V1::Acquisitions::Orders::to_api_mapping->{$column};
240
        if (    exists $order->{ $column }
241
             && defined $mapped_column )
242
        {
243
            # key != undef
244
            $order->{ $mapped_column } = delete $order->{ $column };
245
        }
246
        elsif (    exists $order->{ $column }
247
                && !defined $mapped_column )
248
        {
249
            # key == undef
250
            delete $order->{ $column };
251
        }
252
    }
253
254
    return $order;
255
}
256
257
=head3 _to_model
258
259
Helper function that maps REST api objects into Koha::Acquisition::Order
260
attribute names.
261
262
=cut
263
264
sub _to_model {
265
    my $order = shift;
266
267
    foreach my $attribute ( keys %{ $Koha::REST::V1::Acquisitions::Orders::to_model_mapping } ) {
268
        my $mapped_attribute = $Koha::REST::V1::Acquisitions::Orders::to_model_mapping->{$attribute};
269
        if (    exists $order->{ $attribute }
270
             && defined $mapped_attribute )
271
        {
272
            # key => !undef
273
            $order->{ $mapped_attribute } = delete $order->{ $attribute };
274
        }
275
        elsif (    exists $order->{ $attribute }
276
                && !defined $mapped_attribute )
277
        {
278
            # key => undef / to be deleted
279
            delete $order->{ $attribute };
280
        }
281
    }
282
283
    my @date_fields = (
284
        'datecancellationprinted', 'budgetdate',
285
        'claimed_date',            'entrydate',
286
        'datereceived',            'timestamp'
287
    );
288
    foreach my $field (@date_fields) {
289
        $order->{$field} = output_pref( { str => $order->{$field}, dateformat => 'sql' } )
290
            if exists $order->{$field};
291
    }
292
293
    $order->{uncertainprice} = ( $order->{uncertainprice} ) ? 1 : 0
294
        if exists $order->{uncertainprice};
295
296
    return $order;
297
}
298
299
=head2 Global variables
300
301
=head3 $to_api_mapping
302
303
=cut
304
305
our $to_api_mapping = {
306
    basketno                      => 'basket_id',
307
    biblionumber                  => 'biblio_id',
308
    budgetdate                    => undef,                    # unused
309
    cancellationreason            => 'cancellation_reason',
310
    claimed_date                  => 'last_claim_date',
311
    datecancellationprinted       => 'cancellation_date',
312
    datereceived                  => 'date_received',
313
    discount                      => 'discount_rate',
314
    entrydate                     => 'entry_date',
315
    freight                       => 'shipping_cost',
316
    invoiceid                     => 'invoice_id',
317
    line_item_id                  => undef,                    # EDIFACT related
318
    listprice                     => 'list_price',
319
    order_internalnote            => 'internal_note',
320
    order_vendornote              => 'vendor_note',
321
    ordernumber                   => 'order_id',
322
    orderstatus                   => 'status',
323
    parent_ordernumber            => 'parent_order_id',
324
    purchaseordernumber           => undef,                    # obsolete
325
    quantityreceived              => 'quantity_received',
326
    replacementprice              => 'replacement_price',
327
    sort1                         => 'statistics_1',
328
    sort1_authcat                 => 'statistics_1_authcat',
329
    sort2                         => 'statistics_2',
330
    sort2_authcat                 => 'statistics_2_authcat',
331
    subscriptionid                => 'subscription_id',
332
    suppliers_reference_number    => undef,                    # EDIFACT related
333
    suppliers_reference_qualifier => undef,                    # EDIFACT related
334
    suppliers_report              => undef,                    # EDIFACT related
335
    tax_rate_bak                  => undef,                    # unused
336
    tax_value_bak                 => undef,                    # unused
337
    uncertainprice                => 'uncertain_price',
338
    unitprice                     => 'unit_price',
339
    unitprice_tax_excluded        => 'unit_price_tax_excluded',
340
    unitprice_tax_included        => 'unit_price_tax_included'
341
342
};
343
344
345
=head3 $to_model_mapping
346
347
=cut
348
349
our $to_model_mapping = {
350
    basket_id               => 'basketno',
351
    biblio_id               => 'biblionumber',
352
    last_claim_date         => 'claimed_date',
353
    cancellation_date       => 'datecacellationprinted',
354
    discount_rate           => 'discount',
355
    cancellation_reason     => 'cancellationreason',
356
    date_received           => 'datereceived',
357
    entry_date              => 'entrydate',
358
    shipping_cost           => 'freight',
359
    invoice_id              => 'invoiceid',
360
    list_price              => 'listprice',
361
    internal_note           => 'order_internalnote',
362
    vendor_note             => 'order_vendornote',
363
    order_id                => 'ordernumber',
364
    status                  => 'orderstatus',
365
    parent_order_id         => 'parent_ordernumber',
366
    quantity_received       => 'quantityreceived',
367
    statistics_1            => 'sort1',
368
    statistics_1_authcat    => 'sort1_authcat',
369
    statistics_2            => 'sort2',
370
    statistics_2_authcat    => 'sort2_authcat',
371
    subscription_id         => 'subscriptionid',
372
    uncertain_price         => 'uncertainprice',
373
    unit_price              => 'unitprice',
374
    unit_price_tax_excluded => 'unitprice_tax_excluded',
375
    unit_price_tax_included => 'unitprice_tax_included'
376
};
377
378
379
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