3 # A Class for hiding the ILS's concept of the patron from the OpenSIP
7 package OpenILS::SIP::Patron;
13 use Sys::Syslog qw(syslog);
15 use Digest::MD5 qw(md5_hex);
18 use OpenILS::Application::AppUtils;
19 use OpenILS::Application::Actor;
20 use OpenSRF::Utils qw/:datetime/;
21 use DateTime::Format::ISO8601;
22 my $U = 'OpenILS::Application::AppUtils';
24 our (@ISA, @EXPORT_OK);
28 @EXPORT_OK = qw(invalid_patron);
33 # OpenILS::SIP::Patron->new($barcode);
34 # OpenILS::SIP::Patron->new(barcode => $barcode); # same as above
35 # OpenILS::SIP::Patron->new( usr => $id);
39 my $key = (@_ > 1) ? shift : 'barcode'; # if we have multiple args, the first is the key index (default barcode)
40 my $patron_id = shift;
43 if ($key ne 'usr' and $key ne 'barcode') {
44 syslog("LOG_ERROR", "Patron (card) lookup requested by illegeal key '$key'");
48 unless(defined $patron_id) {
49 syslog("LOG_WARNING", "No patron ID provided to ILS::Patron->new");
53 my $type = ref($class) || $class;
54 my $self = bless({}, $type);
56 syslog("LOG_DEBUG", "OILS: new OpenILS Patron(%s => %s): searching...", $key, $patron_id);
58 my $e = OpenILS::SIP->editor();
77 # in some cases, we don't need all of this data. Only fetch the user + barcode
78 $usr_flesh = {flesh => 1, flesh_fields => {au => ['card']}} if $args{slim_user};
81 if($key eq 'barcode') { # retrieve user by barcode
83 $$usr_flesh{flesh} += 1;
84 $$usr_flesh{flesh_fields}{ac} = ['usr'];
86 my $card = $e->search_actor_card([{barcode => $patron_id}, $usr_flesh])->[0];
88 if(!$card or !$U->is_true($card->active)) {
89 syslog("LOG_WARNING", "No such patron barcode: $patron_id");
96 $user = $e->retrieve_actor_user([$patron_id, $usr_flesh]);
99 if(!$user or $U->is_true($user->deleted)) {
100 syslog("LOG_WARNING", "OILS: Unable to find patron %s => %s", $key, $patron_id);
104 if(!$U->is_true($user->active)) {
105 syslog("LOG_WARNING", "OILS: Patron is inactive %s => %s", $key, $patron_id);
109 # now grab the user's penalties
111 $self->flesh_user_penalties($user, $e) unless $args{slim_user};
113 $self->{editor} = $e;
114 $self->{user} = $user;
115 $self->{id} = ($key eq 'barcode') ? $patron_id : $user->card->barcode; # The barcode IS the ID to SIP.
116 # We give back the passed barcode if the key was indeed a barcode, just to be safe. Otherwise pull it from the card.
118 syslog("LOG_DEBUG", "OILS: new OpenILS Patron(%s => %s): found patron : barred=%s, card:active=%s",
119 $key, $patron_id, $user->barred, $user->card->active );
124 # grab patron penalties. Only grab non-archived penalties that are for fines,
125 # excessive overdues, or otherwise block circluation activity
126 sub flesh_user_penalties {
127 my ($self, $user, $e) = @_;
129 $user->standing_penalties(
130 $e->search_actor_user_standing_penalty([
135 # ignore "archived" penalties
136 {stop_date => undef},
137 {stop_date => {'>' => 'now'}}
145 transform => 'actor.org_unit_ancestors',
151 # at this point, there is no concept of "here", so fetch penalties
152 # for the patron's home lib plus ancestors
153 where => {id => $user->home_ou},
158 # in addition to fines and excessive overdue penalties,
159 # we only care about penalties that result in blocks
160 standing_penalty => {
162 select => {csp => ['id']},
166 {id => [1,2]}, # fines / overdues
167 {block_list => {'!=' => undef}}
184 return format_name($self->{user});
189 return OpenILS::SIP::clean_text(
191 ($u->first_given_name || ''),
192 ($u->second_given_name || ''),
193 ($u->family_name || '')));
198 my $lib = OpenILS::SIP::shortname_from_id($self->{user}->home_ou);
199 syslog('LOG_DEBUG', "OILS: Patron->home_library() = $lib");
205 return "" unless $addr;
206 my $return = OpenILS::SIP::clean_text(
207 join( ' ', map {$_ || ''} (
218 $return =~ s/\s+/ /sg; # Compress any run of of whitespace to one space
224 return $self->{user}->id;
229 my $u = $self->{user};
230 my $str = __addr_string($u->billing_address || $u->mailing_address);
231 syslog('LOG_DEBUG', "OILS: Patron address: $str");
237 return OpenILS::SIP::clean_text($self->{user}->email);
242 return $self->{user}->day_phone;
247 my $dob = OpenILS::SIP->format_date($self->{user}->dob);
248 syslog('LOG_DEBUG', "OILS: Patron DOB = $dob");
254 my $expire = OpenILS::SIP->format_date($self->{user}->expire_date);
255 syslog('LOG_DEBUG', "OILS: Patron Expire = $expire");
262 my $use_code = OpenILS::SIP->get_option_value('patron_type_uses_code') || '';
264 # should we use the no_i18n version of patron profile name (as a 'code')?
265 return $self->{editor}->retrieve_permission_grp_tree(
266 [$self->{user}->profile->id, {no_i18n => 1}])->name
267 if $use_code =~ /true/io;
269 return OpenILS::SIP::clean_text($self->{user}->profile->name);
274 return '000'; # Unspecified
277 # How much more detail do we need to check here?
278 # sec: adding logic to return false if user is barred, has a circulation block
282 my $u = $self->{user};
284 # compute expiration date for borrowing privileges
285 my $expire = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($u->expire_date));
287 # determine whether patron should be allowed to circulate materials:
288 # not barred, doesn't owe too much wrt fines/fees, privileges haven't
290 my $circ_is_blocked =
291 (($u->barred eq 't') or
292 ($u->standing_penalties and @{$u->standing_penalties}) or
293 (CORE::time > $expire->epoch));
296 !$circ_is_blocked and
297 $u->active eq 't' and
298 $u->card->active eq 't';
303 # How much more detail do we need to check here?
306 return $self->charge_ok;
316 return $self->charge_ok;
319 # return true if the card provided is marked as lost
322 return $self->{user}->card->active eq 'f';
325 sub recall_overdue { # not implemented
331 my ($self, $pwd) = @_;
332 syslog('LOG_DEBUG', 'OILS: Patron->check_password()');
333 return 0 unless (defined $pwd and $self->{user});
334 return md5_hex($pwd) eq $self->{user}->passwd;
337 sub currency { # not really implemented
339 syslog('LOG_DEBUG', 'OILS: Patron->currency()');
345 syslog('LOG_DEBUG', 'OILS: Patron->fee_amount()');
346 my $user_id = $self->{user}->id;
348 my $e = $self->{editor};
350 my $summary = $e->retrieve_money_open_user_summary($user_id);
351 $e->rollback; # xact_rollback + disconnect
353 my $total = ($summary) ? $summary->balance_owed : 0;
354 syslog('LOG_INFO', "User ".$self->{id} .":$user_id has a fee amount of \$$total");
360 my $u = $self->{user};
362 return 'barred' if $u->barred eq 't';
366 return $b if $u->active eq 'f';
367 return $b if $u->card->active eq 'f';
369 # if we have any penalties at this point, they are blocking penalties
370 return $b if $u->standing_penalties and @{$u->standing_penalties};
372 # has the patron account expired?
373 my $expire = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($u->expire_date));
374 return $b if CORE::time > $expire->epoch;
379 sub print_line { # not implemented
384 sub too_many_charged { # not implemented
389 sub too_many_overdue {
391 return scalar( # PATRON_EXCEEDS_OVERDUE_COUNT
392 grep { $_->standing_penalty == 2 } @{$self->{user}->standing_penalties}
396 # not completely sure what this means
397 sub too_many_renewal {
402 # not relevant, handled by fines/fees
403 sub too_many_claim_return {
408 # not relevant, handled by fines/fees
414 sub excessive_fines {
416 return scalar( # PATRON_EXCEEDS_FINES
417 grep { $_->standing_penalty == 1 } @{$self->{user}->standing_penalties}
421 # Until someone suggests otherwise, fees and fines are the same
425 return $self->excessive_fines;
428 # not relevant, handled by fines/fees
429 sub too_many_billed {
437 # List of outstanding holds placed
440 my ($self, $start, $end) = @_;
441 syslog('LOG_DEBUG', 'OILS: Patron->hold_items()');
443 my $holds = $self->{editor}->search_action_hold_request(
444 { usr => $self->{user}->id, fulfillment_time => undef, cancel_time => undef }
448 push( @holds, OpenILS::SIP::clean_text($self->__hold_to_title($_)) ) for @$holds;
450 return (defined $start and defined $end) ?
451 [ $holds[($start-1)..($end-1)] ] :
455 sub __hold_to_title {
458 my $e = $self->{editor};
460 my( $id, $mods, $title, $volume, $copy );
462 return __copy_to_title($e,
463 $e->retrieve_asset_copy($hold->target))
464 if $hold->hold_type eq 'C';
466 return __volume_to_title($e,
467 $e->retrieve_asset_call_number($hold->target))
468 if $hold->hold_type eq 'V';
470 return __record_to_title(
471 $e, $hold->target) if $hold->hold_type eq 'T';
473 return __metarecord_to_title(
474 $e, $hold->target) if $hold->hold_type eq 'M';
477 sub __copy_to_title {
478 my( $e, $copy ) = @_;
479 #syslog('LOG_DEBUG', "OILS: copy_to_title(%s)", $copy->id);
480 return $copy->dummy_title if $copy->call_number == -1;
482 my $vol = (ref $copy->call_number) ?
484 $e->retrieve_asset_call_number($copy->call_number);
486 return __volume_to_title($e, $vol);
490 sub __volume_to_title {
491 my( $e, $volume ) = @_;
492 #syslog('LOG_DEBUG', "OILS: volume_to_title(%s)", $volume->id);
493 return __record_to_title($e, $volume->record);
497 sub __record_to_title {
498 my( $e, $title_id ) = @_;
499 #syslog('LOG_DEBUG', "OILS: record_to_title($title_id)");
500 my $mods = $U->simplereq(
502 'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
503 return ($mods) ? $mods->title : "";
506 sub __metarecord_to_title {
507 my( $e, $m_id ) = @_;
508 #syslog('LOG_DEBUG', "OILS: metarecord_to_title($m_id)");
509 my $mods = $U->simplereq(
511 'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
512 return ($U->event_code($mods)) ? "<unknown>" : $mods->title;
517 # remove the hold on item item_id from my hold queue.
518 # return true if I was holding the item, false otherwise.
521 my ($self, $item_id) = @_;
525 sub __patron_items_info {
527 return if $self->{item_info};
529 OpenILS::Application::Actor::_checked_out(
530 0, $self->{editor}, $self->{user}->id);;
536 my ($self, $start, $end) = @_;
538 $self->__patron_items_info();
539 my @overdues = @{$self->{item_info}->{overdue}};
540 #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
543 syslog('LOG_DEBUG', "OILS: overdue_items() fleshing circs @overdues");
545 my $return_datatype = OpenILS::SIP->get_option_value('msg64_summary_datatype') || '';
547 for my $circid (@overdues) {
549 if($return_datatype eq 'barcode') {
550 push( @o, __circ_to_barcode($self->{editor}, $circid));
552 push( @o, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
557 return (defined $start and defined $end) ?
558 [ $overdues[($start-1)..($end-1)] ] : \@overdues;
561 sub __circ_to_barcode {
564 $circ = $e->retrieve_action_circulation($circ);
565 my $copy = $e->retrieve_asset_copy($circ->target_copy);
566 return $copy->barcode;
569 sub __circ_to_title {
570 my( $e, $circ ) = @_;
572 $circ = $e->retrieve_action_circulation($circ);
573 return __copy_to_title( $e,
574 $e->retrieve_asset_copy($circ->target_copy) );
578 my ($self, $start, $end) = shift;
580 $self->__patron_items_info();
583 @{$self->{item_info}->{out}},
584 @{$self->{item_info}->{overdue}}
587 #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
590 syslog('LOG_DEBUG', "OILS: charged_items() fleshing circs @charges");
592 my $return_datatype = OpenILS::SIP->get_option_value('msg64_summary_datatype') || '';
594 for my $circid (@charges) {
596 if($return_datatype eq 'barcode') {
597 push( @c, __circ_to_barcode($self->{editor}, $circid));
599 push( @c, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
605 return (defined $start and defined $end) ?
606 [ $charges[($start-1)..($end-1)] ] :
611 my ($self, $start, $end) = @_;
613 syslog('LOG_DEBUG', 'OILS: Patron->fine_items()');
614 return (defined $start and defined $end) ?
615 [ $fines[($start-1)..($end-1)] ] : \@fines;
618 # not currently supported
620 my ($self, $start, $end) = @_;
625 my ($self, $start, $end) = @_;
626 syslog('LOG_DEBUG', 'OILS: Patron->unavail_holds()');
628 my $ids = $self->{editor}->json_query({
629 select => {ahr => ['id']},
632 usr => $self->{user}->id,
633 fulfillment_time => undef,
634 cancel_time => undef,
636 {current_shelf_lib => undef},
637 {current_shelf_lib => {'!=' => {'+ahr' => 'pickup_lib'}}}
642 my @holds_sip_output = map {
643 OpenILS::SIP::clean_text($self->__hold_to_title($_))
645 $self->{editor}->search_action_hold_request(
646 {id => [map {$_->{id}} @$ids]}
650 return (defined $start and defined $end) ?
651 [ @holds_sip_output[($start-1)..($end-1)] ] :
656 my ($self, $card_retained, $blocked_card_msg) = @_;
657 $blocked_card_msg ||= '';
659 my $e = $self->{editor};
660 my $u = $self->{user};
662 syslog('LOG_INFO', "OILS: Blocking user %s", $u->card->barcode );
664 return $self if $u->card->active eq 'f';
666 $e->xact_begin; # connect and start a new transaction
668 $u->card->active('f');
669 if( ! $e->update_actor_card($u->card) ) {
670 syslog('LOG_ERR', "OILS: Block card update failed: %s", $e->event->{textcode});
671 $e->rollback; # rollback + disconnect
675 # retrieve the un-fleshed user object for update
676 $u = $e->retrieve_actor_user($u->id);
677 my $note = OpenILS::SIP::clean_text($u->alert_message) || "";
678 $note = "<sip> CARD BLOCKED BY SELF-CHECK MACHINE. $blocked_card_msg</sip>\n$note"; # XXX Config option
679 $note =~ s/\s*$//; # kill trailng whitespace
680 $u->alert_message($note);
682 if( ! $e->update_actor_user($u) ) {
683 syslog('LOG_ERR', "OILS: Block: patron alert update failed: %s", $e->event->{textcode});
684 $e->rollback; # rollback + disconnect
689 $self->{user}->alert_message( $note );
691 $e->commit; # commits and disconnects
695 # Testing purposes only
697 my ($self, $card_retained) = @_;
698 $self->{screen_msg} = "All privileges restored.";
700 # Un-mark card as inactive, grep out the patron alert
701 my $e = $self->{editor};
702 my $u = $self->{user};
704 syslog('LOG_INFO', "OILS: Unblocking user %s", $u->card->barcode );
706 return $self if $u->card->active eq 't';
708 $e->xact_begin; # connect and start a new transaction
710 $u->card->active('t');
711 if( ! $e->update_actor_card($u->card) ) {
712 syslog('LOG_ERR', "OILS: Unblock card update failed: %s", $e->event->{textcode});
713 $e->rollback; # rollback + disconnect
717 # retrieve the un-fleshed user object for update
718 $u = $e->retrieve_actor_user($u->id);
719 my $note = OpenILS::SIP::clean_text($u->alert_message) || "";
720 $note =~ s#<sip>.*</sip>##;
721 $note =~ s/^\s*//; # kill leading whitespace
722 $note =~ s/\s*$//; # kill trailng whitespace
723 $u->alert_message($note);
725 if( ! $e->update_actor_user($u) ) {
726 syslog('LOG_ERR', "OILS: Unblock: patron alert update failed: %s", $e->event->{textcode});
727 $e->rollback; # rollback + disconnect
732 $self->{user}->alert_message( $note );
734 $e->commit; # commits and disconnects
743 return "Please contact library staff";
747 return "Please contact library staff";
750 sub inet_privileges {
752 my $e = OpenILS::SIP->editor();
753 $INET_PRIVS = $e->retrieve_all_config_net_access_level() unless $INET_PRIVS;
754 my ($level) = grep { $_->id eq $self->{user}->net_access_level } @$INET_PRIVS;
755 my $name = OpenILS::SIP::clean_text($level->name);
756 syslog('LOG_DEBUG', "OILS: Patron inet_privs = $name");
762 my $extra_fields = {};
763 my $u = $self->{user};
764 foreach my $stat_cat_entry (@{$u->stat_cat_entries}) {
765 my $stat_cat = $stat_cat_entry->stat_cat;
766 next unless ($stat_cat->sip_field);
767 my $value = $stat_cat_entry->stat_cat_entry;
768 if(defined $stat_cat->sip_format && length($stat_cat->sip_format) > 0) { # Has a format string?
769 if($stat_cat->sip_format =~ /^\|(.*)\|$/) { # Regex match?
770 if($value =~ /($1)/) { # If we have a match
771 if(defined $2) { # Check to see if they embedded a capture group
772 $value = $2; # If so, use it
774 else { # No embedded capture group?
775 $value = $1; # Use our outer one
779 $value = ''; # Empty string. Will be checked for below.
782 else { # Not a regex match - Try sprintf match (looking for a %s, if any)
783 $value = sprintf($stat_cat->sip_format, $value);
786 next unless length($value) > 0; # No value = no export
787 $value =~ s/\|//g; # Remove all lingering pipe chars for sane output purposes
788 $extra_fields->{ $stat_cat->sip_field } = [] unless (defined $extra_fields->{$stat_cat->sip_field});
789 push(@{$extra_fields->{ $stat_cat->sip_field}}, $value);
791 return $extra_fields;