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 );
121 $U->log_user_activity($user->id, $self->get_act_who, 'verify');
128 my $config = OpenILS::SIP->config();
129 my $login = OpenILS::SIP->login_account();
131 my $act_who = $config->{implementation_config}->{default_activity_who};
132 my $force_who = $config->{implementation_config}->{force_activity_who};
134 # 1. future: test sip extension for caller-provided ewho and !$force_who
136 # 2. See if the login is tagged with an ewho
137 return $login->{activity_who} if $login->{activity_who};
139 # 3. if all else fails, see if there is an institution-wide ewho
140 return $config->{activity_who} if $config->{activity_who};
145 # grab patron penalties. Only grab non-archived penalties that are for fines,
146 # excessive overdues, or otherwise block circluation activity
147 sub flesh_user_penalties {
148 my ($self, $user, $e) = @_;
150 $user->standing_penalties(
151 $e->search_actor_user_standing_penalty([
156 # ignore "archived" penalties
157 {stop_date => undef},
158 {stop_date => {'>' => 'now'}}
166 transform => 'actor.org_unit_ancestors',
172 # at this point, there is no concept of "here", so fetch penalties
173 # for the patron's home lib plus ancestors
174 where => {id => $user->home_ou},
179 # in addition to fines and excessive overdue penalties,
180 # we only care about penalties that result in blocks
181 standing_penalty => {
183 select => {csp => ['id']},
187 {id => [1,2]}, # fines / overdues
188 {block_list => {'!=' => undef}}
205 return format_name($self->{user});
210 return OpenILS::SIP::clean_text(
212 ($u->first_given_name || ''),
213 ($u->second_given_name || ''),
214 ($u->family_name || '')));
219 my $lib = OpenILS::SIP::shortname_from_id($self->{user}->home_ou);
220 syslog('LOG_DEBUG', "OILS: Patron->home_library() = $lib");
226 return "" unless $addr;
227 my $return = OpenILS::SIP::clean_text(
228 join( ' ', map {$_ || ''} (
239 $return =~ s/\s+/ /sg; # Compress any run of of whitespace to one space
245 return $self->{user}->id;
250 my $u = $self->{user};
251 my $str = __addr_string($u->billing_address || $u->mailing_address);
252 syslog('LOG_DEBUG', "OILS: Patron address: $str");
258 return OpenILS::SIP::clean_text($self->{user}->email);
263 return $self->{user}->day_phone;
268 my $dob = OpenILS::SIP->format_date($self->{user}->dob);
269 syslog('LOG_DEBUG', "OILS: Patron DOB = $dob");
275 my $expire = OpenILS::SIP->format_date($self->{user}->expire_date);
276 syslog('LOG_DEBUG', "OILS: Patron Expire = $expire");
283 my $use_code = OpenILS::SIP->get_option_value('patron_type_uses_code') || '';
285 # should we use the no_i18n version of patron profile name (as a 'code')?
286 return $self->{editor}->retrieve_permission_grp_tree(
287 [$self->{user}->profile->id, {no_i18n => 1}])->name
288 if $use_code =~ /true/io;
290 return OpenILS::SIP::clean_text($self->{user}->profile->name);
295 return '000'; # Unspecified
298 # How much more detail do we need to check here?
299 # sec: adding logic to return false if user is barred, has a circulation block
303 my $u = $self->{user};
305 # compute expiration date for borrowing privileges
306 my $expire = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($u->expire_date));
308 # determine whether patron should be allowed to circulate materials:
309 # not barred, doesn't owe too much wrt fines/fees, privileges haven't
311 my $circ_is_blocked =
312 (($u->barred eq 't') or
313 ($u->standing_penalties and @{$u->standing_penalties}) or
314 (CORE::time > $expire->epoch));
317 !$circ_is_blocked and
318 $u->active eq 't' and
319 $u->card->active eq 't';
324 # How much more detail do we need to check here?
327 return $self->charge_ok;
337 return $self->charge_ok;
340 # return true if the card provided is marked as lost
343 return $self->{user}->card->active eq 'f';
346 sub recall_overdue { # not implemented
352 my ($self, $pwd) = @_;
353 syslog('LOG_DEBUG', 'OILS: Patron->check_password()');
354 return 0 unless (defined $pwd and $self->{user});
355 return md5_hex($pwd) eq $self->{user}->passwd;
358 sub currency { # not really implemented
360 syslog('LOG_DEBUG', 'OILS: Patron->currency()');
366 syslog('LOG_DEBUG', 'OILS: Patron->fee_amount()');
367 my $user_id = $self->{user}->id;
369 my $e = $self->{editor};
371 my $summary = $e->retrieve_money_open_user_summary($user_id);
372 $e->rollback; # xact_rollback + disconnect
374 my $total = ($summary) ? $summary->balance_owed : 0;
375 syslog('LOG_INFO', "User ".$self->{id} .":$user_id has a fee amount of \$$total");
381 my $u = $self->{user};
383 return 'barred' if $u->barred eq 't';
387 return $b if $u->active eq 'f';
388 return $b if $u->card->active eq 'f';
390 # if we have any penalties at this point, they are blocking penalties
391 return $b if $u->standing_penalties and @{$u->standing_penalties};
393 # has the patron account expired?
394 my $expire = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($u->expire_date));
395 return $b if CORE::time > $expire->epoch;
400 sub print_line { # not implemented
405 sub too_many_charged { # not implemented
410 sub too_many_overdue {
412 return scalar( # PATRON_EXCEEDS_OVERDUE_COUNT
413 grep { $_->standing_penalty == 2 } @{$self->{user}->standing_penalties}
417 # not completely sure what this means
418 sub too_many_renewal {
423 # not relevant, handled by fines/fees
424 sub too_many_claim_return {
429 # not relevant, handled by fines/fees
435 sub excessive_fines {
437 return scalar( # PATRON_EXCEEDS_FINES
438 grep { $_->standing_penalty == 1 } @{$self->{user}->standing_penalties}
442 # Until someone suggests otherwise, fees and fines are the same
446 return $self->excessive_fines;
449 # not relevant, handled by fines/fees
450 sub too_many_billed {
458 # List of outstanding holds placed
461 my ($self, $start, $end) = @_;
462 syslog('LOG_DEBUG', 'OILS: Patron->hold_items()');
464 my $holds = $self->{editor}->search_action_hold_request(
465 { usr => $self->{user}->id, fulfillment_time => undef, cancel_time => undef }
469 push( @holds, OpenILS::SIP::clean_text($self->__hold_to_title($_)) ) for @$holds;
471 return (defined $start and defined $end) ?
472 [ $holds[($start-1)..($end-1)] ] :
476 sub __hold_to_title {
479 my $e = $self->{editor};
481 my( $id, $mods, $title, $volume, $copy );
483 return __copy_to_title($e,
484 $e->retrieve_asset_copy($hold->target))
485 if $hold->hold_type eq 'C' or $hold->hold_type eq 'F' or $hold->hold_type eq 'R';
487 return __volume_to_title($e,
488 $e->retrieve_asset_call_number($hold->target))
489 if $hold->hold_type eq 'V';
491 return __record_to_title(
492 $e, $hold->target) if $hold->hold_type eq 'T';
494 return __metarecord_to_title(
495 $e, $hold->target) if $hold->hold_type eq 'M';
498 sub __copy_to_title {
499 my( $e, $copy ) = @_;
500 #syslog('LOG_DEBUG', "OILS: copy_to_title(%s)", $copy->id);
501 return $copy->dummy_title if $copy->call_number == -1;
503 my $vol = (ref $copy->call_number) ?
505 $e->retrieve_asset_call_number($copy->call_number);
507 return __volume_to_title($e, $vol);
511 sub __volume_to_title {
512 my( $e, $volume ) = @_;
513 #syslog('LOG_DEBUG', "OILS: volume_to_title(%s)", $volume->id);
514 return __record_to_title($e, $volume->record);
518 sub __record_to_title {
519 my( $e, $title_id ) = @_;
520 #syslog('LOG_DEBUG', "OILS: record_to_title($title_id)");
521 my $mods = $U->simplereq(
523 'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
524 return ($mods) ? $mods->title : "";
527 sub __metarecord_to_title {
528 my( $e, $m_id ) = @_;
529 #syslog('LOG_DEBUG', "OILS: metarecord_to_title($m_id)");
530 my $mods = $U->simplereq(
532 'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
533 return ($U->event_code($mods)) ? "<unknown>" : $mods->title;
538 # remove the hold on item item_id from my hold queue.
539 # return true if I was holding the item, false otherwise.
542 my ($self, $item_id) = @_;
546 sub __patron_items_info {
548 return if $self->{item_info};
550 OpenILS::Application::Actor::_checked_out(
551 0, $self->{editor}, $self->{user}->id);;
557 my ($self, $start, $end) = @_;
559 $self->__patron_items_info();
560 my @overdues = @{$self->{item_info}->{overdue}};
561 #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
564 syslog('LOG_DEBUG', "OILS: overdue_items() fleshing circs @overdues");
566 my $return_datatype = OpenILS::SIP->get_option_value('msg64_summary_datatype') || '';
568 for my $circid (@overdues) {
570 if($return_datatype eq 'barcode') {
571 push( @o, __circ_to_barcode($self->{editor}, $circid));
573 push( @o, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
578 return (defined $start and defined $end) ?
579 [ $overdues[($start-1)..($end-1)] ] : \@overdues;
582 sub __circ_to_barcode {
585 $circ = $e->retrieve_action_circulation($circ);
586 my $copy = $e->retrieve_asset_copy($circ->target_copy);
587 return $copy->barcode;
590 sub __circ_to_title {
591 my( $e, $circ ) = @_;
593 $circ = $e->retrieve_action_circulation($circ);
594 return __copy_to_title( $e,
595 $e->retrieve_asset_copy($circ->target_copy) );
599 my ($self, $start, $end) = shift;
601 $self->__patron_items_info();
604 @{$self->{item_info}->{out}},
605 @{$self->{item_info}->{overdue}}
608 #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
611 syslog('LOG_DEBUG', "OILS: charged_items() fleshing circs @charges");
613 my $return_datatype = OpenILS::SIP->get_option_value('msg64_summary_datatype') || '';
615 for my $circid (@charges) {
617 if($return_datatype eq 'barcode') {
618 push( @c, __circ_to_barcode($self->{editor}, $circid));
620 push( @c, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
626 return (defined $start and defined $end) ?
627 [ $charges[($start-1)..($end-1)] ] :
632 my ($self, $start, $end) = @_;
634 syslog('LOG_DEBUG', 'OILS: Patron->fine_items()');
635 return (defined $start and defined $end) ?
636 [ $fines[($start-1)..($end-1)] ] : \@fines;
639 # not currently supported
641 my ($self, $start, $end) = @_;
646 my ($self, $start, $end) = @_;
647 syslog('LOG_DEBUG', 'OILS: Patron->unavail_holds()');
649 my $ids = $self->{editor}->json_query({
650 select => {ahr => ['id']},
653 usr => $self->{user}->id,
654 fulfillment_time => undef,
655 cancel_time => undef,
657 {current_shelf_lib => undef},
658 {current_shelf_lib => {'!=' => {'+ahr' => 'pickup_lib'}}}
663 my @holds_sip_output;
664 @holds_sip_output = map {
665 OpenILS::SIP::clean_text($self->__hold_to_title($_))
667 $self->{editor}->search_action_hold_request(
668 {id => [map {$_->{id}} @$ids]}
672 return (defined $start and defined $end) ?
673 [ @holds_sip_output[($start-1)..($end-1)] ] :
678 my ($self, $card_retained, $blocked_card_msg) = @_;
679 $blocked_card_msg ||= '';
681 my $e = $self->{editor};
682 my $u = $self->{user};
684 syslog('LOG_INFO', "OILS: Blocking user %s", $u->card->barcode );
686 return $self if $u->card->active eq 'f';
688 $e->xact_begin; # connect and start a new transaction
690 $u->card->active('f');
691 if( ! $e->update_actor_card($u->card) ) {
692 syslog('LOG_ERR', "OILS: Block card update failed: %s", $e->event->{textcode});
693 $e->rollback; # rollback + disconnect
697 # retrieve the un-fleshed user object for update
698 $u = $e->retrieve_actor_user($u->id);
699 my $note = OpenILS::SIP::clean_text($u->alert_message) || "";
700 $note = "<sip> CARD BLOCKED BY SELF-CHECK MACHINE. $blocked_card_msg</sip>\n$note"; # XXX Config option
701 $note =~ s/\s*$//; # kill trailng whitespace
702 $u->alert_message($note);
704 if( ! $e->update_actor_user($u) ) {
705 syslog('LOG_ERR', "OILS: Block: patron alert update failed: %s", $e->event->{textcode});
706 $e->rollback; # rollback + disconnect
711 $self->{user}->alert_message( $note );
713 $e->commit; # commits and disconnects
717 # Testing purposes only
719 my ($self, $card_retained) = @_;
720 $self->{screen_msg} = "All privileges restored.";
722 # Un-mark card as inactive, grep out the patron alert
723 my $e = $self->{editor};
724 my $u = $self->{user};
726 syslog('LOG_INFO', "OILS: Unblocking user %s", $u->card->barcode );
728 return $self if $u->card->active eq 't';
730 $e->xact_begin; # connect and start a new transaction
732 $u->card->active('t');
733 if( ! $e->update_actor_card($u->card) ) {
734 syslog('LOG_ERR', "OILS: Unblock card update failed: %s", $e->event->{textcode});
735 $e->rollback; # rollback + disconnect
739 # retrieve the un-fleshed user object for update
740 $u = $e->retrieve_actor_user($u->id);
741 my $note = OpenILS::SIP::clean_text($u->alert_message) || "";
742 $note =~ s#<sip>.*</sip>##;
743 $note =~ s/^\s*//; # kill leading whitespace
744 $note =~ s/\s*$//; # kill trailng whitespace
745 $u->alert_message($note);
747 if( ! $e->update_actor_user($u) ) {
748 syslog('LOG_ERR', "OILS: Unblock: patron alert update failed: %s", $e->event->{textcode});
749 $e->rollback; # rollback + disconnect
754 $self->{user}->alert_message( $note );
756 $e->commit; # commits and disconnects
765 return "Please contact library staff";
769 return "Please contact library staff";
772 sub inet_privileges {
774 my $e = OpenILS::SIP->editor();
775 $INET_PRIVS = $e->retrieve_all_config_net_access_level() unless $INET_PRIVS;
776 my ($level) = grep { $_->id eq $self->{user}->net_access_level } @$INET_PRIVS;
777 my $name = OpenILS::SIP::clean_text($level->name);
778 syslog('LOG_DEBUG', "OILS: Patron inet_privs = $name");
784 my $extra_fields = {};
785 my $u = $self->{user};
786 foreach my $stat_cat_entry (@{$u->stat_cat_entries}) {
787 my $stat_cat = $stat_cat_entry->stat_cat;
788 next unless ($stat_cat->sip_field);
789 my $value = $stat_cat_entry->stat_cat_entry;
790 if(defined $stat_cat->sip_format && length($stat_cat->sip_format) > 0) { # Has a format string?
791 if($stat_cat->sip_format =~ /^\|(.*)\|$/) { # Regex match?
792 if($value =~ /($1)/) { # If we have a match
793 if(defined $2) { # Check to see if they embedded a capture group
794 $value = $2; # If so, use it
796 else { # No embedded capture group?
797 $value = $1; # Use our outer one
801 $value = ''; # Empty string. Will be checked for below.
804 else { # Not a regex match - Try sprintf match (looking for a %s, if any)
805 $value = sprintf($stat_cat->sip_format, $value);
808 next unless length($value) > 0; # No value = no export
809 $value =~ s/\|//g; # Remove all lingering pipe chars for sane output purposes
810 $extra_fields->{ $stat_cat->sip_field } = [] unless (defined $extra_fields->{$stat_cat->sip_field});
811 push(@{$extra_fields->{ $stat_cat->sip_field}}, $value);
813 return $extra_fields;