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