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
39 my $sth = actor::user->db_Main->prepare_cached($sql);
40 $sth->execute($adult_age);
44 __PACKAGE__->register_method(
45 api_name => 'open-ils.storage.actor.user.juvenile_to_adult',
47 method => 'juv_to_adult',
57 SUM(COALESCE((SELECT SUM(b.amount) FROM money.billing b WHERE b.voided IS FALSE AND b.xact = x.id),0.0)) -
58 SUM(COALESCE((SELECT SUM(p.amount) FROM money.payment p WHERE p.voided IS FALSE AND p.xact = x.id),0.0))
59 FROM money.billable_xact x
60 WHERE x.usr = ? AND x.xact_finish IS NULL
64 my (undef,$val) = actor::user->db_Main->selectrow_array($sql, {}, $usr);
68 __PACKAGE__->register_method(
69 api_name => 'open-ils.storage.actor.user.total_owed',
71 method => 'usr_total_owed',
74 sub usr_breakdown_out {
79 $self->method_lookup('open-ils.storage.transaction.begin')->run($client);
81 my $out_sql = <<" SQL";
83 FROM action.circulation
85 AND checkin_time IS NULL
86 AND ( (fine_interval >= '1 day' AND due_date >= 'today')
87 OR (fine_interval < '1 day' AND due_date > 'now' ))
88 AND (stop_fines IS NULL
89 OR stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE'))
92 my $out = actor::user->db_Main->selectcol_arrayref($out_sql, {}, $usr);
94 my $od_sql = <<" SQL";
96 FROM action.circulation
98 AND checkin_time IS NULL
99 AND ( (fine_interval >= '1 day' AND due_date < 'today')
100 OR (fine_interval < '1 day' AND due_date < 'now' ))
101 AND (stop_fines IS NULL
102 OR stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE'))
105 my $od = actor::user->db_Main->selectcol_arrayref($od_sql, {}, $usr);
107 my $lost_sql = <<" SQL";
109 FROM action.circulation
110 WHERE usr = ? AND checkin_time IS NULL AND xact_finish IS NULL AND stop_fines = 'LOST'
113 my $lost = actor::user->db_Main->selectcol_arrayref($lost_sql, {}, $usr);
115 my $cl_sql = <<" SQL";
117 FROM action.circulation
118 WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'CLAIMSRETURNED'
121 my $cl = actor::user->db_Main->selectcol_arrayref($cl_sql, {}, $usr);
123 my $lo_sql = <<" SQL";
125 FROM action.circulation
126 WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'LONGOVERDUE'
129 my $lo = actor::user->db_Main->selectcol_arrayref($lo_sql, {}, $usr);
131 $self->method_lookup('open-ils.storage.transaction.rollback')->run($client);
133 if ($self->api_name =~/count$/o) {
134 return { total => scalar(@$out) + scalar(@$od) + scalar(@$lost) + scalar(@$cl) + scalar(@$lo),
135 out => scalar(@$out),
136 overdue => scalar(@$od),
137 lost => scalar(@$lost),
138 claims_returned => scalar(@$cl),
139 long_overdue => scalar(@$lo),
143 return { out => $out,
146 claims_returned => $cl,
150 __PACKAGE__->register_method(
151 api_name => 'open-ils.storage.actor.user.checked_out',
153 method => 'usr_breakdown_out',
155 __PACKAGE__->register_method(
156 api_name => 'open-ils.storage.actor.user.checked_out.count',
158 method => 'usr_breakdown_out',
168 FROM action.circulation
169 WHERE usr = ? AND checkin_time IS NULL
172 my ($val) = actor::user->db_Main->selectrow_array($sql, {}, $usr);
176 __PACKAGE__->register_method(
177 api_name => 'open-ils.storage.actor.user.total_out',
179 method => 'usr_total_out',
186 local $OpenILS::Application::Storage::WRITE = 1;
188 my $delete_sql = <<" SQL";
189 DELETE FROM actor.org_unit_proximity;
192 my $insert_sql = <<" SQL";
193 INSERT INTO actor.org_unit_proximity (from_org, to_org, prox)
196 actor.org_unit_proximity(l.id,r.id)
197 FROM actor.org_unit l,
201 actor::org_unit_proximity->db_Main->do($delete_sql);
202 actor::org_unit_proximity->db_Main->do($insert_sql);
206 __PACKAGE__->register_method(
207 api_name => 'open-ils.storage.actor.org_unit.refresh_proximity',
209 method => 'calc_proximity',
212 sub make_hoo_spanset {
214 return undef unless $hoo;
216 my $today = shift || DateTime->now;
218 my $tz = OpenSRF::AppSession->create('open-ils.actor')->request(
219 'open-ils.actor.ou_setting.ancestor_default' => $hoo->id.'' => 'org_unit.timezone'
220 )->gather(1) || DateTime::TimeZone->new( name => 'local' )->name;
222 my $current_dow = $today->day_of_week_0;
224 my $spanset = DateTime::SpanSet->empty_set;
225 for my $d ( 0 .. 6 ) {
227 my $omethod = 'dow_'.$d.'_open';
228 my $cmethod = 'dow_'.$d.'_close';
230 my $open = interval_to_seconds($hoo->$omethod());
231 my $close = interval_to_seconds($hoo->$cmethod());
233 next if ($open == $close && $open == 0);
235 my $dow_offset = ($d - $current_dow) * $one_day;
236 $close += $one_day if ($close <= $open);
238 $spanset = $spanset->union(
240 start => $today->clone->add( seconds => $dow_offset + $open ),
241 end => $today->clone->add( seconds => $dow_offset + $close )
246 return $spanset->complement;
249 sub make_closure_spanset {
250 my $closures = shift;
251 return undef unless $closures;
253 my $spanset = DateTime::SpanSet->empty_set;
254 for my $k ( keys %$closures ) {
255 my $c = $$closures{$k};
257 $spanset = $spanset->union(
259 start => $_dt_parser->parse_datetime(cleanse_ISO8601($c->{close_start})),
260 end => $_dt_parser->parse_datetime(cleanse_ISO8601($c->{close_end}))
268 sub new_org_closed_overlap {
273 my $direction = shift || 0;
274 my $no_hoo = shift || 0;
276 return undef unless ($date && $ou);
278 # we're given a date and a direction, find any closures that contain the date
279 my $t = actor::org_unit::closed_date->table;
285 ORDER BY close_start ASC, close_end DESC
289 $date = cleanse_ISO8601($date);
291 my $target_date = $_dt_parser->parse_datetime( $date );
292 my ($begin, $end) = ($target_date, $target_date);
294 # create a spanset from the closures that contain the $date
295 my $closure_spanset = make_closure_spanset(
296 actor::org_unit::closed_date->db_Main->selectall_hashref( $sql, 'id', {}, $date, $ou )
299 if ($closure_spanset && $closure_spanset->intersects( $target_date )) {
300 my $closure_intersection = $closure_spanset->intersection( $target_date );
301 $begin = $closure_intersection->min;
302 $end = $closure_intersection->max;
304 if ( $direction <= 0 ) {
305 $begin->subtract( minutes => 1 );
307 while ( my $_b = new_org_closed_overlap($self, $client, $ou, $begin->strftime('%FT%T%z'), -1, 1 ) ) {
308 $begin = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{start}) );
312 if ( $direction >= 0 ) {
313 $end->add( minutes => 1 );
315 while ( my $_a = new_org_closed_overlap($self, $client, $ou, $end->strftime('%FT%T%z'), 1, 1 ) ) {
316 $end = $_dt_parser->parse_datetime( cleanse_ISO8601($_a->{end}) );
323 my $begin_hoo = make_hoo_spanset(actor::org_unit::hours_of_operation->retrieve($ou), $begin);
324 my $end_hoo = make_hoo_spanset(actor::org_unit::hours_of_operation->retrieve($ou), $end );
327 if ( $begin_hoo && $direction <= 0 && $begin_hoo->intersects($begin) ) {
328 my $hoo_intersection = $begin_hoo->intersection( $begin );
329 $begin = $hoo_intersection->min;
330 $begin->subtract( minutes => 1 );
332 while ( my $_b = new_org_closed_overlap($self, $client, $ou, $begin->strftime('%FT%T%z'), -1 ) ) {
333 $begin = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{start}) );
337 if ( $end_hoo && $direction >= 0 && $end_hoo->intersects($end) ) {
338 my $hoo_intersection = $end_hoo->intersection( $end );
339 $end = $hoo_intersection->max;
340 $end->add( minutes => 1 );
343 while ( my $_b = new_org_closed_overlap($self, $client, $ou, $end->strftime('%FT%T%z'), -1 ) ) {
344 $end = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{end}) );
349 my $start = $begin->strftime('%FT%T%z');
350 my $stop = $end->strftime('%FT%T%z');
352 return undef if ($start eq $stop);
353 return { start => $start, end => $stop };
355 __PACKAGE__->register_method(
356 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
358 method => 'new_org_closed_overlap',
361 sub org_closed_overlap {
366 my $direction = shift || 0;
367 my $no_hoo = shift || 0;
369 return undef unless ($date && $ou);
371 my $t = actor::org_unit::closed_date->table;
375 WHERE ? between close_start and close_end
377 ORDER BY close_start ASC, close_end DESC
381 $date = cleanse_ISO8601($date);
382 my ($begin, $end) = ($date,$date);
384 my $hoo = actor::org_unit::hours_of_operation->retrieve($ou);
386 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $date, $ou )) {
387 $begin = cleanse_ISO8601($closure->{close_start});
388 $end = cleanse_ISO8601($closure->{close_end});
390 if ( $direction <= 0 ) {
391 $before = $_dt_parser->parse_datetime( $begin );
392 $before->subtract( minutes => 1 );
394 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->strftime('%FT%T%z'), -1, 1 ) ) {
395 $before = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{start}) );
397 $begin = cleanse_ISO8601($before->strftime('%FT%T%z'));
400 if ( $direction >= 0 ) {
401 $after = $_dt_parser->parse_datetime( $end );
402 $after->add( minutes => 1 );
404 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->strftime('%FT%T%z'), 1, 1 ) ) {
405 $after = $_dt_parser->parse_datetime( cleanse_ISO8601($_a->{end}) );
407 $end = cleanse_ISO8601($after->strftime('%FT%T%z'));
414 if ( $direction <= 0 ) {
415 my $begin_dow = $_dt_parser->parse_datetime( $begin )->day_of_week_0;
416 my $begin_open_meth = "dow_".$begin_dow."_open";
417 my $begin_close_meth = "dow_".$begin_dow."_close";
420 while ($hoo->$begin_open_meth eq '00:00:00' and $hoo->$begin_close_meth eq '00:00:00') {
421 $begin = cleanse_ISO8601($_dt_parser->parse_datetime( $begin )->subtract( days => 1)->strftime('%FT%T%z'));
425 last if ($count > 6);
426 $begin_open_meth = "dow_".$begin_dow."_open";
427 $begin_close_meth = "dow_".$begin_dow."_close";
430 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $begin, $ou )) {
431 $before = $_dt_parser->parse_datetime( $begin );
432 $before->subtract( minutes => 1 );
433 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->strftime('%FT%T%z'), -1 ) ) {
434 $before = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{start}) );
439 if ( $direction >= 0 ) {
440 my $end_dow = $_dt_parser->parse_datetime( $end )->day_of_week_0;
441 my $end_open_meth = "dow_".$end_dow."_open";
442 my $end_close_meth = "dow_".$end_dow."_close";
445 while ($hoo->$end_open_meth eq '00:00:00' and $hoo->$end_close_meth eq '00:00:00') {
446 $end = cleanse_ISO8601($_dt_parser->parse_datetime( $end )->add( days => 1)->strftime('%FT%T%z'));
450 last if ($count > 6);
451 $end_open_meth = "dow_".$end_dow."_open";
452 $end_close_meth = "dow_".$end_dow."_close";
455 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $end, $ou )) {
456 $after = $_dt_parser->parse_datetime( $end );
457 $after->add( minutes => 1 );
459 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->strftime('%FT%T%z'), 1 ) ) {
460 $after = $_dt_parser->parse_datetime( cleanse_ISO8601($_a->{end}) );
462 $end = cleanse_ISO8601($after->strftime('%FT%T%z'));
469 if ($begin eq $date && $end eq $date) {
473 return { start => $begin, end => $end };
475 __PACKAGE__->register_method(
476 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
478 method => 'org_closed_overlap',
481 sub user_by_barcode {
484 my @barcodes = shift;
486 return undef unless @barcodes;
488 for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
490 if (@barcodes == 1) {
491 return $card->usr->to_fieldmapper;
493 $client->respond( $card->usr->to_fieldmapper);
497 __PACKAGE__->register_method(
498 api_name => 'open-ils.storage.direct.actor.user.search.barcode',
500 method => 'user_by_barcode',
509 my $c = actor::card->table;
510 my $p = actor::user->table;
512 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
514 my $list = actor::user->db_Main->selectcol_arrayref($sql);
515 for my $bc ( @$list ) {
516 $client->respond($bc);
520 __PACKAGE__->register_method(
521 api_name => 'open-ils.storage.actor.user.lost_barcodes',
524 method => 'lost_barcodes',
525 signature => <<' NOTE',
526 Returns an array of barcodes that belong to lost cards.
527 @return array of barcodes
531 sub expired_barcodes {
535 my $c = actor::card->table;
536 my $p = actor::user->table;
538 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
540 my $list = actor::user->db_Main->selectcol_arrayref($sql);
541 for my $bc ( @$list ) {
542 $client->respond($bc);
546 __PACKAGE__->register_method(
547 api_name => 'open-ils.storage.actor.user.expired_barcodes',
550 method => 'expired_barcodes',
551 signature => <<' NOTE',
552 Returns an array of barcodes that are currently expired.
553 @return array of barcodes
557 sub barred_barcodes {
561 my $c = actor::card->table;
562 my $p = actor::user->table;
564 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
566 my $list = actor::user->db_Main->selectcol_arrayref($sql);
567 for my $bc ( @$list ) {
568 $client->respond($bc);
572 __PACKAGE__->register_method(
573 api_name => 'open-ils.storage.actor.user.barred_barcodes',
576 method => 'barred_barcodes',
577 signature => <<' NOTE',
578 Returns an array of barcodes that are currently barred.
579 @return array of barcodes
583 sub penalized_barcodes {
587 my $c = actor::card->table;
588 my $p = actor::user_standing_penalty->table;
591 SELECT DISTINCT c.barcode
593 JOIN $p p USING (usr)
594 JOIN config.standing_penalty csp ON (csp.id = p.standing_penalty)
595 WHERE csp.block_list IS NOT NULL
596 AND p.set_date < CURRENT_DATE
597 AND (p.stop_date IS NULL OR p.stop_date > CURRENT_DATE);
600 my $list = actor::user->db_Main->selectcol_arrayref($sql);
601 for my $bc ( @$list ) {
602 $client->respond($bc);
606 __PACKAGE__->register_method(
607 api_name => 'open-ils.storage.actor.user.penalized_barcodes',
610 method => 'penalized_barcodes',
611 signature => <<' NOTE',
612 Returns an array of barcodes that have blocking penalties.
613 @return array of barcodes
617 sub _clean_regex_chars {
620 # Escape metacharacters for SIMILAR TO
621 # (http://www.postgresql.org/docs/8.4/interactive/functions-matching.html)
622 $search =~ s/\_/\\_/g;
623 $search =~ s/\%/\\%/g;
624 $search =~ s/\|/\\|/g;
625 $search =~ s/\*/\\*/g;
626 $search =~ s/\+/\\+/g;
627 $search =~ s/\[/\\[/g;
628 $search =~ s/\]/\\]/g;
629 $search =~ s/\(/\\(/g;
630 $search =~ s/\)/\\)/g;
639 my $limit = shift || 1000;
641 my $inactive = shift;
643 my $search_org = shift || $ws_ou;
644 my $opt_boundary = shift || 0;
645 my $offset = shift || 0;
647 my $penalty_sort = 0;
649 my $strict_opt_in = OpenSRF::Utils::SettingsClient->new->config_value( share => user => 'opt_in' );
651 $sort = ['family_name','first_given_name'] unless ($$sort[0]);
654 if ($$sort[0] eq 'penalties') {
661 # group 2 = phone, ident
664 my $usr = join ' AND ', map { "evergreen.lowercase(CAST($_ AS text)) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
665 my @usrv = map { "^" . _clean_regex_chars($$search{$_}{value}) } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
667 my $addr = join ' AND ', map { "evergreen.lowercase(CAST($_ AS text)) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
668 my @addrv = map { "^" . _clean_regex_chars($$search{$_}{value}) } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
670 my $pv = _clean_regex_chars($$search{phone}{value});
671 my $iv = _clean_regex_chars($$search{ident}{value});
672 my $nv = _clean_regex_chars($$search{name}{value});
673 my $cv = _clean_regex_chars($$search{card}{value});
677 $card = 'JOIN (SELECT DISTINCT usr FROM actor.card WHERE evergreen.lowercase(barcode) LIKE ?||\'%\') AS card ON (card.usr = users.id)';
685 for my $p ( qw/day_phone evening_phone other_phone/ ) {
686 if ($pv =~ /^\d+$/) {
687 push @ps, "evergreen.lowercase(REGEXP_REPLACE($p, '[^0-9]', '', 'g')) ~ ?";
689 push @ps, "evergreen.lowercase($p) ~ ?";
691 push @phonev, "^$pv";
693 $phone = '(' . join(' OR ', @ps) . ')';
700 for my $i ( qw/ident_value ident_value2/ ) {
701 push @is, "evergreen.lowercase($i) ~ ?";
702 push @identv, "^$iv";
704 $ident = '(' . join(' OR ', @is) . ')';
711 for my $n ( qw/first_given_name second_given_name family_name/ ) {
712 push @ns, "evergreen.lowercase($n) ~ ?";
715 $name = '(' . join(' OR ', @ns) . ')';
718 my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
719 my $addr_where = $addr;
722 my $u_table = actor::user->table;
723 my $a_table = actor::user_address->table;
724 my $opt_in_table = actor::usr_org_unit_opt_in->table;
725 my $ou_table = actor::org_unit->table;
727 my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
728 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";
730 my $clone_select = '';
732 #$clone_select = "JOIN (SELECT cu.id as id FROM $a_table ca ".
733 # "JOIN $u_table cu ON (cu.mailing_address = ca.id OR cu.billing_address = ca.id) ".
734 # "WHERE $addr_where) AS clone ON (clone.id = users.id)" if ($addr_where);
739 $select = "$u_select INTERSECT $a_select";
743 } elsif ($addr_where) {
744 $select = "$a_select";
747 return undef if (!$select && !$card);
749 my $order_by = join ', ', map { 'evergreen.lowercase(CAST(users.'. (split / /,$_)[0] . ' AS text)) ' . (split / /,$_)[1] } @$sort;
750 my $distinct_list = join ', ', map { 'evergreen.lowercase(CAST(users.'. (split / /,$_)[0] . ' AS text))' } @$sort;
751 my $group_list = $distinct_list;
756 $inactive = 'AND users.active = TRUE';
759 if (!$ws_ou) { # XXX This should be required!!
760 $ws_ou = actor::org_unit->search( { parent_ou => undef } )->next->id;
763 my $descendants = "actor.org_unit_descendants($search_org)";
765 my $opt_in_where = '';
766 if (lc($strict_opt_in) eq 'true') {
767 $opt_in_where = "AND (";
768 $opt_in_where .= "EXISTS (select id FROM $opt_in_table ";
769 $opt_in_where .= " WHERE org_unit in (select (actor.org_unit_ancestors($ws_ou)).id)";
770 $opt_in_where .= " AND usr = users.id) ";
771 $opt_in_where .= "OR";
772 $opt_in_where .= " users.home_ou IN (select (actor.org_unit_descendants($ws_ou,$opt_boundary)).id))";
775 my $penalty_join = '';
777 $distinct_list = 'COUNT(penalties.id), ' . $distinct_list;
778 $order_by = 'COUNT(penalties.id) DESC, ' . $order_by;
779 unshift @$sort, 'COUNT(penalties.id)';
780 $penalty_join = <<" SQL";
781 LEFT JOIN actor.usr_standing_penalty penalties
782 ON (users.id = penalties.usr AND (penalties.stop_date IS NULL OR penalties.stop_date > NOW()))
786 $select = "JOIN ($select) AS search ON (search.id = users.id)" if ($select);
788 SELECT $distinct_list
789 FROM $u_table AS users $card
790 JOIN $descendants d ON (d.id = users.home_ou)
794 WHERE users.deleted = FALSE
803 return actor::user->db_Main->selectcol_arrayref($select, {Columns=>[scalar(@$sort)]}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv));
805 __PACKAGE__->register_method(
806 api_name => 'open-ils.storage.actor.user.crazy_search',
808 method => 'patron_search',
815 my $select =<<" SQL";
818 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
821 my $sth = actor::org_unit->db_Main->prepare_cached($select);
824 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
828 __PACKAGE__->register_method(
829 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
832 method => 'org_unit_list',
835 sub org_unit_type_list {
839 my $select =<<" SQL";
841 FROM actor.org_unit_type
842 ORDER BY depth, name;
845 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
848 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
852 __PACKAGE__->register_method(
853 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
856 method => 'org_unit_type_list',
859 sub org_unit_full_path {
864 return undef unless (@binds);
866 my $func = 'actor.org_unit_full_path(?)';
867 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
869 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
870 $sth->execute(@binds);
872 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
876 __PACKAGE__->register_method(
877 api_name => 'open-ils.storage.actor.org_unit.full_path',
880 method => 'org_unit_full_path',
883 sub org_unit_ancestors {
888 return undef unless ($id);
890 my $func = 'actor.org_unit_ancestors(?)';
892 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
895 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
896 ORDER BY t.depth, f.name;
898 $sth->execute(''.$id);
900 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
904 __PACKAGE__->register_method(
905 api_name => 'open-ils.storage.actor.org_unit.ancestors',
908 method => 'org_unit_ancestors',
911 sub org_unit_descendants {
917 return undef unless ($id);
919 my $func = 'actor.org_unit_descendants(?)';
920 if (defined $depth) {
921 $func = 'actor.org_unit_descendants(?,?)';
924 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
925 $sth->execute(''.$id, ''.$depth) if (defined $depth);
926 $sth->execute(''.$id) unless (defined $depth);
928 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
932 __PACKAGE__->register_method(
933 api_name => 'open-ils.storage.actor.org_unit.descendants',
936 method => 'org_unit_descendants',
939 sub fleshed_actor_stat_cat {
944 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
947 my $cat = actor::stat_cat->retrieve($sc);
950 my $sc_fm = $cat->to_fieldmapper;
951 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
952 $sc_fm->default_entries( [ map { $_->to_fieldmapper } $cat->default_entries ] );
954 $client->respond( $sc_fm );
960 __PACKAGE__->register_method(
961 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
964 method => 'fleshed_actor_stat_cat',
967 __PACKAGE__->register_method(
968 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
972 method => 'fleshed_actor_stat_cat',
975 #XXX Fix stored proc calls
976 sub ranged_actor_stat_cat_all {
981 return undef unless ($ou);
982 my $s_table = actor::stat_cat->table;
984 my $select = <<" SQL";
987 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
992 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
994 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
997 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
998 my $sc_fm = $sc->to_fieldmapper;
1000 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
1002 $sc_fm->default_entries(
1003 [ $self->method_lookup( 'open-ils.storage.actor.stat_cat_entry_default.ancestor.retrieve' )->run($ou,$sc->id) ]
1005 $client->respond( $sc_fm );
1010 __PACKAGE__->register_method(
1011 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
1015 method => 'ranged_actor_stat_cat_all',
1018 __PACKAGE__->register_method(
1019 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
1023 method => 'ranged_actor_stat_cat_all',
1026 #XXX Fix stored proc calls
1027 sub ranged_actor_stat_cat_entry {
1030 my $ou = ''.shift();
1031 my $sc = ''.shift();
1033 return undef unless ($ou);
1034 my $s_table = actor::stat_cat_entry->table;
1036 my $select = <<" SQL";
1039 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
1044 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
1045 $sth->execute($ou,$sc);
1047 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
1048 my $sce_fm = $sce->to_fieldmapper;
1049 $client->respond( $sce_fm );
1054 __PACKAGE__->register_method(
1055 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
1058 method => 'ranged_actor_stat_cat_entry',
1061 sub actor_stat_cat_entry_default {
1064 my $ou = ''.shift();
1065 my $sc = ''.shift();
1067 return undef unless ($ou);
1068 my $s_table = actor::stat_cat_entry_default->table;
1070 my $select = <<" SQL";
1073 WHERE owner = ? AND stat_cat = ?
1076 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
1077 $sth->execute($ou,$sc);
1079 for my $sced ( map { actor::stat_cat_entry_default->construct($_) } $sth->fetchall_hash ) {
1080 $client->respond( $sced->to_fieldmapper );
1085 __PACKAGE__->register_method(
1086 api_name => 'open-ils.storage.actor.stat_cat_entry_default.retrieve',
1089 method => 'actor_stat_cat_entry_default',
1092 sub actor_stat_cat_entry_default_ancestor {
1095 my $ou = ''.shift();
1096 my $sc = ''.shift();
1098 return undef unless ($ou);
1099 my $s_table = actor::stat_cat_entry_default->table;
1101 my $select = <<" SQL";
1104 JOIN actor.org_unit_ancestors(?) p ON (p.id = s.owner)
1108 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
1109 $sth->execute($ou,$sc);
1111 my @sced = map { actor::stat_cat_entry_default->construct($_) } $sth->fetchall_hash;
1113 my $ancestor_sced = pop @sced;
1115 $client->respond( $ancestor_sced->to_fieldmapper ) if $ancestor_sced;
1119 __PACKAGE__->register_method(
1120 api_name => 'open-ils.storage.actor.stat_cat_entry_default.ancestor.retrieve',
1123 method => 'actor_stat_cat_entry_default_ancestor',