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 juvenile IS TRUE
37 AND AGE(dob) > COALESCE( BTRIM( (
38 SELECT value FROM actor.org_unit_ancestor_setting(
39 'global.juvenile_age_threshold', home_ou)),'"' ), ?)::INTERVAL
42 my $sth = actor::user->db_Main->prepare_cached($sql);
43 $sth->execute($adult_age);
47 __PACKAGE__->register_method(
48 api_name => 'open-ils.storage.actor.user.juvenile_to_adult',
50 method => 'juv_to_adult',
60 SUM(COALESCE((SELECT SUM(b.amount) FROM money.billing b WHERE b.voided IS FALSE AND b.xact = x.id),0.0)) -
61 SUM(COALESCE((SELECT SUM(p.amount) FROM money.payment p WHERE p.voided IS FALSE AND p.xact = x.id),0.0))
62 FROM money.billable_xact x
63 WHERE x.usr = ? AND x.xact_finish IS NULL
67 my (undef,$val) = actor::user->db_Main->selectrow_array($sql, {}, $usr);
71 __PACKAGE__->register_method(
72 api_name => 'open-ils.storage.actor.user.total_owed',
74 method => 'usr_total_owed',
77 sub usr_breakdown_out {
82 $self->method_lookup('open-ils.storage.transaction.begin')->run($client);
84 my $out_sql = <<" SQL";
86 FROM action.circulation
88 AND checkin_time IS NULL
89 AND ( (fine_interval >= '1 day' AND due_date >= 'today')
90 OR (fine_interval < '1 day' AND due_date > 'now' ))
91 AND (stop_fines IS NULL
92 OR stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE'))
95 my $out = actor::user->db_Main->selectcol_arrayref($out_sql, {}, $usr);
97 my $od_sql = <<" SQL";
99 FROM action.circulation
101 AND checkin_time IS NULL
102 AND ( (fine_interval >= '1 day' AND due_date < 'today')
103 OR (fine_interval < '1 day' AND due_date < 'now' ))
104 AND (stop_fines IS NULL
105 OR stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE'))
108 my $od = actor::user->db_Main->selectcol_arrayref($od_sql, {}, $usr);
110 my $lost_sql = <<" SQL";
112 FROM action.circulation
113 WHERE usr = ? AND checkin_time IS NULL AND xact_finish IS NULL AND stop_fines = 'LOST'
116 my $lost = actor::user->db_Main->selectcol_arrayref($lost_sql, {}, $usr);
118 my $cl_sql = <<" SQL";
120 FROM action.circulation
121 WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'CLAIMSRETURNED'
124 my $cl = actor::user->db_Main->selectcol_arrayref($cl_sql, {}, $usr);
126 my $lo_sql = <<" SQL";
128 FROM action.circulation
129 WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'LONGOVERDUE'
132 my $lo = actor::user->db_Main->selectcol_arrayref($lo_sql, {}, $usr);
134 $self->method_lookup('open-ils.storage.transaction.rollback')->run($client);
136 if ($self->api_name =~/count$/o) {
137 return { total => scalar(@$out) + scalar(@$od) + scalar(@$lost) + scalar(@$cl) + scalar(@$lo),
138 out => scalar(@$out),
139 overdue => scalar(@$od),
140 lost => scalar(@$lost),
141 claims_returned => scalar(@$cl),
142 long_overdue => scalar(@$lo),
146 return { out => $out,
149 claims_returned => $cl,
153 __PACKAGE__->register_method(
154 api_name => 'open-ils.storage.actor.user.checked_out',
156 method => 'usr_breakdown_out',
158 __PACKAGE__->register_method(
159 api_name => 'open-ils.storage.actor.user.checked_out.count',
161 method => 'usr_breakdown_out',
171 FROM action.circulation
172 WHERE usr = ? AND checkin_time IS NULL
175 my ($val) = actor::user->db_Main->selectrow_array($sql, {}, $usr);
179 __PACKAGE__->register_method(
180 api_name => 'open-ils.storage.actor.user.total_out',
182 method => 'usr_total_out',
189 local $OpenILS::Application::Storage::WRITE = 1;
191 my $delete_sql = <<" SQL";
192 DELETE FROM actor.org_unit_proximity;
195 my $insert_sql = <<" SQL";
196 INSERT INTO actor.org_unit_proximity (from_org, to_org, prox)
199 actor.org_unit_proximity(l.id,r.id)
200 FROM actor.org_unit l,
204 actor::org_unit_proximity->db_Main->do($delete_sql);
205 actor::org_unit_proximity->db_Main->do($insert_sql);
209 __PACKAGE__->register_method(
210 api_name => 'open-ils.storage.actor.org_unit.refresh_proximity',
212 method => 'calc_proximity',
215 sub make_hoo_spanset {
217 return undef unless $hoo;
219 my $today = shift || DateTime->now;
221 my $tz = OpenSRF::AppSession->create('open-ils.actor')->request(
222 'open-ils.actor.ou_setting.ancestor_default' => $hoo->id.'' => 'org_unit.timezone'
223 )->gather(1) || DateTime::TimeZone->new( name => 'local' )->name;
225 my $current_dow = $today->day_of_week_0;
227 my $spanset = DateTime::SpanSet->empty_set;
228 for my $d ( 0 .. 6 ) {
230 my $omethod = 'dow_'.$d.'_open';
231 my $cmethod = 'dow_'.$d.'_close';
233 my $open = interval_to_seconds($hoo->$omethod());
234 my $close = interval_to_seconds($hoo->$cmethod());
236 next if ($open == $close && $open == 0);
238 my $dow_offset = ($d - $current_dow) * $one_day;
239 $close += $one_day if ($close <= $open);
241 $spanset = $spanset->union(
243 start => $today->clone->add( seconds => $dow_offset + $open ),
244 end => $today->clone->add( seconds => $dow_offset + $close )
249 return $spanset->complement;
252 sub make_closure_spanset {
253 my $closures = shift;
254 return undef unless $closures;
256 my $spanset = DateTime::SpanSet->empty_set;
257 for my $k ( keys %$closures ) {
258 my $c = $$closures{$k};
260 $spanset = $spanset->union(
262 start => $_dt_parser->parse_datetime(cleanse_ISO8601($c->{close_start})),
263 end => $_dt_parser->parse_datetime(cleanse_ISO8601($c->{close_end}))
271 sub new_org_closed_overlap {
276 my $direction = shift || 0;
277 my $no_hoo = shift || 0;
279 return undef unless ($date && $ou);
281 # we're given a date and a direction, find any closures that contain the date
282 my $t = actor::org_unit::closed_date->table;
288 ORDER BY close_start ASC, close_end DESC
292 $date = cleanse_ISO8601($date);
294 my $target_date = $_dt_parser->parse_datetime( $date );
295 my ($begin, $end) = ($target_date, $target_date);
297 # create a spanset from the closures that contain the $date
298 my $closure_spanset = make_closure_spanset(
299 actor::org_unit::closed_date->db_Main->selectall_hashref( $sql, 'id', {}, $date, $ou )
302 if ($closure_spanset && $closure_spanset->intersects( $target_date )) {
303 my $closure_intersection = $closure_spanset->intersection( $target_date );
304 $begin = $closure_intersection->min;
305 $end = $closure_intersection->max;
307 if ( $direction <= 0 ) {
308 $begin->subtract( minutes => 1 );
310 while ( my $_b = new_org_closed_overlap($self, $client, $ou, $begin->strftime('%FT%T%z'), -1, 1 ) ) {
311 $begin = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{start}) );
315 if ( $direction >= 0 ) {
316 $end->add( minutes => 1 );
318 while ( my $_a = new_org_closed_overlap($self, $client, $ou, $end->strftime('%FT%T%z'), 1, 1 ) ) {
319 $end = $_dt_parser->parse_datetime( cleanse_ISO8601($_a->{end}) );
326 my $begin_hoo = make_hoo_spanset(actor::org_unit::hours_of_operation->retrieve($ou), $begin);
327 my $end_hoo = make_hoo_spanset(actor::org_unit::hours_of_operation->retrieve($ou), $end );
330 if ( $begin_hoo && $direction <= 0 && $begin_hoo->intersects($begin) ) {
331 my $hoo_intersection = $begin_hoo->intersection( $begin );
332 $begin = $hoo_intersection->min;
333 $begin->subtract( minutes => 1 );
335 while ( my $_b = new_org_closed_overlap($self, $client, $ou, $begin->strftime('%FT%T%z'), -1 ) ) {
336 $begin = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{start}) );
340 if ( $end_hoo && $direction >= 0 && $end_hoo->intersects($end) ) {
341 my $hoo_intersection = $end_hoo->intersection( $end );
342 $end = $hoo_intersection->max;
343 $end->add( minutes => 1 );
346 while ( my $_b = new_org_closed_overlap($self, $client, $ou, $end->strftime('%FT%T%z'), -1 ) ) {
347 $end = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{end}) );
352 my $start = $begin->strftime('%FT%T%z');
353 my $stop = $end->strftime('%FT%T%z');
355 return undef if ($start eq $stop);
356 return { start => $start, end => $stop };
358 __PACKAGE__->register_method(
359 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
361 method => 'new_org_closed_overlap',
364 sub org_closed_overlap {
369 my $direction = shift || 0;
370 my $no_hoo = shift || 0;
372 return undef unless ($date && $ou);
374 my $t = actor::org_unit::closed_date->table;
378 WHERE ? between close_start and close_end
380 ORDER BY close_start ASC, close_end DESC
384 $date = cleanse_ISO8601($date);
385 my ($begin, $end) = ($date,$date);
387 my $hoo = actor::org_unit::hours_of_operation->retrieve($ou);
389 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $date, $ou )) {
390 $begin = cleanse_ISO8601($closure->{close_start});
391 $end = cleanse_ISO8601($closure->{close_end});
393 if ( $direction <= 0 ) {
394 $before = $_dt_parser->parse_datetime( $begin );
395 $before->subtract( minutes => 1 );
397 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->strftime('%FT%T%z'), -1, 1 ) ) {
398 $before = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{start}) );
400 $begin = cleanse_ISO8601($before->strftime('%FT%T%z'));
403 if ( $direction >= 0 ) {
404 $after = $_dt_parser->parse_datetime( $end );
405 $after->add( minutes => 1 );
407 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->strftime('%FT%T%z'), 1, 1 ) ) {
408 $after = $_dt_parser->parse_datetime( cleanse_ISO8601($_a->{end}) );
410 $end = cleanse_ISO8601($after->strftime('%FT%T%z'));
417 if ( $direction <= 0 ) {
418 my $begin_dow = $_dt_parser->parse_datetime( $begin )->day_of_week_0;
419 my $begin_open_meth = "dow_".$begin_dow."_open";
420 my $begin_close_meth = "dow_".$begin_dow."_close";
423 while ($hoo->$begin_open_meth eq '00:00:00' and $hoo->$begin_close_meth eq '00:00:00') {
424 $begin = cleanse_ISO8601($_dt_parser->parse_datetime( $begin )->subtract( days => 1)->strftime('%FT%T%z'));
428 last if ($count > 6);
429 $begin_open_meth = "dow_".$begin_dow."_open";
430 $begin_close_meth = "dow_".$begin_dow."_close";
433 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $begin, $ou )) {
434 $before = $_dt_parser->parse_datetime( $begin );
435 $before->subtract( minutes => 1 );
436 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->strftime('%FT%T%z'), -1 ) ) {
437 $before = $_dt_parser->parse_datetime( cleanse_ISO8601($_b->{start}) );
442 if ( $direction >= 0 ) {
443 my $end_dow = $_dt_parser->parse_datetime( $end )->day_of_week_0;
444 my $end_open_meth = "dow_".$end_dow."_open";
445 my $end_close_meth = "dow_".$end_dow."_close";
448 while ($hoo->$end_open_meth eq '00:00:00' and $hoo->$end_close_meth eq '00:00:00') {
449 $end = cleanse_ISO8601($_dt_parser->parse_datetime( $end )->add( days => 1)->strftime('%FT%T%z'));
453 last if ($count > 6);
454 $end_open_meth = "dow_".$end_dow."_open";
455 $end_close_meth = "dow_".$end_dow."_close";
458 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $end, $ou )) {
459 $after = $_dt_parser->parse_datetime( $end );
460 $after->add( minutes => 1 );
462 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->strftime('%FT%T%z'), 1 ) ) {
463 $after = $_dt_parser->parse_datetime( cleanse_ISO8601($_a->{end}) );
465 $end = cleanse_ISO8601($after->strftime('%FT%T%z'));
472 if ($begin eq $date && $end eq $date) {
476 return { start => $begin, end => $end };
478 __PACKAGE__->register_method(
479 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
481 method => 'org_closed_overlap',
484 sub user_by_barcode {
487 my @barcodes = shift;
489 return undef unless @barcodes;
491 for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
493 if (@barcodes == 1) {
494 return $card->usr->to_fieldmapper;
496 $client->respond( $card->usr->to_fieldmapper);
500 __PACKAGE__->register_method(
501 api_name => 'open-ils.storage.direct.actor.user.search.barcode',
503 method => 'user_by_barcode',
512 my $c = actor::card->table;
513 my $p = actor::user->table;
515 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
517 my $list = actor::user->db_Main->selectcol_arrayref($sql);
518 for my $bc ( @$list ) {
519 $client->respond($bc);
523 __PACKAGE__->register_method(
524 api_name => 'open-ils.storage.actor.user.lost_barcodes',
527 method => 'lost_barcodes',
528 signature => <<' NOTE',
529 Returns an array of barcodes that belong to lost cards.
530 @return array of barcodes
534 sub expired_barcodes {
538 my $c = actor::card->table;
539 my $p = actor::user->table;
541 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
543 my $list = actor::user->db_Main->selectcol_arrayref($sql);
544 for my $bc ( @$list ) {
545 $client->respond($bc);
549 __PACKAGE__->register_method(
550 api_name => 'open-ils.storage.actor.user.expired_barcodes',
553 method => 'expired_barcodes',
554 signature => <<' NOTE',
555 Returns an array of barcodes that are currently expired.
556 @return array of barcodes
560 sub barred_barcodes {
564 my $c = actor::card->table;
565 my $p = actor::user->table;
567 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
569 my $list = actor::user->db_Main->selectcol_arrayref($sql);
570 for my $bc ( @$list ) {
571 $client->respond($bc);
575 __PACKAGE__->register_method(
576 api_name => 'open-ils.storage.actor.user.barred_barcodes',
579 method => 'barred_barcodes',
580 signature => <<' NOTE',
581 Returns an array of barcodes that are currently barred.
582 @return array of barcodes
586 sub penalized_barcodes {
590 my $c = actor::card->table;
591 my $p = actor::user_standing_penalty->table;
594 SELECT DISTINCT c.barcode
596 JOIN $p p USING (usr)
597 JOIN config.standing_penalty csp ON (csp.id = p.standing_penalty)
598 WHERE csp.block_list IS NOT NULL
599 AND p.set_date < CURRENT_DATE
600 AND (p.stop_date IS NULL OR p.stop_date > CURRENT_DATE);
603 my $list = actor::user->db_Main->selectcol_arrayref($sql);
604 for my $bc ( @$list ) {
605 $client->respond($bc);
609 __PACKAGE__->register_method(
610 api_name => 'open-ils.storage.actor.user.penalized_barcodes',
613 method => 'penalized_barcodes',
614 signature => <<' NOTE',
615 Returns an array of barcodes that have blocking penalties.
616 @return array of barcodes
620 sub _clean_regex_chars {
623 # Escape metacharacters for SIMILAR TO
624 # (http://www.postgresql.org/docs/8.4/interactive/functions-matching.html)
625 $search =~ s/\_/\\_/g;
626 $search =~ s/\%/\\%/g;
627 $search =~ s/\|/\\|/g;
628 $search =~ s/\*/\\*/g;
629 $search =~ s/\+/\\+/g;
630 $search =~ s/\[/\\[/g;
631 $search =~ s/\]/\\]/g;
632 $search =~ s/\(/\\(/g;
633 $search =~ s/\)/\\)/g;
642 my $limit = shift || 1000;
644 my $inactive = shift;
646 my $search_org = shift || $ws_ou;
647 my $opt_boundary = shift || 0;
648 my $offset = shift || 0;
650 my $penalty_sort = 0;
652 my $strict_opt_in = OpenSRF::Utils::SettingsClient->new->config_value( share => user => 'opt_in' );
654 $sort = ['family_name','first_given_name'] unless ($$sort[0]);
657 if ($$sort[0] eq 'penalties') {
664 # group 2 = phone, ident
667 my $usr = join ' AND ', map { "evergreen.lowercase(CAST($_ AS text)) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
668 my @usrv = map { "^" . _clean_regex_chars($$search{$_}{value}) } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
670 my $addr = join ' AND ', map { "evergreen.lowercase(CAST($_ AS text)) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
671 my @addrv = map { "^" . _clean_regex_chars($$search{$_}{value}) } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
673 my $pv = _clean_regex_chars($$search{phone}{value});
674 my $iv = _clean_regex_chars($$search{ident}{value});
675 my $nv = _clean_regex_chars($$search{name}{value});
676 my $cv = _clean_regex_chars($$search{card}{value});
680 $card = 'JOIN (SELECT DISTINCT usr FROM actor.card WHERE evergreen.lowercase(barcode) LIKE ?||\'%\') AS card ON (card.usr = users.id)';
688 for my $p ( qw/day_phone evening_phone other_phone/ ) {
689 if ($pv =~ /^\d+$/) {
690 push @ps, "evergreen.lowercase(REGEXP_REPLACE($p, '[^0-9]', '', 'g')) ~ ?";
692 push @ps, "evergreen.lowercase($p) ~ ?";
694 push @phonev, "^$pv";
696 $phone = '(' . join(' OR ', @ps) . ')';
703 for my $i ( qw/ident_value ident_value2/ ) {
704 push @is, "evergreen.lowercase($i) ~ ?";
705 push @identv, "^$iv";
707 $ident = '(' . join(' OR ', @is) . ')';
714 for my $n ( qw/first_given_name second_given_name family_name/ ) {
715 push @ns, "evergreen.lowercase($n) ~ ?";
718 $name = '(' . join(' OR ', @ns) . ')';
721 my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
722 my $addr_where = $addr;
725 my $u_table = actor::user->table;
726 my $a_table = actor::user_address->table;
727 my $opt_in_table = actor::usr_org_unit_opt_in->table;
728 my $ou_table = actor::org_unit->table;
730 my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
731 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";
733 my $clone_select = '';
735 #$clone_select = "JOIN (SELECT cu.id as id FROM $a_table ca ".
736 # "JOIN $u_table cu ON (cu.mailing_address = ca.id OR cu.billing_address = ca.id) ".
737 # "WHERE $addr_where) AS clone ON (clone.id = users.id)" if ($addr_where);
742 $select = "$u_select INTERSECT $a_select";
746 } elsif ($addr_where) {
747 $select = "$a_select";
750 return undef if (!$select && !$card);
752 my $order_by = join ', ', map { 'evergreen.lowercase(CAST(users.'. (split / /,$_)[0] . ' AS text)) ' . (split / /,$_)[1] } @$sort;
753 my $distinct_list = join ', ', map { 'evergreen.lowercase(CAST(users.'. (split / /,$_)[0] . ' AS text))' } @$sort;
754 my $group_list = $distinct_list;
759 $inactive = 'AND users.active = TRUE';
762 if (!$ws_ou) { # XXX This should be required!!
763 $ws_ou = actor::org_unit->search( { parent_ou => undef } )->next->id;
766 my $descendants = "actor.org_unit_descendants($search_org)";
768 my $opt_in_where = '';
769 if (lc($strict_opt_in) eq 'true') {
770 $opt_in_where = "AND (";
771 $opt_in_where .= "EXISTS (select id FROM $opt_in_table ";
772 $opt_in_where .= " WHERE org_unit in (select (actor.org_unit_ancestors($ws_ou)).id)";
773 $opt_in_where .= " AND usr = users.id) ";
774 $opt_in_where .= "OR";
775 $opt_in_where .= " users.home_ou IN (select (actor.org_unit_descendants($ws_ou,$opt_boundary)).id))";
778 my $penalty_join = '';
780 $distinct_list = 'COUNT(penalties.id), ' . $distinct_list;
781 $order_by = 'COUNT(penalties.id) DESC, ' . $order_by;
782 unshift @$sort, 'COUNT(penalties.id)';
783 $penalty_join = <<" SQL";
784 LEFT JOIN actor.usr_standing_penalty penalties
785 ON (users.id = penalties.usr AND (penalties.stop_date IS NULL OR penalties.stop_date > NOW()))
789 $select = "JOIN ($select) AS search ON (search.id = users.id)" if ($select);
791 SELECT $distinct_list
792 FROM $u_table AS users $card
793 JOIN $descendants d ON (d.id = users.home_ou)
797 WHERE users.deleted = FALSE
806 return actor::user->db_Main->selectcol_arrayref($select, {Columns=>[scalar(@$sort)]}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv));
808 __PACKAGE__->register_method(
809 api_name => 'open-ils.storage.actor.user.crazy_search',
811 method => 'patron_search',
818 my $select =<<" SQL";
821 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
824 my $sth = actor::org_unit->db_Main->prepare_cached($select);
827 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
831 __PACKAGE__->register_method(
832 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
835 method => 'org_unit_list',
838 sub org_unit_type_list {
842 my $select =<<" SQL";
844 FROM actor.org_unit_type
845 ORDER BY depth, name;
848 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
851 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
855 __PACKAGE__->register_method(
856 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
859 method => 'org_unit_type_list',
862 sub org_unit_full_path {
867 return undef unless (@binds);
869 my $func = 'actor.org_unit_full_path(?)';
870 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
872 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
873 $sth->execute(@binds);
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.full_path',
883 method => 'org_unit_full_path',
886 sub org_unit_ancestors {
891 return undef unless ($id);
893 my $func = 'actor.org_unit_ancestors(?)';
895 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
898 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
899 ORDER BY t.depth, f.name;
901 $sth->execute(''.$id);
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.ancestors',
911 method => 'org_unit_ancestors',
914 sub org_unit_descendants {
920 return undef unless ($id);
922 my $func = 'actor.org_unit_descendants(?)';
923 if (defined $depth) {
924 $func = 'actor.org_unit_descendants(?,?)';
927 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
928 $sth->execute(''.$id, ''.$depth) if (defined $depth);
929 $sth->execute(''.$id) unless (defined $depth);
931 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
935 __PACKAGE__->register_method(
936 api_name => 'open-ils.storage.actor.org_unit.descendants',
939 method => 'org_unit_descendants',
942 sub fleshed_actor_stat_cat {
947 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
950 my $cat = actor::stat_cat->retrieve($sc);
953 my $sc_fm = $cat->to_fieldmapper;
954 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
955 $sc_fm->default_entries( [ map { $_->to_fieldmapper } $cat->default_entries ] );
957 $client->respond( $sc_fm );
963 __PACKAGE__->register_method(
964 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
967 method => 'fleshed_actor_stat_cat',
970 __PACKAGE__->register_method(
971 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
975 method => 'fleshed_actor_stat_cat',
978 #XXX Fix stored proc calls
979 sub ranged_actor_stat_cat_all {
984 return undef unless ($ou);
985 my $s_table = actor::stat_cat->table;
987 my $select = <<" SQL";
990 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
995 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
997 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
1000 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
1001 my $sc_fm = $sc->to_fieldmapper;
1003 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
1005 $sc_fm->default_entries(
1006 [ $self->method_lookup( 'open-ils.storage.actor.stat_cat_entry_default.ancestor.retrieve' )->run($ou,$sc->id) ]
1008 $client->respond( $sc_fm );
1013 __PACKAGE__->register_method(
1014 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
1018 method => 'ranged_actor_stat_cat_all',
1021 __PACKAGE__->register_method(
1022 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
1026 method => 'ranged_actor_stat_cat_all',
1029 #XXX Fix stored proc calls
1030 sub ranged_actor_stat_cat_entry {
1033 my $ou = ''.shift();
1034 my $sc = ''.shift();
1036 return undef unless ($ou);
1037 my $s_table = actor::stat_cat_entry->table;
1039 my $select = <<" SQL";
1042 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
1047 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
1048 $sth->execute($ou,$sc);
1050 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
1051 my $sce_fm = $sce->to_fieldmapper;
1052 $client->respond( $sce_fm );
1057 __PACKAGE__->register_method(
1058 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
1061 method => 'ranged_actor_stat_cat_entry',
1064 sub actor_stat_cat_entry_default {
1067 my $ou = ''.shift();
1068 my $sc = ''.shift();
1070 return undef unless ($ou);
1071 my $s_table = actor::stat_cat_entry_default->table;
1073 my $select = <<" SQL";
1076 WHERE owner = ? AND stat_cat = ?
1079 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
1080 $sth->execute($ou,$sc);
1082 for my $sced ( map { actor::stat_cat_entry_default->construct($_) } $sth->fetchall_hash ) {
1083 $client->respond( $sced->to_fieldmapper );
1088 __PACKAGE__->register_method(
1089 api_name => 'open-ils.storage.actor.stat_cat_entry_default.retrieve',
1092 method => 'actor_stat_cat_entry_default',
1095 sub actor_stat_cat_entry_default_ancestor {
1098 my $ou = ''.shift();
1099 my $sc = ''.shift();
1101 return undef unless ($ou);
1102 my $s_table = actor::stat_cat_entry_default->table;
1104 my $select = <<" SQL";
1107 JOIN actor.org_unit_ancestors(?) p ON (p.id = s.owner)
1111 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
1112 $sth->execute($ou,$sc);
1114 my @sced = map { actor::stat_cat_entry_default->construct($_) } $sth->fetchall_hash;
1116 my $ancestor_sced = pop @sced;
1118 $client->respond( $ancestor_sced->to_fieldmapper ) if $ancestor_sced;
1122 __PACKAGE__->register_method(
1123 api_name => 'open-ils.storage.actor.stat_cat_entry_default.ancestor.retrieve',
1126 method => 'actor_stat_cat_entry_default_ancestor',