]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/SIP/Patron.pm
13870af831a9bff2b0c64bd954391d18364ab27a
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / 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 = shift;
34     my $key   = shift;
35     my $patron_id = shift;
36     my %args = @_;
37
38     if ($key ne 'usr' and $key ne 'barcode') {
39         syslog("LOG_ERROR", "Patron (card) lookup requested by illegeal key '$key'");
40         return undef;
41     }
42
43     unless(defined $patron_id) {
44         syslog("LOG_WARNING", "No patron ID provided to ILS::Patron->new");
45         return undef;
46     }
47
48     my $type = ref($class) || $class;
49     my $self = bless({}, $type);
50
51     syslog("LOG_DEBUG", "OILS: new OpenILS Patron(%s => %s): searching...", $key, $patron_id);
52
53     my $e = OpenILS::SIP->editor();
54
55     my $usr_flesh = {
56         flesh => 2,
57         flesh_fields => {
58             au => [
59                 "card",
60                 "addresses",
61                 "billing_address",
62                 "mailing_address",
63                 'profile',
64                 "stat_cat_entries",
65             ],
66             actscecm => [
67                 "stat_cat",
68             ],
69         }
70     };
71
72     # in some cases, we don't need all of this data.  Only fetch the user + barcode
73     $usr_flesh = {flesh => 1, flesh_fields => {au => ['card']}} if $args{slim_user};
74     
75     my $user;
76     if($key eq 'barcode') { # retrieve user by barcode
77
78         $$usr_flesh{flesh} += 1;
79         $$usr_flesh{flesh_fields}{ac} = ['usr'];
80
81         my $card = $e->search_actor_card([{barcode => $patron_id}, $usr_flesh])->[0];
82
83         if(!$card or !$U->is_true($card->active)) {
84             syslog("LOG_WARNING", "No such patron barcode: $patron_id");
85             return undef;
86         }
87
88         $user = $card->usr;
89
90     } else {
91         $user = $e->retrieve_actor_user([$patron_id, $usr_flesh]);
92     }
93
94     if(!$user or $U->is_true($user->deleted)) {
95         syslog("LOG_WARNING", "OILS: Unable to find patron %s => %s", $key, $patron_id);
96         return undef;
97     }
98
99     if(!$U->is_true($user->active)) {
100         syslog("LOG_WARNING", "OILS: Patron is inactive %s => %s", $key, $patron_id);
101         return undef;
102     }
103
104     # now grab the user's penalties
105
106     $self->flesh_user_penalties($user, $e) unless $args{slim_user};
107
108     $self->{authtoken} = $args{authtoken} if $args{authtoken};
109     $self->{editor} = $e;
110     $self->{user}   = $user;
111     $self->{id}     = ($key eq 'barcode') ? $patron_id : $user->card->barcode;   # The barcode IS the ID to SIP.  
112     # We give back the passed barcode if the key was indeed a barcode, just to be safe.  Otherwise pull it from the card.
113
114     syslog("LOG_DEBUG", "OILS: new OpenILS Patron(%s => %s): found patron : barred=%s, card:active=%s", 
115         $key, $patron_id, $user->barred, $user->card->active );
116
117     $U->log_user_activity($user->id, $self->get_act_who, 'verify');
118
119     return $self;
120 }
121
122 sub get_act_who {
123     my $self = shift;
124     my $config = OpenILS::SIP->config();
125     my $login = OpenILS::SIP->login_account();
126
127     my $act_who = $config->{implementation_config}->{default_activity_who};
128     my $force_who = $config->{implementation_config}->{force_activity_who};
129
130     # 1. future: test sip extension for caller-provided ewho and !$force_who
131
132     # 2. See if the login is tagged with an ewho
133     return $login->{activity_who} if $login->{activity_who};
134
135     # 3. if all else fails, see if there is an institution-wide ewho
136     return $config->{activity_who} if $config->{activity_who};
137
138     return undef;
139 }
140
141 # grab patron penalties.  Only grab non-archived penalties that are for fines,
142 # excessive overdues, or otherwise block circluation activity
143 sub flesh_user_penalties {
144     my ($self, $user, $e) = @_;
145
146     $user->standing_penalties(
147         $e->search_actor_user_standing_penalty([
148             {   
149                 usr => $user->id,
150                 '-or' => [
151
152                     # ignore "archived" penalties
153                     {stop_date => undef},
154                     {stop_date => {'>' => 'now'}}
155                 ],
156
157                 org_unit => {
158                     in  => {
159                         select => {
160                             aou => [{
161                                 column => 'id', 
162                                 transform => 'actor.org_unit_ancestors', 
163                                 result_field => 'id'
164                             }]
165                         },
166                         from => 'aou',
167
168                         # at this point, there is no concept of "here", so fetch penalties 
169                         # for the patron's home lib plus ancestors
170                         where => {id => $user->home_ou}, 
171                         distinct => 1
172                     }
173                 },
174
175                 # in addition to fines and excessive overdue penalties, 
176                 # we only care about penalties that result in blocks
177                 standing_penalty => {
178                     in => {
179                         select => {csp => ['id']},
180                         from => 'csp',
181                         where => {
182                             '-or' => [
183                                 {id => [1,2]}, # fines / overdues
184                                 {block_list => {'!=' => undef}}
185                             ]
186                         },
187                     }
188                 }
189             },
190         ])
191     );
192 }
193
194 sub id {
195     my $self = shift;
196     return $self->{id};
197 }
198
199 sub name {
200     my $self = shift;
201     return format_name($self->{user});
202 }
203
204 sub format_name {
205     my $u = shift;
206     return OpenILS::SIP::clean_text(
207         sprintf('%s %s %s', 
208             ($u->first_given_name || ''),
209             ($u->second_given_name || ''),
210             ($u->family_name || '')));
211 }
212
213 sub home_library {
214     my $self = shift;
215     my $lib = OpenILS::SIP::shortname_from_id($self->{user}->home_ou);
216     syslog('LOG_DEBUG', "OILS: Patron->home_library() = $lib");
217     return $lib;
218 }
219
220 sub __addr_string {
221     my $addr = shift;
222     return "" unless $addr;
223     my $return = OpenILS::SIP::clean_text(
224         join( ' ', map {$_ || ''} (
225             $addr->street1,
226             $addr->street2,
227             $addr->city . ',',
228             $addr->county,
229             $addr->state,
230             $addr->country,
231             $addr->post_code
232             )
233         )
234     );
235     $return =~ s/\s+/ /sg;     # Compress any run of of whitespace to one space
236     return $return;
237 }
238
239 sub internal_id {
240     my $self = shift;
241     return $self->{user}->id;
242 }
243
244 sub address {
245     my $self = shift;
246     my $u    = $self->{user};
247     my $str  = __addr_string($u->billing_address || $u->mailing_address);
248     syslog('LOG_DEBUG', "OILS: Patron address: $str");
249     return $str;
250 }
251
252 sub email_addr {
253     my $self = shift;
254     return OpenILS::SIP::clean_text($self->{user}->email);
255 }
256
257 sub home_phone {
258     my $self = shift;
259     return $self->{user}->day_phone;
260 }
261
262 sub sip_birthdate {
263     my $self = shift;
264     my $dob = OpenILS::SIP->format_date($self->{user}->dob);
265     syslog('LOG_DEBUG', "OILS: Patron DOB = $dob");
266     return $dob;
267 }
268
269 sub sip_expire {
270     my $self = shift;
271     my $expire = OpenILS::SIP->format_date($self->{user}->expire_date);
272     syslog('LOG_DEBUG', "OILS: Patron Expire = $expire");
273     return $expire;
274 }
275
276 sub ptype {
277     my $self = shift;
278
279     my $use_code = OpenILS::SIP->get_option_value('patron_type_uses_code') || '';
280
281     # should we use the no_i18n version of patron profile name (as a 'code')?
282     return $self->{editor}->retrieve_permission_grp_tree(
283         [$self->{user}->profile->id, {no_i18n => 1}])->name
284         if $use_code =~ /true/io;
285
286     return OpenILS::SIP::clean_text($self->{user}->profile->name);
287 }
288
289 sub language {
290     my $self = shift;
291     return '000'; # Unspecified
292 }
293
294 # How much more detail do we need to check here?
295 # sec: adding logic to return false if user is barred, has a circulation block
296 # or an expired card
297 sub charge_ok {
298     my $self = shift;
299     my $u = $self->{user};
300
301     # compute expiration date for borrowing privileges
302     my $expire = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($u->expire_date));
303
304     # determine whether patron should be allowed to circulate materials:
305     # not barred, doesn't owe too much wrt fines/fees, privileges haven't
306     # expired
307     my $circ_is_blocked = 
308         (($u->barred eq 't') or
309          ($u->standing_penalties and @{$u->standing_penalties}) or
310          (CORE::time > $expire->epoch));
311
312     return
313         !$circ_is_blocked and
314         $u->active eq 't' and
315         $u->card->active eq 't';
316 }
317
318
319
320 # How much more detail do we need to check here?
321 sub renew_ok {
322     my $self = shift;
323     return $self->charge_ok;
324 }
325
326 sub recall_ok {
327     my $self = shift;
328     return $self->charge_ok if 
329         OpenILS::SIP->get_option_value('patron_calculate_recal_ok');
330     return 0;
331 }
332
333 sub hold_ok {
334     my $self = shift;
335     return $self->charge_ok;
336 }
337
338 # return true if the card provided is marked as lost
339 sub card_lost {
340     my $self = shift;
341     return $self->{user}->card->active eq 'f';
342 }
343
344 sub recall_overdue {        # not implemented
345     my $self = shift;
346     return 0;
347 }
348
349 sub check_password {
350     my ($self, $pwd) = @_;
351     syslog('LOG_DEBUG', 'OILS: Patron->check_password()');
352     return 0 unless (defined $pwd and $self->{user});
353     return md5_hex($pwd) eq $self->{user}->passwd;
354 }
355
356 sub currency {              # not really implemented
357     my $self = shift;
358     syslog('LOG_DEBUG', 'OILS: Patron->currency()');
359     return 'USD';
360 }
361
362 sub fee_amount {
363     my $self = shift;
364     syslog('LOG_DEBUG', 'OILS: Patron->fee_amount()');
365     my $user_id = $self->{user}->id;
366
367     my $e = $self->{editor};
368     $e->xact_begin;
369     my $summary = $e->retrieve_money_open_user_summary($user_id);
370     $e->rollback; # xact_rollback + disconnect
371
372     my $total = ($summary) ? $summary->balance_owed : 0;
373     syslog('LOG_INFO', "User ".$self->{id} .":$user_id has a fee amount of \$$total");
374     return $total;
375 }
376
377 sub screen_msg {
378     my $self = shift;
379     my $u = $self->{user};
380
381     return 'barred' if $u->barred eq 't';
382
383     my $b = 'blocked';
384
385     return $b if $u->active eq 'f';
386     return $b if $u->card->active eq 'f';
387
388     # if we have any penalties at this point, they are blocking penalties
389     return $b if $u->standing_penalties and @{$u->standing_penalties};
390
391     # has the patron account expired?
392     my $expire = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($u->expire_date));
393     return $b if CORE::time > $expire->epoch;
394
395     return 'OK';
396 }
397
398 sub print_line {            # not implemented
399     my $self = shift;
400     return '';
401 }
402
403 sub too_many_charged {      # not implemented
404     my $self = shift;
405     return 0;
406 }
407
408 sub too_many_overdue { 
409     my $self = shift;
410     return scalar( # PATRON_EXCEEDS_OVERDUE_COUNT
411         grep { $_->standing_penalty == 2 } @{$self->{user}->standing_penalties}
412     );
413 }
414
415 # not completely sure what this means
416 sub too_many_renewal {
417     my $self = shift;
418     return 0;
419 }
420
421 # not relevant, handled by fines/fees
422 sub too_many_claim_return {
423     my $self = shift;
424     return 0;
425 }
426
427 # not relevant, handled by fines/fees
428 sub too_many_lost {
429     my $self = shift;
430     return 0;
431 }
432
433 sub excessive_fines { 
434     my $self = shift;
435     return scalar( # PATRON_EXCEEDS_FINES
436         grep { $_->standing_penalty == 1 } @{$self->{user}->standing_penalties}
437     );
438 }
439
440 # Until someone suggests otherwise, fees and fines are the same
441
442 sub excessive_fees {
443     my $self = shift;
444     return $self->excessive_fines;
445 }
446
447 # not relevant, handled by fines/fees
448 sub too_many_billed {
449     my $self = shift;
450     return 0;
451 }
452
453
454
455 #
456 # List of outstanding holds placed
457 #
458 sub hold_items {
459     my ($self, $start, $end) = @_;
460     syslog('LOG_DEBUG', 'OILS: Patron->hold_items()');
461
462      # all of my open holds
463      my $holds = $self->{editor}->search_action_hold_request({ 
464         usr => $self->{user}->id, 
465         fulfillment_time => undef, 
466         cancel_time => undef 
467     });
468
469      return $self->__format_holds($holds, $start, $end);
470 }
471
472 sub unavail_holds {
473      my ($self, $start, $end) = @_;
474      syslog('LOG_DEBUG', 'OILS: Patron->unavail_holds()');
475
476      my $holds = $self->{editor}->search_action_hold_request({
477         usr => $self->{user}->id,
478         fulfillment_time => undef,
479         cancel_time => undef,
480         '-or' => [
481             {current_shelf_lib => undef},
482             {current_shelf_lib => {'!=' => {'+ahr' => 'pickup_lib'}}}
483         ]
484     });
485
486     return $self->__format_holds($holds, $start, $end);
487 }
488
489
490
491 sub __format_holds {
492     my ($self, $holds, $start, $end) = @_;
493
494     return [] unless @$holds;
495
496     my $return_datatype = 
497         OpenILS::SIP->get_option_value('msg64_hold_datatype') || '';
498
499     my @response;
500
501     for my $hold (@$holds) {
502
503         if ($return_datatype eq 'barcode') {
504
505             if (my $copy = $self->find_copy_for_hold($hold)) {
506                 push(@response, $copy->barcode);
507
508             } else {
509                 syslog('LOG_WARNING', 
510                     'OILS: No representative copy found for hold ' . $hold->id);
511             }
512
513         } else {
514             push(@response, 
515                 OpenILS::SIP::clean_text($self->__hold_to_title($hold)));
516         }
517     }
518
519     return (defined $start and defined $end) ? 
520         [ @holds[($start-1)..($end-1)] ] :
521         \@holds;
522 }
523
524 # Finds a representative copy for the given hold.
525 # If no copy exists at all, undef is returned.
526 # The only limit placed on what constitutes a 
527 # "representative" copy is that it cannot be deleted.
528 # Otherwise, any copy that allows us to find the hold
529 # later is good enough.
530 sub find_copy_for_hold {
531     my ($self, $hold) = @_;
532     my $e = $self->{editor};
533
534     return $e->retrieve_asset_copy($hold->current_copy)
535         if $hold->current_copy; 
536
537     return $e->retrieve_asset_copy($hold->target)
538         if $hold->hold_type =~ /C|R|F/;
539
540     return $e->search_asset_copy([
541         {call_number => $hold->target, deleted => 'f'}, 
542         {limit => 1}])->[0] if $hold->hold_type eq 'V';
543
544     my $bre_ids = [$hold->target];
545
546     if ($hold->hold_type eq 'M') {
547         # find all of the bibs that link to the target metarecord
548         my $maps = $e->search_metabib_metarecord_source_map(
549             {metarecord => $hold->target});
550         $bre_ids = [map {$_->record} @$maps];
551     }
552
553     my $vol_ids = $e->search_asset_call_number( 
554         {record => $bre_ids, deleted => 'f'}, 
555         {idlist => 1}
556     );
557
558     return $e->search_asset_copy([
559         {call_number => $vol_ids, deleted => 'f'}, 
560         {limit => 1}
561     ])->[0];
562 }
563
564 # Given a "representative" copy, finds a matching hold
565 sub find_hold_from_copy {
566     my ($self, $barcode) = @_;
567     my $e = $self->{editor};
568     my $hold;
569
570     my $copy = $e->search_asset_copy([
571         {barcode => $barcode, deleted => 'f'},
572         {flesh => 1, flesh_fields => {acp => ['call_number']}}
573     ])->[0];
574
575     return undef unless $copy;
576
577     my $run_hold_query = sub {
578         my %filter = @_;
579         return $e->search_action_hold_request([
580             {   usr => $self->{user}->id,
581                 cancel_time => undef,
582                 fulfillment_time => undef,
583                 %filter
584             }, {
585                 limit => 1,
586                 order_by => {ahr => 'request_time DESC'}
587             }
588         ])->[0];
589     };
590
591     # first see if there is a match on current_copy
592     return $hold if $hold = 
593         $run_hold_query->(current_copy => $copy->id);
594
595     # next, assume bib-level holds are the most common
596     return $hold if $hold = $run_hold_query->(
597         target => $copy->call_number->record, hold_type => 'T');
598
599     # next try metarecord holds
600     my $map = $e->search_metabib_metarecord_source_map(
601         {source => $copy->call_number->record})->[0];
602
603     return $hold if $hold = $run_hold_query->(
604         target => $map->metarecord, hold_type => 'M');
605
606     # volume holds
607     return $hold if $hold = $run_hold_query->(
608         target => $copy->call_number->id, hold_type => 'V');
609
610     # copy holds
611     return $run_hold_query->(
612         target => $copy->id, hold_type => ['C', 'F', 'R']);
613 }
614
615 sub __hold_to_title {
616     my $self = shift;
617     my $hold = shift;
618     my $e = $self->{editor};
619
620     my( $id, $mods, $title, $volume, $copy );
621
622     return __copy_to_title($e, 
623         $e->retrieve_asset_copy($hold->target)) 
624         if $hold->hold_type eq 'C' or $hold->hold_type eq 'F' or $hold->hold_type eq 'R';
625
626     return __volume_to_title($e, 
627         $e->retrieve_asset_call_number($hold->target))
628         if $hold->hold_type eq 'V';
629
630     return __record_to_title(
631         $e, $hold->target) if $hold->hold_type eq 'T';
632
633     return __metarecord_to_title(
634         $e, $hold->target) if $hold->hold_type eq 'M';
635 }
636
637 sub __copy_to_title {
638     my( $e, $copy ) = @_;
639     #syslog('LOG_DEBUG', "OILS: copy_to_title(%s)", $copy->id);
640     return $copy->dummy_title if $copy->call_number == -1;    
641
642     my $vol = (ref $copy->call_number) ?
643         $copy->call_number :
644         $e->retrieve_asset_call_number($copy->call_number);
645
646     return __volume_to_title($e, $vol);
647 }
648
649
650 sub __volume_to_title {
651     my( $e, $volume ) = @_;
652     #syslog('LOG_DEBUG', "OILS: volume_to_title(%s)", $volume->id);
653     return __record_to_title($e, $volume->record);
654 }
655
656
657 sub __record_to_title {
658     my( $e, $title_id ) = @_;
659     #syslog('LOG_DEBUG', "OILS: record_to_title($title_id)");
660     my $mods = $U->simplereq(
661         'open-ils.search',
662         'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
663     return ($mods) ? $mods->title : "";
664 }
665
666 sub __metarecord_to_title {
667     my( $e, $m_id ) = @_;
668     #syslog('LOG_DEBUG', "OILS: metarecord_to_title($m_id)");
669     my $mods = $U->simplereq(
670         'open-ils.search',
671         'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
672     return ($U->event_code($mods)) ? "<unknown>" : $mods->title;
673 }
674
675
676 #
677 # remove the hold on item item_id from my hold queue.
678 # return true if I was holding the item, false otherwise.
679
680 sub drop_hold {
681     my ($self, $item_id) = @_;
682     return 0;
683 }
684
685 sub __patron_items_info {
686     my $self = shift;
687     return if $self->{item_info};
688     $self->{item_info} = 
689         OpenILS::Application::Actor::_checked_out(
690             0, $self->{editor}, $self->{user}->id);;
691 }
692
693
694
695 sub overdue_items {
696     my ($self, $start, $end) = @_;
697
698     $self->__patron_items_info();
699     my @overdues = @{$self->{item_info}->{overdue}};
700     #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
701
702     my @o;
703     syslog('LOG_DEBUG', "OILS: overdue_items() fleshing circs @overdues");
704
705     my $return_datatype = OpenILS::SIP->get_option_value('msg64_summary_datatype') || '';
706     
707     for my $circid (@overdues) {
708         next unless $circid;
709         if($return_datatype eq 'barcode') {
710             push( @o, __circ_to_barcode($self->{editor}, $circid));
711         } else {
712             push( @o, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
713         }
714     }
715     @overdues = @o;
716
717     return (defined $start and defined $end) ? 
718         [ @overdues[($start-1)..($end-1)] ] : \@overdues;
719 }
720
721 sub __circ_to_barcode {
722     my ($e, $circ) = @_;
723     return unless $circ;
724     $circ = $e->retrieve_action_circulation($circ);
725     my $copy = $e->retrieve_asset_copy($circ->target_copy);
726     return $copy->barcode;
727 }
728
729 sub __circ_to_title {
730     my( $e, $circ ) = @_;
731     return unless $circ;
732     $circ = $e->retrieve_action_circulation($circ);
733     return __copy_to_title( $e, 
734         $e->retrieve_asset_copy($circ->target_copy) );
735 }
736
737 # force_bc -- return barcode data regardless of msg64_summary_datatype
738 sub charged_items {
739     my ($self, $start, $end, $force_bc) = shift;
740
741     $self->__patron_items_info();
742
743     my @charges = (
744         @{$self->{item_info}->{out}},
745         @{$self->{item_info}->{overdue}}
746         );
747
748     #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
749
750     my @c;
751     syslog('LOG_DEBUG', "OILS: charged_items() fleshing circs @charges");
752
753     my $return_datatype = OpenILS::SIP->get_option_value('msg64_summary_datatype') || '';
754
755     for my $circid (@charges) {
756         next unless $circid;
757         if($return_datatype eq 'barcode' or $force_bc) {
758             push( @c, __circ_to_barcode($self->{editor}, $circid));
759         } else {
760             push( @c, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
761         }
762     }
763
764     @charges = @c;
765
766     return (defined $start and defined $end) ? 
767         [ @charges[($start-1)..($end-1)] ] :
768         \@charges;
769 }
770
771 sub fine_items {
772     my ($self, $start, $end) = @_;
773     my @fines;
774     eval {
775        my $xacts = $U->simplereq('open-ils.actor', 'open-ils.actor.user.transactions.history.have_balance', $self->{authtoken}, $self->{user}->id);
776        foreach my $xact (@{$xacts}) {
777            my $line = $xact->balance_owed . " " . $xact->last_billing_type . " ";
778            if ($xact->xact_type eq 'circulation') {
779                my $mods = $U->simplereq('open-ils.circ', 'open-ils.circ.circ_transaction.find_title', $self->{authtoken}, $xact->id);
780                $line .= $mods->title . ' / ' . $mods->author;
781            } else {
782                $line .= $xact->last_billing_note;
783            }
784            push @fines, OpenILS::SIP::clean_text($line);
785        }
786     };
787     my $log_status = $@ ? 'ERROR: ' . $@ : 'OK';
788     syslog('LOG_DEBUG', 'OILS: Patron->fine_items() ' . $log_status);
789     return (defined $start and defined $end) ? 
790         [ @fines[($start-1)..($end-1)] ] : \@fines;
791 }
792
793 # not currently supported
794 sub recall_items {
795     my ($self, $start, $end) = @_;
796     return [];
797 }
798
799 sub block {
800     my ($self, $card_retained, $blocked_card_msg) = @_;
801     $blocked_card_msg ||= '';
802
803     my $e = $self->{editor};
804     my $u = $self->{user};
805
806     syslog('LOG_INFO', "OILS: Blocking user %s", $u->card->barcode );
807
808     return $self if $u->card->active eq 'f';
809
810     $e->xact_begin;    # connect and start a new transaction
811
812     $u->card->active('f');
813     if( ! $e->update_actor_card($u->card) ) {
814         syslog('LOG_ERR', "OILS: Block card update failed: %s", $e->event->{textcode});
815         $e->rollback; # rollback + disconnect
816         return $self;
817     }
818
819     # retrieve the un-fleshed user object for update
820     $u = $e->retrieve_actor_user($u->id);
821     my $note = OpenILS::SIP::clean_text($u->alert_message) || "";
822     $note = "<sip> CARD BLOCKED BY SELF-CHECK MACHINE. $blocked_card_msg</sip>\n$note"; # XXX Config option
823     $note =~ s/\s*$//;  # kill trailng whitespace
824     $u->alert_message($note);
825
826     if( ! $e->update_actor_user($u) ) {
827         syslog('LOG_ERR', "OILS: Block: patron alert update failed: %s", $e->event->{textcode});
828         $e->rollback; # rollback + disconnect
829         return $self;
830     }
831
832     # stay in synch
833     $self->{user}->alert_message( $note );
834
835     $e->commit; # commits and disconnects
836     return $self;
837 }
838
839 # Testing purposes only
840 sub enable {
841     my ($self, $card_retained) = @_;
842     $self->{screen_msg} = "All privileges restored.";
843
844     # Un-mark card as inactive, grep out the patron alert
845     my $e = $self->{editor};
846     my $u = $self->{user};
847
848     syslog('LOG_INFO', "OILS: Unblocking user %s", $u->card->barcode );
849
850     return $self if $u->card->active eq 't';
851
852     $e->xact_begin;    # connect and start a new transaction
853
854     $u->card->active('t');
855     if( ! $e->update_actor_card($u->card) ) {
856         syslog('LOG_ERR', "OILS: Unblock card update failed: %s", $e->event->{textcode});
857         $e->rollback; # rollback + disconnect
858         return $self;
859     }
860
861     # retrieve the un-fleshed user object for update
862     $u = $e->retrieve_actor_user($u->id);
863     my $note = OpenILS::SIP::clean_text($u->alert_message) || "";
864     $note =~ s#<sip>.*</sip>##;
865     $note =~ s/^\s*//;  # kill leading whitespace
866     $note =~ s/\s*$//;  # kill trailng whitespace
867     $u->alert_message($note);
868
869     if( ! $e->update_actor_user($u) ) {
870         syslog('LOG_ERR', "OILS: Unblock: patron alert update failed: %s", $e->event->{textcode});
871         $e->rollback; # rollback + disconnect
872         return $self;
873     }
874
875     # stay in synch
876     $self->{user}->alert_message( $note );
877
878     $e->commit; # commits and disconnects
879     return $self;
880 }
881
882 #
883 # Messages
884 #
885
886 sub invalid_patron {
887     return "Please contact library staff";
888 }
889
890 sub charge_denied {
891     return "Please contact library staff";
892 }
893
894 sub inet_privileges {
895     my( $self ) = @_;
896     my $e = OpenILS::SIP->editor();
897     $INET_PRIVS = $e->retrieve_all_config_net_access_level() unless $INET_PRIVS;
898     my ($level) = grep { $_->id eq $self->{user}->net_access_level } @$INET_PRIVS;
899     my $name = OpenILS::SIP::clean_text($level->name);
900     syslog('LOG_DEBUG', "OILS: Patron inet_privs = $name");
901     return $name;
902 }
903
904 sub extra_fields {
905     my( $self ) = @_;
906     my $extra_fields = {};
907     my $u = $self->{user};
908     foreach my $stat_cat_entry (@{$u->stat_cat_entries}) {
909         my $stat_cat = $stat_cat_entry->stat_cat;
910         next unless ($stat_cat->sip_field);
911         my $value = $stat_cat_entry->stat_cat_entry;
912         if(defined $stat_cat->sip_format && length($stat_cat->sip_format) > 0) { # Has a format string?
913             if($stat_cat->sip_format =~ /^\|(.*)\|$/) { # Regex match?
914                 if($value =~ /($1)/) { # If we have a match
915                     if(defined $2) { # Check to see if they embedded a capture group
916                         $value = $2; # If so, use it
917                     }
918                     else { # No embedded capture group?
919                         $value = $1; # Use our outer one
920                     }
921                 }
922                 else { # No match?
923                     $value = ''; # Empty string. Will be checked for below.
924                 }
925             }
926             else { # Not a regex match - Try sprintf match (looking for a %s, if any)
927                 $value = sprintf($stat_cat->sip_format, $value);
928             }
929         }
930         next unless length($value) > 0; # No value = no export
931         $value =~ s/\|//g; # Remove all lingering pipe chars for sane output purposes
932         $extra_fields->{ $stat_cat->sip_field } = [] unless (defined $extra_fields->{$stat_cat->sip_field});
933         push(@{$extra_fields->{ $stat_cat->sip_field}}, $value);
934     }
935     return $extra_fields;
936 }
937
938 1;