]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/actor.pm
ignoring inactive (by default) and deleted users
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher / actor.pm
1 package OpenILS::Application::Storage::Publisher::actor;
2 use base qw/OpenILS::Application::Storage/;
3 use OpenILS::Application::Storage::CDBI::actor;
4 use OpenSRF::Utils::Logger qw/:level/;
5 use OpenSRF::Utils qw/:datetime/;
6 use OpenILS::Utils::Fieldmapper;
7
8 use DateTime;           
9 use DateTime::Format::ISO8601;  
10                                                 
11                                                                                                 
12 my $_dt_parser = DateTime::Format::ISO8601->new;    
13
14 my $log = 'OpenSRF::Utils::Logger';
15
16 sub new_usergroup_id {
17         return actor::user->db_Main->selectrow_array("select nextval('actor.usr_usrgroup_seq'::regclass)");
18 }
19 __PACKAGE__->register_method(
20         api_name        => 'open-ils.storage.actor.user.group_id.new',
21         api_level       => 1,
22         method          => 'new_usergroup_id',
23 );
24
25 sub org_closed_overlap {
26         my $self = shift;
27         my $client = shift;
28         my $ou = shift;
29         my $date = shift;
30         my $direction = shift || 0;
31
32         return undef unless ($date && $ou);
33
34         my $t = actor::org_unit::closed_date->table;
35         my $sql = <<"   SQL";
36                 SELECT  *
37                   FROM  $t
38                   WHERE ? between close_start and close_end
39                         AND org_unit = ?
40                   ORDER BY close_start ASC, close_end DESC
41         SQL
42
43         my $sth = actor::org_unit::closed_date->db_Main->prepare( $sql );
44         $sth->execute($date, $ou);
45         
46         my ($begin, $end);
47         while (my $closure = $sth->fetchrow_hashref) {
48                 $begin ||= $closure->{close_start};
49                 $end = $closure->{close_end};
50
51                 my $before = $_dt_parser->parse_datetime( clense_ISO8601($begin) );
52                 $before->subtract( seconds => 1 );
53                 my $after = $_dt_parser->parse_datetime( clense_ISO8601($end) );
54                 $after->add( seconds => 1 );
55
56                 if ( $direction <= 0 ) {
57                         while ( my $_b = org_closed_overlap($self, $client, $ou, $before->iso8601, -1 ) ) {
58                                 $before = $_dt_parser->parse_datetime( clense_ISO8601($_b->{start}) );
59                         }
60                 }
61
62                 if ( $direction >= 0 ) {
63                         while ( my $_a = org_closed_overlap($self, $client, $ou, $after->iso8601, 1 ) ) {
64                                 $after = $_dt_parser->parse_datetime( clense_ISO8601($_a->{end}) );
65                         }
66                 }
67
68                 $begin = clense_ISO8601($before->iso8601);
69                 $end = clense_ISO8601($after->iso8601);
70         }
71
72         if ($begin && $end) {
73                 return { start => $begin, end => $end };
74         }
75
76         return;
77 }
78 __PACKAGE__->register_method(
79         api_name        => 'open-ils.storage.actor.org_unit.closed_date.overlap',
80         api_level       => 1,
81         method          => 'org_closed_overlap',
82 );
83
84 sub user_by_barcode {
85         my $self = shift;
86         my $client = shift;
87         my @barcodes = shift;
88
89         return undef unless @barcodes;
90
91         for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
92                 next unless $card;
93                 if (@barcodes == 1) {
94                         return $card->usr->to_fieldmapper;
95                 }
96                 $client->respond( $card->usr->to_fieldmapper);
97         }
98         return undef;
99 }
100 __PACKAGE__->register_method(
101         api_name        => 'open-ils.storage.direct.actor.user.search.barcode',
102         api_level       => 1,
103         method          => 'user_by_barcode',
104         stream          => 1,
105         cachable        => 1,
106 );
107
108 sub lost_barcodes {
109         my $self = shift;
110         my $client = shift;
111
112         my $c = actor::card->table;
113         my $p = actor::user->table;
114
115         my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
116
117         my $list = actor::user->db_Main->selectcol_arrayref($sql);
118         for my $bc ( @$list ) {
119                 $client->respond($bc);
120         }
121         return undef;
122 }
123 __PACKAGE__->register_method(
124         api_name        => 'open-ils.storage.actor.user.lost_barcodes',
125         api_level       => 1,
126         stream          => 1,
127         method          => 'lost_barcodes',
128         signature       => <<'  NOTE',
129                 Returns an array of barcodes that belong to lost cards.
130                 @return array of barcodes
131         NOTE
132 );
133
134 sub expired_barcodes {
135         my $self = shift;
136         my $client = shift;
137
138         my $c = actor::card->table;
139         my $p = actor::user->table;
140
141         my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
142
143         my $list = actor::user->db_Main->selectcol_arrayref($sql);
144         for my $bc ( @$list ) {
145                 $client->respond($bc);
146         }
147         return undef;
148 }
149 __PACKAGE__->register_method(
150         api_name        => 'open-ils.storage.actor.user.expired_barcodes',
151         api_level       => 1,
152         stream          => 1,
153         method          => 'expired_barcodes',
154         signature       => <<'  NOTE',
155                 Returns an array of barcodes that are currently expired.
156                 @return array of barcodes
157         NOTE
158 );
159
160 sub barred_barcodes {
161         my $self = shift;
162         my $client = shift;
163
164         my $c = actor::card->table;
165         my $p = actor::user->table;
166
167         my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
168
169         my $list = actor::user->db_Main->selectcol_arrayref($sql);
170         for my $bc ( @$list ) {
171                 $client->respond($bc);
172         }
173         return undef;
174 }
175 __PACKAGE__->register_method(
176         api_name        => 'open-ils.storage.actor.user.barred_barcodes',
177         api_level       => 1,
178         stream          => 1,
179         method          => 'barred_barcodes',
180         signature       => <<'  NOTE',
181                 Returns an array of barcodes that are currently barred.
182                 @return array of barcodes
183         NOTE
184 );
185
186 sub penalized_barcodes {
187         my $self = shift;
188         my $client = shift;
189         my @ignore = @_;
190
191         my $c = actor::card->table;
192         my $p = actor::user_standing_penalty->table;
193
194         my $sql = "SELECT c.barcode FROM $c c JOIN $p p USING (usr)";
195
196         if (@ignore) {
197                 $sql .= ' WHERE penalty_type NOT IN ('. join(',', map { '?' } @ignore) . ')';
198         }
199
200         $sql .= ' GROUP BY c.barcode;';
201
202         my $list = actor::user->db_Main->selectcol_arrayref($sql, {}, @ignore);
203         for my $bc ( @$list ) {
204                 $client->respond($bc);
205         }
206         return undef;
207 }
208 __PACKAGE__->register_method(
209         api_name        => 'open-ils.storage.actor.user.penalized_barcodes',
210         api_level       => 1,
211         stream          => 1,
212         method          => 'penalized_barcodes',
213         signature       => <<'  NOTE',
214                 Returns an array of barcodes that have penalties not listed
215                 as a parameter.  Supply a list of any penalty types that should
216                 not stop a patron from checking out materials.
217
218                 @param ignore_list Penalty type to ignore
219                 @return array of barcodes
220         NOTE
221 );
222
223
224 sub patron_search {
225         my $self = shift;
226         my $client = shift;
227         my $search = shift;
228         my $limit = shift || 1000;
229         my $sort = shift;
230         my $inactive = shift;
231         $sort = ['family_name','first_given_name'] unless ($$sort[0]);
232
233         # group 0 = user
234         # group 1 = address
235         # group 2 = phone, ident
236
237         my $usr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
238         my @usrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
239
240         my $addr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
241         my @addrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
242
243         my $pv = $$search{phone}{value};
244         my $iv = $$search{ident}{value};
245         my $nv = $$search{name}{value};
246
247         my $phone = '';
248         my @ps;
249         my @phonev;
250         if ($pv) {
251                 for my $p ( qw/day_phone evening_phone other_phone/ ) {
252                         push @ps, "LOWER($p) ~ ?";
253                         push @phonev, "^$pv";
254                 }
255                 $phone = '(' . join(' OR ', @ps) . ')';
256         }
257
258         my $ident = '';
259         my @is;
260         my @identv;
261         if ($iv) {
262                 for my $i ( qw/ident_value ident_value2/ ) {
263                         push @is, "LOWER($i) ~ ?";
264                         push @identv, "^$iv";
265                 }
266                 $ident = '(' . join(' OR ', @is) . ')';
267         }
268
269         my $name = '';
270         my @ns;
271         my @namev;
272         if (0 && $nv) {
273                 for my $n ( qw/first_given_name second_given_name family_name/ ) {
274                         push @ns, "LOWER($i) ~ ?";
275                         push @namev, "^$nv";
276                 }
277                 $name = '(' . join(' OR ', @ns) . ')';
278         }
279
280         my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
281         my $addr_where = $addr;
282
283
284         my $u_table = actor::user->table;
285         my $a_table = actor::user_address->table;
286
287         my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
288         my $a_select = "SELECT usr as id FROM $a_table a WHERE $addr_where";
289
290         my $select = '';
291         if ($usr_where) {
292                 if ($addr_where) {
293                         $select = "$u_select INTERSECT $a_select";
294                 } else {
295                         $select = $u_select;
296                 }
297         } elsif ($addr_where) {
298                 $select = $a_select;
299         } else {
300                 return undef;
301         }
302
303         my $order_by = join ', ', map { 'users.'. $_} @$sort;
304
305         if ($inactive) {
306                 $inactive = 'AND user.active = FALSE';
307         } else {
308                 $inactive = '';
309         }
310
311         $select = <<"   SQL";
312                 SELECT  users.id
313                   FROM  $u_table AS users
314                         JOIN ($select) AS search
315                   USING (id)
316                   WHERE users.deleted = FALSE $inactive
317                   ORDER BY $order_by
318                   LIMIT $limit
319         SQL
320
321         return actor::user->db_Main->selectcol_arrayref($select, {}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv));
322 }
323 __PACKAGE__->register_method(
324         api_name        => 'open-ils.storage.actor.user.crazy_search',
325         api_level       => 1,
326         method          => 'patron_search',
327 );
328
329 =comment not gonna use it...
330
331 sub fleshed_search {
332         my $self = shift;
333         my $client = shift;
334         my $searches = shift;
335
336         return undef unless (defined $searches);
337
338         for my $usr ( actor::user->search( $searches ) ) {
339                 next unless $usr;
340                 $client->respond( flesh_user( $usr ) );
341         }
342         return undef;
343 }
344 __PACKAGE__->register_method(
345         api_name        => 'open-ils.storage.fleshed.actor.user.search',
346         api_level       => 1,
347         method          => 'fleshed_search',
348         stream          => 1,
349         cachable        => 1,
350 );
351
352 sub fleshed_search_like {
353         my $self = shift;
354         my $client = shift;
355         my $searches = shift;
356
357         return undef unless (defined $searches);
358
359         for my $usr ( actor::user->search_like( $searches ) ) {
360                 next unless $usr;
361                 $client->respond( flesh_user( $usr ) );
362         }
363         return undef;
364 }
365 __PACKAGE__->register_method(
366         api_name        => 'open-ils.storage.fleshed.actor.user.search_like',
367         api_level       => 1,
368         method          => 'user_by_barcode',
369         stream          => 1,
370         cachable        => 1,
371 );
372
373 sub retrieve_fleshed_user {
374         my $self = shift;
375         my $client = shift;
376         my @ids = shift;
377
378         return undef unless @ids;
379
380         @ids = ($ids[0]) unless ($self->api_name =~ /batch/o); 
381
382         $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
383
384         return undef;
385 }
386 __PACKAGE__->register_method(
387         api_name        => 'open-ils.storage.fleshed.actor.user.retrieve',
388         api_level       => 1,
389         method          => 'retrieve_fleshed_user',
390         cachable        => 1,
391 );
392 __PACKAGE__->register_method(
393         api_name        => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
394         api_level       => 1,
395         method          => 'retrieve_fleshed_user',
396         stream          => 1,
397         cachable        => 1,
398 );
399
400 sub flesh_user {
401         my $usr = shift;
402
403
404         my $standing = $usr->standing;
405         my $profile = $usr->profile;
406         my $ident_type = $usr->ident_type;
407                 
408         my $maddress = $usr->mailing_address;
409         my $baddress = $usr->billing_address;
410         my $card = $usr->card;
411
412         my @addresses = $usr->addresses;
413         my @cards = $usr->cards;
414
415         my $usr_fm = $usr->to_fieldmapper;
416         $usr_fm->standing( $standing->to_fieldmapper );
417         $usr_fm->profile( $profile->to_fieldmapper );
418         $usr_fm->ident_type( $ident_type->to_fieldmapper );
419
420         $usr_fm->card( $card->to_fieldmapper );
421         $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
422         $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
423
424         $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
425         $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
426
427         return $usr_fm;
428 }
429
430 =cut
431
432 sub org_unit_list {
433         my $self = shift;
434         my $client = shift;
435
436         my $select =<<" SQL";
437         SELECT  *
438           FROM  actor.org_unit
439           ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
440         SQL
441
442         my $sth = actor::org_unit->db_Main->prepare_cached($select);
443         $sth->execute;
444
445         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
446
447         return undef;
448 }
449 __PACKAGE__->register_method(
450         api_name        => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
451         api_level       => 1,
452         stream          => 1,
453         method          => 'org_unit_list',
454 );
455
456 sub org_unit_type_list {
457         my $self = shift;
458         my $client = shift;
459
460         my $select =<<" SQL";
461         SELECT  *
462           FROM  actor.org_unit_type
463           ORDER BY depth, name;
464         SQL
465
466         my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
467         $sth->execute;
468
469         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
470
471         return undef;
472 }
473 __PACKAGE__->register_method(
474         api_name        => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
475         api_level       => 1,
476         stream          => 1,
477         method          => 'org_unit_type_list',
478 );
479
480 sub org_unit_full_path {
481         my $self = shift;
482         my $client = shift;
483         my @binds = @_;
484
485         return undef unless (@binds);
486
487         my $func = 'actor.org_unit_full_path(?)';
488         $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
489
490         my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
491         $sth->execute(@binds);
492
493         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
494
495         return undef;
496 }
497 __PACKAGE__->register_method(
498         api_name        => 'open-ils.storage.actor.org_unit.full_path',
499         api_level       => 1,
500         stream          => 1,
501         method          => 'org_unit_full_path',
502 );
503
504 sub org_unit_ancestors {
505         my $self = shift;
506         my $client = shift;
507         my $id = shift;
508
509         return undef unless ($id);
510
511         my $func = 'actor.org_unit_ancestors(?)';
512
513         my $sth = actor::org_unit->db_Main->prepare_cached(<<"  SQL");
514                 SELECT  f.*
515                   FROM  $func f
516                         JOIN actor.org_unit_type t ON (f.ou_type = t.id)
517                   ORDER BY t.depth, f.name;
518         SQL
519         $sth->execute(''.$id);
520
521         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
522
523         return undef;
524 }
525 __PACKAGE__->register_method(
526         api_name        => 'open-ils.storage.actor.org_unit.ancestors',
527         api_level       => 1,
528         stream          => 1,
529         method          => 'org_unit_ancestors',
530 );
531
532 sub org_unit_descendants {
533         my $self = shift;
534         my $client = shift;
535         my $id = shift;
536         my $depth = shift;
537
538         return undef unless ($id);
539
540         my $func = 'actor.org_unit_descendants(?)';
541         if (defined $depth) {
542                 $func = 'actor.org_unit_descendants(?,?)';
543         }
544
545         my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
546         $sth->execute(''.$id, ''.$depth) if (defined $depth);
547         $sth->execute(''.$id) unless (defined $depth);
548
549         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
550
551         return undef;
552 }
553 __PACKAGE__->register_method(
554         api_name        => 'open-ils.storage.actor.org_unit.descendants',
555         api_level       => 1,
556         stream          => 1,
557         method          => 'org_unit_descendants',
558 );
559
560 sub profile_all {
561         my $self = shift;
562         my $client = shift;
563
564         for my $rec ( actor::profile->retrieve_all ) {
565                 $client->respond( $rec->to_fieldmapper );
566         }
567
568         return undef;
569 }
570 __PACKAGE__->register_method(
571         method          => 'profile_all',
572         api_name        => 'open-ils.storage.direct.actor.profile.retrieve.all',
573         argc            => 0,
574         stream          => 1,
575 );
576
577 sub fleshed_actor_stat_cat {
578         my $self = shift;
579         my $client = shift;
580         my @list = @_;
581         
582         @list = ($list[0]) unless ($self->api_name =~ /batch/o);
583
584         for my $sc (@list) {
585                 my $cat = actor::stat_cat->retrieve($sc);
586                 next unless ($cat);
587
588                 my $sc_fm = $cat->to_fieldmapper;
589                 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
590
591                 $client->respond( $sc_fm );
592
593         }
594
595         return undef;
596 }
597 __PACKAGE__->register_method(
598         api_name        => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
599         api_level       => 1,
600         argc            => 1,
601         method          => 'fleshed_actor_stat_cat',
602 );
603
604 __PACKAGE__->register_method(
605         api_name        => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
606         api_level       => 1,
607         argc            => 1,
608         stream          => 1,
609         method          => 'fleshed_actor_stat_cat',
610 );
611
612 #XXX Fix stored proc calls
613 sub ranged_actor_stat_cat_all {
614         my $self = shift;
615         my $client = shift;
616         my $ou = ''.shift();
617         
618         return undef unless ($ou);
619         my $s_table = actor::stat_cat->table;
620
621         my $select = <<"        SQL";
622                 SELECT  s.*
623                   FROM  $s_table s
624                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
625                   ORDER BY name
626         SQL
627
628         $fleshed = 0;
629         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
630
631         my $sth = actor::stat_cat->db_Main->prepare_cached($select);
632         $sth->execute($ou);
633
634         for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
635                 my $sc_fm = $sc->to_fieldmapper;
636                 $sc_fm->entries(
637                         [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
638                 ) if ($fleshed);
639                 $client->respond( $sc_fm );
640         }
641
642         return undef;
643 }
644 __PACKAGE__->register_method(
645         api_name        => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
646         api_level       => 1,
647         argc            => 1,
648         stream          => 1,
649         method          => 'ranged_actor_stat_cat_all',
650 );
651
652 __PACKAGE__->register_method(
653         api_name        => 'open-ils.storage.ranged.actor.stat_cat.all',
654         api_level       => 1,
655         argc            => 1,
656         stream          => 1,
657         method          => 'ranged_actor_stat_cat_all',
658 );
659
660 #XXX Fix stored proc calls
661 sub ranged_actor_stat_cat_entry {
662         my $self = shift;
663         my $client = shift;
664         my $ou = ''.shift();
665         my $sc = ''.shift();
666         
667         return undef unless ($ou);
668         my $s_table = actor::stat_cat_entry->table;
669
670         my $select = <<"        SQL";
671                 SELECT  s.*
672                   FROM  $s_table s
673                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
674                   WHERE stat_cat = ?
675                   ORDER BY name
676         SQL
677
678         my $sth = actor::stat_cat->db_Main->prepare_cached($select);
679         $sth->execute($ou,$sc);
680
681         for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
682                 $client->respond( $sce->to_fieldmapper );
683         }
684
685         return undef;
686 }
687 __PACKAGE__->register_method(
688         api_name        => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
689         api_level       => 1,
690         stream          => 1,
691         method          => 'ranged_actor_stat_cat_entry',
692 );
693
694
695 1;