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];
89 syslog("LOG_WARNING", "No such patron barcode: $patron_id");
96 $user = $e->retrieve_actor_user([$patron_id, $usr_flesh]);
100 syslog("LOG_WARNING", "OILS: Unable to find patron %s => %s", $key, $patron_id);
104 # now grab the user's penalties
106 $self->flesh_user_penalties($user, $e) unless $args{slim_user};
108 $self->{editor} = $e;
109 $self->{user} = $user;
110 $self->{id} = ($key eq 'barcode') ? $patron_id : $user->card->barcode; # The barcode IS the ID to SIP.
111 # We give back the passed barcode if the key was indeed a barcode, just to be safe. Otherwise pull it from the card.
113 syslog("LOG_DEBUG", "OILS: new OpenILS Patron(%s => %s): found patron : barred=%s, card:active=%s",
114 $key, $patron_id, $user->barred, $user->card->active );
119 # grab patron penalties. Only grab non-archived penalties that are for fines,
120 # excessive overdues, or otherwise block circluation activity
121 sub flesh_user_penalties {
122 my ($self, $user, $e) = @_;
124 $user->standing_penalties(
125 $e->search_actor_user_standing_penalty([
130 # ignore "archived" penalties
131 {stop_date => undef},
132 {stop_date => {'>' => 'now'}}
140 transform => 'actor.org_unit_ancestors',
146 # at this point, there is no concept of "here", so fetch penalties
147 # for the patron's home lib plus ancestors
148 where => {id => $user->home_ou},
153 # in addition to fines and excessive overdue penalties,
154 # we only care about penalties that result in blocks
155 standing_penalty => {
157 select => {csp => ['id']},
161 {id => [1,2]}, # fines / overdues
162 {block_list => {'!=' => undef}}
179 return format_name($self->{user});
184 return OpenILS::SIP::clean_text(
186 ($u->first_given_name || ''),
187 ($u->second_given_name || ''),
188 ($u->family_name || '')));
193 my $lib = OpenILS::SIP::shortname_from_id($self->{user}->home_ou);
194 syslog('LOG_DEBUG', "OILS: Patron->home_library() = $lib");
200 return "" unless $addr;
201 my $return = OpenILS::SIP::clean_text(
202 join( ' ', map {$_ || ''} (
213 $return =~ s/\s+/ /sg; # Compress any run of of whitespace to one space
219 return $self->{user}->id;
224 my $u = $self->{user};
225 my $str = __addr_string($u->billing_address || $u->mailing_address);
226 syslog('LOG_DEBUG', "OILS: Patron address: $str");
232 return OpenILS::SIP::clean_text($self->{user}->email);
237 return $self->{user}->day_phone;
242 my $dob = OpenILS::SIP->format_date($self->{user}->dob);
243 syslog('LOG_DEBUG', "OILS: Patron DOB = $dob");
249 my $expire = OpenILS::SIP->format_date($self->{user}->expire_date);
250 syslog('LOG_DEBUG', "OILS: Patron Expire = $expire");
257 my $use_code = OpenILS::SIP->get_option_value('patron_type_uses_code') || '';
259 # should we use the no_i18n version of patron profile name (as a 'code')?
260 return $self->{editor}->retrieve_permission_grp_tree(
261 [$self->{user}->profile->id, {no_i18n => 1}])->name
262 if $use_code =~ /true/io;
264 return OpenILS::SIP::clean_text($self->{user}->profile->name);
269 return '000'; # Unspecified
272 # How much more detail do we need to check here?
275 my $u = $self->{user};
277 $u->barred eq 'f' and
278 $u->active eq 't' and
279 $u->card->active eq 't';
282 # How much more detail do we need to check here?
285 return $self->charge_ok;
295 return $self->charge_ok;
298 # return true if the card provided is marked as lost
301 return $self->{user}->card->active eq 'f';
304 sub recall_overdue { # not implemented
310 my ($self, $pwd) = @_;
311 syslog('LOG_DEBUG', 'OILS: Patron->check_password()');
312 return 0 unless (defined $pwd and $self->{user});
313 return md5_hex($pwd) eq $self->{user}->passwd;
316 sub currency { # not really implemented
318 syslog('LOG_DEBUG', 'OILS: Patron->currency()');
324 syslog('LOG_DEBUG', 'OILS: Patron->fee_amount()');
325 my $user_id = $self->{user}->id;
327 my $e = $self->{editor};
329 my $summary = $e->retrieve_money_open_user_summary($user_id);
330 $e->rollback; # xact_rollback + disconnect
332 my $total = ($summary) ? $summary->balance_owed : 0;
333 syslog('LOG_INFO', "User ".$self->{id} .":$user_id has a fee amount of \$$total");
339 my $u = $self->{user};
341 return 'barred' if $u->barred eq 't';
345 return $b if $u->active eq 'f';
346 return $b if $u->card->active eq 'f';
348 # if we have any penalties at this point, they are blocking penalties
349 return $b if $u->standing_penalties and @{$u->standing_penalties};
351 # has the patron account expired?
352 my $expire = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($u->expire_date));
353 return $b if CORE::time > $expire->epoch;
358 sub print_line { # not implemented
363 sub too_many_charged { # not implemented
368 sub too_many_overdue {
370 return scalar( # PATRON_EXCEEDS_OVERDUE_COUNT
371 grep { $_->standing_penalty == 2 } @{$self->{user}->standing_penalties}
375 # not completely sure what this means
376 sub too_many_renewal {
381 # not relevant, handled by fines/fees
382 sub too_many_claim_return {
387 # not relevant, handled by fines/fees
393 sub excessive_fines {
395 return scalar( # PATRON_EXCEEDS_FINES
396 grep { $_->standing_penalty == 1 } @{$self->{user}->standing_penalties}
400 # Until someone suggests otherwise, fees and fines are the same
404 return $self->excessive_fines;
407 # not relevant, handled by fines/fees
408 sub too_many_billed {
416 # List of outstanding holds placed
419 my ($self, $start, $end) = @_;
420 syslog('LOG_DEBUG', 'OILS: Patron->hold_items()');
422 my $holds = $self->{editor}->search_action_hold_request(
423 { usr => $self->{user}->id, fulfillment_time => undef, cancel_time => undef }
427 push( @holds, OpenILS::SIP::clean_text($self->__hold_to_title($_)) ) for @$holds;
429 return (defined $start and defined $end) ?
430 [ $holds[($start-1)..($end-1)] ] :
434 sub __hold_to_title {
437 my $e = $self->{editor};
439 my( $id, $mods, $title, $volume, $copy );
441 return __copy_to_title($e,
442 $e->retrieve_asset_copy($hold->target))
443 if $hold->hold_type eq 'C';
445 return __volume_to_title($e,
446 $e->retrieve_asset_call_number($hold->target))
447 if $hold->hold_type eq 'V';
449 return __record_to_title(
450 $e, $hold->target) if $hold->hold_type eq 'T';
452 return __metarecord_to_title(
453 $e, $hold->target) if $hold->hold_type eq 'M';
456 sub __copy_to_title {
457 my( $e, $copy ) = @_;
458 #syslog('LOG_DEBUG', "OILS: copy_to_title(%s)", $copy->id);
459 return $copy->dummy_title if $copy->call_number == -1;
461 my $vol = (ref $copy->call_number) ?
463 $e->retrieve_asset_call_number($copy->call_number);
465 return __volume_to_title($e, $vol);
469 sub __volume_to_title {
470 my( $e, $volume ) = @_;
471 #syslog('LOG_DEBUG', "OILS: volume_to_title(%s)", $volume->id);
472 return __record_to_title($e, $volume->record);
476 sub __record_to_title {
477 my( $e, $title_id ) = @_;
478 #syslog('LOG_DEBUG', "OILS: record_to_title($title_id)");
479 my $mods = $U->simplereq(
481 'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
482 return ($mods) ? $mods->title : "";
485 sub __metarecord_to_title {
486 my( $e, $m_id ) = @_;
487 #syslog('LOG_DEBUG', "OILS: metarecord_to_title($m_id)");
488 my $mods = $U->simplereq(
490 'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
491 return ($U->event_code($mods)) ? "<unknown>" : $mods->title;
496 # remove the hold on item item_id from my hold queue.
497 # return true if I was holding the item, false otherwise.
500 my ($self, $item_id) = @_;
504 sub __patron_items_info {
506 return if $self->{item_info};
508 OpenILS::Application::Actor::_checked_out(
509 0, $self->{editor}, $self->{user}->id);;
515 my ($self, $start, $end) = @_;
517 $self->__patron_items_info();
518 my @overdues = @{$self->{item_info}->{overdue}};
519 #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
522 syslog('LOG_DEBUG', "OILS: overdue_items() fleshing circs @overdues");
524 my $return_datatype = OpenILS::SIP->get_option_value('msg64_summary_datatype') || '';
526 for my $circid (@overdues) {
528 if($return_datatype eq 'barcode') {
529 push( @o, __circ_to_barcode($self->{editor}, $circid));
531 push( @o, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
536 return (defined $start and defined $end) ?
537 [ $overdues[($start-1)..($end-1)] ] : \@overdues;
540 sub __circ_to_barcode {
543 $circ = $e->retrieve_action_circulation($circ);
544 my $copy = $e->retrieve_asset_copy($circ->target_copy);
545 return $copy->barcode;
548 sub __circ_to_title {
549 my( $e, $circ ) = @_;
551 $circ = $e->retrieve_action_circulation($circ);
552 return __copy_to_title( $e,
553 $e->retrieve_asset_copy($circ->target_copy) );
557 my ($self, $start, $end) = shift;
559 $self->__patron_items_info();
562 @{$self->{item_info}->{out}},
563 @{$self->{item_info}->{overdue}}
566 #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
569 syslog('LOG_DEBUG', "OILS: charged_items() fleshing circs @charges");
571 my $return_datatype = OpenILS::SIP->get_option_value('msg64_summary_datatype') || '';
573 for my $circid (@charges) {
575 if($return_datatype eq 'barcode') {
576 push( @c, __circ_to_barcode($self->{editor}, $circid));
578 push( @c, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
584 return (defined $start and defined $end) ?
585 [ $charges[($start-1)..($end-1)] ] :
590 my ($self, $start, $end) = @_;
592 syslog('LOG_DEBUG', 'OILS: Patron->fine_items()');
593 return (defined $start and defined $end) ?
594 [ $fines[($start-1)..($end-1)] ] : \@fines;
597 # not currently supported
599 my ($self, $start, $end) = @_;
604 my ($self, $start, $end) = @_;
605 syslog('LOG_DEBUG', 'OILS: Patron->unavail_holds()');
607 my @holds_sip_output = map {
608 OpenILS::SIP::clean_text($self->__hold_to_title($_))
610 $self->{editor}->search_action_hold_request({
611 usr => $self->{user}->id,
612 fulfillment_time => undef,
613 cancel_time => undef,
618 return (defined $start and defined $end) ?
619 [ @holds_sip_output[($start-1)..($end-1)] ] :
624 my ($self, $card_retained, $blocked_card_msg) = @_;
625 $blocked_card_msg ||= '';
627 my $e = $self->{editor};
628 my $u = $self->{user};
630 syslog('LOG_INFO', "OILS: Blocking user %s", $u->card->barcode );
632 return $self if $u->card->active eq 'f';
634 $e->xact_begin; # connect and start a new transaction
636 $u->card->active('f');
637 if( ! $e->update_actor_card($u->card) ) {
638 syslog('LOG_ERR', "OILS: Block card update failed: %s", $e->event->{textcode});
639 $e->rollback; # rollback + disconnect
643 # retrieve the un-fleshed user object for update
644 $u = $e->retrieve_actor_user($u->id);
645 my $note = OpenILS::SIP::clean_text($u->alert_message) || "";
646 $note = "<sip> CARD BLOCKED BY SELF-CHECK MACHINE. $blocked_card_msg</sip>\n$note"; # XXX Config option
647 $note =~ s/\s*$//; # kill trailng whitespace
648 $u->alert_message($note);
650 if( ! $e->update_actor_user($u) ) {
651 syslog('LOG_ERR', "OILS: Block: patron alert update failed: %s", $e->event->{textcode});
652 $e->rollback; # rollback + disconnect
657 $self->{user}->alert_message( $note );
659 $e->commit; # commits and disconnects
663 # Testing purposes only
665 my ($self, $card_retained) = @_;
666 $self->{screen_msg} = "All privileges restored.";
668 # Un-mark card as inactive, grep out the patron alert
669 my $e = $self->{editor};
670 my $u = $self->{user};
672 syslog('LOG_INFO', "OILS: Unblocking user %s", $u->card->barcode );
674 return $self if $u->card->active eq 't';
676 $e->xact_begin; # connect and start a new transaction
678 $u->card->active('t');
679 if( ! $e->update_actor_card($u->card) ) {
680 syslog('LOG_ERR', "OILS: Unblock card update failed: %s", $e->event->{textcode});
681 $e->rollback; # rollback + disconnect
685 # retrieve the un-fleshed user object for update
686 $u = $e->retrieve_actor_user($u->id);
687 my $note = OpenILS::SIP::clean_text($u->alert_message) || "";
688 $note =~ s#<sip>.*</sip>##;
689 $note =~ s/^\s*//; # kill leading whitespace
690 $note =~ s/\s*$//; # kill trailng whitespace
691 $u->alert_message($note);
693 if( ! $e->update_actor_user($u) ) {
694 syslog('LOG_ERR', "OILS: Unblock: patron alert update failed: %s", $e->event->{textcode});
695 $e->rollback; # rollback + disconnect
700 $self->{user}->alert_message( $note );
702 $e->commit; # commits and disconnects
711 return "Please contact library staff";
715 return "Please contact library staff";
718 sub inet_privileges {
720 my $e = OpenILS::SIP->editor();
721 $INET_PRIVS = $e->retrieve_all_config_net_access_level() unless $INET_PRIVS;
722 my ($level) = grep { $_->id eq $self->{user}->net_access_level } @$INET_PRIVS;
723 my $name = OpenILS::SIP::clean_text($level->name);
724 syslog('LOG_DEBUG', "OILS: Patron inet_privs = $name");
730 my $extra_fields = {};
731 my $u = $self->{user};
732 foreach my $stat_cat_entry (@{$u->stat_cat_entries}) {
733 my $stat_cat = $stat_cat_entry->stat_cat;
734 next unless ($stat_cat->sip_field);
735 my $value = $stat_cat_entry->stat_cat_entry;
736 if(defined $stat_cat->sip_format && length($stat_cat->sip_format) > 0) { # Has a format string?
737 if($stat_cat->sip_format =~ /^\|(.*)\|$/) { # Regex match?
738 if($value =~ /($1)/) { # If we have a match
739 if(defined $2) { # Check to see if they embedded a capture group
740 $value = $2; # If so, use it
742 else { # No embedded capture group?
743 $value = $1; # Use our outer one
747 $value = ''; # Empty string. Will be checked for below.
750 else { # Not a regex match - Try sprintf match (looking for a %s, if any)
751 $value = sprintf($stat_cat->sip_format, $value);
754 next unless length($value) > 0; # No value = no export
755 $value =~ s/\|//g; # Remove all lingering pipe chars for sane output purposes
756 $extra_fields->{ $stat_cat->sip_field } = [] unless (defined $extra_fields->{$stat_cat->sip_field});
757 push(@{$extra_fields->{ $stat_cat->sip_field}}, $value);
759 return $extra_fields;