1 # ---------------------------------------------------------------
2 # Copyright © 2014 Jason J.A. Stephenson <jason@sigio.com>
4 # This file is part of NCIPServer.
6 # NCIPServer is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # NCIPServer is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with NCIPServer. If not, see <http://www.gnu.org/licenses/>.
18 # ---------------------------------------------------------------
19 package NCIP::ILS::Evergreen;
22 use XML::LibXML::Simple qw(XMLin);
24 use DateTime::Format::ISO8601;
25 use Digest::MD5 qw/md5_hex/;
27 use OpenSRF::AppSession;
28 use OpenSRF::Utils qw/:datetime/;
29 use OpenSRF::Utils::SettingsClient;
30 use OpenILS::Utils::Fieldmapper;
31 use OpenILS::Utils::Normalize qw(clean_marc);
32 use OpenILS::Application::AppUtils;
33 use OpenILS::Const qw/:const/;
37 use List::MoreUtils qw/uniq/;
38 use POSIX qw/strftime/;
40 # We need a bunch of NCIP::* objects.
44 use NCIP::User::OptionalFields;
45 use NCIP::User::AddressInformation;
47 use NCIP::User::BlockOrTrap;
48 use NCIP::User::Privilege;
49 use NCIP::User::PrivilegeStatus;
50 use NCIP::StructuredPersonalUserName;
51 use NCIP::StructuredAddress;
52 use NCIP::ElectronicAddress;
53 use NCIP::PhysicalAddress;
56 use NCIP::Item::OptionalFields;
57 use NCIP::Item::BibliographicDescription;
58 use NCIP::Item::BibliographicItemId;
59 use NCIP::Item::BibliographicRecordId;
60 use NCIP::Item::Description;
62 # Inherit from NCIP::ILS.
63 use parent qw(NCIP::ILS);
67 Evergreen - Evergreen driver for NCIPServer
71 my $ils = NCIP::ILS::Evergreen->new(name => $config->{NCIP.ils.value});
75 NCIP::ILS::Evergreen is the default driver for Evergreen and
76 NCIPServer. It was initially developed to work with Auto-Graphics'
77 SHAREit software using a subset of an unspecified ILL/DCB profile.
81 # Default values we define for things that might be missing in our
82 # runtime environment or configuration file that absolutely must have
85 # OILS_NCIP_CONFIG_DEFAULT is the default location to find our
86 # driver's configuration file. This location can be overridden by
87 # setting the path in the OILS_NCIP_CONFIG environment variable.
89 # BIB_SOURCE_DEFAULT is the config.bib_source.id to use when creating
90 # "short" bibs. It is used only if no entry is supplied in the
91 # configuration file. The provided default is 2, the id of the
92 # "System Local" source that comes with a default Evergreen
95 OILS_NCIP_CONFIG_DEFAULT => '/openils/conf/oils_ncip.xml',
96 BIB_SOURCE_DEFAULT => 2
99 # A common Evergreen code shortcut to use AppUtils:
100 my $U = 'OpenILS::Application::AppUtils';
102 # The usual constructor:
105 $class = ref($class) if (ref $class);
107 # Instantiate our parent with the rest of the arguments. It
108 # creates a blessed hashref.
109 my $self = $class->SUPER::new(@_);
111 # Look for our configuration file, load, and parse it:
114 # Bootstrap OpenSRF and prepare some OpenILS components.
117 # Initialize the rest of our internal state.
123 =head1 HANDLER METHODS
127 $ils->lookupuser($request);
129 Processes a LookupUser request.
137 # Check our session and login if necessary.
138 $self->login() unless ($self->checkauth());
140 my $message_type = $self->parse_request_type($request);
142 # Let's go ahead and create our response object. We need this even
143 # if there is a problem.
144 my $response = NCIP::Response->new({type => $message_type . "Response"});
145 $response->header($self->make_header($request));
147 # Need to parse the request object to get the user barcode.
148 my ($barcode, $idfield) = $self->find_user_barcode($request);
150 # If we did not find a barcode, then report the problem.
151 if (ref($barcode) eq 'NCIP::Problem') {
152 $response->problem($barcode);
156 # Look up our patron by barcode:
157 my $user = $self->retrieve_user_by_barcode($barcode, $idfield);
158 if (ref($user) eq 'NCIP::Problem') {
159 $response->problem($user);
163 # We got the information, so lets fill in our userdata.
164 my $userdata = NCIP::User->new();
166 # Use the user's main card as the UserId.
167 my $id = NCIP::User::Id->new({
168 UserIdentifierType => 'Barcode',
169 UserIdentifierValue => $user->card->barcode() || $barcode
171 $userdata->UserId($id);
173 # Check if they requested any optional fields and return those.
174 my $elements = $request->{$message_type}->{UserElementType};
176 $elements = [$elements] unless (ref $elements eq 'ARRAY');
177 my $optionalfields = $self->handle_user_elements($user, $elements);
178 $userdata->UserOptionalFields($optionalfields);
181 $response->data($userdata);
188 $ils->acceptitem($request);
190 Processes an AcceptItem request.
198 # Check our session and login if necessary.
199 $self->login() unless ($self->checkauth());
201 # Common preparation.
202 my $message = $self->parse_request_type($request);
203 my $response = NCIP::Response->new({type => $message . 'Response'});
204 $response->header($self->make_header($request));
206 # We only accept holds for the time being.
207 if ($request->{$message}->{RequestedActionType} =~ /^hold\W/i) {
208 # We need the item id or we can't do anything at all.
209 my ($item_barcode, $item_idfield) = $self->find_item_barcode($request);
210 if (ref($item_barcode) eq 'NCIP::Problem') {
211 $response->problem($item_barcode);
215 # We need to find a patron barcode or we can't look anyone up
217 my ($user_barcode, $user_idfield) = $self->find_user_barcode($request, 'UserIdentifierValue');
218 if (ref($user_barcode) eq 'NCIP::Problem') {
219 $response->problem($user_barcode);
222 # Look up our patron by barcode:
223 my $user = $self->retrieve_user_by_barcode($user_barcode, $user_idfield);
224 if (ref($user) eq 'NCIP::Problem') {
225 $response->problem($user);
228 # We're doing patron checks before looking for bibliographic
229 # information and creating the item because problems with the
230 # patron are more likely to occur.
231 my $problem = $self->check_user_for_problems($user, 'HOLD');
233 $response->problem($problem);
237 # Check if the item barcode already exists:
238 my $item = $self->retrieve_copy_details_by_barcode($item_barcode);
240 # What to do here was not defined in the
241 # specification. Since the copies that we create this way
242 # should get deleted when checked in, it would be an error
243 # if we try to create another one. It means that something
244 # has gone wrong somewhere.
248 ProblemType => 'Duplicate Item',
249 ProblemDetail => "Item with barcode $item_barcode already exists.",
250 ProblemElement => $item_idfield,
251 ProblemValue => $item_barcode
258 # Now, we have to create our new copy and/or bib and call number.
260 # First, we have to gather the necessary information from the
261 # request. Store in a hashref for convenience. We may write a
262 # method to get this information in the future if we find we
263 # need it in other handlers. Such a function would be a
264 # candidate to go into our parent, NCIP::ILS.
266 barcode => $item_barcode,
267 call_number => $request->{$message}->{ItemOptionalFields}->{ItemDescription}->{CallNumber},
268 title => $request->{$message}->{ItemOptionalFields}->{BibliographicDescription}->{Title},
269 author => $request->{$message}->{ItemOptionalFields}->{BibliographicDescription}->{Author},
270 publisher => $request->{$message}->{ItemOptionalFields}->{BibliographicDescription}->{Publisher},
271 publication_date => $request->{$message}->{ItemOptionalFields}->{BibliographicDescription}->{PublicationDate},
272 medium => $request->{$message}->{ItemOptionalFields}->{BibliographicDescription}->{MediumType},
273 electronic => $request->{$message}->{ItemOptionalFields}->{BibliographicDescription}->{ElectronicResource}
276 if ($self->{config}->{items}->{use_precats}) {
277 # We only need to create a precat copy.
278 $item = $self->create_precat_copy($item_info);
280 # We have to create a "partial" bib record, a call number and a copy.
281 $item = $self->create_fuller_copy($item_info);
284 # If we failed to create the copy, report a problem.
288 ProblemType => 'Temporary Processing Failure',
289 ProblemDetail => 'Failed to create the item in the system',
290 ProblemElement => $item_idfield,
291 ProblemValue => $item_barcode
297 # We try to find the pickup location in our database. It's OK
298 # if it does not exist, the user's home library will be used
300 my $location = $self->find_location_failover($request->{$message}->{PickupLocation}, $request, $message);
302 # Now, we place the hold on the newly created copy on behalf
303 # of the patron retrieved above.
304 my $hold = $self->place_hold($item, $user, $location, undef, undef, 1);
305 if (ref($hold) eq 'NCIP::Problem') {
306 $response->problem($hold);
310 # Add a hold note with the RequestIdentifierValue for later
311 # lookup in CancelRequestItem. We do not care if it fails.
312 $self->create_hold_note($hold, 'NCIP Remote Request ID', $request->{$message}->{RequestId}->{RequestIdentifierValue});
314 # We return the RequestId and optionally, the ItemID. We'll
315 # just return what was sent to us, since we ignored all of it
318 $data->{RequestId} = NCIP::RequestId->new(
320 AgencyId => $request->{$message}->{RequestId}->{AgencyId},
321 RequestIdentifierType => $request->{$message}->{RequestId}->{RequestIdentifierType},
322 RequestIdentifierValue => $request->{$message}->{RequestId}->{RequestIdentifierValue}
325 $data->{ItemId} = NCIP::Item::Id->new(
327 AgencyId => $request->{$message}->{ItemId}->{AgencyId},
328 ItemIdentifierType => $request->{$message}->{ItemId}->{ItemIdentifierType},
329 ItemIdentifierValue => $request->{$message}->{ItemId}->{ItemIdentifierValue}
332 $response->data($data);
335 my $problem = NCIP::Problem->new();
336 $problem->ProblemType('Unauthorized Combination Of Element Values For System');
337 $problem->ProblemDetail('We only support Hold For Pickup');
338 $problem->ProblemElement('RequestedActionType');
339 $problem->ProblemValue($request->{$message}->{RequestedActionType});
340 $response->problem($problem);
348 $response = $ils->checkinitem($request);
350 Checks the item in if we can find the barcode in the message. It
351 returns problems if it cannot find the item in the system or if the
352 item is not checked out.
354 It could definitely use some more brains at some point as it does not
355 fully support everything that the standard allows. It also does not
356 really check if the checkin succeeded or not.
364 # Check our session and login if necessary:
365 $self->login() unless ($self->checkauth());
368 my $message = $self->parse_request_type($request);
369 my $response = NCIP::Response->new({type => $message . 'Response'});
370 $response->header($self->make_header($request));
372 # We need the copy barcode from the message.
373 my ($item_barcode, $item_idfield) = $self->find_item_barcode($request);
374 if (ref($item_barcode) eq 'NCIP::Problem') {
375 $response->problem($item_barcode);
379 # Retrieve the copy details.
380 my $details = $self->retrieve_copy_details_by_barcode($item_barcode);
382 # Return an Unknown Item problem unless we find the copy.
386 ProblemType => 'Unknown Item',
387 ProblemDetail => "Item with barcode $item_barcode is not known.",
388 ProblemElement => $item_idfield,
389 ProblemValue => $item_barcode
396 # Check if a UserId was provided. If so, this is the patron to
397 # whom the copy should be checked out.
399 my ($user_barcode, $user_idfield) = $self->find_user_barcode($request);
400 # We ignore the problem, because the UserId is optional.
401 if (ref($user_barcode) ne 'NCIP::Problem') {
402 $user = $self->retrieve_user_by_barcode($user_barcode, $user_idfield);
403 # We don't ignore a problem here, however.
404 if (ref($user) eq 'NCIP::Problem') {
405 $response->problem($user);
411 my $copy = $details->{copy};
413 # Look for a circulation and examine its information:
414 my $circ = $details->{circ};
416 # Check the circ details to see if the copy is checked out and, if
417 # the patron was provided, that it is checked out to the patron in
418 # question. We also verify the copy ownership and circulation
420 my $problem = $self->check_circ_details($details, $user);
422 # We need to fill in some information, however.
423 if (!$problem->ProblemValue() && !$problem->ProblemElement()) {
424 $problem->ProblemValue($user_barcode);
425 $problem->ProblemElement($user_idfield);
426 } elsif (!$problem->ProblemElement()) {
427 $problem->ProblemElement($item_idfield);
429 $response->problem($problem);
433 # Checkin parameters. We want to skip hold targeting or making
434 # transits, to force the checkin despite the copy status, as
435 # well as void overdues.
437 copy_barcode => $copy->barcode(),
442 my $result = $U->simplereq(
444 'open-ils.circ.checkin.override',
445 $self->{session}->{authtoken},
448 if (ref($result) eq 'ARRAY') {
449 $result = $result->[0];
451 if ($result->{textcode} eq 'SUCCESS') {
452 # We need to retrieve the copy again because its status may
453 # have changed, and it could have been in a state that won't
454 # let us delete it before the checkin.
455 $details = $self->retrieve_copy_details_by_barcode($copy->barcode());
456 $copy = $details->{copy};
458 # Delete the copy. Since delete_copy checks ownership
459 # before attempting to delete the copy, we don't bother
460 # checking who owns it.
461 $self->delete_copy($copy);
462 # We need the circulation user for the information below, so we retrieve it.
463 my $circ_user = $self->retrieve_user_by_id($circ->usr());
465 ItemId => NCIP::Item::Id->new(
467 AgencyId => $request->{$message}->{ItemId}->{AgencyId},
468 ItemIdentifierType => $request->{$message}->{ItemId}->{ItemIdentifierType},
469 ItemIdentifierValue => $request->{$message}->{ItemId}->{ItemIdentifierValue}
472 UserId => NCIP::User::Id->new(
474 UserIdentifierType => 'Barcode Id',
475 UserIdentifierValue => $circ_user->card->barcode()
480 # Look for UserElements requested and add it to the response:
481 my $elements = $request->{$message}->{UserElementType};
483 $elements = [$elements] unless (ref $elements eq 'ARRAY');
484 my $optionalfields = $self->handle_user_elements($circ_user, $elements);
485 $data->{UserOptionalFields} = $optionalfields;
487 $elements = $request->{$message}->{ItemElementType};
489 $elements = [$elements] unless (ref $elements eq 'ARRAY');
490 my $optionalfields = $self->handle_item_elements($copy, $elements);
491 $data->{ItemOptionalFields} = $optionalfields;
494 $response->data($data);
496 # At some point in the future, we should probably check if
497 # they requested optional user or item elements and return
498 # those. For the time being, we ignore those at the risk of
499 # being considered non-compliant.
501 $response->problem(_problem_from_event('Checkin Failed', $result));
509 $response = $ils->renewitem($request);
511 Handle the RenewItem message.
519 # Check our session and login if necessary:
520 $self->login() unless ($self->checkauth());
523 my $message = $self->parse_request_type($request);
524 my $response = NCIP::Response->new({type => $message . 'Response'});
525 $response->header($self->make_header($request));
527 # We need the copy barcode from the message.
528 my ($item_barcode, $item_idfield) = $self->find_item_barcode($request);
529 if (ref($item_barcode) eq 'NCIP::Problem') {
530 $response->problem($item_barcode);
534 # Retrieve the copy details.
535 my $details = $self->retrieve_copy_details_by_barcode($item_barcode);
537 # Return an Unknown Item problem unless we find the copy.
541 ProblemType => 'Unknown Item',
542 ProblemDetail => "Item with barcode $item_barcode is not known.",
543 ProblemElement => $item_idfield,
544 ProblemValue => $item_barcode
551 # User is required for RenewItem.
552 my ($user_barcode, $user_idfield) = $self->find_user_barcode($request);
553 if (ref($user_barcode) eq 'NCIP::Problem') {
554 $response->problem($user_barcode);
557 my $user = $self->retrieve_user_by_barcode($user_barcode, $user_idfield);
558 if (ref($user) eq 'NCIP::Problem') {
559 $response->problem($user);
564 my $copy = $details->{copy};
566 # Look for a circulation and examine its information:
567 my $circ = $details->{circ};
569 # Check the circ details to see if the copy is checked out and, if
570 # the patron was provided, that it is checked out to the patron in
571 # question. We also verify the copy ownership and circulation
573 my $problem = $self->check_circ_details($details, $user);
575 # We need to fill in some information, however.
576 if (!$problem->ProblemValue() && !$problem->ProblemElement()) {
577 $problem->ProblemValue($user_barcode);
578 $problem->ProblemElement($user_idfield);
579 } elsif (!$problem->ProblemElement()) {
580 $problem->ProblemElement($item_idfield);
582 $response->problem($problem);
586 # Check if user is blocked from renewals:
587 $problem = $self->check_user_for_problems($user, 'RENEW');
589 # Replace the ProblemElement and ProblemValue fields.
590 $problem->ProblemElement($user_idfield);
591 $problem->ProblemValue($user_barcode);
592 $response->problem($problem);
596 # Check if the duration rule allows renewals. It should have been
597 # fleshed during the copy details retrieve.
598 my $rule = $circ->duration_rule();
599 unless (ref($rule)) {
600 $rule = $U->simplereq(
602 'open-ils.pcrud.retrieve.crcd',
603 $self->{session}->{authtoken},
607 if ($rule->max_renewals() < 1) {
611 ProblemType => 'Item Not Renewable',
612 ProblemDetail => 'Item may not be renewed.',
613 ProblemElement => $item_idfield,
614 ProblemValue => $item_barcode
621 # Check if there are renewals remaining on the latest circ:
622 if ($circ->renewal_remaining() < 1) {
626 ProblemType => 'Maximum Renewals Exceeded',
627 ProblemDetail => 'Renewal cannot proceed because the User has already renewed the Item the maximum number of times permitted.',
628 ProblemElement => $item_idfield,
629 ProblemValue => $item_barcode
636 # Now, we attempt the renewal. If it fails, we simply say that the
637 # user is not allowed to renew this item, without getting into
640 copy_id => $copy->id(),
641 patron_id => $user->id(),
644 my $r = $U->simplereq(
646 'open-ils.circ.renew.override',
647 $self->{session}->{authtoken},
651 # We only look at the first one, since more than one usually means
653 if (ref($r) eq 'ARRAY') {
656 if ($r->{textcode} ne 'SUCCESS') {
657 $problem = _problem_from_event('Renewal Failed', $r);
658 $response->problem($problem);
661 ItemId => NCIP::Item::Id->new(
663 AgencyId => $request->{$message}->{ItemId}->{AgencyId},
664 ItemIdentifierType => $request->{$message}->{ItemId}->{ItemIdentifierType},
665 ItemIdentifierValue => $request->{$message}->{ItemId}->{ItemIdentifierValue}
668 UserId => NCIP::User::Id->new(
670 UserIdentifierType => 'Barcode Id',
671 UserIdentifierValue => $user->card->barcode()
675 # We need to retrieve the copy details again to refresh our
676 # circ information to get the new due date.
677 $details = $self->retrieve_copy_details_by_barcode($item_barcode);
678 $circ = $details->{circ};
679 $data->{DateDue} = _fix_date($circ->due_date());
681 # Look for UserElements requested and add it to the response:
682 my $elements = $request->{$message}->{UserElementType};
684 $elements = [$elements] unless (ref $elements eq 'ARRAY');
685 my $optionalfields = $self->handle_user_elements($user, $elements);
686 $data->{UserOptionalFields} = $optionalfields;
688 $elements = $request->{$message}->{ItemElementType};
690 $elements = [$elements] unless (ref $elements eq 'ARRAY');
691 my $optionalfields = $self->handle_item_elements($details->{copy}, $elements);
692 $data->{ItemOptionalFields} = $optionalfields;
695 $response->data($data);
698 # At some point in the future, we should probably check if
699 # they requested optional user or item elements and return
700 # those. For the time being, we ignore those at the risk of
701 # being considered non-compliant.
708 $response = $ils->checkoutitem($request);
710 Handle the Checkoutitem message.
718 # Check our session and login if necessary:
719 $self->login() unless ($self->checkauth());
722 my $message = $self->parse_request_type($request);
723 my $response = NCIP::Response->new({type => $message . 'Response'});
724 $response->header($self->make_header($request));
726 # We need the copy barcode from the message.
727 my ($item_barcode, $item_idfield) = $self->find_item_barcode($request);
728 if (ref($item_barcode) eq 'NCIP::Problem') {
729 $response->problem($item_barcode);
733 # Retrieve the copy details.
734 my $details = $self->retrieve_copy_details_by_barcode($item_barcode);
736 # Return an Unknown Item problem unless we find the copy.
740 ProblemType => 'Unknown Item',
741 ProblemDetail => "Item with barcode $item_barcode is not known.",
742 ProblemElement => $item_idfield,
743 ProblemValue => $item_barcode
750 # User is required for CheckOutItem.
751 my ($user_barcode, $user_idfield) = $self->find_user_barcode($request);
752 if (ref($user_barcode) eq 'NCIP::Problem') {
753 $response->problem($user_barcode);
756 my $user = $self->retrieve_user_by_barcode($user_barcode, $user_idfield);
757 if (ref($user) eq 'NCIP::Problem') {
758 $response->problem($user);
763 my $copy = $details->{copy};
765 # Check if the copy can circulate.
766 unless ($self->copy_can_circulate($copy)) {
770 ProblemType => 'Item Does Not Circulate',
771 ProblemDetail => "Item with barcode $item_barcode does not circulate.",
772 ProblemElement => $item_idfield,
773 ProblemValue => $item_barcode
780 # Look for a circulation and examine its information:
781 my $circ = $details->{circ};
783 # Check if the item is already checked out.
784 if ($circ && !$circ->checkin_time()) {
788 ProblemType => 'Item Already Checked Out',
789 ProblemDetail => "Item with barcode $item_barcode is already checked out.",
790 ProblemElement => $item_idfield,
791 ProblemValue => $item_barcode
798 # Check if user is blocked from circulation:
799 my $problem = $self->check_user_for_problems($user, 'CIRC');
801 # Replace the ProblemElement and ProblemValue fields.
802 $problem->ProblemElement($user_idfield);
803 $problem->ProblemValue($user_barcode);
804 $response->problem($problem);
808 # Check for the copy being in transit and receive or abort it.
809 my $transit = $U->simplereq(
811 'open-ils.circ.open_copy_transit.retrieve',
812 $self->{session}->{authtoken},
815 if (ref($transit) eq 'Fieldmapper::action::transit_copy') {
816 if ($transit->dest() == $self->{session}->{work_ou}->id()) {
817 my $r = $U->simplereq(
819 'open-ils.circ.copy_transit.receive',
820 $self->{session}->{authtoken},
821 {copyid => $copy->id()}
823 } elsif ($transit->source() == $self->{session}->{work_ou}->id()) {
824 my $r = $U->simplereq(
826 'open-ils.circ.transit.abort',
827 $self->{session}->{authtoken},
828 {copyid => $copy->id()}
833 # Now, we attempt the check out. If it fails, we simply say that
834 # the user is not allowed to check out this item, without getting
837 copy_id => $copy->id(),
838 patron_id => $user->id(),
840 my $r = $U->simplereq(
842 'open-ils.circ.checkout.full.override',
843 $self->{session}->{authtoken},
847 # We only look at the first one, since more than one usually means
849 if (ref($r) eq 'ARRAY') {
852 if ($r->{textcode} ne 'SUCCESS') {
853 $problem = _problem_from_event('Check Out Failed', $r);
854 $response->problem($problem);
857 ItemId => NCIP::Item::Id->new(
859 AgencyId => $request->{$message}->{ItemId}->{AgencyId},
860 ItemIdentifierType => $request->{$message}->{ItemId}->{ItemIdentifierType},
861 ItemIdentifierValue => $request->{$message}->{ItemId}->{ItemIdentifierValue}
864 UserId => NCIP::User::Id->new(
866 UserIdentifierType => 'Barcode Id',
867 UserIdentifierValue => $user->card->barcode()
871 # We need to retrieve the copy details again to refresh our
872 # circ information to get the due date.
873 $details = $self->retrieve_copy_details_by_barcode($item_barcode);
874 $circ = $details->{circ};
875 $data->{DateDue} = _fix_date($circ->due_date());
877 # Look for UserElements requested and add it to the response:
878 my $elements = $request->{$message}->{UserElementType};
880 $elements = [$elements] unless (ref $elements eq 'ARRAY');
881 my $optionalfields = $self->handle_user_elements($user, $elements);
882 $data->{UserOptionalFields} = $optionalfields;
884 $elements = $request->{$message}->{ItemElementType};
886 $elements = [$elements] unless (ref $elements eq 'ARRAY');
887 my $optionalfields = $self->handle_item_elements($details->{copy}, $elements);
888 $data->{ItemOptionalFields} = $optionalfields;
891 $response->data($data);
894 # At some point in the future, we should probably check if
895 # they requested optional user or item elements and return
896 # those. For the time being, we ignore those at the risk of
897 # being considered non-compliant.
904 $response = $ils->requestitem($request);
906 Handle the NCIP RequestItem message.
913 # Check our session and login if necessary:
914 $self->login() unless ($self->checkauth());
917 my $message = $self->parse_request_type($request);
918 my $response = NCIP::Response->new({type => $message . 'Response'});
919 $response->header($self->make_header($request));
921 # Because we need to have a user to place a hold, because the user
922 # is likely to have problems, and because getting the item
923 # information for the hold is trickier than getting the user
924 # information, we'll do the user first and short circuit out of
925 # the function if there is a problem with the user.
926 my ($user_barcode, $user_idfield) = $self->find_user_barcode($request);
927 if (ref($user_barcode) eq 'NCIP::Problem') {
928 $response->problem($user_barcode);
931 my $user = $self->retrieve_user_by_barcode($user_barcode, $user_idfield);
932 if (ref($user) eq 'NCIP::Problem') {
933 $response->problem($user);
936 my $problem = $self->check_user_for_problems($user, 'HOLD');
938 $response->problem($problem);
942 # Auto-Graphics send a single BibliographicRecordId to identify
943 # the "item" to place on hold.
945 if ($request->{$message}->{BibliographicId}) {
946 my $idxml = $request->{$message}->{BibliographicId};
947 # The standard allows more than 1. If that hapens, we only
949 $idxml = $idxml->[0] if (ref($idxml) eq 'ARRAY');
950 if ($idxml->{BibliographicRecordId}) {
951 $bibid = NCIP::Item::BibliographicRecordId->new(
952 $idxml->{BibliographicRecordId}
956 unless ($bibid && $bibid->{BibliographicRecordIdentifier}) {
957 $problem = NCIP::Problem->new(
959 ProblemType => 'Needed Data Missing',
960 ProblemDetail => 'Need BibliographicRecordIdentifier to place request',
961 ProblemElement => 'BibliographicRecordIdentifier',
962 ProblemValue => 'NULL'
965 $response->problem($problem);
969 # We need an actual bre.
970 my $bre = $self->retrieve_biblio_record_entry($bibid->{BibliographicRecordIdentifier});
971 if (!$bre || $U->is_true($bre->deleted())) {
972 $problem = NCIP::Problem->new(
974 ProblemType => 'Unknown Item',
975 ProblemDetail => 'Item ' . $bibid->{BibliographicRecordIdentifier} . ' is unknown',
976 ProblemElement => 'BibliographicRecordIdentifier',
977 ProblemValue => $bibid->{BibliographicRecordIdentifier}
980 $response->problem($problem);
984 # Auto-Graphics expects us to limit the selection ou for the hold
985 # to a given library. We look fo that in the AgencyId of the
986 # BibliographRecordId or in the ToAgencyId of the main message.
987 my $selection_ou = $self->find_location_failover($bibid->{AgencyId}, $request, $message);
988 unless ($selection_ou) {
989 $problem = NCIP::Problem->new(
991 ProblemType => 'Unknown Agency',
992 ProblemDetail => 'Agency is not known',
993 ProblemElement => 'BibliographicRecordIdentifier',
994 ProblemValue => $bibid->{AgencyId} || $request->{$message}->{InitiationHeader}->{ToAgencyId}->{AgencyId}
997 $response->problem($problem);
1001 # See if we were given a pickup location.
1003 if ($request->{$message}->{PickupLocation}) {
1004 my $loc = $request->{$message}->{PickupLocation};
1006 $pickup_ou = $self->retrieve_org_unit_by_shortname($loc);
1009 # Look for a NeedBeforeDate to set the expiration.
1010 my $expiration = $request->{$message}->{NeedBeforeDate};
1012 # Check for eligible copies:
1013 if ($self->count_eligible_copies($bre, $user, $pickup_ou, $selection_ou) == 0) {
1017 ProblemType => 'Item Not Available By Need Before Date',
1018 ProblemDetail => 'Item requested will not be available by the date/time the User needs the Item',
1019 ProblemElement => 'BibliographicRecordIdentifier',
1020 ProblemValue => $bre->id()
1028 my $hold = $self->place_hold($bre, $user, $pickup_ou, $expiration, $selection_ou);
1029 if (ref($hold) eq 'NCIP::Problem') {
1030 $response->problem($hold);
1032 # Add a hold note with the RequestIdentifierValue for later
1033 # lookup in CancelRequestItem. We do not care if it fails.
1034 $self->create_hold_note($hold, 'NCIP Remote Request ID', $request->{$message}->{RequestId}->{RequestIdentifierValue});
1036 RequestId => NCIP::RequestId->new(
1037 $request->{$message}->{RequestId}
1039 ItemId => NCIP::Item::Id->new(
1041 AgencyId => $selection_ou->shortname(),
1042 ItemIdentifierValue => $bre->id(),
1043 ItemIdentifierType => 'SYSNUMBER'
1046 UserId => NCIP::User::Id->new(
1048 UserIdentifierValue => $user->card->barcode(),
1049 UserIdentifierType => 'Barcode Id'
1052 RequestType => $request->{$message}->{RequestType},
1053 RequestScopeType => $request->{$message}->{RequestScopeType},
1056 # Look for UserElements requested and add it to the response:
1057 my $elements = $request->{$message}->{UserElementType};
1059 $elements = [$elements] unless (ref $elements eq 'ARRAY');
1060 my $optionalfields = $self->handle_user_elements($user, $elements);
1061 $data->{UserOptionalFields} = $optionalfields;
1063 $elements = $request->{$message}->{ItemElementType};
1064 if ($elements && $hold->current_copy()) {
1065 my $copy_details = $self->retrieve_copy_details_by_id($hold->current_copy());
1066 $elements = [$elements] unless (ref($elements) eq 'ARRAY');
1067 my $optionalfields = $self->handle_item_elements($copy_details->{copy}, $elements);
1068 $data->{ItemOptionalFields} = $optionalfields;
1071 $response->data($data);
1077 =head2 cancelrequestitem
1079 $response = $ils->cancelrequestitem($request);
1081 Handle the NCIP CancelRequestItem message.
1085 sub cancelrequestitem {
1087 my $request = shift;
1088 # Check our session and login if necessary:
1089 $self->login() unless ($self->checkauth());
1092 my $message = $self->parse_request_type($request);
1093 my $response = NCIP::Response->new({type => $message . 'Response'});
1094 $response->header($self->make_header($request));
1096 # UserId is required by the standard, but we might not really need it.
1097 my ($user_barcode, $user_idfield) = $self->find_user_barcode($request);
1098 if (ref($user_barcode) eq 'NCIP::Problem') {
1099 $response->problem($user_barcode);
1102 my $user = $self->retrieve_user_by_barcode($user_barcode, $user_idfield);
1103 if (ref($user) eq 'NCIP::Problem') {
1104 $response->problem($user);
1108 # First, let's see if we can find the hold via the RequestId's
1109 # RequestIdentifierValue. If not, we fall back on the ItemId.
1110 my $hold = $self->find_hold_via_note('NCIP Remote Request ID', $request->{$message}->{RequestId}->{RequestIdentifierValue});
1113 my $item_id = $request->{$message}->{ItemId};
1115 my $idvalue = $item_id->{ItemIdentifierValue};
1116 my $itemagy = $item_id->{AgencyId};
1117 my $selection_ou = $self->find_location_failover($itemagy, $request, $message);
1118 unless ($selection_ou) {
1119 my $problem = NCIP::Problem->new(
1121 ProblemType => 'Unknown Agency',
1122 ProblemDetail => 'Agency is not known',
1123 ProblemElement => 'AgencyId',
1124 ProblemValue => $item_id->{AgencyId} || $request->{$message}->{InitiationHeader}->{ToAgencyId}->{AgencyId}
1127 $response->problem($problem);
1131 # We should support looking up holds by barcode, since we still
1132 # support placing them by barcode, but that is not how it is going
1133 # to work with Auto-Graphics, apparently. I'll leave the
1134 # reimplementation of that for a future enhancement.
1136 # See if we can find the hold:
1137 my $hold = $self->_hold_search($user, $idvalue, $selection_ou);
1142 # If there is a transit, abort it. NOTE: We do this before
1143 # canceling to avoid negative consequences of retargeting and
1144 # in case the reset of the hold done by the transit abort ever
1145 # messes with cancel_time, etc.
1146 if ($hold->transit() && $self->{abort_transit_on_hold_cancel}) {
1147 $self->abort_transit($hold->transit());
1149 my $result = $self->cancel_hold($hold);
1151 $response->problem(_problem_from_event("Temporary Processing Failure", $result));
1154 RequestId => NCIP::RequestId->new(
1156 AgencyId => $request->{$message}->{RequestId}->{AgencyId},
1157 RequestIdentifierType => $request->{$message}->{RequestId}->{RequestIdentifierType},
1158 RequestIdentifierValue => $request->{$message}->{RequestId}->{RequestIdentifierValue}
1161 UserId => NCIP::User::Id->new(
1163 UserIdentifierType => 'Barcode Id',
1164 UserIdentifierValue => $user->card->barcode()
1167 ItemId => NCIP::Item::Id->new(
1169 AgencyId => $request->{$message}->{ItemId}->{AgencyId},
1170 ItemIdentifierType => $request->{$message}->{ItemId}->{ItemIdentifierType},
1171 ItemIdentifierValue => $request->{$message}->{ItemId}->{ItemIdentifierValue}
1175 # Look for UserElements requested and add it to the response:
1176 my $elements = $request->{$message}->{UserElementType};
1178 $elements = [$elements] unless (ref $elements eq 'ARRAY');
1179 my $optionalfields = $self->handle_user_elements($user, $elements);
1180 $data->{UserOptionalFields} = $optionalfields;
1182 $elements = $request->{$message}->{ItemElementType};
1183 if ($elements && $hold->current_copy()) {
1184 $elements = [$elements] unless (ref $elements eq 'ARRAY');
1185 my $copy_details = $self->retrieve_copy_details_by_id($hold->current_copy());
1186 if ($copy_details) {
1187 my $optionalfields = $self->handle_item_elements($copy_details->{copy}, $elements);
1188 $data->{ItemOptionalFields} = $optionalfields;
1191 $response->data($data);
1197 ProblemType => 'Unknown Request',
1198 ProblemDetail => 'No request found for the item and user',
1199 ProblemElement => 'NULL',
1200 ProblemValue => 'NULL'
1209 =head1 METHODS USEFUL to SUBCLASSES
1211 =head2 handle_user_elements
1212 $useroptionalfield = $ils->handle_user_elements($user, $elements);
1214 Returns NCIP::User::OptionalFields for the given user and arrayref of
1219 sub handle_user_elements {
1222 my $elements = shift;
1223 my $optionalfields = NCIP::User::OptionalFields->new();
1225 # First, we'll look for name information.
1226 if (grep {$_ eq 'Name Information'} @$elements) {
1227 my $name = NCIP::StructuredPersonalUserName->new();
1228 $name->Surname($user->family_name());
1229 $name->GivenName($user->first_given_name());
1230 $name->Prefix($user->prefix());
1231 $name->Suffix($user->suffix());
1232 $optionalfields->NameInformation($name);
1235 # Next, check for user address information.
1236 if (grep {$_ eq 'User Address Information'} @$elements) {
1239 # See if the user has any valid, physcial addresses.
1240 foreach my $addr (@{$user->addresses()}) {
1241 next if ($U->is_true($addr->pending()));
1242 my $address = NCIP::User::AddressInformation->new({UserAddressRoleType=>$addr->address_type()});
1243 my $structured = NCIP::StructuredAddress->new();
1244 $structured->Line1($addr->street1());
1245 $structured->Line2($addr->street2());
1246 $structured->Locality($addr->city());
1247 $structured->Region($addr->state());
1248 $structured->PostalCode($addr->post_code());
1249 $structured->Country($addr->country());
1250 $address->PhysicalAddress(
1251 NCIP::PhysicalAddress->new(
1253 StructuredAddress => $structured,
1254 Type => 'Postal Address'
1258 push @$addresses, $address;
1261 # Right now, we're only sharing email address if the user
1263 if ($user->email()) {
1264 my $address = NCIP::User::AddressInformation->new({UserAddressRoleType=>'Email Address'});
1265 $address->ElectronicAddress(
1266 NCIP::ElectronicAddress->new({
1268 Data=>$user->email()
1271 push @$addresses, $address;
1273 # Auto-graphics asked for the phone numbers.
1274 if ($user->day_phone()) {
1275 my $address = NCIP::User::AddressInformation->new({UserAddressRoleType=>'Day Phone'});
1276 $address->ElectronicAddress(
1277 NCIP::ElectronicAddress->new(
1280 Data=>$user->day_phone()
1284 push @$addresses, $address;
1286 if ($user->evening_phone()) {
1287 my $address = NCIP::User::AddressInformation->new({UserAddressRoleType=>'Evening Phone'});
1288 $address->ElectronicAddress(
1289 NCIP::ElectronicAddress->new(
1291 Type=>'Evening Phone',
1292 Data=>$user->evening_phone()
1296 push @$addresses, $address;
1298 if ($user->other_phone()) {
1299 my $address = NCIP::User::AddressInformation->new({UserAddressRoleType=>'Other Phone'});
1300 $address->ElectronicAddress(
1301 NCIP::ElectronicAddress->new(
1303 Type=>'Other Phone',
1304 Data=>$user->other_phone()
1308 push @$addresses, $address;
1311 $optionalfields->UserAddressInformation($addresses);
1314 # Check for User Privilege.
1315 if (grep {$_ eq 'User Privilege'} @$elements) {
1316 # Get the user's group:
1317 my $pgt = $U->simplereq(
1319 'open-ils.pcrud.retrieve.pgt',
1320 $self->{session}->{authtoken},
1324 my $privilege = NCIP::User::Privilege->new();
1325 $privilege->AgencyId($user->home_ou->shortname());
1326 $privilege->AgencyUserPrivilegeType($pgt->name());
1327 $privilege->ValidToDate(_fix_date($user->expire_date()));
1328 $privilege->ValidFromDate(_fix_date($user->create_date()));
1330 my $status = 'Active';
1331 if (_expired($user)) {
1332 $status = 'Expired';
1333 } elsif ($U->is_true($user->barred())) {
1335 } elsif (!$U->is_true($user->active())) {
1336 $status = 'Inactive';
1339 $privilege->UserPrivilegeStatus(
1340 NCIP::User::PrivilegeStatus->new({
1341 UserPrivilegeStatusType => $status
1346 $optionalfields->UserPrivilege([$privilege]);
1350 # Check for Block Or Trap.
1351 if (grep {$_ eq 'Block Or Trap'} @$elements) {
1354 # First, let's check if the profile is blocked from ILL.
1355 if (grep {$_->id() == $user->profile()} @{$self->{blocked_profiles}}) {
1356 my $block = NCIP::User::BlockOrTrap->new();
1357 $block->AgencyId($user->home_ou->shortname());
1358 $block->BlockOrTrapType('Block Interlibrary Loan');
1359 push @$blocks, $block;
1362 # Next, we loop through the user's standing penalties
1363 # looking for blocks on CIRC, HOLD, and RENEW.
1364 my ($have_circ, $have_renew, $have_hold) = (0,0,0);
1365 foreach my $penalty (@{$user->standing_penalties()}) {
1366 next unless($penalty->standing_penalty->block_list());
1367 my @block_list = split(/\|/, $penalty->standing_penalty->block_list());
1368 my $ou = $U->simplereq(
1370 'open-ils.pcrud.retrieve.aou',
1371 $self->{session}->{authtoken},
1372 $penalty->org_unit()
1376 if (!$have_circ && grep {$_ eq 'CIRC'} @block_list) {
1377 my $bot = NCIP::User::BlockOrTrap->new();
1378 $bot->AgencyId($ou->shortname());
1379 $bot->BlockOrTrapType('Block Checkout');
1380 push @$blocks, $bot;
1385 if (!$have_hold && grep {$_ eq 'HOLD' || $_ eq 'FULFILL'} @block_list) {
1386 my $bot = NCIP::User::BlockOrTrap->new();
1387 $bot->AgencyId($ou->shortname());
1388 $bot->BlockOrTrapType('Block Holds');
1389 push @$blocks, $bot;
1394 if (!$have_renew && grep {$_ eq 'RENEW'} @block_list) {
1395 my $bot = NCIP::User::BlockOrTrap->new();
1396 $bot->AgencyId($ou->shortname());
1397 $bot->BlockOrTrapType('Block Renewals');
1398 push @$blocks, $bot;
1402 # Stop after we report one of each, even if more
1404 last if ($have_circ && $have_renew && $have_hold);
1407 $optionalfields->BlockOrTrap($blocks);
1410 return $optionalfields;
1413 =head2 handle_item_elements
1417 sub handle_item_elements {
1420 my $elements = shift;
1421 my $optionalfields = NCIP::Item::OptionalFields->new();
1423 my $details; # In case we need for more than one.
1425 if (grep {$_ eq 'Bibliographic Description'} @$elements) {
1427 # Check for a precat copy, 'cause it is simple.
1428 if ($copy->dummy_title()) {
1429 $description = NCIP::Item::BibliographicDescription->new();
1430 $description->Title($copy->dummy_title());
1431 $description->Author($copy->dummy_author());
1432 if ($copy->dummy_isbn()) {
1433 $description->BibliographicItemId(
1434 NCIP::Item::BibliographicItemId->new(
1436 BibliographicItemIdentifier => $copy->dummy_isbn(),
1437 BibliographicItemIdentifierCode => 'ISBN'
1443 $details = $self->retrieve_copy_details_by_barcode($copy->barcode()) unless($details);
1444 $description = NCIP::Item::BibliographicDescription->new();
1445 $description->Title($details->{mvr}->title());
1446 $description->Author($details->{mvr}->author());
1447 $description->BibliographicRecordId(
1448 NCIP::Item::BibliographicRecordId->new(
1450 BibliographicRecordIdentifier => $details->{mvr}->doc_id(),
1451 BibliographicRecordIdentifierCode => 'SYSNUMBER'
1455 if ($details->{mvr}->publisher()) {
1456 $description->Publisher($details->{mvr}->publisher());
1458 if ($details->{mvr}->pubdate()) {
1459 $description->PublicationDate($details->{mvr}->pubdate());
1461 if ($details->{mvr}->edition()) {
1462 $description->Edition($details->{mvr}->edition());
1465 $optionalfields->BibliographicDescription($description) if ($description);
1468 if (grep {$_ eq 'Item Description'} @$elements) {
1469 $details = $self->retrieve_copy_details_by_barcode($copy->barcode()) unless($details);
1470 # Call Number is the only field we currently return. We also
1471 # do not attempt to retun a prefix and suffix. Someone else
1472 # can deal with that if they want it.
1473 if ($details->{volume}) {
1474 $optionalfields->ItemDescription(
1475 NCIP::Item::Description->new(
1476 {CallNumber => $details->{volume}->label()}
1482 if (grep {$_ eq 'Circulation Status'} @$elements) {
1483 my $status = $copy->status();
1484 $status = $self->retrieve_copy_status($status) unless (ref($status));
1485 $optionalfields->CirculationStatus($status->name()) if ($status);
1488 if (grep {$_ eq 'Date Due'} @$elements) {
1489 $details = $self->retrieve_copy_details_by_barcode($copy->barcode()) unless($details);
1490 if ($details->{circ}) {
1491 if (!$details->{circ}->checkin_time()) {
1492 $optionalfields->DateDue(_fix_date($details->{circ}->due_date()));
1497 if (grep {$_ eq 'Item Use Restriction Type'} @$elements) {
1498 $optionalfields->ItemUseRestrictionType('None');
1501 if (grep {$_ eq 'Physical Condition'} @$elements) {
1502 $optionalfields->PhysicalCondition(
1503 NCIP::Item::PhysicalCondition->new(
1504 {PhysicalConditionType => 'Unknown'}
1509 return $optionalfields;
1516 Login to Evergreen via OpenSRF. It uses internal state from the
1517 configuration file to login.
1521 # Login via OpenSRF to Evergreen.
1525 # Get the authentication seed.
1526 my $seed = $U->simplereq(
1528 'open-ils.auth.authenticate.init',
1529 $self->{config}->{credentials}->{username}
1534 my $response = $U->simplereq(
1536 'open-ils.auth.authenticate.complete',
1538 username => $self->{config}->{credentials}->{username},
1539 password => md5_hex(
1540 $seed . md5_hex($self->{config}->{credentials}->{password})
1543 workstation => $self->{config}->{credentials}->{workstation}
1547 $self->{session}->{authtoken} = $response->{payload}->{authtoken};
1548 $self->{session}->{authtime} = $response->{payload}->{authtime};
1550 # Set/reset the work_ou and user data in case something changed.
1552 # Retrieve the work_ou as an object.
1553 $self->{session}->{work_ou} = $U->simplereq(
1555 'open-ils.pcrud.search.aou',
1556 $self->{session}->{authtoken},
1557 {shortname => $self->{config}->{credentials}->{work_ou}}
1560 # We need the user information in order to do some things.
1561 $self->{session}->{user} = $U->check_user_session($self->{session}->{authtoken});
1569 $valid = $ils->checkauth();
1571 Returns 1 if the object a 'valid' authtoken, 0 if not.
1578 # We use AppUtils to do the heavy lifting.
1579 if (defined($self->{session})) {
1580 if ($U->check_user_session($self->{session}->{authtoken})) {
1587 # If we reach here, we don't have a session, so we are definitely
1592 =head2 retrieve_user_by_barcode
1594 $user = $ils->retrieve_user_by_barcode($user_barcode, $user_idfield);
1596 Do a fleshed retrieve of a patron by barcode. Return the patron if
1597 found and valid. Return a NCIP::Problem of 'Unknown User' otherwise.
1599 The id field argument is used for the ProblemElement field in the
1600 NCIP::Problem object.
1602 An invalid patron is one where the barcode is not found in the
1603 database, the patron is deleted, or the barcode used to retrieve the
1604 patron is not active. The problem element is also returned if an error
1605 occurs during the retrieval.
1609 sub retrieve_user_by_barcode {
1610 my ($self, $barcode, $idfield) = @_;
1611 my $result = $U->simplereq(
1613 'open-ils.actor.user.fleshed.retrieve_by_barcode',
1614 $self->{session}->{authtoken},
1619 # Check for a failure, or a deleted, inactive, or expired user,
1620 # and if so, return empty userdata.
1621 if (!$result || $U->event_code($result) || $U->is_true($result->deleted())
1622 || !grep {$_->barcode() eq $barcode && $U->is_true($_->active())} @{$result->cards()}) {
1624 my $problem = NCIP::Problem->new();
1625 $problem->ProblemType('Unknown User');
1626 $problem->ProblemDetail("User with barcode $barcode unknown");
1627 $problem->ProblemElement($idfield);
1628 $problem->ProblemValue($barcode);
1635 =head2 retrieve_user_by_id
1637 $user = $ils->retrieve_user_by_id($id);
1639 Similar to C<retrieve_user_by_barcode> but takes the user's database
1640 id rather than barcode. This is useful when you have a circulation or
1641 hold and need to get information about the user's involved in the hold
1644 It returns a fleshed user on success or undef on failure.
1648 sub retrieve_user_by_id {
1649 my ($self, $id) = @_;
1651 # Do a fleshed retrieve of the patron, and flesh the fields that
1652 # we would normally use.
1653 my $result = $U->simplereq(
1655 'open-ils.actor.user.fleshed.retrieve',
1656 $self->{session}->{authtoken},
1658 [ 'card', 'cards', 'standing_penalties', 'addresses', 'home_ou', 'settings' ]
1660 # Check for an error.
1661 undef($result) if ($result && $U->event_code($result));
1666 =head2 check_user_for_problems
1668 $problem = $ils>check_user_for_problems($user, 'HOLD, 'CIRC', 'RENEW');
1670 This function checks if a user has a blocked profile or any from a
1671 list of provided blocks. If it does, then a NCIP::Problem object is
1672 returned, otherwise an undefined value is returned.
1674 The list of blocks appears as additional arguments after the user. You
1675 can provide any value(s) that might appear in a standing penalty block
1676 lit in Evergreen. The example above checks for HOLD, CIRC, and
1677 RENEW. Any number of such values can be provided. If none are
1678 provided, the function only checks if the patron's profiles appears in
1679 the object's blocked profiles list.
1681 It stops on the first matching block, if any.
1685 sub check_user_for_problems {
1690 # Fill this in if we have a problem, otherwise just return it.
1693 # First, check the user's profile.
1694 if (grep {$_->id() == $user->profile()} @{$self->{blocked_profiles}}) {
1695 $problem = NCIP::Problem->new(
1697 ProblemType => 'User Blocked',
1698 ProblemDetail => 'User blocked from inter-library loan',
1699 ProblemElement => 'NULL',
1700 ProblemValue => 'NULL'
1705 # Next, check if the patron has one of the indicated blocks.
1707 foreach my $penalty (@{$user->standing_penalties()}) {
1708 if ($penalty->standing_penalty->block_list()) {
1709 my @pblocks = split(/\|/, $penalty->standing_penalty->block_list());
1710 foreach my $block (@blocks) {
1711 if (grep {$_ =~ /$block/} @pblocks) {
1712 $problem = NCIP::Problem->new(
1714 ProblemType => 'User Blocked',
1715 ProblemDetail => 'User blocked from ' .
1716 ($block eq 'HOLD') ? 'holds' : (($block eq 'RENEW') ? 'renewals' :
1717 (($block eq 'CIRC') ? 'checkout' : lc($block))),
1718 ProblemElement => 'NULL',
1719 ProblemValue => 'NULL'
1733 =head2 check_circ_details
1735 $problem = $ils->check_circ_details($details, $user);
1737 Checks if we can checkin or renew a circulation. That is, the
1738 circulation is still open (i.e. the copy is still checked out), if we
1739 either own the copy or are the circulation location, and if the
1740 circulation is for the optional $user argument. The $details argument
1741 is required and comes from the retrieve_copy_details call. $user is
1744 Returns a problem if any of the above conditions fail. Returns undef
1745 if they pass and we can proceed with the checkin or renewal.
1747 If the failure occurred on the copy-related checks, then the
1748 ProblemElement field will be undefined and needs to be filled in with
1749 the item id field name. If the check for the copy being checked out to
1750 the provided user fails, then both ProblemElement and ProblemValue
1751 fields will be empty and need to be filled in by the caller.
1755 sub check_circ_details {
1756 my ($self, $details, $user) = @_;
1758 my $copy = $details->{copy};
1759 my $circ = $details->{circ};
1760 my $transit = $details->{transit};
1762 # Shortcut for the next check.
1763 my $ou_id = $self->{session}->{work_ou}->id();
1765 # We need to have a circulation. The copy needs to either have
1766 # been checked out at the NCIP user's working_ou or it needs to be
1767 # owned there. If the circulation was subsequently checked in,
1768 # then we need an open transit to the NCIP user's working_ou.
1769 if (!$circ || ($circ->circ_lib() != $ou_id && $copy->circ_lib() != $ou_id)
1770 || ($circ->checkin_time() && (!$transit || $transit->dest() != $ou_id))) {
1771 # Item isn't checked out.
1772 return NCIP::Problem->new(
1774 ProblemType => 'Item Not Checked Out',
1775 ProblemDetail => 'Item with barcode ' . $copy->barcode() . ' is not checked out.',
1776 ProblemValue => $copy->barcode()
1780 # Get data on the patron who has it checked out.
1781 my $circ_user = $self->retrieve_user_by_id($circ->usr());
1782 if ($user && $circ_user && $user->id() != $circ_user->id()) {
1783 # The ProblemElement and ProblemValue field need to be
1784 # filled in by the caller.
1785 return NCIP::Problem->new(
1787 ProblemType => 'Item Not Checked Out To This User',
1788 ProblemDetail => 'Item with barcode ' . $copy->barcode() . ' is not checked out to this user.',
1793 # If we get here, we're good to go.
1797 =head2 retrieve_copy_details_by_barcode
1799 $copy = $ils->retrieve_copy_details_by_barcode($copy_barcode);
1801 Look up and retrieve some copy details by the copy barcode. This
1802 method returns either a hashref with the copy details or undefined if
1803 no copy exists with that barcode or if some error occurs.
1805 The hashref has the fields copy, hold, transit, circ, volume, and mvr.
1807 This method differs from C<retrieve_user_by_barcode> in that a copy
1808 cannot be invalid if it exists and it is not always an error if no
1809 copy exists. In some cases, when handling AcceptItem, we might prefer
1810 there to be no copy.
1814 sub retrieve_copy_details_by_barcode {
1816 my $barcode = shift;
1818 my $copy = $U->simplereq(
1820 'open-ils.circ.copy_details.retrieve.barcode',
1821 $self->{session}->{authtoken},
1825 # If $copy is an event, return undefined.
1826 if ($copy && $U->event_code($copy)) {
1833 =head2 retrieve_copy_details_by_id
1835 $copy = $ils->retrieve_copy_details_by_id($copy_id);
1837 Retrieve copy_details by copy id. Same as the above, but with a copy
1838 id instead of barcode.
1842 sub retrieve_copy_details_by_id {
1844 my $copy_id = shift;
1846 my $copy = $U->simplereq(
1848 'open-ils.circ.copy_details.retrieve',
1849 $self->{session}->{authtoken},
1853 # If $copy is an event, return undefined.
1854 if ($copy && $U->event_code($copy)) {
1861 =head2 retrieve_copy_status
1863 $status = $ils->retrieve_copy_status($id);
1865 Retrive a copy status object by database ID.
1869 sub retrieve_copy_status {
1873 my $status = $U->simplereq(
1875 'open-ils.pcrud.retrieve.ccs',
1876 $self->{session}->{authtoken},
1883 =head2 retrieve_org_unit_by_shortname
1885 $org_unit = $ils->retrieve_org_unit_by_shortname($shortname);
1887 Retrieves an org. unit from the database by shortname, and fleshes the
1888 ou_type field. Returns the org. unit as a Fieldmapper object or
1893 sub retrieve_org_unit_by_shortname {
1895 my $shortname = shift;
1897 my $aou = $U->simplereq(
1899 'open-ils.actor.org_unit.retrieve_by_shortname',
1903 # Check for failure from the above.
1904 if (ref($aou) eq 'HASH') {
1908 # We want to retrieve the type and manually "flesh" the object.
1910 my $type = $U->simplereq(
1912 'open-ils.pcrud.retrieve.aout',
1913 $self->{session}->{authtoken},
1916 $aou->ou_type($type) if ($type);
1922 =head2 retrieve_copy_location
1924 $location = $ils->retrieve_copy_location($location_id);
1926 Retrieve a copy location based on id.
1930 sub retrieve_copy_location {
1934 my $location = $U->simplereq(
1936 'open-ils.pcrud.retrieve.acpl',
1937 $self->{session}->{authtoken},
1944 =head2 retrieve_biblio_record_entry
1946 $bre = $ils->retrieve_biblio_record_entry($bre_id);
1948 Given a biblio.record_entry.id, this method retrieves a bre object.
1952 sub retrieve_biblio_record_entry {
1956 my $bre = $U->simplereq(
1958 'open-ils.pcrud.retrieve.bre',
1959 $self->{session}->{authtoken},
1966 =head2 create_precat_copy
1969 barcode => '312340123456789',
1970 author => 'Public, John Q.',
1971 title => 'Magnum Opus',
1972 call_number => '005.82',
1973 publisher => 'Brick House',
1974 publication_date => '2014'
1977 $item = $ils->create_precat_copy($item_info);
1980 Create a "precat" copy to use for the incoming item using a hashref of
1981 item information. At a minimum, the barcode, author and title fields
1982 need to be filled in. The other fields are ignored if provided.
1984 This method is called by the AcceptItem handler if the C<use_precats>
1985 configuration option is turned on.
1989 sub create_precat_copy {
1991 my $item_info = shift;
1993 my $item = Fieldmapper::asset::copy->new();
1994 $item->barcode($item_info->{barcode});
1995 $item->call_number(OILS_PRECAT_CALL_NUMBER);
1996 $item->dummy_title($item_info->{title});
1997 $item->dummy_author($item_info->{author});
1998 $item->circ_lib($self->{session}->{work_ou}->id());
1999 $item->circulate('t');
2000 $item->holdable('t');
2001 $item->opac_visible('f');
2002 $item->deleted('f');
2003 $item->fine_level(OILS_PRECAT_COPY_FINE_LEVEL);
2004 $item->loan_duration(OILS_PRECAT_COPY_LOAN_DURATION);
2007 $item->editor($self->{session}->{user}->id());
2008 $item->creator($self->{session}->{user}->id());
2011 # Actually create it:
2013 my $ses = OpenSRF::AppSession->create('open-ils.pcrud');
2016 $xact = $ses->request(
2017 'open-ils.pcrud.transaction.begin',
2018 $self->{session}->{authtoken}
2020 $item = $ses->request(
2021 'open-ils.pcrud.create.acp',
2022 $self->{session}->{authtoken},
2025 $xact = $ses->request(
2026 'open-ils.pcrud.transaction.commit',
2027 $self->{session}->{authtoken}
2035 'open-ils.pcrud.transaction.rollback',
2036 $self->{session}->{authtoken}
2046 =head2 create_fuller_copy
2049 barcode => '31234003456789',
2050 author => 'Public, John Q.',
2051 title => 'Magnum Opus',
2052 call_number => '005.82',
2053 publisher => 'Brick House',
2054 publication_date => '2014'
2057 $item = $ils->create_fuller_copy($item_info);
2059 Creates a skeletal bibliographic record, call number, and copy for the
2060 incoming item using a hashref with item information in it. At a
2061 minimum, the barcode, author, title, and call_number fields must be
2064 This method is used by the AcceptItem handler if the C<use_precats>
2065 configuration option is NOT set.
2069 sub create_fuller_copy {
2071 my $item_info = shift;
2075 # We do everything in one transaction, because it should be atomic.
2076 my $ses = OpenSRF::AppSession->create('open-ils.pcrud');
2080 $xact = $ses->request(
2081 'open-ils.pcrud.transaction.begin',
2082 $self->{session}->{authtoken}
2089 # The rest depends on there being a transaction.
2092 # Create the MARC record.
2093 my $record = MARC::Record->new();
2094 $record->encoding('UTF-8');
2095 $record->leader('00881nam a2200193 4500');
2096 my $datespec = strftime("%Y%m%d%H%M%S.0", localtime);
2098 push(@fields, MARC::Field->new('005', $datespec));
2099 push(@fields, MARC::Field->new('082', '0', '4', 'a' => $item_info->{call_number}));
2100 push(@fields, MARC::Field->new('245', '0', '0', 'a' => $item_info->{title}));
2101 # Publisher is a little trickier:
2102 if ($item_info->{publisher}) {
2103 my $pub = MARC::Field->new('260', ' ', ' ', 'a' => '[S.l.]', 'b' => $item_info->{publisher});
2104 $pub->add_subfields('c' => $item_info->{publication_date}) if ($item_info->{publication_date});
2105 push(@fields, $pub);
2107 # We have no idea if the author is personal corporate or something else, so we use a 720.
2108 push(@fields, MARC::Field->new('720', ' ', ' ', 'a' => $item_info->{author}, '4' => 'aut'));
2109 $record->append_fields(@fields);
2110 my $marc = clean_marc($record);
2112 # Create the bib object.
2113 my $bib = Fieldmapper::biblio::record_entry->new();
2114 $bib->creator($self->{session}->{user}->id());
2115 $bib->editor($self->{session}->{user}->id());
2116 $bib->source($self->{bib_source}->id());
2123 $bib = $ses->request(
2124 'open-ils.pcrud.create.bre',
2125 $self->{session}->{authtoken},
2133 'open-ils.pcrud.transaction.rollback',
2134 $self->{session}->{authtoken}
2139 # Create the call number
2142 $acn = Fieldmapper::asset::call_number->new();
2143 $acn->creator($self->{session}->{user}->id());
2144 $acn->editor($self->{session}->{user}->id());
2145 $acn->label($item_info->{call_number});
2146 $acn->record($bib->id());
2147 $acn->owning_lib($self->{session}->{work_ou}->id());
2152 $acn = $ses->request(
2153 'open-ils.pcrud.create.acn',
2154 $self->{session}->{authtoken},
2162 'open-ils.pcrud.transaction.rollback',
2163 $self->{session}->{authtoken}
2171 $item = Fieldmapper::asset::copy->new();
2172 $item->barcode($item_info->{barcode});
2173 $item->call_number($acn->id());
2174 $item->circ_lib($self->{session}->{work_ou}->id);
2175 $item->circulate('t');
2176 if ($self->{config}->{items}->{use_force_holds}) {
2177 $item->holdable('f');
2179 $item->holdable('t');
2181 $item->opac_visible('f');
2182 $item->deleted('f');
2183 $item->fine_level(OILS_PRECAT_COPY_FINE_LEVEL);
2184 $item->loan_duration(OILS_PRECAT_COPY_LOAN_DURATION);
2187 $item->editor($self->{session}->{user}->id);
2188 $item->creator($self->{session}->{user}->id);
2192 $item = $ses->request(
2193 'open-ils.pcrud.create.acp',
2194 $self->{session}->{authtoken},
2198 # Cross our fingers and commit the work.
2199 $xact = $ses->request(
2200 'open-ils.pcrud.transaction.commit',
2201 $self->{session}->{authtoken}
2208 'open-ils.pcrud.transaction.rollback',
2209 $self->{session}->{authtoken}
2210 )->gather(1) if ($xact);
2216 # We need to disconnect our session.
2219 # Now, we handle our asset stat_cat entries.
2221 # It would be nice to do these in the above transaction, but
2222 # pcrud does not support the ascecm object, yet.
2223 foreach my $entry (@{$self->{stat_cat_entries}}) {
2224 my $map = Fieldmapper::asset::stat_cat_entry_copy_map->new();
2226 $map->stat_cat($entry->stat_cat());
2227 $map->stat_cat_entry($entry->id());
2228 $map->owning_copy($item->id());
2229 # We don't really worry if it succeeds or not.
2232 'open-ils.circ.stat_cat.asset.copy_map.create',
2233 $self->{session}->{authtoken},
2244 $hold = $ils->place_hold($item, $user, $location, $expiration, $org_unit, $do_notification);
2246 This function places a hold on $item for $user for pickup at
2247 $location. If location is not provided or undefined, the user's home
2248 library is used as a fallback.
2250 The $expiration argument is optional and must be a properly formatted
2251 ISO date time. It will be used as the hold expire time, if
2252 provided. Otherwise the system default time will be used.
2254 The $org_unit parameter is only consulted in the event of $item being
2255 a biblio::record_entry object. In which case, it is expected to be
2256 undefined or an actor::org_unit object. If it is present, then its id
2257 and ou_type depth (if the ou_type field is fleshed) will be used to
2258 control the selection ou and selection depth for the hold. This
2259 essentially limits the hold to being filled by copies belonging to the
2260 specified org_unit or its children.
2262 The $do_notification parameter is true or false for whether or not to
2263 add the patron's default notification options to the hold when placed.
2265 $item can be a copy (asset::copy), volume (asset::call_number), or bib
2266 (biblio::record_entry). The appropriate hold type will be placed
2267 depending on the object.
2269 On success, the method returns the object representing the hold. On
2270 failure, a NCIP::Problem object, describing the failure, is returned.
2278 my $location = shift;
2279 my $expiration = shift;
2280 my $org_unit = shift;
2281 my $do_notification = shift;
2283 # If $location is undefined, use the user's home_ou, which should
2284 # have been fleshed when the user was retrieved.
2285 $location = $user->home_ou() unless ($location);
2287 # $params for the hold.
2288 my $params = { pickup_lib => $location->id(), patronid => $user->id() };
2290 if (ref($item) eq 'Fieldmapper::asset::copy') {
2291 my $type = ($self->{config}->{items}->{use_force_holds}) ? 'F' : 'C';
2292 $params->{hold_type} = $type;
2293 } elsif (ref($item) eq 'Fieldmapper::asset::call_number') {
2294 $params->{hold_type} = 'V';
2295 } elsif (ref($item) eq 'Fieldmapper::biblio::record_entry') {
2296 $params->{hold_type} = 'T';
2297 if ($org_unit && ref($org_unit) eq 'Fieldmapper::actor::org_unit') {
2298 $params->{selection_ou} = $org_unit->id();
2299 $params->{depth} = $org_unit->ou_type->depth() if (ref($org_unit->ou_type()));
2303 # Notification params.
2304 if ($do_notification) {
2305 my ($hold_notify) = grep {$_->name() eq 'opac.hold_notify'} @{$user->settings()};
2307 if ($hold_notify->value() =~ /email/) {
2308 $params->{email_notify} = 1;
2310 if ($hold_notify->value() =~ /phone/) {
2311 my ($default_phone) = grep {$_->name() eq 'opac.default_phone'} @{$user->settings()};
2312 if ($default_phone) {
2313 $params->{phone_notify} = $default_phone->value();
2314 $params->{phone_notify} =~ s/"//g;
2315 } elsif ($user->day_phone()) {
2316 $params->{phone_notify} = $user->day_phone();
2319 if ($hold_notify->value() =~ /sms/) {
2320 my ($sms_carrier) = grep {$_->name() eq 'opac.default_sms_carrier'} @{$user->settings()};
2321 my ($sms_notify) = grep {$_->name() eq 'opac.default_sms_notify'} @{$user->settings()};
2322 if ($sms_carrier && $sms_notify) {
2323 $params->{sms_carrier} = $sms_carrier->value();
2324 $params->{sms_notify} = $sms_notify->value();
2325 $params->{sms_carrier} =~ s/"//g;
2326 $params->{sms_notify} =~ s/"//g;
2330 if ($user->email()) {
2331 $params->{email_notify} = 1;
2333 if ($user->day_phone()) {
2334 $params->{phone_notify} = $user->day_phone();
2339 my $response = $U->simplereq(
2341 'open-ils.circ.holds.test_and_create.batch',
2342 $self->{session}->{authtoken},
2347 if (ref($response->{result})) {
2348 my $event = (ref($response->{result}) eq 'ARRAY') ? $response->{result}->[0] : $response->{result}->{last_event};
2349 if ($event->{textcode} eq 'HOLD_EXISTS') {
2350 return NCIP::Problem->new(
2352 ProblemType => 'Duplicate Request',
2353 ProblemDetail => 'A request for this item already exists for this patron.',
2354 ProblemElement => 'NULL',
2355 ProblemValue => 'NULL'
2359 if ($event->{textcode} eq 'ITEM_NOT_HOLDABLE') {
2360 return NCIP::Problem->new(
2362 ProblemType => 'User Ineligible To Request This Item',
2363 ProblemDetail => 'Agency rules prevent the Item from being requested by the User.',
2364 ProblemElement => 'NULL',
2365 ProblemValue => 'NULL'
2369 if ($event->{textcode} eq 'HIGH_LEVEL_HOLD_HAS_NO_COPIES') {
2370 return NCIP::Problem->new(
2372 ProblemType => 'Unknown Item',
2373 ProblemDetail => 'Agency does not have an Item to fill this request.',
2374 ProblemElement => 'NULL',
2375 ProblemValue => 'NULL'
2379 return _problem_from_event('User Ineligible To Request This Item', $event);
2382 # If we make it here, $response->{result} is a hold id.
2383 my $hold = $U->simplereq(
2385 'open-ils.pcrud.retrieve.ahr',
2386 $self->{session}->{authtoken},
2392 =head2 count_eligible_copies
2394 $result = $ils->count_eligible_copies($target, $patron, $pickup_lib, $selection_lib);
2396 This method counts the copies eligible to fill the remote hold on the
2397 target bre for the patron at pickup lib where the copies are owned at
2398 or below the selection lib. It returns the count of copies eligible
2399 to fill the hold at the time of the call, so zero if none are
2400 available or a positive integer otherwise.
2404 sub count_eligible_copies {
2408 my $pickup_lib = shift;
2409 my $selection_lib = shift;
2411 $pickup_lib = $user->home_ou() unless ($pickup_lib);
2413 # To be used in the pcrud query:
2414 my $selection_ou = (ref($selection_lib)) ? $selection_lib->id() : 1;
2416 # Base params for hold is possible check:
2419 patronid => $user->id(),
2420 pickup_lib => $pickup_lib->id()
2423 # return value: count of eligible copies found.
2424 my $eligible_copies = 0;
2426 # pcrud query to find eligible copies:
2427 my $query = {deleted => 'f', circulate => 't', holdable => 't', status => [0,7]};
2428 # Limit copies by call numbers for the target bre:
2429 $query->{call_number} = {
2431 select => {acn => ['id']},
2434 record => $target->id(),
2440 transform => 'actor.org_unit_descendants',
2442 result_field => 'id'
2446 where => {id => $selection_ou}
2452 # Limit copies by circ_lib:
2453 $query->{circ_lib} = {
2458 transform => 'actor.org_unit_descendants',
2459 result_field => 'id'
2463 where => {id => $selection_ou}
2466 # Limit copies by copy locations that allow circ and holds:
2467 $query->{location} = {
2469 select => { acpl => ['id'] },
2471 where => {holdable => 't', circulate => 't'}
2474 # Search for the copies and check each one to see if it could fill the hold.
2475 my $search = OpenSRF::AppSession->create('open-ils.pcrud')->request(
2476 'open-ils.pcrud.search.acp',
2477 $self->{session}->{authtoken},
2480 while (my $response = $search->recv()) {
2481 if ($response->status() eq 'OK') {
2482 my $copy = $response->content();
2483 $params->{copy_id} = $copy->id();
2484 my $result = $U->simplereq(
2486 'open-ils.circ.title_hold.is_possible',
2487 $self->{session}->{authtoken},
2490 if ($result->{success}) {
2497 return $eligible_copies;
2502 $result = $ils->cancel_hold($hold);
2504 This method cancels the hold argument. It makes no checks on the hold,
2505 so if there are certain conditions that need to be fulfilled before
2506 the hold is canceled, then you must check them before calling this
2509 It returns the result of the backend call to cancel the hold: 1 on
2510 succes or an ILS event on failure.
2518 my $r = $U->simplereq(
2520 'open-ils.circ.hold.cancel',
2521 $self->{session}->{authtoken},
2524 'Canceled via NCIPServer'
2530 =head2 abort_transit
2532 $result = $ils->abort_transit($transit);
2534 This method aborts the passed in transit and returns true or false if
2535 it succeeded. In general, we don't care about the return value here,
2536 but subclasses might.
2542 my $transit = shift;
2543 my $result = $U->simplereq(
2545 'open-ils.circ.transit.abort',
2546 $self->{session}->{authtoken},
2547 {transitid => $transit->id()}
2555 =head2 create_hold_note
2557 $note = $ils->create_hold_note($hold, $title, $body);
2559 This method creates a nold note with title of $title and body of $body
2560 on $hold. It is used to store the RequestIdentifierValue from the
2561 RequestItem message so that we can later retrieve holds using that ID
2562 in order to cancel them.
2564 It returns a note object on success and undef on failure.
2568 sub create_hold_note {
2574 my $note = Fieldmapper::action::hold_request_note->new();
2576 $note->hold($hold->id());
2577 $note->title($title);
2582 my $result = $U->simplereq(
2584 'open-ils.circ.hold_request.note.cud',
2585 $self->{session}->{authtoken},
2588 if (not ref($result)) {
2595 =head2 find_hold_via_note
2597 $hold = $ils->find_hold_via_note($title, $body);
2599 Searches for a hold based on a note title and note body. Returns the
2600 hold, and fleshes its transit (if any), if found, undef otherwise.
2601 The search is limited to unfulfilled, uncanceled hold where the
2602 request_lib equals the NCIPServer working org. unit.
2606 sub find_hold_via_note {
2611 # Build the search clause up here, because it is a bit complex.
2617 select => { ahr => ['id']},
2619 where => {cancel_time => undef, fulfillment_time => undef,
2620 request_lib => $self->{session}->{work_ou}->id()}
2625 my $note = $U->simplereq(
2627 'open-ils.pcrud.search.ahrn',
2628 $self->{session}->{authtoken},
2630 {flesh => 2, flesh_fields => {ahrn => ['hold'], ahr => ['transit']}}
2632 if (ref($note) eq 'Fieldmapper::action::hold_request_note') {
2633 return $note->hold();
2641 $ils->delete_copy($copy);
2643 Deletes the copy, and if it is owned by our work_ou and not a precat,
2644 we also delete the volume and bib on which the copy depends.
2652 # Shortcut for ownership checks below.
2653 my $ou_id = $self->{session}->{work_ou}->id();
2655 # First, make sure the copy is not already deleted and we own it.
2656 return undef if ($U->is_true($copy->deleted()) || $copy->circ_lib() != $ou_id);
2658 # Indicate we want to delete the copy.
2659 $copy->isdeleted(1);
2660 $copy->deleted('t');
2662 # Delete the copy using a backend call that will delete the copy,
2663 # the call number, and bib when appropriate.
2664 my $result = $U->simplereq(
2666 'open-ils.cat.asset.copy.fleshed.batch.update.override',
2667 $self->{session}->{authtoken},
2671 # We are currently not checking for succes or failure of the
2672 # above. At some point, someone may want to.
2677 =head2 copy_can_circulate
2679 $can_circulate = $ils->copy_can_circulate($copy);
2681 Check if the copy's location and the copy itself allow
2682 circulation. Return true if they do, and false if they do not.
2686 sub copy_can_circulate {
2690 my $location = $copy->location();
2691 unless (ref($location)) {
2692 $location = $self->retrieve_copy_location($location);
2695 return ($U->is_true($copy->circulate()) && $U->is_true($location->circulate()));
2698 =head1 OVERRIDDEN PARENT METHODS
2700 =head2 find_user_barcode
2702 We dangerously override our parent's C<find_user_barcode> to return
2703 either the $barcode or a Problem object. In list context the barcode
2704 or problem will be the first argument and the id field, if any, will
2705 be the second. We also add a second, optional, argument to indicate a
2706 default value for the id field in the event of a failure to find
2707 anything at all. (Perl lets us get away with this.)
2711 sub find_user_barcode {
2713 my $request = shift;
2714 my $default = shift;
2717 my $message = $self->parse_request_type($request);
2718 if ($message eq 'LookupUser') {
2719 $default = 'AuthenticationInputData';
2721 $default = 'UserIdentifierValue';
2725 my ($value, $idfield) = $self->SUPER::find_user_barcode($request);
2728 $idfield = $default unless ($idfield);
2729 $value = NCIP::Problem->new();
2730 $value->ProblemType('Needed Data Missing');
2731 $value->ProblemDetail('Cannot find user barcode in message.');
2732 $value->ProblemElement($idfield);
2733 $value->ProblemValue('NULL');
2736 return (wantarray) ? ($value, $idfield) : $value;
2739 =head2 find_item_barcode
2741 We do pretty much the same thing as with C<find_user_barcode> for
2742 C<find_item_barcode>.
2746 sub find_item_barcode {
2748 my $request = shift;
2749 my $default = shift || 'ItemIdentifierValue';
2751 my ($value, $idfield) = $self->SUPER::find_item_barcode($request);
2754 $idfield = $default unless ($idfield);
2755 $value = NCIP::Problem->new();
2756 $value->ProblemType('Needed Data Missing');
2757 $value->ProblemDetail('Cannot find item barcode in message.');
2758 $value->ProblemElement($idfield);
2759 $value->ProblemValue('NULL');
2762 return (wantarray) ? ($value, $idfield) : $value;
2765 =head2 find_location_failover
2767 $location = $ils->find_location_failover($location, $request, $message);
2769 Attempts to retrieve an org_unit by shortname from the passed in
2770 $location. If that fails, $request and $message are used to lookup
2771 the ToAgencyId/AgencyId field and that is used. Returns an org_unit
2772 as retrieved by retrieve_org_unit_by_shortname if successful and undef
2777 sub find_location_failover {
2778 my ($self, $location, $request, $message) = @_;
2779 if ($request && !$message) {
2780 $message = $self->parse_request_type($request);
2784 # Because Auto-Graphics. (This should be configured somehow.)
2785 $location =~ s/^[^-]+-//;
2786 $org_unit = $self->retrieve_org_unit_by_shortname($location);
2788 if ($request && $message && !$org_unit) {
2789 $location = $request->{$message}->{InitiationHeader}->{ToAgencyId}->{AgencyId};
2791 # Because Auto-Graphics. (This should be configured somehow.)
2792 $location =~ s/^[^-]+-//;
2793 $org_unit = $self->retrieve_org_unit_by_shortname($location);
2800 # private subroutines not meant to be used directly by subclasses.
2801 # Most have to do with setup and/or state checking of implementation
2804 # Find, load, and parse our configuration file:
2808 # Find the configuration file via variables:
2809 my $file = OILS_NCIP_CONFIG_DEFAULT;
2810 $file = $ENV{OILS_NCIP_CONFIG} if ($ENV{OILS_NCIP_CONFIG});
2812 $self->{config} = XMLin($file, NormaliseSpace => 2,
2813 ForceArray => ['block_profile', 'stat_cat_entry']);
2816 # Bootstrap OpenSRF::System and load the IDL.
2820 my $bootstrap_config = $self->{config}->{bootstrap};
2821 OpenSRF::System->bootstrap_client(config_file => $bootstrap_config);
2823 my $idl = OpenSRF::Utils::SettingsClient->new->config_value("IDL");
2824 Fieldmapper->import(IDL => $idl);
2827 # Login and then initialize some object data based on the
2832 # Login to Evergreen.
2835 # Load the barred groups as pgt objects into a blocked_profiles
2837 $self->{blocked_profiles} = [];
2838 if (ref($self->{config}->{patrons}) eq 'HASH') {
2839 foreach (@{$self->{config}->{patrons}->{block_profile}}) {
2842 $pgt = $U->simplereq(
2844 'open-ils.pcrud.retrieve.pgt',
2845 $self->{session}->{authtoken},
2849 $pgt = $U->simplereq(
2851 'open-ils.pcrud.search.pgt',
2852 $self->{session}->{authtoken},
2857 push(@{$self->{blocked_profiles}}, $pgt) if ($pgt);
2861 # Load the bib source if we're not using precats.
2862 unless ($self->{config}->{items}->{use_precats}) {
2863 # Retrieve the default
2864 $self->{bib_source} = $U->simplereq(
2866 'open-ils.pcrud.retrieve.cbs',
2867 $self->{session}->{authtoken},
2868 BIB_SOURCE_DEFAULT);
2869 my $data = $self->{config}->{items}->{bib_source};
2871 $data = $data->[0] if (ref($data) eq 'ARRAY');
2874 $result = $U->simplereq(
2876 'open-ils.pcrud.retrieve.cbs',
2877 $self->{session}->{authtoken},
2881 $result = $U->simplereq(
2883 'open-ils.pcrud.search.cbs',
2884 $self->{session}->{authtoken},
2888 $self->{bib_source} = $result if ($result);
2892 # Load the required asset.stat_cat_entries:
2893 $self->{stat_cat_entries} = [];
2894 # First, make a regex for our ou and ancestors:
2895 my $ancestors = join("|", @{$U->get_org_ancestors($self->{session}->{work_ou}->id())});
2896 my $re = qr/(?:$ancestors)/;
2897 # Get the uniq stat_cat ids from the configuration:
2898 my @cats = uniq map {$_->{stat_cat}} @{$self->{config}->{items}->{stat_cat_entry}};
2899 # Retrieve all of the fleshed stat_cats and entries for the above.
2900 my $stat_cats = $U->simplereq(
2902 'open-ils.circ.stat_cat.asset.retrieve.batch',
2903 $self->{session}->{authtoken},
2906 foreach my $entry (@{$self->{config}->{items}->{stat_cat_entry}}) {
2907 # Must have the stat_cat attr and the name, so we must have a
2909 next unless(ref $entry);
2910 my ($stat) = grep {$_->id() == $entry->{stat_cat}} @$stat_cats;
2911 push(@{$self->{stat_cat_entries}}, grep {$_->owner() =~ $re && $_->value() eq $entry->{content}} @{$stat->entries()});
2914 # Check if we should abort transits on request cancellation. We
2915 # put this in a different variable because someone may not have
2916 # updated their configuration since this feature was added and we
2917 # don't want runtime errors.
2918 $self->{abort_transit_on_hold_cancel} = 0;
2919 if ($self->{config}->{holds} && $self->{config}->{holds}->{abort_transit_on_cancel}) {
2920 $self->{abort_transit_on_hold_cancel} = 1;
2924 # Search for holds using the user, idvalue and selection_ou.
2929 my $selection_ou = shift;
2933 # Retrieve all of the user's active holds, and then search them in Perl.
2934 my $holds_list = $U->simplereq(
2936 'open-ils.circ.holds.retrieve',
2937 $self->{session}->{authtoken},
2942 if ($holds_list && @$holds_list) {
2943 my @holds = grep {$_->target == $target && $_->selection_ou == $selection_ou->id()} @{$holds_list};
2944 # There should only be 1, at this point, if there are any.
2953 # Standalone, "helper" functions. These do not take an object or
2956 # Check if a user is past their expiration date.
2961 # Users might not expire. If so, they have no expire_date.
2962 if ($user->expire_date()) {
2963 my $expires = DateTime::Format::ISO8601->parse_datetime(
2964 cleanse_ISO8601($user->expire_date())
2966 my $now = DateTime->now()->epoch();
2967 $expired = $now > $expires;
2973 # Creates a NCIP Problem from an event. Takes a string for the problem
2974 # type, the event hashref (or a string to use for the detail), and
2975 # optional arguments for the ProblemElement and ProblemValue fields.
2976 sub _problem_from_event {
2977 my ($type, $evt, $element, $value) = @_;
2983 my ($textcode, $desc);
2985 # Get the textcode, if available. Otherwise, use the ilsevent
2986 # "id," if available.
2987 if ($evt->{textcode}) {
2988 $textcode = $evt->{textcode};
2989 } elsif ($evt->{ilsevent}) {
2990 $textcode = $evt->{ilsevent};
2993 # Get the description. We favor translated descriptions over
2994 # the English in ils_events.xml.
2996 $desc = $evt->{desc};
2999 # Check if $type was set. As an "undocumented" feature, you
3000 # can pass undef, and we'll use the textcode from the event.
3007 # Set the detail from some combination of the above.
3010 } elsif ($textcode eq 'PERM_FAILURE') {
3011 if ($evt->{ilsperm}) {
3012 $detail = "Permission denied: " . $evt->{ilsperm};
3013 $detail =~ s/\.override$//;
3015 } elsif ($textcode) {
3016 $detail = "ILS returned $textcode error.";
3018 $detail = 'Detail not available.';
3025 return NCIP::Problem->new(
3027 ProblemType => ($type) ? $type : 'Temporary Processing Failure',
3028 ProblemDetail => ($detail) ? $detail : 'Detail not available.',
3029 ProblemElement => ($element) ? $element : 'NULL',
3030 ProblemValue => ($value) ? $value : 'NULL'
3035 # "Fix" dates for output so they validate against the schema
3038 my $out = DateTime::Format::ISO8601->parse_datetime(cleanse_ISO8601($date));
3039 $out->set_time_zone('UTC');
3040 return $out->iso8601();