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',
29 local $OpenILS::Application::Storage::WRITE = 1;
31 my $delete_sql = <<" SQL";
32 DELETE FROM actor.org_unit_proximity;
35 my $insert_sql = <<" SQL";
36 INSERT INTO actor.org_unit_proximity (from_org, to_org, prox)
39 actor.org_unit_proximity(l.id,r.id)
40 FROM actor.org_unit l,
44 actor::org_unit_proximity->db_Main->do($delete_sql);
45 actor::org_unit_proximity->db_Main->do($insert_sql);
49 __PACKAGE__->register_method(
50 api_name => 'open-ils.storage.actor.org_unit.refresh_proximity',
52 method => 'calc_proximity',
56 sub org_closed_overlap {
61 my $direction = shift || 0;
63 return undef unless ($date && $ou);
65 my $t = actor::org_unit::closed_date->table;
69 WHERE ? between close_start and close_end
71 ORDER BY close_start ASC, close_end DESC
74 my $sth = actor::org_unit::closed_date->db_Main->prepare( $sql );
75 $sth->execute($date, $ou);
78 while (my $closure = $sth->fetchrow_hashref) {
79 $begin ||= $closure->{close_start};
80 $end = $closure->{close_end};
82 my $before = $_dt_parser->parse_datetime( clense_ISO8601($begin) );
83 $before->subtract( seconds => 1 );
84 my $after = $_dt_parser->parse_datetime( clense_ISO8601($end) );
85 $after->add( seconds => 1 );
87 if ( $direction <= 0 ) {
88 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->iso8601, -1 ) ) {
89 $before = $_dt_parser->parse_datetime( clense_ISO8601($_b->{start}) );
93 if ( $direction >= 0 ) {
94 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->iso8601, 1 ) ) {
95 $after = $_dt_parser->parse_datetime( clense_ISO8601($_a->{end}) );
99 $begin = clense_ISO8601($before->iso8601);
100 $end = clense_ISO8601($after->iso8601);
103 if ($begin && $end) {
104 return { start => $begin, end => $end };
109 __PACKAGE__->register_method(
110 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
112 method => 'org_closed_overlap',
115 sub user_by_barcode {
118 my @barcodes = shift;
120 return undef unless @barcodes;
122 for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
124 if (@barcodes == 1) {
125 return $card->usr->to_fieldmapper;
127 $client->respond( $card->usr->to_fieldmapper);
131 __PACKAGE__->register_method(
132 api_name => 'open-ils.storage.direct.actor.user.search.barcode',
134 method => 'user_by_barcode',
143 my $c = actor::card->table;
144 my $p = actor::user->table;
146 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
148 my $list = actor::user->db_Main->selectcol_arrayref($sql);
149 for my $bc ( @$list ) {
150 $client->respond($bc);
154 __PACKAGE__->register_method(
155 api_name => 'open-ils.storage.actor.user.lost_barcodes',
158 method => 'lost_barcodes',
159 signature => <<' NOTE',
160 Returns an array of barcodes that belong to lost cards.
161 @return array of barcodes
165 sub expired_barcodes {
169 my $c = actor::card->table;
170 my $p = actor::user->table;
172 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
174 my $list = actor::user->db_Main->selectcol_arrayref($sql);
175 for my $bc ( @$list ) {
176 $client->respond($bc);
180 __PACKAGE__->register_method(
181 api_name => 'open-ils.storage.actor.user.expired_barcodes',
184 method => 'expired_barcodes',
185 signature => <<' NOTE',
186 Returns an array of barcodes that are currently expired.
187 @return array of barcodes
191 sub barred_barcodes {
195 my $c = actor::card->table;
196 my $p = actor::user->table;
198 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
200 my $list = actor::user->db_Main->selectcol_arrayref($sql);
201 for my $bc ( @$list ) {
202 $client->respond($bc);
206 __PACKAGE__->register_method(
207 api_name => 'open-ils.storage.actor.user.barred_barcodes',
210 method => 'barred_barcodes',
211 signature => <<' NOTE',
212 Returns an array of barcodes that are currently barred.
213 @return array of barcodes
217 sub penalized_barcodes {
222 my $c = actor::card->table;
223 my $p = actor::user_standing_penalty->table;
225 my $sql = "SELECT c.barcode FROM $c c JOIN $p p USING (usr)";
228 $sql .= ' WHERE penalty_type NOT IN ('. join(',', map { '?' } @ignore) . ')';
231 $sql .= ' GROUP BY c.barcode;';
233 my $list = actor::user->db_Main->selectcol_arrayref($sql, {}, @ignore);
234 for my $bc ( @$list ) {
235 $client->respond($bc);
239 __PACKAGE__->register_method(
240 api_name => 'open-ils.storage.actor.user.penalized_barcodes',
243 method => 'penalized_barcodes',
244 signature => <<' NOTE',
245 Returns an array of barcodes that have penalties not listed
246 as a parameter. Supply a list of any penalty types that should
247 not stop a patron from checking out materials.
249 @param ignore_list Penalty type to ignore
250 @return array of barcodes
259 my $limit = shift || 1000;
261 my $inactive = shift;
262 $sort = ['family_name','first_given_name'] unless ($$sort[0]);
266 # group 2 = phone, ident
268 my $usr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
269 my @usrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
271 my $addr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
272 my @addrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
274 my $pv = $$search{phone}{value};
275 my $iv = $$search{ident}{value};
276 my $nv = $$search{name}{value};
282 for my $p ( qw/day_phone evening_phone other_phone/ ) {
283 push @ps, "LOWER($p) ~ ?";
284 push @phonev, "^$pv";
286 $phone = '(' . join(' OR ', @ps) . ')';
293 for my $i ( qw/ident_value ident_value2/ ) {
294 push @is, "LOWER($i) ~ ?";
295 push @identv, "^$iv";
297 $ident = '(' . join(' OR ', @is) . ')';
304 for my $n ( qw/first_given_name second_given_name family_name/ ) {
305 push @ns, "LOWER($i) ~ ?";
308 $name = '(' . join(' OR ', @ns) . ')';
311 my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
312 my $addr_where = $addr;
315 my $u_table = actor::user->table;
316 my $a_table = actor::user_address->table;
318 my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
319 my $a_select = "SELECT usr as id FROM $a_table a WHERE $addr_where";
320 my $clone_select = "SELECT cu.id as id FROM $a_table ca ".
321 "JOIN $u_table cu ON (cu.mailing_address = ca.id OR cu.billing_address = ca.id) ".
327 $select = "$u_select INTERSECT $a_select INTERSECT $clone_select";
331 } elsif ($addr_where) {
337 my $order_by = join ', ', map { 'users.'. $_} @$sort;
342 $inactive = 'AND users.active = TRUE';
347 FROM $u_table AS users
348 JOIN ($select) AS search
350 WHERE users.deleted = FALSE $inactive
355 return actor::user->db_Main->selectcol_arrayref($select, {}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv,@addrv));
357 __PACKAGE__->register_method(
358 api_name => 'open-ils.storage.actor.user.crazy_search',
360 method => 'patron_search',
363 =comment not gonna use it...
368 my $searches = shift;
370 return undef unless (defined $searches);
372 for my $usr ( actor::user->search( $searches ) ) {
374 $client->respond( flesh_user( $usr ) );
378 __PACKAGE__->register_method(
379 api_name => 'open-ils.storage.fleshed.actor.user.search',
381 method => 'fleshed_search',
386 sub fleshed_search_like {
389 my $searches = shift;
391 return undef unless (defined $searches);
393 for my $usr ( actor::user->search_like( $searches ) ) {
395 $client->respond( flesh_user( $usr ) );
399 __PACKAGE__->register_method(
400 api_name => 'open-ils.storage.fleshed.actor.user.search_like',
402 method => 'user_by_barcode',
407 sub retrieve_fleshed_user {
412 return undef unless @ids;
414 @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
416 $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
420 __PACKAGE__->register_method(
421 api_name => 'open-ils.storage.fleshed.actor.user.retrieve',
423 method => 'retrieve_fleshed_user',
426 __PACKAGE__->register_method(
427 api_name => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
429 method => 'retrieve_fleshed_user',
438 my $standing = $usr->standing;
439 my $profile = $usr->profile;
440 my $ident_type = $usr->ident_type;
442 my $maddress = $usr->mailing_address;
443 my $baddress = $usr->billing_address;
444 my $card = $usr->card;
446 my @addresses = $usr->addresses;
447 my @cards = $usr->cards;
449 my $usr_fm = $usr->to_fieldmapper;
450 $usr_fm->standing( $standing->to_fieldmapper );
451 $usr_fm->profile( $profile->to_fieldmapper );
452 $usr_fm->ident_type( $ident_type->to_fieldmapper );
454 $usr_fm->card( $card->to_fieldmapper );
455 $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
456 $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
458 $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
459 $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
470 my $select =<<" SQL";
473 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
476 my $sth = actor::org_unit->db_Main->prepare_cached($select);
479 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
483 __PACKAGE__->register_method(
484 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
487 method => 'org_unit_list',
490 sub org_unit_type_list {
494 my $select =<<" SQL";
496 FROM actor.org_unit_type
497 ORDER BY depth, name;
500 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
503 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
507 __PACKAGE__->register_method(
508 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
511 method => 'org_unit_type_list',
514 sub org_unit_full_path {
519 return undef unless (@binds);
521 my $func = 'actor.org_unit_full_path(?)';
522 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
524 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
525 $sth->execute(@binds);
527 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
531 __PACKAGE__->register_method(
532 api_name => 'open-ils.storage.actor.org_unit.full_path',
535 method => 'org_unit_full_path',
538 sub org_unit_ancestors {
543 return undef unless ($id);
545 my $func = 'actor.org_unit_ancestors(?)';
547 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
550 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
551 ORDER BY t.depth, f.name;
553 $sth->execute(''.$id);
555 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
559 __PACKAGE__->register_method(
560 api_name => 'open-ils.storage.actor.org_unit.ancestors',
563 method => 'org_unit_ancestors',
566 sub org_unit_descendants {
572 return undef unless ($id);
574 my $func = 'actor.org_unit_descendants(?)';
575 if (defined $depth) {
576 $func = 'actor.org_unit_descendants(?,?)';
579 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
580 $sth->execute(''.$id, ''.$depth) if (defined $depth);
581 $sth->execute(''.$id) unless (defined $depth);
583 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
587 __PACKAGE__->register_method(
588 api_name => 'open-ils.storage.actor.org_unit.descendants',
591 method => 'org_unit_descendants',
594 sub fleshed_actor_stat_cat {
599 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
602 my $cat = actor::stat_cat->retrieve($sc);
605 my $sc_fm = $cat->to_fieldmapper;
606 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
608 $client->respond( $sc_fm );
614 __PACKAGE__->register_method(
615 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
618 method => 'fleshed_actor_stat_cat',
621 __PACKAGE__->register_method(
622 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
626 method => 'fleshed_actor_stat_cat',
629 #XXX Fix stored proc calls
630 sub ranged_actor_stat_cat_all {
635 return undef unless ($ou);
636 my $s_table = actor::stat_cat->table;
638 my $select = <<" SQL";
641 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
646 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
648 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
651 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
652 my $sc_fm = $sc->to_fieldmapper;
654 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
656 $client->respond( $sc_fm );
661 __PACKAGE__->register_method(
662 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
666 method => 'ranged_actor_stat_cat_all',
669 __PACKAGE__->register_method(
670 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
674 method => 'ranged_actor_stat_cat_all',
677 #XXX Fix stored proc calls
678 sub ranged_actor_stat_cat_entry {
684 return undef unless ($ou);
685 my $s_table = actor::stat_cat_entry->table;
687 my $select = <<" SQL";
690 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
695 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
696 $sth->execute($ou,$sc);
698 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
699 $client->respond( $sce->to_fieldmapper );
704 __PACKAGE__->register_method(
705 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
708 method => 'ranged_actor_stat_cat_entry',