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