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