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