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;
42 if ($key ne 'usr' and $key ne 'barcode') {
43 syslog("LOG_ERROR", "Patron (card) lookup requested by illegeal key '$key'");
47 unless(defined $patron_id) {
48 syslog("LOG_WARNING", "No patron ID provided to ILS::Patron->new");
52 my $type = ref($class) || $class;
55 syslog("LOG_DEBUG", "OILS: new OpenILS Patron(%s => %s): searching...", $key, $patron_id);
57 my $e = OpenILS::SIP->editor();
59 my $user_id = $patron_id;
60 if($key eq 'barcode') {
61 my $card = $e->search_actor_card({barcode => $patron_id})->[0];
63 syslog("LOG_WARNING", "No such patron barcode: $patron_id");
66 $user_id = $card->usr;
69 my $user = $e->retrieve_actor_user([
82 ausp => ['standing_penalty']
88 syslog("LOG_WARNING", "OILS: Unable to find patron %s => %s", $key, $patron_id);
93 $self->{user} = $user;
94 $self->{id} = ($key eq 'barcode') ? $patron_id : $user->card->barcode; # The barcode IS the ID to SIP.
95 # We give back the passed barcode if the key was indeed a barcode, just to be safe. Otherwise pull it from the card.
97 syslog("LOG_DEBUG", "OILS: new OpenILS Patron(%s => %s): found patron : barred=%s, card:active=%s",
98 $key, $patron_id, $user->barred, $user->card->active );
112 my $u = $self->{user};
113 return OpenILS::SIP::clean_text(
115 ($u->first_given_name || ''),
116 ($u->second_given_name || ''),
117 ($u->family_name || '')));
123 my $lib = $self->{editor}->retrieve_actor_org_unit($self->{user}->home_ou)->shortname;
124 syslog('LOG_DEBUG', "OILS: Patron->home_library() = $lib");
130 return "" unless $addr;
131 my $return = OpenILS::SIP::clean_text(
132 join( ' ', map {$_ || ''} (
143 $return =~ s/\s+/ /sg; # Compress any run of of whitespace to one space
149 return $self->{user}->id;
154 my $u = $self->{user};
155 my $str = __addr_string($u->billing_address || $u->mailing_address);
156 syslog('LOG_DEBUG', "OILS: Patron address: $str");
162 return OpenILS::SIP::clean_text($self->{user}->email);
167 return $self->{user}->day_phone;
172 my $dob = OpenILS::SIP->format_date($self->{user}->dob);
173 syslog('LOG_DEBUG', "OILS: Patron DOB = $dob");
180 my $use_code = OpenILS::SIP->get_option_value('patron_type_uses_code') || '';
182 # should we use the no_i18n version of patron profile name (as a 'code')?
183 return $self->{editor}->retrieve_permission_grp_tree(
184 [$self->{user}->profile->id, {no_i18n => 1}])->name
185 if $use_code =~ /true/io;
187 return OpenILS::SIP::clean_text($self->{user}->profile->name);
192 return '000'; # Unspecified
195 # How much more detail do we need to check here?
198 my $u = $self->{user};
199 return (($u->barred eq 'f') and ($u->card->active eq 't'));
202 # How much more detail do we need to check here?
205 return $self->charge_ok;
215 return $self->charge_ok;
218 # return true if the card provided is marked as lost
221 return $self->{user}->card->active eq 'f';
231 my ($self, $pwd) = @_;
232 syslog('LOG_DEBUG', 'OILS: Patron->check_password()');
233 return $pwd and (md5_hex($pwd) eq $self->{user}->passwd);
239 syslog('LOG_DEBUG', 'OILS: Patron->currency()');
245 syslog('LOG_DEBUG', 'OILS: Patron->fee_amount()');
246 my $user_id = $self->{user}->id;
248 my $e = $self->{editor};
250 my $summary = $e->retrieve_money_open_user_summary($user_id);
251 $e->rollback; # xact_rollback + disconnect
253 my $total = ($summary) ? $summary->balance_owed : 0;
254 syslog('LOG_INFO', "User ".$self->{id} .":$user_id has a fee amount of \$$total");
260 my $u = $self->{user};
261 return 'barred' if $u->barred eq 't';
265 return $b if $u->active eq 'f';
266 return $b if $u->card->active eq 'f';
268 if( $u->standing_penalties ) {
270 grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_OVERDUE_COUNT' }
271 @{$u->standing_penalties};
274 grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_FINES' }
275 @{$u->standing_penalties};
278 my $expire = DateTime::Format::ISO8601->new->parse_datetime(
279 cleanse_ISO8601($u->expire_date));
281 return $b if CORE::time > $expire->epoch;
291 sub too_many_charged {
296 sub too_many_overdue {
298 if( $self->{user}->standing_penalties ) {
299 return grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_OVERDUE_COUNT' }
300 @{$self->{user}->standing_penalties};
305 # not completely sure what this means
306 sub too_many_renewal {
311 # not relevant, handled by fines/fees
312 sub too_many_claim_return {
317 # not relevant, handled by fines/fees
323 sub excessive_fines {
325 syslog('LOG_DEBUG', 'OILS: Patron->excessive_fines()');
326 if( $self->{user}->standing_penalties ) {
327 return grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_FINES' }
328 @{$self->{user}->standing_penalties};
334 # Until someone suggests otherwise, fees and fines are the same
338 syslog('LOG_DEBUG', 'OILS: Patron->excessive_fees()');
339 if( $self->{user}->standing_penalties ) {
340 return grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_FINES' }
341 @{$self->{user}->standing_penalties};
346 # not relevant, handled by fines/fees
347 sub too_many_billed {
355 # List of outstanding holds placed
358 my ($self, $start, $end) = @_;
359 syslog('LOG_DEBUG', 'OILS: Patron->hold_items()');
361 my $holds = $self->{editor}->search_action_hold_request(
362 { usr => $self->{user}->id, fulfillment_time => undef, cancel_time => undef }
366 push( @holds, OpenILS::SIP::clean_text($self->__hold_to_title($_)) ) for @$holds;
368 return (defined $start and defined $end) ?
369 [ $holds[($start-1)..($end-1)] ] :
373 sub __hold_to_title {
376 my $e = $self->{editor};
378 my( $id, $mods, $title, $volume, $copy );
380 return __copy_to_title($e,
381 $e->retrieve_asset_copy($hold->target))
382 if $hold->hold_type eq 'C';
384 return __volume_to_title($e,
385 $e->retrieve_asset_call_number($hold->target))
386 if $hold->hold_type eq 'V';
388 return __record_to_title(
389 $e, $hold->target) if $hold->hold_type eq 'T';
391 return __metarecord_to_title(
392 $e, $hold->target) if $hold->hold_type eq 'M';
395 sub __copy_to_title {
396 my( $e, $copy ) = @_;
397 #syslog('LOG_DEBUG', "OILS: copy_to_title(%s)", $copy->id);
398 return $copy->dummy_title if $copy->call_number == -1;
400 my $vol = (ref $copy->call_number) ?
402 $e->retrieve_asset_call_number($copy->call_number);
404 return __volume_to_title($e, $vol);
408 sub __volume_to_title {
409 my( $e, $volume ) = @_;
410 #syslog('LOG_DEBUG', "OILS: volume_to_title(%s)", $volume->id);
411 return __record_to_title($e, $volume->record);
415 sub __record_to_title {
416 my( $e, $title_id ) = @_;
417 #syslog('LOG_DEBUG', "OILS: record_to_title($title_id)");
418 my $mods = $U->simplereq(
420 'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
421 return ($mods) ? $mods->title : "";
424 sub __metarecord_to_title {
425 my( $e, $m_id ) = @_;
426 #syslog('LOG_DEBUG', "OILS: metarecord_to_title($m_id)");
427 my $mods = $U->simplereq(
429 'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
430 return ($U->event_code($mods)) ? "<unknown>" : $mods->title;
435 # remove the hold on item item_id from my hold queue.
436 # return true if I was holding the item, false otherwise.
439 my ($self, $item_id) = @_;
443 sub __patron_items_info {
445 return if $self->{item_info};
447 OpenILS::Application::Actor::_checked_out(
448 0, $self->{editor}, $self->{user}->id);;
454 my ($self, $start, $end) = @_;
456 $self->__patron_items_info();
457 my @overdues = @{$self->{item_info}->{overdue}};
458 #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
461 syslog('LOG_DEBUG', "OILS: overdue_items() fleshing circs @overdues");
463 my $return_datatype = OpenILS::SIP->get_option_value('msg64_summary_datatype') || '';
465 for my $circid (@overdues) {
467 if($return_datatype eq 'barcode') {
468 push( @o, __circ_to_barcode($self->{editor}, $circid));
470 push( @o, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
475 return (defined $start and defined $end) ?
476 [ $overdues[($start-1)..($end-1)] ] : \@overdues;
479 sub __circ_to_barcode {
482 $circ = $e->retrieve_action_circulation($circ);
483 my $copy = $e->retrieve_asset_copy($circ->target_copy);
484 return $copy->barcode;
487 sub __circ_to_title {
488 my( $e, $circ ) = @_;
490 $circ = $e->retrieve_action_circulation($circ);
491 return __copy_to_title( $e,
492 $e->retrieve_asset_copy($circ->target_copy) );
496 my ($self, $start, $end) = shift;
498 $self->__patron_items_info();
501 @{$self->{item_info}->{out}},
502 @{$self->{item_info}->{overdue}}
505 #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
508 syslog('LOG_DEBUG', "OILS: charged_items() fleshing circs @charges");
510 my $return_datatype = OpenILS::SIP->get_option_value('msg64_summary_datatype') || '';
512 for my $circid (@charges) {
514 if($return_datatype eq 'barcode') {
515 push( @c, __circ_to_barcode($self->{editor}, $circid));
517 push( @c, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
523 return (defined $start and defined $end) ?
524 [ $charges[($start-1)..($end-1)] ] :
529 my ($self, $start, $end) = @_;
531 syslog('LOG_DEBUG', 'OILS: Patron->fine_items()');
532 return (defined $start and defined $end) ?
533 [ $fines[($start-1)..($end-1)] ] : \@fines;
536 # not currently supported
538 my ($self, $start, $end) = @_;
543 my ($self, $start, $end) = @_;
545 syslog('LOG_DEBUG', 'OILS: Patron->unavail_holds()');
546 return (defined $start and defined $end) ?
547 [ $holds[($start-1)..($end-1)] ] : \@holds;
551 my ($self, $card_retained, $blocked_card_msg) = @_;
552 $blocked_card_msg ||= '';
554 my $e = $self->{editor};
555 my $u = $self->{user};
557 syslog('LOG_INFO', "OILS: Blocking user %s", $u->card->barcode );
559 return $self if $u->card->active eq 'f';
561 $e->xact_begin; # connect and start a new transaction
563 $u->card->active('f');
564 if( ! $e->update_actor_card($u->card) ) {
565 syslog('LOG_ERR', "OILS: Block card update failed: %s", $e->event->{textcode});
566 $e->rollback; # rollback + disconnect
570 # retrieve the un-fleshed user object for update
571 $u = $e->retrieve_actor_user($u->id);
572 my $note = OpenILS::SIP::clean_text($u->alert_message) || "";
573 $note = "<sip> CARD BLOCKED BY SELF-CHECK MACHINE. $blocked_card_msg</sip>\n$note"; # XXX Config option
574 $note =~ s/\s*$//; # kill trailng whitespace
575 $u->alert_message($note);
577 if( ! $e->update_actor_user($u) ) {
578 syslog('LOG_ERR', "OILS: Block: patron alert update failed: %s", $e->event->{textcode});
579 $e->rollback; # rollback + disconnect
584 $self->{user}->alert_message( $note );
586 $e->commit; # commits and disconnects
590 # Testing purposes only
592 my ($self, $card_retained) = @_;
593 $self->{screen_msg} = "All privileges restored.";
595 # Un-mark card as inactive, grep out the patron alert
596 my $e = $self->{editor};
597 my $u = $self->{user};
599 syslog('LOG_INFO', "OILS: Unblocking user %s", $u->card->barcode );
601 return $self if $u->card->active eq 't';
603 $e->xact_begin; # connect and start a new transaction
605 $u->card->active('t');
606 if( ! $e->update_actor_card($u->card) ) {
607 syslog('LOG_ERR', "OILS: Unblock card update failed: %s", $e->event->{textcode});
608 $e->rollback; # rollback + disconnect
612 # retrieve the un-fleshed user object for update
613 $u = $e->retrieve_actor_user($u->id);
614 my $note = OpenILS::SIP::clean_text($u->alert_message) || "";
615 $note =~ s#<sip>.*</sip>##;
616 $note =~ s/^\s*//; # kill leading whitespace
617 $note =~ s/\s*$//; # kill trailng whitespace
618 $u->alert_message($note);
620 if( ! $e->update_actor_user($u) ) {
621 syslog('LOG_ERR', "OILS: Unblock: patron alert update failed: %s", $e->event->{textcode});
622 $e->rollback; # rollback + disconnect
627 $self->{user}->alert_message( $note );
629 $e->commit; # commits and disconnects
638 return "Please contact library staff";
642 return "Please contact library staff";
645 sub inet_privileges {
647 my $e = OpenILS::SIP->editor();
648 $INET_PRIVS = $e->retrieve_all_config_net_access_level() unless $INET_PRIVS;
649 my ($level) = grep { $_->id eq $self->{user}->net_access_level } @$INET_PRIVS;
650 my $name = OpenILS::SIP::clean_text($level->name);
651 syslog('LOG_DEBUG', "OILS: Patron inet_privs = $name");