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 my $apputils = "OpenILS::Application::AppUtils";
18 sub _d { warn "Patron:\n" . Dumper(shift()); }
19 my $cache_client = OpenSRF::Utils::Cache->new("global", 0);
22 __PACKAGE__->register_method(
23 method => "update_patron",
24 api_name => "open-ils.actor.patron.update",);
27 my( $self, $client, $user_session, $patron ) = @_;
29 my $session = $apputils->start_db_session();
32 warn $user_session . " " . $patron . "\n";
36 OpenILS::Application::AppUtils->check_user_session(
37 $user_session ); #throws EX on error
39 # XXX does this user have permission to add/create users. Granularity?
40 # $new_patron is the patron in progress. $patron is the original patron
41 # passed in with the method. new_patron will change as the components
42 # of patron are added/updated.
46 if(ref($patron->card)) { $patron->card( $patron->card->id ); }
47 if(ref($patron->billing_address)) { $patron->billing_address( $patron->billing_address->id ); }
48 if(ref($patron->mailing_address)) { $patron->mailing_address( $patron->mailing_address->id ); }
50 # create/update the patron first so we can use his id
51 if($patron->isnew()) {
53 $new_patron = _add_patron($session, _clone_patron($patron), $user_obj);
55 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
56 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
57 $client->respond_complete($new_patron->ex);
61 } else { $new_patron = $patron; }
63 $new_patron = _add_update_addresses($session, $patron, $new_patron, $user_obj);
65 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
66 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
67 $client->respond_complete($new_patron->ex);
71 $new_patron = _add_update_cards($session, $patron, $new_patron, $user_obj);
73 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
74 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
75 $client->respond_complete($new_patron->ex);
79 $new_patron = _add_survey_responses($session, $patron, $new_patron, $user_obj);
80 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
81 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
82 $client->respond_complete($new_patron->ex);
87 # re-update the patron if anything has happened to him during this process
88 if($new_patron->ischanged()) {
89 $new_patron = _update_patron($session, $new_patron, $user_obj);
91 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
92 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
93 $client->respond_complete($new_patron->ex);
98 $apputils->commit_db_session($session);
100 $session = OpenSRF::AppSession->create("open-ils.storage");
101 $new_patron = _create_stat_maps($session, $user_session, $patron, $new_patron, $user_obj);
102 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
103 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
104 $client->respond_complete($new_patron->ex);
109 warn "Patron Update/Create complete\n";
110 return flesh_user($new_patron->id());
116 __PACKAGE__->register_method(
117 method => "user_retrieve_fleshed_by_id",
118 api_name => "open-ils.actor.user.fleshed.retrieve",);
120 sub user_retrieve_fleshed_by_id {
121 my( $self, $client, $user_session, $user_id ) = @_;
122 my $user_obj = $apputils->check_user_session( $user_session );
124 if( $user_obj->id ne $user_id ) {
125 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER")) {
126 return OpenILS::Perm->new("VIEW_USER");
130 return flesh_user($user_id);
141 $session = OpenSRF::AppSession->create("open-ils.storage");
145 # grab the user with the given id
146 my $ureq = $session->request(
147 "open-ils.storage.direct.actor.user.retrieve", $id);
148 my $user = $ureq->gather(1);
150 if(!$user) { return undef; }
153 my $cards_req = $session->request(
154 "open-ils.storage.direct.actor.card.search.usr.atomic",
156 $user->cards( $cards_req->gather(1) );
158 for my $c(@{$user->cards}) {
159 if($c->id == $user->card || $c->id eq $user->card ) {
160 warn "Setting my card to " . $c->id . "\n";
165 my $add_req = $session->request(
166 "open-ils.storage.direct.actor.user_address.search.usr.atomic",
168 $user->addresses( $add_req->gather(1) );
170 for my $c(@{$user->addresses}) {
171 if($c->id == $user->billing_address || $c->id eq $user->billing_address ) {
172 warn "Setting my address to " . $c->id . "\n";
173 $user->billing_address($c);
177 for my $c(@{$user->addresses}) {
178 if($c->id == $user->mailing_address || $c->id eq $user->mailing_address ) {
179 warn "Setting my address to " . $c->id . "\n";
180 $user->mailing_address($c);
184 my $stat_req = $session->request(
185 "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr.atomic",
187 $user->stat_cat_entries($stat_req->gather(1));
189 if($kill) { $session->disconnect(); }
190 $user->clear_passwd();
198 # clone and clear stuff that would break the database
202 my $new_patron = Fieldmapper::actor::user->new();
204 my $fmap = $Fieldmapper::fieldmap;
205 no strict; # shallow clone, may be useful in the fieldmapper
207 (keys %{$fmap->{"Fieldmapper::actor::user"}->{'fields'}}) {
208 $new_patron->$field( $patron->$field() );
213 $new_patron->clear_billing_address();
214 $new_patron->clear_mailing_address();
215 $new_patron->clear_addresses();
216 $new_patron->clear_card();
217 $new_patron->clear_cards();
218 $new_patron->clear_id();
219 $new_patron->clear_isnew();
220 $new_patron->clear_ischanged();
221 $new_patron->clear_isdeleted();
222 $new_patron->clear_stat_cat_entries();
231 my $user_obj = shift;
234 if($apputils->check_user_perms(
235 $user_obj->id, $user_obj->home_ou, "CREATE_USER")) {
236 return OpenILS::Perm->new("CREATE_USER");
239 warn "Creating new patron\n";
242 my $req = $session->request(
243 "open-ils.storage.direct.actor.user.create",$patron);
244 my $id = $req->gather(1);
246 return OpenILS::EX->new("DUPLICATE_USER_USERNAME");
249 # retrieve the patron from the db to collect defaults
250 my $ureq = $session->request(
251 "open-ils.storage.direct.actor.user.retrieve",
254 warn "Created new patron with id $id\n";
256 return $ureq->gather(1);
261 my( $session, $patron, $user_obj) = @_;
264 if($apputils->check_user_perms(
265 $user_obj->id, $user_obj->home_ou, "UPDATE_USER")) {
266 return OpenILS::Perm->new("UPDATE_USER");
269 warn "updating patron " . Dumper($patron) . "\n";
271 my $req = $session->request(
272 "open-ils.storage.direct.actor.user.update",$patron );
273 my $status = $req->gather(1);
274 if(!defined($status)) {
275 throw OpenSRF::EX::ERROR
276 ("Unknown error updating patron");
282 sub _add_update_addresses {
285 my $new_patron = shift;
287 my $current_id; # id of the address before creation
289 for my $address (@{$patron->addresses()}) {
291 $address->usr($new_patron->id());
293 if(ref($address) and $address->isnew()) {
294 warn "Adding new address at street " . $address->street1() . "\n";
296 $current_id = $address->id();
297 $address = _add_address($session,$address);
299 if( $patron->billing_address() and
300 $patron->billing_address() == $current_id ) {
301 $new_patron->billing_address($address->id());
302 $new_patron->ischanged(1);
305 if( $patron->mailing_address() and
306 $patron->mailing_address() == $current_id ) {
307 $new_patron->mailing_address($address->id());
308 $new_patron->ischanged(1);
311 } elsif( ref($address) and $address->ischanged() ) {
312 warn "Updating address at street " . $address->street1();
313 $address->usr($new_patron->id());
314 _update_address($session,$address);
316 } elsif( ref($address) and $address->isdeleted() ) {
317 warn "Deleting address at street " . $address->street1();
319 if( $address->id() == $new_patron->mailing_address() ) {
320 $new_patron->clear_mailing_address();
321 _update_patron($session, $new_patron);
324 if( $address->id() == $new_patron->billing_address() ) {
325 $new_patron->clear_billing_address();
326 _update_patron($session, $new_patron);
329 _delete_address($session,$address);
337 # adds an address to the db and returns the address with new id
339 my($session, $address) = @_;
340 $address->clear_id();
342 # put the address into the database
343 my $req = $session->request(
344 "open-ils.storage.direct.actor.user_address.create",
348 my $id = $req->gather(1);
350 throw OpenSRF::EX::ERROR
351 ("Unable to create new user address");
354 warn "Created address with id $id\n";
356 # update all the necessary id's
362 sub _update_address {
363 my( $session, $address ) = @_;
364 my $req = $session->request(
365 "open-ils.storage.direct.actor.user_address.update",
367 my $status = $req->gather(1);
368 if(!defined($status)) {
369 throw OpenSRF::EX::ERROR
370 ("Unknown error updating address");
377 sub _add_update_cards {
381 my $new_patron = shift;
383 my $virtual_id; #id of the card before creation
384 for my $card (@{$patron->cards()}) {
386 $card->usr($new_patron->id());
388 if(ref($card) and $card->isnew()) {
390 $virtual_id = $card->id();
391 $card = _add_card($session,$card);
392 if(UNIVERSAL::isa($card,"OpenILS::EX")) {
396 #if(ref($patron->card)) { $patron->card($patron->card->id); }
397 if($patron->card() == $virtual_id) {
398 $new_patron->card($card->id());
399 $new_patron->ischanged(1);
402 } elsif( ref($card) and $card->ischanged() ) {
403 $card->usr($new_patron->id());
404 _update_card($session, $card);
411 # adds an card to the db and returns the card with new id
413 my( $session, $card ) = @_;
416 warn "Adding card with barcode " . $card->barcode() . "\n";
417 my $req = $session->request(
418 "open-ils.storage.direct.actor.card.create",
421 my $id = $req->gather(1);
423 return OpenILS::EX->new("DUPLICATE_INVALID_USER_BARCODE");
427 warn "Created patron card with id $id\n";
433 my( $session, $card ) = @_;
436 my $req = $session->request(
437 "open-ils.storage.direct.actor.card.update",
439 my $status = $req->gather(1);
440 if(!defined($status)) {
441 throw OpenSRF::EX::ERROR
442 ("Unknown error updating card");
450 sub _delete_address {
451 my( $session, $address ) = @_;
453 warn "Deleting address " . $address->street1() . "\n";
455 my $req = $session->request(
456 "open-ils.storage.direct.actor.user_address.delete",
458 my $status = $req->gather(1);
459 if(!defined($status)) {
460 throw OpenSRF::EX::ERROR
461 ("Unknown error updating address");
463 warn "Delete address status is $status\n";
468 sub _add_survey_responses {
469 my ($session, $patron, $new_patron) = @_;
471 warn "updating responses for user " . $new_patron->id . "\n";
473 my $responses = $patron->survey_responses;
477 for my $resp( @$responses ) {
478 $resp->usr($new_patron->id);
481 my $status = $apputils->simple_scalar_request(
483 "open-ils.circ.survey.submit.user_id",
492 sub _create_stat_maps {
494 my($session, $user_session, $patron, $new_patron) = @_;
496 my $maps = $patron->stat_cat_entries();
498 for my $map (@$maps) {
500 next unless($map->isnew() || $map->ischanged());
502 my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
504 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
508 $map->target_usr($new_patron->id);
510 warn "Updating stat entry with method $method and session $user_session and map $map\n";
512 my $req = $session->request($method, $map);
513 my $status = $req->gather(1);
518 throw OpenSRF::EX::ERROR
519 ("Error updating stat map with method $method");
529 __PACKAGE__->register_method(
530 method => "search_username",
531 api_name => "open-ils.actor.user.search.username",
534 sub search_username {
535 my($self, $client, $username) = @_;
536 my $users = OpenILS::Application::AppUtils->simple_scalar_request(
538 "open-ils.storage.direct.actor.user.search.usrname.atomic",
546 __PACKAGE__->register_method(
547 method => "user_retrieve_by_barcode",
548 api_name => "open-ils.actor.user.fleshed.retrieve_by_barcode",);
550 sub user_retrieve_by_barcode {
551 my($self, $client, $user_session, $barcode) = @_;
552 warn "Searching for user with barcode $barcode\n";
553 my $user_obj = $apputils->check_user_session( $user_session );
555 my $session = OpenSRF::AppSession->create("open-ils.storage");
557 # find the card with the given barcode
558 my $creq = $session->request(
559 "open-ils.storage.direct.actor.card.search.barcode.atomic",
561 my $card = $creq->gather(1);
563 if(!$card || !$card->[0]) {
564 $session->disconnect();
569 my $user = flesh_user($card->usr(), $session);
570 $session->disconnect();
577 __PACKAGE__->register_method(
578 method => "get_user_by_id",
579 api_name => "open-ils.actor.user.retrieve",);
582 my ($self, $client, $user_session, $id) = @_;
584 my $user_obj = $apputils->check_user_session( $user_session );
586 return $apputils->simple_scalar_request(
588 "open-ils.storage.direct.actor.user.retrieve",
594 __PACKAGE__->register_method(
595 method => "get_org_types",
596 api_name => "open-ils.actor.org_types.retrieve",);
600 my($self, $client) = @_;
602 return $org_types if $org_types;
604 $apputils->simple_scalar_request(
606 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
611 __PACKAGE__->register_method(
612 method => "get_user_profiles",
613 api_name => "open-ils.actor.user.profiles.retrieve",
617 sub get_user_profiles {
618 return $user_profiles if $user_profiles;
620 return $user_profiles =
621 $apputils->simple_scalar_request(
623 "open-ils.storage.direct.actor.profile.retrieve.all.atomic");
628 __PACKAGE__->register_method(
629 method => "get_user_ident_types",
630 api_name => "open-ils.actor.user.ident_types.retrieve",
633 sub get_user_ident_types {
634 return $ident_types if $ident_types;
635 return $ident_types =
636 $apputils->simple_scalar_request(
638 "open-ils.storage.direct.config.identification_type.retrieve.all.atomic" );
644 __PACKAGE__->register_method(
645 method => "get_org_unit",
646 api_name => "open-ils.actor.org_unit.retrieve",
651 my( $self, $client, $user_session, $org_id ) = @_;
653 if(defined($user_session) && !defined($org_id)) {
655 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
656 if(!defined($org_id)) {
657 $org_id = $user_obj->home_ou;
662 my $home_ou = OpenILS::Application::AppUtils->simple_scalar_request(
664 "open-ils.storage.direct.actor.org_unit.retrieve",
673 __PACKAGE__->register_method(
674 method => "get_org_tree",
675 api_name => "open-ils.actor.org_tree.retrieve",
677 note => "Returns the entire org tree structure",
681 my( $self, $client) = @_;
683 # see if it's in the cache
684 warn "Getting ORG Tree\n";
685 my $tree = $cache_client->get_cache('orgtree');
687 warn "Found orgtree in cache. returning...\n";
691 my $orglist = $apputils->simple_scalar_request(
693 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
696 warn "found org list\n";
699 $tree = $self->build_org_tree($orglist);
700 $cache_client->put_cache('orgtree', $tree);
706 # turns an org list into an org tree
709 my( $self, $orglist) = @_;
711 return $orglist unless (
712 ref($orglist) and @$orglist > 1 );
715 $a->ou_type <=> $b->ou_type ||
716 $a->name cmp $b->name } @$orglist;
718 for my $org (@list) {
720 next unless ($org and defined($org->parent_ou));
721 my ($parent) = grep { $_->id == $org->parent_ou } @list;
724 $parent->children([]) unless defined($parent->children);
725 push( @{$parent->children}, $org );
733 __PACKAGE__->register_method(
734 method => "get_org_descendants",
735 api_name => "open-ils.actor.org_tree.descendants.retrieve"
738 # depth is optional. org_unit is the id
739 sub get_org_descendants {
740 my( $self, $client, $org_unit, $depth ) = @_;
741 my $orglist = $apputils->simple_scalar_request(
743 "open-ils.storage.actor.org_unit.descendants.atomic",
745 return $self->build_org_tree($orglist);
749 __PACKAGE__->register_method(
750 method => "get_org_ancestors",
751 api_name => "open-ils.actor.org_tree.ancestors.retrieve"
754 # depth is optional. org_unit is the id
755 sub get_org_ancestors {
756 my( $self, $client, $org_unit, $depth ) = @_;
757 my $orglist = $apputils->simple_scalar_request(
759 "open-ils.storage.actor.org_unit.ancestors.atomic",
761 return $self->build_org_tree($orglist);
765 __PACKAGE__->register_method(
766 method => "get_standings",
767 api_name => "open-ils.actor.standings.retrieve"
772 return $user_standings if $user_standings;
773 return $user_standings =
774 $apputils->simple_scalar_request(
776 "open-ils.storage.direct.config.standing.retrieve.all.atomic" );
781 __PACKAGE__->register_method(
782 method => "get_my_org_path",
783 api_name => "open-ils.actor.org_unit.full_path.retrieve"
786 sub get_my_org_path {
787 my( $self, $client, $user_session, $org_id ) = @_;
788 my $user_obj = $apputils->check_user_session($user_session);
789 if(!defined($org_id)) { $org_id = $user_obj->home_ou; }
791 return $apputils->simple_scalar_request(
793 "open-ils.storage.actor.org_unit.full_path.atomic",
798 __PACKAGE__->register_method(
799 method => "patron_adv_search",
800 api_name => "open-ils.actor.patron.search.advanced" );
802 sub patron_adv_search {
803 my( $self, $client, $staff_login, $search_hash ) = @_;
806 warn "patron adv with $staff_login and search " .
807 Dumper($search_hash) . "\n";
809 my $session = OpenSRF::AppSession->create("open-ils.storage");
810 my $req = $session->request(
811 "open-ils.storage.actor.user.crazy_search", $search_hash);
813 my $ans = $req->gather(1);
815 my %hash = map { ($_ =>1) } @$ans;
816 $ans = [ keys %hash ];
818 warn "Returning @$ans\n";
820 $session->disconnect();
827 sub _verify_password {
828 my($user_session, $password) = @_;
829 my $user_obj = $apputils->check_user_session($user_session);
831 #grab the user with password
832 $user_obj = $apputils->simple_scalar_request(
834 "open-ils.storage.direct.actor.user.retrieve",
837 if($user_obj->passwd eq $password) {
845 __PACKAGE__->register_method(
846 method => "update_password",
847 api_name => "open-ils.actor.user.password.update");
849 __PACKAGE__->register_method(
850 method => "update_password",
851 api_name => "open-ils.actor.user.username.update");
853 __PACKAGE__->register_method(
854 method => "update_password",
855 api_name => "open-ils.actor.user.email.update");
857 sub update_password {
858 my( $self, $client, $user_session, $new_value, $current_password ) = @_;
860 warn "Updating user with method " .$self->api_name . "\n";
861 my $user_obj = $apputils->check_user_session($user_session);
863 if($self->api_name =~ /password/) {
865 #make sure they know the current password
866 if(!_verify_password($user_session, md5_hex($current_password))) {
867 return OpenILS::EX->new("USER_WRONG_PASSWORD")->ex;
870 $user_obj->passwd($new_value);
872 elsif($self->api_name =~ /username/) {
873 $user_obj->usrname($new_value);
876 elsif($self->api_name =~ /email/) {
877 warn "Updating email to $new_value\n";
878 $user_obj->email($new_value);
881 my $session = $apputils->start_db_session();
882 $user_obj = _update_patron($session, $user_obj);
883 $apputils->commit_db_session($session);
885 if($user_obj) { return 1; }
890 __PACKAGE__->register_method(
891 method => "check_user_perms",
892 api_name => "open-ils.actor.user.perm.check",
893 notes => <<" NOTES");
894 Takes a login session, user id, an org id, and an array of perm type strings. For each
895 perm type, if the user does *not* have the given permission it is added
896 to a list which is returned from the method. If all permissions
897 are allowed, an empty list is returned
898 if the logged in user does not match 'user_id', then the logged in user must
899 have VIEW_PERMISSION priveleges.
902 sub check_user_perms {
903 my( $self, $client, $login_session, $user_id, $org_id, $perm_types ) = @_;
904 my $user_obj = $apputils->check_user_session($login_session);
906 if($user_obj->id ne $user_id) {
907 if($apputils->check_user_perms($user_obj->id, $org_id, "VIEW_PERMISSION")) {
908 return OpenILS::Perm->new("VIEW_PERMISSION");
913 for my $perm (@$perm_types) {
914 if($apputils->check_user_perms($user_id, $org_id, $perm)) {
915 push @not_allowed, $perm;
924 __PACKAGE__->register_method(
925 method => "user_fines_summary",
926 api_name => "open-ils.actor.user.fines.summary",
927 notes => <<" NOTES");
928 Returns a short summary of the users total open fines, excluding voided fines
929 Params are login_session, user_id
930 Returns a 'mus' object.
933 sub user_fines_summary {
934 my( $self, $client, $login_session, $user_id ) = @_;
936 my $user_obj = $apputils->check_user_session($login_session);
937 if($user_obj->id ne $user_id) {
938 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_FINES_SUMMARY")) {
939 return OpenILS::Perm->new("VIEW_USER_FINES_SUMMARY");
943 return $apputils->simple_scalar_request(
945 "open-ils.storage.direct.money.user_summary.search.usr",
953 __PACKAGE__->register_method(
954 method => "user_transactions",
955 api_name => "open-ils.actor.user.transactions",
956 notes => <<" NOTES");
957 Returns a list of open user transactions (mbts objects);
958 Params are login_session, user_id
959 Optional third parameter is the transactions type. defaults to all
962 __PACKAGE__->register_method(
963 method => "user_transactions",
964 api_name => "open-ils.actor.user.transactions.have_charge",
965 notes => <<" NOTES");
966 Returns a list of all open user transactions (mbts objects) that have an initial charge
967 Params are login_session, user_id
968 Optional third parameter is the transactions type. defaults to all
971 __PACKAGE__->register_method(
972 method => "user_transactions",
973 api_name => "open-ils.actor.user.transactions.have_balance",
974 notes => <<" NOTES");
975 Returns a list of all open user transactions (mbts objects) that have a balance
976 Params are login_session, user_id
977 Optional third parameter is the transactions type. defaults to all
980 __PACKAGE__->register_method(
981 method => "user_transactions",
982 api_name => "open-ils.actor.user.transactions.fleshed",
983 notes => <<" NOTES");
984 Returns an object/hash of transaction, circ, title where transaction = an open
985 user transactions (mbts objects), circ is the attached circluation, and title
986 is the title the circ points to
987 Params are login_session, user_id
988 Optional third parameter is the transactions type. defaults to all
991 __PACKAGE__->register_method(
992 method => "user_transactions",
993 api_name => "open-ils.actor.user.transactions.have_charge.fleshed",
994 notes => <<" NOTES");
995 Returns an object/hash of transaction, circ, title where transaction = an open
996 user transactions that has an initial charge (mbts objects), circ is the
997 attached circluation, and title is the title the circ points to
998 Params are login_session, user_id
999 Optional third parameter is the transactions type. defaults to all
1002 __PACKAGE__->register_method(
1003 method => "user_transactions",
1004 api_name => "open-ils.actor.user.transactions.have_balance.fleshed",
1005 notes => <<" NOTES");
1006 Returns an object/hash of transaction, circ, title where transaction = an open
1007 user transaction that has a balance (mbts objects), circ is the attached
1008 circluation, and title is the title the circ points to
1009 Params are login_session, user_id
1010 Optional third parameter is the transaction type. defaults to all
1015 sub user_transactions {
1016 my( $self, $client, $login_session, $user_id, $type ) = @_;
1018 my $user_obj = $apputils->check_user_session($login_session);
1019 if($user_obj->id ne $user_id) {
1020 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_TRANSACTIONS")) {
1021 return OpenILS::Perm->new("VIEW_USER_TRANSACTIONS");
1025 my $api = $self->api_name();
1028 if(defined($type)) { @xact = (xact_type => $type);
1029 } else { @xact = (); }
1031 if($api =~ /have_charge/) {
1033 $trans = $apputils->simple_scalar_request(
1035 "open-ils.storage.direct.money.billable_transaction_summary.search_where.atomic",
1036 { usr => $user_id, total_owed => { ">" => 0 }, @xact });
1038 } elsif($api =~ /have_balance/) {
1040 $trans = $apputils->simple_scalar_request(
1042 "open-ils.storage.direct.money.billable_transaction_summary.search_where.atomic",
1043 { usr => $user_id, balance_owed => { ">" => 0 }, @xact });
1047 $trans = $apputils->simple_scalar_request(
1049 "open-ils.storage.direct.money.billable_transaction_summary.search_where.atomic",
1050 { usr => $user_id, @xact });
1053 if($api !~ /fleshed/) { return $trans; }
1058 for my $t (@$trans) {
1062 my $circ = $apputils->simple_scalar_request(
1064 "open-ils.storage.direct.action.circulation.retrieve",
1067 my $title = $apputils->simple_scalar_request(
1069 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
1070 $circ->target_copy );
1072 my $u = OpenILS::Utils::ModsParser->new();
1073 $u->start_mods_batch($title->marc());
1074 my $mods = $u->finish_mods_batch();
1076 push @resp, {transaction => $t, circ => $circ, record => $mods };
1086 __PACKAGE__->register_method(
1087 method => "retrieve_groups",
1088 api_name => "open-ils.actor.groups.retrieve",
1089 notes => <<" NOTES");
1090 Returns a list of user groups
1092 sub retrieve_groups {
1093 my( $self, $client ) = @_;
1094 return $apputils->simple_scalar_request(
1096 "open-ils.storage.direct.permission.grp_tree.retrieve.all.atomic");
1150 some old methods that may be good to keep around for now
1153 my( $session, $card ) = @_;
1155 warn "Deleting card with barcode " . $card->barcode() . "\n";
1156 my $req = $session->request(
1157 "open-ils.storage.direct.actor.card.delete",
1159 my $status = $req->gather(1);
1160 if(!defined($status)) {
1161 throw OpenSRF::EX::ERROR
1162 ("Unknown error updating card");
1168 # deletes the patron and any attached addresses and cards
1169 __PACKAGE__->register_method(
1170 method => "delete_patron",
1171 api_name => "open-ils.actor.patron.delete",
1176 my( $self, $client, $patron ) = @_;
1177 my $session = $apputils->start_db_session();
1182 $patron->clear_mailing_address();
1183 $patron->clear_billing_address();
1184 $patron->ischanged(1);
1186 _update_patron($session, $patron);
1187 _delete_address($session,$_) for (@{$patron->addresses()});
1188 _delete_card($session,$_) for (@{$patron->cards()});
1189 _delete_patron($session,$patron);
1190 $apputils->commit_db_session($session);
1192 } catch Error with {
1194 $err = "-*- Failure deleting user: $e";
1195 $apputils->rollback_db_session($session);
1199 if($err) { throw OpenSRF::EX::ERROR ($err); }
1200 warn "Patron Delete complete\n";
1204 sub _delete_patron {
1205 my( $session, $patron ) = @_;
1207 warn "Deleting patron " . $patron->usrname() . "\n";
1209 my $req = $session->request(
1210 "open-ils.storage.direct.actor.user.delete",
1212 my $status = $req->gather(1);
1213 if(!defined($status)) {
1214 throw OpenSRF::EX::ERROR
1215 ("Unknown error updating patron");