]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/Actor.pm
4c54513ac7bbe24805abd0f79315cda6dc17dd3d
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / Actor.pm
1 package OpenILS::Application::Circ::Actor;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
4 use Data::Dumper;
5 use OpenSRF::EX qw(:try);
6 use OpenILS::Application::AppUtils;
7 use OpenILS::Utils::Fieldmapper;
8
9 my $apputils = "OpenILS::Application::AppUtils";
10 sub _d { warn "Patron:\n" . Dumper(shift()); }
11
12
13 __PACKAGE__->register_method(
14         method  => "update_patron",
15         api_name        => "open-ils.circ.patron.create",
16 );
17
18 __PACKAGE__->register_method(
19         method  => "update_patron",
20         api_name        => "open-ils.circ.patron.update",
21 );
22
23
24 sub update_patron {
25         my( $self, $client, $patron ) = @_;
26
27         my $session = $apputils->start_db_session();
28         my $err = undef;
29
30
31         # $new_patron is the patron in progress.  $patron is the original patron
32         # passed in with the method.  new_patron will change as the components
33         # of patron are added/updated.
34
35         try {
36
37                 my $new_patron;
38
39                 # create/update the patron first so we can use his id
40                 if( $self->api_name =~ /create/ ) {
41                         $new_patron = _add_patron(
42                                         $session, _clone_patron($patron));
43                 } else { 
44                         $new_patron = $patron; 
45                 }
46
47                 $new_patron = _add_update_addresses($session, $patron, $new_patron);
48                 $new_patron = _add_update_cards($session, $patron, $new_patron);
49
50                 # re-update the patron if anything has happened to him during this process
51                 if($new_patron->ischanged()) {
52                         $new_patron = _update_patron($session, $new_patron);
53                 }
54                 $apputils->commit_db_session($session);
55
56         } catch Error with { 
57                 my $e = shift;
58                 $err =  "-*- Failure adding user: $e";
59                 $apputils->rollback_db_session($session);
60                 warn $e;
61         };
62
63         warn "Patron Update/Create complete\n";
64
65         if($err) { throw OpenSRF::EX::ERROR ($err); }
66
67         return 1;
68 }
69
70
71 # clone and clear stuff that would break the database
72 sub _clone_patron {
73         my $patron = shift;
74
75         my $new_patron = Fieldmapper::actor::user->new();
76
77         my $fmap = $Fieldmapper::fieldmap;
78         no strict; # shallow clone, may be useful in the fieldmapper
79         for my $field 
80                 (keys %{$fmap->{"Fieldmapper::actor::user"}->{'fields'}}) {
81                         $new_patron->$field( $patron->$field() );
82         }
83         use strict;
84
85         # clear these
86         $new_patron->clear_billing_address();
87         $new_patron->clear_mailing_address();
88         $new_patron->clear_addresses();
89         $new_patron->clear_card();
90         $new_patron->clear_cards();
91         $new_patron->clear_id();
92         $new_patron->clear_isnew();
93         $new_patron->clear_changed();
94         $new_patron->clear_deleted();
95
96         return $new_patron;
97 }
98
99
100 sub _add_patron {
101         my $session             = shift;
102         my $patron              = shift;
103
104         my $req = $session->request(
105                 "open-ils.storage.direct.actor.user.create",$patron);
106         my $id = $req->gather(1);
107         if(!$id) { throw OpenSRF::EX::ERROR ("Unable to create new user"); }
108         warn "Created new patron with id $id\n";
109         $patron->id($id);
110
111         return $patron;
112 }
113
114
115 sub _update_patron {
116         my( $session, $patron) = @_;
117
118         my $req = $session->request(
119                 "open-ils.storage.direct.actor.user.update",$patron );
120         my $status = $req->gather(1);
121         if(!defined($status)) { 
122                 throw OpenSRF::EX::ERROR 
123                         ("Unknown error updating patron"); 
124         }
125         return $patron;
126 }
127
128
129 sub _add_update_addresses {
130         my $session = shift;
131         my $patron = shift;
132         my $new_patron = shift;
133
134         my $current_id; # id of the address before creation
135
136         for my $address (@{$patron->addresses()}) {
137
138                 $address->usr($new_patron->id());
139
140                 if(ref($address) and $address->isnew()) {
141                         warn "Adding new address at street " . $address->street1() . "\n";
142
143                         $current_id = $address->id();
144                         $address = _add_address($session,$address);
145
146                         if( $patron->billing_address() == $current_id ) {
147                                 $new_patron->billing_address($address->id());
148                                 $new_patron->ischanged(1);
149                         }
150
151                         if( $patron->mailing_address() == $current_id ) {
152                                 $new_patron->mailing_address($address->id());
153                                 $new_patron->ischanged(1);
154                         }
155
156                 } elsif( ref($address) and $address->ischanged() ) {
157                         warn "Updating address at street " . $address->street1();
158                         _update_address($session,$address);
159                 }
160         }
161
162         return $new_patron;
163 }
164
165
166 # adds an address to the db and returns the address with new id
167 sub _add_address {
168         my($session, $address) = @_;
169         $address->clear_id();
170
171         # put the address into the database
172         my $req = $session->request(
173                 "open-ils.storage.direct.actor.user_address.create",
174                 $address );
175
176         #update the id
177         my $id = $req->gather(1);
178         if(!$id) { 
179                 throw OpenSRF::EX::ERROR 
180                         ("Unable to create new user address"); 
181         }
182
183         warn "Created address with id $id\n";
184
185         # update all the necessary id's
186         $address->id( $id );
187         return $address;
188 }
189
190
191 sub _update_address {
192         my( $session, $address ) = @_;
193         my $req = $session->request(
194                 "open-ils.storage.direct.actor.user_address.update",
195                 $address );
196         my $status = $req->gather(1);
197         if(!defined($status)) { 
198                 throw OpenSRF::EX::ERROR 
199                         ("Unknown error updating address"); 
200         }
201         return $address;
202 }
203
204
205
206 sub _add_update_cards {
207
208         my $session = shift;
209         my $patron = shift;
210         my $new_patron = shift;
211
212         my $virtual_id; #id of the card before creation
213         for my $card (@{$patron->cards()}) {
214
215                 $card->usr($new_patron->id());
216
217                 if(ref($card) and $card->isnew()) {
218
219                         $virtual_id = $card->id();
220                         $card = _add_card($session,$card);
221
222                         if($patron->card() == $virtual_id) {
223                                 $new_patron->card($card->id());
224                                 $new_patron->ischanged(1);
225                         }
226
227                 } elsif( ref($card) and $card->ischanged() ) {
228                         _update_card($session, $card);
229                 }
230         }
231         return $new_patron;
232 }
233
234
235 # adds an card to the db and returns the card with new id
236 sub _add_card {
237         my( $session, $card ) = @_;
238         $card->clear_id();
239
240         warn "Adding card with barcode " . $card->barcode() . "\n";
241         my $req = $session->request(
242                 "open-ils.storage.direct.actor.card.create",
243                 $card );
244
245         my $id = $req->gather(1);
246         if(!$id) { 
247                 throw OpenSRF::EX::ERROR 
248                         ("Unknown error creating card"); 
249         }
250
251         $card->id($id);
252         warn "Created patron card with id $id\n";
253         return $card;
254 }
255
256
257 sub _update_card {
258         my( $session, $card ) = @_;
259         warn Dumper $card;
260
261         my $req = $session->request(
262                 "open-ils.storage.direct.actor.card.update",
263                 $card );
264         my $status = $req->gather(1);
265         if(!defined($status)) { 
266                 throw OpenSRF::EX::ERROR 
267                         ("Unknown error updating card"); 
268         }
269         return $card;
270 }
271
272
273 1;