]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/SIP/Patron.pm
722a907c7c138cafe13392366380616ca562a115
[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         return $b if $u->card->active eq 'f';
213
214         if( $u->standing_penalties ) {
215                 return $b if 
216                         grep { $_->penalty_type eq 'PATRON_EXCEEDS_OVERDUE_COUNT' } 
217                                 @{$u->standing_penalties};
218
219                 return $b if 
220                         grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' } 
221                                 @{$u->standing_penalties};
222         }
223
224         my $expire = DateTime::Format::ISO8601->new->parse_datetime(
225                 clense_ISO8601($u->expire_date));
226
227         return $b if CORE::time > $expire->epoch;
228
229         return 'OK';
230 }
231
232 sub print_line {
233     my $self = shift;
234         return '';
235 }
236
237 sub too_many_charged {
238     my $self = shift;
239         return 0;
240 }
241
242 sub too_many_overdue {
243         my $self = shift;
244         if( $self->{user}->standing_penalties ) {
245                 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_OVERDUE_COUNT' } 
246                         @{$self->{user}->standing_penalties};
247         }
248         return 0;
249 }
250
251 # not completely sure what this means
252 sub too_many_renewal {
253     my $self = shift;
254         return 0;
255 }
256
257 # not relevant, handled by fines/fees
258 sub too_many_claim_return {
259     my $self = shift;
260         return 0;
261 }
262
263 # not relevant, handled by fines/fees
264 sub too_many_lost {
265     my $self = shift;
266         return 0;
267 }
268
269 sub excessive_fines {
270     my $self = shift;
271         if( $self->{user}->standing_penalties ) {
272                 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' } 
273                         @{$self->{user}->standing_penalties};
274         }
275         return 0;
276 }
277
278
279 # Until someone suggests otherwise, fees and fines are the same
280
281 sub excessive_fees {
282         my $self = shift;
283         if( $self->{user}->standing_penalties ) {
284                 return grep { $_->penalty_type eq 'PATRON_EXCEEDS_FINES' } 
285                         @{$self->{user}->standing_penalties};
286         }
287         return 0;
288 }
289
290 # not relevant, handled by fines/fees
291 sub too_many_billed {
292     my $self = shift;
293         return 0;
294 }
295
296
297
298 #
299 # List of outstanding holds placed
300 #
301 sub hold_items {
302     my ($self, $start, $end) = @_;
303
304          my $holds = $self->{editor}->search_action_hold_request(
305                 { usr => $self->{user}->id, fulfillment_time => undef }
306          );
307
308         my @holds;
309         push( @holds, $self->__hold_to_title($_) ) for @$holds;
310
311         return (defined $start and defined $end) ? 
312                 [ $holds[($start-1)..($end-1)] ] : 
313                 \@holds;
314 }
315
316 sub __hold_to_title {
317         my $self = shift;
318         my $hold = shift;
319         my $e = $self->{editor};
320
321         my( $id, $mods, $title, $volume, $copy );
322
323         return __copy_to_title($e, 
324                 $e->retrieve_asset_copy($hold->target)) 
325                 if $hold->hold_type eq 'C';
326
327         return __volume_to_title($e, 
328                 $e->retrieve_asset_call_number($hold->target))
329                 if $hold->hold_type eq 'V';
330
331         return __record_to_title(
332                 $e, $hold->target) if $hold->hold_type eq 'T';
333
334         return __metarecord_to_title(
335                 $e, $hold->target) if $hold->hold_type eq 'M';
336 }
337
338 sub __copy_to_title {
339         my( $e, $copy ) = @_;
340         #syslog('LOG_DEBUG', "OILS: copy_to_title(%s)", $copy->id);
341         return $copy->dummy_title if $copy->call_number == -1;  
342         my $vol = $e->retrieve_asset_call_number($copy->call_number);
343         return __volume_to_title($e, $vol);
344 }
345
346
347 sub __volume_to_title {
348         my( $e, $volume ) = @_;
349         #syslog('LOG_DEBUG', "OILS: volume_to_title(%s)", $volume->id);
350         return __record_to_title($e, $volume->record);
351 }
352
353
354 sub __record_to_title {
355         my( $e, $title_id ) = @_;
356         #syslog('LOG_DEBUG', "OILS: record_to_title($title_id)");
357         my $mods = $U->simplereq(
358                 'open-ils.search',
359                 'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
360         return ($mods) ? $mods->title : "";
361 }
362
363 sub __metarecord_to_title {
364         my( $e, $m_id ) = @_;
365         #syslog('LOG_DEBUG', "OILS: metarecord_to_title($m_id)");
366         my $mods = $U->simplereq(
367                 'open-ils.search',
368                 'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
369         return ($U->event_code($mods)) ? "<unknown>" : $mods->title;
370 }
371
372
373 #
374 # remove the hold on item item_id from my hold queue.
375 # return true if I was holding the item, false otherwise.
376
377 sub drop_hold {
378     my ($self, $item_id) = @_;
379     return 0;
380 }
381
382 sub __patron_items_info {
383         my $self = shift;
384         return if $self->{items_info};
385         $self->{item_info} = 
386                 OpenILS::Application::Actor::_checked_out(
387                         0, $self->{editor}, $self->{user}->id);;
388 }
389
390 sub overdue_items {
391         my ($self, $start, $end) = @_;
392
393         $self->__patron_items_info();
394         my @overdues = @{$self->{item_info}->{overdue}};
395         #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
396
397         my @o;
398         syslog('LOG_DEBUG', "OILS: overdue_items() fleshing circs @overdues");
399         for my $circid (@overdues) {
400                 next unless $circid;
401                 push( @o, __circ_to_title($self->{editor}, $circid) );
402         }
403         @overdues = @o;
404
405         return (defined $start and defined $end) ? 
406                 [ $overdues[($start-1)..($end-1)] ] : \@overdues;
407 }
408
409 sub __circ_to_title {
410         my( $e, $circ ) = @_;
411         return unless $circ;
412         $circ = $e->retrieve_action_circulation($circ);
413         return __copy_to_title( $e, 
414                 $e->retrieve_asset_copy($circ->target_copy) );
415 }
416
417 sub charged_items {
418         my ($self, $start, $end) = shift;
419
420         $self->__patron_items_info();
421
422         my @charges = (
423                 @{$self->{item_info}->{out}},
424                 @{$self->{item_info}->{overdue}}
425                 );
426
427         #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
428
429         my @c;
430         syslog('LOG_DEBUG', "OILS: charged_items() fleshing circs @charges");
431         for my $circid (@charges) {
432                 next unless $circid;
433                 push( @c, __circ_to_title($self->{editor}, $circid) );
434         }
435         @charges = @c;
436
437         return (defined $start and defined $end) ? 
438                 [ $charges[($start-1)..($end-1)] ] : 
439                 \@charges;
440 }
441
442 sub fine_items {
443         my ($self, $start, $end) = @_;
444         my @fines;
445         return (defined $start and defined $end) ? 
446                 [ $fines[($start-1)..($end-1)] ] : \@fines;
447 }
448
449 # not currently supported
450 sub recall_items {
451     my ($self, $start, $end) = @_;
452          return [];
453 }
454
455 sub unavail_holds {
456         my ($self, $start, $end) = @_;
457         my @holds;
458         return (defined $start and defined $end) ? 
459                 [ $holds[($start-1)..($end-1)] ] : \@holds;
460 }
461
462 sub block {
463         my ($self, $card_retained, $blocked_card_msg) = @_;
464
465         my $u = $self->{user};
466         my $e = $self->{editor} = OpenILS::SIP->reset_editor();
467
468         syslog('LOG_INFO', "OILS: Blocking user %s", $u->card->barcode );
469
470         return $self if $u->card->active eq 'f';
471
472         $u->card->active('f');
473         if( ! $e->update_actor_card($u->card) ) {
474                 syslog('LOG_ERR', "OILS: Block card update failed: %s", $e->event->{textcode});
475                 $e->xact_rollback;
476                 return $self;
477         }
478
479         # retrieve the un-fleshed user object for update
480         $u = $e->retrieve_actor_user($u->id);
481         my $note = $u->alert_message || "";
482         $note = "CARD BLOCKED BY SELF-CHECK MACHINE\n$note"; # XXX Config option
483
484         $u->alert_message($note);
485
486         if( ! $e->update_actor_user($u) ) {
487                 syslog('LOG_ERR', "OILS: Block: patron alert update failed: %s", $e->event->{textcode});
488                 $e->xact_rollback;
489                 return $self;
490         }
491
492         # stay in synch
493         $self->{user}->alert_message( $note );
494
495         $e->commit; # commits and resets
496         $self->{editor} = OpenILS::SIP->reset_editor();
497         return $self;
498 }
499
500 # Testing purposes only
501 sub enable {
502     my $self = shift;
503          # Un-mark card as inactive, grep out the patron alert
504     $self->{screen_msg} = "All privileges restored.";
505     return $self;
506 }
507
508 #
509 # Messages
510 #
511
512 sub invalid_patron {
513     return "Please contact library staff";
514 }
515
516 sub charge_denied {
517     return "Please contact library staff";
518 }
519
520 sub inet_privileges {
521         my( $self ) = @_;
522         my $e = OpenILS::SIP->editor();
523         $INET_PRIVS = $e->retrieve_all_config_net_access_level() unless $INET_PRIVS;
524         my ($level) = grep { $_->id eq $self->{user}->net_access_level } @$INET_PRIVS;
525         my $name = $level->name;
526         syslog('LOG_DEBUG', "OILS: Patron inet_privs = $name");
527         return $name;
528 }
529
530
531 1;