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