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 $itemtype = $body->{itemtype
};
55 my $borrower = Koha
::Patrons
->find($borrowernumber);
57 return $c->render( status
=> 404,
58 openapi
=> {error
=> "Borrower not found"} );
61 unless ($biblionumber or $itemnumber) {
62 return $c->render( status
=> 400, openapi
=> {
63 error
=> "At least one of biblionumber, itemnumber should be given"
66 unless ($branchcode) {
67 return $c->render( status
=> 400,
68 openapi
=> { error
=> "Branchcode is required" } );
73 my $item = Koha
::Items
->find( $itemnumber );
74 $biblio = $item->biblio;
75 if ($biblionumber and $biblionumber != $biblio->biblionumber) {
79 error
=> "Item $itemnumber doesn't belong to biblio $biblionumber"
82 $biblionumber ||= $biblio->biblionumber;
84 $biblio = Koha
::Biblios
->find( $biblionumber );
89 ? CanItemBeReserved
( $borrowernumber, $itemnumber )
90 : CanBookBeReserved
( $borrowernumber, $biblionumber );
92 unless ($can_reserve eq 'OK') {
93 return $c->render( status
=> 403, openapi
=> {
94 error
=> "Reserve cannot be placed. Reason: $can_reserve"
98 my $priority = C4
::Reserves
::CalculatePriority
($biblionumber);
99 $itemnumber ||= undef;
101 # AddReserve expects date to be in syspref format
102 if ($expirationdate) {
103 $expirationdate = output_pref
(dt_from_string
($expirationdate, 'iso'));
106 my $reserve_id = C4
::Reserves
::AddReserve
($branchcode, $borrowernumber,
107 $biblionumber, undef, $priority, undef, $expirationdate, undef,
108 $biblio->title, $itemnumber, undef, $itemtype);
110 unless ($reserve_id) {
111 return $c->render( status
=> 500, openapi
=> {
112 error
=> "Error while placing reserve. See Koha logs for details."
116 my $reserve = Koha
::Holds
->find($reserve_id);
118 return $c->render( status
=> 201, openapi
=> $reserve );
122 my $c = shift->openapi->valid_input or return;
124 my $reserve_id = $c->validation->param('reserve_id');
125 my $hold = Koha
::Holds
->find( $reserve_id );
128 return $c->render( status
=> 404,
129 openapi
=> {error
=> "Reserve not found"} );
132 my $body = $c->req->json;
134 my $branchcode = $body->{branchcode
};
135 my $priority = $body->{priority
};
136 my $suspend_until = $body->{suspend_until
};
138 if ($suspend_until) {
139 $suspend_until = output_pref
(dt_from_string
($suspend_until, 'iso'));
143 reserve_id
=> $reserve_id,
144 branchcode
=> $branchcode,
146 suspend_until
=> $suspend_until,
149 C4
::Reserves
::ModReserve
($params);
150 $hold = Koha
::Holds
->find($reserve_id);
152 return $c->render( status
=> 200, openapi
=> $hold );
156 my $c = shift->openapi->valid_input or return;
158 my $reserve_id = $c->validation->param('reserve_id');
159 my $hold = Koha
::Holds
->find( $reserve_id );
162 return $c->render( status
=> 404, openapi
=> {error
=> "Reserve not found"} );
167 return $c->render( status
=> 200, openapi
=> {} );