]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/actor.pm
include closed days (hours of operation) in due date calculation
[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 = ? AND x.xact_finish IS NULL
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         my $no_hoo = shift || 0;
181
182         return undef unless ($date && $ou);
183
184         my $t = actor::org_unit::closed_date->table;
185         my $sql = <<"   SQL";
186                 SELECT  *
187                   FROM  $t
188                   WHERE ? between close_start and close_end
189                         AND org_unit = ?
190                   ORDER BY close_start ASC, close_end DESC
191         SQL
192
193         my $sth = actor::org_unit::closed_date->db_Main->prepare( $sql );
194         $sth->execute($date, $ou);
195
196         $date = clense_ISO8601($date);
197         
198         my ($begin, $end);
199         while (my $closure = $sth->fetchrow_hashref) {
200                 $begin ||= $closure->{close_start};
201                 $end = $closure->{close_end};
202
203                 my $before = $_dt_parser->parse_datetime( clense_ISO8601($begin) );
204                 $before->subtract( seconds => 1 );
205                 my $after = $_dt_parser->parse_datetime( clense_ISO8601($end) );
206                 $after->add( seconds => 1 );
207
208                 if ( $direction <= 0 ) {
209                         while ( my $_b = org_closed_overlap($self, $client, $ou, $before->iso8601, -1, 1 ) ) {
210                                 $before = $_dt_parser->parse_datetime( clense_ISO8601($_b->{start}) );
211                         }
212                 }
213
214                 if ( $direction >= 0 ) {
215                         while ( my $_a = org_closed_overlap($self, $client, $ou, $after->iso8601, 1, 1 ) ) {
216                                 $after = $_dt_parser->parse_datetime( clense_ISO8601($_a->{end}) );
217                         }
218                 }
219
220                 $begin = clense_ISO8601($before->iso8601);
221                 $end = clense_ISO8601($after->iso8601);
222         }
223
224         $begin ||= $date;
225         $end ||= $date;
226
227
228         if ( !$no_hoo ) {
229                 if ( my $hoo = actor::org_unit::hours_of_operation->retrieve($ou) ) {
230
231                         my $begin_dow = $_dt_parser->parse_datetime( $begin )->day_of_week_0;
232                         my $begin_open_meth = "dow_".$begin_dow."_open";
233                         my $begin_close_meth = "dow_".$begin_dow."_close";
234
235                         my $count = 1;
236                         while ($hoo->$begin_open_meth eq '00:00:00' and $hoo->$begin_close_meth eq '00:00:00') {
237                                 $begin = clense_ISO8601($_dt_parser->parse_datetime( $begin )->subtract( days => 1)->iso8601);
238                                 $begin_dow++;
239                                 $begin_dow %= 7;
240                                 $count++;
241                                 last if ($count > 6);
242                                 $begin_open_meth = "dow_".$begin_dow."_open";
243                                 $begin_close_meth = "dow_".$begin_dow."_close";
244                         }
245         
246                         my $end_dow = $_dt_parser->parse_datetime( $end )->day_of_week_0;
247                         my $end_open_meth = "dow_".$end_dow."_open";
248                         my $end_close_meth = "dow_".$end_dow."_close";
249         
250                         $count = 1;
251                         while ($hoo->$end_open_meth eq '00:00:00' and $hoo->$end_close_meth eq '00:00:00') {
252                                 $end = clense_ISO8601($_dt_parser->parse_datetime( $end )->add( days => 1)->iso8601);
253                                 $end_dow++;
254                                 $end_dow %= 7;
255                                 $count++;
256                                 last if ($count > 6);
257                                 $end_open_meth = "dow_".$end_dow."_open";
258                                 $end_close_meth = "dow_".$end_dow."_close";
259                         }
260                 }
261         }
262
263         if ($begin eq $date && $end eq $date) {
264                 return undef;
265         }
266
267         return { start => $begin, end => $end };
268 }
269 __PACKAGE__->register_method(
270         api_name        => 'open-ils.storage.actor.org_unit.closed_date.overlap',
271         api_level       => 1,
272         method          => 'org_closed_overlap',
273 );
274
275 sub user_by_barcode {
276         my $self = shift;
277         my $client = shift;
278         my @barcodes = shift;
279
280         return undef unless @barcodes;
281
282         for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
283                 next unless $card;
284                 if (@barcodes == 1) {
285                         return $card->usr->to_fieldmapper;
286                 }
287                 $client->respond( $card->usr->to_fieldmapper);
288         }
289         return undef;
290 }
291 __PACKAGE__->register_method(
292         api_name        => 'open-ils.storage.direct.actor.user.search.barcode',
293         api_level       => 1,
294         method          => 'user_by_barcode',
295         stream          => 1,
296         cachable        => 1,
297 );
298
299 sub lost_barcodes {
300         my $self = shift;
301         my $client = shift;
302
303         my $c = actor::card->table;
304         my $p = actor::user->table;
305
306         my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
307
308         my $list = actor::user->db_Main->selectcol_arrayref($sql);
309         for my $bc ( @$list ) {
310                 $client->respond($bc);
311         }
312         return undef;
313 }
314 __PACKAGE__->register_method(
315         api_name        => 'open-ils.storage.actor.user.lost_barcodes',
316         api_level       => 1,
317         stream          => 1,
318         method          => 'lost_barcodes',
319         signature       => <<'  NOTE',
320                 Returns an array of barcodes that belong to lost cards.
321                 @return array of barcodes
322         NOTE
323 );
324
325 sub expired_barcodes {
326         my $self = shift;
327         my $client = shift;
328
329         my $c = actor::card->table;
330         my $p = actor::user->table;
331
332         my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
333
334         my $list = actor::user->db_Main->selectcol_arrayref($sql);
335         for my $bc ( @$list ) {
336                 $client->respond($bc);
337         }
338         return undef;
339 }
340 __PACKAGE__->register_method(
341         api_name        => 'open-ils.storage.actor.user.expired_barcodes',
342         api_level       => 1,
343         stream          => 1,
344         method          => 'expired_barcodes',
345         signature       => <<'  NOTE',
346                 Returns an array of barcodes that are currently expired.
347                 @return array of barcodes
348         NOTE
349 );
350
351 sub barred_barcodes {
352         my $self = shift;
353         my $client = shift;
354
355         my $c = actor::card->table;
356         my $p = actor::user->table;
357
358         my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
359
360         my $list = actor::user->db_Main->selectcol_arrayref($sql);
361         for my $bc ( @$list ) {
362                 $client->respond($bc);
363         }
364         return undef;
365 }
366 __PACKAGE__->register_method(
367         api_name        => 'open-ils.storage.actor.user.barred_barcodes',
368         api_level       => 1,
369         stream          => 1,
370         method          => 'barred_barcodes',
371         signature       => <<'  NOTE',
372                 Returns an array of barcodes that are currently barred.
373                 @return array of barcodes
374         NOTE
375 );
376
377 sub penalized_barcodes {
378         my $self = shift;
379         my $client = shift;
380         my @ignore = @_;
381
382         my $c = actor::card->table;
383         my $p = actor::user_standing_penalty->table;
384
385         my $sql = "SELECT c.barcode FROM $c c JOIN $p p USING (usr)";
386
387         if (@ignore) {
388                 $sql .= ' WHERE penalty_type NOT IN ('. join(',', map { '?' } @ignore) . ')';
389         }
390
391         $sql .= ' GROUP BY c.barcode;';
392
393         my $list = actor::user->db_Main->selectcol_arrayref($sql, {}, @ignore);
394         for my $bc ( @$list ) {
395                 $client->respond($bc);
396         }
397         return undef;
398 }
399 __PACKAGE__->register_method(
400         api_name        => 'open-ils.storage.actor.user.penalized_barcodes',
401         api_level       => 1,
402         stream          => 1,
403         method          => 'penalized_barcodes',
404         signature       => <<'  NOTE',
405                 Returns an array of barcodes that have penalties not listed
406                 as a parameter.  Supply a list of any penalty types that should
407                 not stop a patron from checking out materials.
408
409                 @param ignore_list Penalty type to ignore
410                 @return array of barcodes
411         NOTE
412 );
413
414
415 sub patron_search {
416         my $self = shift;
417         my $client = shift;
418         my $search = shift;
419         my $limit = shift || 1000;
420         my $sort = shift;
421         my $inactive = shift;
422         $sort = ['family_name','first_given_name'] unless ($$sort[0]);
423
424         # group 0 = user
425         # group 1 = address
426         # group 2 = phone, ident
427
428         my $usr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
429         my @usrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
430
431         my $addr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
432         my @addrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
433
434         my $pv = $$search{phone}{value};
435         my $iv = $$search{ident}{value};
436         my $nv = $$search{name}{value};
437
438         my $phone = '';
439         my @ps;
440         my @phonev;
441         if ($pv) {
442                 for my $p ( qw/day_phone evening_phone other_phone/ ) {
443                         push @ps, "LOWER($p) ~ ?";
444                         push @phonev, "^$pv";
445                 }
446                 $phone = '(' . join(' OR ', @ps) . ')';
447         }
448
449         my $ident = '';
450         my @is;
451         my @identv;
452         if ($iv) {
453                 for my $i ( qw/ident_value ident_value2/ ) {
454                         push @is, "LOWER($i) ~ ?";
455                         push @identv, "^$iv";
456                 }
457                 $ident = '(' . join(' OR ', @is) . ')';
458         }
459
460         my $name = '';
461         my @ns;
462         my @namev;
463         if (0 && $nv) {
464                 for my $n ( qw/first_given_name second_given_name family_name/ ) {
465                         push @ns, "LOWER($i) ~ ?";
466                         push @namev, "^$nv";
467                 }
468                 $name = '(' . join(' OR ', @ns) . ')';
469         }
470
471         my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
472         my $addr_where = $addr;
473
474
475         my $u_table = actor::user->table;
476         my $a_table = actor::user_address->table;
477
478         my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
479         my $a_select = "SELECT usr as id FROM $a_table a WHERE $addr_where";
480         my $clone_select = '';
481         $clone_select = "JOIN (SELECT cu.id as id FROM $a_table ca ".
482                            "JOIN $u_table cu ON (cu.mailing_address = ca.id OR cu.billing_address = ca.id) ".
483                            "WHERE $addr_where) AS clone USING (id)" if ($addr_where);
484
485         my $select = '';
486         if ($usr_where) {
487                 if ($addr_where) {
488                         $select = "$u_select INTERSECT $a_select";
489                 } else {
490                         $select = $u_select;
491                 }
492         } elsif ($addr_where) {
493                 $select = "$a_select";
494         } else {
495                 return undef;
496         }
497
498         my $order_by = join ', ', map { 'users.'. $_} @$sort;
499
500         if ($inactive) {
501                 $inactive = '';
502         } else {
503                 $inactive = 'AND users.active = TRUE';
504         }
505
506         $select = <<"   SQL";
507                 SELECT  users.id
508                   FROM  $u_table AS users
509                         JOIN ($select) AS search
510                   USING (id)
511                   $clone_select
512                   WHERE users.deleted = FALSE $inactive
513                   ORDER BY $order_by
514                   LIMIT $limit
515         SQL
516
517         return actor::user->db_Main->selectcol_arrayref($select, {}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv,@addrv));
518 }
519 __PACKAGE__->register_method(
520         api_name        => 'open-ils.storage.actor.user.crazy_search',
521         api_level       => 1,
522         method          => 'patron_search',
523 );
524
525 =comment not gonna use it...
526
527 sub fleshed_search {
528         my $self = shift;
529         my $client = shift;
530         my $searches = shift;
531
532         return undef unless (defined $searches);
533
534         for my $usr ( actor::user->search( $searches ) ) {
535                 next unless $usr;
536                 $client->respond( flesh_user( $usr ) );
537         }
538         return undef;
539 }
540 __PACKAGE__->register_method(
541         api_name        => 'open-ils.storage.fleshed.actor.user.search',
542         api_level       => 1,
543         method          => 'fleshed_search',
544         stream          => 1,
545         cachable        => 1,
546 );
547
548 sub fleshed_search_like {
549         my $self = shift;
550         my $client = shift;
551         my $searches = shift;
552
553         return undef unless (defined $searches);
554
555         for my $usr ( actor::user->search_like( $searches ) ) {
556                 next unless $usr;
557                 $client->respond( flesh_user( $usr ) );
558         }
559         return undef;
560 }
561 __PACKAGE__->register_method(
562         api_name        => 'open-ils.storage.fleshed.actor.user.search_like',
563         api_level       => 1,
564         method          => 'user_by_barcode',
565         stream          => 1,
566         cachable        => 1,
567 );
568
569 sub retrieve_fleshed_user {
570         my $self = shift;
571         my $client = shift;
572         my @ids = shift;
573
574         return undef unless @ids;
575
576         @ids = ($ids[0]) unless ($self->api_name =~ /batch/o); 
577
578         $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
579
580         return undef;
581 }
582 __PACKAGE__->register_method(
583         api_name        => 'open-ils.storage.fleshed.actor.user.retrieve',
584         api_level       => 1,
585         method          => 'retrieve_fleshed_user',
586         cachable        => 1,
587 );
588 __PACKAGE__->register_method(
589         api_name        => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
590         api_level       => 1,
591         method          => 'retrieve_fleshed_user',
592         stream          => 1,
593         cachable        => 1,
594 );
595
596 sub flesh_user {
597         my $usr = shift;
598
599
600         my $standing = $usr->standing;
601         my $profile = $usr->profile;
602         my $ident_type = $usr->ident_type;
603                 
604         my $maddress = $usr->mailing_address;
605         my $baddress = $usr->billing_address;
606         my $card = $usr->card;
607
608         my @addresses = $usr->addresses;
609         my @cards = $usr->cards;
610
611         my $usr_fm = $usr->to_fieldmapper;
612         $usr_fm->standing( $standing->to_fieldmapper );
613         $usr_fm->profile( $profile->to_fieldmapper );
614         $usr_fm->ident_type( $ident_type->to_fieldmapper );
615
616         $usr_fm->card( $card->to_fieldmapper );
617         $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
618         $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
619
620         $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
621         $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
622
623         return $usr_fm;
624 }
625
626 =cut
627
628 sub org_unit_list {
629         my $self = shift;
630         my $client = shift;
631
632         my $select =<<" SQL";
633         SELECT  *
634           FROM  actor.org_unit
635           ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
636         SQL
637
638         my $sth = actor::org_unit->db_Main->prepare_cached($select);
639         $sth->execute;
640
641         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
642
643         return undef;
644 }
645 __PACKAGE__->register_method(
646         api_name        => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
647         api_level       => 1,
648         stream          => 1,
649         method          => 'org_unit_list',
650 );
651
652 sub org_unit_type_list {
653         my $self = shift;
654         my $client = shift;
655
656         my $select =<<" SQL";
657         SELECT  *
658           FROM  actor.org_unit_type
659           ORDER BY depth, name;
660         SQL
661
662         my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
663         $sth->execute;
664
665         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
666
667         return undef;
668 }
669 __PACKAGE__->register_method(
670         api_name        => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
671         api_level       => 1,
672         stream          => 1,
673         method          => 'org_unit_type_list',
674 );
675
676 sub org_unit_full_path {
677         my $self = shift;
678         my $client = shift;
679         my @binds = @_;
680
681         return undef unless (@binds);
682
683         my $func = 'actor.org_unit_full_path(?)';
684         $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
685
686         my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
687         $sth->execute(@binds);
688
689         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
690
691         return undef;
692 }
693 __PACKAGE__->register_method(
694         api_name        => 'open-ils.storage.actor.org_unit.full_path',
695         api_level       => 1,
696         stream          => 1,
697         method          => 'org_unit_full_path',
698 );
699
700 sub org_unit_ancestors {
701         my $self = shift;
702         my $client = shift;
703         my $id = shift;
704
705         return undef unless ($id);
706
707         my $func = 'actor.org_unit_ancestors(?)';
708
709         my $sth = actor::org_unit->db_Main->prepare_cached(<<"  SQL");
710                 SELECT  f.*
711                   FROM  $func f
712                         JOIN actor.org_unit_type t ON (f.ou_type = t.id)
713                   ORDER BY t.depth, f.name;
714         SQL
715         $sth->execute(''.$id);
716
717         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
718
719         return undef;
720 }
721 __PACKAGE__->register_method(
722         api_name        => 'open-ils.storage.actor.org_unit.ancestors',
723         api_level       => 1,
724         stream          => 1,
725         method          => 'org_unit_ancestors',
726 );
727
728 sub org_unit_descendants {
729         my $self = shift;
730         my $client = shift;
731         my $id = shift;
732         my $depth = shift;
733
734         return undef unless ($id);
735
736         my $func = 'actor.org_unit_descendants(?)';
737         if (defined $depth) {
738                 $func = 'actor.org_unit_descendants(?,?)';
739         }
740
741         my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
742         $sth->execute(''.$id, ''.$depth) if (defined $depth);
743         $sth->execute(''.$id) unless (defined $depth);
744
745         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
746
747         return undef;
748 }
749 __PACKAGE__->register_method(
750         api_name        => 'open-ils.storage.actor.org_unit.descendants',
751         api_level       => 1,
752         stream          => 1,
753         method          => 'org_unit_descendants',
754 );
755
756 sub fleshed_actor_stat_cat {
757         my $self = shift;
758         my $client = shift;
759         my @list = @_;
760         
761         @list = ($list[0]) unless ($self->api_name =~ /batch/o);
762
763         for my $sc (@list) {
764                 my $cat = actor::stat_cat->retrieve($sc);
765                 next unless ($cat);
766
767                 my $sc_fm = $cat->to_fieldmapper;
768                 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
769
770                 $client->respond( $sc_fm );
771
772         }
773
774         return undef;
775 }
776 __PACKAGE__->register_method(
777         api_name        => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
778         api_level       => 1,
779         argc            => 1,
780         method          => 'fleshed_actor_stat_cat',
781 );
782
783 __PACKAGE__->register_method(
784         api_name        => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
785         api_level       => 1,
786         argc            => 1,
787         stream          => 1,
788         method          => 'fleshed_actor_stat_cat',
789 );
790
791 #XXX Fix stored proc calls
792 sub ranged_actor_stat_cat_all {
793         my $self = shift;
794         my $client = shift;
795         my $ou = ''.shift();
796         
797         return undef unless ($ou);
798         my $s_table = actor::stat_cat->table;
799
800         my $select = <<"        SQL";
801                 SELECT  s.*
802                   FROM  $s_table s
803                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
804                   ORDER BY name
805         SQL
806
807         $fleshed = 0;
808         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
809
810         my $sth = actor::stat_cat->db_Main->prepare_cached($select);
811         $sth->execute($ou);
812
813         for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
814                 my $sc_fm = $sc->to_fieldmapper;
815                 $sc_fm->entries(
816                         [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
817                 ) if ($fleshed);
818                 $client->respond( $sc_fm );
819         }
820
821         return undef;
822 }
823 __PACKAGE__->register_method(
824         api_name        => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
825         api_level       => 1,
826         argc            => 1,
827         stream          => 1,
828         method          => 'ranged_actor_stat_cat_all',
829 );
830
831 __PACKAGE__->register_method(
832         api_name        => 'open-ils.storage.ranged.actor.stat_cat.all',
833         api_level       => 1,
834         argc            => 1,
835         stream          => 1,
836         method          => 'ranged_actor_stat_cat_all',
837 );
838
839 #XXX Fix stored proc calls
840 sub ranged_actor_stat_cat_entry {
841         my $self = shift;
842         my $client = shift;
843         my $ou = ''.shift();
844         my $sc = ''.shift();
845         
846         return undef unless ($ou);
847         my $s_table = actor::stat_cat_entry->table;
848
849         my $select = <<"        SQL";
850                 SELECT  s.*
851                   FROM  $s_table s
852                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
853                   WHERE stat_cat = ?
854                   ORDER BY name
855         SQL
856
857         my $sth = actor::stat_cat->db_Main->prepare_cached($select);
858         $sth->execute($ou,$sc);
859
860         for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
861                 $client->respond( $sce->to_fieldmapper );
862         }
863
864         return undef;
865 }
866 __PACKAGE__->register_method(
867         api_name        => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
868         api_level       => 1,
869         stream          => 1,
870         method          => 'ranged_actor_stat_cat_entry',
871 );
872
873
874 1;