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;
16 my $_dt_parser = DateTime::Format::ISO8601->new;
18 my $log = 'OpenSRF::Utils::Logger';
20 sub new_usergroup_id {
21 return actor::user->db_Main->selectrow_array("select nextval('actor.usr_usrgroup_seq'::regclass)");
23 __PACKAGE__->register_method(
24 api_name => 'open-ils.storage.actor.user.group_id.new',
26 method => 'new_usergroup_id',
32 my $adult_age = shift;
37 WHERE AGE(dob) > ?::INTERVAL;
40 my $sth = actor::user->db_Main->prepare_cached($sql);
41 $sth->execute($adult_age);
45 __PACKAGE__->register_method(
46 api_name => 'open-ils.storage.actor.user.juvenile_to_adult',
48 method => 'juv_to_adult',
58 SUM(COALESCE((SELECT SUM(b.amount) FROM money.billing b WHERE b.voided IS FALSE AND b.xact = x.id),0.0)) -
59 SUM(COALESCE((SELECT SUM(p.amount) FROM money.payment p WHERE p.voided IS FALSE AND p.xact = x.id),0.0))
60 FROM money.billable_xact x
61 WHERE x.usr = ? AND x.xact_finish IS NULL
65 my (undef,$val) = actor::user->db_Main->selectrow_array($sql, {}, $usr);
69 __PACKAGE__->register_method(
70 api_name => 'open-ils.storage.actor.user.total_owed',
72 method => 'usr_total_owed',
75 sub usr_breakdown_out {
80 $self->method_lookup('open-ils.storage.transaction.begin')->run($client);
82 my $out_sql = <<" SQL";
84 FROM action.circulation
86 AND checkin_time IS NULL
87 AND ( (fine_interval >= '1 day' AND due_date >= 'today')
88 OR (fine_interval < '1 day' AND due_date > 'now' ))
89 AND (stop_fines IS NULL
90 OR stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE'))
93 my $out = actor::user->db_Main->selectcol_arrayref($out_sql, {}, $usr);
95 my $od_sql = <<" SQL";
97 FROM action.circulation
99 AND checkin_time IS NULL
100 AND ( (fine_interval >= '1 day' AND due_date < 'today')
101 OR (fine_interval < '1 day' AND due_date < 'now' ))
102 AND (stop_fines IS NULL
103 OR stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE'))
106 my $od = actor::user->db_Main->selectcol_arrayref($od_sql, {}, $usr);
108 my $lost_sql = <<" SQL";
110 FROM action.circulation
111 WHERE usr = ? AND checkin_time IS NULL AND xact_finish IS NULL AND stop_fines = 'LOST'
114 my $lost = actor::user->db_Main->selectcol_arrayref($lost_sql, {}, $usr);
116 my $cl_sql = <<" SQL";
118 FROM action.circulation
119 WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'CLAIMSRETURNED'
122 my $cl = actor::user->db_Main->selectcol_arrayref($cl_sql, {}, $usr);
124 my $lo_sql = <<" SQL";
126 FROM action.circulation
127 WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'LONGOVERDUE'
130 my $lo = actor::user->db_Main->selectcol_arrayref($lo_sql, {}, $usr);
132 $self->method_lookup('open-ils.storage.transaction.rollback')->run($client);
134 if ($self->api_name =~/count$/o) {
135 return { total => scalar(@$out) + scalar(@$od) + scalar(@$lost) + scalar(@$cl) + scalar(@$lo),
136 out => scalar(@$out),
137 overdue => scalar(@$od),
138 lost => scalar(@$lost),
139 claims_returned => scalar(@$cl),
140 long_overdue => scalar(@$lo),
144 return { out => $out,
147 claims_returned => $cl,
151 __PACKAGE__->register_method(
152 api_name => 'open-ils.storage.actor.user.checked_out',
154 method => 'usr_breakdown_out',
156 __PACKAGE__->register_method(
157 api_name => 'open-ils.storage.actor.user.checked_out.count',
159 method => 'usr_breakdown_out',
169 FROM action.circulation
170 WHERE usr = ? AND checkin_time IS NULL
173 my ($val) = actor::user->db_Main->selectrow_array($sql, {}, $usr);
177 __PACKAGE__->register_method(
178 api_name => 'open-ils.storage.actor.user.total_out',
180 method => 'usr_total_out',
187 local $OpenILS::Application::Storage::WRITE = 1;
189 my $delete_sql = <<" SQL";
190 DELETE FROM actor.org_unit_proximity;
193 my $insert_sql = <<" SQL";
194 INSERT INTO actor.org_unit_proximity (from_org, to_org, prox)
197 actor.org_unit_proximity(l.id,r.id)
198 FROM actor.org_unit l,
202 actor::org_unit_proximity->db_Main->do($delete_sql);
203 actor::org_unit_proximity->db_Main->do($insert_sql);
207 __PACKAGE__->register_method(
208 api_name => 'open-ils.storage.actor.org_unit.refresh_proximity',
210 method => 'calc_proximity',
213 sub make_hoo_spanset {
215 return undef unless $hoo;
217 my $today = shift || DateTime->now;
219 my $tz = OpenSRF::AppSession->create('open-ils.actor')->request(
220 'open-ils.actor.ou_setting.ancestor_default' => $hoo->id.'' => 'org_unit.timezone'
221 )->gather(1) || DateTime::TimeZone->new( name => 'local' )->name;
223 my $current_dow = $today->day_of_week_0;
225 my $spanset = DateTime::SpanSet->empty_set;
226 for my $d ( 0 .. 6 ) {
228 my $omethod = 'dow_'.$d.'_open';
229 my $cmethod = 'dow_'.$d.'_close';
231 my $open = interval_to_seconds($hoo->$omethod());
232 my $close = interval_to_seconds($hoo->$cmethod());
234 next if ($open == $close && $open == 0);
236 my $dow_offset = ($d - $current_dow) * $one_day;
237 $close += $one_day if ($close <= $open);
239 $spanset = $spanset->union(
241 start => $today->clone->add( seconds => $dow_offset + $open ),
242 end => $today->clone->add( seconds => $dow_offset + $close )
247 return $spanset->complement;
250 sub make_closure_spanset {
251 my $closures = shift;
252 return undef unless $closures;
254 my $spanset = DateTime::SpanSet->empty_set;
255 for my $k ( keys %$closures ) {
256 my $c = $$closures{$k};
258 $spanset = $spanset->union(
260 start => $_dt_parser->parse_datetime(cleanse_ISO8601($c->{close_start})),
261 end => $_dt_parser->parse_datetime(cleanse_ISO8601($c->{close_end}))
269 sub new_org_closed_overlap {
274 my $direction = shift || 0;
275 my $no_hoo = shift || 0;
277 return undef unless ($date && $ou);
279 # we're given a date and a direction, find any closures that contain the date
280 my $t = actor::org_unit::closed_date->table;
286 ORDER BY close_start ASC, close_end DESC
290 $date = cleanse_ISO8601($date);
292 my $target_date = $_dt_parser->parse_datetime( $date );
293 my ($begin, $end) = ($target_date, $target_date);
295 # create a spanset from the closures that contain the $date
296 my $closure_spanset = make_closure_spanset(
297 actor::org_unit::closed_date->db_Main->selectall_hashref( $sql, 'id', {}, $date, $ou )
300 if ($closure_spanset && $closure_spanset->intersects( $target_date )) {
301 my $closure_intersection = $closure_spanset->intersection( $target_date );
302 $begin = $closure_intersection->min;
303 $end = $closure_intersection->max;
305 if ( $direction <= 0 ) {
306 $begin->subtract( minutes => 1 );
308 while ( my $_b = new_org_closed_overlap($self, $client, $ou, $begin->strftime('%FT%T%z'), -1, 1 ) ) {
309 $begin = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{start}) );
313 if ( $direction >= 0 ) {
314 $end->add( minutes => 1 );
316 while ( my $_a = new_org_closed_overlap($self, $client, $ou, $end->strftime('%FT%T%z'), 1, 1 ) ) {
317 $end = $_dt_parser->parse_datetime( cleanse_ISO8601($_a->{end}) );
324 my $begin_hoo = make_hoo_spanset(actor::org_unit::hours_of_operation->retrieve($ou), $begin);
325 my $end_hoo = make_hoo_spanset(actor::org_unit::hours_of_operation->retrieve($ou), $end );
328 if ( $begin_hoo && $direction <= 0 && $begin_hoo->intersects($begin) ) {
329 my $hoo_intersection = $begin_hoo->intersection( $begin );
330 $begin = $hoo_intersection->min;
331 $begin->subtract( minutes => 1 );
333 while ( my $_b = new_org_closed_overlap($self, $client, $ou, $begin->strftime('%FT%T%z'), -1 ) ) {
334 $begin = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{start}) );
338 if ( $end_hoo && $direction >= 0 && $end_hoo->intersects($end) ) {
339 my $hoo_intersection = $end_hoo->intersection( $end );
340 $end = $hoo_intersection->max;
341 $end->add( minutes => 1 );
344 while ( my $_b = new_org_closed_overlap($self, $client, $ou, $end->strftime('%FT%T%z'), -1 ) ) {
345 $end = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{end}) );
350 my $start = $begin->strftime('%FT%T%z');
351 my $stop = $end->strftime('%FT%T%z');
353 return undef if ($start eq $stop);
354 return { start => $start, end => $stop };
356 __PACKAGE__->register_method(
357 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
359 method => 'new_org_closed_overlap',
362 sub org_closed_overlap {
367 my $direction = shift || 0;
368 my $no_hoo = shift || 0;
370 return undef unless ($date && $ou);
372 my $t = actor::org_unit::closed_date->table;
376 WHERE ? between close_start and close_end
378 ORDER BY close_start ASC, close_end DESC
382 $date = cleanse_ISO8601($date);
383 my ($begin, $end) = ($date,$date);
385 my $hoo = actor::org_unit::hours_of_operation->retrieve($ou);
387 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $date, $ou )) {
388 $begin = cleanse_ISO8601($closure->{close_start});
389 $end = cleanse_ISO8601($closure->{close_end});
391 if ( $direction <= 0 ) {
392 $before = $_dt_parser->parse_datetime( $begin );
393 $before->subtract( minutes => 1 );
395 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->strftime('%FT%T%z'), -1, 1 ) ) {
396 $before = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{start}) );
398 $begin = cleanse_ISO8601($before->strftime('%FT%T%z'));
401 if ( $direction >= 0 ) {
402 $after = $_dt_parser->parse_datetime( $end );
403 $after->add( minutes => 1 );
405 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->strftime('%FT%T%z'), 1, 1 ) ) {
406 $after = $_dt_parser->parse_datetime( cleanse_ISO8601($_a->{end}) );
408 $end = cleanse_ISO8601($after->strftime('%FT%T%z'));
415 if ( $direction <= 0 ) {
416 my $begin_dow = $_dt_parser->parse_datetime( $begin )->day_of_week_0;
417 my $begin_open_meth = "dow_".$begin_dow."_open";
418 my $begin_close_meth = "dow_".$begin_dow."_close";
421 while ($hoo->$begin_open_meth eq '00:00:00' and $hoo->$begin_close_meth eq '00:00:00') {
422 $begin = cleanse_ISO8601($_dt_parser->parse_datetime( $begin )->subtract( days => 1)->strftime('%FT%T%z'));
426 last if ($count > 6);
427 $begin_open_meth = "dow_".$begin_dow."_open";
428 $begin_close_meth = "dow_".$begin_dow."_close";
431 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $begin, $ou )) {
432 $before = $_dt_parser->parse_datetime( $begin );
433 $before->subtract( minutes => 1 );
434 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->strftime('%FT%T%z'), -1 ) ) {
435 $before = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{start}) );
440 if ( $direction >= 0 ) {
441 my $end_dow = $_dt_parser->parse_datetime( $end )->day_of_week_0;
442 my $end_open_meth = "dow_".$end_dow."_open";
443 my $end_close_meth = "dow_".$end_dow."_close";
446 while ($hoo->$end_open_meth eq '00:00:00' and $hoo->$end_close_meth eq '00:00:00') {
447 $end = cleanse_ISO8601($_dt_parser->parse_datetime( $end )->add( days => 1)->strftime('%FT%T%z'));
451 last if ($count > 6);
452 $end_open_meth = "dow_".$end_dow."_open";
453 $end_close_meth = "dow_".$end_dow."_close";
456 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $end, $ou )) {
457 $after = $_dt_parser->parse_datetime( $end );
458 $after->add( minutes => 1 );
460 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->strftime('%FT%T%z'), 1 ) ) {
461 $after = $_dt_parser->parse_datetime( cleanse_ISO8601($_a->{end}) );
463 $end = cleanse_ISO8601($after->strftime('%FT%T%z'));
470 if ($begin eq $date && $end eq $date) {
474 return { start => $begin, end => $end };
476 __PACKAGE__->register_method(
477 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
479 method => 'org_closed_overlap',
482 sub user_by_barcode {
485 my @barcodes = shift;
487 return undef unless @barcodes;
489 for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
491 if (@barcodes == 1) {
492 return $card->usr->to_fieldmapper;
494 $client->respond( $card->usr->to_fieldmapper);
498 __PACKAGE__->register_method(
499 api_name => 'open-ils.storage.direct.actor.user.search.barcode',
501 method => 'user_by_barcode',
510 my $c = actor::card->table;
511 my $p = actor::user->table;
513 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
515 my $list = actor::user->db_Main->selectcol_arrayref($sql);
516 for my $bc ( @$list ) {
517 $client->respond($bc);
521 __PACKAGE__->register_method(
522 api_name => 'open-ils.storage.actor.user.lost_barcodes',
525 method => 'lost_barcodes',
526 signature => <<' NOTE',
527 Returns an array of barcodes that belong to lost cards.
528 @return array of barcodes
532 sub expired_barcodes {
536 my $c = actor::card->table;
537 my $p = actor::user->table;
539 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
541 my $list = actor::user->db_Main->selectcol_arrayref($sql);
542 for my $bc ( @$list ) {
543 $client->respond($bc);
547 __PACKAGE__->register_method(
548 api_name => 'open-ils.storage.actor.user.expired_barcodes',
551 method => 'expired_barcodes',
552 signature => <<' NOTE',
553 Returns an array of barcodes that are currently expired.
554 @return array of barcodes
558 sub barred_barcodes {
562 my $c = actor::card->table;
563 my $p = actor::user->table;
565 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
567 my $list = actor::user->db_Main->selectcol_arrayref($sql);
568 for my $bc ( @$list ) {
569 $client->respond($bc);
573 __PACKAGE__->register_method(
574 api_name => 'open-ils.storage.actor.user.barred_barcodes',
577 method => 'barred_barcodes',
578 signature => <<' NOTE',
579 Returns an array of barcodes that are currently barred.
580 @return array of barcodes
584 sub penalized_barcodes {
588 my $c = actor::card->table;
589 my $p = actor::user_standing_penalty->table;
592 SELECT DISTINCT c.barcode
594 JOIN $p p USING (usr)
595 JOIN config.standing_penalty csp ON (csp.id = p.standing_penalty)
596 WHERE csp.block_list IS NOT NULL
597 AND p.set_date < CURRENT_DATE
598 AND (p.stop_date IS NULL OR p.stop_date > CURRENT_DATE);
601 my $list = actor::user->db_Main->selectcol_arrayref($sql);
602 for my $bc ( @$list ) {
603 $client->respond($bc);
607 __PACKAGE__->register_method(
608 api_name => 'open-ils.storage.actor.user.penalized_barcodes',
611 method => 'penalized_barcodes',
612 signature => <<' NOTE',
613 Returns an array of barcodes that have blocking penalties.
614 @return array of barcodes
623 my $limit = shift || 1000;
625 my $inactive = shift;
627 my $ws_ou_depth = shift || 0;
629 my $penalty_sort = 0;
631 my $strict_opt_in = OpenSRF::Utils::SettingsClient->new->config_value( share => user => 'opt_in' );
633 $sort = ['family_name','first_given_name'] unless ($$sort[0]);
636 if ($$sort[0] eq 'penalties') {
643 # group 2 = phone, ident
646 my $usr = join ' AND ', map { "LOWER(CAST($_ AS text)) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
647 my @usrv = map { "^" . encode_utf8($$search{$_}{value}) } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
649 my $addr = join ' AND ', map { "LOWER(CAST($_ AS text)) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
650 my @addrv = map { "^" . encode_utf8($$search{$_}{value}) } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
652 my $pv = encode_utf8($$search{phone}{value});
653 my $iv = encode_utf8($$search{ident}{value});
654 my $nv = encode_utf8($$search{name}{value});
655 my $cv = encode_utf8($$search{card}{value});
659 $card = 'JOIN (SELECT DISTINCT usr FROM actor.card WHERE LOWER(barcode) LIKE ?||\'%\') AS card ON (card.usr = users.id)';
667 for my $p ( qw/day_phone evening_phone other_phone/ ) {
668 push @ps, "LOWER($p) ~ ?";
669 push @phonev, "^$pv";
671 $phone = '(' . join(' OR ', @ps) . ')';
678 for my $i ( qw/ident_value ident_value2/ ) {
679 push @is, "LOWER($i) ~ ?";
680 push @identv, "^$iv";
682 $ident = '(' . join(' OR ', @is) . ')';
689 for my $n ( qw/first_given_name second_given_name family_name/ ) {
690 push @ns, "LOWER($n) ~ ?";
693 $name = '(' . join(' OR ', @ns) . ')';
696 my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
697 my $addr_where = $addr;
700 my $u_table = actor::user->table;
701 my $a_table = actor::user_address->table;
702 my $opt_in_table = actor::usr_org_unit_opt_in->table;
703 my $ou_table = actor::org_unit->table;
705 my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
706 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";
708 my $clone_select = '';
710 #$clone_select = "JOIN (SELECT cu.id as id FROM $a_table ca ".
711 # "JOIN $u_table cu ON (cu.mailing_address = ca.id OR cu.billing_address = ca.id) ".
712 # "WHERE $addr_where) AS clone ON (clone.id = users.id)" if ($addr_where);
717 $select = "$u_select INTERSECT $a_select";
721 } elsif ($addr_where) {
722 $select = "$a_select";
725 return undef if (!$select && !$card);
727 my $order_by = join ', ', map { 'LOWER(CAST(users.'. (split / /,$_)[0] . ' AS text)) ' . (split / /,$_)[1] } @$sort;
728 my $distinct_list = join ', ', map { 'LOWER(CAST(users.'. (split / /,$_)[0] . ' AS text))' } @$sort;
729 my $group_list = $distinct_list;
734 $inactive = 'AND users.active = TRUE';
737 if (!$ws_ou) { # XXX This should be required!!
738 $ws_ou = actor::org_unit->search( { parent_ou => undef } )->next->id;
741 my $opt_in_join = '';
742 my $opt_in_where = '';
743 if (lc($strict_opt_in) eq 'true') {
744 $opt_in_join = "LEFT JOIN $opt_in_table oi ON (oi.org_unit = $ws_ou AND users.id = oi.usr)";
745 $opt_in_where = "AND (oi.id IS NOT NULL OR users.home_ou = $ws_ou)";
748 my $penalty_join = '';
750 $distinct_list = 'COUNT(penalties.id), ' . $distinct_list;
751 $order_by = 'COUNT(penalties.id) DESC, ' . $order_by;
752 unshift @$sort, 'COUNT(penalties.id)';
753 $penalty_join = <<" SQL";
754 LEFT JOIN actor.usr_standing_penalty penalties
755 ON (users.id = penalties.usr AND (penalties.stop_date IS NULL OR penalties.stop_date > NOW()))
759 my $descendants = "actor.org_unit_descendants($ws_ou, $ws_ou_depth)";
761 $select = "JOIN ($select) AS search ON (search.id = users.id)" if ($select);
763 SELECT $distinct_list
764 FROM $u_table AS users $card
765 JOIN $descendants d ON (d.id = users.home_ou)
770 WHERE users.deleted = FALSE
778 return actor::user->db_Main->selectcol_arrayref($select, {Columns=>[scalar(@$sort)]}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv));
780 __PACKAGE__->register_method(
781 api_name => 'open-ils.storage.actor.user.crazy_search',
783 method => 'patron_search',
790 my $select =<<" SQL";
793 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
796 my $sth = actor::org_unit->db_Main->prepare_cached($select);
799 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
803 __PACKAGE__->register_method(
804 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
807 method => 'org_unit_list',
810 sub org_unit_type_list {
814 my $select =<<" SQL";
816 FROM actor.org_unit_type
817 ORDER BY depth, name;
820 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
823 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
827 __PACKAGE__->register_method(
828 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
831 method => 'org_unit_type_list',
834 sub org_unit_full_path {
839 return undef unless (@binds);
841 my $func = 'actor.org_unit_full_path(?)';
842 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
844 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
845 $sth->execute(@binds);
847 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
851 __PACKAGE__->register_method(
852 api_name => 'open-ils.storage.actor.org_unit.full_path',
855 method => 'org_unit_full_path',
858 sub org_unit_ancestors {
863 return undef unless ($id);
865 my $func = 'actor.org_unit_ancestors(?)';
867 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
870 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
871 ORDER BY t.depth, f.name;
873 $sth->execute(''.$id);
875 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
879 __PACKAGE__->register_method(
880 api_name => 'open-ils.storage.actor.org_unit.ancestors',
883 method => 'org_unit_ancestors',
886 sub org_unit_descendants {
892 return undef unless ($id);
894 my $func = 'actor.org_unit_descendants(?)';
895 if (defined $depth) {
896 $func = 'actor.org_unit_descendants(?,?)';
899 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
900 $sth->execute(''.$id, ''.$depth) if (defined $depth);
901 $sth->execute(''.$id) unless (defined $depth);
903 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
907 __PACKAGE__->register_method(
908 api_name => 'open-ils.storage.actor.org_unit.descendants',
911 method => 'org_unit_descendants',
914 sub fleshed_actor_stat_cat {
919 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
922 my $cat = actor::stat_cat->retrieve($sc);
925 my $sc_fm = $cat->to_fieldmapper;
926 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
928 $client->respond( $sc_fm );
934 __PACKAGE__->register_method(
935 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
938 method => 'fleshed_actor_stat_cat',
941 __PACKAGE__->register_method(
942 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
946 method => 'fleshed_actor_stat_cat',
949 #XXX Fix stored proc calls
950 sub ranged_actor_stat_cat_all {
955 return undef unless ($ou);
956 my $s_table = actor::stat_cat->table;
958 my $select = <<" SQL";
961 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
966 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
968 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
971 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
972 my $sc_fm = $sc->to_fieldmapper;
974 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
976 $client->respond( $sc_fm );
981 __PACKAGE__->register_method(
982 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
986 method => 'ranged_actor_stat_cat_all',
989 __PACKAGE__->register_method(
990 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
994 method => 'ranged_actor_stat_cat_all',
997 #XXX Fix stored proc calls
998 sub ranged_actor_stat_cat_entry {
1001 my $ou = ''.shift();
1002 my $sc = ''.shift();
1004 return undef unless ($ou);
1005 my $s_table = actor::stat_cat_entry->table;
1007 my $select = <<" SQL";
1010 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
1015 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
1016 $sth->execute($ou,$sc);
1018 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
1019 $client->respond( $sce->to_fieldmapper );
1024 __PACKAGE__->register_method(
1025 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
1028 method => 'ranged_actor_stat_cat_entry',