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 my ($class, $patron_id) = @_;
34 my $type = ref($class) || $class;
37 syslog("LOG_DEBUG", "OILS: new OpenILS Patron(%s): searching...", $patron_id);
39 my $e = OpenILS::SIP->editor();
41 my $c = $e->search_actor_card({barcode => $patron_id}, {idlist=>1});
46 $user = $e->search_actor_user(
67 $user = (@$user) ? $$user[0] : undef;
71 syslog("LOG_WARNING", "OILS: Unable to find patron %s", $patron_id);
75 $self->{user} = $user;
76 $self->{id} = $patron_id;
79 syslog("LOG_DEBUG", "OILS: new OpenILS Patron(%s): found patron : barred=%s, card:active=%s",
80 $patron_id, $self->{user}->barred, $self->{user}->card->active );
94 my $u = $self->{user};
95 return $u->first_given_name . ' ' .
96 $u->second_given_name . ' ' . $u->family_name;
101 return "" unless $addr;
102 return $addr->street1 .' '.
113 my $u = $self->{user};
114 my $addr = $u->billing_address;
115 $addr = $u->mailing_address unless $addr;
116 my $str = __addr_string($addr);
117 syslog('LOG_DEBUG', "OILS: Patron address: $str");
118 #my $maddr = $u->mailing_address;
119 #$str .= "\n" . __addr_string($maddr)
120 #if $maddr and $maddr->id ne $addr->id;
126 return $self->{user}->email;
131 return $self->{user}->day_phone;
136 my $dob = OpenILS::SIP->format_date($self->{user}->dob);
137 syslog('LOG_DEBUG', "OILS: Patron DOB = $dob");
143 return $self->{user}->profile->name;
148 return '000'; # Unspecified
151 # How much more detail do we need to check here?
154 my $u = $self->{user};
155 return (($u->barred eq 'f') and ($u->card->active eq 't'));
158 # How much more detail do we need to check here?
161 return $self->charge_ok;
174 # return true if the card provided is marked as lost
187 my ($self, $pwd) = @_;
188 return md5_hex($pwd) eq $self->{user}->passwd;
212 sub too_many_charged {
217 sub too_many_overdue {
219 if( $self->{user}->standing_penalties ) {
220 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_OVERDUE_COUNT' }
221 @{$self->{user}->standing_penalties};
226 # not completely sure what this means
227 sub too_many_renewal {
232 # not relevant, handled by fines/fees
233 sub too_many_claim_return {
238 # not relevant, handled by fines/fees
244 sub excessive_fines {
246 if( $self->{user}->standing_penalties ) {
247 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' }
248 @{$self->{user}->standing_penalties};
254 # Until someone suggests otherwise, fees and fines are the same
258 if( $self->{user}->standing_penalties ) {
259 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' }
260 @{$self->{user}->standing_penalties};
265 # not relevant, handled by fines/fees
266 sub too_many_billed {
274 # List of outstanding holds placed
277 my ($self, $start, $end) = @_;
279 my $holds = $self->{editor}->search_action_hold_request(
280 { usr => $self->{user}->id, fulfillment_time => undef }
284 push( @holds, $self->__hold_to_title($_) ) for @$holds;
286 return (defined $start and defined $end) ?
287 [ $holds[($start-1)..($end-1)] ] :
291 sub __hold_to_title {
294 my $e = $self->{editor};
296 my( $id, $mods, $title, $volume, $copy );
298 return __copy_to_title($e,
299 $e->retrieve_asset_copy($hold->target))
300 if $hold->hold_type eq 'C';
302 return __volume_to_title($e,
303 $e->retrieve_asset_call_number($hold->target))
304 if $hold->hold_type eq 'V';
306 return __record_to_title(
307 $e, $hold->target) if $hold->hold_type eq 'T';
309 return __metarecord_to_title(
310 $e, $hold->target) if $hold->hold_type eq 'M';
313 sub __copy_to_title {
314 my( $e, $copy ) = @_;
315 #syslog('LOG_DEBUG', "OILS: copy_to_title(%s)", $copy->id);
316 return $copy->dummy_title if $copy->call_number == -1;
317 my $vol = $e->retrieve_asset_call_number($copy->call_number);
318 return __volume_to_title($e, $vol);
322 sub __volume_to_title {
323 my( $e, $volume ) = @_;
324 #syslog('LOG_DEBUG', "OILS: volume_to_title(%s)", $volume->id);
325 return __record_to_title($e, $volume->record);
329 sub __record_to_title {
330 my( $e, $title_id ) = @_;
331 #syslog('LOG_DEBUG', "OILS: record_to_title($title_id)");
332 my $mods = $U->simplereq(
334 'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
335 return ($mods) ? $mods->title : "";
338 sub __metarecord_to_title {
339 my( $e, $m_id ) = @_;
340 #syslog('LOG_DEBUG', "OILS: metarecord_to_title($m_id)");
341 my $mods = $U->simplereq(
343 'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
344 return ($U->event_code($mods)) ? "<unknown>" : $mods->title;
349 # remove the hold on item item_id from my hold queue.
350 # return true if I was holding the item, false otherwise.
353 my ($self, $item_id) = @_;
357 sub __patron_items_info {
359 return if $self->{items_info};
361 OpenILS::Application::Actor::_checked_out(
362 0, $self->{editor}, $self->{user}->id);;
366 my ($self, $start, $end) = @_;
368 $self->__patron_items_info();
369 my @overdues = @{$self->{item_info}->{overdue}};
370 #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
373 syslog('LOG_DEBUG', "OILS: overdue_items() fleshing circs @overdues");
374 for my $circid (@overdues) {
376 push( @o, __circ_to_title($self->{editor}, $circid) );
380 return (defined $start and defined $end) ?
381 [ $overdues[($start-1)..($end-1)] ] : \@overdues;
384 sub __circ_to_title {
385 my( $e, $circ ) = @_;
387 $circ = $e->retrieve_action_circulation($circ);
388 return __copy_to_title( $e,
389 $e->retrieve_asset_copy($circ->target_copy) );
393 my ($self, $start, $end) = shift;
395 $self->__patron_items_info();
398 @{$self->{item_info}->{out}},
399 @{$self->{item_info}->{overdue}}
402 #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
405 syslog('LOG_DEBUG', "OILS: charged_items() fleshing circs @charges");
406 for my $circid (@charges) {
408 push( @c, __circ_to_title($self->{editor}, $circid) );
412 return (defined $start and defined $end) ?
413 [ $charges[($start-1)..($end-1)] ] :
418 my ($self, $start, $end) = @_;
420 return (defined $start and defined $end) ?
421 [ $fines[($start-1)..($end-1)] ] : \@fines;
424 # not currently supported
426 my ($self, $start, $end) = @_;
431 my ($self, $start, $end) = @_;
433 return (defined $start and defined $end) ?
434 [ $holds[($start-1)..($end-1)] ] : \@holds;
438 my ($self, $card_retained, $blocked_card_msg) = @_;
440 my $u = $self->{user};
441 my $e = $self->{editor} = OpenILS::SIP->reset_editor();
443 syslog('LOG_INFO', "OILS: Blocking user %s", $u->card->barcode );
445 return $self if $u->card->active eq 'f';
447 $u->card->active('f');
448 if( ! $e->update_actor_card($u->card) ) {
449 syslog('LOG_ERR', "OILS: Block card update failed: %s", $e->event->{textcode});
454 # retrieve the un-fleshed user object for update
455 $u = $e->retrieve_actor_user($u->id);
456 my $note = $u->alert_message || "";
457 $note = "CARD BLOCKED BY SELF-CHECK MACHINE\n$note"; # XXX Config option
459 $u->alert_message($note);
461 if( ! $e->update_actor_user($u) ) {
462 syslog('LOG_ERR', "OILS: Block: patron alert update failed: %s", $e->event->{textcode});
468 $self->{user}->alert_message( $note );
470 $e->commit; # commits and resets
471 $self->{editor} = OpenILS::SIP->reset_editor();
475 # Testing purposes only
478 # Un-mark card as inactive, grep out the patron alert
479 $self->{screen_msg} = "All privileges restored.";
488 return "Please contact library staff";
492 return "Please contact library staff";
495 sub inet_privileges {
497 my $e = OpenILS::SIP->editor();
498 $INET_PRIVS = $e->retrieve_all_config_net_access_level() unless $INET_PRIVS;
499 my ($level) = grep { $_->id eq $self->{user}->net_access_level } @$INET_PRIVS;
500 my $name = $level->name;
501 syslog('LOG_DEBUG', "OILS: Patron inet_privs = $name");