1 package OpenILS::Application::Actor;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
7 use Digest::MD5 qw(md5_hex);
9 use OpenSRF::EX qw(:try);
13 use OpenILS::Application::AppUtils;
14 use OpenILS::Utils::Fieldmapper;
15 use OpenILS::Application::Search::Actor;
16 use OpenILS::Utils::ModsParser;
17 use OpenSRF::Utils::Logger;
18 my $logger = "OpenSRF::Utils::Logger";
20 use OpenSRF::Utils::Cache;
23 use OpenILS::Application::Actor::Container;
25 OpenILS::Application::Actor::Container->initialize();
28 my $apputils = "OpenILS::Application::AppUtils";
30 sub _d { warn "Patron:\n" . Dumper(shift()); }
35 my $set_user_settings;
38 __PACKAGE__->register_method(
39 method => "set_user_settings",
40 api_name => "open-ils.actor.patron.settings.update",
42 sub set_user_settings {
43 my( $self, $client, $user_session, $uid, $settings ) = @_;
45 my( $staff, $user, $evt ) =
46 $apputils->checkses_requestor( $user_session, $uid, 'UPDATE_USER' );
49 return $apputils->simple_scalar_request(
51 'open-ils.storage.direct.actor.user_setting.batch.merge',
52 map { [{ usr => $user->id, name => $_}, {value => $$settings{$_}}] } keys %$settings );
57 __PACKAGE__->register_method(
58 method => "set_ou_settings",
59 api_name => "open-ils.actor.org_unit.settings.update",
62 my( $self, $client, $user_session, $ouid, $settings ) = @_;
64 my( $staff, $evt ) = $apputils->checkses( $user_session );
66 $evt = $apputils->check_perms( $staff->id, $ouid, 'UPDATE_ORG_UNIT' );
71 map { [{ org_unit => $ouid, name => $_}, {value => $$settings{$_}}] } keys %$settings;
74 $logger->debug("Updating org unit [$ouid] settings with: " . Dumper($params));
76 return $apputils->simplereq(
78 'open-ils.storage.direct.actor.org_unit_setting.merge', @$params );
82 my $fetch_user_settings;
83 my $fetch_ou_settings;
85 __PACKAGE__->register_method(
86 method => "user_settings",
87 api_name => "open-ils.actor.patron.settings.retrieve",
90 my( $self, $client, $user_session, $uid ) = @_;
92 my( $staff, $user, $evt ) =
93 $apputils->checkses_requestor( $user_session, $uid, 'VIEW_USER' );
96 $logger->debug("User " . $staff->id . " fetching user $uid\n");
97 my $s = $apputils->simplereq(
99 'open-ils.storage.direct.actor.user_setting.search.usr.atomic',$uid );
101 return { map { ($_->name,$_->value) } @$s };
106 __PACKAGE__->register_method(
107 method => "ou_settings",
108 api_name => "open-ils.actor.org_unit.settings.retrieve",
111 my( $self, $client, $ouid ) = @_;
113 my $s = $apputils->simplereq(
115 'open-ils.storage.direct.actor.org_unit_setting.search.org_unit.atomic', $ouid);
117 return { map { ($_->name,$_->value) } @$s };
122 __PACKAGE__->register_method(
123 method => "update_patron",
124 api_name => "open-ils.actor.patron.update",);
127 my( $self, $client, $user_session, $patron ) = @_;
129 my $session = $apputils->start_db_session();
132 warn $user_session . " " . $patron . "\n";
136 OpenILS::Application::AppUtils->check_user_session(
137 $user_session ); #throws EX on error
139 # XXX does this user have permission to add/create users. Granularity?
140 # $new_patron is the patron in progress. $patron is the original patron
141 # passed in with the method. new_patron will change as the components
142 # of patron are added/updated.
146 if(ref($patron->card)) { $patron->card( $patron->card->id ); }
147 if(ref($patron->billing_address)) { $patron->billing_address( $patron->billing_address->id ); }
148 if(ref($patron->mailing_address)) { $patron->mailing_address( $patron->mailing_address->id ); }
150 # create/update the patron first so we can use his id
151 if($patron->isnew()) {
153 $new_patron = _add_patron($session, _clone_patron($patron), $user_obj);
155 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
156 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
157 $client->respond_complete($new_patron->ex);
161 } else { $new_patron = $patron; }
163 $new_patron = _add_update_addresses($session, $patron, $new_patron, $user_obj);
165 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
166 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
167 $client->respond_complete($new_patron->ex);
171 $new_patron = _add_update_cards($session, $patron, $new_patron, $user_obj);
173 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
174 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
175 $client->respond_complete($new_patron->ex);
179 $new_patron = _add_survey_responses($session, $patron, $new_patron, $user_obj);
180 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
181 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
182 $client->respond_complete($new_patron->ex);
187 # re-update the patron if anything has happened to him during this process
188 if($new_patron->ischanged()) {
189 $new_patron = _update_patron($session, $new_patron, $user_obj);
191 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
192 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
193 $client->respond_complete($new_patron->ex);
198 $apputils->commit_db_session($session);
200 $session = OpenSRF::AppSession->create("open-ils.storage");
201 $new_patron = _create_stat_maps($session, $user_session, $patron, $new_patron, $user_obj);
202 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
203 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
204 $client->respond_complete($new_patron->ex);
209 warn "Patron Update/Create complete\n";
210 return flesh_user($new_patron->id());
216 __PACKAGE__->register_method(
217 method => "user_retrieve_fleshed_by_id",
218 api_name => "open-ils.actor.user.fleshed.retrieve",);
220 sub user_retrieve_fleshed_by_id {
221 my( $self, $client, $user_session, $user_id ) = @_;
222 my $user_obj = $apputils->check_user_session( $user_session );
224 if(!defined($user_id)) { $user_id = $user_obj->id; }
226 if( $user_obj->id ne $user_id ) {
227 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER")) {
228 return OpenILS::Perm->new("VIEW_USER");
232 return flesh_user($user_id);
243 $session = OpenSRF::AppSession->create("open-ils.storage");
247 # grab the user with the given id
248 my $ureq = $session->request(
249 "open-ils.storage.direct.actor.user.retrieve", $id);
250 my $user = $ureq->gather(1);
252 if(!$user) { return undef; }
255 my $cards_req = $session->request(
256 "open-ils.storage.direct.actor.card.search.usr.atomic",
258 $user->cards( $cards_req->gather(1) );
260 for my $c(@{$user->cards}) {
261 if($c->id == $user->card || $c->id eq $user->card ) {
262 warn "Setting my card to " . $c->id . "\n";
267 my $add_req = $session->request(
268 "open-ils.storage.direct.actor.user_address.search.usr.atomic",
270 $user->addresses( $add_req->gather(1) );
272 for my $c(@{$user->addresses}) {
273 if($c->id == $user->billing_address || $c->id eq $user->billing_address ) {
274 warn "Setting my address to " . $c->id . "\n";
275 $user->billing_address($c);
279 for my $c(@{$user->addresses}) {
280 if($c->id == $user->mailing_address || $c->id eq $user->mailing_address ) {
281 warn "Setting my address to " . $c->id . "\n";
282 $user->mailing_address($c);
286 my $stat_req = $session->request(
287 "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr.atomic",
289 $user->stat_cat_entries($stat_req->gather(1));
291 if($kill) { $session->disconnect(); }
292 $user->clear_passwd();
300 # clone and clear stuff that would break the database
304 my $new_patron = $patron->clone;
306 # Using the Fieldmapper clone method
307 #my $new_patron = Fieldmapper::actor::user->new();
309 #my $fmap = $Fieldmapper::fieldmap;
310 #no strict; # shallow clone, may be useful in the fieldmapper
312 # (keys %{$fmap->{"Fieldmapper::actor::user"}->{'fields'}}) {
313 # $new_patron->$field( $patron->$field() );
318 $new_patron->clear_billing_address();
319 $new_patron->clear_mailing_address();
320 $new_patron->clear_addresses();
321 $new_patron->clear_card();
322 $new_patron->clear_cards();
323 $new_patron->clear_id();
324 $new_patron->clear_isnew();
325 $new_patron->clear_ischanged();
326 $new_patron->clear_isdeleted();
327 $new_patron->clear_stat_cat_entries();
336 my $user_obj = shift;
339 if($apputils->check_user_perms(
340 $user_obj->id, $user_obj->home_ou, "CREATE_USER")) {
341 return OpenILS::Perm->new("CREATE_USER");
344 warn "Creating new patron\n";
347 my $req = $session->request(
348 "open-ils.storage.direct.actor.user.create",$patron);
349 my $id = $req->gather(1);
351 return OpenILS::EX->new("DUPLICATE_USER_USERNAME");
354 # retrieve the patron from the db to collect defaults
355 my $ureq = $session->request(
356 "open-ils.storage.direct.actor.user.retrieve",
359 warn "Created new patron with id $id\n";
361 return $ureq->gather(1);
366 my( $session, $patron, $user_obj) = @_;
369 if($patron->id ne $user_obj->id) {
370 if($apputils->check_user_perms(
371 $user_obj->id, $user_obj->home_ou, "UPDATE_USER")) {
372 return OpenILS::Perm->new("UPDATE_USER");
376 warn "updating patron " . Dumper($patron) . "\n";
378 my $req = $session->request(
379 "open-ils.storage.direct.actor.user.update",$patron );
380 my $status = $req->gather(1);
381 if(!defined($status)) {
382 throw OpenSRF::EX::ERROR
383 ("Unknown error updating patron");
389 sub _add_update_addresses {
392 my $new_patron = shift;
394 my $current_id; # id of the address before creation
396 for my $address (@{$patron->addresses()}) {
398 $address->usr($new_patron->id());
400 if(ref($address) and $address->isnew()) {
401 warn "Adding new address at street " . $address->street1() . "\n";
403 $current_id = $address->id();
404 $address = _add_address($session,$address);
406 if( $patron->billing_address() and
407 $patron->billing_address() == $current_id ) {
408 $new_patron->billing_address($address->id());
409 $new_patron->ischanged(1);
412 if( $patron->mailing_address() and
413 $patron->mailing_address() == $current_id ) {
414 $new_patron->mailing_address($address->id());
415 $new_patron->ischanged(1);
418 } elsif( ref($address) and $address->ischanged() ) {
419 warn "Updating address at street " . $address->street1();
420 $address->usr($new_patron->id());
421 _update_address($session,$address);
423 } elsif( ref($address) and $address->isdeleted() ) {
424 warn "Deleting address at street " . $address->street1();
426 if( $address->id() == $new_patron->mailing_address() ) {
427 $new_patron->clear_mailing_address();
428 _update_patron($session, $new_patron);
431 if( $address->id() == $new_patron->billing_address() ) {
432 $new_patron->clear_billing_address();
433 _update_patron($session, $new_patron);
436 _delete_address($session,$address);
444 # adds an address to the db and returns the address with new id
446 my($session, $address) = @_;
447 $address->clear_id();
450 warn "Adding Address:\n";
451 warn Dumper($address);
453 # put the address into the database
454 my $req = $session->request(
455 "open-ils.storage.direct.actor.user_address.create",
459 my $id = $req->gather(1);
461 throw OpenSRF::EX::ERROR
462 ("Unable to create new user address");
465 warn "Created address with id $id\n";
467 # update all the necessary id's
473 sub _update_address {
474 my( $session, $address ) = @_;
475 my $req = $session->request(
476 "open-ils.storage.direct.actor.user_address.update",
478 my $status = $req->gather(1);
479 if(!defined($status)) {
480 throw OpenSRF::EX::ERROR
481 ("Unknown error updating address");
488 sub _add_update_cards {
492 my $new_patron = shift;
494 my $virtual_id; #id of the card before creation
495 for my $card (@{$patron->cards()}) {
497 $card->usr($new_patron->id());
499 if(ref($card) and $card->isnew()) {
501 $virtual_id = $card->id();
502 $card = _add_card($session,$card);
503 if(UNIVERSAL::isa($card,"OpenILS::EX")) {
507 #if(ref($patron->card)) { $patron->card($patron->card->id); }
508 if($patron->card() == $virtual_id) {
509 $new_patron->card($card->id());
510 $new_patron->ischanged(1);
513 } elsif( ref($card) and $card->ischanged() ) {
514 $card->usr($new_patron->id());
515 _update_card($session, $card);
522 # adds an card to the db and returns the card with new id
524 my( $session, $card ) = @_;
527 warn "Adding card with barcode " . $card->barcode() . "\n";
528 my $req = $session->request(
529 "open-ils.storage.direct.actor.card.create",
532 my $id = $req->gather(1);
534 return OpenILS::EX->new("DUPLICATE_INVALID_USER_BARCODE");
538 warn "Created patron card with id $id\n";
544 my( $session, $card ) = @_;
547 my $req = $session->request(
548 "open-ils.storage.direct.actor.card.update",
550 my $status = $req->gather(1);
551 if(!defined($status)) {
552 throw OpenSRF::EX::ERROR
553 ("Unknown error updating card");
561 sub _delete_address {
562 my( $session, $address ) = @_;
564 warn "Deleting address " . $address->street1() . "\n";
566 my $req = $session->request(
567 "open-ils.storage.direct.actor.user_address.delete",
569 my $status = $req->gather(1);
570 if(!defined($status)) {
571 throw OpenSRF::EX::ERROR
572 ("Unknown error updating address");
574 warn "Delete address status is $status\n";
579 sub _add_survey_responses {
580 my ($session, $patron, $new_patron) = @_;
582 warn "updating responses for user " . $new_patron->id . "\n";
584 my $responses = $patron->survey_responses;
588 for my $resp( @$responses ) {
589 $resp->usr($new_patron->id);
592 my $status = $apputils->simple_scalar_request(
594 "open-ils.circ.survey.submit.user_id",
603 sub _create_stat_maps {
605 my($session, $user_session, $patron, $new_patron) = @_;
607 my $maps = $patron->stat_cat_entries();
609 for my $map (@$maps) {
611 next unless($map->isnew() || $map->ischanged());
613 my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
615 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
619 $map->target_usr($new_patron->id);
621 warn "Updating stat entry with method $method and session $user_session and map $map\n";
623 my $req = $session->request($method, $map);
624 my $status = $req->gather(1);
629 throw OpenSRF::EX::ERROR
630 ("Error updating stat map with method $method");
640 __PACKAGE__->register_method(
641 method => "search_username",
642 api_name => "open-ils.actor.user.search.username",
645 sub search_username {
646 my($self, $client, $username) = @_;
647 my $users = OpenILS::Application::AppUtils->simple_scalar_request(
649 "open-ils.storage.direct.actor.user.search.usrname.atomic",
657 __PACKAGE__->register_method(
658 method => "user_retrieve_by_barcode",
659 api_name => "open-ils.actor.user.fleshed.retrieve_by_barcode",);
661 sub user_retrieve_by_barcode {
662 my($self, $client, $user_session, $barcode) = @_;
664 $logger->debug("Searching for user with barcode $barcode");
665 #my $user_obj = $apputils->check_user_session( $user_session );
666 my ($user_obj, $evt) = $apputils->check_ses($user_session);
669 my $session = OpenSRF::AppSession->create("open-ils.storage");
671 # find the card with the given barcode
672 my $creq = $session->request(
673 "open-ils.storage.direct.actor.card.search.barcode.atomic",
675 my $card = $creq->gather(1);
677 if(!$card || !$card->[0]) {
678 $session->disconnect();
679 return OpenILS::Event->new( 'USER_NOT_FOUND' );
683 my $user = flesh_user($card->usr(), $session);
684 $session->disconnect();
685 if(!$user) { return OpenILS::Event->new( 'USER_NOT_FOUND' ); }
692 __PACKAGE__->register_method(
693 method => "get_user_by_id",
694 api_name => "open-ils.actor.user.retrieve",);
697 my ($self, $client, $user_session, $id) = @_;
699 my $user_obj = $apputils->check_user_session( $user_session );
701 return $apputils->simple_scalar_request(
703 "open-ils.storage.direct.actor.user.retrieve",
709 __PACKAGE__->register_method(
710 method => "get_org_types",
711 api_name => "open-ils.actor.org_types.retrieve",);
715 my($self, $client) = @_;
717 return $org_types if $org_types;
719 $apputils->simple_scalar_request(
721 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
726 __PACKAGE__->register_method(
727 method => "get_user_profiles",
728 api_name => "open-ils.actor.user.profiles.retrieve",
732 sub get_user_profiles {
733 return $user_profiles if $user_profiles;
735 return $user_profiles =
736 $apputils->simple_scalar_request(
738 "open-ils.storage.direct.actor.profile.retrieve.all.atomic");
743 __PACKAGE__->register_method(
744 method => "get_user_ident_types",
745 api_name => "open-ils.actor.user.ident_types.retrieve",
748 sub get_user_ident_types {
749 return $ident_types if $ident_types;
750 return $ident_types =
751 $apputils->simple_scalar_request(
753 "open-ils.storage.direct.config.identification_type.retrieve.all.atomic" );
759 __PACKAGE__->register_method(
760 method => "get_org_unit",
761 api_name => "open-ils.actor.org_unit.retrieve",
766 my( $self, $client, $user_session, $org_id ) = @_;
768 if(defined($user_session) && !defined($org_id)) {
770 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
771 if(!defined($org_id)) {
772 $org_id = $user_obj->home_ou;
777 my $home_ou = OpenILS::Application::AppUtils->simple_scalar_request(
779 "open-ils.storage.direct.actor.org_unit.retrieve",
788 __PACKAGE__->register_method(
789 method => "get_org_tree",
790 api_name => "open-ils.actor.org_tree.retrieve",
792 note => "Returns the entire org tree structure",
796 my( $self, $client) = @_;
799 $cache_client = OpenSRF::Utils::Cache->new("global", 0);
801 # see if it's in the cache
802 warn "Getting ORG Tree\n";
803 my $tree = $cache_client->get_cache('orgtree');
805 warn "Found orgtree in cache. returning...\n";
809 my $orglist = $apputils->simple_scalar_request(
811 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
814 warn "found org list\n";
817 $tree = $self->build_org_tree($orglist);
818 $cache_client->put_cache('orgtree', $tree);
824 # turns an org list into an org tree
827 my( $self, $orglist) = @_;
829 return $orglist unless (
830 ref($orglist) and @$orglist > 1 );
833 $a->ou_type <=> $b->ou_type ||
834 $a->name cmp $b->name } @$orglist;
836 for my $org (@list) {
838 next unless ($org and defined($org->parent_ou));
839 my ($parent) = grep { $_->id == $org->parent_ou } @list;
842 $parent->children([]) unless defined($parent->children);
843 push( @{$parent->children}, $org );
851 __PACKAGE__->register_method(
852 method => "get_org_descendants",
853 api_name => "open-ils.actor.org_tree.descendants.retrieve"
856 # depth is optional. org_unit is the id
857 sub get_org_descendants {
858 my( $self, $client, $org_unit, $depth ) = @_;
859 my $orglist = $apputils->simple_scalar_request(
861 "open-ils.storage.actor.org_unit.descendants.atomic",
863 return $self->build_org_tree($orglist);
867 __PACKAGE__->register_method(
868 method => "get_org_ancestors",
869 api_name => "open-ils.actor.org_tree.ancestors.retrieve"
872 # depth is optional. org_unit is the id
873 sub get_org_ancestors {
874 my( $self, $client, $org_unit, $depth ) = @_;
875 my $orglist = $apputils->simple_scalar_request(
877 "open-ils.storage.actor.org_unit.ancestors.atomic",
879 return $self->build_org_tree($orglist);
883 __PACKAGE__->register_method(
884 method => "get_standings",
885 api_name => "open-ils.actor.standings.retrieve"
890 return $user_standings if $user_standings;
891 return $user_standings =
892 $apputils->simple_scalar_request(
894 "open-ils.storage.direct.config.standing.retrieve.all.atomic" );
899 __PACKAGE__->register_method(
900 method => "get_my_org_path",
901 api_name => "open-ils.actor.org_unit.full_path.retrieve"
904 sub get_my_org_path {
905 my( $self, $client, $user_session, $org_id ) = @_;
906 my $user_obj = $apputils->check_user_session($user_session);
907 if(!defined($org_id)) { $org_id = $user_obj->home_ou; }
909 return $apputils->simple_scalar_request(
911 "open-ils.storage.actor.org_unit.full_path.atomic",
916 __PACKAGE__->register_method(
917 method => "patron_adv_search",
918 api_name => "open-ils.actor.patron.search.advanced" );
920 sub patron_adv_search {
921 my( $self, $client, $staff_login, $search_hash ) = @_;
924 warn "patron adv with $staff_login and search " .
925 Dumper($search_hash) . "\n";
927 my $session = OpenSRF::AppSession->create("open-ils.storage");
928 my $req = $session->request(
929 "open-ils.storage.actor.user.crazy_search", $search_hash);
931 my $ans = $req->gather(1);
933 my %hash = map { ($_ =>1) } @$ans;
934 $ans = [ keys %hash ];
936 warn "Returning @$ans\n";
938 $session->disconnect();
945 sub _verify_password {
946 my($user_session, $password) = @_;
947 my $user_obj = $apputils->check_user_session($user_session);
949 #grab the user with password
950 $user_obj = $apputils->simple_scalar_request(
952 "open-ils.storage.direct.actor.user.retrieve",
955 if($user_obj->passwd eq $password) {
963 __PACKAGE__->register_method(
964 method => "update_password",
965 api_name => "open-ils.actor.user.password.update");
967 __PACKAGE__->register_method(
968 method => "update_password",
969 api_name => "open-ils.actor.user.username.update");
971 __PACKAGE__->register_method(
972 method => "update_password",
973 api_name => "open-ils.actor.user.email.update");
975 sub update_password {
976 my( $self, $client, $user_session, $new_value, $current_password ) = @_;
978 warn "Updating user with method " .$self->api_name . "\n";
979 my $user_obj = $apputils->check_user_session($user_session);
981 if($self->api_name =~ /password/) {
983 #make sure they know the current password
984 if(!_verify_password($user_session, md5_hex($current_password))) {
985 return OpenILS::EX->new("USER_WRONG_PASSWORD")->ex;
988 $user_obj->passwd($new_value);
990 elsif($self->api_name =~ /username/) {
991 $user_obj->usrname($new_value);
994 elsif($self->api_name =~ /email/) {
995 warn "Updating email to $new_value\n";
996 $user_obj->email($new_value);
999 my $session = $apputils->start_db_session();
1000 $user_obj = _update_patron($session, $user_obj, $user_obj);
1001 $apputils->commit_db_session($session);
1003 if($user_obj) { return 1; }
1008 __PACKAGE__->register_method(
1009 method => "check_user_perms",
1010 api_name => "open-ils.actor.user.perm.check",
1011 notes => <<" NOTES");
1012 Takes a login session, user id, an org id, and an array of perm type strings. For each
1013 perm type, if the user does *not* have the given permission it is added
1014 to a list which is returned from the method. If all permissions
1015 are allowed, an empty list is returned
1016 if the logged in user does not match 'user_id', then the logged in user must
1017 have VIEW_PERMISSION priveleges.
1020 sub check_user_perms {
1021 my( $self, $client, $login_session, $user_id, $org_id, $perm_types ) = @_;
1023 my( $staff, $evt ) = $apputils->checkses($login_session);
1024 return $evt if $evt;
1026 if($staff->id ne $user_id) {
1027 if( my $evt = $apputils->check_perms(
1028 $staff->id, $org_id, 'VIEW_PERMISSION') ) {
1034 for my $perm (@$perm_types) {
1035 if($apputils->check_user_perms($user_id, $org_id, $perm)) {
1036 push @not_allowed, $perm;
1040 return \@not_allowed
1045 __PACKAGE__->register_method(
1046 method => "user_fines_summary",
1047 api_name => "open-ils.actor.user.fines.summary",
1048 notes => <<" NOTES");
1049 Returns a short summary of the users total open fines, excluding voided fines
1050 Params are login_session, user_id
1051 Returns a 'mous' object.
1054 sub user_fines_summary {
1055 my( $self, $client, $login_session, $user_id ) = @_;
1057 my $user_obj = $apputils->check_user_session($login_session);
1058 if($user_obj->id ne $user_id) {
1059 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_FINES_SUMMARY")) {
1060 return OpenILS::Perm->new("VIEW_USER_FINES_SUMMARY");
1064 return $apputils->simple_scalar_request(
1066 "open-ils.storage.direct.money.open_user_summary.search.usr",
1074 __PACKAGE__->register_method(
1075 method => "user_transactions",
1076 api_name => "open-ils.actor.user.transactions",
1077 notes => <<" NOTES");
1078 Returns a list of open user transactions (mbts objects);
1079 Params are login_session, user_id
1080 Optional third parameter is the transactions type. defaults to all
1083 __PACKAGE__->register_method(
1084 method => "user_transactions",
1085 api_name => "open-ils.actor.user.transactions.have_charge",
1086 notes => <<" NOTES");
1087 Returns a list of all open user transactions (mbts objects) that have an initial charge
1088 Params are login_session, user_id
1089 Optional third parameter is the transactions type. defaults to all
1092 __PACKAGE__->register_method(
1093 method => "user_transactions",
1094 api_name => "open-ils.actor.user.transactions.have_balance",
1095 notes => <<" NOTES");
1096 Returns a list of all open user transactions (mbts objects) that have a balance
1097 Params are login_session, user_id
1098 Optional third parameter is the transactions type. defaults to all
1101 __PACKAGE__->register_method(
1102 method => "user_transactions",
1103 api_name => "open-ils.actor.user.transactions.fleshed",
1104 notes => <<" NOTES");
1105 Returns an object/hash of transaction, circ, title where transaction = an open
1106 user transactions (mbts objects), circ is the attached circluation, and title
1107 is the title the circ points to
1108 Params are login_session, user_id
1109 Optional third parameter is the transactions type. defaults to all
1112 __PACKAGE__->register_method(
1113 method => "user_transactions",
1114 api_name => "open-ils.actor.user.transactions.have_charge.fleshed",
1115 notes => <<" NOTES");
1116 Returns an object/hash of transaction, circ, title where transaction = an open
1117 user transactions that has an initial charge (mbts objects), circ is the
1118 attached circluation, and title is the title the circ points to
1119 Params are login_session, user_id
1120 Optional third parameter is the transactions type. defaults to all
1123 __PACKAGE__->register_method(
1124 method => "user_transactions",
1125 api_name => "open-ils.actor.user.transactions.have_balance.fleshed",
1126 notes => <<" NOTES");
1127 Returns an object/hash of transaction, circ, title where transaction = an open
1128 user transaction that has a balance (mbts objects), circ is the attached
1129 circluation, and title is the title the circ points to
1130 Params are login_session, user_id
1131 Optional third parameter is the transaction type. defaults to all
1136 sub user_transactions {
1137 my( $self, $client, $login_session, $user_id, $type ) = @_;
1139 my( $user_obj, $target, $evt ) = $apputils->checkses_requestor(
1140 $login_session, $user_id, 'VIEW_USER_TRANSACTIONS' );
1141 return $evt if $evt;
1143 my $api = $self->api_name();
1147 if(defined($type)) { @xact = (xact_type => $type);
1149 } else { @xact = (); }
1151 if($api =~ /have_charge/) {
1153 $trans = $apputils->simple_scalar_request(
1155 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1156 { usr => $user_id, total_owed => { ">" => 0 }, @xact });
1158 } elsif($api =~ /have_balance/) {
1160 $trans = $apputils->simple_scalar_request(
1162 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1163 { usr => $user_id, balance_owed => { ">" => 0 }, @xact });
1167 $trans = $apputils->simple_scalar_request(
1169 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1170 { usr => $user_id, @xact });
1173 if($api !~ /fleshed/) { return $trans; }
1178 for my $t (@$trans) {
1182 my $circ = $apputils->simple_scalar_request(
1184 "open-ils.storage.direct.action.circulation.retrieve",
1189 my $title = $apputils->simple_scalar_request(
1191 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
1192 $circ->target_copy );
1196 my $u = OpenILS::Utils::ModsParser->new();
1197 $u->start_mods_batch($title->marc());
1198 my $mods = $u->finish_mods_batch();
1200 push @resp, {transaction => $t, circ => $circ, record => $mods };
1210 __PACKAGE__->register_method(
1211 method => "retrieve_groups",
1212 api_name => "open-ils.actor.groups.retrieve",
1213 notes => <<" NOTES");
1214 Returns a list of user groups
1216 sub retrieve_groups {
1217 my( $self, $client ) = @_;
1218 return $apputils->simple_scalar_request(
1220 "open-ils.storage.direct.permission.grp_tree.retrieve.all.atomic");
1274 some old methods that may be good to keep around for now
1277 my( $session, $card ) = @_;
1279 warn "Deleting card with barcode " . $card->barcode() . "\n";
1280 my $req = $session->request(
1281 "open-ils.storage.direct.actor.card.delete",
1283 my $status = $req->gather(1);
1284 if(!defined($status)) {
1285 throw OpenSRF::EX::ERROR
1286 ("Unknown error updating card");
1292 # deletes the patron and any attached addresses and cards
1293 __PACKAGE__->register_method(
1294 method => "delete_patron",
1295 api_name => "open-ils.actor.patron.delete",
1300 my( $self, $client, $patron ) = @_;
1301 my $session = $apputils->start_db_session();
1306 $patron->clear_mailing_address();
1307 $patron->clear_billing_address();
1308 $patron->ischanged(1);
1310 _update_patron($session, $patron);
1311 _delete_address($session,$_) for (@{$patron->addresses()});
1312 _delete_card($session,$_) for (@{$patron->cards()});
1313 _delete_patron($session,$patron);
1314 $apputils->commit_db_session($session);
1316 } catch Error with {
1318 $err = "-*- Failure deleting user: $e";
1319 $apputils->rollback_db_session($session);
1323 if($err) { throw OpenSRF::EX::ERROR ($err); }
1324 warn "Patron Delete complete\n";
1328 sub _delete_patron {
1329 my( $session, $patron ) = @_;
1331 warn "Deleting patron " . $patron->usrname() . "\n";
1333 my $req = $session->request(
1334 "open-ils.storage.direct.actor.user.delete",
1336 my $status = $req->gather(1);
1337 if(!defined($status)) {
1338 throw OpenSRF::EX::ERROR
1339 ("Unknown error updating patron");