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