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 return "" unless $addr;
102 return $addr->street1 .' '.
113 my $u = $self->{user};
114 my $addr = $u->billing_address;
115 $addr = $u->mailing_address unless $addr;
116 my $str = __addr_string($addr);
117 syslog('LOG_DEBUG', "OILS: Patron address: $str");
123 return $self->{user}->email;
128 return $self->{user}->day_phone;
133 my $dob = OpenILS::SIP->format_date($self->{user}->dob);
134 syslog('LOG_DEBUG', "OILS: Patron DOB = $dob");
140 return $self->{user}->profile->name;
145 return '000'; # Unspecified
148 # How much more detail do we need to check here?
151 my $u = $self->{user};
152 return (($u->barred eq 'f') and ($u->card->active eq 't'));
155 # How much more detail do we need to check here?
158 return $self->charge_ok;
168 return $self->charge_ok;
171 # return true if the card provided is marked as lost
174 return $self->{user}->card->active eq 'f';
184 my ($self, $pwd) = @_;
185 return md5_hex($pwd) eq $self->{user}->passwd;
198 my $e = OpenILS::SIP->editor();
199 my $xacts = $e->search_money_open_billable_transaction_summary(
200 { usr => $self->{user}->id, balance_owed => { '!=' => 0 } } );
202 $total += $_->balance_owed for @$xacts;
216 sub too_many_charged {
221 sub too_many_overdue {
223 if( $self->{user}->standing_penalties ) {
224 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_OVERDUE_COUNT' }
225 @{$self->{user}->standing_penalties};
230 # not completely sure what this means
231 sub too_many_renewal {
236 # not relevant, handled by fines/fees
237 sub too_many_claim_return {
242 # not relevant, handled by fines/fees
248 sub excessive_fines {
250 if( $self->{user}->standing_penalties ) {
251 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' }
252 @{$self->{user}->standing_penalties};
258 # Until someone suggests otherwise, fees and fines are the same
262 if( $self->{user}->standing_penalties ) {
263 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' }
264 @{$self->{user}->standing_penalties};
269 # not relevant, handled by fines/fees
270 sub too_many_billed {
278 # List of outstanding holds placed
281 my ($self, $start, $end) = @_;
283 my $holds = $self->{editor}->search_action_hold_request(
284 { usr => $self->{user}->id, fulfillment_time => undef }
288 push( @holds, $self->__hold_to_title($_) ) for @$holds;
290 return (defined $start and defined $end) ?
291 [ $holds[($start-1)..($end-1)] ] :
295 sub __hold_to_title {
298 my $e = $self->{editor};
300 my( $id, $mods, $title, $volume, $copy );
302 return __copy_to_title($e,
303 $e->retrieve_asset_copy($hold->target))
304 if $hold->hold_type eq 'C';
306 return __volume_to_title($e,
307 $e->retrieve_asset_call_number($hold->target))
308 if $hold->hold_type eq 'V';
310 return __record_to_title(
311 $e, $hold->target) if $hold->hold_type eq 'T';
313 return __metarecord_to_title(
314 $e, $hold->target) if $hold->hold_type eq 'M';
317 sub __copy_to_title {
318 my( $e, $copy ) = @_;
319 #syslog('LOG_DEBUG', "OILS: copy_to_title(%s)", $copy->id);
320 return $copy->dummy_title if $copy->call_number == -1;
321 my $vol = $e->retrieve_asset_call_number($copy->call_number);
322 return __volume_to_title($e, $vol);
326 sub __volume_to_title {
327 my( $e, $volume ) = @_;
328 #syslog('LOG_DEBUG', "OILS: volume_to_title(%s)", $volume->id);
329 return __record_to_title($e, $volume->record);
333 sub __record_to_title {
334 my( $e, $title_id ) = @_;
335 #syslog('LOG_DEBUG', "OILS: record_to_title($title_id)");
336 my $mods = $U->simplereq(
338 'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
339 return ($mods) ? $mods->title : "";
342 sub __metarecord_to_title {
343 my( $e, $m_id ) = @_;
344 #syslog('LOG_DEBUG', "OILS: metarecord_to_title($m_id)");
345 my $mods = $U->simplereq(
347 'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
348 return ($U->event_code($mods)) ? "<unknown>" : $mods->title;
353 # remove the hold on item item_id from my hold queue.
354 # return true if I was holding the item, false otherwise.
357 my ($self, $item_id) = @_;
361 sub __patron_items_info {
363 return if $self->{items_info};
365 OpenILS::Application::Actor::_checked_out(
366 0, $self->{editor}, $self->{user}->id);;
370 my ($self, $start, $end) = @_;
372 $self->__patron_items_info();
373 my @overdues = @{$self->{item_info}->{overdue}};
374 #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
377 syslog('LOG_DEBUG', "OILS: overdue_items() fleshing circs @overdues");
378 for my $circid (@overdues) {
380 push( @o, __circ_to_title($self->{editor}, $circid) );
384 return (defined $start and defined $end) ?
385 [ $overdues[($start-1)..($end-1)] ] : \@overdues;
388 sub __circ_to_title {
389 my( $e, $circ ) = @_;
391 $circ = $e->retrieve_action_circulation($circ);
392 return __copy_to_title( $e,
393 $e->retrieve_asset_copy($circ->target_copy) );
397 my ($self, $start, $end) = shift;
399 $self->__patron_items_info();
402 @{$self->{item_info}->{out}},
403 @{$self->{item_info}->{overdue}}
406 #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
409 syslog('LOG_DEBUG', "OILS: charged_items() fleshing circs @charges");
410 for my $circid (@charges) {
412 push( @c, __circ_to_title($self->{editor}, $circid) );
416 return (defined $start and defined $end) ?
417 [ $charges[($start-1)..($end-1)] ] :
422 my ($self, $start, $end) = @_;
424 return (defined $start and defined $end) ?
425 [ $fines[($start-1)..($end-1)] ] : \@fines;
428 # not currently supported
430 my ($self, $start, $end) = @_;
435 my ($self, $start, $end) = @_;
437 return (defined $start and defined $end) ?
438 [ $holds[($start-1)..($end-1)] ] : \@holds;
442 my ($self, $card_retained, $blocked_card_msg) = @_;
444 my $u = $self->{user};
445 my $e = $self->{editor} = OpenILS::SIP->reset_editor();
447 syslog('LOG_INFO', "OILS: Blocking user %s", $u->card->barcode );
449 return $self if $u->card->active eq 'f';
451 $u->card->active('f');
452 if( ! $e->update_actor_card($u->card) ) {
453 syslog('LOG_ERR', "OILS: Block card update failed: %s", $e->event->{textcode});
458 # retrieve the un-fleshed user object for update
459 $u = $e->retrieve_actor_user($u->id);
460 my $note = $u->alert_message || "";
461 $note = "CARD BLOCKED BY SELF-CHECK MACHINE\n$note"; # XXX Config option
463 $u->alert_message($note);
465 if( ! $e->update_actor_user($u) ) {
466 syslog('LOG_ERR', "OILS: Block: patron alert update failed: %s", $e->event->{textcode});
472 $self->{user}->alert_message( $note );
474 $e->commit; # commits and resets
475 $self->{editor} = OpenILS::SIP->reset_editor();
479 # Testing purposes only
482 # Un-mark card as inactive, grep out the patron alert
483 $self->{screen_msg} = "All privileges restored.";
492 return "Please contact library staff";
496 return "Please contact library staff";
499 sub inet_privileges {
501 my $e = OpenILS::SIP->editor();
502 $INET_PRIVS = $e->retrieve_all_config_net_access_level() unless $INET_PRIVS;
503 my ($level) = grep { $_->id eq $self->{user}->net_access_level } @$INET_PRIVS;
504 my $name = $level->name;
505 syslog('LOG_DEBUG', "OILS: Patron inet_privs = $name");