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();
73 # in some cases, we don't need all of this data. Only fetch the user + barcode
74 $usr_flesh = {flesh => 1, flesh_fields => {au => ['card']}} if $args{slim_user};
77 if($key eq 'barcode') { # retrieve user by barcode
79 $$usr_flesh{flesh} += 1;
80 $$usr_flesh{flesh_fields}{ac} = ['usr'];
82 my $card = $e->search_actor_card([{barcode => $patron_id}, $usr_flesh])->[0];
85 syslog("LOG_WARNING", "No such patron barcode: $patron_id");
92 $user = $e->retrieve_actor_user([$patron_id, $usr_flesh]);
96 syslog("LOG_WARNING", "OILS: Unable to find patron %s => %s", $key, $patron_id);
100 # now grab the user's penalties
102 $self->flesh_user_penalties($user, $e) unless $args{slim_user};
104 $self->{editor} = $e;
105 $self->{user} = $user;
106 $self->{id} = ($key eq 'barcode') ? $patron_id : $user->card->barcode; # The barcode IS the ID to SIP.
107 # We give back the passed barcode if the key was indeed a barcode, just to be safe. Otherwise pull it from the card.
109 syslog("LOG_DEBUG", "OILS: new OpenILS Patron(%s => %s): found patron : barred=%s, card:active=%s",
110 $key, $patron_id, $user->barred, $user->card->active );
115 # grab patron penalties. Only grab non-archived penalties that are for fines,
116 # excessive overdues, or otherwise block circluation activity
117 sub flesh_user_penalties {
118 my ($self, $user, $e) = @_;
120 $user->standing_penalties(
121 $e->search_actor_user_standing_penalty([
126 # ignore "archived" penalties
127 {stop_date => undef},
128 {stop_date => {'>' => 'now'}}
136 transform => 'actor.org_unit_ancestors',
142 # at this point, there is no concept of "here", so fetch penalties
143 # for the patron's home lib plus ancestors
144 where => {id => $user->home_ou},
149 # in addition to fines and excessive overdue penalties,
150 # we only care about penalties that result in blocks
151 standing_penalty => {
153 select => {csp => ['id']},
157 {id => [1,2]}, # fines / overdues
158 {block_list => {'!=' => undef}}
175 return format_name($self->{user});
180 return OpenILS::SIP::clean_text(
182 ($u->first_given_name || ''),
183 ($u->second_given_name || ''),
184 ($u->family_name || '')));
189 my $lib = OpenILS::SIP::shortname_from_id($self->{user}->home_ou);
190 syslog('LOG_DEBUG', "OILS: Patron->home_library() = $lib");
196 return "" unless $addr;
197 my $return = OpenILS::SIP::clean_text(
198 join( ' ', map {$_ || ''} (
209 $return =~ s/\s+/ /sg; # Compress any run of of whitespace to one space
215 return $self->{user}->id;
220 my $u = $self->{user};
221 my $str = __addr_string($u->billing_address || $u->mailing_address);
222 syslog('LOG_DEBUG', "OILS: Patron address: $str");
228 return OpenILS::SIP::clean_text($self->{user}->email);
233 return $self->{user}->day_phone;
238 my $dob = OpenILS::SIP->format_date($self->{user}->dob);
239 syslog('LOG_DEBUG', "OILS: Patron DOB = $dob");
245 my $expire = OpenILS::SIP->format_date($self->{user}->expire_date);
246 syslog('LOG_DEBUG', "OILS: Patron Expire = $expire");
253 my $use_code = OpenILS::SIP->get_option_value('patron_type_uses_code') || '';
255 # should we use the no_i18n version of patron profile name (as a 'code')?
256 return $self->{editor}->retrieve_permission_grp_tree(
257 [$self->{user}->profile->id, {no_i18n => 1}])->name
258 if $use_code =~ /true/io;
260 return OpenILS::SIP::clean_text($self->{user}->profile->name);
265 return '000'; # Unspecified
268 # How much more detail do we need to check here?
271 my $u = $self->{user};
272 return (($u->barred eq 'f') and ($u->card->active eq 't'));
275 # How much more detail do we need to check here?
278 return $self->charge_ok;
288 return $self->charge_ok;
291 # return true if the card provided is marked as lost
294 return $self->{user}->card->active eq 'f';
297 sub recall_overdue { # not implemented
303 my ($self, $pwd) = @_;
304 syslog('LOG_DEBUG', 'OILS: Patron->check_password()');
305 return 0 unless (defined $pwd and $self->{user});
306 return md5_hex($pwd) eq $self->{user}->passwd;
309 sub currency { # not really implemented
311 syslog('LOG_DEBUG', 'OILS: Patron->currency()');
317 syslog('LOG_DEBUG', 'OILS: Patron->fee_amount()');
318 my $user_id = $self->{user}->id;
320 my $e = $self->{editor};
322 my $summary = $e->retrieve_money_open_user_summary($user_id);
323 $e->rollback; # xact_rollback + disconnect
325 my $total = ($summary) ? $summary->balance_owed : 0;
326 syslog('LOG_INFO', "User ".$self->{id} .":$user_id has a fee amount of \$$total");
332 my $u = $self->{user};
334 return 'barred' if $u->barred eq 't';
338 return $b if $u->active eq 'f';
339 return $b if $u->card->active eq 'f';
341 # if we have any penalties at this point, they are blocking penalties
342 return $b if $u->standing_penalties and @{$u->standing_penalties};
344 # has the patron account expired?
345 my $expire = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($u->expire_date));
346 return $b if CORE::time > $expire->epoch;
351 sub print_line { # not implemented
356 sub too_many_charged { # not implemented
361 sub too_many_overdue {
363 return scalar( # PATRON_EXCEEDS_OVERDUE_COUNT
364 grep { $_->standing_penalty == 2 } @{$self->{user}->standing_penalties}
368 # not completely sure what this means
369 sub too_many_renewal {
374 # not relevant, handled by fines/fees
375 sub too_many_claim_return {
380 # not relevant, handled by fines/fees
386 sub excessive_fines {
388 return scalar( # PATRON_EXCEEDS_FINES
389 grep { $_->standing_penalty == 1 } @{$self->{user}->standing_penalties}
393 # Until someone suggests otherwise, fees and fines are the same
397 return $self->excessive_fines;
400 # not relevant, handled by fines/fees
401 sub too_many_billed {
409 # List of outstanding holds placed
412 my ($self, $start, $end) = @_;
413 syslog('LOG_DEBUG', 'OILS: Patron->hold_items()');
415 my $holds = $self->{editor}->search_action_hold_request(
416 { usr => $self->{user}->id, fulfillment_time => undef, cancel_time => undef }
420 push( @holds, OpenILS::SIP::clean_text($self->__hold_to_title($_)) ) for @$holds;
422 return (defined $start and defined $end) ?
423 [ $holds[($start-1)..($end-1)] ] :
427 sub __hold_to_title {
430 my $e = $self->{editor};
432 my( $id, $mods, $title, $volume, $copy );
434 return __copy_to_title($e,
435 $e->retrieve_asset_copy($hold->target))
436 if $hold->hold_type eq 'C';
438 return __volume_to_title($e,
439 $e->retrieve_asset_call_number($hold->target))
440 if $hold->hold_type eq 'V';
442 return __record_to_title(
443 $e, $hold->target) if $hold->hold_type eq 'T';
445 return __metarecord_to_title(
446 $e, $hold->target) if $hold->hold_type eq 'M';
449 sub __copy_to_title {
450 my( $e, $copy ) = @_;
451 #syslog('LOG_DEBUG', "OILS: copy_to_title(%s)", $copy->id);
452 return $copy->dummy_title if $copy->call_number == -1;
454 my $vol = (ref $copy->call_number) ?
456 $e->retrieve_asset_call_number($copy->call_number);
458 return __volume_to_title($e, $vol);
462 sub __volume_to_title {
463 my( $e, $volume ) = @_;
464 #syslog('LOG_DEBUG', "OILS: volume_to_title(%s)", $volume->id);
465 return __record_to_title($e, $volume->record);
469 sub __record_to_title {
470 my( $e, $title_id ) = @_;
471 #syslog('LOG_DEBUG', "OILS: record_to_title($title_id)");
472 my $mods = $U->simplereq(
474 'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
475 return ($mods) ? $mods->title : "";
478 sub __metarecord_to_title {
479 my( $e, $m_id ) = @_;
480 #syslog('LOG_DEBUG', "OILS: metarecord_to_title($m_id)");
481 my $mods = $U->simplereq(
483 'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
484 return ($U->event_code($mods)) ? "<unknown>" : $mods->title;
489 # remove the hold on item item_id from my hold queue.
490 # return true if I was holding the item, false otherwise.
493 my ($self, $item_id) = @_;
497 sub __patron_items_info {
499 return if $self->{item_info};
501 OpenILS::Application::Actor::_checked_out(
502 0, $self->{editor}, $self->{user}->id);;
508 my ($self, $start, $end) = @_;
510 $self->__patron_items_info();
511 my @overdues = @{$self->{item_info}->{overdue}};
512 #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
515 syslog('LOG_DEBUG', "OILS: overdue_items() fleshing circs @overdues");
517 my $return_datatype = OpenILS::SIP->get_option_value('msg64_summary_datatype') || '';
519 for my $circid (@overdues) {
521 if($return_datatype eq 'barcode') {
522 push( @o, __circ_to_barcode($self->{editor}, $circid));
524 push( @o, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
529 return (defined $start and defined $end) ?
530 [ $overdues[($start-1)..($end-1)] ] : \@overdues;
533 sub __circ_to_barcode {
536 $circ = $e->retrieve_action_circulation($circ);
537 my $copy = $e->retrieve_asset_copy($circ->target_copy);
538 return $copy->barcode;
541 sub __circ_to_title {
542 my( $e, $circ ) = @_;
544 $circ = $e->retrieve_action_circulation($circ);
545 return __copy_to_title( $e,
546 $e->retrieve_asset_copy($circ->target_copy) );
550 my ($self, $start, $end) = shift;
552 $self->__patron_items_info();
555 @{$self->{item_info}->{out}},
556 @{$self->{item_info}->{overdue}}
559 #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
562 syslog('LOG_DEBUG', "OILS: charged_items() fleshing circs @charges");
564 my $return_datatype = OpenILS::SIP->get_option_value('msg64_summary_datatype') || '';
566 for my $circid (@charges) {
568 if($return_datatype eq 'barcode') {
569 push( @c, __circ_to_barcode($self->{editor}, $circid));
571 push( @c, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
577 return (defined $start and defined $end) ?
578 [ $charges[($start-1)..($end-1)] ] :
583 my ($self, $start, $end) = @_;
585 syslog('LOG_DEBUG', 'OILS: Patron->fine_items()');
586 return (defined $start and defined $end) ?
587 [ $fines[($start-1)..($end-1)] ] : \@fines;
590 # not currently supported
592 my ($self, $start, $end) = @_;
597 my ($self, $start, $end) = @_;
598 syslog('LOG_DEBUG', 'OILS: Patron->unavail_holds()');
600 my @holds_sip_output = map {
601 OpenILS::SIP::clean_text($self->__hold_to_title($_))
603 $self->{editor}->search_action_hold_request({
604 usr => $self->{user}->id,
605 fulfillment_time => undef,
606 cancel_time => undef,
611 return (defined $start and defined $end) ?
612 [ @holds_sip_output[($start-1)..($end-1)] ] :
617 my ($self, $card_retained, $blocked_card_msg) = @_;
618 $blocked_card_msg ||= '';
620 my $e = $self->{editor};
621 my $u = $self->{user};
623 syslog('LOG_INFO', "OILS: Blocking user %s", $u->card->barcode );
625 return $self if $u->card->active eq 'f';
627 $e->xact_begin; # connect and start a new transaction
629 $u->card->active('f');
630 if( ! $e->update_actor_card($u->card) ) {
631 syslog('LOG_ERR', "OILS: Block card update failed: %s", $e->event->{textcode});
632 $e->rollback; # rollback + disconnect
636 # retrieve the un-fleshed user object for update
637 $u = $e->retrieve_actor_user($u->id);
638 my $note = OpenILS::SIP::clean_text($u->alert_message) || "";
639 $note = "<sip> CARD BLOCKED BY SELF-CHECK MACHINE. $blocked_card_msg</sip>\n$note"; # XXX Config option
640 $note =~ s/\s*$//; # kill trailng whitespace
641 $u->alert_message($note);
643 if( ! $e->update_actor_user($u) ) {
644 syslog('LOG_ERR', "OILS: Block: patron alert update failed: %s", $e->event->{textcode});
645 $e->rollback; # rollback + disconnect
650 $self->{user}->alert_message( $note );
652 $e->commit; # commits and disconnects
656 # Testing purposes only
658 my ($self, $card_retained) = @_;
659 $self->{screen_msg} = "All privileges restored.";
661 # Un-mark card as inactive, grep out the patron alert
662 my $e = $self->{editor};
663 my $u = $self->{user};
665 syslog('LOG_INFO', "OILS: Unblocking user %s", $u->card->barcode );
667 return $self if $u->card->active eq 't';
669 $e->xact_begin; # connect and start a new transaction
671 $u->card->active('t');
672 if( ! $e->update_actor_card($u->card) ) {
673 syslog('LOG_ERR', "OILS: Unblock card update failed: %s", $e->event->{textcode});
674 $e->rollback; # rollback + disconnect
678 # retrieve the un-fleshed user object for update
679 $u = $e->retrieve_actor_user($u->id);
680 my $note = OpenILS::SIP::clean_text($u->alert_message) || "";
681 $note =~ s#<sip>.*</sip>##;
682 $note =~ s/^\s*//; # kill leading whitespace
683 $note =~ s/\s*$//; # kill trailng whitespace
684 $u->alert_message($note);
686 if( ! $e->update_actor_user($u) ) {
687 syslog('LOG_ERR', "OILS: Unblock: patron alert update failed: %s", $e->event->{textcode});
688 $e->rollback; # rollback + disconnect
693 $self->{user}->alert_message( $note );
695 $e->commit; # commits and disconnects
704 return "Please contact library staff";
708 return "Please contact library staff";
711 sub inet_privileges {
713 my $e = OpenILS::SIP->editor();
714 $INET_PRIVS = $e->retrieve_all_config_net_access_level() unless $INET_PRIVS;
715 my ($level) = grep { $_->id eq $self->{user}->net_access_level } @$INET_PRIVS;
716 my $name = OpenILS::SIP::clean_text($level->name);
717 syslog('LOG_DEBUG', "OILS: Patron inet_privs = $name");