]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/SIP/Patron.pm
Lots of cleanup, logging improvements, and comments.
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / SIP / Patron.pm
1 #
2
3 # A Class for hiding the ILS's concept of the patron from the OpenSIP
4 # system
5 #
6
7 package OpenILS::SIP::Patron;
8
9 use strict;
10 use warnings;
11 use Exporter;
12
13 use Sys::Syslog qw(syslog);
14 use Data::Dumper;
15 use Digest::MD5 qw(md5_hex);
16
17 use OpenILS::SIP;
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';
23
24 our (@ISA, @EXPORT_OK);
25
26 @ISA = qw(Exporter);
27
28 @EXPORT_OK = qw(invalid_patron);
29
30 my $INET_PRIVS;
31
32 #
33 # OpenILS::SIP::Patron->new($barcode);
34 # OpenILS::SIP::Patron->new(barcode => $barcode);   # same as above
35 # OpenILS::SIP::Patron->new(    usr => $id);       
36
37 sub new {
38     my ($class, $patron_id) = @_;
39     my $type = ref($class) || $class;
40     my $self = {};
41
42         syslog("LOG_DEBUG", "OILS: new OpenILS Patron(%s): searching...", $patron_id);
43
44         my $e = OpenILS::SIP->editor();
45
46         my $c = $e->search_actor_card({barcode => $patron_id}, {idlist=>1});
47         my $user;
48
49         if( @$c ) {
50
51                 $user = $e->search_actor_user(
52                         [
53                                 { card => $$c[0] },
54                                 {
55                                         flesh => 2,
56                                         flesh_fields => {
57                                                 "au" => [
58                                                         #"cards",
59                                                         "card",
60                                                         "standing_penalties",
61                                                         "addresses",
62                                                         "billing_address",
63                                                         "mailing_address",
64                                                         #"stat_cat_entries",
65                                                         'profile',
66                                                 ],
67                         ausp => ['standing_penalty']
68                                         }
69                                 }
70                         ]
71                 );
72
73                 $user = (@$user) ? $$user[0] : undef;
74          }
75
76          if(!$user) {
77                 syslog("LOG_WARNING", "OILS: Unable to find patron %s", $patron_id);
78                 return undef;
79          }
80
81         $self->{user}   = $user;
82         $self->{id}     = $patron_id;
83         $self->{editor} = $e;
84
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 );
87
88
89         bless $self, $type;
90         return $self;
91 }
92
93 sub id {
94     my $self = shift;
95     return $self->{id};
96 }
97
98 sub name {
99     my $self = shift;
100     my $u = $self->{user};
101     return OpenILS::SIP::clean_text($u->first_given_name . ' ' . 
102             $u->second_given_name . ' ' . $u->family_name);
103 }
104
105 sub home_library {
106     my $self = shift;
107     my $lib = $self->{editor}->retrieve_actor_org_unit($self->{user}->home_ou)->shortname;
108         syslog('LOG_DEBUG', "OILS: Patron->home_library() = $lib");
109     return $lib;
110 }
111
112 sub __addr_string {
113     my $addr = shift;
114     return "" unless $addr;
115     return OpenILS::SIP::clean_text($addr->street1 .' '. 
116         $addr->street2 .' '.
117         $addr->city .' '.
118         $addr->county .' '.
119         $addr->state .' '.
120         $addr->country .' '.
121         $addr->post_code);
122 }
123
124 sub address {
125         my $self = shift;
126         my $u    = $self->{user};
127         my $str  = __addr_string($u->billing_address || $u->mailing_address);
128         syslog('LOG_DEBUG', "OILS: Patron address: $str");
129         return $str;
130 }
131
132 sub email_addr {
133     my $self = shift;
134     return OpenILS::SIP::clean_text($self->{user}->email);
135 }
136
137 sub home_phone {
138     my $self = shift;
139     return $self->{user}->day_phone;
140 }
141
142 sub sip_birthdate {
143         my $self = shift;
144         my $dob = OpenILS::SIP->format_date($self->{user}->dob);
145         syslog('LOG_DEBUG', "OILS: Patron DOB = $dob");
146         return $dob;
147 }
148
149 sub ptype {
150     my $self = shift;
151     return OpenILS::SIP::clean_text($self->{user}->profile->name);
152 }
153
154 sub language {
155     my $self = shift;
156     return '000'; # Unspecified
157 }
158
159 # How much more detail do we need to check here?
160 sub charge_ok {
161     my $self = shift;
162          my $u = $self->{user};
163          return (($u->barred eq 'f') and ($u->card->active eq 't'));
164 }
165
166 # How much more detail do we need to check here?
167 sub renew_ok {
168     my $self = shift;
169          return $self->charge_ok;
170 }
171
172 sub recall_ok {
173     my $self = shift;
174     return 0;
175 }
176
177 sub hold_ok {
178     my $self = shift;
179          return $self->charge_ok;
180 }
181
182 # return true if the card provided is marked as lost
183 sub card_lost {
184     my $self = shift;
185          return $self->{user}->card->active eq 'f';
186 }
187
188 sub recall_overdue {
189     my $self = shift;
190     return 0;
191 }
192
193
194 sub check_password {
195         my ($self, $pwd) = @_;
196         syslog('LOG_DEBUG', 'OILS: Patron->check_password()');
197         return md5_hex($pwd) eq $self->{user}->passwd;
198 }
199
200
201 sub currency {
202         my $self = shift;
203         syslog('LOG_DEBUG', 'OILS: Patron->currency()');
204         return 'USD';
205 }
206
207 sub fee_amount {
208         my $self = shift;
209         syslog('LOG_DEBUG', 'OILS: Patron->fee_amount()');
210
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);
215
216         my $total = $summary->balance_owed;
217         syslog('LOG_INFO', "User ".$self->{id} .':'.$self->{user}->id." has a fee amount of \$$total");
218         return $total;
219 }
220
221 sub screen_msg {
222         my $self = shift;
223         my $u = $self->{user};
224         return 'barred' if $u->barred eq 't';
225
226         my $b = 'blocked';
227
228         return $b if $u->active eq 'f';
229         return $b if $u->card->active eq 'f';
230
231         if( $u->standing_penalties ) {
232                 return $b if 
233                         grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_OVERDUE_COUNT' } 
234                                 @{$u->standing_penalties};
235
236                 return $b if 
237                         grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_FINES' } 
238                                 @{$u->standing_penalties};
239         }
240
241         my $expire = DateTime::Format::ISO8601->new->parse_datetime(
242                 cleanse_ISO8601($u->expire_date));
243
244         return $b if CORE::time > $expire->epoch;
245
246         return 'OK';
247 }
248
249 sub print_line {
250     my $self = shift;
251         return '';
252 }
253
254 sub too_many_charged {
255     my $self = shift;
256         return 0;
257 }
258
259 sub too_many_overdue {
260         my $self = shift;
261         if( $self->{user}->standing_penalties ) {
262                 return grep { $_->standing_penalty->name eq 'PATRON_EXCEEDS_OVERDUE_COUNT' } 
263                         @{$self->{user}->standing_penalties};
264         }
265         return 0;
266 }
267
268 # not completely sure what this means
269 sub too_many_renewal {
270     my $self = shift;
271     return 0;
272 }
273
274 # not relevant, handled by fines/fees
275 sub too_many_claim_return {
276     my $self = shift;
277     return 0;
278 }
279
280 # not relevant, handled by fines/fees
281 sub too_many_lost {
282     my $self = shift;
283     return 0;
284 }
285
286 sub excessive_fines {
287     my $self = shift;
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};
292         }
293         return 0;
294 }
295
296
297 # Until someone suggests otherwise, fees and fines are the same
298
299 sub excessive_fees {
300         my $self = shift;
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};
305         }
306         return 0;
307 }
308
309 # not relevant, handled by fines/fees
310 sub too_many_billed {
311     my $self = shift;
312         return 0;
313 }
314
315
316
317 #
318 # List of outstanding holds placed
319 #
320 sub hold_items {
321     my ($self, $start, $end) = @_;
322         syslog('LOG_DEBUG', 'OILS: Patron->hold_items()');
323
324          my $holds = $self->{editor}->search_action_hold_request(
325                 { usr => $self->{user}->id, fulfillment_time => undef, cancel_time => undef }
326          );
327
328         my @holds;
329         push( @holds, OpenILS::SIP::clean_text($self->__hold_to_title($_)) ) for @$holds;
330
331         return (defined $start and defined $end) ? 
332                 [ $holds[($start-1)..($end-1)] ] : 
333                 \@holds;
334 }
335
336 sub __hold_to_title {
337         my $self = shift;
338         my $hold = shift;
339         my $e = $self->{editor};
340
341         my( $id, $mods, $title, $volume, $copy );
342
343         return __copy_to_title($e, 
344                 $e->retrieve_asset_copy($hold->target)) 
345                 if $hold->hold_type eq 'C';
346
347         return __volume_to_title($e, 
348                 $e->retrieve_asset_call_number($hold->target))
349                 if $hold->hold_type eq 'V';
350
351         return __record_to_title(
352                 $e, $hold->target) if $hold->hold_type eq 'T';
353
354         return __metarecord_to_title(
355                 $e, $hold->target) if $hold->hold_type eq 'M';
356 }
357
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;  
362
363         my $vol = (ref $copy->call_number) ?
364                 $copy->call_number :
365                 $e->retrieve_asset_call_number($copy->call_number);
366
367         return __volume_to_title($e, $vol);
368 }
369
370
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);
375 }
376
377
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(
382                 'open-ils.search',
383                 'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
384         return ($mods) ? $mods->title : "";
385 }
386
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(
391                 'open-ils.search',
392                 'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
393         return ($U->event_code($mods)) ? "<unknown>" : $mods->title;
394 }
395
396
397 #
398 # remove the hold on item item_id from my hold queue.
399 # return true if I was holding the item, false otherwise.
400
401 sub drop_hold {
402     my ($self, $item_id) = @_;
403     return 0;
404 }
405
406 sub __patron_items_info {
407         my $self = shift;
408         return if $self->{item_info};
409         $self->{item_info} = 
410                 OpenILS::Application::Actor::_checked_out(
411                         0, $self->{editor}, $self->{user}->id);;
412 }
413
414 sub overdue_items {
415         my ($self, $start, $end) = @_;
416
417         $self->__patron_items_info();
418         my @overdues = @{$self->{item_info}->{overdue}};
419         #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
420
421         my @o;
422         syslog('LOG_DEBUG', "OILS: overdue_items() fleshing circs @overdues");
423         
424         
425         my @return_datatype = grep { $_->{name} eq 'msg64_summary_datatype' } OpenILS::SIP::config()->{implementation_config}->{options}->{option};
426         
427         for my $circid (@overdues) {
428                 next unless $circid;
429                 if(@return_datatype and $return_datatype[0]->{value} eq 'barcode') {
430                         push( @o, __circ_to_barcode($self->{editor}, $circid));
431                 } else {
432                         push( @o, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
433                 }
434         }
435         @overdues = @o;
436
437         return (defined $start and defined $end) ? 
438                 [ $overdues[($start-1)..($end-1)] ] : \@overdues;
439 }
440
441 sub __circ_to_barcode {
442         my ($e, $circ) = @_;
443         return unless $circ;
444         $circ = $e->retrieve_action_circulation($circ);
445         my $copy = $e->retrieve_asset_copy($circ->target_copy);
446         return $copy->barcode;
447 }
448
449 sub __circ_to_title {
450         my( $e, $circ ) = @_;
451         return unless $circ;
452         $circ = $e->retrieve_action_circulation($circ);
453         return __copy_to_title( $e, 
454                 $e->retrieve_asset_copy($circ->target_copy) );
455 }
456
457 sub charged_items {
458         my ($self, $start, $end) = shift;
459
460         $self->__patron_items_info();
461
462         my @charges = (
463                 @{$self->{item_info}->{out}},
464                 @{$self->{item_info}->{overdue}}
465                 );
466
467         #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
468
469         my @c;
470         syslog('LOG_DEBUG', "OILS: charged_items() fleshing circs @charges");
471
472         my @return_datatype = grep { $_->{name} eq 'msg64_summary_datatype' } OpenILS::SIP::config()->{implementation_config}->{options}->{option};
473
474         for my $circid (@charges) {
475                 next unless $circid;
476                 if(@return_datatype and $return_datatype[0]->{value} eq 'barcode') {
477                         push( @c, __circ_to_barcode($self->{editor}, $circid));
478                 } else {
479                         push( @c, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
480                 }
481         }
482
483         @charges = @c;
484
485         return (defined $start and defined $end) ? 
486                 [ $charges[($start-1)..($end-1)] ] : 
487                 \@charges;
488 }
489
490 sub fine_items {
491         my ($self, $start, $end) = @_;
492         my @fines;
493         syslog('LOG_DEBUG', 'OILS: Patron->fine_items()');
494         return (defined $start and defined $end) ? 
495                 [ $fines[($start-1)..($end-1)] ] : \@fines;
496 }
497
498 # not currently supported
499 sub recall_items {
500     my ($self, $start, $end) = @_;
501     return [];
502 }
503
504 sub unavail_holds {
505         my ($self, $start, $end) = @_;
506         my @holds;
507         syslog('LOG_DEBUG', 'OILS: Patron->unavail_holds()');
508         return (defined $start and defined $end) ? 
509                 [ $holds[($start-1)..($end-1)] ] : \@holds;
510 }
511
512 sub block {
513         my ($self, $card_retained, $blocked_card_msg) = @_;
514
515     my $e = $self->{editor};
516         my $u = $self->{user};
517
518         syslog('LOG_INFO', "OILS: Blocking user %s", $u->card->barcode );
519
520         return $self if $u->card->active eq 'f';
521
522     # connect and start a new transaction
523     $e->xact_begin;
524
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
529                 return $self;
530         }
531
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
536
537         $u->alert_message($note);
538
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
542                 return $self;
543         }
544
545         # stay in synch
546         $self->{user}->alert_message( $note );
547
548         $e->commit; # commits and disconnects
549         return $self;
550 }
551
552 # Testing purposes only
553 sub enable {
554     my $self = shift;
555          # Un-mark card as inactive, grep out the patron alert
556     $self->{screen_msg} = "All privileges restored.";
557     return $self;
558 }
559
560 #
561 # Messages
562 #
563
564 sub invalid_patron {
565     return "Please contact library staff";
566 }
567
568 sub charge_denied {
569     return "Please contact library staff";
570 }
571
572 sub inet_privileges {
573     my( $self ) = @_;
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");
579     return $name;
580 }
581
582
583 1;