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);
38 my ($class, $patron_id) = @_;
39 my $type = ref($class) || $class;
42 syslog("LOG_DEBUG", "OILS: new OpenILS Patron(%s): searching...", $patron_id);
44 my $e = OpenILS::SIP->editor();
46 my $c = $e->search_actor_card({barcode => $patron_id}, {idlist=>1});
51 $user = $e->search_actor_user(
67 ausp => ['standing_penalty']
73 $user = (@$user) ? $$user[0] : undef;
77 syslog("LOG_WARNING", "OILS: Unable to find patron %s", $patron_id);
81 $self->{user} = $user;
82 $self->{id} = $patron_id;
85 syslog("LOG_DEBUG", "OILS: new OpenILS Patron(%s): found patron : barred=%s, card:active=%s",
86 $patron_id, $self->{user}->barred, $self->{user}->card->active );
100 my $u = $self->{user};
101 return OpenILS::SIP::clean_text($u->first_given_name . ' ' .
102 $u->second_given_name . ' ' . $u->family_name);
107 my $lib = $self->{editor}->retrieve_actor_org_unit($self->{user}->home_ou)->shortname;
108 syslog('LOG_DEBUG', "OILS: Patron->home_library() = $lib");
114 return "" unless $addr;
115 return OpenILS::SIP::clean_text($addr->street1 .' '.
126 my $u = $self->{user};
127 my $str = __addr_string($u->billing_address || $u->mailing_address);
128 syslog('LOG_DEBUG', "OILS: Patron address: $str");
134 return OpenILS::SIP::clean_text($self->{user}->email);
139 return $self->{user}->day_phone;
144 my $dob = OpenILS::SIP->format_date($self->{user}->dob);
145 syslog('LOG_DEBUG', "OILS: Patron DOB = $dob");
151 return OpenILS::SIP::clean_text($self->{user}->profile->name);
156 return '000'; # Unspecified
159 # How much more detail do we need to check here?
162 my $u = $self->{user};
163 return (($u->barred eq 'f') and ($u->card->active eq 't'));
166 # How much more detail do we need to check here?
169 return $self->charge_ok;
179 return $self->charge_ok;
182 # return true if the card provided is marked as lost
185 return $self->{user}->card->active eq 'f';
195 my ($self, $pwd) = @_;
196 syslog('LOG_DEBUG', 'OILS: Patron->check_password()');
197 return md5_hex($pwd) eq $self->{user}->passwd;
203 syslog('LOG_DEBUG', 'OILS: Patron->currency()');
209 syslog('LOG_DEBUG', 'OILS: Patron->fee_amount()');
211 my $ses = $U->start_db_session();
212 my $summary = $ses->request(
213 'open-ils.storage.money.open_user_summary.search', $self->{user}->id )->gather(1);
214 $U->rollback_db_session($ses);
216 my $total = $summary->balance_owed;
217 syslog('LOG_INFO', "User ".$self->{id} .':'.$self->{user}->id." has a fee amount of \$$total");
223 my $u = $self->{user};
224 return 'barred' if $u->barred eq 't';
228 return $b if $u->active eq 'f';
229 return $b if $u->card->active eq 'f';
231 if( $u->standing_penalties ) {
233 grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_OVERDUE_COUNT' }
234 @{$u->standing_penalties};
237 grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_FINES' }
238 @{$u->standing_penalties};
241 my $expire = DateTime::Format::ISO8601->new->parse_datetime(
242 cleanse_ISO8601($u->expire_date));
244 return $b if CORE::time > $expire->epoch;
254 sub too_many_charged {
259 sub too_many_overdue {
261 if( $self->{user}->standing_penalties ) {
262 return grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_OVERDUE_COUNT' }
263 @{$self->{user}->standing_penalties};
268 # not completely sure what this means
269 sub too_many_renewal {
274 # not relevant, handled by fines/fees
275 sub too_many_claim_return {
280 # not relevant, handled by fines/fees
286 sub excessive_fines {
288 syslog('LOG_DEBUG', 'OILS: Patron->excessive_fines()');
289 if( $self->{user}->standing_penalties ) {
290 return grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_FINES' }
291 @{$self->{user}->standing_penalties};
297 # Until someone suggests otherwise, fees and fines are the same
301 syslog('LOG_DEBUG', 'OILS: Patron->excessive_fees()');
302 if( $self->{user}->standing_penalties ) {
303 return grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_FINES' }
304 @{$self->{user}->standing_penalties};
309 # not relevant, handled by fines/fees
310 sub too_many_billed {
318 # List of outstanding holds placed
321 my ($self, $start, $end) = @_;
322 syslog('LOG_DEBUG', 'OILS: Patron->hold_items()');
324 my $holds = $self->{editor}->search_action_hold_request(
325 { usr => $self->{user}->id, fulfillment_time => undef, cancel_time => undef }
329 push( @holds, OpenILS::SIP::clean_text($self->__hold_to_title($_)) ) for @$holds;
331 return (defined $start and defined $end) ?
332 [ $holds[($start-1)..($end-1)] ] :
336 sub __hold_to_title {
339 my $e = $self->{editor};
341 my( $id, $mods, $title, $volume, $copy );
343 return __copy_to_title($e,
344 $e->retrieve_asset_copy($hold->target))
345 if $hold->hold_type eq 'C';
347 return __volume_to_title($e,
348 $e->retrieve_asset_call_number($hold->target))
349 if $hold->hold_type eq 'V';
351 return __record_to_title(
352 $e, $hold->target) if $hold->hold_type eq 'T';
354 return __metarecord_to_title(
355 $e, $hold->target) if $hold->hold_type eq 'M';
358 sub __copy_to_title {
359 my( $e, $copy ) = @_;
360 #syslog('LOG_DEBUG', "OILS: copy_to_title(%s)", $copy->id);
361 return $copy->dummy_title if $copy->call_number == -1;
363 my $vol = (ref $copy->call_number) ?
365 $e->retrieve_asset_call_number($copy->call_number);
367 return __volume_to_title($e, $vol);
371 sub __volume_to_title {
372 my( $e, $volume ) = @_;
373 #syslog('LOG_DEBUG', "OILS: volume_to_title(%s)", $volume->id);
374 return __record_to_title($e, $volume->record);
378 sub __record_to_title {
379 my( $e, $title_id ) = @_;
380 #syslog('LOG_DEBUG', "OILS: record_to_title($title_id)");
381 my $mods = $U->simplereq(
383 'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
384 return ($mods) ? $mods->title : "";
387 sub __metarecord_to_title {
388 my( $e, $m_id ) = @_;
389 #syslog('LOG_DEBUG', "OILS: metarecord_to_title($m_id)");
390 my $mods = $U->simplereq(
392 'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
393 return ($U->event_code($mods)) ? "<unknown>" : $mods->title;
398 # remove the hold on item item_id from my hold queue.
399 # return true if I was holding the item, false otherwise.
402 my ($self, $item_id) = @_;
406 sub __patron_items_info {
408 return if $self->{item_info};
410 OpenILS::Application::Actor::_checked_out(
411 0, $self->{editor}, $self->{user}->id);;
415 my ($self, $start, $end) = @_;
417 $self->__patron_items_info();
418 my @overdues = @{$self->{item_info}->{overdue}};
419 #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
422 syslog('LOG_DEBUG', "OILS: overdue_items() fleshing circs @overdues");
425 my @return_datatype = grep { $_->{name} eq 'msg64_summary_datatype' } OpenILS::SIP::config()->{implementation_config}->{options}->{option};
427 for my $circid (@overdues) {
429 if(@return_datatype and $return_datatype[0]->{value} eq 'barcode') {
430 push( @o, __circ_to_barcode($self->{editor}, $circid));
432 push( @o, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
437 return (defined $start and defined $end) ?
438 [ $overdues[($start-1)..($end-1)] ] : \@overdues;
441 sub __circ_to_barcode {
444 $circ = $e->retrieve_action_circulation($circ);
445 my $copy = $e->retrieve_asset_copy($circ->target_copy);
446 return $copy->barcode;
449 sub __circ_to_title {
450 my( $e, $circ ) = @_;
452 $circ = $e->retrieve_action_circulation($circ);
453 return __copy_to_title( $e,
454 $e->retrieve_asset_copy($circ->target_copy) );
458 my ($self, $start, $end) = shift;
460 $self->__patron_items_info();
463 @{$self->{item_info}->{out}},
464 @{$self->{item_info}->{overdue}}
467 #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
470 syslog('LOG_DEBUG', "OILS: charged_items() fleshing circs @charges");
472 my @return_datatype = grep { $_->{name} eq 'msg64_summary_datatype' } OpenILS::SIP::config()->{implementation_config}->{options}->{option};
474 for my $circid (@charges) {
476 if(@return_datatype and $return_datatype[0]->{value} eq 'barcode') {
477 push( @c, __circ_to_barcode($self->{editor}, $circid));
479 push( @c, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
485 return (defined $start and defined $end) ?
486 [ $charges[($start-1)..($end-1)] ] :
491 my ($self, $start, $end) = @_;
493 syslog('LOG_DEBUG', 'OILS: Patron->fine_items()');
494 return (defined $start and defined $end) ?
495 [ $fines[($start-1)..($end-1)] ] : \@fines;
498 # not currently supported
500 my ($self, $start, $end) = @_;
505 my ($self, $start, $end) = @_;
507 syslog('LOG_DEBUG', 'OILS: Patron->unavail_holds()');
508 return (defined $start and defined $end) ?
509 [ $holds[($start-1)..($end-1)] ] : \@holds;
513 my ($self, $card_retained, $blocked_card_msg) = @_;
515 my $e = $self->{editor};
516 my $u = $self->{user};
518 syslog('LOG_INFO', "OILS: Blocking user %s", $u->card->barcode );
520 return $self if $u->card->active eq 'f';
522 # connect and start a new transaction
525 $u->card->active('f');
526 if( ! $e->update_actor_card($u->card) ) {
527 syslog('LOG_ERR', "OILS: Block card update failed: %s", $e->event->{textcode});
528 $e->rollback; # rollback + disconnect
532 # retrieve the un-fleshed user object for update
533 $u = $e->retrieve_actor_user($u->id);
534 my $note = OpenILS::SIP::clean_text($u->alert_message) || "";
535 $note = "CARD BLOCKED BY SELF-CHECK MACHINE\n$note"; # XXX Config option
537 $u->alert_message($note);
539 if( ! $e->update_actor_user($u) ) {
540 syslog('LOG_ERR', "OILS: Block: patron alert update failed: %s", $e->event->{textcode});
541 $e->rollback; # rollback + disconnect
546 $self->{user}->alert_message( $note );
548 $e->commit; # commits and disconnects
552 # Testing purposes only
555 # Un-mark card as inactive, grep out the patron alert
556 $self->{screen_msg} = "All privileges restored.";
565 return "Please contact library staff";
569 return "Please contact library staff";
572 sub inet_privileges {
574 my $e = OpenILS::SIP->editor();
575 $INET_PRIVS = $e->retrieve_all_config_net_access_level() unless $INET_PRIVS;
576 my ($level) = grep { $_->id eq $self->{user}->net_access_level } @$INET_PRIVS;
577 my $name = OpenILS::SIP::clean_text($level->name);
578 syslog('LOG_DEBUG', "OILS: Patron inet_privs = $name");