1 package OpenILS::Application::Actor;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
6 use Digest::MD5 qw(md5_hex);
8 use OpenSRF::EX qw(:try);
12 use OpenILS::Application::AppUtils;
13 use OpenILS::Utils::Fieldmapper;
14 use OpenILS::Application::Search::Actor;
15 use OpenILS::Utils::ModsParser;
17 use OpenSRF::Utils::Cache;
19 my $apputils = "OpenILS::Application::AppUtils";
21 sub _d { warn "Patron:\n" . Dumper(shift()); }
26 __PACKAGE__->register_method(
27 method => "update_patron",
28 api_name => "open-ils.actor.patron.update",);
31 my( $self, $client, $user_session, $patron ) = @_;
33 my $session = $apputils->start_db_session();
36 warn $user_session . " " . $patron . "\n";
40 OpenILS::Application::AppUtils->check_user_session(
41 $user_session ); #throws EX on error
43 # XXX does this user have permission to add/create users. Granularity?
44 # $new_patron is the patron in progress. $patron is the original patron
45 # passed in with the method. new_patron will change as the components
46 # of patron are added/updated.
50 if(ref($patron->card)) { $patron->card( $patron->card->id ); }
51 if(ref($patron->billing_address)) { $patron->billing_address( $patron->billing_address->id ); }
52 if(ref($patron->mailing_address)) { $patron->mailing_address( $patron->mailing_address->id ); }
54 # create/update the patron first so we can use his id
55 if($patron->isnew()) {
57 $new_patron = _add_patron($session, _clone_patron($patron), $user_obj);
59 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
60 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
61 $client->respond_complete($new_patron->ex);
65 } else { $new_patron = $patron; }
67 $new_patron = _add_update_addresses($session, $patron, $new_patron, $user_obj);
69 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
70 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
71 $client->respond_complete($new_patron->ex);
75 $new_patron = _add_update_cards($session, $patron, $new_patron, $user_obj);
77 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
78 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
79 $client->respond_complete($new_patron->ex);
83 $new_patron = _add_survey_responses($session, $patron, $new_patron, $user_obj);
84 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
85 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
86 $client->respond_complete($new_patron->ex);
91 # re-update the patron if anything has happened to him during this process
92 if($new_patron->ischanged()) {
93 $new_patron = _update_patron($session, $new_patron, $user_obj);
95 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
96 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
97 $client->respond_complete($new_patron->ex);
102 $apputils->commit_db_session($session);
104 $session = OpenSRF::AppSession->create("open-ils.storage");
105 $new_patron = _create_stat_maps($session, $user_session, $patron, $new_patron, $user_obj);
106 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
107 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
108 $client->respond_complete($new_patron->ex);
113 warn "Patron Update/Create complete\n";
114 return flesh_user($new_patron->id());
120 __PACKAGE__->register_method(
121 method => "user_retrieve_fleshed_by_id",
122 api_name => "open-ils.actor.user.fleshed.retrieve",);
124 sub user_retrieve_fleshed_by_id {
125 my( $self, $client, $user_session, $user_id ) = @_;
126 my $user_obj = $apputils->check_user_session( $user_session );
128 if( $user_obj->id ne $user_id ) {
129 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER")) {
130 return OpenILS::Perm->new("VIEW_USER");
134 return flesh_user($user_id);
145 $session = OpenSRF::AppSession->create("open-ils.storage");
149 # grab the user with the given id
150 my $ureq = $session->request(
151 "open-ils.storage.direct.actor.user.retrieve", $id);
152 my $user = $ureq->gather(1);
154 if(!$user) { return undef; }
157 my $cards_req = $session->request(
158 "open-ils.storage.direct.actor.card.search.usr.atomic",
160 $user->cards( $cards_req->gather(1) );
162 for my $c(@{$user->cards}) {
163 if($c->id == $user->card || $c->id eq $user->card ) {
164 warn "Setting my card to " . $c->id . "\n";
169 my $add_req = $session->request(
170 "open-ils.storage.direct.actor.user_address.search.usr.atomic",
172 $user->addresses( $add_req->gather(1) );
174 for my $c(@{$user->addresses}) {
175 if($c->id == $user->billing_address || $c->id eq $user->billing_address ) {
176 warn "Setting my address to " . $c->id . "\n";
177 $user->billing_address($c);
181 for my $c(@{$user->addresses}) {
182 if($c->id == $user->mailing_address || $c->id eq $user->mailing_address ) {
183 warn "Setting my address to " . $c->id . "\n";
184 $user->mailing_address($c);
188 my $stat_req = $session->request(
189 "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr.atomic",
191 $user->stat_cat_entries($stat_req->gather(1));
193 if($kill) { $session->disconnect(); }
194 $user->clear_passwd();
202 # clone and clear stuff that would break the database
206 my $new_patron = Fieldmapper::actor::user->new();
208 my $fmap = $Fieldmapper::fieldmap;
209 no strict; # shallow clone, may be useful in the fieldmapper
211 (keys %{$fmap->{"Fieldmapper::actor::user"}->{'fields'}}) {
212 $new_patron->$field( $patron->$field() );
217 $new_patron->clear_billing_address();
218 $new_patron->clear_mailing_address();
219 $new_patron->clear_addresses();
220 $new_patron->clear_card();
221 $new_patron->clear_cards();
222 $new_patron->clear_id();
223 $new_patron->clear_isnew();
224 $new_patron->clear_ischanged();
225 $new_patron->clear_isdeleted();
226 $new_patron->clear_stat_cat_entries();
235 my $user_obj = shift;
238 if($apputils->check_user_perms(
239 $user_obj->id, $user_obj->home_ou, "CREATE_USER")) {
240 return OpenILS::Perm->new("CREATE_USER");
243 warn "Creating new patron\n";
246 my $req = $session->request(
247 "open-ils.storage.direct.actor.user.create",$patron);
248 my $id = $req->gather(1);
250 return OpenILS::EX->new("DUPLICATE_USER_USERNAME");
253 # retrieve the patron from the db to collect defaults
254 my $ureq = $session->request(
255 "open-ils.storage.direct.actor.user.retrieve",
258 warn "Created new patron with id $id\n";
260 return $ureq->gather(1);
265 my( $session, $patron, $user_obj) = @_;
268 if($apputils->check_user_perms(
269 $user_obj->id, $user_obj->home_ou, "UPDATE_USER")) {
270 return OpenILS::Perm->new("UPDATE_USER");
273 warn "updating patron " . Dumper($patron) . "\n";
275 my $req = $session->request(
276 "open-ils.storage.direct.actor.user.update",$patron );
277 my $status = $req->gather(1);
278 if(!defined($status)) {
279 throw OpenSRF::EX::ERROR
280 ("Unknown error updating patron");
286 sub _add_update_addresses {
289 my $new_patron = shift;
291 my $current_id; # id of the address before creation
293 for my $address (@{$patron->addresses()}) {
295 $address->usr($new_patron->id());
297 if(ref($address) and $address->isnew()) {
298 warn "Adding new address at street " . $address->street1() . "\n";
300 $current_id = $address->id();
301 $address = _add_address($session,$address);
303 if( $patron->billing_address() and
304 $patron->billing_address() == $current_id ) {
305 $new_patron->billing_address($address->id());
306 $new_patron->ischanged(1);
309 if( $patron->mailing_address() and
310 $patron->mailing_address() == $current_id ) {
311 $new_patron->mailing_address($address->id());
312 $new_patron->ischanged(1);
315 } elsif( ref($address) and $address->ischanged() ) {
316 warn "Updating address at street " . $address->street1();
317 $address->usr($new_patron->id());
318 _update_address($session,$address);
320 } elsif( ref($address) and $address->isdeleted() ) {
321 warn "Deleting address at street " . $address->street1();
323 if( $address->id() == $new_patron->mailing_address() ) {
324 $new_patron->clear_mailing_address();
325 _update_patron($session, $new_patron);
328 if( $address->id() == $new_patron->billing_address() ) {
329 $new_patron->clear_billing_address();
330 _update_patron($session, $new_patron);
333 _delete_address($session,$address);
341 # adds an address to the db and returns the address with new id
343 my($session, $address) = @_;
344 $address->clear_id();
346 # put the address into the database
347 my $req = $session->request(
348 "open-ils.storage.direct.actor.user_address.create",
352 my $id = $req->gather(1);
354 throw OpenSRF::EX::ERROR
355 ("Unable to create new user address");
358 warn "Created address with id $id\n";
360 # update all the necessary id's
366 sub _update_address {
367 my( $session, $address ) = @_;
368 my $req = $session->request(
369 "open-ils.storage.direct.actor.user_address.update",
371 my $status = $req->gather(1);
372 if(!defined($status)) {
373 throw OpenSRF::EX::ERROR
374 ("Unknown error updating address");
381 sub _add_update_cards {
385 my $new_patron = shift;
387 my $virtual_id; #id of the card before creation
388 for my $card (@{$patron->cards()}) {
390 $card->usr($new_patron->id());
392 if(ref($card) and $card->isnew()) {
394 $virtual_id = $card->id();
395 $card = _add_card($session,$card);
396 if(UNIVERSAL::isa($card,"OpenILS::EX")) {
400 #if(ref($patron->card)) { $patron->card($patron->card->id); }
401 if($patron->card() == $virtual_id) {
402 $new_patron->card($card->id());
403 $new_patron->ischanged(1);
406 } elsif( ref($card) and $card->ischanged() ) {
407 $card->usr($new_patron->id());
408 _update_card($session, $card);
415 # adds an card to the db and returns the card with new id
417 my( $session, $card ) = @_;
420 warn "Adding card with barcode " . $card->barcode() . "\n";
421 my $req = $session->request(
422 "open-ils.storage.direct.actor.card.create",
425 my $id = $req->gather(1);
427 return OpenILS::EX->new("DUPLICATE_INVALID_USER_BARCODE");
431 warn "Created patron card with id $id\n";
437 my( $session, $card ) = @_;
440 my $req = $session->request(
441 "open-ils.storage.direct.actor.card.update",
443 my $status = $req->gather(1);
444 if(!defined($status)) {
445 throw OpenSRF::EX::ERROR
446 ("Unknown error updating card");
454 sub _delete_address {
455 my( $session, $address ) = @_;
457 warn "Deleting address " . $address->street1() . "\n";
459 my $req = $session->request(
460 "open-ils.storage.direct.actor.user_address.delete",
462 my $status = $req->gather(1);
463 if(!defined($status)) {
464 throw OpenSRF::EX::ERROR
465 ("Unknown error updating address");
467 warn "Delete address status is $status\n";
472 sub _add_survey_responses {
473 my ($session, $patron, $new_patron) = @_;
475 warn "updating responses for user " . $new_patron->id . "\n";
477 my $responses = $patron->survey_responses;
481 for my $resp( @$responses ) {
482 $resp->usr($new_patron->id);
485 my $status = $apputils->simple_scalar_request(
487 "open-ils.circ.survey.submit.user_id",
496 sub _create_stat_maps {
498 my($session, $user_session, $patron, $new_patron) = @_;
500 my $maps = $patron->stat_cat_entries();
502 for my $map (@$maps) {
504 next unless($map->isnew() || $map->ischanged());
506 my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
508 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
512 $map->target_usr($new_patron->id);
514 warn "Updating stat entry with method $method and session $user_session and map $map\n";
516 my $req = $session->request($method, $map);
517 my $status = $req->gather(1);
522 throw OpenSRF::EX::ERROR
523 ("Error updating stat map with method $method");
533 __PACKAGE__->register_method(
534 method => "search_username",
535 api_name => "open-ils.actor.user.search.username",
538 sub search_username {
539 my($self, $client, $username) = @_;
540 my $users = OpenILS::Application::AppUtils->simple_scalar_request(
542 "open-ils.storage.direct.actor.user.search.usrname.atomic",
550 __PACKAGE__->register_method(
551 method => "user_retrieve_by_barcode",
552 api_name => "open-ils.actor.user.fleshed.retrieve_by_barcode",);
554 sub user_retrieve_by_barcode {
555 my($self, $client, $user_session, $barcode) = @_;
556 warn "Searching for user with barcode $barcode\n";
557 my $user_obj = $apputils->check_user_session( $user_session );
559 my $session = OpenSRF::AppSession->create("open-ils.storage");
561 # find the card with the given barcode
562 my $creq = $session->request(
563 "open-ils.storage.direct.actor.card.search.barcode.atomic",
565 my $card = $creq->gather(1);
567 if(!$card || !$card->[0]) {
568 $session->disconnect();
573 my $user = flesh_user($card->usr(), $session);
574 $session->disconnect();
581 __PACKAGE__->register_method(
582 method => "get_user_by_id",
583 api_name => "open-ils.actor.user.retrieve",);
586 my ($self, $client, $user_session, $id) = @_;
588 my $user_obj = $apputils->check_user_session( $user_session );
590 return $apputils->simple_scalar_request(
592 "open-ils.storage.direct.actor.user.retrieve",
598 __PACKAGE__->register_method(
599 method => "get_org_types",
600 api_name => "open-ils.actor.org_types.retrieve",);
604 my($self, $client) = @_;
606 return $org_types if $org_types;
608 $apputils->simple_scalar_request(
610 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
615 __PACKAGE__->register_method(
616 method => "get_user_profiles",
617 api_name => "open-ils.actor.user.profiles.retrieve",
621 sub get_user_profiles {
622 return $user_profiles if $user_profiles;
624 return $user_profiles =
625 $apputils->simple_scalar_request(
627 "open-ils.storage.direct.actor.profile.retrieve.all.atomic");
632 __PACKAGE__->register_method(
633 method => "get_user_ident_types",
634 api_name => "open-ils.actor.user.ident_types.retrieve",
637 sub get_user_ident_types {
638 return $ident_types if $ident_types;
639 return $ident_types =
640 $apputils->simple_scalar_request(
642 "open-ils.storage.direct.config.identification_type.retrieve.all.atomic" );
648 __PACKAGE__->register_method(
649 method => "get_org_unit",
650 api_name => "open-ils.actor.org_unit.retrieve",
655 my( $self, $client, $user_session, $org_id ) = @_;
657 if(defined($user_session) && !defined($org_id)) {
659 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
660 if(!defined($org_id)) {
661 $org_id = $user_obj->home_ou;
666 my $home_ou = OpenILS::Application::AppUtils->simple_scalar_request(
668 "open-ils.storage.direct.actor.org_unit.retrieve",
677 __PACKAGE__->register_method(
678 method => "get_org_tree",
679 api_name => "open-ils.actor.org_tree.retrieve",
681 note => "Returns the entire org tree structure",
685 my( $self, $client) = @_;
688 $cache_client = OpenSRF::Utils::Cache->new("global", 0);
690 # see if it's in the cache
691 warn "Getting ORG Tree\n";
692 my $tree = $cache_client->get_cache('orgtree');
694 warn "Found orgtree in cache. returning...\n";
698 my $orglist = $apputils->simple_scalar_request(
700 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
703 warn "found org list\n";
706 $tree = $self->build_org_tree($orglist);
707 $cache_client->put_cache('orgtree', $tree);
713 # turns an org list into an org tree
716 my( $self, $orglist) = @_;
718 return $orglist unless (
719 ref($orglist) and @$orglist > 1 );
722 $a->ou_type <=> $b->ou_type ||
723 $a->name cmp $b->name } @$orglist;
725 for my $org (@list) {
727 next unless ($org and defined($org->parent_ou));
728 my ($parent) = grep { $_->id == $org->parent_ou } @list;
731 $parent->children([]) unless defined($parent->children);
732 push( @{$parent->children}, $org );
740 __PACKAGE__->register_method(
741 method => "get_org_descendants",
742 api_name => "open-ils.actor.org_tree.descendants.retrieve"
745 # depth is optional. org_unit is the id
746 sub get_org_descendants {
747 my( $self, $client, $org_unit, $depth ) = @_;
748 my $orglist = $apputils->simple_scalar_request(
750 "open-ils.storage.actor.org_unit.descendants.atomic",
752 return $self->build_org_tree($orglist);
756 __PACKAGE__->register_method(
757 method => "get_org_ancestors",
758 api_name => "open-ils.actor.org_tree.ancestors.retrieve"
761 # depth is optional. org_unit is the id
762 sub get_org_ancestors {
763 my( $self, $client, $org_unit, $depth ) = @_;
764 my $orglist = $apputils->simple_scalar_request(
766 "open-ils.storage.actor.org_unit.ancestors.atomic",
768 return $self->build_org_tree($orglist);
772 __PACKAGE__->register_method(
773 method => "get_standings",
774 api_name => "open-ils.actor.standings.retrieve"
779 return $user_standings if $user_standings;
780 return $user_standings =
781 $apputils->simple_scalar_request(
783 "open-ils.storage.direct.config.standing.retrieve.all.atomic" );
788 __PACKAGE__->register_method(
789 method => "get_my_org_path",
790 api_name => "open-ils.actor.org_unit.full_path.retrieve"
793 sub get_my_org_path {
794 my( $self, $client, $user_session, $org_id ) = @_;
795 my $user_obj = $apputils->check_user_session($user_session);
796 if(!defined($org_id)) { $org_id = $user_obj->home_ou; }
798 return $apputils->simple_scalar_request(
800 "open-ils.storage.actor.org_unit.full_path.atomic",
805 __PACKAGE__->register_method(
806 method => "patron_adv_search",
807 api_name => "open-ils.actor.patron.search.advanced" );
809 sub patron_adv_search {
810 my( $self, $client, $staff_login, $search_hash ) = @_;
813 warn "patron adv with $staff_login and search " .
814 Dumper($search_hash) . "\n";
816 my $session = OpenSRF::AppSession->create("open-ils.storage");
817 my $req = $session->request(
818 "open-ils.storage.actor.user.crazy_search", $search_hash);
820 my $ans = $req->gather(1);
822 my %hash = map { ($_ =>1) } @$ans;
823 $ans = [ keys %hash ];
825 warn "Returning @$ans\n";
827 $session->disconnect();
834 sub _verify_password {
835 my($user_session, $password) = @_;
836 my $user_obj = $apputils->check_user_session($user_session);
838 #grab the user with password
839 $user_obj = $apputils->simple_scalar_request(
841 "open-ils.storage.direct.actor.user.retrieve",
844 if($user_obj->passwd eq $password) {
852 __PACKAGE__->register_method(
853 method => "update_password",
854 api_name => "open-ils.actor.user.password.update");
856 __PACKAGE__->register_method(
857 method => "update_password",
858 api_name => "open-ils.actor.user.username.update");
860 __PACKAGE__->register_method(
861 method => "update_password",
862 api_name => "open-ils.actor.user.email.update");
864 sub update_password {
865 my( $self, $client, $user_session, $new_value, $current_password ) = @_;
867 warn "Updating user with method " .$self->api_name . "\n";
868 my $user_obj = $apputils->check_user_session($user_session);
870 if($self->api_name =~ /password/) {
872 #make sure they know the current password
873 if(!_verify_password($user_session, md5_hex($current_password))) {
874 return OpenILS::EX->new("USER_WRONG_PASSWORD")->ex;
877 $user_obj->passwd($new_value);
879 elsif($self->api_name =~ /username/) {
880 $user_obj->usrname($new_value);
883 elsif($self->api_name =~ /email/) {
884 warn "Updating email to $new_value\n";
885 $user_obj->email($new_value);
888 my $session = $apputils->start_db_session();
889 $user_obj = _update_patron($session, $user_obj);
890 $apputils->commit_db_session($session);
892 if($user_obj) { return 1; }
897 __PACKAGE__->register_method(
898 method => "check_user_perms",
899 api_name => "open-ils.actor.user.perm.check",
900 notes => <<" NOTES");
901 Takes a login session, user id, an org id, and an array of perm type strings. For each
902 perm type, if the user does *not* have the given permission it is added
903 to a list which is returned from the method. If all permissions
904 are allowed, an empty list is returned
905 if the logged in user does not match 'user_id', then the logged in user must
906 have VIEW_PERMISSION priveleges.
909 sub check_user_perms {
910 my( $self, $client, $login_session, $user_id, $org_id, $perm_types ) = @_;
911 my $user_obj = $apputils->check_user_session($login_session);
913 if($user_obj->id ne $user_id) {
914 if($apputils->check_user_perms($user_obj->id, $org_id, "VIEW_PERMISSION")) {
915 return OpenILS::Perm->new("VIEW_PERMISSION");
920 for my $perm (@$perm_types) {
921 if($apputils->check_user_perms($user_id, $org_id, $perm)) {
922 push @not_allowed, $perm;
931 __PACKAGE__->register_method(
932 method => "user_fines_summary",
933 api_name => "open-ils.actor.user.fines.summary",
934 notes => <<" NOTES");
935 Returns a short summary of the users total open fines, excluding voided fines
936 Params are login_session, user_id
937 Returns a 'mus' object.
940 sub user_fines_summary {
941 my( $self, $client, $login_session, $user_id ) = @_;
943 my $user_obj = $apputils->check_user_session($login_session);
944 if($user_obj->id ne $user_id) {
945 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_FINES_SUMMARY")) {
946 return OpenILS::Perm->new("VIEW_USER_FINES_SUMMARY");
950 return $apputils->simple_scalar_request(
952 "open-ils.storage.direct.money.user_summary.search.usr",
960 __PACKAGE__->register_method(
961 method => "user_transactions",
962 api_name => "open-ils.actor.user.transactions",
963 notes => <<" NOTES");
964 Returns a list of open user transactions (mbts objects);
965 Params are login_session, user_id
966 Optional third parameter is the transactions type. defaults to all
969 __PACKAGE__->register_method(
970 method => "user_transactions",
971 api_name => "open-ils.actor.user.transactions.have_charge",
972 notes => <<" NOTES");
973 Returns a list of all open user transactions (mbts objects) that have an initial charge
974 Params are login_session, user_id
975 Optional third parameter is the transactions type. defaults to all
978 __PACKAGE__->register_method(
979 method => "user_transactions",
980 api_name => "open-ils.actor.user.transactions.have_balance",
981 notes => <<" NOTES");
982 Returns a list of all open user transactions (mbts objects) that have a balance
983 Params are login_session, user_id
984 Optional third parameter is the transactions type. defaults to all
987 __PACKAGE__->register_method(
988 method => "user_transactions",
989 api_name => "open-ils.actor.user.transactions.fleshed",
990 notes => <<" NOTES");
991 Returns an object/hash of transaction, circ, title where transaction = an open
992 user transactions (mbts objects), circ is the attached circluation, and title
993 is the title the circ points to
994 Params are login_session, user_id
995 Optional third parameter is the transactions type. defaults to all
998 __PACKAGE__->register_method(
999 method => "user_transactions",
1000 api_name => "open-ils.actor.user.transactions.have_charge.fleshed",
1001 notes => <<" NOTES");
1002 Returns an object/hash of transaction, circ, title where transaction = an open
1003 user transactions that has an initial charge (mbts objects), circ is the
1004 attached circluation, and title is the title the circ points to
1005 Params are login_session, user_id
1006 Optional third parameter is the transactions type. defaults to all
1009 __PACKAGE__->register_method(
1010 method => "user_transactions",
1011 api_name => "open-ils.actor.user.transactions.have_balance.fleshed",
1012 notes => <<" NOTES");
1013 Returns an object/hash of transaction, circ, title where transaction = an open
1014 user transaction that has a balance (mbts objects), circ is the attached
1015 circluation, and title is the title the circ points to
1016 Params are login_session, user_id
1017 Optional third parameter is the transaction type. defaults to all
1022 sub user_transactions {
1023 my( $self, $client, $login_session, $user_id, $type ) = @_;
1025 my $user_obj = $apputils->check_user_session($login_session);
1026 if($user_obj->id ne $user_id) {
1027 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_TRANSACTIONS")) {
1028 return OpenILS::Perm->new("VIEW_USER_TRANSACTIONS");
1032 my $api = $self->api_name();
1035 if(defined($type)) { @xact = (xact_type => $type);
1036 } else { @xact = (); }
1038 if($api =~ /have_charge/) {
1040 $trans = $apputils->simple_scalar_request(
1042 "open-ils.storage.direct.money.billable_transaction_summary.search_where.atomic",
1043 { usr => $user_id, total_owed => { ">" => 0 }, @xact });
1045 } elsif($api =~ /have_balance/) {
1047 $trans = $apputils->simple_scalar_request(
1049 "open-ils.storage.direct.money.billable_transaction_summary.search_where.atomic",
1050 { usr => $user_id, balance_owed => { ">" => 0 }, @xact });
1054 $trans = $apputils->simple_scalar_request(
1056 "open-ils.storage.direct.money.billable_transaction_summary.search_where.atomic",
1057 { usr => $user_id, @xact });
1060 if($api !~ /fleshed/) { return $trans; }
1065 for my $t (@$trans) {
1069 my $circ = $apputils->simple_scalar_request(
1071 "open-ils.storage.direct.action.circulation.retrieve",
1074 my $title = $apputils->simple_scalar_request(
1076 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
1077 $circ->target_copy );
1079 my $u = OpenILS::Utils::ModsParser->new();
1080 $u->start_mods_batch($title->marc());
1081 my $mods = $u->finish_mods_batch();
1083 push @resp, {transaction => $t, circ => $circ, record => $mods };
1093 __PACKAGE__->register_method(
1094 method => "retrieve_groups",
1095 api_name => "open-ils.actor.groups.retrieve",
1096 notes => <<" NOTES");
1097 Returns a list of user groups
1099 sub retrieve_groups {
1100 my( $self, $client ) = @_;
1101 return $apputils->simple_scalar_request(
1103 "open-ils.storage.direct.permission.grp_tree.retrieve.all.atomic");
1157 some old methods that may be good to keep around for now
1160 my( $session, $card ) = @_;
1162 warn "Deleting card with barcode " . $card->barcode() . "\n";
1163 my $req = $session->request(
1164 "open-ils.storage.direct.actor.card.delete",
1166 my $status = $req->gather(1);
1167 if(!defined($status)) {
1168 throw OpenSRF::EX::ERROR
1169 ("Unknown error updating card");
1175 # deletes the patron and any attached addresses and cards
1176 __PACKAGE__->register_method(
1177 method => "delete_patron",
1178 api_name => "open-ils.actor.patron.delete",
1183 my( $self, $client, $patron ) = @_;
1184 my $session = $apputils->start_db_session();
1189 $patron->clear_mailing_address();
1190 $patron->clear_billing_address();
1191 $patron->ischanged(1);
1193 _update_patron($session, $patron);
1194 _delete_address($session,$_) for (@{$patron->addresses()});
1195 _delete_card($session,$_) for (@{$patron->cards()});
1196 _delete_patron($session,$patron);
1197 $apputils->commit_db_session($session);
1199 } catch Error with {
1201 $err = "-*- Failure deleting user: $e";
1202 $apputils->rollback_db_session($session);
1206 if($err) { throw OpenSRF::EX::ERROR ($err); }
1207 warn "Patron Delete complete\n";
1211 sub _delete_patron {
1212 my( $session, $patron ) = @_;
1214 warn "Deleting patron " . $patron->usrname() . "\n";
1216 my $req = $session->request(
1217 "open-ils.storage.direct.actor.user.delete",
1219 my $status = $req->gather(1);
1220 if(!defined($status)) {
1221 throw OpenSRF::EX::ERROR
1222 ("Unknown error updating patron");