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

(-)a/Koha/REST/V1/Reserve.pm (-54 / +101 lines)
Lines 44-114 sub add { Link Here
44
44
45
    my $body = $c->req->json;
45
    my $body = $c->req->json;
46
46
47
    my $borrowernumber = $body->{borrowernumber};
47
    my $placed;
48
    my $biblionumber = $body->{biblionumber};
48
    my $sorted_requests;
49
    my $itemnumber = $body->{itemnumber};
49
    foreach my $requested (@$body) {
50
    my $branchcode = $body->{branchcode};
50
        my $key = $requested->{borrowernumber} . '_' . $requested->{biblionumber};
51
    my $expirationdate = $body->{expirationdate};
51
        push @{ $sorted_requests->{$key} }, $requested;
52
    my $borrower = Koha::Patrons->find($borrowernumber);
53
    unless ($borrower) {
54
        return $c->$cb({error => "Borrower not found"}, 404);
55
    }
52
    }
56
53
57
    unless ($biblionumber or $itemnumber) {
54
    foreach my $key (keys %$sorted_requests) {
58
        return $c->$cb({
55
        my $requests = $sorted_requests->{$key};
59
            error => "At least one of biblionumber, itemnumber should be given"
56
        my ($borrowernumber, $biblionumber) = split('_', $key);
60
        }, 400);
57
61
    }
58
        my $holds = Koha::Holds->search({
62
    unless ($branchcode) {
59
            borrowernumber => $borrowernumber,
63
        return $c->$cb({
60
            biblionumber   => $biblionumber,
64
            error => "Branchcode is required"
61
            found          => undef
65
        }, 400);
62
        });
66
    }
67
63
68
    if ($itemnumber) {
64
        my $max_for_record = GetMaxPatronHoldsForRecord($borrowernumber, $biblionumber);
69
        my $item_biblionumber = C4::Biblio::GetBiblionumberFromItemnumber($itemnumber);
65
        if ($holds->count() + scalar(@$requests) > $max_for_record) {
70
        if ($biblionumber and $biblionumber != $item_biblionumber) {
71
            return $c->$cb({
66
            return $c->$cb({
72
                error => "Item $itemnumber doesn't belong to biblio $biblionumber"
67
                error => "Too many holds for this record"
73
            }, 400);
68
            }, 400);
74
        }
69
        }
75
        $biblionumber ||= $item_biblionumber;
76
    }
77
78
    my $biblio = C4::Biblio::GetBiblio($biblionumber);
79
70
80
    my $can_reserve =
71
        my @toreserve;
81
      $itemnumber
72
        my $hold_level = $holds->forced_hold_level();
82
      ? CanItemBeReserved( $borrowernumber, $itemnumber )
73
        foreach my $request (@$requests) {
83
      : CanBookBeReserved( $borrowernumber, $biblionumber );
74
            my $itemnumber = $request->{itemnumber};
84
75
            my $branchcode = $request->{branchcode};
85
    unless ($can_reserve eq 'OK') {
76
            my $expirationdate = $request->{expirationdate};
86
        return $c->$cb({
77
87
            error => "Reserve cannot be placed. Reason: $can_reserve"
78
            my $level = $itemnumber ? 'item' : 'record';
88
        }, 403);
79
            if ($hold_level && $hold_level ne $level) {
89
    }
80
                return $c->$cb({
90
81
                    error => "Differents holds level for the same record."
91
    my $priority = C4::Reserves::CalculatePriority($biblionumber);
82
                }, 400);
92
    $itemnumber ||= undef;
83
            }
84
            $hold_level = $level;
85
86
            my $borrower = Koha::Patrons->find($borrowernumber);
87
            unless ($borrower) {
88
                return $c->$cb({error => "Borrower not found"}, 404);
89
            }
90
91
            unless ($biblionumber or $itemnumber) {
92
                return $c->$cb({
93
                    error => "At least one of biblionumber, itemnumber should be given"
94
                }, 400);
95
            }
96
            unless ($branchcode) {
97
                return $c->$cb({
98
                    error => "Branchcode is required"
99
                }, 400);
100
            }
101
102
            if ($itemnumber) {
103
                my $item_biblionumber = C4::Biblio::GetBiblionumberFromItemnumber($itemnumber);
104
                if ($itemnumber and $biblionumber and $biblionumber != $item_biblionumber) {
105
                    return $c->$cb({
106
                        error => "Item $itemnumber doesn't belong to biblio $biblionumber"
107
                    }, 400);
108
                }
109
                $biblionumber ||= $item_biblionumber;
110
            }
111
112
            my $biblio = C4::Biblio::GetBiblio($biblionumber);
113
114
            my $can_reserve =
115
              $itemnumber
116
              ? CanItemBeReserved( $borrowernumber, $itemnumber )
117
              : CanBookBeReserved( $borrowernumber, $biblionumber );
118
119
            unless ($can_reserve eq 'OK') {
120
                return $c->$cb({
121
                    error => "Reserve cannot be placed. Reason: $can_reserve"
122
                }, 403);
123
            }
124
125
            my $priority = C4::Reserves::CalculatePriority($biblionumber);
126
            $itemnumber ||= undef;
127
128
            # AddReserve expects date to be in syspref format
129
            if ($expirationdate) {
130
                $expirationdate = output_pref(dt_from_string($expirationdate, 'iso'));
131
            }
132
133
            push @toreserve, {
134
                branchcode => $branchcode,
135
                borrowernumber => $borrowernumber,
136
                biblionumber => $biblionumber,
137
                priority => $priority,
138
                expirationdate => $expirationdate,
139
                title => $biblio->{title},
140
                itemnumber => $itemnumber,
141
            };
142
        }
93
143
94
    # AddReserve expects date to be in syspref format
144
        foreach my $r (@toreserve) {
95
    if ($expirationdate) {
145
            my $reserve_id = C4::Reserves::AddReserve($r->{branchcode},
96
        $expirationdate = output_pref(dt_from_string($expirationdate, 'iso'));
146
                $r->{borrowernumber}, $r->{biblionumber}, undef, $r->{priority}, undef,
97
    }
147
                $r->{expirationdate}, undef, $r->{title}, $r->{itemnumber});
98
148
99
    my $reserve_id = C4::Reserves::AddReserve($branchcode, $borrowernumber,
149
            unless ($reserve_id) {
100
        $biblionumber, undef, $priority, undef, $expirationdate, undef,
150
                return $c->$cb({
101
        $biblio->{title}, $itemnumber);
151
                    error => "Error while placing reserve. See Koha logs for details."
152
                }, 500);
153
            }
102
154
103
    unless ($reserve_id) {
155
            push @{ $placed }, C4::Reserves::GetReserve($reserve_id);
104
        return $c->$cb({
156
        }
105
            error => "Error while placing reserve. See Koha logs for details."
106
        }, 500);
107
    }
157
    }
108
158
    return $c->$cb($placed, 201);
109
    my $reserve = C4::Reserves::GetReserve($reserve_id);
110
111
    return $c->$cb($reserve, 201);
112
}
159
}
113
160
114
sub edit {
161
sub edit {
(-)a/api/v1/swagger.json (-24 / +30 lines)
Lines 109-136 Link Here
109
            "description": "A JSON object containing informations about the new reserve",
109
            "description": "A JSON object containing informations about the new reserve",
110
            "required": true,
110
            "required": true,
111
            "schema": {
111
            "schema": {
112
              "type": "object",
112
              "type": "array",
113
              "properties": {
113
              "items": {
114
                "borrowernumber": {
114
                "type": "object",
115
                  "description": "Borrower internal identifier",
115
                "properties": {
116
                  "type": "integer"
116
                  "borrowernumber": {
117
                },
117
                    "description": "Borrower internal identifier",
118
                "biblionumber": {
118
                    "type": "integer"
119
                  "description": "Biblio internal identifier",
119
                  },
120
                  "type": "integer"
120
                  "biblionumber": {
121
                },
121
                    "description": "Biblio internal identifier",
122
                "itemnumber": {
122
                    "type": "integer"
123
                  "description": "Item internal identifier",
123
                  },
124
                  "type": "integer"
124
                  "itemnumber": {
125
                },
125
                    "description": "Item internal identifier",
126
                "branchcode": {
126
                    "type": "integer"
127
                  "description": "Pickup location",
127
                  },
128
                  "type": "string"
128
                  "branchcode": {
129
                },
129
                    "description": "Pickup location",
130
                "expirationdate": {
130
                    "type": "string"
131
                  "description": "Reserve end date",
131
                  },
132
                  "type": "string",
132
                  "expirationdate": {
133
                  "format": "date"
133
                    "description": "Reserve end date",
134
                    "type": "string",
135
                    "format": "date"
136
                  }
134
                }
137
                }
135
              }
138
              }
136
            }
139
            }
Lines 140-147 Link Here
140
        "produces": ["application/json"],
143
        "produces": ["application/json"],
141
        "responses": {
144
        "responses": {
142
          "201": {
145
          "201": {
143
            "description": "Created reserve",
146
            "description": "Created reserves",
144
            "schema": { "$ref": "#/definitions/reserve" }
147
            "schema": {
148
                "type": "array",
149
                "items": { "$ref": "#/definitions/reserve" }
150
            }
145
          },
151
          },
146
          "400": {
152
          "400": {
147
            "description": "Missing or wrong parameters",
153
            "description": "Missing or wrong parameters",
(-)a/t/db_dependent/api/v1/reserves.t (-10 / +59 lines)
Lines 17-24 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 30;
20
use Test::More tests => 43;
21
use Test::Mojo;
21
use Test::Mojo;
22
use t::lib::TestBuilder;
22
23
23
use DateTime;
24
use DateTime;
24
25
Lines 34-43 my $dbh = C4::Context->dbh; Link Here
34
$dbh->{AutoCommit} = 0;
35
$dbh->{AutoCommit} = 0;
35
$dbh->{RaiseError} = 1;
36
$dbh->{RaiseError} = 1;
36
37
38
my $builder = t::lib::TestBuilder->new();
37
my $t = Test::Mojo->new('Koha::REST::V1');
39
my $t = Test::Mojo->new('Koha::REST::V1');
38
40
39
my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode();
41
my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
40
my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
42
my $categorycode2 = $builder->build({ source => 'Category' })->{ categorycode };
43
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
41
44
42
my $borrower = Koha::Patron->new;
45
my $borrower = Koha::Patron->new;
43
$borrower->categorycode( $categorycode );
46
$borrower->categorycode( $categorycode );
Lines 47-53 $borrower->store; Link Here
47
my $borrowernumber = $borrower->borrowernumber;
50
my $borrowernumber = $borrower->borrowernumber;
48
51
49
my $borrower2 = Koha::Patron->new;
52
my $borrower2 = Koha::Patron->new;
50
$borrower2->categorycode( $categorycode );
53
$borrower2->categorycode( $categorycode2 );
51
$borrower2->branchcode( $branchcode );
54
$borrower2->branchcode( $branchcode );
52
$borrower2->surname("Test Surname 2");
55
$borrower2->surname("Test Surname 2");
53
$borrower2->store;
56
$borrower2->store;
Lines 55-60 my $borrowernumber2 = $borrower2->borrowernumber; Link Here
55
58
56
my $biblionumber = create_biblio('RESTful Web APIs');
59
my $biblionumber = create_biblio('RESTful Web APIs');
57
my $itemnumber = create_item($biblionumber, 'TEST000001');
60
my $itemnumber = create_item($biblionumber, 'TEST000001');
61
my $itemnumber2 = create_item($biblionumber, 'TEST000002');
62
63
my $biblionumber2 = create_biblio('Calamity');
64
my $itemnumber3 = create_item($biblionumber2, 'TEST000003');
65
my $itemnumber4 = create_item($biblionumber2, 'TEST000004');
66
my $itemnumber5 = create_item($biblionumber2, 'TEST000005');
58
67
59
my $reserve_id = C4::Reserves::AddReserve($branchcode, $borrowernumber,
68
my $reserve_id = C4::Reserves::AddReserve($branchcode, $borrowernumber,
60
    $biblionumber, undef, 1, undef, undef, undef, '', $itemnumber);
69
    $biblionumber, undef, 1, undef, undef, undef, '', $itemnumber);
Lines 99-119 $dbh->do('DELETE FROM issuingrules'); Link Here
99
$dbh->do(q{
108
$dbh->do(q{
100
    INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
109
    INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
101
    VALUES (?, ?, ?, ?)
110
    VALUES (?, ?, ?, ?)
102
}, {}, '*', '*', '*', 1);
111
}, {}, $categorycode, '*', '*', 1);
112
113
$dbh->do(q{
114
    INSERT INTO issuingrules (categorycode, branchcode, itemtype, holds_per_record, reservesallowed)
115
    VALUES (?, ?, ?, ?, ?)
116
}, {}, $categorycode2, '*', '*', '3', 5);
103
117
104
my $expirationdate = DateTime->now->add(days => 10)->ymd;
118
my $expirationdate = DateTime->now->add(days => 10)->ymd;
105
my $post_data = {
119
my $post_data = [{
106
    borrowernumber => int($borrowernumber),
120
    borrowernumber => int($borrowernumber),
107
    biblionumber => int($biblionumber),
121
    biblionumber => int($biblionumber),
108
    itemnumber => int($itemnumber),
122
    itemnumber => int($itemnumber),
109
    branchcode => $branchcode,
123
    branchcode => $branchcode,
110
    expirationdate => $expirationdate,
124
    expirationdate => $expirationdate,
111
};
125
}];
112
$t->post_ok("/api/v1/reserves" => json => $post_data)
126
$t->post_ok("/api/v1/reserves" => json => $post_data)
113
  ->status_is(201)
127
  ->status_is(201)
114
  ->json_has('/reserve_id');
128
  ->json_has('/0/reserve_id');
115
129
116
$reserve_id = $t->tx->res->json->{reserve_id};
130
$reserve_id = $t->tx->res->json->[0]->{reserve_id};
117
131
118
$t->get_ok("/api/v1/reserves?borrowernumber=$borrowernumber")
132
$t->get_ok("/api/v1/reserves?borrowernumber=$borrowernumber")
119
  ->status_is(200)
133
  ->status_is(200)
Lines 121-130 $t->get_ok("/api/v1/reserves?borrowernumber=$borrowernumber") Link Here
121
  ->json_is('/0/expirationdate', $expirationdate)
135
  ->json_is('/0/expirationdate', $expirationdate)
122
  ->json_is('/0/branchcode', $branchcode);
136
  ->json_is('/0/branchcode', $branchcode);
123
137
138
$post_data->[0]->{biblionumber} = int($biblionumber2);
139
$post_data->[0]->{itemnumber} = int($itemnumber3);
124
$t->post_ok("/api/v1/reserves" => json => $post_data)
140
$t->post_ok("/api/v1/reserves" => json => $post_data)
125
  ->status_is(403)
141
  ->status_is(403)
126
  ->json_like('/error', qr/tooManyReserves/);
142
  ->json_like('/error', qr/tooManyReserves/);
127
143
144
$post_data = [
145
    {
146
        borrowernumber => int($borrowernumber2),
147
        biblionumber => int($biblionumber2),
148
        itemnumber => int($itemnumber3),
149
        branchcode => $branchcode,
150
    },
151
    {
152
        borrowernumber => int($borrowernumber2),
153
        biblionumber => int($biblionumber2),
154
        branchcode => $branchcode,
155
    },
156
];
157
158
$t->post_ok("/api/v1/reserves" => json => $post_data)
159
  ->status_is(400)
160
  ->json_like('/error', qr/Differents holds level/);
161
162
delete $post_data->[0]->{itemnumber};
163
$t->post_ok("/api/v1/reserves" => json => $post_data)
164
  ->status_is(201)
165
  ->json_has('/0/reserve_id')
166
  ->json_has('/1/reserve_id');
167
168
$t->post_ok("/api/v1/reserves" => json => $post_data)
169
  ->status_is(400)
170
  ->json_like('/error', qr/Too many holds/);
171
172
delete $post_data->[1];
173
$post_data->[0]->{itemnumber} = int($itemnumber3);
174
$t->post_ok("/api/v1/reserves" => json => $post_data)
175
  ->status_is(400)
176
  ->json_like('/error', qr/Differents holds level/);
177
128
178
129
$dbh->rollback;
179
$dbh->rollback;
130
180
131
- 

Return to bug 15567