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 my $sth = actor::org_unit::closed_date->db_Main->prepare( $sql );
194 $sth->execute($date, $ou);
196 $date = clense_ISO8601($date);
199 while (my $closure = $sth->fetchrow_hashref) {
200 $begin ||= $closure->{close_start};
201 $end = $closure->{close_end};
203 my $before = $_dt_parser->parse_datetime( clense_ISO8601($begin) );
204 $before->subtract( seconds => 1 );
205 my $after = $_dt_parser->parse_datetime( clense_ISO8601($end) );
206 $after->add( seconds => 1 );
208 if ( $direction <= 0 ) {
209 while ( my $_b = org_closed_overlap($self, $client, $ou, $before->iso8601, -1, 1 ) ) {
210 $before = $_dt_parser->parse_datetime( clense_ISO8601($_b->{start}) );
214 if ( $direction >= 0 ) {
215 while ( my $_a = org_closed_overlap($self, $client, $ou, $after->iso8601, 1, 1 ) ) {
216 $after = $_dt_parser->parse_datetime( clense_ISO8601($_a->{end}) );
220 $begin = clense_ISO8601($before->iso8601);
221 $end = clense_ISO8601($after->iso8601);
229 if ( my $hoo = actor::org_unit::hours_of_operation->retrieve($ou) ) {
231 my $begin_dow = $_dt_parser->parse_datetime( $begin )->day_of_week_0;
232 my $begin_open_meth = "dow_".$begin_dow."_open";
233 my $begin_close_meth = "dow_".$begin_dow."_close";
236 while ($hoo->$begin_open_meth eq '00:00:00' and $hoo->$begin_close_meth eq '00:00:00') {
237 $begin = clense_ISO8601($_dt_parser->parse_datetime( $begin )->subtract( days => 1)->iso8601);
241 last if ($count > 6);
242 $begin_open_meth = "dow_".$begin_dow."_open";
243 $begin_close_meth = "dow_".$begin_dow."_close";
246 my $end_dow = $_dt_parser->parse_datetime( $end )->day_of_week_0;
247 my $end_open_meth = "dow_".$end_dow."_open";
248 my $end_close_meth = "dow_".$end_dow."_close";
251 while ($hoo->$end_open_meth eq '00:00:00' and $hoo->$end_close_meth eq '00:00:00') {
252 $end = clense_ISO8601($_dt_parser->parse_datetime( $end )->add( days => 1)->iso8601);
256 last if ($count > 6);
257 $end_open_meth = "dow_".$end_dow."_open";
258 $end_close_meth = "dow_".$end_dow."_close";
263 if ($begin eq $date && $end eq $date) {
267 return { start => $begin, end => $end };
269 __PACKAGE__->register_method(
270 api_name => 'open-ils.storage.actor.org_unit.closed_date.overlap',
272 method => 'org_closed_overlap',
275 sub user_by_barcode {
278 my @barcodes = shift;
280 return undef unless @barcodes;
282 for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
284 if (@barcodes == 1) {
285 return $card->usr->to_fieldmapper;
287 $client->respond( $card->usr->to_fieldmapper);
291 __PACKAGE__->register_method(
292 api_name => 'open-ils.storage.direct.actor.user.search.barcode',
294 method => 'user_by_barcode',
303 my $c = actor::card->table;
304 my $p = actor::user->table;
306 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
308 my $list = actor::user->db_Main->selectcol_arrayref($sql);
309 for my $bc ( @$list ) {
310 $client->respond($bc);
314 __PACKAGE__->register_method(
315 api_name => 'open-ils.storage.actor.user.lost_barcodes',
318 method => 'lost_barcodes',
319 signature => <<' NOTE',
320 Returns an array of barcodes that belong to lost cards.
321 @return array of barcodes
325 sub expired_barcodes {
329 my $c = actor::card->table;
330 my $p = actor::user->table;
332 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
334 my $list = actor::user->db_Main->selectcol_arrayref($sql);
335 for my $bc ( @$list ) {
336 $client->respond($bc);
340 __PACKAGE__->register_method(
341 api_name => 'open-ils.storage.actor.user.expired_barcodes',
344 method => 'expired_barcodes',
345 signature => <<' NOTE',
346 Returns an array of barcodes that are currently expired.
347 @return array of barcodes
351 sub barred_barcodes {
355 my $c = actor::card->table;
356 my $p = actor::user->table;
358 my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
360 my $list = actor::user->db_Main->selectcol_arrayref($sql);
361 for my $bc ( @$list ) {
362 $client->respond($bc);
366 __PACKAGE__->register_method(
367 api_name => 'open-ils.storage.actor.user.barred_barcodes',
370 method => 'barred_barcodes',
371 signature => <<' NOTE',
372 Returns an array of barcodes that are currently barred.
373 @return array of barcodes
377 sub penalized_barcodes {
382 my $c = actor::card->table;
383 my $p = actor::user_standing_penalty->table;
385 my $sql = "SELECT c.barcode FROM $c c JOIN $p p USING (usr)";
388 $sql .= ' WHERE penalty_type NOT IN ('. join(',', map { '?' } @ignore) . ')';
391 $sql .= ' GROUP BY c.barcode;';
393 my $list = actor::user->db_Main->selectcol_arrayref($sql, {}, @ignore);
394 for my $bc ( @$list ) {
395 $client->respond($bc);
399 __PACKAGE__->register_method(
400 api_name => 'open-ils.storage.actor.user.penalized_barcodes',
403 method => 'penalized_barcodes',
404 signature => <<' NOTE',
405 Returns an array of barcodes that have penalties not listed
406 as a parameter. Supply a list of any penalty types that should
407 not stop a patron from checking out materials.
409 @param ignore_list Penalty type to ignore
410 @return array of barcodes
419 my $limit = shift || 1000;
421 my $inactive = shift;
422 $sort = ['family_name','first_given_name'] unless ($$sort[0]);
426 # group 2 = phone, ident
428 my $usr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
429 my @usrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
431 my $addr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
432 my @addrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
434 my $pv = $$search{phone}{value};
435 my $iv = $$search{ident}{value};
436 my $nv = $$search{name}{value};
442 for my $p ( qw/day_phone evening_phone other_phone/ ) {
443 push @ps, "LOWER($p) ~ ?";
444 push @phonev, "^$pv";
446 $phone = '(' . join(' OR ', @ps) . ')';
453 for my $i ( qw/ident_value ident_value2/ ) {
454 push @is, "LOWER($i) ~ ?";
455 push @identv, "^$iv";
457 $ident = '(' . join(' OR ', @is) . ')';
464 for my $n ( qw/first_given_name second_given_name family_name/ ) {
465 push @ns, "LOWER($i) ~ ?";
468 $name = '(' . join(' OR ', @ns) . ')';
471 my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
472 my $addr_where = $addr;
475 my $u_table = actor::user->table;
476 my $a_table = actor::user_address->table;
478 my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
479 my $a_select = "SELECT usr as id FROM $a_table a WHERE $addr_where";
480 my $clone_select = '';
481 $clone_select = "JOIN (SELECT cu.id as id FROM $a_table ca ".
482 "JOIN $u_table cu ON (cu.mailing_address = ca.id OR cu.billing_address = ca.id) ".
483 "WHERE $addr_where) AS clone USING (id)" if ($addr_where);
488 $select = "$u_select INTERSECT $a_select";
492 } elsif ($addr_where) {
493 $select = "$a_select";
498 my $order_by = join ', ', map { 'users.'. $_} @$sort;
503 $inactive = 'AND users.active = TRUE';
508 FROM $u_table AS users
509 JOIN ($select) AS search
512 WHERE users.deleted = FALSE $inactive
517 return actor::user->db_Main->selectcol_arrayref($select, {}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv,@addrv));
519 __PACKAGE__->register_method(
520 api_name => 'open-ils.storage.actor.user.crazy_search',
522 method => 'patron_search',
525 =comment not gonna use it...
530 my $searches = shift;
532 return undef unless (defined $searches);
534 for my $usr ( actor::user->search( $searches ) ) {
536 $client->respond( flesh_user( $usr ) );
540 __PACKAGE__->register_method(
541 api_name => 'open-ils.storage.fleshed.actor.user.search',
543 method => 'fleshed_search',
548 sub fleshed_search_like {
551 my $searches = shift;
553 return undef unless (defined $searches);
555 for my $usr ( actor::user->search_like( $searches ) ) {
557 $client->respond( flesh_user( $usr ) );
561 __PACKAGE__->register_method(
562 api_name => 'open-ils.storage.fleshed.actor.user.search_like',
564 method => 'user_by_barcode',
569 sub retrieve_fleshed_user {
574 return undef unless @ids;
576 @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
578 $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
582 __PACKAGE__->register_method(
583 api_name => 'open-ils.storage.fleshed.actor.user.retrieve',
585 method => 'retrieve_fleshed_user',
588 __PACKAGE__->register_method(
589 api_name => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
591 method => 'retrieve_fleshed_user',
600 my $standing = $usr->standing;
601 my $profile = $usr->profile;
602 my $ident_type = $usr->ident_type;
604 my $maddress = $usr->mailing_address;
605 my $baddress = $usr->billing_address;
606 my $card = $usr->card;
608 my @addresses = $usr->addresses;
609 my @cards = $usr->cards;
611 my $usr_fm = $usr->to_fieldmapper;
612 $usr_fm->standing( $standing->to_fieldmapper );
613 $usr_fm->profile( $profile->to_fieldmapper );
614 $usr_fm->ident_type( $ident_type->to_fieldmapper );
616 $usr_fm->card( $card->to_fieldmapper );
617 $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
618 $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
620 $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
621 $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
632 my $select =<<" SQL";
635 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
638 my $sth = actor::org_unit->db_Main->prepare_cached($select);
641 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
645 __PACKAGE__->register_method(
646 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
649 method => 'org_unit_list',
652 sub org_unit_type_list {
656 my $select =<<" SQL";
658 FROM actor.org_unit_type
659 ORDER BY depth, name;
662 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
665 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
669 __PACKAGE__->register_method(
670 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
673 method => 'org_unit_type_list',
676 sub org_unit_full_path {
681 return undef unless (@binds);
683 my $func = 'actor.org_unit_full_path(?)';
684 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
686 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
687 $sth->execute(@binds);
689 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
693 __PACKAGE__->register_method(
694 api_name => 'open-ils.storage.actor.org_unit.full_path',
697 method => 'org_unit_full_path',
700 sub org_unit_ancestors {
705 return undef unless ($id);
707 my $func = 'actor.org_unit_ancestors(?)';
709 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
712 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
713 ORDER BY t.depth, f.name;
715 $sth->execute(''.$id);
717 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
721 __PACKAGE__->register_method(
722 api_name => 'open-ils.storage.actor.org_unit.ancestors',
725 method => 'org_unit_ancestors',
728 sub org_unit_descendants {
734 return undef unless ($id);
736 my $func = 'actor.org_unit_descendants(?)';
737 if (defined $depth) {
738 $func = 'actor.org_unit_descendants(?,?)';
741 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
742 $sth->execute(''.$id, ''.$depth) if (defined $depth);
743 $sth->execute(''.$id) unless (defined $depth);
745 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
749 __PACKAGE__->register_method(
750 api_name => 'open-ils.storage.actor.org_unit.descendants',
753 method => 'org_unit_descendants',
756 sub fleshed_actor_stat_cat {
761 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
764 my $cat = actor::stat_cat->retrieve($sc);
767 my $sc_fm = $cat->to_fieldmapper;
768 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
770 $client->respond( $sc_fm );
776 __PACKAGE__->register_method(
777 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
780 method => 'fleshed_actor_stat_cat',
783 __PACKAGE__->register_method(
784 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
788 method => 'fleshed_actor_stat_cat',
791 #XXX Fix stored proc calls
792 sub ranged_actor_stat_cat_all {
797 return undef unless ($ou);
798 my $s_table = actor::stat_cat->table;
800 my $select = <<" SQL";
803 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
808 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
810 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
813 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
814 my $sc_fm = $sc->to_fieldmapper;
816 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
818 $client->respond( $sc_fm );
823 __PACKAGE__->register_method(
824 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
828 method => 'ranged_actor_stat_cat_all',
831 __PACKAGE__->register_method(
832 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
836 method => 'ranged_actor_stat_cat_all',
839 #XXX Fix stored proc calls
840 sub ranged_actor_stat_cat_entry {
846 return undef unless ($ou);
847 my $s_table = actor::stat_cat_entry->table;
849 my $select = <<" SQL";
852 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
857 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
858 $sth->execute($ou,$sc);
860 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
861 $client->respond( $sce->to_fieldmapper );
866 __PACKAGE__->register_method(
867 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
870 method => 'ranged_actor_stat_cat_entry',