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