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 xact_finish 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 xact_finish 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 if ( $direction <= 0 ) {
228 my $begin_dow = $_dt_parser->parse_datetime( $begin )->day_of_week_0;
229 my $begin_open_meth = "dow_".$begin_dow."_open";
230 my $begin_close_meth = "dow_".$begin_dow."_close";
233 while ($hoo->$begin_open_meth eq '00:00:00' and $hoo->$begin_close_meth eq '00:00:00') {
234 $begin = clense_ISO8601($_dt_parser->parse_datetime( $begin )->subtract( days => 1)->iso8601);
238 last if ($count > 6);
239 $begin_open_meth = "dow_".$begin_dow."_open";
240 $begin_close_meth = "dow_".$begin_dow."_close";
243 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $begin, $ou )) {
244 $before = $_dt_parser->parse_datetime( $begin );
245 $before->subtract( minutes => 1 );
246 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->iso8601, -1 ) ) {
247 $before = $_dt_parser->parse_datetime( clense_ISO8601($_b->{start}) );
252 if ( $direction >= 0 ) {
253 my $end_dow = $_dt_parser->parse_datetime( $end )->day_of_week_0;
254 my $end_open_meth = "dow_".$end_dow."_open";
255 my $end_close_meth = "dow_".$end_dow."_close";
258 while ($hoo->$end_open_meth eq '00:00:00' and $hoo->$end_close_meth eq '00:00:00') {
259 $end = clense_ISO8601($_dt_parser->parse_datetime( $end )->add( days => 1)->iso8601);
263 last if ($count > 6);
264 $end_open_meth = "dow_".$end_dow."_open";
265 $end_close_meth = "dow_".$end_dow."_close";
268 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $end, $ou )) {
269 $after = $_dt_parser->parse_datetime( $end );
270 $after->add( minutes => 1 );
272 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->iso8601, 1 ) ) {
273 $after = $_dt_parser->parse_datetime( clense_ISO8601($_a->{end}) );
275 $end = clense_ISO8601($after->iso8601);
282 if ($begin eq $date && $end eq $date) {
286 return { start => $begin, end => $end };
288 __PACKAGE__->register_method(
289 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
291 method => 'org_closed_overlap',
294 sub user_by_barcode {
297 my @barcodes = shift;
299 return undef unless @barcodes;
301 for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
303 if (@barcodes == 1) {
304 return $card->usr->to_fieldmapper;
306 $client->respond( $card->usr->to_fieldmapper);
310 __PACKAGE__->register_method(
311 api_name => 'open-ils.storage.direct.actor.user.search.barcode',
313 method => 'user_by_barcode',
322 my $c = actor::card->table;
323 my $p = actor::user->table;
325 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
327 my $list = actor::user->db_Main->selectcol_arrayref($sql);
328 for my $bc ( @$list ) {
329 $client->respond($bc);
333 __PACKAGE__->register_method(
334 api_name => 'open-ils.storage.actor.user.lost_barcodes',
337 method => 'lost_barcodes',
338 signature => <<' NOTE',
339 Returns an array of barcodes that belong to lost cards.
340 @return array of barcodes
344 sub expired_barcodes {
348 my $c = actor::card->table;
349 my $p = actor::user->table;
351 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
353 my $list = actor::user->db_Main->selectcol_arrayref($sql);
354 for my $bc ( @$list ) {
355 $client->respond($bc);
359 __PACKAGE__->register_method(
360 api_name => 'open-ils.storage.actor.user.expired_barcodes',
363 method => 'expired_barcodes',
364 signature => <<' NOTE',
365 Returns an array of barcodes that are currently expired.
366 @return array of barcodes
370 sub barred_barcodes {
374 my $c = actor::card->table;
375 my $p = actor::user->table;
377 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
379 my $list = actor::user->db_Main->selectcol_arrayref($sql);
380 for my $bc ( @$list ) {
381 $client->respond($bc);
385 __PACKAGE__->register_method(
386 api_name => 'open-ils.storage.actor.user.barred_barcodes',
389 method => 'barred_barcodes',
390 signature => <<' NOTE',
391 Returns an array of barcodes that are currently barred.
392 @return array of barcodes
396 sub penalized_barcodes {
401 my $c = actor::card->table;
402 my $p = actor::user_standing_penalty->table;
404 my $sql = "SELECT c.barcode FROM $c c JOIN $p p USING (usr)";
407 $sql .= ' WHERE penalty_type NOT IN ('. join(',', map { '?' } @ignore) . ')';
410 $sql .= ' GROUP BY c.barcode;';
412 my $list = actor::user->db_Main->selectcol_arrayref($sql, {}, @ignore);
413 for my $bc ( @$list ) {
414 $client->respond($bc);
418 __PACKAGE__->register_method(
419 api_name => 'open-ils.storage.actor.user.penalized_barcodes',
422 method => 'penalized_barcodes',
423 signature => <<' NOTE',
424 Returns an array of barcodes that have penalties not listed
425 as a parameter. Supply a list of any penalty types that should
426 not stop a patron from checking out materials.
428 @param ignore_list Penalty type to ignore
429 @return array of barcodes
438 my $limit = shift || 1000;
440 my $inactive = shift;
442 $sort = ['family_name','first_given_name'] unless ($$sort[0]);
447 # group 2 = phone, ident
449 my $usr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
450 my @usrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
452 my $addr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
453 my @addrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
455 my $pv = $$search{phone}{value};
456 my $iv = $$search{ident}{value};
457 my $nv = $$search{name}{value};
463 for my $p ( qw/day_phone evening_phone other_phone/ ) {
464 push @ps, "LOWER($p) ~ ?";
465 push @phonev, "^$pv";
467 $phone = '(' . join(' OR ', @ps) . ')';
474 for my $i ( qw/ident_value ident_value2/ ) {
475 push @is, "LOWER($i) ~ ?";
476 push @identv, "^$iv";
478 $ident = '(' . join(' OR ', @is) . ')';
485 for my $n ( qw/first_given_name second_given_name family_name/ ) {
486 push @ns, "LOWER($i) ~ ?";
489 $name = '(' . join(' OR ', @ns) . ')';
492 my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
493 my $addr_where = $addr;
496 my $u_table = actor::user->table;
497 my $a_table = actor::user_address->table;
499 my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
500 my $a_select = "SELECT usr as id FROM $a_table a WHERE $addr_where";
501 my $clone_select = '';
502 $clone_select = "JOIN (SELECT cu.id as id FROM $a_table ca ".
503 "JOIN $u_table cu ON (cu.mailing_address = ca.id OR cu.billing_address = ca.id) ".
504 "WHERE $addr_where) AS clone USING (id)" if ($addr_where);
509 $select = "$u_select INTERSECT $a_select";
513 } elsif ($addr_where) {
514 $select = "$a_select";
519 my $order_by = join ', ', map { 'LOWER(users.'. (split / /,$_)[0] . ') ' . (split / /,$_)[1] } @$sort;
520 my $distinct_list = join ', ', map { 'LOWER(users.'. (split / /,$_)[0] . ')' } @$sort;
525 $inactive = 'AND users.active = TRUE';
529 SELECT DISTINCT $distinct_list
530 FROM $u_table AS users
531 JOIN ($select) AS search USING (id)
533 WHERE users.deleted = FALSE $inactive
538 return actor::user->db_Main->selectcol_arrayref($select, {Columns=>[scalar(@$sort)]}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv,@addrv));
540 __PACKAGE__->register_method(
541 api_name => 'open-ils.storage.actor.user.crazy_search',
543 method => 'patron_search',
546 =comment not gonna use it...
551 my $searches = shift;
553 return undef unless (defined $searches);
555 for my $usr ( actor::user->search( $searches ) ) {
557 $client->respond( flesh_user( $usr ) );
561 __PACKAGE__->register_method(
562 api_name => 'open-ils.storage.fleshed.actor.user.search',
564 method => 'fleshed_search',
569 sub fleshed_search_like {
572 my $searches = shift;
574 return undef unless (defined $searches);
576 for my $usr ( actor::user->search_like( $searches ) ) {
578 $client->respond( flesh_user( $usr ) );
582 __PACKAGE__->register_method(
583 api_name => 'open-ils.storage.fleshed.actor.user.search_like',
585 method => 'user_by_barcode',
590 sub retrieve_fleshed_user {
595 return undef unless @ids;
597 @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
599 $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
603 __PACKAGE__->register_method(
604 api_name => 'open-ils.storage.fleshed.actor.user.retrieve',
606 method => 'retrieve_fleshed_user',
609 __PACKAGE__->register_method(
610 api_name => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
612 method => 'retrieve_fleshed_user',
621 my $standing = $usr->standing;
622 my $profile = $usr->profile;
623 my $ident_type = $usr->ident_type;
625 my $maddress = $usr->mailing_address;
626 my $baddress = $usr->billing_address;
627 my $card = $usr->card;
629 my @addresses = $usr->addresses;
630 my @cards = $usr->cards;
632 my $usr_fm = $usr->to_fieldmapper;
633 $usr_fm->standing( $standing->to_fieldmapper );
634 $usr_fm->profile( $profile->to_fieldmapper );
635 $usr_fm->ident_type( $ident_type->to_fieldmapper );
637 $usr_fm->card( $card->to_fieldmapper );
638 $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
639 $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
641 $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
642 $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
653 my $select =<<" SQL";
656 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
659 my $sth = actor::org_unit->db_Main->prepare_cached($select);
662 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
666 __PACKAGE__->register_method(
667 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
670 method => 'org_unit_list',
673 sub org_unit_type_list {
677 my $select =<<" SQL";
679 FROM actor.org_unit_type
680 ORDER BY depth, name;
683 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
686 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
690 __PACKAGE__->register_method(
691 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
694 method => 'org_unit_type_list',
697 sub org_unit_full_path {
702 return undef unless (@binds);
704 my $func = 'actor.org_unit_full_path(?)';
705 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
707 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
708 $sth->execute(@binds);
710 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
714 __PACKAGE__->register_method(
715 api_name => 'open-ils.storage.actor.org_unit.full_path',
718 method => 'org_unit_full_path',
721 sub org_unit_ancestors {
726 return undef unless ($id);
728 my $func = 'actor.org_unit_ancestors(?)';
730 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
733 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
734 ORDER BY t.depth, f.name;
736 $sth->execute(''.$id);
738 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
742 __PACKAGE__->register_method(
743 api_name => 'open-ils.storage.actor.org_unit.ancestors',
746 method => 'org_unit_ancestors',
749 sub org_unit_descendants {
755 return undef unless ($id);
757 my $func = 'actor.org_unit_descendants(?)';
758 if (defined $depth) {
759 $func = 'actor.org_unit_descendants(?,?)';
762 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
763 $sth->execute(''.$id, ''.$depth) if (defined $depth);
764 $sth->execute(''.$id) unless (defined $depth);
766 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
770 __PACKAGE__->register_method(
771 api_name => 'open-ils.storage.actor.org_unit.descendants',
774 method => 'org_unit_descendants',
777 sub fleshed_actor_stat_cat {
782 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
785 my $cat = actor::stat_cat->retrieve($sc);
788 my $sc_fm = $cat->to_fieldmapper;
789 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
791 $client->respond( $sc_fm );
797 __PACKAGE__->register_method(
798 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
801 method => 'fleshed_actor_stat_cat',
804 __PACKAGE__->register_method(
805 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
809 method => 'fleshed_actor_stat_cat',
812 #XXX Fix stored proc calls
813 sub ranged_actor_stat_cat_all {
818 return undef unless ($ou);
819 my $s_table = actor::stat_cat->table;
821 my $select = <<" SQL";
824 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
829 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
831 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
834 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
835 my $sc_fm = $sc->to_fieldmapper;
837 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
839 $client->respond( $sc_fm );
844 __PACKAGE__->register_method(
845 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
849 method => 'ranged_actor_stat_cat_all',
852 __PACKAGE__->register_method(
853 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
857 method => 'ranged_actor_stat_cat_all',
860 #XXX Fix stored proc calls
861 sub ranged_actor_stat_cat_entry {
867 return undef unless ($ou);
868 my $s_table = actor::stat_cat_entry->table;
870 my $select = <<" SQL";
873 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
878 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
879 $sth->execute($ou,$sc);
881 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
882 $client->respond( $sce->to_fieldmapper );
887 __PACKAGE__->register_method(
888 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
891 method => 'ranged_actor_stat_cat_entry',