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
59 AND checkin_time IS NULL
60 AND ( (fine_interval >= '1 day' AND due_date >= 'today')
61 OR (fine_interval < '1 day' AND due_date > 'now' ))
62 AND (stop_fines IS NULL
63 OR stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE'))
66 my $out = actor::user->db_Main->selectcol_arrayref($out_sql, {}, $usr);
68 my $od_sql = <<" SQL";
70 FROM action.circulation
72 AND checkin_time IS NULL
73 AND ( (fine_interval >= '1 day' AND due_date < 'today')
74 OR (fine_interval < '1 day' AND due_date < 'now' ))
75 AND (stop_fines IS NULL
76 OR stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE'))
79 my $od = actor::user->db_Main->selectcol_arrayref($od_sql, {}, $usr);
81 my $lost_sql = <<" SQL";
83 FROM action.circulation
84 WHERE usr = ? AND checkin_time IS NULL AND xact_finish IS NULL AND stop_fines = 'LOST'
87 my $lost = actor::user->db_Main->selectcol_arrayref($lost_sql, {}, $usr);
89 my $cl_sql = <<" SQL";
91 FROM action.circulation
92 WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'CLAIMSRETURNED'
95 my $cl = actor::user->db_Main->selectcol_arrayref($cl_sql, {}, $usr);
97 my $lo_sql = <<" SQL";
99 FROM action.circulation
100 WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'LONGOVERDUE'
103 my $lo = actor::user->db_Main->selectcol_arrayref($lo_sql, {}, $usr);
105 if ($self->api_name =~/count$/o) {
106 return { total => scalar(@$out) + scalar(@$od) + scalar(@$lost) + scalar(@$cl) + scalar(@$lo),
107 out => scalar(@$out),
108 overdue => scalar(@$od),
109 lost => scalar(@$lost),
110 claims_returned => scalar(@$cl),
111 long_overdue => scalar(@$lo),
115 return { out => $out,
118 claims_returned => $cl,
122 __PACKAGE__->register_method(
123 api_name => 'open-ils.storage.actor.user.checked_out',
125 method => 'usr_breakdown_out',
127 __PACKAGE__->register_method(
128 api_name => 'open-ils.storage.actor.user.checked_out.count',
130 method => 'usr_breakdown_out',
140 FROM action.circulation
141 WHERE usr = ? AND checkin_time IS NULL
144 my ($val) = actor::user->db_Main->selectrow_array($sql, {}, $usr);
148 __PACKAGE__->register_method(
149 api_name => 'open-ils.storage.actor.user.total_out',
151 method => 'usr_total_out',
158 local $OpenILS::Application::Storage::WRITE = 1;
160 my $delete_sql = <<" SQL";
161 DELETE FROM actor.org_unit_proximity;
164 my $insert_sql = <<" SQL";
165 INSERT INTO actor.org_unit_proximity (from_org, to_org, prox)
168 actor.org_unit_proximity(l.id,r.id)
169 FROM actor.org_unit l,
173 actor::org_unit_proximity->db_Main->do($delete_sql);
174 actor::org_unit_proximity->db_Main->do($insert_sql);
178 __PACKAGE__->register_method(
179 api_name => 'open-ils.storage.actor.org_unit.refresh_proximity',
181 method => 'calc_proximity',
185 sub org_closed_overlap {
190 my $direction = shift || 0;
191 my $no_hoo = shift || 0;
193 return undef unless ($date && $ou);
195 my $t = actor::org_unit::closed_date->table;
199 WHERE ? between close_start and close_end
201 ORDER BY close_start ASC, close_end DESC
205 $date = clense_ISO8601($date);
206 my ($begin, $end) = ($date,$date);
208 my $hoo = actor::org_unit::hours_of_operation->retrieve($ou);
210 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $date, $ou )) {
211 $begin = clense_ISO8601($closure->{close_start});
212 $end = clense_ISO8601($closure->{close_end});
214 if ( $direction <= 0 ) {
215 $before = $_dt_parser->parse_datetime( $begin );
216 $before->subtract( minutes => 1 );
218 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->iso8601, -1, 1 ) ) {
219 $before = $_dt_parser->parse_datetime( clense_ISO8601($_b->{start}) );
221 $begin = clense_ISO8601($before->iso8601);
224 if ( $direction >= 0 ) {
225 $after = $_dt_parser->parse_datetime( $end );
226 $after->add( minutes => 1 );
228 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->iso8601, 1, 1 ) ) {
229 $after = $_dt_parser->parse_datetime( clense_ISO8601($_a->{end}) );
231 $end = clense_ISO8601($after->iso8601);
238 if ( $direction <= 0 ) {
239 my $begin_dow = $_dt_parser->parse_datetime( $begin )->day_of_week_0;
240 my $begin_open_meth = "dow_".$begin_dow."_open";
241 my $begin_close_meth = "dow_".$begin_dow."_close";
244 while ($hoo->$begin_open_meth eq '00:00:00' and $hoo->$begin_close_meth eq '00:00:00') {
245 $begin = clense_ISO8601($_dt_parser->parse_datetime( $begin )->subtract( days => 1)->iso8601);
249 last if ($count > 6);
250 $begin_open_meth = "dow_".$begin_dow."_open";
251 $begin_close_meth = "dow_".$begin_dow."_close";
254 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $begin, $ou )) {
255 $before = $_dt_parser->parse_datetime( $begin );
256 $before->subtract( minutes => 1 );
257 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->iso8601, -1 ) ) {
258 $before = $_dt_parser->parse_datetime( clense_ISO8601($_b->{start}) );
263 if ( $direction >= 0 ) {
264 my $end_dow = $_dt_parser->parse_datetime( $end )->day_of_week_0;
265 my $end_open_meth = "dow_".$end_dow."_open";
266 my $end_close_meth = "dow_".$end_dow."_close";
269 while ($hoo->$end_open_meth eq '00:00:00' and $hoo->$end_close_meth eq '00:00:00') {
270 $end = clense_ISO8601($_dt_parser->parse_datetime( $end )->add( days => 1)->iso8601);
274 last if ($count > 6);
275 $end_open_meth = "dow_".$end_dow."_open";
276 $end_close_meth = "dow_".$end_dow."_close";
279 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $end, $ou )) {
280 $after = $_dt_parser->parse_datetime( $end );
281 $after->add( minutes => 1 );
283 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->iso8601, 1 ) ) {
284 $after = $_dt_parser->parse_datetime( clense_ISO8601($_a->{end}) );
286 $end = clense_ISO8601($after->iso8601);
293 if ($begin eq $date && $end eq $date) {
297 return { start => $begin, end => $end };
299 __PACKAGE__->register_method(
300 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
302 method => 'org_closed_overlap',
305 sub user_by_barcode {
308 my @barcodes = shift;
310 return undef unless @barcodes;
312 for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
314 if (@barcodes == 1) {
315 return $card->usr->to_fieldmapper;
317 $client->respond( $card->usr->to_fieldmapper);
321 __PACKAGE__->register_method(
322 api_name => 'open-ils.storage.direct.actor.user.search.barcode',
324 method => 'user_by_barcode',
333 my $c = actor::card->table;
334 my $p = actor::user->table;
336 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
338 my $list = actor::user->db_Main->selectcol_arrayref($sql);
339 for my $bc ( @$list ) {
340 $client->respond($bc);
344 __PACKAGE__->register_method(
345 api_name => 'open-ils.storage.actor.user.lost_barcodes',
348 method => 'lost_barcodes',
349 signature => <<' NOTE',
350 Returns an array of barcodes that belong to lost cards.
351 @return array of barcodes
355 sub expired_barcodes {
359 my $c = actor::card->table;
360 my $p = actor::user->table;
362 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
364 my $list = actor::user->db_Main->selectcol_arrayref($sql);
365 for my $bc ( @$list ) {
366 $client->respond($bc);
370 __PACKAGE__->register_method(
371 api_name => 'open-ils.storage.actor.user.expired_barcodes',
374 method => 'expired_barcodes',
375 signature => <<' NOTE',
376 Returns an array of barcodes that are currently expired.
377 @return array of barcodes
381 sub barred_barcodes {
385 my $c = actor::card->table;
386 my $p = actor::user->table;
388 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
390 my $list = actor::user->db_Main->selectcol_arrayref($sql);
391 for my $bc ( @$list ) {
392 $client->respond($bc);
396 __PACKAGE__->register_method(
397 api_name => 'open-ils.storage.actor.user.barred_barcodes',
400 method => 'barred_barcodes',
401 signature => <<' NOTE',
402 Returns an array of barcodes that are currently barred.
403 @return array of barcodes
407 sub penalized_barcodes {
412 my $c = actor::card->table;
413 my $p = actor::user_standing_penalty->table;
415 my $sql = "SELECT c.barcode FROM $c c JOIN $p p USING (usr)";
418 $sql .= ' WHERE penalty_type NOT IN ('. join(',', map { '?' } @ignore) . ')';
421 $sql .= ' GROUP BY c.barcode;';
423 my $list = actor::user->db_Main->selectcol_arrayref($sql, {}, @ignore);
424 for my $bc ( @$list ) {
425 $client->respond($bc);
429 __PACKAGE__->register_method(
430 api_name => 'open-ils.storage.actor.user.penalized_barcodes',
433 method => 'penalized_barcodes',
434 signature => <<' NOTE',
435 Returns an array of barcodes that have penalties not listed
436 as a parameter. Supply a list of any penalty types that should
437 not stop a patron from checking out materials.
439 @param ignore_list Penalty type to ignore
440 @return array of barcodes
449 my $limit = shift || 1000;
451 my $inactive = shift;
453 my $ws_ou_depth = shift || 0;
455 my $strict_opt_in = OpenSRF::Utils::SettingsClient->new->config_value( share => user => 'opt_in' );
457 $sort = ['family_name','first_given_name'] unless ($$sort[0]);
462 # group 2 = phone, ident
464 my $usr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
465 my @usrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
467 my $addr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
468 my @addrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
470 my $pv = $$search{phone}{value};
471 my $iv = $$search{ident}{value};
472 my $nv = $$search{name}{value};
478 for my $p ( qw/day_phone evening_phone other_phone/ ) {
479 push @ps, "LOWER($p) ~ ?";
480 push @phonev, "^$pv";
482 $phone = '(' . join(' OR ', @ps) . ')';
489 for my $i ( qw/ident_value ident_value2/ ) {
490 push @is, "LOWER($i) ~ ?";
491 push @identv, "^$iv";
493 $ident = '(' . join(' OR ', @is) . ')';
500 for my $n ( qw/first_given_name second_given_name family_name/ ) {
501 push @ns, "LOWER($i) ~ ?";
504 $name = '(' . join(' OR ', @ns) . ')';
507 my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
508 my $addr_where = $addr;
511 my $u_table = actor::user->table;
512 my $a_table = actor::user_address->table;
513 my $opt_in_table = actor::usr_org_unit_opt_in->table;
514 my $ou_table = actor::org_unit->table;
516 my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
517 my $a_select = "SELECT usr as id FROM $a_table a WHERE $addr_where";
518 my $clone_select = '';
519 $clone_select = "JOIN (SELECT cu.id as id FROM $a_table ca ".
520 "JOIN $u_table cu ON (cu.mailing_address = ca.id OR cu.billing_address = ca.id) ".
521 "WHERE $addr_where) AS clone ON (clone.id = users.id)" if ($addr_where);
526 $select = "$u_select INTERSECT $a_select";
530 } elsif ($addr_where) {
531 $select = "$a_select";
536 my $order_by = join ', ', map { 'LOWER(users.'. (split / /,$_)[0] . ') ' . (split / /,$_)[1] } @$sort;
537 my $distinct_list = join ', ', map { 'LOWER(users.'. (split / /,$_)[0] . ')' } @$sort;
542 $inactive = 'AND users.active = TRUE';
545 if (!$ws_ou) { # XXX This should be required!!
546 $ws_ou = actor::org_unit->search( { parent_ou => undef } )->next->id;
549 my $opt_in_join = '';
550 my $opt_in_where = '';
551 if (lc($strict_opt_in) eq 'true') {
552 $opt_in_join = "LEFT JOIN $opt_in_table oi ON (oi.org_unit = $ws_ou AND users.id = oi.usr)";
553 $opt_in_where = "AND (oi.id IS NOT NULL OR users.home_ou = $ws_ou)";
556 my $descendants = "actor.org_unit_descendants($ws_ou, $ws_ou_depth)";
559 SELECT DISTINCT $distinct_list
560 FROM $u_table AS users
561 JOIN ($select) AS search ON (search.id = users.id)
562 JOIN $descendants d ON (d.id = users.home_ou)
565 WHERE users.deleted = FALSE
572 return actor::user->db_Main->selectcol_arrayref($select, {Columns=>[scalar(@$sort)]}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv,@addrv));
574 __PACKAGE__->register_method(
575 api_name => 'open-ils.storage.actor.user.crazy_search',
577 method => 'patron_search',
580 =comment not gonna use it...
585 my $searches = shift;
587 return undef unless (defined $searches);
589 for my $usr ( actor::user->search( $searches ) ) {
591 $client->respond( flesh_user( $usr ) );
595 __PACKAGE__->register_method(
596 api_name => 'open-ils.storage.fleshed.actor.user.search',
598 method => 'fleshed_search',
603 sub fleshed_search_like {
606 my $searches = shift;
608 return undef unless (defined $searches);
610 for my $usr ( actor::user->search_like( $searches ) ) {
612 $client->respond( flesh_user( $usr ) );
616 __PACKAGE__->register_method(
617 api_name => 'open-ils.storage.fleshed.actor.user.search_like',
619 method => 'user_by_barcode',
624 sub retrieve_fleshed_user {
629 return undef unless @ids;
631 @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
633 $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
637 __PACKAGE__->register_method(
638 api_name => 'open-ils.storage.fleshed.actor.user.retrieve',
640 method => 'retrieve_fleshed_user',
643 __PACKAGE__->register_method(
644 api_name => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
646 method => 'retrieve_fleshed_user',
655 my $standing = $usr->standing;
656 my $profile = $usr->profile;
657 my $ident_type = $usr->ident_type;
659 my $maddress = $usr->mailing_address;
660 my $baddress = $usr->billing_address;
661 my $card = $usr->card;
663 my @addresses = $usr->addresses;
664 my @cards = $usr->cards;
666 my $usr_fm = $usr->to_fieldmapper;
667 $usr_fm->standing( $standing->to_fieldmapper );
668 $usr_fm->profile( $profile->to_fieldmapper );
669 $usr_fm->ident_type( $ident_type->to_fieldmapper );
671 $usr_fm->card( $card->to_fieldmapper );
672 $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
673 $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
675 $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
676 $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
687 my $select =<<" SQL";
690 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
693 my $sth = actor::org_unit->db_Main->prepare_cached($select);
696 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
700 __PACKAGE__->register_method(
701 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
704 method => 'org_unit_list',
707 sub org_unit_type_list {
711 my $select =<<" SQL";
713 FROM actor.org_unit_type
714 ORDER BY depth, name;
717 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
720 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
724 __PACKAGE__->register_method(
725 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
728 method => 'org_unit_type_list',
731 sub org_unit_full_path {
736 return undef unless (@binds);
738 my $func = 'actor.org_unit_full_path(?)';
739 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
741 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
742 $sth->execute(@binds);
744 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
748 __PACKAGE__->register_method(
749 api_name => 'open-ils.storage.actor.org_unit.full_path',
752 method => 'org_unit_full_path',
755 sub org_unit_ancestors {
760 return undef unless ($id);
762 my $func = 'actor.org_unit_ancestors(?)';
764 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
767 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
768 ORDER BY t.depth, f.name;
770 $sth->execute(''.$id);
772 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
776 __PACKAGE__->register_method(
777 api_name => 'open-ils.storage.actor.org_unit.ancestors',
780 method => 'org_unit_ancestors',
783 sub org_unit_descendants {
789 return undef unless ($id);
791 my $func = 'actor.org_unit_descendants(?)';
792 if (defined $depth) {
793 $func = 'actor.org_unit_descendants(?,?)';
796 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
797 $sth->execute(''.$id, ''.$depth) if (defined $depth);
798 $sth->execute(''.$id) unless (defined $depth);
800 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
804 __PACKAGE__->register_method(
805 api_name => 'open-ils.storage.actor.org_unit.descendants',
808 method => 'org_unit_descendants',
811 sub fleshed_actor_stat_cat {
816 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
819 my $cat = actor::stat_cat->retrieve($sc);
822 my $sc_fm = $cat->to_fieldmapper;
823 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
825 $client->respond( $sc_fm );
831 __PACKAGE__->register_method(
832 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
835 method => 'fleshed_actor_stat_cat',
838 __PACKAGE__->register_method(
839 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
843 method => 'fleshed_actor_stat_cat',
846 #XXX Fix stored proc calls
847 sub ranged_actor_stat_cat_all {
852 return undef unless ($ou);
853 my $s_table = actor::stat_cat->table;
855 my $select = <<" SQL";
858 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
863 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
865 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
868 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
869 my $sc_fm = $sc->to_fieldmapper;
871 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
873 $client->respond( $sc_fm );
878 __PACKAGE__->register_method(
879 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
883 method => 'ranged_actor_stat_cat_all',
886 __PACKAGE__->register_method(
887 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
891 method => 'ranged_actor_stat_cat_all',
894 #XXX Fix stored proc calls
895 sub ranged_actor_stat_cat_entry {
901 return undef unless ($ou);
902 my $s_table = actor::stat_cat_entry->table;
904 my $select = <<" SQL";
907 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
912 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
913 $sth->execute($ou,$sc);
915 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
916 $client->respond( $sce->to_fieldmapper );
921 __PACKAGE__->register_method(
922 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
925 method => 'ranged_actor_stat_cat_entry',