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 = '';
321 $clone_select = "JOIN (SELECT cu.id as id FROM $a_table ca ".
322 "JOIN $u_table cu ON (cu.mailing_address = ca.id OR cu.billing_address = ca.id) ".
323 "WHERE $addr_where) AS clone USING (id)" if ($addr_where);
328 $select = "$u_select INTERSECT $a_select";
332 } elsif ($addr_where) {
333 $select = "$a_select";
338 my $order_by = join ', ', map { 'users.'. $_} @$sort;
343 $inactive = 'AND users.active = TRUE';
348 FROM $u_table AS users
349 JOIN ($select) AS search
352 WHERE users.deleted = FALSE $inactive
357 return actor::user->db_Main->selectcol_arrayref($select, {}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv,@addrv));
359 __PACKAGE__->register_method(
360 api_name => 'open-ils.storage.actor.user.crazy_search',
362 method => 'patron_search',
365 =comment not gonna use it...
370 my $searches = shift;
372 return undef unless (defined $searches);
374 for my $usr ( actor::user->search( $searches ) ) {
376 $client->respond( flesh_user( $usr ) );
380 __PACKAGE__->register_method(
381 api_name => 'open-ils.storage.fleshed.actor.user.search',
383 method => 'fleshed_search',
388 sub fleshed_search_like {
391 my $searches = shift;
393 return undef unless (defined $searches);
395 for my $usr ( actor::user->search_like( $searches ) ) {
397 $client->respond( flesh_user( $usr ) );
401 __PACKAGE__->register_method(
402 api_name => 'open-ils.storage.fleshed.actor.user.search_like',
404 method => 'user_by_barcode',
409 sub retrieve_fleshed_user {
414 return undef unless @ids;
416 @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
418 $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
422 __PACKAGE__->register_method(
423 api_name => 'open-ils.storage.fleshed.actor.user.retrieve',
425 method => 'retrieve_fleshed_user',
428 __PACKAGE__->register_method(
429 api_name => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
431 method => 'retrieve_fleshed_user',
440 my $standing = $usr->standing;
441 my $profile = $usr->profile;
442 my $ident_type = $usr->ident_type;
444 my $maddress = $usr->mailing_address;
445 my $baddress = $usr->billing_address;
446 my $card = $usr->card;
448 my @addresses = $usr->addresses;
449 my @cards = $usr->cards;
451 my $usr_fm = $usr->to_fieldmapper;
452 $usr_fm->standing( $standing->to_fieldmapper );
453 $usr_fm->profile( $profile->to_fieldmapper );
454 $usr_fm->ident_type( $ident_type->to_fieldmapper );
456 $usr_fm->card( $card->to_fieldmapper );
457 $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
458 $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
460 $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
461 $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
472 my $select =<<" SQL";
475 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
478 my $sth = actor::org_unit->db_Main->prepare_cached($select);
481 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
485 __PACKAGE__->register_method(
486 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
489 method => 'org_unit_list',
492 sub org_unit_type_list {
496 my $select =<<" SQL";
498 FROM actor.org_unit_type
499 ORDER BY depth, name;
502 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
505 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
509 __PACKAGE__->register_method(
510 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
513 method => 'org_unit_type_list',
516 sub org_unit_full_path {
521 return undef unless (@binds);
523 my $func = 'actor.org_unit_full_path(?)';
524 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
526 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
527 $sth->execute(@binds);
529 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
533 __PACKAGE__->register_method(
534 api_name => 'open-ils.storage.actor.org_unit.full_path',
537 method => 'org_unit_full_path',
540 sub org_unit_ancestors {
545 return undef unless ($id);
547 my $func = 'actor.org_unit_ancestors(?)';
549 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
552 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
553 ORDER BY t.depth, f.name;
555 $sth->execute(''.$id);
557 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
561 __PACKAGE__->register_method(
562 api_name => 'open-ils.storage.actor.org_unit.ancestors',
565 method => 'org_unit_ancestors',
568 sub org_unit_descendants {
574 return undef unless ($id);
576 my $func = 'actor.org_unit_descendants(?)';
577 if (defined $depth) {
578 $func = 'actor.org_unit_descendants(?,?)';
581 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
582 $sth->execute(''.$id, ''.$depth) if (defined $depth);
583 $sth->execute(''.$id) unless (defined $depth);
585 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
589 __PACKAGE__->register_method(
590 api_name => 'open-ils.storage.actor.org_unit.descendants',
593 method => 'org_unit_descendants',
596 sub fleshed_actor_stat_cat {
601 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
604 my $cat = actor::stat_cat->retrieve($sc);
607 my $sc_fm = $cat->to_fieldmapper;
608 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
610 $client->respond( $sc_fm );
616 __PACKAGE__->register_method(
617 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
620 method => 'fleshed_actor_stat_cat',
623 __PACKAGE__->register_method(
624 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
628 method => 'fleshed_actor_stat_cat',
631 #XXX Fix stored proc calls
632 sub ranged_actor_stat_cat_all {
637 return undef unless ($ou);
638 my $s_table = actor::stat_cat->table;
640 my $select = <<" SQL";
643 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
648 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
650 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
653 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
654 my $sc_fm = $sc->to_fieldmapper;
656 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
658 $client->respond( $sc_fm );
663 __PACKAGE__->register_method(
664 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
668 method => 'ranged_actor_stat_cat_all',
671 __PACKAGE__->register_method(
672 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
676 method => 'ranged_actor_stat_cat_all',
679 #XXX Fix stored proc calls
680 sub ranged_actor_stat_cat_entry {
686 return undef unless ($ou);
687 my $s_table = actor::stat_cat_entry->table;
689 my $select = <<" SQL";
692 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
697 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
698 $sth->execute($ou,$sc);
700 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
701 $client->respond( $sce->to_fieldmapper );
706 __PACKAGE__->register_method(
707 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
710 method => 'ranged_actor_stat_cat_entry',