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

(-)a/Koha/REST/V1/Acquisitions/Orders.pm (-2 / +161 lines)
Lines 22-27 use Mojo::Base 'Mojolicious::Controller'; Link Here
22
use Koha::Acquisition::Orders;
22
use Koha::Acquisition::Orders;
23
use Koha::DateUtils;
23
use Koha::DateUtils;
24
24
25
use Clone 'clone';
26
use JSON qw(decode_json);
25
use Scalar::Util qw( blessed );
27
use Scalar::Util qw( blessed );
26
use Try::Tiny;
28
use Try::Tiny;
27
29
Lines 44-54 sub list { Link Here
44
    my $c = shift->openapi->valid_input or return;
46
    my $c = shift->openapi->valid_input or return;
45
47
46
    return try {
48
    return try {
47
        my $orders = $c->objects->search( Koha::Acquisition::Orders->new );
49
50
        my $only_active = delete $c->validation->output->{only_active};
51
        my $order_id    = delete $c->validation->output->{order_id};
52
53
        my $orders_rs;
54
55
        if ( $only_active ) {
56
            $orders_rs = Koha::Acquisition::Orders->filter_by_active;
57
        }
58
        else {
59
            $orders_rs = Koha::Acquisition::Orders->new;
60
        }
61
62
        $orders_rs = $orders_rs->filter_by_id_including_transfers({ ordernumber => $order_id })
63
            if $order_id;
64
65
        my $args = $c->validation->output;
66
        my $attributes = {};
67
68
        # Extract reserved params
69
        my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
70
        # Look for embeds
71
        my $embed = $c->stash('koha.embed');
72
        my $fixed_embed = clone($embed);
73
        if ( exists $fixed_embed->{biblio} ) {
74
            # Add biblioitems to prefetch
75
            # FIXME remove if we merge biblio + biblioitems
76
            $fixed_embed->{biblio}->{children}->{biblioitem} = {};
77
            $c->stash('koha.embed', $fixed_embed);
78
        }
79
80
        # If no pagination parameters are passed, default
81
        $reserved_params->{_per_page} //= C4::Context->preference('RESTdefaultPageSize');
82
        $reserved_params->{_page}     //= 1;
83
84
        unless ( $reserved_params->{_per_page} == -1 ) {
85
            # Merge pagination into query attributes
86
            $c->dbic_merge_pagination(
87
                {
88
                    filter => $attributes,
89
                    params => $reserved_params
90
                }
91
            );
92
        }
93
94
        # Generate prefetches for embedded stuff
95
        $c->dbic_merge_prefetch(
96
            {
97
                attributes => $attributes,
98
                result_set => $orders_rs
99
            }
100
        );
101
102
        # Call the to_model function by reference, if defined
103
        if ( defined $filtered_params ) {
104
105
            # Apply the mapping function to the passed params
106
            $filtered_params = $orders_rs->attributes_from_api($filtered_params);
107
            $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
108
        }
109
110
        if ( defined $path_params ) {
111
112
            # Apply the mapping function to the passed params
113
            $filtered_params //= {};
114
            $path_params = $orders_rs->attributes_from_api($path_params);
115
            foreach my $param (keys %{$path_params}) {
116
                $filtered_params->{$param} = $path_params->{$param};
117
            }
118
        }
119
120
        if ( defined $reserved_params->{q} || defined $reserved_params->{query} || defined $reserved_params->{'x-koha-query'}) {
121
            $filtered_params //={};
122
            my @query_params_array;
123
            my $query_params;
124
            if ( exists $reserved_params->{query} and defined $reserved_params->{query} ) {
125
                push @query_params_array, fix_query({ query => $reserved_params->{query} });
126
            }
127
            if ( exists $reserved_params->{q} and defined $reserved_params->{q}) {
128
                push @query_params_array, fix_query({ query => decode_json($reserved_params->{q}) });
129
            }
130
            if ( exists $reserved_params->{'x-koha-query'} and defined $reserved_params->{'x-koha-query'} ) {
131
                push @query_params_array, fix_query({ query => decode_json($reserved_params->{'x-koha-query'}) });;
132
            }
133
134
            if(scalar(@query_params_array) > 1) {
135
                $query_params = {'-and' => \@query_params_array};
136
            }
137
            else {
138
                $query_params = $query_params_array[0];
139
            }
140
141
            $filtered_params = $c->merge_q_params( $filtered_params, $query_params, $orders_rs );
142
        }
143
144
        # Perform search
145
        my $orders = $orders_rs->search( $filtered_params, $attributes );
146
147
        if ($orders->is_paged) {
148
            $c->add_pagination_headers({
149
                total => $orders->pager->total_entries,
150
                params => $args,
151
            });
152
        }
153
        else {
154
            $c->add_pagination_headers({
155
                total => $orders->count,
156
                params => $args,
157
            });
158
        }
48
159
49
        return $c->render(
160
        return $c->render(
50
            status  => 200,
161
            status  => 200,
51
            openapi => $orders
162
            openapi => $orders->to_api({ embed => $embed })
52
        );
163
        );
53
    }
164
    }
54
    catch {
165
    catch {
Lines 185-188 sub delete { Link Here
185
    };
296
    };
186
}
297
}
187
298
299
=head2 Internal methods
300
301
=head3 fix_query
302
303
    my $query = fix_query($query);
304
305
This method takes care of recursively fixing queries that should be done
306
against biblioitems (instead if biblio as exposed on the API)
307
308
=cut
309
310
sub fix_query {
311
    my ($args) = @_;
312
313
    my $query = $args->{query};
314
    my $biblioitem_fields = {
315
        'biblio.isbn' => 'biblio.biblioitem.isbn',
316
        'biblio.ean'  => 'biblio.biblioitem.ean'
317
    };
318
319
    if ( ref($query) eq 'HASH' ) {
320
        foreach my $key (keys %{$query}) {
321
            if ( exists $biblioitem_fields->{$key}) {
322
                my $subq = delete $query->{$key};
323
                $query->{$biblioitem_fields->{$key}} = (ref($subq) eq 'HASH')
324
                        ? fix_query({ query => $subq })
325
                        : $subq;
326
            }
327
            else {
328
                $query->{$key} = fix_query({ query => $query->{$key} });
329
            }
330
        }
331
    }
332
    elsif ( ref($query) eq 'ARRAY' ) {
333
        my @accum;
334
        foreach my $item (@{$query}) {
335
            push @accum, fix_query({ query => $item });
336
        }
337
        $query = \@accum;
338
    }
339
    else { # scalar
340
        $query = $biblioitem_fields->{$query}
341
            if exists $biblioitem_fields->{$query};
342
    }
343
344
    return $query;
345
}
346
188
1;
347
1;
(-)a/api/v1/swagger/definitions/order.json (-1 / +8 lines)
Lines 13-19 Link Here
13
            "description": "Identifier for the linked bibliographic record"
13
            "description": "Identifier for the linked bibliographic record"
14
        },
14
        },
15
        "created_by": {
15
        "created_by": {
16
            "type": "integer",
16
            "type": [
17
                "integer",
18
                "null"
19
            ],
17
            "description": "Interal patron identifier of the order line creator"
20
            "description": "Interal patron identifier of the order line creator"
18
        },
21
        },
19
        "entry_date": {
22
        "entry_date": {
Lines 302-307 Link Here
302
                "null"
305
                "null"
303
            ]
306
            ]
304
        },
307
        },
308
        "current_item_level_holds_count": {
309
            "type": "integer",
310
            "description": "Current holds count for associated items"
311
        },
305
        "fund": {
312
        "fund": {
306
            "type": [
313
            "type": [
307
                "object",
314
                "object",
(-)a/api/v1/swagger/paths/acquisitions_orders.json (-5 / +34 lines)
Lines 39-44 Link Here
39
                    "required": false,
39
                    "required": false,
40
                    "type": "string"
40
                    "type": "string"
41
                },
41
                },
42
                {
43
                    "name": "only_active",
44
                    "in": "query",
45
                    "description": "If only active orders should be listed",
46
                    "required": false,
47
                    "type": "boolean"
48
                },
42
                {
49
                {
43
                    "$ref": "../parameters.json#/match"
50
                    "$ref": "../parameters.json#/match"
44
                },
51
                },
Lines 50-55 Link Here
50
                },
57
                },
51
                {
58
                {
52
                    "$ref": "../parameters.json#/per_page"
59
                    "$ref": "../parameters.json#/per_page"
60
                },
61
                {
62
                    "$ref": "../parameters.json#/q_param"
63
                },
64
                {
65
                    "$ref": "../parameters.json#/q_body"
66
                },
67
                {
68
                    "$ref": "../parameters.json#/q_header"
53
                }
69
                }
54
            ],
70
            ],
55
            "responses": {
71
            "responses": {
Lines 100-110 Link Here
100
            },
116
            },
101
            "x-koha-embed": [
117
            "x-koha-embed": [
102
                "basket",
118
                "basket",
119
                "basket.basket_group",
120
                "basket.creator",
121
                "biblio",
122
                "biblio.active_orders+count",
123
                "biblio.holds+count",
124
                "biblio.items+count",
125
                "biblio.suggestions.suggester",
103
                "fund",
126
                "fund",
127
                "current_item_level_holds+count",
104
                "invoice",
128
                "invoice",
105
                "subscription",
106
                "items",
129
                "items",
107
                "biblio"
130
                "subscription"
108
            ]
131
            ]
109
        },
132
        },
110
        "post": {
133
        "post": {
Lines 240-250 Link Here
240
            },
263
            },
241
            "x-koha-embed": [
264
            "x-koha-embed": [
242
                "basket",
265
                "basket",
266
                "basket.basket_group",
267
                "basket.creator",
268
                "biblio",
269
                "biblio.active_orders+count",
270
                "biblio.holds+count",
271
                "biblio.items+count",
272
                "biblio.suggestions.suggester",
243
                "fund",
273
                "fund",
274
                "current_item_level_holds+count",
244
                "invoice",
275
                "invoice",
245
                "subscription",
246
                "items",
276
                "items",
247
                "biblio"
277
                "subscription"
248
            ]
278
            ]
249
        },
279
        },
250
        "put": {
280
        "put": {
251
- 

Return to bug 20212