]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/actor.pm
fixing the patron clone search
[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 = "JOIN (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) AS clone USING (id)" if ($addr_where);
323
324         my $select = '';
325         if ($usr_where) {
326                 if ($addr_where) {
327                         $select = "$u_select INTERSECT $a_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                   $clone_select
351                   WHERE users.deleted = FALSE $inactive
352                   ORDER BY $order_by
353                   LIMIT $limit
354         SQL
355
356         return actor::user->db_Main->selectcol_arrayref($select, {}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv,@addrv));
357 }
358 __PACKAGE__->register_method(
359         api_name        => 'open-ils.storage.actor.user.crazy_search',
360         api_level       => 1,
361         method          => 'patron_search',
362 );
363
364 =comment not gonna use it...
365
366 sub fleshed_search {
367         my $self = shift;
368         my $client = shift;
369         my $searches = shift;
370
371         return undef unless (defined $searches);
372
373         for my $usr ( actor::user->search( $searches ) ) {
374                 next unless $usr;
375                 $client->respond( flesh_user( $usr ) );
376         }
377         return undef;
378 }
379 __PACKAGE__->register_method(
380         api_name        => 'open-ils.storage.fleshed.actor.user.search',
381         api_level       => 1,
382         method          => 'fleshed_search',
383         stream          => 1,
384         cachable        => 1,
385 );
386
387 sub fleshed_search_like {
388         my $self = shift;
389         my $client = shift;
390         my $searches = shift;
391
392         return undef unless (defined $searches);
393
394         for my $usr ( actor::user->search_like( $searches ) ) {
395                 next unless $usr;
396                 $client->respond( flesh_user( $usr ) );
397         }
398         return undef;
399 }
400 __PACKAGE__->register_method(
401         api_name        => 'open-ils.storage.fleshed.actor.user.search_like',
402         api_level       => 1,
403         method          => 'user_by_barcode',
404         stream          => 1,
405         cachable        => 1,
406 );
407
408 sub retrieve_fleshed_user {
409         my $self = shift;
410         my $client = shift;
411         my @ids = shift;
412
413         return undef unless @ids;
414
415         @ids = ($ids[0]) unless ($self->api_name =~ /batch/o); 
416
417         $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
418
419         return undef;
420 }
421 __PACKAGE__->register_method(
422         api_name        => 'open-ils.storage.fleshed.actor.user.retrieve',
423         api_level       => 1,
424         method          => 'retrieve_fleshed_user',
425         cachable        => 1,
426 );
427 __PACKAGE__->register_method(
428         api_name        => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
429         api_level       => 1,
430         method          => 'retrieve_fleshed_user',
431         stream          => 1,
432         cachable        => 1,
433 );
434
435 sub flesh_user {
436         my $usr = shift;
437
438
439         my $standing = $usr->standing;
440         my $profile = $usr->profile;
441         my $ident_type = $usr->ident_type;
442                 
443         my $maddress = $usr->mailing_address;
444         my $baddress = $usr->billing_address;
445         my $card = $usr->card;
446
447         my @addresses = $usr->addresses;
448         my @cards = $usr->cards;
449
450         my $usr_fm = $usr->to_fieldmapper;
451         $usr_fm->standing( $standing->to_fieldmapper );
452         $usr_fm->profile( $profile->to_fieldmapper );
453         $usr_fm->ident_type( $ident_type->to_fieldmapper );
454
455         $usr_fm->card( $card->to_fieldmapper );
456         $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
457         $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
458
459         $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
460         $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
461
462         return $usr_fm;
463 }
464
465 =cut
466
467 sub org_unit_list {
468         my $self = shift;
469         my $client = shift;
470
471         my $select =<<" SQL";
472         SELECT  *
473           FROM  actor.org_unit
474           ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
475         SQL
476
477         my $sth = actor::org_unit->db_Main->prepare_cached($select);
478         $sth->execute;
479
480         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
481
482         return undef;
483 }
484 __PACKAGE__->register_method(
485         api_name        => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
486         api_level       => 1,
487         stream          => 1,
488         method          => 'org_unit_list',
489 );
490
491 sub org_unit_type_list {
492         my $self = shift;
493         my $client = shift;
494
495         my $select =<<" SQL";
496         SELECT  *
497           FROM  actor.org_unit_type
498           ORDER BY depth, name;
499         SQL
500
501         my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
502         $sth->execute;
503
504         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
505
506         return undef;
507 }
508 __PACKAGE__->register_method(
509         api_name        => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
510         api_level       => 1,
511         stream          => 1,
512         method          => 'org_unit_type_list',
513 );
514
515 sub org_unit_full_path {
516         my $self = shift;
517         my $client = shift;
518         my @binds = @_;
519
520         return undef unless (@binds);
521
522         my $func = 'actor.org_unit_full_path(?)';
523         $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
524
525         my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
526         $sth->execute(@binds);
527
528         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
529
530         return undef;
531 }
532 __PACKAGE__->register_method(
533         api_name        => 'open-ils.storage.actor.org_unit.full_path',
534         api_level       => 1,
535         stream          => 1,
536         method          => 'org_unit_full_path',
537 );
538
539 sub org_unit_ancestors {
540         my $self = shift;
541         my $client = shift;
542         my $id = shift;
543
544         return undef unless ($id);
545
546         my $func = 'actor.org_unit_ancestors(?)';
547
548         my $sth = actor::org_unit->db_Main->prepare_cached(<<"  SQL");
549                 SELECT  f.*
550                   FROM  $func f
551                         JOIN actor.org_unit_type t ON (f.ou_type = t.id)
552                   ORDER BY t.depth, f.name;
553         SQL
554         $sth->execute(''.$id);
555
556         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
557
558         return undef;
559 }
560 __PACKAGE__->register_method(
561         api_name        => 'open-ils.storage.actor.org_unit.ancestors',
562         api_level       => 1,
563         stream          => 1,
564         method          => 'org_unit_ancestors',
565 );
566
567 sub org_unit_descendants {
568         my $self = shift;
569         my $client = shift;
570         my $id = shift;
571         my $depth = shift;
572
573         return undef unless ($id);
574
575         my $func = 'actor.org_unit_descendants(?)';
576         if (defined $depth) {
577                 $func = 'actor.org_unit_descendants(?,?)';
578         }
579
580         my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
581         $sth->execute(''.$id, ''.$depth) if (defined $depth);
582         $sth->execute(''.$id) unless (defined $depth);
583
584         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
585
586         return undef;
587 }
588 __PACKAGE__->register_method(
589         api_name        => 'open-ils.storage.actor.org_unit.descendants',
590         api_level       => 1,
591         stream          => 1,
592         method          => 'org_unit_descendants',
593 );
594
595 sub fleshed_actor_stat_cat {
596         my $self = shift;
597         my $client = shift;
598         my @list = @_;
599         
600         @list = ($list[0]) unless ($self->api_name =~ /batch/o);
601
602         for my $sc (@list) {
603                 my $cat = actor::stat_cat->retrieve($sc);
604                 next unless ($cat);
605
606                 my $sc_fm = $cat->to_fieldmapper;
607                 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
608
609                 $client->respond( $sc_fm );
610
611         }
612
613         return undef;
614 }
615 __PACKAGE__->register_method(
616         api_name        => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
617         api_level       => 1,
618         argc            => 1,
619         method          => 'fleshed_actor_stat_cat',
620 );
621
622 __PACKAGE__->register_method(
623         api_name        => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
624         api_level       => 1,
625         argc            => 1,
626         stream          => 1,
627         method          => 'fleshed_actor_stat_cat',
628 );
629
630 #XXX Fix stored proc calls
631 sub ranged_actor_stat_cat_all {
632         my $self = shift;
633         my $client = shift;
634         my $ou = ''.shift();
635         
636         return undef unless ($ou);
637         my $s_table = actor::stat_cat->table;
638
639         my $select = <<"        SQL";
640                 SELECT  s.*
641                   FROM  $s_table s
642                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
643                   ORDER BY name
644         SQL
645
646         $fleshed = 0;
647         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
648
649         my $sth = actor::stat_cat->db_Main->prepare_cached($select);
650         $sth->execute($ou);
651
652         for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
653                 my $sc_fm = $sc->to_fieldmapper;
654                 $sc_fm->entries(
655                         [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
656                 ) if ($fleshed);
657                 $client->respond( $sc_fm );
658         }
659
660         return undef;
661 }
662 __PACKAGE__->register_method(
663         api_name        => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
664         api_level       => 1,
665         argc            => 1,
666         stream          => 1,
667         method          => 'ranged_actor_stat_cat_all',
668 );
669
670 __PACKAGE__->register_method(
671         api_name        => 'open-ils.storage.ranged.actor.stat_cat.all',
672         api_level       => 1,
673         argc            => 1,
674         stream          => 1,
675         method          => 'ranged_actor_stat_cat_all',
676 );
677
678 #XXX Fix stored proc calls
679 sub ranged_actor_stat_cat_entry {
680         my $self = shift;
681         my $client = shift;
682         my $ou = ''.shift();
683         my $sc = ''.shift();
684         
685         return undef unless ($ou);
686         my $s_table = actor::stat_cat_entry->table;
687
688         my $select = <<"        SQL";
689                 SELECT  s.*
690                   FROM  $s_table s
691                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
692                   WHERE stat_cat = ?
693                   ORDER BY name
694         SQL
695
696         my $sth = actor::stat_cat->db_Main->prepare_cached($select);
697         $sth->execute($ou,$sc);
698
699         for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
700                 $client->respond( $sce->to_fieldmapper );
701         }
702
703         return undef;
704 }
705 __PACKAGE__->register_method(
706         api_name        => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
707         api_level       => 1,
708         stream          => 1,
709         method          => 'ranged_actor_stat_cat_entry',
710 );
711
712
713 1;