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 org_closed_overlap {
20 my $direction = shift || 0;
22 return undef unless ($date && $ou);
24 my $t = actor::org_unit::closed_date->table;
28 WHERE ? between close_start and close_end
30 ORDER BY close_start ASC, close_end DESC
33 my $sth = actor::org_unit::closed_date->db_Main->prepare( $sql );
34 $sth->execute($date, $ou);
37 while (my $closure = $sth->fetchrow_hashref) {
38 $begin ||= $closure->{close_start};
39 $end = $closure->{close_end};
41 my $before = $_dt_parser->parse_datetime( $begin );
42 $before->subtract( days => 1 );
43 my $after = $_dt_parser->parse_datetime( $end );
44 $after->add( days => 1 );
46 if ( $direction <= 0 ) {
47 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->ymd, -1 ) ) {
48 $before = $_dt_parser->parse_datetime( $_b->{start} );
52 if ( $direction >= 0 ) {
53 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->ymd, 1 ) ) {
54 $after = $_dt_parser->parse_datetime( $_a->{end} );
58 $begin = $before->ymd;
63 return { start => $begin, end => $end };
68 __PACKAGE__->register_method(
69 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
71 method => 'org_closed_overlap',
79 return undef unless @barcodes;
81 for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
84 return $card->usr->to_fieldmapper;
86 $client->respond( $card->usr->to_fieldmapper);
90 __PACKAGE__->register_method(
91 api_name => 'open-ils.storage.direct.actor.user.search.barcode',
93 method => 'user_by_barcode',
102 my $c = actor::card->table;
103 my $p = actor::user->table;
105 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
107 my $list = actor::user->db_Main->selectcol_arrayref($sql);
108 for my $bc ( @$list ) {
109 $client->respond($bc);
113 __PACKAGE__->register_method(
114 api_name => 'open-ils.storage.actor.user.lost_barcodes',
117 method => 'lost_barcodes',
118 signature => <<' NOTE',
119 Returns an array of barcodes that belong to lost cards.
120 @return array of barcodes
124 sub expired_barcodes {
128 my $c = actor::card->table;
129 my $p = actor::user->table;
131 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
133 my $list = actor::user->db_Main->selectcol_arrayref($sql);
134 for my $bc ( @$list ) {
135 $client->respond($bc);
139 __PACKAGE__->register_method(
140 api_name => 'open-ils.storage.actor.user.expired_barcodes',
143 method => 'expired_barcodes',
144 signature => <<' NOTE',
145 Returns an array of barcodes that are currently expired.
146 @return array of barcodes
150 sub barred_barcodes {
154 my $c = actor::card->table;
155 my $p = actor::user->table;
157 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
159 my $list = actor::user->db_Main->selectcol_arrayref($sql);
160 for my $bc ( @$list ) {
161 $client->respond($bc);
165 __PACKAGE__->register_method(
166 api_name => 'open-ils.storage.actor.user.barred_barcodes',
169 method => 'barred_barcodes',
170 signature => <<' NOTE',
171 Returns an array of barcodes that are currently barred.
172 @return array of barcodes
176 sub penalized_barcodes {
181 my $c = actor::card->table;
182 my $p = actor::user_standing_penalty->table;
184 my $sql = "SELECT c.barcode FROM $c c JOIN $p p USING (usr)";
187 $sql .= ' WHERE penalty_type NOT IN ('. join(',', map { '?' } @ignore) . ')';
190 $sql .= ' GROUP BY c.barcode;';
192 my $list = actor::user->db_Main->selectcol_arrayref($sql, {}, @ignore);
193 for my $bc ( @$list ) {
194 $client->respond($bc);
198 __PACKAGE__->register_method(
199 api_name => 'open-ils.storage.actor.user.penalized_barcodes',
202 method => 'penalized_barcodes',
203 signature => <<' NOTE',
204 Returns an array of barcodes that have penalties not listed
205 as a parameter. Supply a list of any penalty types that should
206 not stop a patron from checking out materials.
208 @param ignore_list Penalty type to ignore
209 @return array of barcodes
218 my $limit = shift || 1000;
220 $sort = ['family_name','first_given_name'] unless ($$sort[0]);
224 # group 2 = phone, ident
226 my $usr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
227 my @usrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
229 my $addr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
230 my @addrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
232 my $pv = $$search{phone}{value};
233 my $iv = $$search{ident}{value};
234 my $nv = $$search{name}{value};
240 for my $p ( qw/day_phone evening_phone other_phone/ ) {
241 push @ps, "LOWER($p) ~ ?";
242 push @phonev, "^$pv";
244 $phone = '(' . join(' OR ', @ps) . ')';
251 for my $i ( qw/ident_value ident_value2/ ) {
252 push @is, "LOWER($i) ~ ?";
253 push @identv, "^$iv";
255 $ident = '(' . join(' OR ', @is) . ')';
262 for my $n ( qw/first_given_name second_given_name family_name/ ) {
263 push @ns, "LOWER($i) ~ ?";
266 $name = '(' . join(' OR ', @ns) . ')';
269 my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
270 my $addr_where = $addr;
273 my $u_table = actor::user->table;
274 my $a_table = actor::user_address->table;
276 my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
277 my $a_select = "SELECT usr as id FROM $a_table a WHERE $addr_where";
282 $select = "$u_select INTERSECT $a_select";
286 } elsif ($addr_where) {
292 my $order_by = join ', ', map { 'users.'. $_} @$sort;
294 $select = "SELECT users.id FROM $u_table AS users JOIN ($select) AS search USING (id) ORDER BY $order_by LIMIT $limit";
296 return actor::user->db_Main->selectcol_arrayref($select, {}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv));
298 __PACKAGE__->register_method(
299 api_name => 'open-ils.storage.actor.user.crazy_search',
301 method => 'patron_search',
304 =comment not gonna use it...
309 my $searches = shift;
311 return undef unless (defined $searches);
313 for my $usr ( actor::user->search( $searches ) ) {
315 $client->respond( flesh_user( $usr ) );
319 __PACKAGE__->register_method(
320 api_name => 'open-ils.storage.fleshed.actor.user.search',
322 method => 'fleshed_search',
327 sub fleshed_search_like {
330 my $searches = shift;
332 return undef unless (defined $searches);
334 for my $usr ( actor::user->search_like( $searches ) ) {
336 $client->respond( flesh_user( $usr ) );
340 __PACKAGE__->register_method(
341 api_name => 'open-ils.storage.fleshed.actor.user.search_like',
343 method => 'user_by_barcode',
348 sub retrieve_fleshed_user {
353 return undef unless @ids;
355 @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
357 $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
361 __PACKAGE__->register_method(
362 api_name => 'open-ils.storage.fleshed.actor.user.retrieve',
364 method => 'retrieve_fleshed_user',
367 __PACKAGE__->register_method(
368 api_name => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
370 method => 'retrieve_fleshed_user',
379 my $standing = $usr->standing;
380 my $profile = $usr->profile;
381 my $ident_type = $usr->ident_type;
383 my $maddress = $usr->mailing_address;
384 my $baddress = $usr->billing_address;
385 my $card = $usr->card;
387 my @addresses = $usr->addresses;
388 my @cards = $usr->cards;
390 my $usr_fm = $usr->to_fieldmapper;
391 $usr_fm->standing( $standing->to_fieldmapper );
392 $usr_fm->profile( $profile->to_fieldmapper );
393 $usr_fm->ident_type( $ident_type->to_fieldmapper );
395 $usr_fm->card( $card->to_fieldmapper );
396 $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
397 $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
399 $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
400 $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
411 my $select =<<" SQL";
414 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
417 my $sth = actor::org_unit->db_Main->prepare_cached($select);
420 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
424 __PACKAGE__->register_method(
425 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
428 method => 'org_unit_list',
431 sub org_unit_type_list {
435 my $select =<<" SQL";
437 FROM actor.org_unit_type
438 ORDER BY depth, name;
441 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
444 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
448 __PACKAGE__->register_method(
449 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
452 method => 'org_unit_type_list',
455 sub org_unit_full_path {
460 return undef unless (@binds);
462 my $func = 'actor.org_unit_full_path(?)';
463 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
465 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
466 $sth->execute(@binds);
468 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
472 __PACKAGE__->register_method(
473 api_name => 'open-ils.storage.actor.org_unit.full_path',
476 method => 'org_unit_full_path',
479 sub org_unit_ancestors {
484 return undef unless ($id);
486 my $func = 'actor.org_unit_ancestors(?)';
488 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
491 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
492 ORDER BY t.depth, f.name;
494 $sth->execute(''.$id);
496 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
500 __PACKAGE__->register_method(
501 api_name => 'open-ils.storage.actor.org_unit.ancestors',
504 method => 'org_unit_ancestors',
507 sub org_unit_descendants {
513 return undef unless ($id);
515 my $func = 'actor.org_unit_descendants(?)';
516 if (defined $depth) {
517 $func = 'actor.org_unit_descendants(?,?)';
520 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
521 $sth->execute(''.$id, ''.$depth) if (defined $depth);
522 $sth->execute(''.$id) unless (defined $depth);
524 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
528 __PACKAGE__->register_method(
529 api_name => 'open-ils.storage.actor.org_unit.descendants',
532 method => 'org_unit_descendants',
539 for my $rec ( actor::profile->retrieve_all ) {
540 $client->respond( $rec->to_fieldmapper );
545 __PACKAGE__->register_method(
546 method => 'profile_all',
547 api_name => 'open-ils.storage.direct.actor.profile.retrieve.all',
552 sub fleshed_actor_stat_cat {
557 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
560 my $cat = actor::stat_cat->retrieve($sc);
563 my $sc_fm = $cat->to_fieldmapper;
564 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
566 $client->respond( $sc_fm );
572 __PACKAGE__->register_method(
573 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
576 method => 'fleshed_actor_stat_cat',
579 __PACKAGE__->register_method(
580 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
584 method => 'fleshed_actor_stat_cat',
587 #XXX Fix stored proc calls
588 sub ranged_actor_stat_cat_all {
593 return undef unless ($ou);
594 my $s_table = actor::stat_cat->table;
596 my $select = <<" SQL";
599 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
604 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
606 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
609 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
610 my $sc_fm = $sc->to_fieldmapper;
612 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
614 $client->respond( $sc_fm );
619 __PACKAGE__->register_method(
620 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
624 method => 'ranged_actor_stat_cat_all',
627 __PACKAGE__->register_method(
628 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
632 method => 'ranged_actor_stat_cat_all',
635 #XXX Fix stored proc calls
636 sub ranged_actor_stat_cat_entry {
642 return undef unless ($ou);
643 my $s_table = actor::stat_cat_entry->table;
645 my $select = <<" SQL";
648 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
653 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
654 $sth->execute($ou,$sc);
656 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
657 $client->respond( $sce->to_fieldmapper );
662 __PACKAGE__->register_method(
663 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
666 method => 'ranged_actor_stat_cat_entry',