Bug 16213: Allow to select hold's itemtype when using API
[koha.git] / Koha / REST / V1 / Hold.pm
blob2927a7ad0aad76456bbded72946784a4e8368d0c
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
8 # version.
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.
18 use Modern::Perl;
20 use Mojo::Base 'Mojolicious::Controller';
22 use C4::Biblio;
23 use C4::Reserves;
25 use Koha::Items;
26 use Koha::Patrons;
27 use Koha::Holds;
28 use Koha::DateUtils;
30 sub list {
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);
43 sub add {
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);
56 unless ($borrower) {
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"
64 } );
66 unless ($branchcode) {
67 return $c->render( status => 400,
68 openapi => { error => "Branchcode is required" } );
71 my $biblio;
72 if ($itemnumber) {
73 my $item = Koha::Items->find( $itemnumber );
74 $biblio = $item->biblio;
75 if ($biblionumber and $biblionumber != $biblio->biblionumber) {
76 return $c->render(
77 status => 400,
78 openapi => {
79 error => "Item $itemnumber doesn't belong to biblio $biblionumber"
80 });
82 $biblionumber ||= $biblio->biblionumber;
83 } else {
84 $biblio = Koha::Biblios->find( $biblionumber );
87 my $can_reserve =
88 $itemnumber
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"
95 } );
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."
113 } );
116 my $reserve = Koha::Holds->find($reserve_id);
118 return $c->render( status => 201, openapi => $reserve );
121 sub edit {
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 );
127 unless ($hold) {
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'));
142 my $params = {
143 reserve_id => $reserve_id,
144 branchcode => $branchcode,
145 rank => $priority,
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 );
155 sub delete {
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 );
161 unless ($hold) {
162 return $c->render( status => 404, openapi => {error => "Reserve not found"} );
165 $hold->cancel;
167 return $c->render( status => 200, openapi => {} );