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);
17 use OpenILS::Application::AppUtils;
18 use OpenILS::Application::Actor;
19 my $U = 'OpenILS::Application::AppUtils';
21 our (@ISA, @EXPORT_OK);
25 @EXPORT_OK = qw(invalid_patron);
28 my ($class, $patron_id) = @_;
29 my $type = ref($class) || $class;
32 syslog("LOG_DEBUG", "new OpenILS Patron(%s): searching...", $patron_id);
34 require OpenILS::Utils::CStoreEditor;
35 my $e = OpenILS::Utils::CStoreEditor->new;
37 if(!UNIVERSAL::can($e, 'search_actor_card')) {
38 syslog("LOG_WARNING", "Reloading CStoreEditor...");
39 delete $INC{'OpenILS/Utils/CStoreEditor.pm'};
40 require OpenILS::Utils::CStoreEditor;
41 $e = OpenILS::Utils::CStoreEditor->new;
45 my $c = $e->search_actor_card({barcode => $patron_id}, {idlist=>1});
50 $user = $e->search_actor_user(
71 $user = (@$user) ? $$user[0] : undef;
75 syslog("LOG_WARNING", "Unable to find patron %s", $patron_id);
79 $self->{user} = $user;
80 $self->{id} = $patron_id;
83 syslog("LOG_DEBUG", "new OpenILS Patron(%s): found patron : barred=%s, card:active=%s",
84 $patron_id, $self->{user}->barred, $self->{user}->card->active );
97 my $u = $self->{user};
98 return $u->first_given_name . ' ' .
99 $u->second_given_name . ' ' . $u->family_name;
104 return "" unless $addr;
105 return $addr->street1 .' '.
116 my $u = $self->{user};
117 my $addr = $u->billing_address;
118 my $str = __addr_string($addr);
119 my $maddr = $u->mailing_address;
120 $str .= "\n" . __addr_string($maddr)
121 if $maddr and $maddr->id ne $addr->id;
127 return $self->{user}->email;
132 return $self->{user}->day_phone;
137 return $self->{user}->dob;
142 return $self->{user}->profile->name;
147 return '000'; # Unspecified
150 # How much more detail do we need to check here?
153 my $u = $self->{user};
154 return (($u->barred eq 'f') and ($u->card->active eq 't'));
157 # How much more detail do we need to check here?
160 return $self->charge_ok;
173 # return true if the card provided is marked as lost
186 my ($self, $pwd) = @_;
187 return md5_hex($pwd) eq $self->{user}->passwd;
211 sub too_many_charged {
216 sub too_many_overdue {
218 if( $self->{user}->standing_penalties ) {
219 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_OVERDUE_COUNT' }
220 @{$self->{user}->standing_penalties};
225 # not completely sure what this means
226 sub too_many_renewal {
231 # not relevant, handled by fines/fees
232 sub too_many_claim_return {
237 # not relevant, handled by fines/fees
243 sub excessive_fines {
245 if( $self->{user}->standing_penalties ) {
246 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' }
247 @{$self->{user}->standing_penalties};
253 # Until someone suggests otherwise, fees and fines are the same
257 if( $self->{user}->standing_penalties ) {
258 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' }
259 @{$self->{user}->standing_penalties};
264 # not relevant, handled by fines/fees
265 sub too_many_billed {
273 # List of outstanding holds placed
276 my ($self, $start, $end) = @_;
278 my $holds = $self->{editor}->search_action_hold_request(
279 { usr => $self->{user}->id, fulfillment_time => undef }
283 push( @holds, $self->__hold_to_title($_) ) for @$holds;
285 return (defined $start and defined $end) ?
286 [ $holds[($start-1)..($end-1)] ] :
290 sub __hold_to_title {
293 my $e = $self->{editor};
295 my( $id, $mods, $title, $volume, $copy );
297 return __copy_to_title($e,
298 $e->retrieve_asset_copy($hold->target))
299 if $hold->hold_type eq 'C';
301 return __volume_to_title($e,
302 $e->retrieve_asset_call_number($hold->target))
303 if $hold->hold_type eq 'V';
305 return __record_to_title(
306 $e, $hold->target) if $hold->hold_type eq 'T';
308 return __metarecord_to_title(
309 $e, $hold->target) if $hold->hold_type eq 'M';
312 sub __copy_to_title {
313 my( $e, $copy ) = @_;
314 return $copy->dummy_title if $copy->call_number == -1;
315 my $vol = $e->retrieve_asset_call_number($copy->call_number);
316 return __volume_to_title($e, $vol);
320 sub __volume_to_title {
321 my( $e, $volume ) = @_;
322 return __record_to_title($e, $volume->record);
326 sub __record_to_title {
327 my( $e, $title_id ) = @_;
328 my $mods = $U->simplereq(
330 'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
331 return ($mods) ? $mods->title : "";
334 sub __metarecord_to_title {
335 my( $e, $m_id ) = @_;
336 my $mods = $U->simplereq(
338 'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
339 return ($mods) ? $mods->title : "";
344 # remove the hold on item item_id from my hold queue.
345 # return true if I was holding the item, false otherwise.
348 my ($self, $item_id) = @_;
352 sub __patron_items_info {
354 return if $self->{items_info};
356 OpenILS::Application::Actor::_checked_out(
357 0, $self->{editor}, $self->{user}->id);;
361 my ($self, $start, $end) = @_;
363 $self->__patron_items_info();
364 my @overdues = @{$self->{item_info}->{overdue}};
365 #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
368 for my $circid (@overdues) {
370 syslog('LOG_DEBUG', "overdue_items() fleshing circ $circid");
371 push( @o, __circ_to_title($self->{editor}, $circid) );
375 return (defined $start and defined $end) ?
376 [ $overdues[($start-1)..($end-1)] ] : \@overdues;
379 sub __circ_to_title {
380 my( $e, $circ ) = @_;
382 $circ = $e->retrieve_action_circulation($circ);
383 return __copy_to_title( $e,
384 $e->retrieve_asset_copy($circ->target_copy) );
388 my ($self, $start, $end) = shift;
390 $self->__patron_items_info();
393 @{$self->{item_info}->{out}},
394 @{$self->{item_info}->{overdue}}
397 #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
400 for my $circid (@charges) {
402 syslog('LOG_DEBUG', "charged_items() fleshing circ $circid");
403 push( @c, __circ_to_title($self->{editor}, $circid) );
407 return (defined $start and defined $end) ?
408 [ $charges[($start-1)..($end-1)] ] :
413 my ($self, $start, $end) = @_;
415 return (defined $start and defined $end) ?
416 [ $fines[($start-1)..($end-1)] ] : \@fines;
419 # not currently supported
421 my ($self, $start, $end) = @_;
426 my ($self, $start, $end) = @_;
428 return (defined $start and defined $end) ?
429 [ $holds[($start-1)..($end-1)] ] : \@holds;
433 my ($self, $card_retained, $blocked_card_msg) = @_;
435 my $u = $self->{user};
436 my $e = $self->{editor};
438 syslog('LOG_INFO', "Blocking user %s", $u->card->barcode );
440 if(!$e->{xact}) { $e->reset; $e->{xact} = 1; }
442 return $self if $u->card->active eq 'f';
444 $u->card->active('f');
445 if( ! $e->update_actor_card($u->card) ) {
446 syslog('LOG_ERR', "Block card update failed: %s", $e->event->{textcode});
451 # retrieve the un-fleshed user object for update
452 $u = $e->retrieve_actor_user($u->id);
453 my $note = $u->alert_message || "";
454 $note = "CARD BLOCKED BY SELF-CHECK MACHINE\n$note"; # XXX Config option
456 $u->alert_message($note);
458 if( ! $e->update_actor_user($u) ) {
459 syslog('LOG_ERR', "Block: patron alert update failed: %s", $e->event->{textcode});
465 $self->{user}->alert_message( $note );
467 $e->finish; # commits and resets
471 # Testing purposes only
474 # Un-mark card as inactive, grep out the patron alert
475 $self->{screen_msg} = "All privileges restored.";
484 return "Please contact library staff";
488 return "Please contact library staff";