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