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(
62 ausp => ['standing_penalty']
68 $user = (@$user) ? $$user[0] : undef;
72 syslog("LOG_WARNING", "OILS: Unable to find patron %s", $patron_id);
76 $self->{user} = $user;
77 $self->{id} = $patron_id;
80 syslog("LOG_DEBUG", "OILS: new OpenILS Patron(%s): found patron : barred=%s, card:active=%s",
81 $patron_id, $self->{user}->barred, $self->{user}->card->active );
95 my $u = $self->{user};
96 return OpenILS::SIP::clean_text($u->first_given_name . ' ' .
97 $u->second_given_name . ' ' . $u->family_name);
102 my $lib = $self->{editor}->retrieve_actor_org_unit($self->{user}->home_ou)->shortname;
103 syslog('LOG_DEBUG', "OILS: Patron->home_library() = $lib");
109 return "" unless $addr;
110 return OpenILS::SIP::clean_text($addr->street1 .' '.
121 my $u = $self->{user};
122 my $addr = $u->billing_address;
123 $addr = $u->mailing_address unless $addr;
124 my $str = __addr_string($addr);
125 syslog('LOG_DEBUG', "OILS: Patron address: $str");
131 return OpenILS::SIP::clean_text($self->{user}->email);
136 return $self->{user}->day_phone;
141 my $dob = OpenILS::SIP->format_date($self->{user}->dob);
142 syslog('LOG_DEBUG', "OILS: Patron DOB = $dob");
148 return OpenILS::SIP::clean_text($self->{user}->profile->name);
153 return '000'; # Unspecified
156 # How much more detail do we need to check here?
159 my $u = $self->{user};
160 return (($u->barred eq 'f') and ($u->card->active eq 't'));
163 # How much more detail do we need to check here?
166 return $self->charge_ok;
176 return $self->charge_ok;
179 # return true if the card provided is marked as lost
182 return $self->{user}->card->active eq 'f';
192 my ($self, $pwd) = @_;
193 syslog('LOG_DEBUG', 'OILS: Patron->check_password()');
194 return md5_hex($pwd) eq $self->{user}->passwd;
200 syslog('LOG_DEBUG', 'OILS: Patron->currency()');
206 syslog('LOG_DEBUG', 'OILS: Patron->fee_amount()');
208 my $ses = $U->start_db_session();
209 my $summary = $ses->request(
210 'open-ils.storage.money.open_user_summary.search', $self->{user}->id )->gather(1);
211 $U->rollback_db_session($ses);
213 my $total = $summary->balance_owed;
214 syslog('LOG_INFO', "User ".$self->{id} .':'.$self->{user}->id." has a fee amount of \$$total");
220 my $u = $self->{user};
221 return 'barred' if $u->barred eq 't';
225 return $b if $u->active eq 'f';
226 return $b if $u->card->active eq 'f';
228 if( $u->standing_penalties ) {
230 grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_OVERDUE_COUNT' }
231 @{$u->standing_penalties};
234 grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_FINES' }
235 @{$u->standing_penalties};
238 my $expire = DateTime::Format::ISO8601->new->parse_datetime(
239 cleanse_ISO8601($u->expire_date));
241 return $b if CORE::time > $expire->epoch;
251 sub too_many_charged {
256 sub too_many_overdue {
258 if( $self->{user}->standing_penalties ) {
259 return grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_OVERDUE_COUNT' }
260 @{$self->{user}->standing_penalties};
265 # not completely sure what this means
266 sub too_many_renewal {
271 # not relevant, handled by fines/fees
272 sub too_many_claim_return {
277 # not relevant, handled by fines/fees
283 sub excessive_fines {
285 syslog('LOG_DEBUG', 'OILS: Patron->excessive_fines()');
286 if( $self->{user}->standing_penalties ) {
287 return grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_FINES' }
288 @{$self->{user}->standing_penalties};
294 # Until someone suggests otherwise, fees and fines are the same
298 syslog('LOG_DEBUG', 'OILS: Patron->excessive_fees()');
299 if( $self->{user}->standing_penalties ) {
300 return grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_FINES' }
301 @{$self->{user}->standing_penalties};
306 # not relevant, handled by fines/fees
307 sub too_many_billed {
315 # List of outstanding holds placed
318 my ($self, $start, $end) = @_;
319 syslog('LOG_DEBUG', 'OILS: Patron->hold_items()');
321 my $holds = $self->{editor}->search_action_hold_request(
322 { usr => $self->{user}->id, fulfillment_time => undef, cancel_time => undef }
326 push( @holds, OpenILS::SIP::clean_text($self->__hold_to_title($_)) ) for @$holds;
328 return (defined $start and defined $end) ?
329 [ $holds[($start-1)..($end-1)] ] :
333 sub __hold_to_title {
336 my $e = $self->{editor};
338 my( $id, $mods, $title, $volume, $copy );
340 return __copy_to_title($e,
341 $e->retrieve_asset_copy($hold->target))
342 if $hold->hold_type eq 'C';
344 return __volume_to_title($e,
345 $e->retrieve_asset_call_number($hold->target))
346 if $hold->hold_type eq 'V';
348 return __record_to_title(
349 $e, $hold->target) if $hold->hold_type eq 'T';
351 return __metarecord_to_title(
352 $e, $hold->target) if $hold->hold_type eq 'M';
355 sub __copy_to_title {
356 my( $e, $copy ) = @_;
357 #syslog('LOG_DEBUG', "OILS: copy_to_title(%s)", $copy->id);
358 return $copy->dummy_title if $copy->call_number == -1;
360 my $vol = (ref $copy->call_number) ?
362 $e->retrieve_asset_call_number($copy->call_number);
364 return __volume_to_title($e, $vol);
368 sub __volume_to_title {
369 my( $e, $volume ) = @_;
370 #syslog('LOG_DEBUG', "OILS: volume_to_title(%s)", $volume->id);
371 return __record_to_title($e, $volume->record);
375 sub __record_to_title {
376 my( $e, $title_id ) = @_;
377 #syslog('LOG_DEBUG', "OILS: record_to_title($title_id)");
378 my $mods = $U->simplereq(
380 'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
381 return ($mods) ? $mods->title : "";
384 sub __metarecord_to_title {
385 my( $e, $m_id ) = @_;
386 #syslog('LOG_DEBUG', "OILS: metarecord_to_title($m_id)");
387 my $mods = $U->simplereq(
389 'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
390 return ($U->event_code($mods)) ? "<unknown>" : $mods->title;
395 # remove the hold on item item_id from my hold queue.
396 # return true if I was holding the item, false otherwise.
399 my ($self, $item_id) = @_;
403 sub __patron_items_info {
405 return if $self->{item_info};
407 OpenILS::Application::Actor::_checked_out(
408 0, $self->{editor}, $self->{user}->id);;
412 my ($self, $start, $end) = @_;
414 $self->__patron_items_info();
415 my @overdues = @{$self->{item_info}->{overdue}};
416 #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
419 syslog('LOG_DEBUG', "OILS: overdue_items() fleshing circs @overdues");
422 my @return_datatype = grep { $_->{name} eq 'msg64_summary_datatype' } OpenILS::SIP::config()->{implementation_config}->{options}->{option};
424 for my $circid (@overdues) {
426 if(@return_datatype and $return_datatype[0]->{value} eq 'barcode') {
427 push( @o, __circ_to_barcode($self->{editor}, $circid));
429 push( @o, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
434 return (defined $start and defined $end) ?
435 [ $overdues[($start-1)..($end-1)] ] : \@overdues;
438 sub __circ_to_barcode {
441 $circ = $e->retrieve_action_circulation($circ);
442 my $copy = $e->retrieve_asset_copy($circ->target_copy);
443 return $copy->barcode;
446 sub __circ_to_title {
447 my( $e, $circ ) = @_;
449 $circ = $e->retrieve_action_circulation($circ);
450 return __copy_to_title( $e,
451 $e->retrieve_asset_copy($circ->target_copy) );
455 my ($self, $start, $end) = shift;
457 $self->__patron_items_info();
460 @{$self->{item_info}->{out}},
461 @{$self->{item_info}->{overdue}}
464 #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
467 syslog('LOG_DEBUG', "OILS: charged_items() fleshing circs @charges");
469 my @return_datatype = grep { $_->{name} eq 'msg64_summary_datatype' } OpenILS::SIP::config()->{implementation_config}->{options}->{option};
471 for my $circid (@charges) {
473 if(@return_datatype and $return_datatype[0]->{value} eq 'barcode') {
474 push( @c, __circ_to_barcode($self->{editor}, $circid));
476 push( @c, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
482 return (defined $start and defined $end) ?
483 [ $charges[($start-1)..($end-1)] ] :
488 my ($self, $start, $end) = @_;
490 syslog('LOG_DEBUG', 'OILS: Patron->fine_items()');
491 return (defined $start and defined $end) ?
492 [ $fines[($start-1)..($end-1)] ] : \@fines;
495 # not currently supported
497 my ($self, $start, $end) = @_;
502 my ($self, $start, $end) = @_;
504 syslog('LOG_DEBUG', 'OILS: Patron->unavail_holds()');
505 return (defined $start and defined $end) ?
506 [ $holds[($start-1)..($end-1)] ] : \@holds;
510 my ($self, $card_retained, $blocked_card_msg) = @_;
512 my $e = $self->{editor};
513 my $u = $self->{user};
515 syslog('LOG_INFO', "OILS: Blocking user %s", $u->card->barcode );
517 return $self if $u->card->active eq 'f';
519 # connect and start a new transaction
522 $u->card->active('f');
523 if( ! $e->update_actor_card($u->card) ) {
524 syslog('LOG_ERR', "OILS: Block card update failed: %s", $e->event->{textcode});
525 $e->rollback; # rollback + disconnect
529 # retrieve the un-fleshed user object for update
530 $u = $e->retrieve_actor_user($u->id);
531 my $note = OpenILS::SIP::clean_text($u->alert_message) || "";
532 $note = "CARD BLOCKED BY SELF-CHECK MACHINE\n$note"; # XXX Config option
534 $u->alert_message($note);
536 if( ! $e->update_actor_user($u) ) {
537 syslog('LOG_ERR', "OILS: Block: patron alert update failed: %s", $e->event->{textcode});
538 $e->rollback; # rollback + disconnect
543 $self->{user}->alert_message( $note );
545 $e->commit; # commits and disconnects
549 # Testing purposes only
552 # Un-mark card as inactive, grep out the patron alert
553 $self->{screen_msg} = "All privileges restored.";
562 return "Please contact library staff";
566 return "Please contact library staff";
569 sub inet_privileges {
571 my $e = OpenILS::SIP->editor();
572 $INET_PRIVS = $e->retrieve_all_config_net_access_level() unless $INET_PRIVS;
573 my ($level) = grep { $_->id eq $self->{user}->net_access_level } @$INET_PRIVS;
574 my $name = OpenILS::SIP::clean_text($level->name);
575 syslog('LOG_DEBUG', "OILS: Patron inet_privs = $name");