1 package Koha::REST::V1::Hold;
3 # This file is part of Koha.
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
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.
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.
20 use Mojo::Base 'Mojolicious::Controller';
31 my ($c, $args, $cb) = @_;
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;
38 my $holds = Koha::Holds->search($params);
40 return $c->$cb($holds, 200);
44 my ($c, $args, $cb) = @_;
46 my $body = $c->req->json;
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);
55 return $c->$cb({error => "Borrower not found"}, 404);
58 unless ($biblionumber or $itemnumber) {
60 error => "At least one of biblionumber, itemnumber should be given"
63 unless ($branchcode) {
65 error => "Branchcode is required"
71 my $item = Koha::Items->find( $itemnumber );
72 $biblio = $item->biblio;
73 if ($biblionumber and $biblionumber != $biblio->biblionumber) {
75 error => "Item $itemnumber doesn't belong to biblio $biblionumber"
78 $biblionumber ||= $biblio->biblionumber;
80 $biblio = Koha::Biblios->find( $biblionumber );
85 ? CanItemBeReserved( $borrowernumber, $itemnumber )
86 : CanBookBeReserved( $borrowernumber, $biblionumber );
88 unless ($can_reserve eq 'OK') {
90 error => "Reserve cannot be placed. Reason: $can_reserve"
94 my $priority = C4::Reserves::CalculatePriority($biblionumber);
95 $itemnumber ||= undef;
97 # AddReserve expects date to be in syspref format
98 if ($expirationdate) {
99 $expirationdate = output_pref(dt_from_string($expirationdate, 'iso'));
102 my $reserve_id = C4::Reserves::AddReserve($branchcode, $borrowernumber,
103 $biblionumber, undef, $priority, undef, $expirationdate, undef,
104 $biblio->title, $itemnumber);
106 unless ($reserve_id) {
108 error => "Error while placing reserve. See Koha logs for details."
112 my $reserve = Koha::Holds->find($reserve_id);
114 return $c->$cb($reserve, 201);
118 my ($c, $args, $cb) = @_;
120 my $reserve_id = $args->{reserve_id};
121 my $reserve = C4::Reserves::GetReserve($reserve_id);
124 return $c->$cb({error => "Reserve not found"}, 404);
127 my $body = $c->req->json;
129 my $branchcode = $body->{branchcode};
130 my $priority = $body->{priority};
131 my $suspend_until = $body->{suspend_until};
133 if ($suspend_until) {
134 $suspend_until = output_pref(dt_from_string($suspend_until, 'iso'));
138 reserve_id => $reserve_id,
139 branchcode => $branchcode,
141 suspend_until => $suspend_until,
144 C4::Reserves::ModReserve($params);
145 $reserve = Koha::Holds->find($reserve_id);
147 return $c->$cb($reserve, 200);
151 my ($c, $args, $cb) = @_;
153 my $reserve_id = $args->{reserve_id};
154 my $reserve = C4::Reserves::GetReserve($reserve_id);
157 return $c->$cb({error => "Reserve not found"}, 404);
160 C4::Reserves::CancelReserve({ reserve_id => $reserve_id });
162 return $c->$cb({}, 200);