|
Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# Copyright 2014 ByWater Solutions |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 8 |
# terms of the GNU General Public License as published by the Free Software |
| 9 |
# Foundation; either version 3 of the License, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along |
| 17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
|
| 20 |
package C4::NCIP::RequestItem; |
| 21 |
|
| 22 |
use Modern::Perl; |
| 23 |
|
| 24 |
=head1 NAME |
| 25 |
|
| 26 |
C4::NCIP::RequestItem - NCIP module for effective processing of RequestItem NCIP service |
| 27 |
|
| 28 |
=head1 SYNOPSIS |
| 29 |
|
| 30 |
use C4::NCIP::RequestItem; |
| 31 |
|
| 32 |
=head1 DESCRIPTION |
| 33 |
|
| 34 |
Info about NCIP and it's services can be found here: http://www.niso.org/workrooms/ncip/resources/ |
| 35 |
|
| 36 |
=cut |
| 37 |
|
| 38 |
=head1 METHODS |
| 39 |
|
| 40 |
=head2 requestItem |
| 41 |
|
| 42 |
requestItem($cgiInput) |
| 43 |
|
| 44 |
Expected input is as e.g. as follows: |
| 45 |
|
| 46 |
http://KohaIntranet:8080/cgi-bin/koha/svc/ncip?service=request_item&requestType=Hold&userId=3&itemid=4&pickupExpiryDate=28/03/2015&pickupLocation=DOSP |
| 47 |
or |
| 48 |
http://KohaIntranet:8080/cgi-bin/koha/svc/ncip?service=request_item&userId=3&bibId=7 |
| 49 |
|
| 50 |
|
| 51 |
REQUIRED PARAMS: |
| 52 |
Param 'service=request_item' tells svc/ncip to forward the query here. |
| 53 |
Param 'userId=3' specifies borrowernumber to place Reserve to. |
| 54 |
Param 'itemId=4' specifies itemnumber to place Reserve on. |
| 55 |
This param can be replaced with 'barcode=1103246'. But still one of these is required. |
| 56 |
Or with 'bibId=3' - then it is Bibliographic Level Hold. |
| 57 |
|
| 58 |
OPTIONAL PARAMS: |
| 59 |
Param 'requestType=Hold' can be either 'Hold' or 'Loan'. |
| 60 |
Param 'pickupExpiryDate=28/06/2015' tells until what date is user interested into specified item. |
| 61 |
Param 'pickuplocation=DOSP' specifies which branch is user expecting pickup at. |
| 62 |
|
| 63 |
=cut |
| 64 |
|
| 65 |
sub requestItem { |
| 66 |
my $query = shift; |
| 67 |
my $userId = $query->param('userId'); |
| 68 |
|
| 69 |
C4::NCIP::NcipUtils::print400($query, "Param userId is undefined..") |
| 70 |
unless $userId; |
| 71 |
|
| 72 |
my $bibId = $query->param('bibId'); |
| 73 |
my $itemId = $query->param('itemId'); |
| 74 |
my $barcode = $query->param('barcode'); |
| 75 |
|
| 76 |
C4::NCIP::NcipUtils::print400($query, |
| 77 |
"Cannot process both bibId & itemId/barcode .. you have to choose only one" |
| 78 |
) if $bibId and ($itemId or $barcode); |
| 79 |
|
| 80 |
my $itemLevelHold = 1; |
| 81 |
unless ($itemId) { |
| 82 |
if ($bibId) { |
| 83 |
my $canBeReserved |
| 84 |
= C4::Reserves::CanBookBeReserved($userId, $bibId); |
| 85 |
|
| 86 |
print409($query, "Book cannot be reserved.. $canBeReserved") |
| 87 |
unless ($canBeReserved eq 'OK'); |
| 88 |
|
| 89 |
$itemLevelHold = 0; |
| 90 |
} else { |
| 91 |
C4::NCIP::NcipUtils::print400($query, |
| 92 |
"Param bibId neither any of itemId and barcode is undefined") |
| 93 |
unless $barcode; |
| 94 |
|
| 95 |
$itemId = C4::Items::GetItemnumberFromBarcode($barcode); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
if ($itemLevelHold) { |
| 100 |
my $canBeReserved = C4::Reserves::CanItemBeReserved($userId, $itemId); |
| 101 |
|
| 102 |
C4::NCIP::NcipUtils::print409($query, |
| 103 |
"Item cannot be reserved.. $canBeReserved") |
| 104 |
unless $canBeReserved eq 'OK'; |
| 105 |
|
| 106 |
$bibId = C4::Biblio::GetBiblionumberFromItemnumber($itemId); |
| 107 |
} |
| 108 |
|
| 109 |
# RequestType specifies if user wants the book now or doesn't mind to get into queue |
| 110 |
my $requestType = $query->param('requestType'); |
| 111 |
|
| 112 |
if ($requestType) { |
| 113 |
C4::NCIP::NcipUtils::print400($query, |
| 114 |
"Param requestType not recognized.. Can be \'Loan\' or \'Hold\'") |
| 115 |
if (not $requestType =~ /^Loan$|^Hold$/); |
| 116 |
} else { |
| 117 |
$requestType = 'Hold'; |
| 118 |
} |
| 119 |
|
| 120 |
# Process rank & whether user hasn't requested this item yet .. |
| 121 |
my $reserves = C4::Reserves::GetReservesFromBiblionumber( |
| 122 |
{biblionumber => $bibId, itemnumber => $itemId, all_dates => 1}); |
| 123 |
|
| 124 |
foreach my $res (@$reserves) { |
| 125 |
C4::NCIP::NcipUtils::print403($query, |
| 126 |
"User already has item requested") |
| 127 |
if $res->{borrowernumber} eq $userId; |
| 128 |
} |
| 129 |
|
| 130 |
my $rank = scalar(@$reserves); |
| 131 |
|
| 132 |
C4::NCIP::NcipUtils::print409($query, |
| 133 |
"Loan not possible .. holdqueuelength exists") |
| 134 |
if $requestType ne 'Hold' and $rank != 0; |
| 135 |
|
| 136 |
my $expirationdate = $query->param('pickupExpiryDate'); |
| 137 |
my $startdate = $query->param('earliestDateNeeded'); |
| 138 |
my $notes = $query->param('notes') || 'Placed by svc/ncip'; |
| 139 |
my $pickupLocation = $query->param('pickupLocation') |
| 140 |
|| C4::Context->userenv->{'branch'}; |
| 141 |
|
| 142 |
if ($itemLevelHold) { |
| 143 |
placeHold( |
| 144 |
$query, $bibId, $itemId, $userId, |
| 145 |
$pickupLocation, $startdate, $expirationdate, $notes, |
| 146 |
++$rank, undef |
| 147 |
); |
| 148 |
} else { |
| 149 |
placeHold( |
| 150 |
$query, $bibId, undef, $userId, |
| 151 |
$pickupLocation, $startdate, $expirationdate, $notes, |
| 152 |
++$rank, 'any' |
| 153 |
); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
=head2 placeHold |
| 158 |
|
| 159 |
placeHold($inputCGI, $biblionumber, $itemnumber, $borrowernumber, $pickup, $startdate, $expirationdate, $notes, $rank, $requesttype) |
| 160 |
|
| 161 |
=cut |
| 162 |
|
| 163 |
sub placeHold { |
| 164 |
my ($query, $bibId, $itemId, $userId, |
| 165 |
$branch, $startdate, $expirationdate, $notes, |
| 166 |
$rank, $request |
| 167 |
) = @_; |
| 168 |
|
| 169 |
my $found; |
| 170 |
|
| 171 |
my $userExists = C4::Members::GetBorrowerCategorycode($userId); |
| 172 |
|
| 173 |
C4::NCIP::NcipUtils::print404($query, "User not found..") |
| 174 |
unless $userExists; |
| 175 |
|
| 176 |
my $reserveId = C4::Reserves::AddReserve( |
| 177 |
$branch, $userId, $bibId, 'a', |
| 178 |
undef, $rank, $startdate, $expirationdate, |
| 179 |
$notes, undef, $itemId, $found |
| 180 |
); |
| 181 |
|
| 182 |
my $results; |
| 183 |
|
| 184 |
$results->{'status'} = 'reserved'; |
| 185 |
$results->{'bibId'} = $bibId; |
| 186 |
$results->{'userId'} = $userId; |
| 187 |
$results->{'requestId'} = $reserveId; |
| 188 |
|
| 189 |
$results->{'itemId'} = $itemId if $itemId; |
| 190 |
|
| 191 |
C4::NCIP::NcipUtils::printJson($query, $results); |
| 192 |
} |
| 193 |
|
| 194 |
1; |