]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Actor.pm
updated mods parser with new virtual_record fields
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Actor.pm
1 package OpenILS::Application::Actor;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
4 use Data::Dumper;
5
6 use Digest::MD5 qw(md5_hex);
7
8 use OpenSRF::EX qw(:try);
9 use OpenILS::EX;
10
11 use OpenILS::Application::AppUtils;
12 use OpenILS::Utils::Fieldmapper;
13 use OpenILS::Application::Search::Actor;
14
15 my $apputils = "OpenILS::Application::AppUtils";
16 sub _d { warn "Patron:\n" . Dumper(shift()); }
17 my $cache_client = OpenSRF::Utils::Cache->new("global", 0);
18
19
20 __PACKAGE__->register_method(
21         method  => "update_patron",
22         api_name        => "open-ils.actor.patron.update",);
23
24 sub update_patron {
25         my( $self, $client, $user_session, $patron ) = @_;
26
27         my $session = $apputils->start_db_session();
28         my $err = undef;
29
30         warn $user_session . " " . $patron . "\n";
31         _d($patron);
32
33         my $user_obj = 
34                 OpenILS::Application::AppUtils->check_user_session( 
35                                 $user_session ); #throws EX on error
36
37         # XXX does this user have permission to add/create users.  Granularity?
38
39         # $new_patron is the patron in progress.  $patron is the original patron
40         # passed in with the method.  new_patron will change as the components
41         # of patron are added/updated.
42
43         my $new_patron;
44
45         #try {
46                 # create/update the patron first so we can use his id
47                 if($patron->isnew()) {
48                         $new_patron = _add_patron(
49                                         $session, _clone_patron($patron));
50                         if(UNIVERSAL::isa($new_patron, "OpenILS::EX")) {
51                                 $client->respond_complete($new_patron->ex);
52                                 return undef;
53                         }
54
55                 } else { 
56                         $new_patron = $patron; 
57                 }
58
59                 $new_patron = _add_update_addresses($session, $patron, $new_patron);
60                 $new_patron = _add_update_cards($session, $patron, $new_patron);
61
62                 if(UNIVERSAL::isa($new_patron,"OpenILS::EX")) {
63                         $client->respond_complete($new_patron->ex);
64                         return undef;
65                 }
66
67                 $new_patron = _add_survey_responses($session, $patron, $new_patron);
68                 $new_patron     = _create_stat_maps($session, $user_session, $patron, $new_patron);
69
70                 # re-update the patron if anything has happened to him during this process
71                 if($new_patron->ischanged()) {
72                         $new_patron = _update_patron($session, $new_patron);
73                 }
74                 $apputils->commit_db_session($session);
75
76 =head
77         } catch Error with { 
78                 my $e = shift;
79                 $err =  "-*- Failure adding user: $e";
80                 $apputils->rollback_db_session($session);
81                 warn $err;
82         };
83
84         if($err) { throw OpenSRF::EX::ERROR ($err); }
85 =cut
86
87         warn "Patron Update/Create complete\n";
88         return flesh_user($new_patron->id());
89 }
90
91
92
93
94 __PACKAGE__->register_method(
95         method  => "user_retrieve_fleshed_by_id",
96         api_name        => "open-ils.actor.user.fleshed.retrieve",);
97
98 sub user_retrieve_fleshed_by_id {
99         my( $self, $client, $user_session, $user_id ) = @_;
100         my $user_obj = $apputils->check_user_session( $user_session ); 
101         return flesh_user($user_id);
102 }
103
104
105 sub flesh_user {
106         my $id = shift;
107         my $session = shift;
108
109         my $kill = 0;
110
111         if(!$session) {
112                 $session = OpenSRF::AppSession->create("open-ils.storage");
113                 $kill = 1;
114         }
115
116         # grab the user with the given id 
117         my $ureq = $session->request(
118                         "open-ils.storage.direct.actor.user.retrieve", $id);
119         my $user = $ureq->gather(1);
120
121         # grab the cards
122         my $cards_req = $session->request(
123                         "open-ils.storage.direct.actor.card.search.usr",
124                         $user->id() );
125         $user->cards( $cards_req->gather(1) );
126
127         for my $c(@{$user->cards}) {
128                 if($c->id == $user->card || $c->id eq $user->card ) {
129                         warn "Setting my card to " . $c->id . "\n";
130                         $user->card($c);
131                 }
132         }
133
134         my $add_req = $session->request(
135                         "open-ils.storage.direct.actor.user_address.search.usr",
136                         $user->id() );
137         $user->addresses( $add_req->gather(1) );
138
139         for my $c(@{$user->addresses}) {
140                 if($c->id == $user->billing_address || $c->id eq $user->billing_address ) {
141                         warn "Setting my address to " . $c->id . "\n";
142                         $user->billing_address($c);
143                 }
144         }
145
146         for my $c(@{$user->addresses}) {
147                 if($c->id == $user->mailing_address || $c->id eq $user->mailing_address ) {
148                         warn "Setting my address to " . $c->id . "\n";
149                         $user->mailing_address($c);
150                 }
151         }
152
153         my $stat_req = $session->request(
154                 "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr",
155                 $user->id() );
156         $user->stat_cat_entries($stat_req->gather(1));
157
158         if($kill) { $session->disconnect(); }
159         $user->clear_passwd();
160         warn Dumper $user;
161
162         return $user;
163
164 }
165
166
167 # clone and clear stuff that would break the database
168 sub _clone_patron {
169         my $patron = shift;
170
171         my $new_patron = Fieldmapper::actor::user->new();
172
173         my $fmap = $Fieldmapper::fieldmap;
174         no strict; # shallow clone, may be useful in the fieldmapper
175         for my $field 
176                 (keys %{$fmap->{"Fieldmapper::actor::user"}->{'fields'}}) {
177                         $new_patron->$field( $patron->$field() );
178         }
179         use strict;
180
181         # clear these
182         $new_patron->clear_billing_address();
183         $new_patron->clear_mailing_address();
184         $new_patron->clear_addresses();
185         $new_patron->clear_card();
186         $new_patron->clear_cards();
187         $new_patron->clear_id();
188         $new_patron->clear_isnew();
189         $new_patron->clear_changed();
190         $new_patron->clear_deleted();
191         $new_patron->clear_stat_cat_entries();
192
193         return $new_patron;
194 }
195
196
197 sub _add_patron {
198         my $session             = shift;
199         my $patron              = shift;
200
201         warn "Creating new patron\n";
202         _d($patron);
203
204         my $req = $session->request(
205                 "open-ils.storage.direct.actor.user.create",$patron);
206         my $id = $req->gather(1);
207         if(!$id) { 
208                 return OpenILS::EX->new("DUPLICATE_USER_USERNAME");
209         }
210
211         # retrieve the patron from the db to collect defaults
212         my $ureq = $session->request(
213                         "open-ils.storage.direct.actor.user.retrieve",
214                         $id);
215
216         warn "Created new patron with id $id\n";
217
218         return $ureq->gather(1);
219 }
220
221
222 sub _update_patron {
223         my( $session, $patron) = @_;
224
225         warn "updating patron " . Dumper($patron) . "\n";
226
227         my $req = $session->request(
228                 "open-ils.storage.direct.actor.user.update",$patron );
229         my $status = $req->gather(1);
230         if(!defined($status)) { 
231                 throw OpenSRF::EX::ERROR 
232                         ("Unknown error updating patron"); 
233         }
234         return $patron;
235 }
236
237
238 sub _add_update_addresses {
239         my $session = shift;
240         my $patron = shift;
241         my $new_patron = shift;
242
243         my $current_id; # id of the address before creation
244
245         for my $address (@{$patron->addresses()}) {
246
247                 $address->usr($new_patron->id());
248
249                 if(ref($address) and $address->isnew()) {
250                         warn "Adding new address at street " . $address->street1() . "\n";
251
252                         $current_id = $address->id();
253                         $address = _add_address($session,$address);
254
255                         if( $patron->billing_address() and 
256                                         $patron->billing_address() == $current_id ) {
257                                 $new_patron->billing_address($address->id());
258                                 $new_patron->ischanged(1);
259                         }
260
261                         if( $patron->mailing_address() and
262                                         $patron->mailing_address() == $current_id ) {
263                                 $new_patron->mailing_address($address->id());
264                                 $new_patron->ischanged(1);
265                         }
266
267                 } elsif( ref($address) and $address->ischanged() ) {
268                         warn "Updating address at street " . $address->street1();
269                         $address->usr($new_patron->id());
270                         _update_address($session,$address);
271
272                 } elsif( ref($address) and $address->isdeleted() ) {
273                         warn "Deleting address at street " . $address->street1();
274
275                         if( $address->id() == $new_patron->mailing_address() ) {
276                                 $new_patron->clear_mailing_address();
277                                 _update_patron($session, $new_patron);
278                         }
279
280                         if( $address->id() == $new_patron->billing_address() ) {
281                                 $new_patron->clear_billing_address();
282                                 _update_patron($session, $new_patron);
283                         }
284
285                         _delete_address($session,$address);
286                 }
287         }
288
289         return $new_patron;
290 }
291
292
293 # adds an address to the db and returns the address with new id
294 sub _add_address {
295         my($session, $address) = @_;
296         $address->clear_id();
297
298         # put the address into the database
299         my $req = $session->request(
300                 "open-ils.storage.direct.actor.user_address.create",
301                 $address );
302
303         #update the id
304         my $id = $req->gather(1);
305         if(!$id) { 
306                 throw OpenSRF::EX::ERROR 
307                         ("Unable to create new user address"); 
308         }
309
310         warn "Created address with id $id\n";
311
312         # update all the necessary id's
313         $address->id( $id );
314         return $address;
315 }
316
317
318 sub _update_address {
319         my( $session, $address ) = @_;
320         my $req = $session->request(
321                 "open-ils.storage.direct.actor.user_address.update",
322                 $address );
323         my $status = $req->gather(1);
324         if(!defined($status)) { 
325                 throw OpenSRF::EX::ERROR 
326                         ("Unknown error updating address"); 
327         }
328         return $address;
329 }
330
331
332
333 sub _add_update_cards {
334
335         my $session = shift;
336         my $patron = shift;
337         my $new_patron = shift;
338
339         my $virtual_id; #id of the card before creation
340         for my $card (@{$patron->cards()}) {
341
342                 $card->usr($new_patron->id());
343
344                 if(ref($card) and $card->isnew()) {
345
346                         $virtual_id = $card->id();
347                         $card = _add_card($session,$card);
348                         if(UNIVERSAL::isa($card,"OpenILS::EX")) {
349                                 return $card;
350                         }
351
352                         if($patron->card() == $virtual_id) {
353                                 $new_patron->card($card->id());
354                                 $new_patron->ischanged(1);
355                         }
356
357                 } elsif( ref($card) and $card->ischanged() ) {
358                         $card->usr($new_patron->id());
359                         _update_card($session, $card);
360                 }
361         }
362         return $new_patron;
363 }
364
365
366 # adds an card to the db and returns the card with new id
367 sub _add_card {
368         my( $session, $card ) = @_;
369         $card->clear_id();
370
371         warn "Adding card with barcode " . $card->barcode() . "\n";
372         my $req = $session->request(
373                 "open-ils.storage.direct.actor.card.create",
374                 $card );
375
376         my $id = $req->gather(1);
377         if(!$id) { 
378                 return OpenILS::EX->new("DUPLICATE_INVALID_USER_BARCODE");
379         }
380
381         $card->id($id);
382         warn "Created patron card with id $id\n";
383         return $card;
384 }
385
386
387 sub _update_card {
388         my( $session, $card ) = @_;
389         warn Dumper $card;
390
391         my $req = $session->request(
392                 "open-ils.storage.direct.actor.card.update",
393                 $card );
394         my $status = $req->gather(1);
395         if(!defined($status)) { 
396                 throw OpenSRF::EX::ERROR 
397                         ("Unknown error updating card"); 
398         }
399         return $card;
400 }
401
402
403
404
405 sub _delete_address {
406         my( $session, $address ) = @_;
407
408         warn "Deleting address " . $address->street1() . "\n";
409
410         my $req = $session->request(
411                 "open-ils.storage.direct.actor.user_address.delete",
412                 $address );
413         my $status = $req->gather(1);
414         if(!defined($status)) { 
415                 throw OpenSRF::EX::ERROR 
416                         ("Unknown error updating address"); 
417         }
418         warn "Delete address status is $status\n";
419 }
420
421
422
423 sub _add_survey_responses {
424         my ($session, $patron, $new_patron) = @_;
425
426         warn "updating responses for user " . $new_patron->id . "\n";
427
428         my $responses = $patron->survey_responses;
429         for my $resp( @$responses ) {
430                 $resp->usr($new_patron->id);
431         }
432
433         my $status = $apputils->simple_scalar_request(
434                 "open-ils.circ", 
435                 "open-ils.circ.survey.submit.user_id",
436                 $responses );
437
438         return $new_patron;
439 }
440
441
442 sub _create_stat_maps {
443
444         my($session, $user_session, $patron, $new_patron) = @_;
445
446         my $maps = $patron->stat_cat_entries();
447
448         for my $map (@$maps) {
449
450                 next unless($map->isnew() || $map->ischanged());
451
452                 my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
453                 if($map->isnew()) {
454                         $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
455                 }
456
457                 $map->target_usr($new_patron->id);
458
459                 warn "Updating stat entry with method $method and session $user_session and map $map\n";
460
461                 my $req = $session->request($method, $map);
462                 my $status = $req->gather(1);
463
464                 warn "Updated\n";
465
466                 if(!$status) {
467                         throw OpenSRF::EX::ERROR 
468                                 ("Error updating stat map with method $method");        
469                 }
470         }
471
472         return $new_patron;
473 }
474
475
476
477 __PACKAGE__->register_method(
478         method  => "search_username",
479         api_name        => "open-ils.actor.user.search.username",
480 );
481
482 sub search_username {
483         my($self, $client, $username) = @_;
484         my $users = OpenILS::Application::AppUtils->simple_scalar_request(
485                         "open-ils.storage", 
486                         "open-ils.storage.direct.actor.user.search.usrname",
487                         $username );
488         return $users;
489 }
490
491
492
493
494 __PACKAGE__->register_method(
495         method  => "user_retrieve_by_barcode",
496         api_name        => "open-ils.actor.user.fleshed.retrieve_by_barcode",);
497
498 sub user_retrieve_by_barcode {
499         my($self, $client, $user_session, $barcode) = @_;
500         warn "Searching for user with barcode $barcode\n";
501         my $user_obj = $apputils->check_user_session( $user_session ); 
502
503         my $session = OpenSRF::AppSession->create("open-ils.storage");
504
505         # find the card with the given barcode
506         my $creq        = $session->request(
507                         "open-ils.storage.direct.actor.card.search.barcode",
508                         $barcode );
509         my $card = $creq->gather(1);
510
511         if(!$card || !$card->[0]) {
512                 $session->disconnect();
513                 return undef;
514         }
515
516         $card = $card->[0];
517         my $user = flesh_user($card->usr(), $session);
518         $session->disconnect();
519         return $user;
520
521 }
522
523
524
525 __PACKAGE__->register_method(
526         method  => "get_user_by_id",
527         api_name        => "open-ils.actor.user.retrieve",);
528
529 sub get_user_by_id {
530         my ($self, $client, $user_session, $id) = @_;
531
532         my $user_obj = $apputils->check_user_session( $user_session ); 
533
534         return $apputils->simple_scalar_request(
535                 "open-ils.storage",
536                 "open-ils.storage.direct.actor.user.retrieve",
537                 $id );
538 }
539
540
541
542 __PACKAGE__->register_method(
543         method  => "get_org_types",
544         api_name        => "open-ils.actor.org_types.retrieve",);
545
546 my $org_types;
547 sub get_org_types {
548         my($self, $client) = @_;
549
550         return $org_types if $org_types;
551          return $org_types = 
552                  $apputils->simple_scalar_request(
553                         "open-ils.storage",
554                         "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
555 }
556
557
558
559 __PACKAGE__->register_method(
560         method  => "get_user_profiles",
561         api_name        => "open-ils.actor.user.profiles.retrieve",
562 );
563
564 my $user_profiles;
565 sub get_user_profiles {
566         return $user_profiles if $user_profiles;
567
568         return $user_profiles = 
569                 $apputils->simple_scalar_request(
570                         "open-ils.storage",
571                         "open-ils.storage.direct.actor.profile.retrieve.all.atomic");
572 }
573
574
575
576 __PACKAGE__->register_method(
577         method  => "get_user_ident_types",
578         api_name        => "open-ils.actor.user.ident_types.retrieve",
579 );
580 my $ident_types;
581 sub get_user_ident_types {
582         return $ident_types if $ident_types;
583         return $ident_types = 
584                 $apputils->simple_scalar_request(
585                 "open-ils.storage",
586                 "open-ils.storage.direct.config.identification_type.retrieve.all.atomic" );
587 }
588
589
590
591
592 __PACKAGE__->register_method(
593         method  => "get_org_unit",
594         api_name        => "open-ils.actor.org_unit.retrieve",
595 );
596
597 sub get_org_unit {
598
599         my( $self, $client, $user_session, $org_id ) = @_;
600
601         if(defined($user_session) && !defined($org_id)) {
602                 my $user_obj = 
603                         OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
604                 if(!defined($org_id)) {
605                         $org_id = $user_obj->home_ou;
606                 }
607         }
608
609
610         my $home_ou = OpenILS::Application::AppUtils->simple_scalar_request(
611                 "open-ils.storage",
612                 "open-ils.storage.direct.actor.org_unit.retrieve", 
613                 $org_id );
614
615         return $home_ou;
616 }
617
618
619 # build the org tree
620
621 __PACKAGE__->register_method(
622         method  => "get_org_tree",
623         api_name        => "open-ils.actor.org_tree.retrieve",
624         argc            => 1, 
625         note            => "Returns the entire org tree structure",
626 );
627
628 sub get_org_tree {
629         my( $self, $client) = @_;
630
631         # see if it's in the cache
632         warn "Getting ORG Tree\n";
633         my $tree = $cache_client->get_cache('orgtree');
634         if($tree) { 
635                 warn "Found orgtree in cache. returning...\n";
636                 return $tree; 
637         }
638
639         my $orglist = $apputils->simple_scalar_request( 
640                 "open-ils.storage", 
641                 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
642
643         $tree = $self->build_org_tree($orglist);
644         $cache_client->put_cache('orgtree', $tree);
645
646         return $tree;
647
648 }
649
650 # turns an org list into an org tree
651 sub build_org_tree {
652
653         my( $self, $orglist) = @_;
654
655         return $orglist unless ( 
656                         ref($orglist) and @$orglist > 1 );
657
658         my @list = sort { 
659                 $a->ou_type <=> $b->ou_type ||
660                 $a->name cmp $b->name } @$orglist;
661
662         for my $org (@list) {
663
664                 next unless ($org and defined($org->parent_ou));
665                 my ($parent) = grep { $_->id == $org->parent_ou } @list;
666                 next unless $parent;
667
668                 $parent->children([]) unless defined($parent->children); 
669                 push( @{$parent->children}, $org );
670         }
671
672         return $list[0];
673
674 }
675
676
677 __PACKAGE__->register_method(
678         method  => "get_org_descendants",
679         api_name        => "open-ils.actor.org_tree.descendants.retrieve"
680 );
681
682 # depth is optional.  org_unit is the id
683 sub get_org_descendants {
684         my( $self, $client, $org_unit, $depth ) = @_;
685         my $orglist = $apputils->simple_scalar_request(
686                         "open-ils.storage", 
687                         "open-ils.storage.actor.org_unit.descendants.atomic",
688                         $org_unit, $depth );
689         return $self->build_org_tree($orglist);
690 }
691
692
693 __PACKAGE__->register_method(
694         method  => "get_org_ancestors",
695         api_name        => "open-ils.actor.org_tree.ancestors.retrieve"
696 );
697
698 # depth is optional.  org_unit is the id
699 sub get_org_ancestors {
700         my( $self, $client, $org_unit, $depth ) = @_;
701         my $orglist = $apputils->simple_scalar_request(
702                         "open-ils.storage", 
703                         "open-ils.storage.actor.org_unit.ancestors.atomic",
704                         $org_unit, $depth );
705         return $self->build_org_tree($orglist);
706 }
707
708
709 __PACKAGE__->register_method(
710         method  => "get_standings",
711         api_name        => "open-ils.actor.standings.retrieve"
712 );
713
714 my $user_standings;
715 sub get_standings {
716         return $user_standings if $user_standings;
717         return $user_standings = 
718                 $apputils->simple_scalar_request(
719                         "open-ils.storage",
720                         "open-ils.storage.direct.config.standing.retrieve.all.atomic" );
721 }
722
723
724
725 __PACKAGE__->register_method(
726         method  => "get_my_org_path",
727         api_name        => "open-ils.actor.org_unit.full_path.retrieve"
728 );
729
730 sub get_my_org_path {
731         my( $self, $client, $user_session, $org_id ) = @_;
732         my $user_obj = $apputils->check_user_session($user_session); 
733         if(!defined($org_id)) { $org_id = $user_obj->home_ou; }
734
735         return $apputils->simple_scalar_request(
736                 "open-ils.storage",
737                 "open-ils.storage.actor.org_unit.full_path.atomic",
738                 $org_id );
739 }
740
741
742 __PACKAGE__->register_method(
743         method  => "patron_adv_search",
744         api_name        => "open-ils.actor.patron.search.advanced" );
745
746 sub patron_adv_search {
747         my( $self, $client, $staff_login, $search_hash ) = @_;
748
749         use Data::Dumper;
750         warn "patron adv with $staff_login and search " . 
751                 Dumper($search_hash) . "\n";
752
753         my $session = OpenSRF::AppSession->create("open-ils.storage");
754         my $req = $session->request(
755                 "open-ils.storage.actor.user.crazy_search", $search_hash);
756
757         my $ans = $req->gather(1);
758
759         my %hash = map { ($_ =>1) } @$ans;
760         $ans = [ keys %hash ];
761
762         warn "Returning @$ans\n";
763
764         $session->disconnect();
765         return $ans;
766
767 }
768
769
770
771 sub _verify_password {
772         my($user_session, $password) = @_;
773         my $user_obj = $apputils->check_user_session($user_session); 
774
775         #grab the user with password
776         $user_obj = $apputils->simple_scalar_request(
777                 "open-ils.storage", 
778                 "open-ils.storage.direct.actor.user.retrieve",
779                 $user_obj->id );
780
781         if($user_obj->passwd eq $password) {
782                 return 1;
783         }
784
785         return 0;
786 }
787
788
789 __PACKAGE__->register_method(
790         method  => "update_password",
791         api_name        => "open-ils.actor.user.password.update");
792
793 __PACKAGE__->register_method(
794         method  => "update_password",
795         api_name        => "open-ils.actor.user.username.update");
796
797 __PACKAGE__->register_method(
798         method  => "update_password",
799         api_name        => "open-ils.actor.user.email.update");
800
801 sub update_password {
802         my( $self, $client, $user_session, $new_value, $current_password ) = @_;
803
804         warn "Updating user with method " .$self->api_name . "\n";
805         my $user_obj = $apputils->check_user_session($user_session); 
806
807         if($self->api_name =~ /password/) {
808
809                 #make sure they know the current password
810                 if(!_verify_password($user_session, md5_hex($current_password))) {
811                         return OpenILS::EX->new("USER_WRONG_PASSWORD")->ex;
812                 }
813
814                 $user_obj->passwd($new_value);
815         } 
816         elsif($self->api_name =~ /username/) {
817                 $user_obj->usrname($new_value);
818         }
819
820         elsif($self->api_name =~ /email/) {
821                 warn "Updating email to $new_value\n";
822                 $user_obj->email($new_value);
823         }
824
825         my $session = $apputils->start_db_session();
826         $user_obj = _update_patron($session, $user_obj);
827         $apputils->commit_db_session($session);
828
829         if($user_obj) { return 1; }
830         return undef;
831 }
832
833
834 # returns undef on success, the first perm_type that failed
835 # on permission error
836
837 __PACKAGE__->register_method(
838         method  => "check_user_perms",
839         api_name        => "open-ils.actor.user.perm.check");
840
841 sub check_user_perms {
842         my( $self, $client, $user_id, $org_id, @perm_types ) = @_;
843 }
844
845
846
847
848
849
850
851 1;
852
853
854
855
856 __END__
857
858
859 some old methods that may be good to keep around for now
860
861 sub _delete_card {
862         my( $session, $card ) = @_;
863
864         warn "Deleting card with barcode " . $card->barcode() . "\n";
865         my $req = $session->request(
866                 "open-ils.storage.direct.actor.card.delete",
867                 $card );
868         my $status = $req->gather(1);
869         if(!defined($status)) { 
870                 throw OpenSRF::EX::ERROR 
871                         ("Unknown error updating card"); 
872         }
873 }
874
875
876
877 # deletes the patron and any attached addresses and cards
878 __PACKAGE__->register_method(
879         method  => "delete_patron",
880         api_name        => "open-ils.actor.patron.delete",
881 );
882
883 sub delete_patron {
884
885         my( $self, $client, $patron ) = @_;
886         my $session = $apputils->start_db_session();
887         my $err = undef;
888
889         try {
890
891                 $patron->clear_mailing_address();
892                 $patron->clear_billing_address();
893                 $patron->ischanged(1);
894
895                 _update_patron($session, $patron);
896                 _delete_address($session,$_) for (@{$patron->addresses()});
897                 _delete_card($session,$_) for (@{$patron->cards()});
898                 _delete_patron($session,$patron);
899                 $apputils->commit_db_session($session);
900
901         } catch Error with {
902                 my $e = shift;
903                 $err =  "-*- Failure deleting user: $e";
904                 $apputils->rollback_db_session($session);
905                 warn $err;
906         };
907
908         if($err) { throw OpenSRF::EX::ERROR ($err); }
909         warn "Patron Delete complete\n";
910         return 1;
911 }
912
913 sub _delete_patron {
914         my( $session, $patron ) = @_;
915
916         warn "Deleting patron " . $patron->usrname() . "\n";
917
918         my $req = $session->request(
919                 "open-ils.storage.direct.actor.user.delete",
920                 $patron );
921         my $status = $req->gather(1);
922         if(!defined($status)) { 
923                 throw OpenSRF::EX::ERROR 
924                         ("Unknown error updating patron"); 
925         }
926 }
927
928