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