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
193 $date = clense_ISO8601($date);
195 my $sth = actor::org_unit::closed_date->db_Main->prepare( $sql );
196 $sth->execute($date, $ou);
199 while (my $closure = $sth->fetchrow_hashref) {
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);
230 if ( my $hoo = actor::org_unit::hours_of_operation->retrieve($ou) ) {
232 my $begin_dow = $_dt_parser->parse_datetime( $begin )->day_of_week_0;
233 my $begin_open_meth = "dow_".$begin_dow."_open";
234 my $begin_close_meth = "dow_".$begin_dow."_close";
237 while ($hoo->$begin_open_meth eq '00:00:00' and $hoo->$begin_close_meth eq '00:00:00') {
238 $begin = clense_ISO8601($_dt_parser->parse_datetime( $begin )->subtract( days => 1)->iso8601);
242 last if ($count > 6);
243 $begin_open_meth = "dow_".$begin_dow."_open";
244 $begin_close_meth = "dow_".$begin_dow."_close";
247 my $end_dow = $_dt_parser->parse_datetime( $end )->day_of_week_0;
248 my $end_open_meth = "dow_".$end_dow."_open";
249 my $end_close_meth = "dow_".$end_dow."_close";
252 while ($hoo->$end_open_meth eq '00:00:00' and $hoo->$end_close_meth eq '00:00:00') {
253 $end = clense_ISO8601($_dt_parser->parse_datetime( $end )->add( days => 1)->iso8601);
257 last if ($count > 6);
258 $end_open_meth = "dow_".$end_dow."_open";
259 $end_close_meth = "dow_".$end_dow."_close";
264 if ($begin eq $date && $end eq $date) {
268 return { start => $begin, end => $end };
270 __PACKAGE__->register_method(
271 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
273 method => 'org_closed_overlap',
276 sub user_by_barcode {
279 my @barcodes = shift;
281 return undef unless @barcodes;
283 for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
285 if (@barcodes == 1) {
286 return $card->usr->to_fieldmapper;
288 $client->respond( $card->usr->to_fieldmapper);
292 __PACKAGE__->register_method(
293 api_name => 'open-ils.storage.direct.actor.user.search.barcode',
295 method => 'user_by_barcode',
304 my $c = actor::card->table;
305 my $p = actor::user->table;
307 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
309 my $list = actor::user->db_Main->selectcol_arrayref($sql);
310 for my $bc ( @$list ) {
311 $client->respond($bc);
315 __PACKAGE__->register_method(
316 api_name => 'open-ils.storage.actor.user.lost_barcodes',
319 method => 'lost_barcodes',
320 signature => <<' NOTE',
321 Returns an array of barcodes that belong to lost cards.
322 @return array of barcodes
326 sub expired_barcodes {
330 my $c = actor::card->table;
331 my $p = actor::user->table;
333 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
335 my $list = actor::user->db_Main->selectcol_arrayref($sql);
336 for my $bc ( @$list ) {
337 $client->respond($bc);
341 __PACKAGE__->register_method(
342 api_name => 'open-ils.storage.actor.user.expired_barcodes',
345 method => 'expired_barcodes',
346 signature => <<' NOTE',
347 Returns an array of barcodes that are currently expired.
348 @return array of barcodes
352 sub barred_barcodes {
356 my $c = actor::card->table;
357 my $p = actor::user->table;
359 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
361 my $list = actor::user->db_Main->selectcol_arrayref($sql);
362 for my $bc ( @$list ) {
363 $client->respond($bc);
367 __PACKAGE__->register_method(
368 api_name => 'open-ils.storage.actor.user.barred_barcodes',
371 method => 'barred_barcodes',
372 signature => <<' NOTE',
373 Returns an array of barcodes that are currently barred.
374 @return array of barcodes
378 sub penalized_barcodes {
383 my $c = actor::card->table;
384 my $p = actor::user_standing_penalty->table;
386 my $sql = "SELECT c.barcode FROM $c c JOIN $p p USING (usr)";
389 $sql .= ' WHERE penalty_type NOT IN ('. join(',', map { '?' } @ignore) . ')';
392 $sql .= ' GROUP BY c.barcode;';
394 my $list = actor::user->db_Main->selectcol_arrayref($sql, {}, @ignore);
395 for my $bc ( @$list ) {
396 $client->respond($bc);
400 __PACKAGE__->register_method(
401 api_name => 'open-ils.storage.actor.user.penalized_barcodes',
404 method => 'penalized_barcodes',
405 signature => <<' NOTE',
406 Returns an array of barcodes that have penalties not listed
407 as a parameter. Supply a list of any penalty types that should
408 not stop a patron from checking out materials.
410 @param ignore_list Penalty type to ignore
411 @return array of barcodes
420 my $limit = shift || 1000;
422 my $inactive = shift;
423 $sort = ['family_name','first_given_name'] unless ($$sort[0]);
427 # group 2 = phone, ident
429 my $usr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
430 my @usrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
432 my $addr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
433 my @addrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
435 my $pv = $$search{phone}{value};
436 my $iv = $$search{ident}{value};
437 my $nv = $$search{name}{value};
443 for my $p ( qw/day_phone evening_phone other_phone/ ) {
444 push @ps, "LOWER($p) ~ ?";
445 push @phonev, "^$pv";
447 $phone = '(' . join(' OR ', @ps) . ')';
454 for my $i ( qw/ident_value ident_value2/ ) {
455 push @is, "LOWER($i) ~ ?";
456 push @identv, "^$iv";
458 $ident = '(' . join(' OR ', @is) . ')';
465 for my $n ( qw/first_given_name second_given_name family_name/ ) {
466 push @ns, "LOWER($i) ~ ?";
469 $name = '(' . join(' OR ', @ns) . ')';
472 my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
473 my $addr_where = $addr;
476 my $u_table = actor::user->table;
477 my $a_table = actor::user_address->table;
479 my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
480 my $a_select = "SELECT usr as id FROM $a_table a WHERE $addr_where";
481 my $clone_select = '';
482 $clone_select = "JOIN (SELECT cu.id as id FROM $a_table ca ".
483 "JOIN $u_table cu ON (cu.mailing_address = ca.id OR cu.billing_address = ca.id) ".
484 "WHERE $addr_where) AS clone USING (id)" if ($addr_where);
489 $select = "$u_select INTERSECT $a_select";
493 } elsif ($addr_where) {
494 $select = "$a_select";
499 my $order_by = join ', ', map { 'users.'. $_ } @$sort;
500 my $distinct_list = join ', ', map { 'users.'. (split / /, $_)[0] } @$sort;
505 $inactive = 'AND users.active = TRUE';
509 SELECT DISTINCT $distinct_list, users.id
510 FROM $u_table AS users
511 JOIN ($select) AS search
514 WHERE users.deleted = FALSE $inactive
519 return actor::user->db_Main->selectcol_arrayref($select, {}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv,@addrv));
521 __PACKAGE__->register_method(
522 api_name => 'open-ils.storage.actor.user.crazy_search',
524 method => 'patron_search',
527 =comment not gonna use it...
532 my $searches = shift;
534 return undef unless (defined $searches);
536 for my $usr ( actor::user->search( $searches ) ) {
538 $client->respond( flesh_user( $usr ) );
542 __PACKAGE__->register_method(
543 api_name => 'open-ils.storage.fleshed.actor.user.search',
545 method => 'fleshed_search',
550 sub fleshed_search_like {
553 my $searches = shift;
555 return undef unless (defined $searches);
557 for my $usr ( actor::user->search_like( $searches ) ) {
559 $client->respond( flesh_user( $usr ) );
563 __PACKAGE__->register_method(
564 api_name => 'open-ils.storage.fleshed.actor.user.search_like',
566 method => 'user_by_barcode',
571 sub retrieve_fleshed_user {
576 return undef unless @ids;
578 @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
580 $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
584 __PACKAGE__->register_method(
585 api_name => 'open-ils.storage.fleshed.actor.user.retrieve',
587 method => 'retrieve_fleshed_user',
590 __PACKAGE__->register_method(
591 api_name => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
593 method => 'retrieve_fleshed_user',
602 my $standing = $usr->standing;
603 my $profile = $usr->profile;
604 my $ident_type = $usr->ident_type;
606 my $maddress = $usr->mailing_address;
607 my $baddress = $usr->billing_address;
608 my $card = $usr->card;
610 my @addresses = $usr->addresses;
611 my @cards = $usr->cards;
613 my $usr_fm = $usr->to_fieldmapper;
614 $usr_fm->standing( $standing->to_fieldmapper );
615 $usr_fm->profile( $profile->to_fieldmapper );
616 $usr_fm->ident_type( $ident_type->to_fieldmapper );
618 $usr_fm->card( $card->to_fieldmapper );
619 $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
620 $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
622 $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
623 $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
634 my $select =<<" SQL";
637 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
640 my $sth = actor::org_unit->db_Main->prepare_cached($select);
643 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
647 __PACKAGE__->register_method(
648 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
651 method => 'org_unit_list',
654 sub org_unit_type_list {
658 my $select =<<" SQL";
660 FROM actor.org_unit_type
661 ORDER BY depth, name;
664 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
667 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
671 __PACKAGE__->register_method(
672 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
675 method => 'org_unit_type_list',
678 sub org_unit_full_path {
683 return undef unless (@binds);
685 my $func = 'actor.org_unit_full_path(?)';
686 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
688 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
689 $sth->execute(@binds);
691 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
695 __PACKAGE__->register_method(
696 api_name => 'open-ils.storage.actor.org_unit.full_path',
699 method => 'org_unit_full_path',
702 sub org_unit_ancestors {
707 return undef unless ($id);
709 my $func = 'actor.org_unit_ancestors(?)';
711 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
714 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
715 ORDER BY t.depth, f.name;
717 $sth->execute(''.$id);
719 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
723 __PACKAGE__->register_method(
724 api_name => 'open-ils.storage.actor.org_unit.ancestors',
727 method => 'org_unit_ancestors',
730 sub org_unit_descendants {
736 return undef unless ($id);
738 my $func = 'actor.org_unit_descendants(?)';
739 if (defined $depth) {
740 $func = 'actor.org_unit_descendants(?,?)';
743 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
744 $sth->execute(''.$id, ''.$depth) if (defined $depth);
745 $sth->execute(''.$id) unless (defined $depth);
747 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
751 __PACKAGE__->register_method(
752 api_name => 'open-ils.storage.actor.org_unit.descendants',
755 method => 'org_unit_descendants',
758 sub fleshed_actor_stat_cat {
763 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
766 my $cat = actor::stat_cat->retrieve($sc);
769 my $sc_fm = $cat->to_fieldmapper;
770 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
772 $client->respond( $sc_fm );
778 __PACKAGE__->register_method(
779 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
782 method => 'fleshed_actor_stat_cat',
785 __PACKAGE__->register_method(
786 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
790 method => 'fleshed_actor_stat_cat',
793 #XXX Fix stored proc calls
794 sub ranged_actor_stat_cat_all {
799 return undef unless ($ou);
800 my $s_table = actor::stat_cat->table;
802 my $select = <<" SQL";
805 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
810 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
812 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
815 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
816 my $sc_fm = $sc->to_fieldmapper;
818 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
820 $client->respond( $sc_fm );
825 __PACKAGE__->register_method(
826 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
830 method => 'ranged_actor_stat_cat_all',
833 __PACKAGE__->register_method(
834 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
838 method => 'ranged_actor_stat_cat_all',
841 #XXX Fix stored proc calls
842 sub ranged_actor_stat_cat_entry {
848 return undef unless ($ou);
849 my $s_table = actor::stat_cat_entry->table;
851 my $select = <<" SQL";
854 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
859 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
860 $sth->execute($ou,$sc);
862 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
863 $client->respond( $sce->to_fieldmapper );
868 __PACKAGE__->register_method(
869 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
872 method => 'ranged_actor_stat_cat_entry',