Bug 18260: Koha::Biblio - Remove GetBiblio
[koha.git] / Koha / REST / V1 / Hold.pm
1 package Koha::REST::V1::Hold;
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 C4::Biblio;
23 use C4::Reserves;
24
25 use Koha::Items;
26 use Koha::Patrons;
27 use Koha::Holds;
28 use Koha::DateUtils;
29
30 sub list {
31     my ($c, $args, $cb) = @_;
32
33     my $params = $c->req->query_params->to_hash;
34     my @valid_params = Koha::Holds->_resultset->result_source->columns;
35     foreach my $key (keys %$params) {
36         delete $params->{$key} unless grep { $key eq $_ } @valid_params;
37     }
38     my $holds = Koha::Holds->search($params);
39
40     return $c->$cb($holds, 200);
41 }
42
43 sub add {
44     my ($c, $args, $cb) = @_;
45
46     my $body = $c->req->json;
47
48     my $borrowernumber = $body->{borrowernumber};
49     my $biblionumber = $body->{biblionumber};
50     my $itemnumber = $body->{itemnumber};
51     my $branchcode = $body->{branchcode};
52     my $expirationdate = $body->{expirationdate};
53     my $borrower = Koha::Patrons->find($borrowernumber);
54     unless ($borrower) {
55         return $c->$cb({error => "Borrower not found"}, 404);
56     }
57
58     unless ($biblionumber or $itemnumber) {
59         return $c->$cb({
60             error => "At least one of biblionumber, itemnumber should be given"
61         }, 400);
62     }
63     unless ($branchcode) {
64         return $c->$cb({
65             error => "Branchcode is required"
66         }, 400);
67     }
68
69     my $biblio;
70     if ($itemnumber) {
71         my $item = Koha::Items->find( $itemnumber );
72         $biblio = $item->biblio;
73         if ($biblionumber and $biblionumber != $biblio->biblionumber) {
74             return $c->$cb({
75                 error => "Item $itemnumber doesn't belong to biblio $biblionumber"
76             }, 400);
77         }
78         $biblionumber ||= $biblio->biblionumber;
79     } else {
80         $biblio = Koha::Biblios->find( $biblionumber );
81     }
82
83     my $can_reserve =
84       $itemnumber
85       ? CanItemBeReserved( $borrowernumber, $itemnumber )
86       : CanBookBeReserved( $borrowernumber, $biblionumber );
87
88     unless ($can_reserve eq 'OK') {
89         return $c->$cb({
90             error => "Reserve cannot be placed. Reason: $can_reserve"
91         }, 403);
92     }
93
94     my $priority = C4::Reserves::CalculatePriority($biblionumber);
95     $itemnumber ||= undef;
96
97     # AddReserve expects date to be in syspref format
98     if ($expirationdate) {
99         $expirationdate = output_pref(dt_from_string($expirationdate, 'iso'));
100     }
101
102     my $reserve_id = C4::Reserves::AddReserve($branchcode, $borrowernumber,
103         $biblionumber, undef, $priority, undef, $expirationdate, undef,
104         $biblio->title, $itemnumber);
105
106     unless ($reserve_id) {
107         return $c->$cb({
108             error => "Error while placing reserve. See Koha logs for details."
109         }, 500);
110     }
111
112     my $reserve = Koha::Holds->find($reserve_id);
113
114     return $c->$cb($reserve, 201);
115 }
116
117 sub edit {
118     my ($c, $args, $cb) = @_;
119
120     my $reserve_id = $args->{reserve_id};
121     my $reserve = C4::Reserves::GetReserve($reserve_id);
122
123     unless ($reserve) {
124         return $c->$cb({error => "Reserve not found"}, 404);
125     }
126
127     my $body = $c->req->json;
128
129     my $branchcode = $body->{branchcode};
130     my $priority = $body->{priority};
131     my $suspend_until = $body->{suspend_until};
132
133     if ($suspend_until) {
134         $suspend_until = output_pref(dt_from_string($suspend_until, 'iso'));
135     }
136
137     my $params = {
138         reserve_id => $reserve_id,
139         branchcode => $branchcode,
140         rank => $priority,
141         suspend_until => $suspend_until,
142     };
143
144     C4::Reserves::ModReserve($params);
145     $reserve = Koha::Holds->find($reserve_id);
146
147     return $c->$cb($reserve, 200);
148 }
149
150 sub delete {
151     my ($c, $args, $cb) = @_;
152
153     my $reserve_id = $args->{reserve_id};
154     my $reserve = C4::Reserves::GetReserve($reserve_id);
155
156     unless ($reserve) {
157         return $c->$cb({error => "Reserve not found"}, 404);
158     }
159
160     C4::Reserves::CancelReserve({ reserve_id => $reserve_id });
161
162     return $c->$cb({}, 200);
163 }
164
165 1;