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 OpenILS::Utils::Fieldmapper;
8 use DateTime::Format::ISO8601;
11 my $_dt_parser = DateTime::Format::ISO8601->new;
13 my $log = 'OpenSRF::Utils::Logger';
15 sub new_usergroup_id {
16 return actor::user->db_Main->selectrow_array("select nextval('actor.usr_usrgroup_seq'::regclass)");
18 __PACKAGE__->register_method(
19 api_name => 'open-ils.storage.actor.user.group_id.new',
21 method => 'new_usergroup_id',
24 sub org_closed_overlap {
29 my $direction = shift || 0;
31 return undef unless ($date && $ou);
33 my $t = actor::org_unit::closed_date->table;
37 WHERE ? between close_start and close_end
39 ORDER BY close_start ASC, close_end DESC
42 my $sth = actor::org_unit::closed_date->db_Main->prepare( $sql );
43 $sth->execute($date, $ou);
46 while (my $closure = $sth->fetchrow_hashref) {
47 $begin ||= $closure->{close_start};
48 $end = $closure->{close_end};
50 my $before = $_dt_parser->parse_datetime( $begin );
51 $before->subtract( days => 1 );
52 my $after = $_dt_parser->parse_datetime( $end );
53 $after->add( days => 1 );
55 if ( $direction <= 0 ) {
56 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->ymd, -1 ) ) {
57 $before = $_dt_parser->parse_datetime( $_b->{start} );
61 if ( $direction >= 0 ) {
62 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->ymd, 1 ) ) {
63 $after = $_dt_parser->parse_datetime( $_a->{end} );
67 $begin = $before->ymd;
72 return { start => $begin, end => $end };
77 __PACKAGE__->register_method(
78 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
80 method => 'org_closed_overlap',
88 return undef unless @barcodes;
90 for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
93 return $card->usr->to_fieldmapper;
95 $client->respond( $card->usr->to_fieldmapper);
99 __PACKAGE__->register_method(
100 api_name => 'open-ils.storage.direct.actor.user.search.barcode',
102 method => 'user_by_barcode',
111 my $c = actor::card->table;
112 my $p = actor::user->table;
114 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
116 my $list = actor::user->db_Main->selectcol_arrayref($sql);
117 for my $bc ( @$list ) {
118 $client->respond($bc);
122 __PACKAGE__->register_method(
123 api_name => 'open-ils.storage.actor.user.lost_barcodes',
126 method => 'lost_barcodes',
127 signature => <<' NOTE',
128 Returns an array of barcodes that belong to lost cards.
129 @return array of barcodes
133 sub expired_barcodes {
137 my $c = actor::card->table;
138 my $p = actor::user->table;
140 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
142 my $list = actor::user->db_Main->selectcol_arrayref($sql);
143 for my $bc ( @$list ) {
144 $client->respond($bc);
148 __PACKAGE__->register_method(
149 api_name => 'open-ils.storage.actor.user.expired_barcodes',
152 method => 'expired_barcodes',
153 signature => <<' NOTE',
154 Returns an array of barcodes that are currently expired.
155 @return array of barcodes
159 sub barred_barcodes {
163 my $c = actor::card->table;
164 my $p = actor::user->table;
166 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
168 my $list = actor::user->db_Main->selectcol_arrayref($sql);
169 for my $bc ( @$list ) {
170 $client->respond($bc);
174 __PACKAGE__->register_method(
175 api_name => 'open-ils.storage.actor.user.barred_barcodes',
178 method => 'barred_barcodes',
179 signature => <<' NOTE',
180 Returns an array of barcodes that are currently barred.
181 @return array of barcodes
185 sub penalized_barcodes {
190 my $c = actor::card->table;
191 my $p = actor::user_standing_penalty->table;
193 my $sql = "SELECT c.barcode FROM $c c JOIN $p p USING (usr)";
196 $sql .= ' WHERE penalty_type NOT IN ('. join(',', map { '?' } @ignore) . ')';
199 $sql .= ' GROUP BY c.barcode;';
201 my $list = actor::user->db_Main->selectcol_arrayref($sql, {}, @ignore);
202 for my $bc ( @$list ) {
203 $client->respond($bc);
207 __PACKAGE__->register_method(
208 api_name => 'open-ils.storage.actor.user.penalized_barcodes',
211 method => 'penalized_barcodes',
212 signature => <<' NOTE',
213 Returns an array of barcodes that have penalties not listed
214 as a parameter. Supply a list of any penalty types that should
215 not stop a patron from checking out materials.
217 @param ignore_list Penalty type to ignore
218 @return array of barcodes
227 my $limit = shift || 1000;
229 $sort = ['family_name','first_given_name'] unless ($$sort[0]);
233 # group 2 = phone, ident
235 my $usr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
236 my @usrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
238 my $addr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
239 my @addrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
241 my $pv = $$search{phone}{value};
242 my $iv = $$search{ident}{value};
243 my $nv = $$search{name}{value};
249 for my $p ( qw/day_phone evening_phone other_phone/ ) {
250 push @ps, "LOWER($p) ~ ?";
251 push @phonev, "^$pv";
253 $phone = '(' . join(' OR ', @ps) . ')';
260 for my $i ( qw/ident_value ident_value2/ ) {
261 push @is, "LOWER($i) ~ ?";
262 push @identv, "^$iv";
264 $ident = '(' . join(' OR ', @is) . ')';
271 for my $n ( qw/first_given_name second_given_name family_name/ ) {
272 push @ns, "LOWER($i) ~ ?";
275 $name = '(' . join(' OR ', @ns) . ')';
278 my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
279 my $addr_where = $addr;
282 my $u_table = actor::user->table;
283 my $a_table = actor::user_address->table;
285 my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
286 my $a_select = "SELECT usr as id FROM $a_table a WHERE $addr_where";
291 $select = "$u_select INTERSECT $a_select";
295 } elsif ($addr_where) {
301 my $order_by = join ', ', map { 'users.'. $_} @$sort;
303 $select = "SELECT users.id FROM $u_table AS users JOIN ($select) AS search USING (id) ORDER BY $order_by LIMIT $limit";
305 return actor::user->db_Main->selectcol_arrayref($select, {}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv));
307 __PACKAGE__->register_method(
308 api_name => 'open-ils.storage.actor.user.crazy_search',
310 method => 'patron_search',
313 =comment not gonna use it...
318 my $searches = shift;
320 return undef unless (defined $searches);
322 for my $usr ( actor::user->search( $searches ) ) {
324 $client->respond( flesh_user( $usr ) );
328 __PACKAGE__->register_method(
329 api_name => 'open-ils.storage.fleshed.actor.user.search',
331 method => 'fleshed_search',
336 sub fleshed_search_like {
339 my $searches = shift;
341 return undef unless (defined $searches);
343 for my $usr ( actor::user->search_like( $searches ) ) {
345 $client->respond( flesh_user( $usr ) );
349 __PACKAGE__->register_method(
350 api_name => 'open-ils.storage.fleshed.actor.user.search_like',
352 method => 'user_by_barcode',
357 sub retrieve_fleshed_user {
362 return undef unless @ids;
364 @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
366 $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
370 __PACKAGE__->register_method(
371 api_name => 'open-ils.storage.fleshed.actor.user.retrieve',
373 method => 'retrieve_fleshed_user',
376 __PACKAGE__->register_method(
377 api_name => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
379 method => 'retrieve_fleshed_user',
388 my $standing = $usr->standing;
389 my $profile = $usr->profile;
390 my $ident_type = $usr->ident_type;
392 my $maddress = $usr->mailing_address;
393 my $baddress = $usr->billing_address;
394 my $card = $usr->card;
396 my @addresses = $usr->addresses;
397 my @cards = $usr->cards;
399 my $usr_fm = $usr->to_fieldmapper;
400 $usr_fm->standing( $standing->to_fieldmapper );
401 $usr_fm->profile( $profile->to_fieldmapper );
402 $usr_fm->ident_type( $ident_type->to_fieldmapper );
404 $usr_fm->card( $card->to_fieldmapper );
405 $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
406 $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
408 $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
409 $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
420 my $select =<<" SQL";
423 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
426 my $sth = actor::org_unit->db_Main->prepare_cached($select);
429 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
433 __PACKAGE__->register_method(
434 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
437 method => 'org_unit_list',
440 sub org_unit_type_list {
444 my $select =<<" SQL";
446 FROM actor.org_unit_type
447 ORDER BY depth, name;
450 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
453 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
457 __PACKAGE__->register_method(
458 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
461 method => 'org_unit_type_list',
464 sub org_unit_full_path {
469 return undef unless (@binds);
471 my $func = 'actor.org_unit_full_path(?)';
472 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
474 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
475 $sth->execute(@binds);
477 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
481 __PACKAGE__->register_method(
482 api_name => 'open-ils.storage.actor.org_unit.full_path',
485 method => 'org_unit_full_path',
488 sub org_unit_ancestors {
493 return undef unless ($id);
495 my $func = 'actor.org_unit_ancestors(?)';
497 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
500 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
501 ORDER BY t.depth, f.name;
503 $sth->execute(''.$id);
505 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
509 __PACKAGE__->register_method(
510 api_name => 'open-ils.storage.actor.org_unit.ancestors',
513 method => 'org_unit_ancestors',
516 sub org_unit_descendants {
522 return undef unless ($id);
524 my $func = 'actor.org_unit_descendants(?)';
525 if (defined $depth) {
526 $func = 'actor.org_unit_descendants(?,?)';
529 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
530 $sth->execute(''.$id, ''.$depth) if (defined $depth);
531 $sth->execute(''.$id) unless (defined $depth);
533 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
537 __PACKAGE__->register_method(
538 api_name => 'open-ils.storage.actor.org_unit.descendants',
541 method => 'org_unit_descendants',
548 for my $rec ( actor::profile->retrieve_all ) {
549 $client->respond( $rec->to_fieldmapper );
554 __PACKAGE__->register_method(
555 method => 'profile_all',
556 api_name => 'open-ils.storage.direct.actor.profile.retrieve.all',
561 sub fleshed_actor_stat_cat {
566 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
569 my $cat = actor::stat_cat->retrieve($sc);
572 my $sc_fm = $cat->to_fieldmapper;
573 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
575 $client->respond( $sc_fm );
581 __PACKAGE__->register_method(
582 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
585 method => 'fleshed_actor_stat_cat',
588 __PACKAGE__->register_method(
589 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
593 method => 'fleshed_actor_stat_cat',
596 #XXX Fix stored proc calls
597 sub ranged_actor_stat_cat_all {
602 return undef unless ($ou);
603 my $s_table = actor::stat_cat->table;
605 my $select = <<" SQL";
608 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
613 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
615 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
618 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
619 my $sc_fm = $sc->to_fieldmapper;
621 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
623 $client->respond( $sc_fm );
628 __PACKAGE__->register_method(
629 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
633 method => 'ranged_actor_stat_cat_all',
636 __PACKAGE__->register_method(
637 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
641 method => 'ranged_actor_stat_cat_all',
644 #XXX Fix stored proc calls
645 sub ranged_actor_stat_cat_entry {
651 return undef unless ($ou);
652 my $s_table = actor::stat_cat_entry->table;
654 my $select = <<" SQL";
657 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
662 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
663 $sth->execute($ou,$sc);
665 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
666 $client->respond( $sce->to_fieldmapper );
671 __PACKAGE__->register_method(
672 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
675 method => 'ranged_actor_stat_cat_entry',