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 my $lib = $self->{editor}->retrieve_actor_org_unit($self->{user}->home_ou)->shortname;
102 syslog('LOG_DEBUG', "OILS: Patron home library is $lib");
108 return "" unless $addr;
109 return $addr->street1 .' '.
120 my $u = $self->{user};
121 my $addr = $u->billing_address;
122 $addr = $u->mailing_address unless $addr;
123 my $str = __addr_string($addr);
124 syslog('LOG_DEBUG', "OILS: Patron address: $str");
130 return $self->{user}->email;
135 return $self->{user}->day_phone;
140 my $dob = OpenILS::SIP->format_date($self->{user}->dob);
141 syslog('LOG_DEBUG', "OILS: Patron DOB = $dob");
147 return $self->{user}->profile->name;
152 return '000'; # Unspecified
155 # How much more detail do we need to check here?
158 my $u = $self->{user};
159 return (($u->barred eq 'f') and ($u->card->active eq 't'));
162 # How much more detail do we need to check here?
165 return $self->charge_ok;
175 return $self->charge_ok;
178 # return true if the card provided is marked as lost
181 return $self->{user}->card->active eq 'f';
191 my ($self, $pwd) = @_;
192 syslog('LOG_DEBUG', 'OILS: Patron->check_password()');
193 return md5_hex($pwd) eq $self->{user}->passwd;
199 syslog('LOG_DEBUG', 'OILS: Patron->currency()');
205 syslog('LOG_DEBUG', 'OILS: Patron->fee_amount()');
207 my $ses = $U->start_db_session();
208 my $summary = $ses->request(
209 'open-ils.storage.money.open_user_summary.search', $self->{user}->id )->gather(1);
210 $U->rollback_db_session($ses);
212 my $total = $summary->balance_owed;
213 syslog('LOG_INFO', "User ".$self->{id} .':'.$self->{user}->id." has a fee amount of \$$total");
219 my $u = $self->{user};
220 return 'barred' if $u->barred eq 't';
224 return $b if $u->active eq 'f';
225 return $b if $u->card->active eq 'f';
227 if( $u->standing_penalties ) {
229 grep { $_->penalty_type eq 'PATRON_EXCEEDS_OVERDUE_COUNT' }
230 @{$u->standing_penalties};
233 grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' }
234 @{$u->standing_penalties};
237 my $expire = DateTime::Format::ISO8601->new->parse_datetime(
238 clense_ISO8601($u->expire_date));
240 return $b if CORE::time > $expire->epoch;
250 sub too_many_charged {
255 sub too_many_overdue {
257 if( $self->{user}->standing_penalties ) {
258 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_OVERDUE_COUNT' }
259 @{$self->{user}->standing_penalties};
264 # not completely sure what this means
265 sub too_many_renewal {
270 # not relevant, handled by fines/fees
271 sub too_many_claim_return {
276 # not relevant, handled by fines/fees
282 sub excessive_fines {
284 syslog('LOG_DEBUG', 'OILS: Patron->excessive_fines()');
285 if( $self->{user}->standing_penalties ) {
286 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' }
287 @{$self->{user}->standing_penalties};
293 # Until someone suggests otherwise, fees and fines are the same
297 syslog('LOG_DEBUG', 'OILS: Patron->excessive_fees()');
298 if( $self->{user}->standing_penalties ) {
299 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' }
300 @{$self->{user}->standing_penalties};
305 # not relevant, handled by fines/fees
306 sub too_many_billed {
314 # List of outstanding holds placed
317 my ($self, $start, $end) = @_;
319 my $holds = $self->{editor}->search_action_hold_request(
320 { usr => $self->{user}->id, fulfillment_time => undef, cancel_time => undef }
324 push( @holds, $self->__hold_to_title($_) ) for @$holds;
326 return (defined $start and defined $end) ?
327 [ $holds[($start-1)..($end-1)] ] :
331 sub __hold_to_title {
334 my $e = $self->{editor};
336 my( $id, $mods, $title, $volume, $copy );
338 return __copy_to_title($e,
339 $e->retrieve_asset_copy($hold->target))
340 if $hold->hold_type eq 'C';
342 return __volume_to_title($e,
343 $e->retrieve_asset_call_number($hold->target))
344 if $hold->hold_type eq 'V';
346 return __record_to_title(
347 $e, $hold->target) if $hold->hold_type eq 'T';
349 return __metarecord_to_title(
350 $e, $hold->target) if $hold->hold_type eq 'M';
353 sub __copy_to_title {
354 my( $e, $copy ) = @_;
355 #syslog('LOG_DEBUG', "OILS: copy_to_title(%s)", $copy->id);
356 return $copy->dummy_title if $copy->call_number == -1;
358 my $vol = (ref $copy->call_number) ?
360 $e->retrieve_asset_call_number($copy->call_number);
362 return __volume_to_title($e, $vol);
366 sub __volume_to_title {
367 my( $e, $volume ) = @_;
368 #syslog('LOG_DEBUG', "OILS: volume_to_title(%s)", $volume->id);
369 return __record_to_title($e, $volume->record);
373 sub __record_to_title {
374 my( $e, $title_id ) = @_;
375 #syslog('LOG_DEBUG', "OILS: record_to_title($title_id)");
376 my $mods = $U->simplereq(
378 'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
379 return ($mods) ? $mods->title : "";
382 sub __metarecord_to_title {
383 my( $e, $m_id ) = @_;
384 #syslog('LOG_DEBUG', "OILS: metarecord_to_title($m_id)");
385 my $mods = $U->simplereq(
387 'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
388 return ($U->event_code($mods)) ? "<unknown>" : $mods->title;
393 # remove the hold on item item_id from my hold queue.
394 # return true if I was holding the item, false otherwise.
397 my ($self, $item_id) = @_;
401 sub __patron_items_info {
403 return if $self->{items_info};
405 OpenILS::Application::Actor::_checked_out(
406 0, $self->{editor}, $self->{user}->id);;
410 my ($self, $start, $end) = @_;
412 $self->__patron_items_info();
413 my @overdues = @{$self->{item_info}->{overdue}};
414 #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
417 syslog('LOG_DEBUG', "OILS: overdue_items() fleshing circs @overdues");
418 for my $circid (@overdues) {
420 push( @o, __circ_to_title($self->{editor}, $circid) );
424 return (defined $start and defined $end) ?
425 [ $overdues[($start-1)..($end-1)] ] : \@overdues;
428 sub __circ_to_title {
429 my( $e, $circ ) = @_;
431 $circ = $e->retrieve_action_circulation($circ);
432 return __copy_to_title( $e,
433 $e->retrieve_asset_copy($circ->target_copy) );
437 my ($self, $start, $end) = shift;
439 $self->__patron_items_info();
442 @{$self->{item_info}->{out}},
443 @{$self->{item_info}->{overdue}}
446 #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
449 syslog('LOG_DEBUG', "OILS: charged_items() fleshing circs @charges");
450 for my $circid (@charges) {
452 push( @c, __circ_to_title($self->{editor}, $circid) );
456 return (defined $start and defined $end) ?
457 [ $charges[($start-1)..($end-1)] ] :
462 my ($self, $start, $end) = @_;
464 syslog('LOG_DEBUG', 'OILS: Patron->fine_items()');
465 return (defined $start and defined $end) ?
466 [ $fines[($start-1)..($end-1)] ] : \@fines;
469 # not currently supported
471 my ($self, $start, $end) = @_;
476 my ($self, $start, $end) = @_;
478 return (defined $start and defined $end) ?
479 [ $holds[($start-1)..($end-1)] ] : \@holds;
483 my ($self, $card_retained, $blocked_card_msg) = @_;
485 my $u = $self->{user};
486 my $e = $self->{editor} = OpenILS::SIP->reset_editor();
488 syslog('LOG_INFO', "OILS: Blocking user %s", $u->card->barcode );
490 return $self if $u->card->active eq 'f';
492 $u->card->active('f');
493 if( ! $e->update_actor_card($u->card) ) {
494 syslog('LOG_ERR', "OILS: Block card update failed: %s", $e->event->{textcode});
499 # retrieve the un-fleshed user object for update
500 $u = $e->retrieve_actor_user($u->id);
501 my $note = $u->alert_message || "";
502 $note = "CARD BLOCKED BY SELF-CHECK MACHINE\n$note"; # XXX Config option
504 $u->alert_message($note);
506 if( ! $e->update_actor_user($u) ) {
507 syslog('LOG_ERR', "OILS: Block: patron alert update failed: %s", $e->event->{textcode});
513 $self->{user}->alert_message( $note );
515 $e->commit; # commits and resets
516 $self->{editor} = OpenILS::SIP->reset_editor();
520 # Testing purposes only
523 # Un-mark card as inactive, grep out the patron alert
524 $self->{screen_msg} = "All privileges restored.";
533 return "Please contact library staff";
537 return "Please contact library staff";
540 sub inet_privileges {
542 my $e = OpenILS::SIP->editor();
543 $INET_PRIVS = $e->retrieve_all_config_net_access_level() unless $INET_PRIVS;
544 my ($level) = grep { $_->id eq $self->{user}->net_access_level } @$INET_PRIVS;
545 my $name = $level->name;
546 syslog('LOG_DEBUG', "OILS: Patron inet_privs = $name");