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;
7 use OpenSRF::Utils::SettingsClient;
10 use DateTime::Format::ISO8601;
13 my $_dt_parser = DateTime::Format::ISO8601->new;
15 my $log = 'OpenSRF::Utils::Logger';
17 sub new_usergroup_id {
18 return actor::user->db_Main->selectrow_array("select nextval('actor.usr_usrgroup_seq'::regclass)");
20 __PACKAGE__->register_method(
21 api_name => 'open-ils.storage.actor.user.group_id.new',
23 method => 'new_usergroup_id',
33 SUM(COALESCE((SELECT SUM(b.amount) FROM money.billing b WHERE b.voided IS FALSE AND b.xact = x.id),0.0)) -
34 SUM(COALESCE((SELECT SUM(p.amount) FROM money.payment p WHERE p.voided IS FALSE AND p.xact = x.id),0.0))
35 FROM money.billable_xact x
36 WHERE x.usr = ? AND x.xact_finish IS NULL
40 my (undef,$val) = actor::user->db_Main->selectrow_array($sql, {}, $usr);
44 __PACKAGE__->register_method(
45 api_name => 'open-ils.storage.actor.user.total_owed',
47 method => 'usr_total_owed',
50 sub usr_breakdown_out {
55 my $out_sql = <<" SQL";
57 FROM action.circulation
58 WHERE usr = ? AND checkin_time IS NULL AND due_date >= 'today' AND (stop_fines IS NULL OR stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE'))
60 AND checkin_time IS NULL
61 AND ( (fine_interval >= '1 day' AND due_date >= 'today')
62 OR (fine_interval < '1 day' AND due_date > 'now' ))
63 AND (stop_fines IS NULL
64 OR stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE'))
67 my $out = actor::user->db_Main->selectcol_arrayref($out_sql, {}, $usr);
69 my $od_sql = <<" SQL";
71 FROM action.circulation
73 AND checkin_time IS NULL
74 AND ( (fine_interval >= '1 day' AND due_date < 'today')
75 OR (fine_interval < '1 day' AND due_date < 'now' ))
76 AND (stop_fines IS NULL
77 OR stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE'))
80 my $od = actor::user->db_Main->selectcol_arrayref($od_sql, {}, $usr);
82 my $lost_sql = <<" SQL";
84 FROM action.circulation
85 WHERE usr = ? AND checkin_time IS NULL AND xact_finish IS NULL AND stop_fines = 'LOST'
88 my $lost = actor::user->db_Main->selectcol_arrayref($lost_sql, {}, $usr);
90 my $cl_sql = <<" SQL";
92 FROM action.circulation
93 WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'CLAIMSRETURNED'
96 my $cl = actor::user->db_Main->selectcol_arrayref($cl_sql, {}, $usr);
98 my $lo_sql = <<" SQL";
100 FROM action.circulation
101 WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'LONGOVERDUE'
104 my $lo = actor::user->db_Main->selectcol_arrayref($lo_sql, {}, $usr);
106 if ($self->api_name =~/count$/o) {
107 return { total => scalar(@$out) + scalar(@$od) + scalar(@$lost) + scalar(@$cl) + scalar(@$lo),
108 out => scalar(@$out),
109 overdue => scalar(@$od),
110 lost => scalar(@$lost),
111 claims_returned => scalar(@$cl),
112 long_overdue => scalar(@$lo),
116 return { out => $out,
119 claims_returned => $cl,
123 __PACKAGE__->register_method(
124 api_name => 'open-ils.storage.actor.user.checked_out',
126 method => 'usr_breakdown_out',
128 __PACKAGE__->register_method(
129 api_name => 'open-ils.storage.actor.user.checked_out.count',
131 method => 'usr_breakdown_out',
141 FROM action.circulation
142 WHERE usr = ? AND checkin_time IS NULL
145 my ($val) = actor::user->db_Main->selectrow_array($sql, {}, $usr);
149 __PACKAGE__->register_method(
150 api_name => 'open-ils.storage.actor.user.total_out',
152 method => 'usr_total_out',
159 local $OpenILS::Application::Storage::WRITE = 1;
161 my $delete_sql = <<" SQL";
162 DELETE FROM actor.org_unit_proximity;
165 my $insert_sql = <<" SQL";
166 INSERT INTO actor.org_unit_proximity (from_org, to_org, prox)
169 actor.org_unit_proximity(l.id,r.id)
170 FROM actor.org_unit l,
174 actor::org_unit_proximity->db_Main->do($delete_sql);
175 actor::org_unit_proximity->db_Main->do($insert_sql);
179 __PACKAGE__->register_method(
180 api_name => 'open-ils.storage.actor.org_unit.refresh_proximity',
182 method => 'calc_proximity',
186 sub org_closed_overlap {
191 my $direction = shift || 0;
192 my $no_hoo = shift || 0;
194 return undef unless ($date && $ou);
196 my $t = actor::org_unit::closed_date->table;
200 WHERE ? between close_start and close_end
202 ORDER BY close_start ASC, close_end DESC
206 $date = clense_ISO8601($date);
207 my ($begin, $end) = ($date,$date);
209 my $hoo = actor::org_unit::hours_of_operation->retrieve($ou);
211 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $date, $ou )) {
212 $begin = clense_ISO8601($closure->{close_start});
213 $end = clense_ISO8601($closure->{close_end});
215 if ( $direction <= 0 ) {
216 $before = $_dt_parser->parse_datetime( $begin );
217 $before->subtract( minutes => 1 );
219 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->iso8601, -1, 1 ) ) {
220 $before = $_dt_parser->parse_datetime( clense_ISO8601($_b->{start}) );
222 $begin = clense_ISO8601($before->iso8601);
225 if ( $direction >= 0 ) {
226 $after = $_dt_parser->parse_datetime( $end );
227 $after->add( minutes => 1 );
229 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->iso8601, 1, 1 ) ) {
230 $after = $_dt_parser->parse_datetime( clense_ISO8601($_a->{end}) );
232 $end = clense_ISO8601($after->iso8601);
239 if ( $direction <= 0 ) {
240 my $begin_dow = $_dt_parser->parse_datetime( $begin )->day_of_week_0;
241 my $begin_open_meth = "dow_".$begin_dow."_open";
242 my $begin_close_meth = "dow_".$begin_dow."_close";
245 while ($hoo->$begin_open_meth eq '00:00:00' and $hoo->$begin_close_meth eq '00:00:00') {
246 $begin = clense_ISO8601($_dt_parser->parse_datetime( $begin )->subtract( days => 1)->iso8601);
250 last if ($count > 6);
251 $begin_open_meth = "dow_".$begin_dow."_open";
252 $begin_close_meth = "dow_".$begin_dow."_close";
255 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $begin, $ou )) {
256 $before = $_dt_parser->parse_datetime( $begin );
257 $before->subtract( minutes => 1 );
258 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->iso8601, -1 ) ) {
259 $before = $_dt_parser->parse_datetime( clense_ISO8601($_b->{start}) );
264 if ( $direction >= 0 ) {
265 my $end_dow = $_dt_parser->parse_datetime( $end )->day_of_week_0;
266 my $end_open_meth = "dow_".$end_dow."_open";
267 my $end_close_meth = "dow_".$end_dow."_close";
270 while ($hoo->$end_open_meth eq '00:00:00' and $hoo->$end_close_meth eq '00:00:00') {
271 $end = clense_ISO8601($_dt_parser->parse_datetime( $end )->add( days => 1)->iso8601);
275 last if ($count > 6);
276 $end_open_meth = "dow_".$end_dow."_open";
277 $end_close_meth = "dow_".$end_dow."_close";
280 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $end, $ou )) {
281 $after = $_dt_parser->parse_datetime( $end );
282 $after->add( minutes => 1 );
284 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->iso8601, 1 ) ) {
285 $after = $_dt_parser->parse_datetime( clense_ISO8601($_a->{end}) );
287 $end = clense_ISO8601($after->iso8601);
294 if ($begin eq $date && $end eq $date) {
298 return { start => $begin, end => $end };
300 __PACKAGE__->register_method(
301 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
303 method => 'org_closed_overlap',
306 sub user_by_barcode {
309 my @barcodes = shift;
311 return undef unless @barcodes;
313 for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
315 if (@barcodes == 1) {
316 return $card->usr->to_fieldmapper;
318 $client->respond( $card->usr->to_fieldmapper);
322 __PACKAGE__->register_method(
323 api_name => 'open-ils.storage.direct.actor.user.search.barcode',
325 method => 'user_by_barcode',
334 my $c = actor::card->table;
335 my $p = actor::user->table;
337 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
339 my $list = actor::user->db_Main->selectcol_arrayref($sql);
340 for my $bc ( @$list ) {
341 $client->respond($bc);
345 __PACKAGE__->register_method(
346 api_name => 'open-ils.storage.actor.user.lost_barcodes',
349 method => 'lost_barcodes',
350 signature => <<' NOTE',
351 Returns an array of barcodes that belong to lost cards.
352 @return array of barcodes
356 sub expired_barcodes {
360 my $c = actor::card->table;
361 my $p = actor::user->table;
363 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
365 my $list = actor::user->db_Main->selectcol_arrayref($sql);
366 for my $bc ( @$list ) {
367 $client->respond($bc);
371 __PACKAGE__->register_method(
372 api_name => 'open-ils.storage.actor.user.expired_barcodes',
375 method => 'expired_barcodes',
376 signature => <<' NOTE',
377 Returns an array of barcodes that are currently expired.
378 @return array of barcodes
382 sub barred_barcodes {
386 my $c = actor::card->table;
387 my $p = actor::user->table;
389 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
391 my $list = actor::user->db_Main->selectcol_arrayref($sql);
392 for my $bc ( @$list ) {
393 $client->respond($bc);
397 __PACKAGE__->register_method(
398 api_name => 'open-ils.storage.actor.user.barred_barcodes',
401 method => 'barred_barcodes',
402 signature => <<' NOTE',
403 Returns an array of barcodes that are currently barred.
404 @return array of barcodes
408 sub penalized_barcodes {
413 my $c = actor::card->table;
414 my $p = actor::user_standing_penalty->table;
416 my $sql = "SELECT c.barcode FROM $c c JOIN $p p USING (usr)";
419 $sql .= ' WHERE penalty_type NOT IN ('. join(',', map { '?' } @ignore) . ')';
422 $sql .= ' GROUP BY c.barcode;';
424 my $list = actor::user->db_Main->selectcol_arrayref($sql, {}, @ignore);
425 for my $bc ( @$list ) {
426 $client->respond($bc);
430 __PACKAGE__->register_method(
431 api_name => 'open-ils.storage.actor.user.penalized_barcodes',
434 method => 'penalized_barcodes',
435 signature => <<' NOTE',
436 Returns an array of barcodes that have penalties not listed
437 as a parameter. Supply a list of any penalty types that should
438 not stop a patron from checking out materials.
440 @param ignore_list Penalty type to ignore
441 @return array of barcodes
450 my $limit = shift || 1000;
452 my $inactive = shift;
454 my $ws_ou_depth = shift || 0;
456 my $strict_opt_in = OpenSRF::Utils::SettingsClient->new->config_value( share => user => 'opt_in' );
458 $sort = ['family_name','first_given_name'] unless ($$sort[0]);
463 # group 2 = phone, ident
465 my $usr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
466 my @usrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
468 my $addr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
469 my @addrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
471 my $pv = $$search{phone}{value};
472 my $iv = $$search{ident}{value};
473 my $nv = $$search{name}{value};
479 for my $p ( qw/day_phone evening_phone other_phone/ ) {
480 push @ps, "LOWER($p) ~ ?";
481 push @phonev, "^$pv";
483 $phone = '(' . join(' OR ', @ps) . ')';
490 for my $i ( qw/ident_value ident_value2/ ) {
491 push @is, "LOWER($i) ~ ?";
492 push @identv, "^$iv";
494 $ident = '(' . join(' OR ', @is) . ')';
501 for my $n ( qw/first_given_name second_given_name family_name/ ) {
502 push @ns, "LOWER($i) ~ ?";
505 $name = '(' . join(' OR ', @ns) . ')';
508 my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
509 my $addr_where = $addr;
512 my $u_table = actor::user->table;
513 my $a_table = actor::user_address->table;
514 my $opt_in_table = actor::usr_org_unit_opt_in->table;
515 my $ou_table = actor::org_unit->table;
517 my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
518 my $a_select = "SELECT usr as id FROM $a_table a WHERE $addr_where";
519 my $clone_select = '';
520 $clone_select = "JOIN (SELECT cu.id as id FROM $a_table ca ".
521 "JOIN $u_table cu ON (cu.mailing_address = ca.id OR cu.billing_address = ca.id) ".
522 "WHERE $addr_where) AS clone ON (clone.id = users.id)" if ($addr_where);
527 $select = "$u_select INTERSECT $a_select";
531 } elsif ($addr_where) {
532 $select = "$a_select";
537 my $order_by = join ', ', map { 'LOWER(users.'. (split / /,$_)[0] . ') ' . (split / /,$_)[1] } @$sort;
538 my $distinct_list = join ', ', map { 'LOWER(users.'. (split / /,$_)[0] . ')' } @$sort;
543 $inactive = 'AND users.active = TRUE';
546 if (!$ws_ou) { # XXX This should be required!!
547 $ws_ou = actor::org_unit->search( { parent_ou => undef } )->next->id;
550 my $opt_in_join = '';
551 my $opt_in_where = '';
552 if (lc($strict_opt_in) eq 'true') {
553 $opt_in_join = "LEFT JOIN $opt_in_table oi ON (oi.org_unit = $ws_ou AND users.id = oi.usr)";
554 $opt_in_where = "AND (oi.id IS NOT NULL OR users.home_ou = $ws_ou)";
557 my $descendants = "actor.org_unit_descendants($ws_ou, $ws_ou_depth)";
560 SELECT DISTINCT $distinct_list
561 FROM $u_table AS users
562 JOIN ($select) AS search ON (search.id = users.id)
563 JOIN $descendants d ON (d.id = users.home_ou)
566 WHERE users.deleted = FALSE
573 return actor::user->db_Main->selectcol_arrayref($select, {Columns=>[scalar(@$sort)]}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv,@addrv));
575 __PACKAGE__->register_method(
576 api_name => 'open-ils.storage.actor.user.crazy_search',
578 method => 'patron_search',
581 =comment not gonna use it...
586 my $searches = shift;
588 return undef unless (defined $searches);
590 for my $usr ( actor::user->search( $searches ) ) {
592 $client->respond( flesh_user( $usr ) );
596 __PACKAGE__->register_method(
597 api_name => 'open-ils.storage.fleshed.actor.user.search',
599 method => 'fleshed_search',
604 sub fleshed_search_like {
607 my $searches = shift;
609 return undef unless (defined $searches);
611 for my $usr ( actor::user->search_like( $searches ) ) {
613 $client->respond( flesh_user( $usr ) );
617 __PACKAGE__->register_method(
618 api_name => 'open-ils.storage.fleshed.actor.user.search_like',
620 method => 'user_by_barcode',
625 sub retrieve_fleshed_user {
630 return undef unless @ids;
632 @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
634 $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
638 __PACKAGE__->register_method(
639 api_name => 'open-ils.storage.fleshed.actor.user.retrieve',
641 method => 'retrieve_fleshed_user',
644 __PACKAGE__->register_method(
645 api_name => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
647 method => 'retrieve_fleshed_user',
656 my $standing = $usr->standing;
657 my $profile = $usr->profile;
658 my $ident_type = $usr->ident_type;
660 my $maddress = $usr->mailing_address;
661 my $baddress = $usr->billing_address;
662 my $card = $usr->card;
664 my @addresses = $usr->addresses;
665 my @cards = $usr->cards;
667 my $usr_fm = $usr->to_fieldmapper;
668 $usr_fm->standing( $standing->to_fieldmapper );
669 $usr_fm->profile( $profile->to_fieldmapper );
670 $usr_fm->ident_type( $ident_type->to_fieldmapper );
672 $usr_fm->card( $card->to_fieldmapper );
673 $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
674 $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
676 $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
677 $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
688 my $select =<<" SQL";
691 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
694 my $sth = actor::org_unit->db_Main->prepare_cached($select);
697 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
701 __PACKAGE__->register_method(
702 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
705 method => 'org_unit_list',
708 sub org_unit_type_list {
712 my $select =<<" SQL";
714 FROM actor.org_unit_type
715 ORDER BY depth, name;
718 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
721 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
725 __PACKAGE__->register_method(
726 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
729 method => 'org_unit_type_list',
732 sub org_unit_full_path {
737 return undef unless (@binds);
739 my $func = 'actor.org_unit_full_path(?)';
740 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
742 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
743 $sth->execute(@binds);
745 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
749 __PACKAGE__->register_method(
750 api_name => 'open-ils.storage.actor.org_unit.full_path',
753 method => 'org_unit_full_path',
756 sub org_unit_ancestors {
761 return undef unless ($id);
763 my $func = 'actor.org_unit_ancestors(?)';
765 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
768 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
769 ORDER BY t.depth, f.name;
771 $sth->execute(''.$id);
773 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
777 __PACKAGE__->register_method(
778 api_name => 'open-ils.storage.actor.org_unit.ancestors',
781 method => 'org_unit_ancestors',
784 sub org_unit_descendants {
790 return undef unless ($id);
792 my $func = 'actor.org_unit_descendants(?)';
793 if (defined $depth) {
794 $func = 'actor.org_unit_descendants(?,?)';
797 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
798 $sth->execute(''.$id, ''.$depth) if (defined $depth);
799 $sth->execute(''.$id) unless (defined $depth);
801 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
805 __PACKAGE__->register_method(
806 api_name => 'open-ils.storage.actor.org_unit.descendants',
809 method => 'org_unit_descendants',
812 sub fleshed_actor_stat_cat {
817 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
820 my $cat = actor::stat_cat->retrieve($sc);
823 my $sc_fm = $cat->to_fieldmapper;
824 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
826 $client->respond( $sc_fm );
832 __PACKAGE__->register_method(
833 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
836 method => 'fleshed_actor_stat_cat',
839 __PACKAGE__->register_method(
840 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
844 method => 'fleshed_actor_stat_cat',
847 #XXX Fix stored proc calls
848 sub ranged_actor_stat_cat_all {
853 return undef unless ($ou);
854 my $s_table = actor::stat_cat->table;
856 my $select = <<" SQL";
859 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
864 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
866 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
869 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
870 my $sc_fm = $sc->to_fieldmapper;
872 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
874 $client->respond( $sc_fm );
879 __PACKAGE__->register_method(
880 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
884 method => 'ranged_actor_stat_cat_all',
887 __PACKAGE__->register_method(
888 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
892 method => 'ranged_actor_stat_cat_all',
895 #XXX Fix stored proc calls
896 sub ranged_actor_stat_cat_entry {
902 return undef unless ($ou);
903 my $s_table = actor::stat_cat_entry->table;
905 my $select = <<" SQL";
908 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
913 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
914 $sth->execute($ou,$sc);
916 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
917 $client->respond( $sce->to_fieldmapper );
922 __PACKAGE__->register_method(
923 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
926 method => 'ranged_actor_stat_cat_entry',