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;
9 use DateTime::Format::ISO8601;
12 my $_dt_parser = DateTime::Format::ISO8601->new;
14 my $log = 'OpenSRF::Utils::Logger';
16 sub new_usergroup_id {
17 return actor::user->db_Main->selectrow_array("select nextval('actor.usr_usrgroup_seq'::regclass)");
19 __PACKAGE__->register_method(
20 api_name => 'open-ils.storage.actor.user.group_id.new',
22 method => 'new_usergroup_id',
25 sub org_closed_overlap {
30 my $direction = shift || 0;
32 return undef unless ($date && $ou);
34 my $t = actor::org_unit::closed_date->table;
38 WHERE ? between close_start and close_end
40 ORDER BY close_start ASC, close_end DESC
43 my $sth = actor::org_unit::closed_date->db_Main->prepare( $sql );
44 $sth->execute($date, $ou);
47 while (my $closure = $sth->fetchrow_hashref) {
48 $begin ||= $closure->{close_start};
49 $end = $closure->{close_end};
51 my $before = $_dt_parser->parse_datetime( clense_ISO8601($begin) );
52 $before->subtract( seconds => 1 );
53 my $after = $_dt_parser->parse_datetime( clense_ISO8601($end) );
54 $after->add( seconds => 1 );
56 if ( $direction <= 0 ) {
57 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->iso8601, -1 ) ) {
58 $before = $_dt_parser->parse_datetime( clense_ISO8601($_b->{start}) );
62 if ( $direction >= 0 ) {
63 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->iso8601, 1 ) ) {
64 $after = $_dt_parser->parse_datetime( clense_ISO8601($_a->{end}) );
68 $begin = clense_ISO8601($before->iso8601);
69 $end = clense_ISO8601($after->iso8601);
73 return { start => $begin, end => $end };
78 __PACKAGE__->register_method(
79 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
81 method => 'org_closed_overlap',
89 return undef unless @barcodes;
91 for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
94 return $card->usr->to_fieldmapper;
96 $client->respond( $card->usr->to_fieldmapper);
100 __PACKAGE__->register_method(
101 api_name => 'open-ils.storage.direct.actor.user.search.barcode',
103 method => 'user_by_barcode',
112 my $c = actor::card->table;
113 my $p = actor::user->table;
115 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
117 my $list = actor::user->db_Main->selectcol_arrayref($sql);
118 for my $bc ( @$list ) {
119 $client->respond($bc);
123 __PACKAGE__->register_method(
124 api_name => 'open-ils.storage.actor.user.lost_barcodes',
127 method => 'lost_barcodes',
128 signature => <<' NOTE',
129 Returns an array of barcodes that belong to lost cards.
130 @return array of barcodes
134 sub expired_barcodes {
138 my $c = actor::card->table;
139 my $p = actor::user->table;
141 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
143 my $list = actor::user->db_Main->selectcol_arrayref($sql);
144 for my $bc ( @$list ) {
145 $client->respond($bc);
149 __PACKAGE__->register_method(
150 api_name => 'open-ils.storage.actor.user.expired_barcodes',
153 method => 'expired_barcodes',
154 signature => <<' NOTE',
155 Returns an array of barcodes that are currently expired.
156 @return array of barcodes
160 sub barred_barcodes {
164 my $c = actor::card->table;
165 my $p = actor::user->table;
167 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
169 my $list = actor::user->db_Main->selectcol_arrayref($sql);
170 for my $bc ( @$list ) {
171 $client->respond($bc);
175 __PACKAGE__->register_method(
176 api_name => 'open-ils.storage.actor.user.barred_barcodes',
179 method => 'barred_barcodes',
180 signature => <<' NOTE',
181 Returns an array of barcodes that are currently barred.
182 @return array of barcodes
186 sub penalized_barcodes {
191 my $c = actor::card->table;
192 my $p = actor::user_standing_penalty->table;
194 my $sql = "SELECT c.barcode FROM $c c JOIN $p p USING (usr)";
197 $sql .= ' WHERE penalty_type NOT IN ('. join(',', map { '?' } @ignore) . ')';
200 $sql .= ' GROUP BY c.barcode;';
202 my $list = actor::user->db_Main->selectcol_arrayref($sql, {}, @ignore);
203 for my $bc ( @$list ) {
204 $client->respond($bc);
208 __PACKAGE__->register_method(
209 api_name => 'open-ils.storage.actor.user.penalized_barcodes',
212 method => 'penalized_barcodes',
213 signature => <<' NOTE',
214 Returns an array of barcodes that have penalties not listed
215 as a parameter. Supply a list of any penalty types that should
216 not stop a patron from checking out materials.
218 @param ignore_list Penalty type to ignore
219 @return array of barcodes
228 my $limit = shift || 1000;
230 my $inactive = shift;
231 $sort = ['family_name','first_given_name'] unless ($$sort[0]);
235 # group 2 = phone, ident
237 my $usr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
238 my @usrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
240 my $addr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
241 my @addrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
243 my $pv = $$search{phone}{value};
244 my $iv = $$search{ident}{value};
245 my $nv = $$search{name}{value};
251 for my $p ( qw/day_phone evening_phone other_phone/ ) {
252 push @ps, "LOWER($p) ~ ?";
253 push @phonev, "^$pv";
255 $phone = '(' . join(' OR ', @ps) . ')';
262 for my $i ( qw/ident_value ident_value2/ ) {
263 push @is, "LOWER($i) ~ ?";
264 push @identv, "^$iv";
266 $ident = '(' . join(' OR ', @is) . ')';
273 for my $n ( qw/first_given_name second_given_name family_name/ ) {
274 push @ns, "LOWER($i) ~ ?";
277 $name = '(' . join(' OR ', @ns) . ')';
280 my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
281 my $addr_where = $addr;
284 my $u_table = actor::user->table;
285 my $a_table = actor::user_address->table;
287 my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
288 my $a_select = "SELECT usr as id FROM $a_table a WHERE $addr_where";
293 $select = "$u_select INTERSECT $a_select";
297 } elsif ($addr_where) {
303 my $order_by = join ', ', map { 'users.'. $_} @$sort;
308 $inactive = 'AND users.active = FALSE';
313 FROM $u_table AS users
314 JOIN ($select) AS search
316 WHERE users.deleted = FALSE $inactive
321 return actor::user->db_Main->selectcol_arrayref($select, {}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv));
323 __PACKAGE__->register_method(
324 api_name => 'open-ils.storage.actor.user.crazy_search',
326 method => 'patron_search',
329 =comment not gonna use it...
334 my $searches = shift;
336 return undef unless (defined $searches);
338 for my $usr ( actor::user->search( $searches ) ) {
340 $client->respond( flesh_user( $usr ) );
344 __PACKAGE__->register_method(
345 api_name => 'open-ils.storage.fleshed.actor.user.search',
347 method => 'fleshed_search',
352 sub fleshed_search_like {
355 my $searches = shift;
357 return undef unless (defined $searches);
359 for my $usr ( actor::user->search_like( $searches ) ) {
361 $client->respond( flesh_user( $usr ) );
365 __PACKAGE__->register_method(
366 api_name => 'open-ils.storage.fleshed.actor.user.search_like',
368 method => 'user_by_barcode',
373 sub retrieve_fleshed_user {
378 return undef unless @ids;
380 @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
382 $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
386 __PACKAGE__->register_method(
387 api_name => 'open-ils.storage.fleshed.actor.user.retrieve',
389 method => 'retrieve_fleshed_user',
392 __PACKAGE__->register_method(
393 api_name => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
395 method => 'retrieve_fleshed_user',
404 my $standing = $usr->standing;
405 my $profile = $usr->profile;
406 my $ident_type = $usr->ident_type;
408 my $maddress = $usr->mailing_address;
409 my $baddress = $usr->billing_address;
410 my $card = $usr->card;
412 my @addresses = $usr->addresses;
413 my @cards = $usr->cards;
415 my $usr_fm = $usr->to_fieldmapper;
416 $usr_fm->standing( $standing->to_fieldmapper );
417 $usr_fm->profile( $profile->to_fieldmapper );
418 $usr_fm->ident_type( $ident_type->to_fieldmapper );
420 $usr_fm->card( $card->to_fieldmapper );
421 $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
422 $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
424 $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
425 $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
436 my $select =<<" SQL";
439 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
442 my $sth = actor::org_unit->db_Main->prepare_cached($select);
445 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
449 __PACKAGE__->register_method(
450 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
453 method => 'org_unit_list',
456 sub org_unit_type_list {
460 my $select =<<" SQL";
462 FROM actor.org_unit_type
463 ORDER BY depth, name;
466 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
469 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
473 __PACKAGE__->register_method(
474 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
477 method => 'org_unit_type_list',
480 sub org_unit_full_path {
485 return undef unless (@binds);
487 my $func = 'actor.org_unit_full_path(?)';
488 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
490 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
491 $sth->execute(@binds);
493 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
497 __PACKAGE__->register_method(
498 api_name => 'open-ils.storage.actor.org_unit.full_path',
501 method => 'org_unit_full_path',
504 sub org_unit_ancestors {
509 return undef unless ($id);
511 my $func = 'actor.org_unit_ancestors(?)';
513 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
516 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
517 ORDER BY t.depth, f.name;
519 $sth->execute(''.$id);
521 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
525 __PACKAGE__->register_method(
526 api_name => 'open-ils.storage.actor.org_unit.ancestors',
529 method => 'org_unit_ancestors',
532 sub org_unit_descendants {
538 return undef unless ($id);
540 my $func = 'actor.org_unit_descendants(?)';
541 if (defined $depth) {
542 $func = 'actor.org_unit_descendants(?,?)';
545 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
546 $sth->execute(''.$id, ''.$depth) if (defined $depth);
547 $sth->execute(''.$id) unless (defined $depth);
549 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
553 __PACKAGE__->register_method(
554 api_name => 'open-ils.storage.actor.org_unit.descendants',
557 method => 'org_unit_descendants',
564 for my $rec ( actor::profile->retrieve_all ) {
565 $client->respond( $rec->to_fieldmapper );
570 __PACKAGE__->register_method(
571 method => 'profile_all',
572 api_name => 'open-ils.storage.direct.actor.profile.retrieve.all',
577 sub fleshed_actor_stat_cat {
582 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
585 my $cat = actor::stat_cat->retrieve($sc);
588 my $sc_fm = $cat->to_fieldmapper;
589 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
591 $client->respond( $sc_fm );
597 __PACKAGE__->register_method(
598 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
601 method => 'fleshed_actor_stat_cat',
604 __PACKAGE__->register_method(
605 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
609 method => 'fleshed_actor_stat_cat',
612 #XXX Fix stored proc calls
613 sub ranged_actor_stat_cat_all {
618 return undef unless ($ou);
619 my $s_table = actor::stat_cat->table;
621 my $select = <<" SQL";
624 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
629 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
631 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
634 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
635 my $sc_fm = $sc->to_fieldmapper;
637 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
639 $client->respond( $sc_fm );
644 __PACKAGE__->register_method(
645 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
649 method => 'ranged_actor_stat_cat_all',
652 __PACKAGE__->register_method(
653 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
657 method => 'ranged_actor_stat_cat_all',
660 #XXX Fix stored proc calls
661 sub ranged_actor_stat_cat_entry {
667 return undef unless ($ou);
668 my $s_table = actor::stat_cat_entry->table;
670 my $select = <<" SQL";
673 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
678 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
679 $sth->execute($ou,$sc);
681 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
682 $client->respond( $sce->to_fieldmapper );
687 __PACKAGE__->register_method(
688 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
691 method => 'ranged_actor_stat_cat_entry',