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);
29 my ($class, $patron_id) = @_;
30 my $type = ref($class) || $class;
33 syslog("LOG_DEBUG", "new OpenILS Patron(%s): searching...", $patron_id);
35 my $e = OpenILS::SIP->editor();
37 my $c = $e->search_actor_card({barcode => $patron_id}, {idlist=>1});
42 $user = $e->search_actor_user(
63 $user = (@$user) ? $$user[0] : undef;
67 syslog("LOG_WARNING", "Unable to find patron %s", $patron_id);
71 $self->{user} = $user;
72 $self->{id} = $patron_id;
75 syslog("LOG_DEBUG", "new OpenILS Patron(%s): found patron : barred=%s, card:active=%s",
76 $patron_id, $self->{user}->barred, $self->{user}->card->active );
89 my $u = $self->{user};
90 return $u->first_given_name . ' ' .
91 $u->second_given_name . ' ' . $u->family_name;
96 return "" unless $addr;
97 return $addr->street1 .' '.
108 my $u = $self->{user};
109 my $addr = $u->billing_address;
110 my $str = __addr_string($addr);
111 my $maddr = $u->mailing_address;
112 $str .= "\n" . __addr_string($maddr)
113 if $maddr and $maddr->id ne $addr->id;
119 return $self->{user}->email;
124 return $self->{user}->day_phone;
129 return $self->{user}->dob;
134 return $self->{user}->profile->name;
139 return '000'; # Unspecified
142 # How much more detail do we need to check here?
145 my $u = $self->{user};
146 return (($u->barred eq 'f') and ($u->card->active eq 't'));
149 # How much more detail do we need to check here?
152 return $self->charge_ok;
165 # return true if the card provided is marked as lost
178 my ($self, $pwd) = @_;
179 return md5_hex($pwd) eq $self->{user}->passwd;
203 sub too_many_charged {
208 sub too_many_overdue {
210 if( $self->{user}->standing_penalties ) {
211 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_OVERDUE_COUNT' }
212 @{$self->{user}->standing_penalties};
217 # not completely sure what this means
218 sub too_many_renewal {
223 # not relevant, handled by fines/fees
224 sub too_many_claim_return {
229 # not relevant, handled by fines/fees
235 sub excessive_fines {
237 if( $self->{user}->standing_penalties ) {
238 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' }
239 @{$self->{user}->standing_penalties};
245 # Until someone suggests otherwise, fees and fines are the same
249 if( $self->{user}->standing_penalties ) {
250 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' }
251 @{$self->{user}->standing_penalties};
256 # not relevant, handled by fines/fees
257 sub too_many_billed {
265 # List of outstanding holds placed
268 my ($self, $start, $end) = @_;
270 my $holds = $self->{editor}->search_action_hold_request(
271 { usr => $self->{user}->id, fulfillment_time => undef }
275 push( @holds, $self->__hold_to_title($_) ) for @$holds;
277 return (defined $start and defined $end) ?
278 [ $holds[($start-1)..($end-1)] ] :
282 sub __hold_to_title {
285 my $e = $self->{editor};
287 my( $id, $mods, $title, $volume, $copy );
289 return __copy_to_title($e,
290 $e->retrieve_asset_copy($hold->target))
291 if $hold->hold_type eq 'C';
293 return __volume_to_title($e,
294 $e->retrieve_asset_call_number($hold->target))
295 if $hold->hold_type eq 'V';
297 return __record_to_title(
298 $e, $hold->target) if $hold->hold_type eq 'T';
300 return __metarecord_to_title(
301 $e, $hold->target) if $hold->hold_type eq 'M';
304 sub __copy_to_title {
305 my( $e, $copy ) = @_;
306 syslog('LOG_DEBUG', "copy_to_title(%s)", $copy->id);
307 return $copy->dummy_title if $copy->call_number == -1;
308 my $vol = $e->retrieve_asset_call_number($copy->call_number);
309 return __volume_to_title($e, $vol);
313 sub __volume_to_title {
314 my( $e, $volume ) = @_;
315 syslog('LOG_DEBUG', "volume_to_title(%s)", $volume->id);
316 return __record_to_title($e, $volume->record);
320 sub __record_to_title {
321 my( $e, $title_id ) = @_;
322 syslog('LOG_DEBUG', "record_to_title($title_id)");
323 my $mods = $U->simplereq(
325 'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
326 return ($mods) ? $mods->title : "";
329 sub __metarecord_to_title {
330 my( $e, $m_id ) = @_;
331 syslog('LOG_DEBUG', "metarecord_to_title($m_id)");
332 my $mods = $U->simplereq(
334 'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
335 return ($mods) ? $mods->title : "";
340 # remove the hold on item item_id from my hold queue.
341 # return true if I was holding the item, false otherwise.
344 my ($self, $item_id) = @_;
348 sub __patron_items_info {
350 return if $self->{items_info};
352 OpenILS::Application::Actor::_checked_out(
353 0, $self->{editor}, $self->{user}->id);;
357 my ($self, $start, $end) = @_;
359 $self->__patron_items_info();
360 my @overdues = @{$self->{item_info}->{overdue}};
361 #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
364 for my $circid (@overdues) {
366 syslog('LOG_DEBUG', "overdue_items() fleshing circ $circid");
367 push( @o, __circ_to_title($self->{editor}, $circid) );
371 return (defined $start and defined $end) ?
372 [ $overdues[($start-1)..($end-1)] ] : \@overdues;
375 sub __circ_to_title {
376 my( $e, $circ ) = @_;
378 $circ = $e->retrieve_action_circulation($circ);
379 return __copy_to_title( $e,
380 $e->retrieve_asset_copy($circ->target_copy) );
384 my ($self, $start, $end) = shift;
386 $self->__patron_items_info();
389 @{$self->{item_info}->{out}},
390 @{$self->{item_info}->{overdue}}
393 #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
396 for my $circid (@charges) {
398 syslog('LOG_DEBUG', "charged_items() fleshing circ $circid");
399 push( @c, __circ_to_title($self->{editor}, $circid) );
403 return (defined $start and defined $end) ?
404 [ $charges[($start-1)..($end-1)] ] :
409 my ($self, $start, $end) = @_;
411 return (defined $start and defined $end) ?
412 [ $fines[($start-1)..($end-1)] ] : \@fines;
415 # not currently supported
417 my ($self, $start, $end) = @_;
422 my ($self, $start, $end) = @_;
424 return (defined $start and defined $end) ?
425 [ $holds[($start-1)..($end-1)] ] : \@holds;
429 my ($self, $card_retained, $blocked_card_msg) = @_;
431 my $u = $self->{user};
432 my $e = $self->{editor} = OpenILS::SIP->reset_editor();
434 syslog('LOG_INFO', "Blocking user %s", $u->card->barcode );
436 return $self if $u->card->active eq 'f';
438 $u->card->active('f');
439 if( ! $e->update_actor_card($u->card) ) {
440 syslog('LOG_ERR', "Block card update failed: %s", $e->event->{textcode});
445 # retrieve the un-fleshed user object for update
446 $u = $e->retrieve_actor_user($u->id);
447 my $note = $u->alert_message || "";
448 $note = "CARD BLOCKED BY SELF-CHECK MACHINE\n$note"; # XXX Config option
450 $u->alert_message($note);
452 if( ! $e->update_actor_user($u) ) {
453 syslog('LOG_ERR', "Block: patron alert update failed: %s", $e->event->{textcode});
459 $self->{user}->alert_message( $note );
461 $e->commit; # commits and resets
462 $self->{editor} = OpenILS::SIP->reset_editor();
466 # Testing purposes only
469 # Un-mark card as inactive, grep out the patron alert
470 $self->{screen_msg} = "All privileges restored.";
479 return "Please contact library staff";
483 return "Please contact library staff";