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 = shift->openapi->valid_input or return;
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->render(status
=> 200, openapi
=> $holds);
44 my $c = shift->openapi->valid_input or return;
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->render( status
=> 404,
56 openapi
=> {error
=> "Borrower not found"} );
59 unless ($biblionumber or $itemnumber) {
60 return $c->render( status
=> 400, openapi
=> {
61 error
=> "At least one of biblionumber, itemnumber should be given"
64 unless ($branchcode) {
65 return $c->render( status
=> 400,
66 openapi
=> { error
=> "Branchcode is required" } );
71 my $item = Koha
::Items
->find( $itemnumber );
72 $biblio = $item->biblio;
73 if ($biblionumber and $biblionumber != $biblio->biblionumber) {
77 error
=> "Item $itemnumber doesn't belong to biblio $biblionumber"
80 $biblionumber ||= $biblio->biblionumber;
82 $biblio = Koha
::Biblios
->find( $biblionumber );
87 ? CanItemBeReserved
( $borrowernumber, $itemnumber )
88 : CanBookBeReserved
( $borrowernumber, $biblionumber );
90 unless ($can_reserve eq 'OK') {
91 return $c->render( status
=> 403, openapi
=> {
92 error
=> "Reserve cannot be placed. Reason: $can_reserve"
96 my $priority = C4
::Reserves
::CalculatePriority
($biblionumber);
97 $itemnumber ||= undef;
99 # AddReserve expects date to be in syspref format
100 if ($expirationdate) {
101 $expirationdate = output_pref
(dt_from_string
($expirationdate, 'iso'));
104 my $reserve_id = C4
::Reserves
::AddReserve
($branchcode, $borrowernumber,
105 $biblionumber, undef, $priority, undef, $expirationdate, undef,
106 $biblio->title, $itemnumber);
108 unless ($reserve_id) {
109 return $c->render( status
=> 500, openapi
=> {
110 error
=> "Error while placing reserve. See Koha logs for details."
114 my $reserve = Koha
::Holds
->find($reserve_id);
116 return $c->render( status
=> 201, openapi
=> $reserve );
120 my $c = shift->openapi->valid_input or return;
122 my $reserve_id = $c->validation->param('reserve_id');
123 my $hold = Koha
::Holds
->find( $reserve_id );
126 return $c->render( status
=> 404,
127 openapi
=> {error
=> "Reserve not found"} );
130 my $body = $c->req->json;
132 my $branchcode = $body->{branchcode
};
133 my $priority = $body->{priority
};
134 my $suspend_until = $body->{suspend_until
};
136 if ($suspend_until) {
137 $suspend_until = output_pref
(dt_from_string
($suspend_until, 'iso'));
141 reserve_id
=> $reserve_id,
142 branchcode
=> $branchcode,
144 suspend_until
=> $suspend_until,
147 C4
::Reserves
::ModReserve
($params);
148 $hold = Koha
::Holds
->find($reserve_id);
150 return $c->render( status
=> 200, openapi
=> $hold );
154 my $c = shift->openapi->valid_input or return;
156 my $reserve_id = $c->validation->param('reserve_id');
157 my $hold = Koha
::Holds
->find( $reserve_id );
160 return $c->render( status
=> 404, openapi
=> {error
=> "Reserve not found"} );
165 return $c->render( status
=> 200, openapi
=> {} );