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'))
61 my $out = actor::user->db_Main->selectcol_arrayref($out_sql, {}, $usr);
63 my $od_sql = <<" SQL";
65 FROM action.circulation
66 WHERE usr = ? AND checkin_time IS NULL AND due_date < 'today' AND (stop_fines IS NULL OR stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE'))
69 my $od = actor::user->db_Main->selectcol_arrayref($od_sql, {}, $usr);
71 my $lost_sql = <<" SQL";
73 FROM action.circulation
74 WHERE usr = ? AND checkin_time IS NULL AND xact_finish IS NULL AND stop_fines = 'LOST'
77 my $lost = actor::user->db_Main->selectcol_arrayref($lost_sql, {}, $usr);
79 my $cl_sql = <<" SQL";
81 FROM action.circulation
82 WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'CLAIMSRETURNED'
85 my $cl = actor::user->db_Main->selectcol_arrayref($cl_sql, {}, $usr);
87 my $lo_sql = <<" SQL";
89 FROM action.circulation
90 WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'LONGOVERDUE'
93 my $lo = actor::user->db_Main->selectcol_arrayref($lo_sql, {}, $usr);
95 if ($self->api_name =~/count$/o) {
96 return { total => scalar(@$out) + scalar(@$od) + scalar(@$lost) + scalar(@$cl) + scalar(@$lo),
98 overdue => scalar(@$od),
99 lost => scalar(@$lost),
100 claims_returned => scalar(@$cl),
101 long_overdue => scalar(@$lo),
105 return { out => $out,
108 claims_returned => $cl,
112 __PACKAGE__->register_method(
113 api_name => 'open-ils.storage.actor.user.checked_out',
115 method => 'usr_breakdown_out',
117 __PACKAGE__->register_method(
118 api_name => 'open-ils.storage.actor.user.checked_out.count',
120 method => 'usr_breakdown_out',
130 FROM action.circulation
131 WHERE usr = ? AND checkin_time IS NULL
134 my ($val) = actor::user->db_Main->selectrow_array($sql, {}, $usr);
138 __PACKAGE__->register_method(
139 api_name => 'open-ils.storage.actor.user.total_out',
141 method => 'usr_total_out',
148 local $OpenILS::Application::Storage::WRITE = 1;
150 my $delete_sql = <<" SQL";
151 DELETE FROM actor.org_unit_proximity;
154 my $insert_sql = <<" SQL";
155 INSERT INTO actor.org_unit_proximity (from_org, to_org, prox)
158 actor.org_unit_proximity(l.id,r.id)
159 FROM actor.org_unit l,
163 actor::org_unit_proximity->db_Main->do($delete_sql);
164 actor::org_unit_proximity->db_Main->do($insert_sql);
168 __PACKAGE__->register_method(
169 api_name => 'open-ils.storage.actor.org_unit.refresh_proximity',
171 method => 'calc_proximity',
175 sub org_closed_overlap {
180 my $direction = shift || 0;
181 my $no_hoo = shift || 0;
183 return undef unless ($date && $ou);
185 my $t = actor::org_unit::closed_date->table;
189 WHERE ? between close_start and close_end
191 ORDER BY close_start ASC, close_end DESC
195 $date = clense_ISO8601($date);
196 my ($begin, $end) = ($date,$date);
198 my $hoo = actor::org_unit::hours_of_operation->retrieve($ou);
200 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $date, $ou )) {
201 $begin = clense_ISO8601($closure->{close_start});
202 $end = clense_ISO8601($closure->{close_end});
204 if ( $direction <= 0 ) {
205 $before = $_dt_parser->parse_datetime( $begin );
206 $before->subtract( minutes => 1 );
208 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->iso8601, -1, 1 ) ) {
209 $before = $_dt_parser->parse_datetime( clense_ISO8601($_b->{start}) );
211 $begin = clense_ISO8601($before->iso8601);
214 if ( $direction >= 0 ) {
215 $after = $_dt_parser->parse_datetime( $end );
216 $after->add( minutes => 1 );
218 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->iso8601, 1, 1 ) ) {
219 $after = $_dt_parser->parse_datetime( clense_ISO8601($_a->{end}) );
221 $end = clense_ISO8601($after->iso8601);
228 if ( $direction <= 0 ) {
229 my $begin_dow = $_dt_parser->parse_datetime( $begin )->day_of_week_0;
230 my $begin_open_meth = "dow_".$begin_dow."_open";
231 my $begin_close_meth = "dow_".$begin_dow."_close";
234 while ($hoo->$begin_open_meth eq '00:00:00' and $hoo->$begin_close_meth eq '00:00:00') {
235 $begin = clense_ISO8601($_dt_parser->parse_datetime( $begin )->subtract( days => 1)->iso8601);
239 last if ($count > 6);
240 $begin_open_meth = "dow_".$begin_dow."_open";
241 $begin_close_meth = "dow_".$begin_dow."_close";
244 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $begin, $ou )) {
245 $before = $_dt_parser->parse_datetime( $begin );
246 $before->subtract( minutes => 1 );
247 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->iso8601, -1 ) ) {
248 $before = $_dt_parser->parse_datetime( clense_ISO8601($_b->{start}) );
253 if ( $direction >= 0 ) {
254 my $end_dow = $_dt_parser->parse_datetime( $end )->day_of_week_0;
255 my $end_open_meth = "dow_".$end_dow."_open";
256 my $end_close_meth = "dow_".$end_dow."_close";
259 while ($hoo->$end_open_meth eq '00:00:00' and $hoo->$end_close_meth eq '00:00:00') {
260 $end = clense_ISO8601($_dt_parser->parse_datetime( $end )->add( days => 1)->iso8601);
264 last if ($count > 6);
265 $end_open_meth = "dow_".$end_dow."_open";
266 $end_close_meth = "dow_".$end_dow."_close";
269 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $end, $ou )) {
270 $after = $_dt_parser->parse_datetime( $end );
271 $after->add( minutes => 1 );
273 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->iso8601, 1 ) ) {
274 $after = $_dt_parser->parse_datetime( clense_ISO8601($_a->{end}) );
276 $end = clense_ISO8601($after->iso8601);
283 if ($begin eq $date && $end eq $date) {
287 return { start => $begin, end => $end };
289 __PACKAGE__->register_method(
290 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
292 method => 'org_closed_overlap',
295 sub user_by_barcode {
298 my @barcodes = shift;
300 return undef unless @barcodes;
302 for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
304 if (@barcodes == 1) {
305 return $card->usr->to_fieldmapper;
307 $client->respond( $card->usr->to_fieldmapper);
311 __PACKAGE__->register_method(
312 api_name => 'open-ils.storage.direct.actor.user.search.barcode',
314 method => 'user_by_barcode',
323 my $c = actor::card->table;
324 my $p = actor::user->table;
326 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
328 my $list = actor::user->db_Main->selectcol_arrayref($sql);
329 for my $bc ( @$list ) {
330 $client->respond($bc);
334 __PACKAGE__->register_method(
335 api_name => 'open-ils.storage.actor.user.lost_barcodes',
338 method => 'lost_barcodes',
339 signature => <<' NOTE',
340 Returns an array of barcodes that belong to lost cards.
341 @return array of barcodes
345 sub expired_barcodes {
349 my $c = actor::card->table;
350 my $p = actor::user->table;
352 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
354 my $list = actor::user->db_Main->selectcol_arrayref($sql);
355 for my $bc ( @$list ) {
356 $client->respond($bc);
360 __PACKAGE__->register_method(
361 api_name => 'open-ils.storage.actor.user.expired_barcodes',
364 method => 'expired_barcodes',
365 signature => <<' NOTE',
366 Returns an array of barcodes that are currently expired.
367 @return array of barcodes
371 sub barred_barcodes {
375 my $c = actor::card->table;
376 my $p = actor::user->table;
378 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
380 my $list = actor::user->db_Main->selectcol_arrayref($sql);
381 for my $bc ( @$list ) {
382 $client->respond($bc);
386 __PACKAGE__->register_method(
387 api_name => 'open-ils.storage.actor.user.barred_barcodes',
390 method => 'barred_barcodes',
391 signature => <<' NOTE',
392 Returns an array of barcodes that are currently barred.
393 @return array of barcodes
397 sub penalized_barcodes {
402 my $c = actor::card->table;
403 my $p = actor::user_standing_penalty->table;
405 my $sql = "SELECT c.barcode FROM $c c JOIN $p p USING (usr)";
408 $sql .= ' WHERE penalty_type NOT IN ('. join(',', map { '?' } @ignore) . ')';
411 $sql .= ' GROUP BY c.barcode;';
413 my $list = actor::user->db_Main->selectcol_arrayref($sql, {}, @ignore);
414 for my $bc ( @$list ) {
415 $client->respond($bc);
419 __PACKAGE__->register_method(
420 api_name => 'open-ils.storage.actor.user.penalized_barcodes',
423 method => 'penalized_barcodes',
424 signature => <<' NOTE',
425 Returns an array of barcodes that have penalties not listed
426 as a parameter. Supply a list of any penalty types that should
427 not stop a patron from checking out materials.
429 @param ignore_list Penalty type to ignore
430 @return array of barcodes
439 my $limit = shift || 1000;
441 my $inactive = shift;
443 my $ws_ou_depth = shift || 0;
445 my $strict_opt_in = OpenSRF::Utils::SettingsClient->new->config_value( share => user => 'opt_in' );
447 $sort = ['family_name','first_given_name'] unless ($$sort[0]);
452 # group 2 = phone, ident
454 my $usr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
455 my @usrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
457 my $addr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
458 my @addrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
460 my $pv = $$search{phone}{value};
461 my $iv = $$search{ident}{value};
462 my $nv = $$search{name}{value};
468 for my $p ( qw/day_phone evening_phone other_phone/ ) {
469 push @ps, "LOWER($p) ~ ?";
470 push @phonev, "^$pv";
472 $phone = '(' . join(' OR ', @ps) . ')';
479 for my $i ( qw/ident_value ident_value2/ ) {
480 push @is, "LOWER($i) ~ ?";
481 push @identv, "^$iv";
483 $ident = '(' . join(' OR ', @is) . ')';
490 for my $n ( qw/first_given_name second_given_name family_name/ ) {
491 push @ns, "LOWER($i) ~ ?";
494 $name = '(' . join(' OR ', @ns) . ')';
497 my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
498 my $addr_where = $addr;
501 my $u_table = actor::user->table;
502 my $a_table = actor::user_address->table;
503 my $opt_in_table = actor::usr_org_unit_opt_in->table;
504 my $ou_table = actor::org_unit->table;
506 my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
507 my $a_select = "SELECT usr as id FROM $a_table a WHERE $addr_where";
508 my $clone_select = '';
509 $clone_select = "JOIN (SELECT cu.id as id FROM $a_table ca ".
510 "JOIN $u_table cu ON (cu.mailing_address = ca.id OR cu.billing_address = ca.id) ".
511 "WHERE $addr_where) AS clone USING (id)" if ($addr_where);
516 $select = "$u_select INTERSECT $a_select";
520 } elsif ($addr_where) {
521 $select = "$a_select";
526 my $order_by = join ', ', map { 'LOWER(users.'. (split / /,$_)[0] . ') ' . (split / /,$_)[1] } @$sort;
527 my $distinct_list = join ', ', map { 'LOWER(users.'. (split / /,$_)[0] . ')' } @$sort;
532 $inactive = 'AND users.active = TRUE';
535 if (!$ws_ou) { # XXX This should be required!!
536 $ws_ou = actor::org_unit->search( { parent_ou => undef } )->next->id;
539 my $opt_in_join = '';
540 my $opt_in_where = '';
541 if (lc($strict_opt_in) eq 'true') {
542 $opt_in_join = "LEFT JOIN $opt_in_table oi ON (oi.org_unit = $ws_ou AND users.id = oi.usr)";
543 $opt_in_where = "AND (oi.id IS NOT NULL OR users.home_ou = $ws_ou)";
546 my $descendants = "actor.org_unit_descendants($ws_ou, $ws_ou_depth)";
549 SELECT DISTINCT $distinct_list
550 FROM $u_table AS users
551 JOIN ($select) AS search USING (id)
552 JOIN $descendants d ON (d.id = users.home_ou)
555 WHERE users.deleted = FALSE
562 return actor::user->db_Main->selectcol_arrayref($select, {Columns=>[scalar(@$sort)]}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv,@addrv));
564 __PACKAGE__->register_method(
565 api_name => 'open-ils.storage.actor.user.crazy_search',
567 method => 'patron_search',
570 =comment not gonna use it...
575 my $searches = shift;
577 return undef unless (defined $searches);
579 for my $usr ( actor::user->search( $searches ) ) {
581 $client->respond( flesh_user( $usr ) );
585 __PACKAGE__->register_method(
586 api_name => 'open-ils.storage.fleshed.actor.user.search',
588 method => 'fleshed_search',
593 sub fleshed_search_like {
596 my $searches = shift;
598 return undef unless (defined $searches);
600 for my $usr ( actor::user->search_like( $searches ) ) {
602 $client->respond( flesh_user( $usr ) );
606 __PACKAGE__->register_method(
607 api_name => 'open-ils.storage.fleshed.actor.user.search_like',
609 method => 'user_by_barcode',
614 sub retrieve_fleshed_user {
619 return undef unless @ids;
621 @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
623 $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
627 __PACKAGE__->register_method(
628 api_name => 'open-ils.storage.fleshed.actor.user.retrieve',
630 method => 'retrieve_fleshed_user',
633 __PACKAGE__->register_method(
634 api_name => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
636 method => 'retrieve_fleshed_user',
645 my $standing = $usr->standing;
646 my $profile = $usr->profile;
647 my $ident_type = $usr->ident_type;
649 my $maddress = $usr->mailing_address;
650 my $baddress = $usr->billing_address;
651 my $card = $usr->card;
653 my @addresses = $usr->addresses;
654 my @cards = $usr->cards;
656 my $usr_fm = $usr->to_fieldmapper;
657 $usr_fm->standing( $standing->to_fieldmapper );
658 $usr_fm->profile( $profile->to_fieldmapper );
659 $usr_fm->ident_type( $ident_type->to_fieldmapper );
661 $usr_fm->card( $card->to_fieldmapper );
662 $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
663 $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
665 $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
666 $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
677 my $select =<<" SQL";
680 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
683 my $sth = actor::org_unit->db_Main->prepare_cached($select);
686 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
690 __PACKAGE__->register_method(
691 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
694 method => 'org_unit_list',
697 sub org_unit_type_list {
701 my $select =<<" SQL";
703 FROM actor.org_unit_type
704 ORDER BY depth, name;
707 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
710 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
714 __PACKAGE__->register_method(
715 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
718 method => 'org_unit_type_list',
721 sub org_unit_full_path {
726 return undef unless (@binds);
728 my $func = 'actor.org_unit_full_path(?)';
729 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
731 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
732 $sth->execute(@binds);
734 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
738 __PACKAGE__->register_method(
739 api_name => 'open-ils.storage.actor.org_unit.full_path',
742 method => 'org_unit_full_path',
745 sub org_unit_ancestors {
750 return undef unless ($id);
752 my $func = 'actor.org_unit_ancestors(?)';
754 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
757 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
758 ORDER BY t.depth, f.name;
760 $sth->execute(''.$id);
762 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
766 __PACKAGE__->register_method(
767 api_name => 'open-ils.storage.actor.org_unit.ancestors',
770 method => 'org_unit_ancestors',
773 sub org_unit_descendants {
779 return undef unless ($id);
781 my $func = 'actor.org_unit_descendants(?)';
782 if (defined $depth) {
783 $func = 'actor.org_unit_descendants(?,?)';
786 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
787 $sth->execute(''.$id, ''.$depth) if (defined $depth);
788 $sth->execute(''.$id) unless (defined $depth);
790 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
794 __PACKAGE__->register_method(
795 api_name => 'open-ils.storage.actor.org_unit.descendants',
798 method => 'org_unit_descendants',
801 sub fleshed_actor_stat_cat {
806 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
809 my $cat = actor::stat_cat->retrieve($sc);
812 my $sc_fm = $cat->to_fieldmapper;
813 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
815 $client->respond( $sc_fm );
821 __PACKAGE__->register_method(
822 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
825 method => 'fleshed_actor_stat_cat',
828 __PACKAGE__->register_method(
829 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
833 method => 'fleshed_actor_stat_cat',
836 #XXX Fix stored proc calls
837 sub ranged_actor_stat_cat_all {
842 return undef unless ($ou);
843 my $s_table = actor::stat_cat->table;
845 my $select = <<" SQL";
848 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
853 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
855 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
858 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
859 my $sc_fm = $sc->to_fieldmapper;
861 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
863 $client->respond( $sc_fm );
868 __PACKAGE__->register_method(
869 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
873 method => 'ranged_actor_stat_cat_all',
876 __PACKAGE__->register_method(
877 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
881 method => 'ranged_actor_stat_cat_all',
884 #XXX Fix stored proc calls
885 sub ranged_actor_stat_cat_entry {
891 return undef unless ($ou);
892 my $s_table = actor::stat_cat_entry->table;
894 my $select = <<" SQL";
897 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
902 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
903 $sth->execute($ou,$sc);
905 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
906 $client->respond( $sce->to_fieldmapper );
911 __PACKAGE__->register_method(
912 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
915 method => 'ranged_actor_stat_cat_entry',