]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/SIP/Patron.pm
32b566eca955e5a816f09fdf86a14629a5bd1cb7
[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 sub new {
33     my ($class, $patron_id) = @_;
34     my $type = ref($class) || $class;
35     my $self = {};
36
37         syslog("LOG_DEBUG", "OILS: new OpenILS Patron(%s): searching...", $patron_id);
38
39         my $e = OpenILS::SIP->editor();
40
41         my $c = $e->search_actor_card({barcode => $patron_id}, {idlist=>1});
42         my $user;
43
44         if( @$c ) {
45
46                 $user = $e->search_actor_user(
47                         [
48                                 { card => $$c[0] },
49                                 {
50                                         flesh => 1,
51                                         flesh_fields => {
52                                                 "au" => [
53                                                         #"cards",
54                                                         "card",
55                                                         "standing_penalties",
56                                                         "addresses",
57                                                         "billing_address",
58                                                         "mailing_address",
59                                                         #"stat_cat_entries",
60                                                         'profile',
61                                                 ]
62                                         }
63                                 }
64                         ]
65                 );
66
67                 $user = (@$user) ? $$user[0] : undef;
68          }
69
70          if(!$user) {
71                 syslog("LOG_WARNING", "OILS: Unable to find patron %s", $patron_id);
72                 return undef;
73          }
74
75         $self->{user}           = $user;
76         $self->{id}                     = $patron_id;
77         $self->{editor} = $e;
78
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 );
81
82
83         bless $self, $type;
84         return $self;
85 }
86
87 sub id {
88     my $self = shift;
89     return $self->{id};
90 }
91
92 sub name {
93     my $self = shift;
94          my $u = $self->{user};
95          return $u->first_given_name . ' ' . 
96                 $u->second_given_name . ' ' . $u->family_name;
97 }
98
99 sub __addr_string {
100         my $addr = shift;
101         return "" unless $addr;
102         return $addr->street1 .' '. 
103                 $addr->street2 .' '.
104                 $addr->city .' '.
105                 $addr->county .' '.
106                 $addr->state .' '.
107                 $addr->country .' '.
108                 $addr->post_code;
109 }
110
111 sub address {
112         my $self = shift;
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");
118         #my $maddr = $u->mailing_address;
119         #$str .= "\n" . __addr_string($maddr) 
120                 #if $maddr and $maddr->id ne $addr->id;
121         return $str;
122 }
123
124 sub email_addr {
125     my $self = shift;
126         return $self->{user}->email;
127 }
128
129 sub home_phone {
130     my $self = shift;
131         return $self->{user}->day_phone;
132 }
133
134 sub sip_birthdate {
135         my $self = shift;
136         my $dob = $self->{user}->dob;
137         return "" unless $dob;
138
139         $dob = DateTime::Format::ISO8601->new->
140                 parse_datetime(OpenSRF::Utils::clense_ISO8601($dob));
141         my @time = localtime($dob->epoch);
142
143         my $year = $time[5]+1900;
144         my $mon = $time[4]+1;
145         my $day = $time[3];
146
147         $mon =~ s/^(\d)$/0$1/;
148         $day =~ s/^(\d)$/0$1/;
149         $dob = "$year$mon$day";
150
151         syslog('LOG_DEBUG', "OILS: Patron DOB = $dob");
152
153         return $dob;
154 }
155
156 sub ptype {
157     my $self = shift;
158         return $self->{user}->profile->name;
159 }
160
161 sub language {
162     my $self = shift;
163     return '000'; # Unspecified
164 }
165
166 # How much more detail do we need to check here?
167 sub charge_ok {
168     my $self = shift;
169          my $u = $self->{user};
170          return (($u->barred eq 'f') and ($u->card->active eq 't'));
171 }
172
173 # How much more detail do we need to check here?
174 sub renew_ok {
175     my $self = shift;
176          return $self->charge_ok;
177 }
178
179 sub recall_ok {
180     my $self = shift;
181     return 0;
182 }
183
184 sub hold_ok {
185     my $self = shift;
186     return 0;
187 }
188
189 # return true if the card provided is marked as lost
190 sub card_lost {
191     my $self = shift;
192     return 0;
193 }
194
195 sub recall_overdue {
196     my $self = shift;
197     return 0;
198 }
199
200
201 sub check_password {
202         my ($self, $pwd) = @_;
203         return md5_hex($pwd) eq $self->{user}->passwd;
204 }
205
206
207 sub currency {
208         my $self = shift;
209         return 'USD';
210 }
211
212 sub fee_amount {
213         my $self = shift;
214         return 0;
215 }
216
217 sub screen_msg {
218     my $self = shift;
219         return '';
220 }
221
222 sub print_line {
223     my $self = shift;
224         return '';
225 }
226
227 sub too_many_charged {
228     my $self = shift;
229         return 0;
230 }
231
232 sub too_many_overdue {
233         my $self = shift;
234         if( $self->{user}->standing_penalties ) {
235                 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_OVERDUE_COUNT' } 
236                         @{$self->{user}->standing_penalties};
237         }
238         return 0;
239 }
240
241 # not completely sure what this means
242 sub too_many_renewal {
243     my $self = shift;
244         return 0;
245 }
246
247 # not relevant, handled by fines/fees
248 sub too_many_claim_return {
249     my $self = shift;
250         return 0;
251 }
252
253 # not relevant, handled by fines/fees
254 sub too_many_lost {
255     my $self = shift;
256         return 0;
257 }
258
259 sub excessive_fines {
260     my $self = shift;
261         if( $self->{user}->standing_penalties ) {
262                 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' } 
263                         @{$self->{user}->standing_penalties};
264         }
265         return 0;
266 }
267
268
269 # Until someone suggests otherwise, fees and fines are the same
270
271 sub excessive_fees {
272         my $self = shift;
273         if( $self->{user}->standing_penalties ) {
274                 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' } 
275                         @{$self->{user}->standing_penalties};
276         }
277         return 0;
278 }
279
280 # not relevant, handled by fines/fees
281 sub too_many_billed {
282     my $self = shift;
283         return 0;
284 }
285
286
287
288 #
289 # List of outstanding holds placed
290 #
291 sub hold_items {
292     my ($self, $start, $end) = @_;
293
294          my $holds = $self->{editor}->search_action_hold_request(
295                 { usr => $self->{user}->id, fulfillment_time => undef }
296          );
297
298         my @holds;
299         push( @holds, $self->__hold_to_title($_) ) for @$holds;
300
301         return (defined $start and defined $end) ? 
302                 [ $holds[($start-1)..($end-1)] ] : 
303                 \@holds;
304 }
305
306 sub __hold_to_title {
307         my $self = shift;
308         my $hold = shift;
309         my $e = $self->{editor};
310
311         my( $id, $mods, $title, $volume, $copy );
312
313         return __copy_to_title($e, 
314                 $e->retrieve_asset_copy($hold->target)) 
315                 if $hold->hold_type eq 'C';
316
317         return __volume_to_title($e, 
318                 $e->retrieve_asset_call_number($hold->target))
319                 if $hold->hold_type eq 'V';
320
321         return __record_to_title(
322                 $e, $hold->target) if $hold->hold_type eq 'T';
323
324         return __metarecord_to_title(
325                 $e, $hold->target) if $hold->hold_type eq 'M';
326 }
327
328 sub __copy_to_title {
329         my( $e, $copy ) = @_;
330         syslog('LOG_DEBUG', "OILS: copy_to_title(%s)", $copy->id);
331         return $copy->dummy_title if $copy->call_number == -1;  
332         my $vol = $e->retrieve_asset_call_number($copy->call_number);
333         return __volume_to_title($e, $vol);
334 }
335
336
337 sub __volume_to_title {
338         my( $e, $volume ) = @_;
339         syslog('LOG_DEBUG', "OILS: volume_to_title(%s)", $volume->id);
340         return __record_to_title($e, $volume->record);
341 }
342
343
344 sub __record_to_title {
345         my( $e, $title_id ) = @_;
346         syslog('LOG_DEBUG', "OILS: record_to_title($title_id)");
347         my $mods = $U->simplereq(
348                 'open-ils.search',
349                 'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
350         return ($mods) ? $mods->title : "";
351 }
352
353 sub __metarecord_to_title {
354         my( $e, $m_id ) = @_;
355         syslog('LOG_DEBUG', "OILS: metarecord_to_title($m_id)");
356         my $mods = $U->simplereq(
357                 'open-ils.search',
358                 'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
359         return ($U->event_code($mods)) ? "<unknown>" : $mods->title;
360 }
361
362
363 #
364 # remove the hold on item item_id from my hold queue.
365 # return true if I was holding the item, false otherwise.
366
367 sub drop_hold {
368     my ($self, $item_id) = @_;
369     return 0;
370 }
371
372 sub __patron_items_info {
373         my $self = shift;
374         return if $self->{items_info};
375         $self->{item_info} = 
376                 OpenILS::Application::Actor::_checked_out(
377                         0, $self->{editor}, $self->{user}->id);;
378 }
379
380 sub overdue_items {
381         my ($self, $start, $end) = @_;
382
383         $self->__patron_items_info();
384         my @overdues = @{$self->{item_info}->{overdue}};
385         #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
386
387         my @o;
388         for my $circid (@overdues) {
389                 next unless $circid;
390                 syslog('LOG_DEBUG', "OILS: overdue_items() fleshing circ $circid");
391                 push( @o, __circ_to_title($self->{editor}, $circid) );
392         }
393         @overdues = @o;
394
395         return (defined $start and defined $end) ? 
396                 [ $overdues[($start-1)..($end-1)] ] : \@overdues;
397 }
398
399 sub __circ_to_title {
400         my( $e, $circ ) = @_;
401         return unless $circ;
402         $circ = $e->retrieve_action_circulation($circ);
403         return __copy_to_title( $e, 
404                 $e->retrieve_asset_copy($circ->target_copy) );
405 }
406
407 sub charged_items {
408         my ($self, $start, $end) = shift;
409
410         $self->__patron_items_info();
411
412         my @charges = (
413                 @{$self->{item_info}->{out}},
414                 @{$self->{item_info}->{overdue}}
415                 );
416
417         #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
418
419         my @c;
420         for my $circid (@charges) {
421                 next unless $circid;
422                 syslog('LOG_DEBUG', "OILS: charged_items() fleshing circ $circid");
423                 push( @c, __circ_to_title($self->{editor}, $circid) );
424         }
425         @charges = @c;
426
427         return (defined $start and defined $end) ? 
428                 [ $charges[($start-1)..($end-1)] ] : 
429                 \@charges;
430 }
431
432 sub fine_items {
433         my ($self, $start, $end) = @_;
434         my @fines;
435         return (defined $start and defined $end) ? 
436                 [ $fines[($start-1)..($end-1)] ] : \@fines;
437 }
438
439 # not currently supported
440 sub recall_items {
441     my ($self, $start, $end) = @_;
442          return [];
443 }
444
445 sub unavail_holds {
446         my ($self, $start, $end) = @_;
447         my @holds;
448         return (defined $start and defined $end) ? 
449                 [ $holds[($start-1)..($end-1)] ] : \@holds;
450 }
451
452 sub block {
453         my ($self, $card_retained, $blocked_card_msg) = @_;
454
455         my $u = $self->{user};
456         my $e = $self->{editor} = OpenILS::SIP->reset_editor();
457
458         syslog('LOG_INFO', "OILS: Blocking user %s", $u->card->barcode );
459
460         return $self if $u->card->active eq 'f';
461
462         $u->card->active('f');
463         if( ! $e->update_actor_card($u->card) ) {
464                 syslog('LOG_ERR', "OILS: Block card update failed: %s", $e->event->{textcode});
465                 $e->xact_rollback;
466                 return $self;
467         }
468
469         # retrieve the un-fleshed user object for update
470         $u = $e->retrieve_actor_user($u->id);
471         my $note = $u->alert_message || "";
472         $note = "CARD BLOCKED BY SELF-CHECK MACHINE\n$note"; # XXX Config option
473
474         $u->alert_message($note);
475
476         if( ! $e->update_actor_user($u) ) {
477                 syslog('LOG_ERR', "OILS: Block: patron alert update failed: %s", $e->event->{textcode});
478                 $e->xact_rollback;
479                 return $self;
480         }
481
482         # stay in synch
483         $self->{user}->alert_message( $note );
484
485         $e->commit; # commits and resets
486         $self->{editor} = OpenILS::SIP->reset_editor();
487         return $self;
488 }
489
490 # Testing purposes only
491 sub enable {
492     my $self = shift;
493          # Un-mark card as inactive, grep out the patron alert
494     $self->{screen_msg} = "All privileges restored.";
495     return $self;
496 }
497
498 #
499 # Messages
500 #
501
502 sub invalid_patron {
503     return "Please contact library staff";
504 }
505
506 sub charge_denied {
507     return "Please contact library staff";
508 }
509
510 sub inet_privileges {
511         my( $self ) = @_;
512         my $e = OpenILS::SIP->editor();
513         $INET_PRIVS = $e->retrieve_all_config_net_access_level() unless $INET_PRIVS;
514         my ($level) = grep { $_->id eq $self->{user}->net_access_level } @$INET_PRIVS;
515         my $name = $level->name;
516         syslog('LOG_DEBUG', "OILS: Patron inet_privs = $name");
517         return $name;
518 }
519
520
521 1;