]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/actor.pm
6c3fc24a9fd55578743d5cc55d9f933858fbc498
[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         $sort = ['family_name','first_given_name'] unless ($$sort[0]);
231
232         # group 0 = user
233         # group 1 = address
234         # group 2 = phone, ident
235
236         my $usr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
237         my @usrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
238
239         my $addr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
240         my @addrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
241
242         my $pv = $$search{phone}{value};
243         my $iv = $$search{ident}{value};
244         my $nv = $$search{name}{value};
245
246         my $phone = '';
247         my @ps;
248         my @phonev;
249         if ($pv) {
250                 for my $p ( qw/day_phone evening_phone other_phone/ ) {
251                         push @ps, "LOWER($p) ~ ?";
252                         push @phonev, "^$pv";
253                 }
254                 $phone = '(' . join(' OR ', @ps) . ')';
255         }
256
257         my $ident = '';
258         my @is;
259         my @identv;
260         if ($iv) {
261                 for my $i ( qw/ident_value ident_value2/ ) {
262                         push @is, "LOWER($i) ~ ?";
263                         push @identv, "^$iv";
264                 }
265                 $ident = '(' . join(' OR ', @is) . ')';
266         }
267
268         my $name = '';
269         my @ns;
270         my @namev;
271         if (0 && $nv) {
272                 for my $n ( qw/first_given_name second_given_name family_name/ ) {
273                         push @ns, "LOWER($i) ~ ?";
274                         push @namev, "^$nv";
275                 }
276                 $name = '(' . join(' OR ', @ns) . ')';
277         }
278
279         my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
280         my $addr_where = $addr;
281
282
283         my $u_table = actor::user->table;
284         my $a_table = actor::user_address->table;
285
286         my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
287         my $a_select = "SELECT usr as id FROM $a_table a WHERE $addr_where";
288
289         my $select = '';
290         if ($usr_where) {
291                 if ($addr_where) {
292                         $select = "$u_select INTERSECT $a_select";
293                 } else {
294                         $select = $u_select;
295                 }
296         } elsif ($addr_where) {
297                 $select = $a_select;
298         } else {
299                 return undef;
300         }
301
302         my $order_by = join ', ', map { 'users.'. $_} @$sort;
303                 
304         $select = "SELECT users.id FROM $u_table AS users JOIN ($select) AS search USING (id) ORDER BY $order_by LIMIT $limit";
305
306         return actor::user->db_Main->selectcol_arrayref($select, {}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv));
307 }
308 __PACKAGE__->register_method(
309         api_name        => 'open-ils.storage.actor.user.crazy_search',
310         api_level       => 1,
311         method          => 'patron_search',
312 );
313
314 =comment not gonna use it...
315
316 sub fleshed_search {
317         my $self = shift;
318         my $client = shift;
319         my $searches = shift;
320
321         return undef unless (defined $searches);
322
323         for my $usr ( actor::user->search( $searches ) ) {
324                 next unless $usr;
325                 $client->respond( flesh_user( $usr ) );
326         }
327         return undef;
328 }
329 __PACKAGE__->register_method(
330         api_name        => 'open-ils.storage.fleshed.actor.user.search',
331         api_level       => 1,
332         method          => 'fleshed_search',
333         stream          => 1,
334         cachable        => 1,
335 );
336
337 sub fleshed_search_like {
338         my $self = shift;
339         my $client = shift;
340         my $searches = shift;
341
342         return undef unless (defined $searches);
343
344         for my $usr ( actor::user->search_like( $searches ) ) {
345                 next unless $usr;
346                 $client->respond( flesh_user( $usr ) );
347         }
348         return undef;
349 }
350 __PACKAGE__->register_method(
351         api_name        => 'open-ils.storage.fleshed.actor.user.search_like',
352         api_level       => 1,
353         method          => 'user_by_barcode',
354         stream          => 1,
355         cachable        => 1,
356 );
357
358 sub retrieve_fleshed_user {
359         my $self = shift;
360         my $client = shift;
361         my @ids = shift;
362
363         return undef unless @ids;
364
365         @ids = ($ids[0]) unless ($self->api_name =~ /batch/o); 
366
367         $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
368
369         return undef;
370 }
371 __PACKAGE__->register_method(
372         api_name        => 'open-ils.storage.fleshed.actor.user.retrieve',
373         api_level       => 1,
374         method          => 'retrieve_fleshed_user',
375         cachable        => 1,
376 );
377 __PACKAGE__->register_method(
378         api_name        => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
379         api_level       => 1,
380         method          => 'retrieve_fleshed_user',
381         stream          => 1,
382         cachable        => 1,
383 );
384
385 sub flesh_user {
386         my $usr = shift;
387
388
389         my $standing = $usr->standing;
390         my $profile = $usr->profile;
391         my $ident_type = $usr->ident_type;
392                 
393         my $maddress = $usr->mailing_address;
394         my $baddress = $usr->billing_address;
395         my $card = $usr->card;
396
397         my @addresses = $usr->addresses;
398         my @cards = $usr->cards;
399
400         my $usr_fm = $usr->to_fieldmapper;
401         $usr_fm->standing( $standing->to_fieldmapper );
402         $usr_fm->profile( $profile->to_fieldmapper );
403         $usr_fm->ident_type( $ident_type->to_fieldmapper );
404
405         $usr_fm->card( $card->to_fieldmapper );
406         $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
407         $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
408
409         $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
410         $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
411
412         return $usr_fm;
413 }
414
415 =cut
416
417 sub org_unit_list {
418         my $self = shift;
419         my $client = shift;
420
421         my $select =<<" SQL";
422         SELECT  *
423           FROM  actor.org_unit
424           ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
425         SQL
426
427         my $sth = actor::org_unit->db_Main->prepare_cached($select);
428         $sth->execute;
429
430         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
431
432         return undef;
433 }
434 __PACKAGE__->register_method(
435         api_name        => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
436         api_level       => 1,
437         stream          => 1,
438         method          => 'org_unit_list',
439 );
440
441 sub org_unit_type_list {
442         my $self = shift;
443         my $client = shift;
444
445         my $select =<<" SQL";
446         SELECT  *
447           FROM  actor.org_unit_type
448           ORDER BY depth, name;
449         SQL
450
451         my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
452         $sth->execute;
453
454         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
455
456         return undef;
457 }
458 __PACKAGE__->register_method(
459         api_name        => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
460         api_level       => 1,
461         stream          => 1,
462         method          => 'org_unit_type_list',
463 );
464
465 sub org_unit_full_path {
466         my $self = shift;
467         my $client = shift;
468         my @binds = @_;
469
470         return undef unless (@binds);
471
472         my $func = 'actor.org_unit_full_path(?)';
473         $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
474
475         my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
476         $sth->execute(@binds);
477
478         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
479
480         return undef;
481 }
482 __PACKAGE__->register_method(
483         api_name        => 'open-ils.storage.actor.org_unit.full_path',
484         api_level       => 1,
485         stream          => 1,
486         method          => 'org_unit_full_path',
487 );
488
489 sub org_unit_ancestors {
490         my $self = shift;
491         my $client = shift;
492         my $id = shift;
493
494         return undef unless ($id);
495
496         my $func = 'actor.org_unit_ancestors(?)';
497
498         my $sth = actor::org_unit->db_Main->prepare_cached(<<"  SQL");
499                 SELECT  f.*
500                   FROM  $func f
501                         JOIN actor.org_unit_type t ON (f.ou_type = t.id)
502                   ORDER BY t.depth, f.name;
503         SQL
504         $sth->execute(''.$id);
505
506         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
507
508         return undef;
509 }
510 __PACKAGE__->register_method(
511         api_name        => 'open-ils.storage.actor.org_unit.ancestors',
512         api_level       => 1,
513         stream          => 1,
514         method          => 'org_unit_ancestors',
515 );
516
517 sub org_unit_descendants {
518         my $self = shift;
519         my $client = shift;
520         my $id = shift;
521         my $depth = shift;
522
523         return undef unless ($id);
524
525         my $func = 'actor.org_unit_descendants(?)';
526         if (defined $depth) {
527                 $func = 'actor.org_unit_descendants(?,?)';
528         }
529
530         my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
531         $sth->execute(''.$id, ''.$depth) if (defined $depth);
532         $sth->execute(''.$id) unless (defined $depth);
533
534         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
535
536         return undef;
537 }
538 __PACKAGE__->register_method(
539         api_name        => 'open-ils.storage.actor.org_unit.descendants',
540         api_level       => 1,
541         stream          => 1,
542         method          => 'org_unit_descendants',
543 );
544
545 sub profile_all {
546         my $self = shift;
547         my $client = shift;
548
549         for my $rec ( actor::profile->retrieve_all ) {
550                 $client->respond( $rec->to_fieldmapper );
551         }
552
553         return undef;
554 }
555 __PACKAGE__->register_method(
556         method          => 'profile_all',
557         api_name        => 'open-ils.storage.direct.actor.profile.retrieve.all',
558         argc            => 0,
559         stream          => 1,
560 );
561
562 sub fleshed_actor_stat_cat {
563         my $self = shift;
564         my $client = shift;
565         my @list = @_;
566         
567         @list = ($list[0]) unless ($self->api_name =~ /batch/o);
568
569         for my $sc (@list) {
570                 my $cat = actor::stat_cat->retrieve($sc);
571                 next unless ($cat);
572
573                 my $sc_fm = $cat->to_fieldmapper;
574                 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
575
576                 $client->respond( $sc_fm );
577
578         }
579
580         return undef;
581 }
582 __PACKAGE__->register_method(
583         api_name        => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
584         api_level       => 1,
585         argc            => 1,
586         method          => 'fleshed_actor_stat_cat',
587 );
588
589 __PACKAGE__->register_method(
590         api_name        => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
591         api_level       => 1,
592         argc            => 1,
593         stream          => 1,
594         method          => 'fleshed_actor_stat_cat',
595 );
596
597 #XXX Fix stored proc calls
598 sub ranged_actor_stat_cat_all {
599         my $self = shift;
600         my $client = shift;
601         my $ou = ''.shift();
602         
603         return undef unless ($ou);
604         my $s_table = actor::stat_cat->table;
605
606         my $select = <<"        SQL";
607                 SELECT  s.*
608                   FROM  $s_table s
609                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
610                   ORDER BY name
611         SQL
612
613         $fleshed = 0;
614         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
615
616         my $sth = actor::stat_cat->db_Main->prepare_cached($select);
617         $sth->execute($ou);
618
619         for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
620                 my $sc_fm = $sc->to_fieldmapper;
621                 $sc_fm->entries(
622                         [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
623                 ) if ($fleshed);
624                 $client->respond( $sc_fm );
625         }
626
627         return undef;
628 }
629 __PACKAGE__->register_method(
630         api_name        => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
631         api_level       => 1,
632         argc            => 1,
633         stream          => 1,
634         method          => 'ranged_actor_stat_cat_all',
635 );
636
637 __PACKAGE__->register_method(
638         api_name        => 'open-ils.storage.ranged.actor.stat_cat.all',
639         api_level       => 1,
640         argc            => 1,
641         stream          => 1,
642         method          => 'ranged_actor_stat_cat_all',
643 );
644
645 #XXX Fix stored proc calls
646 sub ranged_actor_stat_cat_entry {
647         my $self = shift;
648         my $client = shift;
649         my $ou = ''.shift();
650         my $sc = ''.shift();
651         
652         return undef unless ($ou);
653         my $s_table = actor::stat_cat_entry->table;
654
655         my $select = <<"        SQL";
656                 SELECT  s.*
657                   FROM  $s_table s
658                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
659                   WHERE stat_cat = ?
660                   ORDER BY name
661         SQL
662
663         my $sth = actor::stat_cat->db_Main->prepare_cached($select);
664         $sth->execute($ou,$sc);
665
666         for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
667                 $client->respond( $sce->to_fieldmapper );
668         }
669
670         return undef;
671 }
672 __PACKAGE__->register_method(
673         api_name        => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
674         api_level       => 1,
675         stream          => 1,
676         method          => 'ranged_actor_stat_cat_entry',
677 );
678
679
680 1;