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 my $U = 'OpenILS::Application::AppUtils';
22 our (@ISA, @EXPORT_OK);
26 @EXPORT_OK = qw(invalid_patron);
31 my ($class, $patron_id) = @_;
32 my $type = ref($class) || $class;
35 syslog("LOG_DEBUG", "new OpenILS Patron(%s): searching...", $patron_id);
37 my $e = OpenILS::SIP->editor();
39 my $c = $e->search_actor_card({barcode => $patron_id}, {idlist=>1});
44 $user = $e->search_actor_user(
65 $user = (@$user) ? $$user[0] : undef;
69 syslog("LOG_WARNING", "Unable to find patron %s", $patron_id);
73 $self->{user} = $user;
74 $self->{id} = $patron_id;
77 syslog("LOG_DEBUG", "new OpenILS Patron(%s): found patron : barred=%s, card:active=%s",
78 $patron_id, $self->{user}->barred, $self->{user}->card->active );
92 my $u = $self->{user};
93 return $u->first_given_name . ' ' .
94 $u->second_given_name . ' ' . $u->family_name;
99 return "" unless $addr;
100 return $addr->street1 .' '.
111 my $u = $self->{user};
112 my $addr = $u->billing_address;
113 my $str = __addr_string($addr);
114 my $maddr = $u->mailing_address;
115 $str .= "\n" . __addr_string($maddr)
116 if $maddr and $maddr->id ne $addr->id;
122 return $self->{user}->email;
127 return $self->{user}->day_phone;
132 return $self->{user}->dob;
137 return $self->{user}->profile->name;
142 return '000'; # Unspecified
145 # How much more detail do we need to check here?
148 my $u = $self->{user};
149 return (($u->barred eq 'f') and ($u->card->active eq 't'));
152 # How much more detail do we need to check here?
155 return $self->charge_ok;
168 # return true if the card provided is marked as lost
181 my ($self, $pwd) = @_;
182 return md5_hex($pwd) eq $self->{user}->passwd;
206 sub too_many_charged {
211 sub too_many_overdue {
213 if( $self->{user}->standing_penalties ) {
214 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_OVERDUE_COUNT' }
215 @{$self->{user}->standing_penalties};
220 # not completely sure what this means
221 sub too_many_renewal {
226 # not relevant, handled by fines/fees
227 sub too_many_claim_return {
232 # not relevant, handled by fines/fees
238 sub excessive_fines {
240 if( $self->{user}->standing_penalties ) {
241 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' }
242 @{$self->{user}->standing_penalties};
248 # Until someone suggests otherwise, fees and fines are the same
252 if( $self->{user}->standing_penalties ) {
253 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' }
254 @{$self->{user}->standing_penalties};
259 # not relevant, handled by fines/fees
260 sub too_many_billed {
268 # List of outstanding holds placed
271 my ($self, $start, $end) = @_;
273 my $holds = $self->{editor}->search_action_hold_request(
274 { usr => $self->{user}->id, fulfillment_time => undef }
278 push( @holds, $self->__hold_to_title($_) ) for @$holds;
280 return (defined $start and defined $end) ?
281 [ $holds[($start-1)..($end-1)] ] :
285 sub __hold_to_title {
288 my $e = $self->{editor};
290 my( $id, $mods, $title, $volume, $copy );
292 return __copy_to_title($e,
293 $e->retrieve_asset_copy($hold->target))
294 if $hold->hold_type eq 'C';
296 return __volume_to_title($e,
297 $e->retrieve_asset_call_number($hold->target))
298 if $hold->hold_type eq 'V';
300 return __record_to_title(
301 $e, $hold->target) if $hold->hold_type eq 'T';
303 return __metarecord_to_title(
304 $e, $hold->target) if $hold->hold_type eq 'M';
307 sub __copy_to_title {
308 my( $e, $copy ) = @_;
309 syslog('LOG_DEBUG', "copy_to_title(%s)", $copy->id);
310 return $copy->dummy_title if $copy->call_number == -1;
311 my $vol = $e->retrieve_asset_call_number($copy->call_number);
312 return __volume_to_title($e, $vol);
316 sub __volume_to_title {
317 my( $e, $volume ) = @_;
318 syslog('LOG_DEBUG', "volume_to_title(%s)", $volume->id);
319 return __record_to_title($e, $volume->record);
323 sub __record_to_title {
324 my( $e, $title_id ) = @_;
325 syslog('LOG_DEBUG', "record_to_title($title_id)");
326 my $mods = $U->simplereq(
328 'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
329 return ($mods) ? $mods->title : "";
332 sub __metarecord_to_title {
333 my( $e, $m_id ) = @_;
334 syslog('LOG_DEBUG', "metarecord_to_title($m_id)");
335 my $mods = $U->simplereq(
337 'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
338 return ($mods) ? $mods->title : "";
343 # remove the hold on item item_id from my hold queue.
344 # return true if I was holding the item, false otherwise.
347 my ($self, $item_id) = @_;
351 sub __patron_items_info {
353 return if $self->{items_info};
355 OpenILS::Application::Actor::_checked_out(
356 0, $self->{editor}, $self->{user}->id);;
360 my ($self, $start, $end) = @_;
362 $self->__patron_items_info();
363 my @overdues = @{$self->{item_info}->{overdue}};
364 #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
367 for my $circid (@overdues) {
369 syslog('LOG_DEBUG', "overdue_items() fleshing circ $circid");
370 push( @o, __circ_to_title($self->{editor}, $circid) );
374 return (defined $start and defined $end) ?
375 [ $overdues[($start-1)..($end-1)] ] : \@overdues;
378 sub __circ_to_title {
379 my( $e, $circ ) = @_;
381 $circ = $e->retrieve_action_circulation($circ);
382 return __copy_to_title( $e,
383 $e->retrieve_asset_copy($circ->target_copy) );
387 my ($self, $start, $end) = shift;
389 $self->__patron_items_info();
392 @{$self->{item_info}->{out}},
393 @{$self->{item_info}->{overdue}}
396 #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
399 for my $circid (@charges) {
401 syslog('LOG_DEBUG', "charged_items() fleshing circ $circid");
402 push( @c, __circ_to_title($self->{editor}, $circid) );
406 return (defined $start and defined $end) ?
407 [ $charges[($start-1)..($end-1)] ] :
412 my ($self, $start, $end) = @_;
414 return (defined $start and defined $end) ?
415 [ $fines[($start-1)..($end-1)] ] : \@fines;
418 # not currently supported
420 my ($self, $start, $end) = @_;
425 my ($self, $start, $end) = @_;
427 return (defined $start and defined $end) ?
428 [ $holds[($start-1)..($end-1)] ] : \@holds;
432 my ($self, $card_retained, $blocked_card_msg) = @_;
434 my $u = $self->{user};
435 my $e = $self->{editor} = OpenILS::SIP->reset_editor();
437 syslog('LOG_INFO', "Blocking user %s", $u->card->barcode );
439 return $self if $u->card->active eq 'f';
441 $u->card->active('f');
442 if( ! $e->update_actor_card($u->card) ) {
443 syslog('LOG_ERR', "Block card update failed: %s", $e->event->{textcode});
448 # retrieve the un-fleshed user object for update
449 $u = $e->retrieve_actor_user($u->id);
450 my $note = $u->alert_message || "";
451 $note = "CARD BLOCKED BY SELF-CHECK MACHINE\n$note"; # XXX Config option
453 $u->alert_message($note);
455 if( ! $e->update_actor_user($u) ) {
456 syslog('LOG_ERR', "Block: patron alert update failed: %s", $e->event->{textcode});
462 $self->{user}->alert_message( $note );
464 $e->commit; # commits and resets
465 $self->{editor} = OpenILS::SIP->reset_editor();
469 # Testing purposes only
472 # Un-mark card as inactive, grep out the patron alert
473 $self->{screen_msg} = "All privileges restored.";
482 return "Please contact library staff";
486 return "Please contact library staff";
489 sub inet_privileges {
491 my $e = OpenILS::SIP->editor();
492 $INET_PRIVS = $e->retrieve_all_config_net_access_level() unless $INET_PRIVS;
493 my ($level) = grep { $_->id eq $self->{user}->net_access_level } @$INET_PRIVS;