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