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 = "JOIN (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) ".
322 "WHERE $addr_where) AS clone USING (id)" if ($addr_where);
327 $select = "$u_select INTERSECT $a_select";
331 } elsif ($addr_where) {
332 $select = "$a_select";
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
351 WHERE users.deleted = FALSE $inactive
356 return actor::user->db_Main->selectcol_arrayref($select, {}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv,@addrv));
358 __PACKAGE__->register_method(
359 api_name => 'open-ils.storage.actor.user.crazy_search',
361 method => 'patron_search',
364 =comment not gonna use it...
369 my $searches = shift;
371 return undef unless (defined $searches);
373 for my $usr ( actor::user->search( $searches ) ) {
375 $client->respond( flesh_user( $usr ) );
379 __PACKAGE__->register_method(
380 api_name => 'open-ils.storage.fleshed.actor.user.search',
382 method => 'fleshed_search',
387 sub fleshed_search_like {
390 my $searches = shift;
392 return undef unless (defined $searches);
394 for my $usr ( actor::user->search_like( $searches ) ) {
396 $client->respond( flesh_user( $usr ) );
400 __PACKAGE__->register_method(
401 api_name => 'open-ils.storage.fleshed.actor.user.search_like',
403 method => 'user_by_barcode',
408 sub retrieve_fleshed_user {
413 return undef unless @ids;
415 @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
417 $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
421 __PACKAGE__->register_method(
422 api_name => 'open-ils.storage.fleshed.actor.user.retrieve',
424 method => 'retrieve_fleshed_user',
427 __PACKAGE__->register_method(
428 api_name => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
430 method => 'retrieve_fleshed_user',
439 my $standing = $usr->standing;
440 my $profile = $usr->profile;
441 my $ident_type = $usr->ident_type;
443 my $maddress = $usr->mailing_address;
444 my $baddress = $usr->billing_address;
445 my $card = $usr->card;
447 my @addresses = $usr->addresses;
448 my @cards = $usr->cards;
450 my $usr_fm = $usr->to_fieldmapper;
451 $usr_fm->standing( $standing->to_fieldmapper );
452 $usr_fm->profile( $profile->to_fieldmapper );
453 $usr_fm->ident_type( $ident_type->to_fieldmapper );
455 $usr_fm->card( $card->to_fieldmapper );
456 $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
457 $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
459 $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
460 $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
471 my $select =<<" SQL";
474 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
477 my $sth = actor::org_unit->db_Main->prepare_cached($select);
480 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
484 __PACKAGE__->register_method(
485 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
488 method => 'org_unit_list',
491 sub org_unit_type_list {
495 my $select =<<" SQL";
497 FROM actor.org_unit_type
498 ORDER BY depth, name;
501 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
504 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
508 __PACKAGE__->register_method(
509 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
512 method => 'org_unit_type_list',
515 sub org_unit_full_path {
520 return undef unless (@binds);
522 my $func = 'actor.org_unit_full_path(?)';
523 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
525 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
526 $sth->execute(@binds);
528 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
532 __PACKAGE__->register_method(
533 api_name => 'open-ils.storage.actor.org_unit.full_path',
536 method => 'org_unit_full_path',
539 sub org_unit_ancestors {
544 return undef unless ($id);
546 my $func = 'actor.org_unit_ancestors(?)';
548 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
551 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
552 ORDER BY t.depth, f.name;
554 $sth->execute(''.$id);
556 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
560 __PACKAGE__->register_method(
561 api_name => 'open-ils.storage.actor.org_unit.ancestors',
564 method => 'org_unit_ancestors',
567 sub org_unit_descendants {
573 return undef unless ($id);
575 my $func = 'actor.org_unit_descendants(?)';
576 if (defined $depth) {
577 $func = 'actor.org_unit_descendants(?,?)';
580 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
581 $sth->execute(''.$id, ''.$depth) if (defined $depth);
582 $sth->execute(''.$id) unless (defined $depth);
584 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
588 __PACKAGE__->register_method(
589 api_name => 'open-ils.storage.actor.org_unit.descendants',
592 method => 'org_unit_descendants',
595 sub fleshed_actor_stat_cat {
600 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
603 my $cat = actor::stat_cat->retrieve($sc);
606 my $sc_fm = $cat->to_fieldmapper;
607 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
609 $client->respond( $sc_fm );
615 __PACKAGE__->register_method(
616 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
619 method => 'fleshed_actor_stat_cat',
622 __PACKAGE__->register_method(
623 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
627 method => 'fleshed_actor_stat_cat',
630 #XXX Fix stored proc calls
631 sub ranged_actor_stat_cat_all {
636 return undef unless ($ou);
637 my $s_table = actor::stat_cat->table;
639 my $select = <<" SQL";
642 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
647 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
649 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
652 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
653 my $sc_fm = $sc->to_fieldmapper;
655 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
657 $client->respond( $sc_fm );
662 __PACKAGE__->register_method(
663 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
667 method => 'ranged_actor_stat_cat_all',
670 __PACKAGE__->register_method(
671 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
675 method => 'ranged_actor_stat_cat_all',
678 #XXX Fix stored proc calls
679 sub ranged_actor_stat_cat_entry {
685 return undef unless ($ou);
686 my $s_table = actor::stat_cat_entry->table;
688 my $select = <<" SQL";
691 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
696 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
697 $sth->execute($ou,$sc);
699 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
700 $client->respond( $sce->to_fieldmapper );
705 __PACKAGE__->register_method(
706 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
709 method => 'ranged_actor_stat_cat_entry',