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;
9 use DateTime::Format::ISO8601;
12 my $_dt_parser = DateTime::Format::ISO8601->new;
14 my $log = 'OpenSRF::Utils::Logger';
16 sub new_usergroup_id {
17 return actor::user->db_Main->selectrow_array("select nextval('actor.usr_usrgroup_seq'::regclass)");
19 __PACKAGE__->register_method(
20 api_name => 'open-ils.storage.actor.user.group_id.new',
22 method => 'new_usergroup_id',
32 SUM(COALESCE((SELECT SUM(b.amount) FROM money.billing b WHERE b.voided IS FALSE AND b.xact = x.id),0.0)) -
33 SUM(COALESCE((SELECT SUM(p.amount) FROM money.payment p WHERE p.voided IS FALSE AND p.xact = x.id),0.0))
34 FROM money.billable_xact x
35 WHERE x.usr = ? AND x.xact_finish IS NULL
39 my (undef,$val) = actor::user->db_Main->selectrow_array($sql, {}, $usr);
43 __PACKAGE__->register_method(
44 api_name => 'open-ils.storage.actor.user.total_owed',
46 method => 'usr_total_owed',
49 sub usr_breakdown_out {
54 my $out_sql = <<" SQL";
56 FROM action.circulation
57 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 my $out = actor::user->db_Main->selectcol_arrayref($out_sql, {}, $usr);
62 my $od_sql = <<" SQL";
64 FROM action.circulation
65 WHERE usr = ? AND checkin_time IS NULL AND due_date < 'today' AND (stop_fines IS NULL OR stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE'))
68 my $od = actor::user->db_Main->selectcol_arrayref($od_sql, {}, $usr);
70 my $lost_sql = <<" SQL";
72 FROM action.circulation
73 WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'LOST'
76 my $lost = actor::user->db_Main->selectcol_arrayref($lost_sql, {}, $usr);
78 my $cl_sql = <<" SQL";
80 FROM action.circulation
81 WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'CLAIMSRETURNED'
84 my $cl = actor::user->db_Main->selectcol_arrayref($cl_sql, {}, $usr);
86 my $lo_sql = <<" SQL";
88 FROM action.circulation
89 WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'LONGOVERDUE'
92 my $lo = actor::user->db_Main->selectcol_arrayref($lo_sql, {}, $usr);
94 if ($self->api_name =~/count$/o) {
95 return { total => scalar(@$out) + scalar(@$od) + scalar(@$lost) + scalar(@$cl) + scalar(@$lo),
97 overdue => scalar(@$od),
98 lost => scalar(@$lost),
99 claims_returned => scalar(@$cl),
100 long_overdue => scalar(@$lo),
104 return { out => $out,
107 claims_returned => $cl,
111 __PACKAGE__->register_method(
112 api_name => 'open-ils.storage.actor.user.checked_out',
114 method => 'usr_breakdown_out',
116 __PACKAGE__->register_method(
117 api_name => 'open-ils.storage.actor.user.checked_out.count',
119 method => 'usr_breakdown_out',
129 FROM action.circulation
130 WHERE usr = ? AND checkin_time IS NULL
133 my ($val) = actor::user->db_Main->selectrow_array($sql, {}, $usr);
137 __PACKAGE__->register_method(
138 api_name => 'open-ils.storage.actor.user.total_out',
140 method => 'usr_total_out',
147 local $OpenILS::Application::Storage::WRITE = 1;
149 my $delete_sql = <<" SQL";
150 DELETE FROM actor.org_unit_proximity;
153 my $insert_sql = <<" SQL";
154 INSERT INTO actor.org_unit_proximity (from_org, to_org, prox)
157 actor.org_unit_proximity(l.id,r.id)
158 FROM actor.org_unit l,
162 actor::org_unit_proximity->db_Main->do($delete_sql);
163 actor::org_unit_proximity->db_Main->do($insert_sql);
167 __PACKAGE__->register_method(
168 api_name => 'open-ils.storage.actor.org_unit.refresh_proximity',
170 method => 'calc_proximity',
174 sub org_closed_overlap {
179 my $direction = shift || 0;
180 my $no_hoo = shift || 0;
182 return undef unless ($date && $ou);
184 my $t = actor::org_unit::closed_date->table;
188 WHERE ? between close_start and close_end
190 ORDER BY close_start ASC, close_end DESC
194 $date = clense_ISO8601($date);
195 my ($begin, $end) = ($date,$date);
197 my $hoo = actor::org_unit::hours_of_operation->retrieve($ou);
199 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $date, $ou )) {
200 $begin = clense_ISO8601($closure->{close_start});
201 $end = clense_ISO8601($closure->{close_end});
203 if ( $direction <= 0 ) {
204 $before = $_dt_parser->parse_datetime( $begin );
205 $before->subtract( minutes => 1 );
207 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->iso8601, -1, 1 ) ) {
208 $before = $_dt_parser->parse_datetime( clense_ISO8601($_b->{start}) );
210 $begin = clense_ISO8601($before->iso8601);
213 if ( $direction >= 0 ) {
214 $after = $_dt_parser->parse_datetime( $end );
215 $after->add( minutes => 1 );
217 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->iso8601, 1, 1 ) ) {
218 $after = $_dt_parser->parse_datetime( clense_ISO8601($_a->{end}) );
220 $end = clense_ISO8601($after->iso8601);
227 my $begin_dow = $_dt_parser->parse_datetime( $begin )->day_of_week_0;
228 my $begin_open_meth = "dow_".$begin_dow."_open";
229 my $begin_close_meth = "dow_".$begin_dow."_close";
232 while ($hoo->$begin_open_meth eq '00:00:00' and $hoo->$begin_close_meth eq '00:00:00') {
233 $begin = clense_ISO8601($_dt_parser->parse_datetime( $begin )->subtract( days => 1)->iso8601);
237 last if ($count > 6);
238 $begin_open_meth = "dow_".$begin_dow."_open";
239 $begin_close_meth = "dow_".$begin_dow."_close";
242 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $begin, $ou )) {
243 $before = $_dt_parser->parse_datetime( $begin );
244 $before->subtract( minutes => 1 );
245 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->iso8601, -1 ) ) {
246 $before = $_dt_parser->parse_datetime( clense_ISO8601($_b->{start}) );
250 my $end_dow = $_dt_parser->parse_datetime( $end )->day_of_week_0;
251 my $end_open_meth = "dow_".$end_dow."_open";
252 my $end_close_meth = "dow_".$end_dow."_close";
255 while ($hoo->$end_open_meth eq '00:00:00' and $hoo->$end_close_meth eq '00:00:00') {
256 $end = clense_ISO8601($_dt_parser->parse_datetime( $end )->add( days => 1)->iso8601);
260 last if ($count > 6);
261 $end_open_meth = "dow_".$end_dow."_open";
262 $end_close_meth = "dow_".$end_dow."_close";
265 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $end, $ou )) {
266 $after = $_dt_parser->parse_datetime( $end );
267 $after->add( minutes => 1 );
269 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->iso8601, 1 ) ) {
270 $after = $_dt_parser->parse_datetime( clense_ISO8601($_a->{end}) );
272 $end = clense_ISO8601($after->iso8601);
278 if ($begin eq $date && $end eq $date) {
282 return { start => $begin, end => $end };
284 __PACKAGE__->register_method(
285 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
287 method => 'org_closed_overlap',
290 sub user_by_barcode {
293 my @barcodes = shift;
295 return undef unless @barcodes;
297 for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
299 if (@barcodes == 1) {
300 return $card->usr->to_fieldmapper;
302 $client->respond( $card->usr->to_fieldmapper);
306 __PACKAGE__->register_method(
307 api_name => 'open-ils.storage.direct.actor.user.search.barcode',
309 method => 'user_by_barcode',
318 my $c = actor::card->table;
319 my $p = actor::user->table;
321 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
323 my $list = actor::user->db_Main->selectcol_arrayref($sql);
324 for my $bc ( @$list ) {
325 $client->respond($bc);
329 __PACKAGE__->register_method(
330 api_name => 'open-ils.storage.actor.user.lost_barcodes',
333 method => 'lost_barcodes',
334 signature => <<' NOTE',
335 Returns an array of barcodes that belong to lost cards.
336 @return array of barcodes
340 sub expired_barcodes {
344 my $c = actor::card->table;
345 my $p = actor::user->table;
347 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
349 my $list = actor::user->db_Main->selectcol_arrayref($sql);
350 for my $bc ( @$list ) {
351 $client->respond($bc);
355 __PACKAGE__->register_method(
356 api_name => 'open-ils.storage.actor.user.expired_barcodes',
359 method => 'expired_barcodes',
360 signature => <<' NOTE',
361 Returns an array of barcodes that are currently expired.
362 @return array of barcodes
366 sub barred_barcodes {
370 my $c = actor::card->table;
371 my $p = actor::user->table;
373 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
375 my $list = actor::user->db_Main->selectcol_arrayref($sql);
376 for my $bc ( @$list ) {
377 $client->respond($bc);
381 __PACKAGE__->register_method(
382 api_name => 'open-ils.storage.actor.user.barred_barcodes',
385 method => 'barred_barcodes',
386 signature => <<' NOTE',
387 Returns an array of barcodes that are currently barred.
388 @return array of barcodes
392 sub penalized_barcodes {
397 my $c = actor::card->table;
398 my $p = actor::user_standing_penalty->table;
400 my $sql = "SELECT c.barcode FROM $c c JOIN $p p USING (usr)";
403 $sql .= ' WHERE penalty_type NOT IN ('. join(',', map { '?' } @ignore) . ')';
406 $sql .= ' GROUP BY c.barcode;';
408 my $list = actor::user->db_Main->selectcol_arrayref($sql, {}, @ignore);
409 for my $bc ( @$list ) {
410 $client->respond($bc);
414 __PACKAGE__->register_method(
415 api_name => 'open-ils.storage.actor.user.penalized_barcodes',
418 method => 'penalized_barcodes',
419 signature => <<' NOTE',
420 Returns an array of barcodes that have penalties not listed
421 as a parameter. Supply a list of any penalty types that should
422 not stop a patron from checking out materials.
424 @param ignore_list Penalty type to ignore
425 @return array of barcodes
434 my $limit = shift || 1000;
436 my $inactive = shift;
438 $sort = ['family_name','first_given_name'] unless ($$sort[0]);
443 # group 2 = phone, ident
445 my $usr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
446 my @usrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
448 my $addr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
449 my @addrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
451 my $pv = $$search{phone}{value};
452 my $iv = $$search{ident}{value};
453 my $nv = $$search{name}{value};
459 for my $p ( qw/day_phone evening_phone other_phone/ ) {
460 push @ps, "LOWER($p) ~ ?";
461 push @phonev, "^$pv";
463 $phone = '(' . join(' OR ', @ps) . ')';
470 for my $i ( qw/ident_value ident_value2/ ) {
471 push @is, "LOWER($i) ~ ?";
472 push @identv, "^$iv";
474 $ident = '(' . join(' OR ', @is) . ')';
481 for my $n ( qw/first_given_name second_given_name family_name/ ) {
482 push @ns, "LOWER($i) ~ ?";
485 $name = '(' . join(' OR ', @ns) . ')';
488 my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
489 my $addr_where = $addr;
492 my $u_table = actor::user->table;
493 my $a_table = actor::user_address->table;
495 my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
496 my $a_select = "SELECT usr as id FROM $a_table a WHERE $addr_where";
497 my $clone_select = '';
498 $clone_select = "JOIN (SELECT cu.id as id FROM $a_table ca ".
499 "JOIN $u_table cu ON (cu.mailing_address = ca.id OR cu.billing_address = ca.id) ".
500 "WHERE $addr_where) AS clone USING (id)" if ($addr_where);
505 $select = "$u_select INTERSECT $a_select";
509 } elsif ($addr_where) {
510 $select = "$a_select";
515 my $order_by = join ', ', map { 'LOWER(users.'. (split / /,$_)[0] . ') ' . (split / /,$_)[1] } @$sort;
516 my $distinct_list = join ', ', map { 'LOWER(users.'. (split / /,$_)[0] . ')' } @$sort;
521 $inactive = 'AND users.active = TRUE';
525 SELECT DISTINCT $distinct_list
526 FROM $u_table AS users
527 JOIN ($select) AS search
530 WHERE users.deleted = FALSE $inactive
535 return actor::user->db_Main->selectcol_arrayref($select, {Columns=>[scalar(@$sort)]}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv,@addrv));
537 __PACKAGE__->register_method(
538 api_name => 'open-ils.storage.actor.user.crazy_search',
540 method => 'patron_search',
543 =comment not gonna use it...
548 my $searches = shift;
550 return undef unless (defined $searches);
552 for my $usr ( actor::user->search( $searches ) ) {
554 $client->respond( flesh_user( $usr ) );
558 __PACKAGE__->register_method(
559 api_name => 'open-ils.storage.fleshed.actor.user.search',
561 method => 'fleshed_search',
566 sub fleshed_search_like {
569 my $searches = shift;
571 return undef unless (defined $searches);
573 for my $usr ( actor::user->search_like( $searches ) ) {
575 $client->respond( flesh_user( $usr ) );
579 __PACKAGE__->register_method(
580 api_name => 'open-ils.storage.fleshed.actor.user.search_like',
582 method => 'user_by_barcode',
587 sub retrieve_fleshed_user {
592 return undef unless @ids;
594 @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
596 $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
600 __PACKAGE__->register_method(
601 api_name => 'open-ils.storage.fleshed.actor.user.retrieve',
603 method => 'retrieve_fleshed_user',
606 __PACKAGE__->register_method(
607 api_name => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
609 method => 'retrieve_fleshed_user',
618 my $standing = $usr->standing;
619 my $profile = $usr->profile;
620 my $ident_type = $usr->ident_type;
622 my $maddress = $usr->mailing_address;
623 my $baddress = $usr->billing_address;
624 my $card = $usr->card;
626 my @addresses = $usr->addresses;
627 my @cards = $usr->cards;
629 my $usr_fm = $usr->to_fieldmapper;
630 $usr_fm->standing( $standing->to_fieldmapper );
631 $usr_fm->profile( $profile->to_fieldmapper );
632 $usr_fm->ident_type( $ident_type->to_fieldmapper );
634 $usr_fm->card( $card->to_fieldmapper );
635 $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
636 $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
638 $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
639 $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
650 my $select =<<" SQL";
653 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
656 my $sth = actor::org_unit->db_Main->prepare_cached($select);
659 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
663 __PACKAGE__->register_method(
664 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
667 method => 'org_unit_list',
670 sub org_unit_type_list {
674 my $select =<<" SQL";
676 FROM actor.org_unit_type
677 ORDER BY depth, name;
680 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
683 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
687 __PACKAGE__->register_method(
688 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
691 method => 'org_unit_type_list',
694 sub org_unit_full_path {
699 return undef unless (@binds);
701 my $func = 'actor.org_unit_full_path(?)';
702 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
704 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
705 $sth->execute(@binds);
707 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
711 __PACKAGE__->register_method(
712 api_name => 'open-ils.storage.actor.org_unit.full_path',
715 method => 'org_unit_full_path',
718 sub org_unit_ancestors {
723 return undef unless ($id);
725 my $func = 'actor.org_unit_ancestors(?)';
727 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
730 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
731 ORDER BY t.depth, f.name;
733 $sth->execute(''.$id);
735 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
739 __PACKAGE__->register_method(
740 api_name => 'open-ils.storage.actor.org_unit.ancestors',
743 method => 'org_unit_ancestors',
746 sub org_unit_descendants {
752 return undef unless ($id);
754 my $func = 'actor.org_unit_descendants(?)';
755 if (defined $depth) {
756 $func = 'actor.org_unit_descendants(?,?)';
759 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
760 $sth->execute(''.$id, ''.$depth) if (defined $depth);
761 $sth->execute(''.$id) unless (defined $depth);
763 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
767 __PACKAGE__->register_method(
768 api_name => 'open-ils.storage.actor.org_unit.descendants',
771 method => 'org_unit_descendants',
774 sub fleshed_actor_stat_cat {
779 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
782 my $cat = actor::stat_cat->retrieve($sc);
785 my $sc_fm = $cat->to_fieldmapper;
786 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
788 $client->respond( $sc_fm );
794 __PACKAGE__->register_method(
795 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
798 method => 'fleshed_actor_stat_cat',
801 __PACKAGE__->register_method(
802 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
806 method => 'fleshed_actor_stat_cat',
809 #XXX Fix stored proc calls
810 sub ranged_actor_stat_cat_all {
815 return undef unless ($ou);
816 my $s_table = actor::stat_cat->table;
818 my $select = <<" SQL";
821 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
826 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
828 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
831 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
832 my $sc_fm = $sc->to_fieldmapper;
834 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
836 $client->respond( $sc_fm );
841 __PACKAGE__->register_method(
842 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
846 method => 'ranged_actor_stat_cat_all',
849 __PACKAGE__->register_method(
850 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
854 method => 'ranged_actor_stat_cat_all',
857 #XXX Fix stored proc calls
858 sub ranged_actor_stat_cat_entry {
864 return undef unless ($ou);
865 my $s_table = actor::stat_cat_entry->table;
867 my $select = <<" SQL";
870 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
875 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
876 $sth->execute($ou,$sc);
878 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
879 $client->respond( $sce->to_fieldmapper );
884 __PACKAGE__->register_method(
885 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
888 method => 'ranged_actor_stat_cat_entry',