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 use OpenSRF::Utils::SettingsClient;
10 use DateTime::Format::ISO8601;
12 use DateTime::SpanSet;
14 my $_dt_parser = DateTime::Format::ISO8601->new;
16 my $log = 'OpenSRF::Utils::Logger';
18 sub new_usergroup_id {
19 return actor::user->db_Main->selectrow_array("select nextval('actor.usr_usrgroup_seq'::regclass)");
21 __PACKAGE__->register_method(
22 api_name => 'open-ils.storage.actor.user.group_id.new',
24 method => 'new_usergroup_id',
30 my $adult_age = shift;
35 WHERE AGE(dob) > ?::INTERVAL;
38 my $sth = actor::user->db_Main->prepare_cached($sql);
39 $sth->execute($adult_age);
43 __PACKAGE__->register_method(
44 api_name => 'open-ils.storage.actor.user.juvenile_to_adult',
46 method => 'juv_to_adult',
56 SUM(COALESCE((SELECT SUM(b.amount) FROM money.billing b WHERE b.voided IS FALSE AND b.xact = x.id),0.0)) -
57 SUM(COALESCE((SELECT SUM(p.amount) FROM money.payment p WHERE p.voided IS FALSE AND p.xact = x.id),0.0))
58 FROM money.billable_xact x
59 WHERE x.usr = ? AND x.xact_finish IS NULL
63 my (undef,$val) = actor::user->db_Main->selectrow_array($sql, {}, $usr);
67 __PACKAGE__->register_method(
68 api_name => 'open-ils.storage.actor.user.total_owed',
70 method => 'usr_total_owed',
73 sub usr_breakdown_out {
78 $self->method_lookup('open-ils.storage.transaction.begin')->run($client);
80 my $out_sql = <<" SQL";
82 FROM action.circulation
84 AND checkin_time IS NULL
85 AND ( (fine_interval >= '1 day' AND due_date >= 'today')
86 OR (fine_interval < '1 day' AND due_date > 'now' ))
87 AND (stop_fines IS NULL
88 OR stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE'))
91 my $out = actor::user->db_Main->selectcol_arrayref($out_sql, {}, $usr);
93 my $od_sql = <<" SQL";
95 FROM action.circulation
97 AND checkin_time IS NULL
98 AND ( (fine_interval >= '1 day' AND due_date < 'today')
99 OR (fine_interval < '1 day' AND due_date < 'now' ))
100 AND (stop_fines IS NULL
101 OR stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE'))
104 my $od = actor::user->db_Main->selectcol_arrayref($od_sql, {}, $usr);
106 my $lost_sql = <<" SQL";
108 FROM action.circulation
109 WHERE usr = ? AND checkin_time IS NULL AND xact_finish IS NULL AND stop_fines = 'LOST'
112 my $lost = actor::user->db_Main->selectcol_arrayref($lost_sql, {}, $usr);
114 my $cl_sql = <<" SQL";
116 FROM action.circulation
117 WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'CLAIMSRETURNED'
120 my $cl = actor::user->db_Main->selectcol_arrayref($cl_sql, {}, $usr);
122 my $lo_sql = <<" SQL";
124 FROM action.circulation
125 WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'LONGOVERDUE'
128 my $lo = actor::user->db_Main->selectcol_arrayref($lo_sql, {}, $usr);
130 $self->method_lookup('open-ils.storage.transaction.rollback')->run($client);
132 if ($self->api_name =~/count$/o) {
133 return { total => scalar(@$out) + scalar(@$od) + scalar(@$lost) + scalar(@$cl) + scalar(@$lo),
134 out => scalar(@$out),
135 overdue => scalar(@$od),
136 lost => scalar(@$lost),
137 claims_returned => scalar(@$cl),
138 long_overdue => scalar(@$lo),
142 return { out => $out,
145 claims_returned => $cl,
149 __PACKAGE__->register_method(
150 api_name => 'open-ils.storage.actor.user.checked_out',
152 method => 'usr_breakdown_out',
154 __PACKAGE__->register_method(
155 api_name => 'open-ils.storage.actor.user.checked_out.count',
157 method => 'usr_breakdown_out',
167 FROM action.circulation
168 WHERE usr = ? AND checkin_time IS NULL
171 my ($val) = actor::user->db_Main->selectrow_array($sql, {}, $usr);
175 __PACKAGE__->register_method(
176 api_name => 'open-ils.storage.actor.user.total_out',
178 method => 'usr_total_out',
185 local $OpenILS::Application::Storage::WRITE = 1;
187 my $delete_sql = <<" SQL";
188 DELETE FROM actor.org_unit_proximity;
191 my $insert_sql = <<" SQL";
192 INSERT INTO actor.org_unit_proximity (from_org, to_org, prox)
195 actor.org_unit_proximity(l.id,r.id)
196 FROM actor.org_unit l,
200 actor::org_unit_proximity->db_Main->do($delete_sql);
201 actor::org_unit_proximity->db_Main->do($insert_sql);
205 __PACKAGE__->register_method(
206 api_name => 'open-ils.storage.actor.org_unit.refresh_proximity',
208 method => 'calc_proximity',
211 sub make_hoo_spanset {
213 return undef unless $hoo;
215 my $today = shift || DateTime->now;
217 my $tz = OpenSRF::AppSession->create('open-ils.actor')->request(
218 'open-ils.actor.ou_setting.ancestor_default' => $hoo->id.'' => 'org_unit.timezone'
219 )->gather(1) || DateTime::TimeZone->new( name => 'local' )->name;
221 my $current_dow = $today->day_of_week_0;
223 my $spanset = DateTime::SpanSet->empty_set;
224 for my $d ( 0 .. 6 ) {
226 my $omethod = 'dow_'.$d.'_open';
227 my $cmethod = 'dow_'.$d.'_close';
229 my $open = interval_to_seconds($hoo->$omethod());
230 my $close = interval_to_seconds($hoo->$cmethod());
232 next if ($open == $close && $open == 0);
234 my $dow_offset = ($d - $current_dow) * $one_day;
235 $close += $one_day if ($close <= $open);
237 $spanset = $spanset->union(
239 start => $today->clone->add( seconds => $dow_offset + $open ),
240 end => $today->clone->add( seconds => $dow_offset + $close )
245 return $spanset->complement;
248 sub make_closure_spanset {
249 my $closures = shift;
250 return undef unless $closures;
252 my $spanset = DateTime::SpanSet->empty_set;
253 for my $k ( keys %$closures ) {
254 my $c = $$closures{$k};
256 $spanset = $spanset->union(
258 start => $_dt_parser->parse_datetime(cleanse_ISO8601($c->{close_start})),
259 end => $_dt_parser->parse_datetime(cleanse_ISO8601($c->{close_end}))
267 sub new_org_closed_overlap {
272 my $direction = shift || 0;
273 my $no_hoo = shift || 0;
275 return undef unless ($date && $ou);
277 # we're given a date and a direction, find any closures that contain the date
278 my $t = actor::org_unit::closed_date->table;
284 ORDER BY close_start ASC, close_end DESC
288 $date = cleanse_ISO8601($date);
290 my $target_date = $_dt_parser->parse_datetime( $date );
291 my ($begin, $end) = ($target_date, $target_date);
293 # create a spanset from the closures that contain the $date
294 my $closure_spanset = make_closure_spanset(
295 actor::org_unit::closed_date->db_Main->selectall_hashref( $sql, 'id', {}, $date, $ou )
298 if ($closure_spanset && $closure_spanset->intersects( $target_date )) {
299 my $closure_intersection = $closure_spanset->intersection( $target_date );
300 $begin = $closure_intersection->min;
301 $end = $closure_intersection->max;
303 if ( $direction <= 0 ) {
304 $begin->subtract( minutes => 1 );
306 while ( my $_b = new_org_closed_overlap($self, $client, $ou, $begin->strftime('%FT%T%z'), -1, 1 ) ) {
307 $begin = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{start}) );
311 if ( $direction >= 0 ) {
312 $end->add( minutes => 1 );
314 while ( my $_a = new_org_closed_overlap($self, $client, $ou, $end->strftime('%FT%T%z'), 1, 1 ) ) {
315 $end = $_dt_parser->parse_datetime( cleanse_ISO8601($_a->{end}) );
322 my $begin_hoo = make_hoo_spanset(actor::org_unit::hours_of_operation->retrieve($ou), $begin);
323 my $end_hoo = make_hoo_spanset(actor::org_unit::hours_of_operation->retrieve($ou), $end );
326 if ( $begin_hoo && $direction <= 0 && $begin_hoo->intersects($begin) ) {
327 my $hoo_intersection = $begin_hoo->intersection( $begin );
328 $begin = $hoo_intersection->min;
329 $begin->subtract( minutes => 1 );
331 while ( my $_b = new_org_closed_overlap($self, $client, $ou, $begin->strftime('%FT%T%z'), -1 ) ) {
332 $begin = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{start}) );
336 if ( $end_hoo && $direction >= 0 && $end_hoo->intersects($end) ) {
337 my $hoo_intersection = $end_hoo->intersection( $end );
338 $end = $hoo_intersection->max;
339 $end->add( minutes => 1 );
342 while ( my $_b = new_org_closed_overlap($self, $client, $ou, $end->strftime('%FT%T%z'), -1 ) ) {
343 $end = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{end}) );
348 my $start = $begin->strftime('%FT%T%z');
349 my $stop = $end->strftime('%FT%T%z');
351 return undef if ($start eq $stop);
352 return { start => $start, end => $stop };
354 __PACKAGE__->register_method(
355 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
357 method => 'new_org_closed_overlap',
360 sub org_closed_overlap {
365 my $direction = shift || 0;
366 my $no_hoo = shift || 0;
368 return undef unless ($date && $ou);
370 my $t = actor::org_unit::closed_date->table;
374 WHERE ? between close_start and close_end
376 ORDER BY close_start ASC, close_end DESC
380 $date = cleanse_ISO8601($date);
381 my ($begin, $end) = ($date,$date);
383 my $hoo = actor::org_unit::hours_of_operation->retrieve($ou);
385 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $date, $ou )) {
386 $begin = cleanse_ISO8601($closure->{close_start});
387 $end = cleanse_ISO8601($closure->{close_end});
389 if ( $direction <= 0 ) {
390 $before = $_dt_parser->parse_datetime( $begin );
391 $before->subtract( minutes => 1 );
393 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->strftime('%FT%T%z'), -1, 1 ) ) {
394 $before = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{start}) );
396 $begin = cleanse_ISO8601($before->strftime('%FT%T%z'));
399 if ( $direction >= 0 ) {
400 $after = $_dt_parser->parse_datetime( $end );
401 $after->add( minutes => 1 );
403 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->strftime('%FT%T%z'), 1, 1 ) ) {
404 $after = $_dt_parser->parse_datetime( cleanse_ISO8601($_a->{end}) );
406 $end = cleanse_ISO8601($after->strftime('%FT%T%z'));
413 if ( $direction <= 0 ) {
414 my $begin_dow = $_dt_parser->parse_datetime( $begin )->day_of_week_0;
415 my $begin_open_meth = "dow_".$begin_dow."_open";
416 my $begin_close_meth = "dow_".$begin_dow."_close";
419 while ($hoo->$begin_open_meth eq '00:00:00' and $hoo->$begin_close_meth eq '00:00:00') {
420 $begin = cleanse_ISO8601($_dt_parser->parse_datetime( $begin )->subtract( days => 1)->strftime('%FT%T%z'));
424 last if ($count > 6);
425 $begin_open_meth = "dow_".$begin_dow."_open";
426 $begin_close_meth = "dow_".$begin_dow."_close";
429 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $begin, $ou )) {
430 $before = $_dt_parser->parse_datetime( $begin );
431 $before->subtract( minutes => 1 );
432 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->strftime('%FT%T%z'), -1 ) ) {
433 $before = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{start}) );
438 if ( $direction >= 0 ) {
439 my $end_dow = $_dt_parser->parse_datetime( $end )->day_of_week_0;
440 my $end_open_meth = "dow_".$end_dow."_open";
441 my $end_close_meth = "dow_".$end_dow."_close";
444 while ($hoo->$end_open_meth eq '00:00:00' and $hoo->$end_close_meth eq '00:00:00') {
445 $end = cleanse_ISO8601($_dt_parser->parse_datetime( $end )->add( days => 1)->strftime('%FT%T%z'));
449 last if ($count > 6);
450 $end_open_meth = "dow_".$end_dow."_open";
451 $end_close_meth = "dow_".$end_dow."_close";
454 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $end, $ou )) {
455 $after = $_dt_parser->parse_datetime( $end );
456 $after->add( minutes => 1 );
458 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->strftime('%FT%T%z'), 1 ) ) {
459 $after = $_dt_parser->parse_datetime( cleanse_ISO8601($_a->{end}) );
461 $end = cleanse_ISO8601($after->strftime('%FT%T%z'));
468 if ($begin eq $date && $end eq $date) {
472 return { start => $begin, end => $end };
474 __PACKAGE__->register_method(
475 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
477 method => 'org_closed_overlap',
480 sub user_by_barcode {
483 my @barcodes = shift;
485 return undef unless @barcodes;
487 for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
489 if (@barcodes == 1) {
490 return $card->usr->to_fieldmapper;
492 $client->respond( $card->usr->to_fieldmapper);
496 __PACKAGE__->register_method(
497 api_name => 'open-ils.storage.direct.actor.user.search.barcode',
499 method => 'user_by_barcode',
508 my $c = actor::card->table;
509 my $p = actor::user->table;
511 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
513 my $list = actor::user->db_Main->selectcol_arrayref($sql);
514 for my $bc ( @$list ) {
515 $client->respond($bc);
519 __PACKAGE__->register_method(
520 api_name => 'open-ils.storage.actor.user.lost_barcodes',
523 method => 'lost_barcodes',
524 signature => <<' NOTE',
525 Returns an array of barcodes that belong to lost cards.
526 @return array of barcodes
530 sub expired_barcodes {
534 my $c = actor::card->table;
535 my $p = actor::user->table;
537 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
539 my $list = actor::user->db_Main->selectcol_arrayref($sql);
540 for my $bc ( @$list ) {
541 $client->respond($bc);
545 __PACKAGE__->register_method(
546 api_name => 'open-ils.storage.actor.user.expired_barcodes',
549 method => 'expired_barcodes',
550 signature => <<' NOTE',
551 Returns an array of barcodes that are currently expired.
552 @return array of barcodes
556 sub barred_barcodes {
560 my $c = actor::card->table;
561 my $p = actor::user->table;
563 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
565 my $list = actor::user->db_Main->selectcol_arrayref($sql);
566 for my $bc ( @$list ) {
567 $client->respond($bc);
571 __PACKAGE__->register_method(
572 api_name => 'open-ils.storage.actor.user.barred_barcodes',
575 method => 'barred_barcodes',
576 signature => <<' NOTE',
577 Returns an array of barcodes that are currently barred.
578 @return array of barcodes
582 sub penalized_barcodes {
586 my $c = actor::card->table;
587 my $p = actor::user_standing_penalty->table;
590 SELECT DISTINCT c.barcode
592 JOIN $p p USING (usr)
593 JOIN config.standing_penalty csp ON (csp.id = p.standing_penalty)
594 WHERE csp.block_list IS NOT NULL
595 AND p.set_date < CURRENT_DATE
596 AND (p.stop_date IS NULL OR p.stop_date > CURRENT_DATE);
599 my $list = actor::user->db_Main->selectcol_arrayref($sql);
600 for my $bc ( @$list ) {
601 $client->respond($bc);
605 __PACKAGE__->register_method(
606 api_name => 'open-ils.storage.actor.user.penalized_barcodes',
609 method => 'penalized_barcodes',
610 signature => <<' NOTE',
611 Returns an array of barcodes that have blocking penalties.
612 @return array of barcodes
621 my $limit = shift || 1000;
623 my $inactive = shift;
625 my $ws_ou_depth = shift || 0;
627 my $penalty_sort = 0;
629 my $strict_opt_in = OpenSRF::Utils::SettingsClient->new->config_value( share => user => 'opt_in' );
631 $sort = ['family_name','first_given_name'] unless ($$sort[0]);
634 if ($$sort[0] eq 'penalties') {
641 # group 2 = phone, ident
644 my $usr = join ' AND ', map { "evergreen.lowercase(CAST($_ AS text)) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
645 my @usrv = map { "^" . $$search{$_}{value} } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
647 my $addr = join ' AND ', map { "evergreen.lowercase(CAST($_ AS text)) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
648 my @addrv = map { "^" . $$search{$_}{value} } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
650 my $pv = $$search{phone}{value};
651 my $iv = $$search{ident}{value};
652 my $nv = $$search{name}{value};
653 my $cv = $$search{card}{value};
657 $card = 'JOIN (SELECT DISTINCT usr FROM actor.card WHERE evergreen.lowercase(barcode) LIKE ?||\'%\') AS card ON (card.usr = users.id)';
665 for my $p ( qw/day_phone evening_phone other_phone/ ) {
666 push @ps, "evergreen.lowercase($p) ~ ?";
667 push @phonev, "^$pv";
669 $phone = '(' . join(' OR ', @ps) . ')';
676 for my $i ( qw/ident_value ident_value2/ ) {
677 push @is, "evergreen.lowercase($i) ~ ?";
678 push @identv, "^$iv";
680 $ident = '(' . join(' OR ', @is) . ')';
687 for my $n ( qw/first_given_name second_given_name family_name/ ) {
688 push @ns, "evergreen.lowercase($n) ~ ?";
691 $name = '(' . join(' OR ', @ns) . ')';
694 my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
695 my $addr_where = $addr;
698 my $u_table = actor::user->table;
699 my $a_table = actor::user_address->table;
700 my $opt_in_table = actor::usr_org_unit_opt_in->table;
701 my $ou_table = actor::org_unit->table;
703 my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
704 my $a_select = "SELECT u.id as id FROM $a_table a JOIN $u_table u ON (u.mailing_address = a.id OR u.billing_address = a.id) WHERE $addr_where";
706 my $clone_select = '';
708 #$clone_select = "JOIN (SELECT cu.id as id FROM $a_table ca ".
709 # "JOIN $u_table cu ON (cu.mailing_address = ca.id OR cu.billing_address = ca.id) ".
710 # "WHERE $addr_where) AS clone ON (clone.id = users.id)" if ($addr_where);
715 $select = "$u_select INTERSECT $a_select";
719 } elsif ($addr_where) {
720 $select = "$a_select";
723 return undef if (!$select && !$card);
725 my $order_by = join ', ', map { 'evergreen.lowercase(CAST(users.'. (split / /,$_)[0] . ' AS text)) ' . (split / /,$_)[1] } @$sort;
726 my $distinct_list = join ', ', map { 'evergreen.lowercase(CAST(users.'. (split / /,$_)[0] . ' AS text))' } @$sort;
727 my $group_list = $distinct_list;
732 $inactive = 'AND users.active = TRUE';
735 if (!$ws_ou) { # XXX This should be required!!
736 $ws_ou = actor::org_unit->search( { parent_ou => undef } )->next->id;
739 my $opt_in_join = '';
740 my $opt_in_where = '';
741 if (lc($strict_opt_in) eq 'true') {
742 $opt_in_join = "LEFT JOIN $opt_in_table oi ON (oi.org_unit = $ws_ou AND users.id = oi.usr)";
743 $opt_in_where = "AND (oi.id IS NOT NULL OR users.home_ou = $ws_ou)";
746 my $penalty_join = '';
748 $distinct_list = 'COUNT(penalties.id), ' . $distinct_list;
749 $order_by = 'COUNT(penalties.id) DESC, ' . $order_by;
750 unshift @$sort, 'COUNT(penalties.id)';
751 $penalty_join = <<" SQL";
752 LEFT JOIN actor.usr_standing_penalty penalties
753 ON (users.id = penalties.usr AND (penalties.stop_date IS NULL OR penalties.stop_date > NOW()))
757 my $descendants = "actor.org_unit_descendants($ws_ou, $ws_ou_depth)";
759 $select = "JOIN ($select) AS search ON (search.id = users.id)" if ($select);
761 SELECT $distinct_list
762 FROM $u_table AS users $card
763 JOIN $descendants d ON (d.id = users.home_ou)
768 WHERE users.deleted = FALSE
776 return actor::user->db_Main->selectcol_arrayref($select, {Columns=>[scalar(@$sort)]}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv));
778 __PACKAGE__->register_method(
779 api_name => 'open-ils.storage.actor.user.crazy_search',
781 method => 'patron_search',
788 my $select =<<" SQL";
791 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
794 my $sth = actor::org_unit->db_Main->prepare_cached($select);
797 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
801 __PACKAGE__->register_method(
802 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
805 method => 'org_unit_list',
808 sub org_unit_type_list {
812 my $select =<<" SQL";
814 FROM actor.org_unit_type
815 ORDER BY depth, name;
818 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
821 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
825 __PACKAGE__->register_method(
826 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
829 method => 'org_unit_type_list',
832 sub org_unit_full_path {
837 return undef unless (@binds);
839 my $func = 'actor.org_unit_full_path(?)';
840 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
842 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
843 $sth->execute(@binds);
845 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
849 __PACKAGE__->register_method(
850 api_name => 'open-ils.storage.actor.org_unit.full_path',
853 method => 'org_unit_full_path',
856 sub org_unit_ancestors {
861 return undef unless ($id);
863 my $func = 'actor.org_unit_ancestors(?)';
865 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
868 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
869 ORDER BY t.depth, f.name;
871 $sth->execute(''.$id);
873 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
877 __PACKAGE__->register_method(
878 api_name => 'open-ils.storage.actor.org_unit.ancestors',
881 method => 'org_unit_ancestors',
884 sub org_unit_descendants {
890 return undef unless ($id);
892 my $func = 'actor.org_unit_descendants(?)';
893 if (defined $depth) {
894 $func = 'actor.org_unit_descendants(?,?)';
897 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
898 $sth->execute(''.$id, ''.$depth) if (defined $depth);
899 $sth->execute(''.$id) unless (defined $depth);
901 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
905 __PACKAGE__->register_method(
906 api_name => 'open-ils.storage.actor.org_unit.descendants',
909 method => 'org_unit_descendants',
912 sub fleshed_actor_stat_cat {
917 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
920 my $cat = actor::stat_cat->retrieve($sc);
923 my $sc_fm = $cat->to_fieldmapper;
924 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
926 $client->respond( $sc_fm );
932 __PACKAGE__->register_method(
933 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
936 method => 'fleshed_actor_stat_cat',
939 __PACKAGE__->register_method(
940 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
944 method => 'fleshed_actor_stat_cat',
947 #XXX Fix stored proc calls
948 sub ranged_actor_stat_cat_all {
953 return undef unless ($ou);
954 my $s_table = actor::stat_cat->table;
956 my $select = <<" SQL";
959 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
964 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
966 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
969 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
970 my $sc_fm = $sc->to_fieldmapper;
972 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
974 $client->respond( $sc_fm );
979 __PACKAGE__->register_method(
980 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
984 method => 'ranged_actor_stat_cat_all',
987 __PACKAGE__->register_method(
988 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
992 method => 'ranged_actor_stat_cat_all',
995 #XXX Fix stored proc calls
996 sub ranged_actor_stat_cat_entry {
1000 my $sc = ''.shift();
1002 return undef unless ($ou);
1003 my $s_table = actor::stat_cat_entry->table;
1005 my $select = <<" SQL";
1008 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
1013 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
1014 $sth->execute($ou,$sc);
1016 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
1017 $client->respond( $sce->to_fieldmapper );
1022 __PACKAGE__->register_method(
1023 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
1026 method => 'ranged_actor_stat_cat_entry',