]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/offline/offline.pl
stringify the copy location list before making it a param
[Evergreen.git] / Open-ILS / src / offline / offline.pl
1 #!/usr/bin/perl
2 use strict; use warnings;
3 use CGI;
4 use OpenSRF::Utils::JSON;
5 use OpenSRF::System;
6 use OpenSRF::Utils::Logger qw/$logger/;
7 use OpenILS::Application::AppUtils;
8 use OpenILS::Event;
9 use OpenSRF::EX qw/:try/;
10 use Data::Dumper;
11 use OpenILS::Utils::Fieldmapper;
12 use Digest::MD5 qw/md5_hex/;
13 use OpenSRF::Utils qw/:daemon/;
14 use OpenILS::Utils::OfflineStore;
15 use OpenSRF::Utils::SettingsClient;
16 use OpenSRF::Utils;
17 use DateTime;
18
19 use DBI;
20 $DBI::trace = 1;
21
22 my $U = "OpenILS::Application::AppUtils";
23 my $DB = "OpenILS::Utils::OfflineStore";
24 my $SES = "${DB}::Session";
25 my $SCRIPT = "OpenILS::Utils::OfflineStore::Script";
26 my $user_groups;
27
28 # --------------------------------------------------------------------
29 # Load the config
30 # --------------------------------------------------------------------
31 our %config;
32 do '##CONFIG##/offline-config.pl';
33
34
35 my $cgi                 = new CGI;
36 my $basedir             = $config{base_dir} || die "Offline config error: no base_dir defined\n";
37 my $bootstrap   = $config{bootstrap} || die "Offline config error: no bootstrap defined\n";
38 my $wsname              = $cgi->param('ws');
39 my $org                 = $cgi->param('org');
40 my $authtoken   = $cgi->param('ses') || "";
41 my $seskey              = $cgi->param('seskey');
42 my $action              = $cgi->param('action'); # - create, load, execute, status
43 my $requestor; 
44 my $wsobj;
45 my $orgobj;
46 my $evt;
47
48
49 &ol_init;
50 &ol_runtime_init;
51 &ol_do_action;
52
53
54 # --------------------------------------------------------------------
55 # Set it all up
56 # This function should behave as a child_init might behave in case 
57 # this is moved to mod_perl
58 # --------------------------------------------------------------------
59 sub ol_init {
60         $DB->DBFile($config{dsn}, $config{usr}, $config{pw});
61         ol_connect();
62 }
63
64 sub ol_connect {
65         OpenSRF::System->bootstrap_client(config_file => $bootstrap ); 
66         Fieldmapper->import(IDL => 
67                 OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
68
69 }
70
71
72 sub _ol_debug_params {
73         my $s = "";
74         my @params = $cgi->param;
75         @params = sort { $a cmp $b } @params;
76         $s .= "$_=" . $cgi->param($_) . "\n" for @params;
77         $s =~ s/\n$//o;
78         warn '-'x60 ."\n$s\n";
79 }
80
81
82 # --------------------------------------------------------------------
83 # Finds the requestor and other info specific to this request
84 # --------------------------------------------------------------------
85 sub ol_runtime_init {
86
87         # fetch the requestor object
88         ($requestor, $evt) = $U->checkses($authtoken);
89         ol_handle_result($evt) if $evt;
90
91         # try the param, the workstation, and finally the user's ws org
92         if(!$org) { 
93                 $wsobj = ol_fetch_workstation($wsname);
94                 $org = $wsobj->owning_lib if $wsobj;
95                 $org = $requestor->ws_ou unless $org;
96                 ol_handle_result(OpenILS::Event->new('OFFLINE_NO_ORG')) unless $org;
97         }
98
99     $user_groups = $U->simplereq(
100         'open-ils.actor', 'open-ils.actor.groups.retrieve');
101 }
102
103
104 # --------------------------------------------------------------------
105 # Runs the requested action
106 # --------------------------------------------------------------------
107 sub ol_do_action {
108
109         my $payload;
110
111         if( $action eq 'create' ) {
112                 
113                 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_UPLOAD');
114                 ol_handle_result($evt) if $evt;
115                 $payload = ol_create_session();
116
117         } elsif( $action eq 'load' ) {
118
119                 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_UPLOAD');
120                 ol_handle_result($evt) if $evt;
121                 $payload = ol_load();
122
123         } elsif( $action eq 'execute' ) {
124
125                 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_EXECUTE');
126                 ol_handle_result($evt) if $evt;
127                 $payload = ol_execute();
128
129         } elsif( $action eq 'status' ) {
130
131                 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_VIEW');
132                 ol_handle_result($evt) if $evt;
133                 $payload = ol_status();
134         }
135
136         ol_handle_event('SUCCESS', payload => $payload );
137 }
138
139
140 # --------------------------------------------------------------------
141 # Creates a new session
142 # --------------------------------------------------------------------
143 sub ol_create_session {
144
145         my $desc = $cgi->param('desc') || "";
146         $seskey = time . "_${$}_" . int(rand() * 1000);
147
148         $logger->debug("offline: user ".$requestor->id.
149                 " creating new session with key $seskey and description $desc");
150
151         $SES->create(
152                 {       
153                         key                             => $seskey,
154                         org                             => $org,
155                         description             => $desc,
156                         creator                 => $requestor->id,
157                         create_time             => CORE::time(), 
158                         num_complete    => 0,
159                 } 
160         );
161
162         return $seskey;
163 }
164
165
166 # --------------------------------------------------------------------
167 # Holds the meta-info for a script file
168 # --------------------------------------------------------------------
169 sub ol_create_script {
170         my $count = shift;
171
172         my $session = ol_find_session($seskey);
173         my $delta = $cgi->param('delta') || 0;
174
175         my $script = $session->add_to_scripts( 
176                 {
177                         requestor       => $requestor->id,
178                         create_time     => CORE::time,
179                         workstation     => $wsname,
180                         logfile         => "$basedir/pending/$org/$seskey/$wsname.log",
181                         time_delta      => $delta,
182                         count                   => $count,
183                 }
184         );
185 }
186
187 # --------------------------------------------------------------------
188 # Finds the current session in the db
189 # --------------------------------------------------------------------
190 sub ol_find_session {
191         my $ses = $SES->retrieve($seskey);
192         ol_handle_event('OFFLINE_INVALID_SESSION', payload => $seskey) unless $ses;
193         return $ses;
194 }
195
196 # --------------------------------------------------------------------
197 # Finds a script object in the DB based on workstation and seskey
198 # --------------------------------------------------------------------
199 sub ol_find_script {
200         my $ws = shift || $wsname;
201         my $sk = shift || $seskey;
202         my ($script) = $SCRIPT->search( session => $seskey, workstation => $ws );
203         return $script;
204 }
205
206 # --------------------------------------------------------------------
207 # Creates a new script in the database and loads the new script file
208 # --------------------------------------------------------------------
209 sub ol_load {
210
211         my $session = ol_find_session;
212         my $handle      = $cgi->upload('file');
213         my $outdir      = "$basedir/pending/$org/$seskey";
214         my $outfile = "$outdir/$wsname.log";
215
216         ol_handle_event('OFFLINE_SESSION_FILE_EXISTS') if ol_find_script();
217         ol_handle_event('OFFLINE_SESSION_ACTIVE') if $session->in_process;
218         ol_handle_event('OFFLINE_SESSION_COMPLETE') if $session->end_time;
219
220         qx/mkdir -p $outdir/;
221         my $x = 0;
222         open(FILE, ">>$outfile") or ol_handle_event('OFFLINE_FILE_ERROR');
223         while( <$handle> ) { print FILE; $x++;}
224         close(FILE);
225
226         ol_create_script($x);
227
228         return undef;
229 }
230
231
232 # --------------------------------------------------------------------
233 sub ol_handle_result {
234         my $obj = shift;
235         my $json = OpenSRF::Utils::JSON->perl2JSON($obj);
236
237         # Clear this so it's not remembered
238         $evt = undef;
239
240         if( $cgi->param('html')) {
241                 my $html = "<html><body onload='xulG.handle_event($json)'></body></html>";
242                 print "content-type: text/html\n\n";
243                 print "$html\n";
244
245         } else {
246
247                 print "content-type: text/plain\n\n";
248                 print "$json\n";
249         }
250
251         exit(0);
252 }
253
254 # --------------------------------------------------------------------
255 sub ol_handle_event {
256         my( $evt, @args ) = @_;
257         ol_handle_result(OpenILS::Event->new($evt, @args));
258 }
259
260
261 # --------------------------------------------------------------------
262 sub ol_flesh_session {
263         my $session = shift;
264         my %data;
265
266         map { $data{$_} = $session->$_ } $session->columns;
267         $data{scripts} = [];
268
269         for my $script ($session->scripts) {
270                 my %sdata;
271                 map { $sdata{$_} = $script->$_ } $script->columns;
272
273                 # the client doesn't need this info
274                 delete $sdata{session};
275                 delete $sdata{id};
276                 delete $sdata{logfile};
277
278                 push( @{$data{scripts}}, \%sdata );
279         }
280
281         return \%data;
282 }
283
284
285 # --------------------------------------------------------------------
286 # Returns various information on the sessions and scripts
287 # --------------------------------------------------------------------
288 sub ol_status {
289
290         my $type = $cgi->param('status_type') || "scripts";
291
292         # --------------------------------------------------------------------
293         # Returns info on every script upload attached to the current session
294         # --------------------------------------------------------------------
295         if( $type eq 'scripts' ) {
296                 my $session = ol_find_session();
297                 ol_handle_result(ol_flesh_session($session));
298
299
300         # --------------------------------------------------------------------
301         # Returns all scripts and sessions for the given org
302         # --------------------------------------------------------------------
303         } elsif( $type eq 'sessions' ) {
304                 my @sessions = $SES->search( org => $org );
305
306                 # can I do this in the DB without raw SQL?
307                 @sessions = sort { $a->create_time <=> $b->create_time } @sessions; 
308                 my @data;
309                 push( @data, ol_flesh_session($_) ) for @sessions;
310                 ol_handle_result(\@data);
311
312
313         # --------------------------------------------------------------------
314         # Returns total commands and completed commands counts
315         # --------------------------------------------------------------------
316         } elsif( $type eq 'summary' ) {
317                 my $session = ol_find_session();
318
319                 $logger->debug("offline: retrieving summary info ".
320                         "for session ".$session->key." with completed=".$session->num_complete);
321
322                 my $count = 0;
323                 $count += $_->count for ($session->scripts);
324                 ol_handle_result(
325                         { total => $count, num_complete => $session->num_complete });
326
327
328
329         # --------------------------------------------------------------------
330         # Returns the list of non-SUCCESS events that have occurred so far for 
331         # this set of commands
332         # --------------------------------------------------------------------
333         } elsif( $type eq 'exceptions' ) {
334
335                 my $session = ol_find_session();
336                 my $resfile = "$basedir/pending/$org/$seskey/results";
337                 if( $session->end_time ) {
338                         $resfile = "$basedir/archive/$org/$seskey/results";
339                 }
340                 my $data = ol_file_to_perl($resfile);
341                 $data = [ grep { $_->{event}->{ilsevent} ne '0' } @$data ];
342                 ol_handle_result($data);
343         }
344 }
345
346
347 sub ol_fetch_workstation {
348         my $name = shift;
349         $logger->debug("offline: Fetching workstation $name");
350         my $ws = $U->storagereq(
351                 'open-ils.storage.direct.actor.workstation.search.name', $name);
352         ol_handle_result(OpenILS::Event->new('ACTOR_WORKSTATION_NOT_FOUND')) unless $ws;
353         return $ws;
354 }
355
356
357
358
359 # --------------------------------------------------------------------
360 # Sorts the script commands then forks a child to executes them.
361 # --------------------------------------------------------------------
362 sub ol_execute {
363
364         my $session = ol_find_session();
365         ol_handle_event('OFFLINE_SESSION_ACTIVE') if $session->in_process;
366         ol_handle_event('OFFLINE_SESSION_COMPLETE') if $session->end_time;
367
368         my $commands = ol_collect_commands();
369
370         # --------------------------------------------------------------------
371         # Note that we must disconnect from opensrf before forking or the 
372         # connection will be borked...
373         # --------------------------------------------------------------------
374         OpenSRF::Transport::PeerHandle->retrieve->disconnect;
375         $DB->disconnect;
376
377
378         if( safe_fork() ) {
379
380                 # --------------------------------------------------------------------
381                 # Tell the client all is well
382                 # --------------------------------------------------------------------
383                 ol_handle_event('SUCCESS'); # - this exits
384
385         } else {
386
387
388                 # --------------------------------------------------------------------
389                 # close stdout/stderr or apache will wait on the child to finish
390                 # --------------------------------------------------------------------
391                 close(STDOUT);
392                 close(STDERR);
393
394                 $logger->debug("offline: child $$ processing data...");
395
396                 # --------------------------------------------------------------------
397                 # The child re-connects to the opensrf network and processes
398                 # the script requests 
399                 # --------------------------------------------------------------------
400                 OpenSRF::System->bootstrap_client(config_file => $bootstrap);
401         
402                 try {
403
404                         #use Class::DBI
405                         #Class::DBI->autoupdate(1);
406
407                         $DB->autoupdate(1);
408
409                         my $sesion = ol_find_session();
410                         $session->in_process(1);
411                         ol_process_commands($session, $commands);
412                         ol_archive_files($session);
413
414                 } catch Error with {
415                         my $e = shift;
416                         $logger->error("offline: child process error $e");
417                 };
418         }
419 }
420
421 sub ol_file_to_perl {
422         my $fname = shift;
423         open(F, "$fname") or ol_handle_event('OFFLINE_FILE_ERROR');
424         my @d = <F>;
425         my @p;
426         push(@p, OpenSRF::Utils::JSON->JSON2perl($_)) for @d;
427         close(F);
428         return \@p;
429 }
430
431 # collects the commands and sorts them on timestamp+delta
432 sub ol_collect_commands {
433         my $ses = ol_find_session();
434         my @commands;
435
436         # cycle through every script loaded to this session
437         for my $script ($ses->scripts) {
438                 my $coms = ol_file_to_perl($script->logfile);
439
440                 # cycle through all of the commands for this script
441                 for my $com (@$coms) {
442                         $$com{_workstation} = $script->workstation;
443                         $$com{_realtime} = $script->time_delta + $com->{timestamp};
444                         push( @commands, $com );
445                 }
446         }
447
448         # make sure thera are no blank commands
449         @commands = grep { ($_ and $_->{type}) } @commands;
450
451         # sort on realtime
452         @commands = sort { $a->{_realtime} <=> $b->{_realtime} } @commands;
453
454         # push user registrations to the front
455         my @regs                = grep { $_->{type} eq 'register' } @commands;
456         my @others      = grep { $_->{type} ne 'register' } @commands;
457
458         return [ @regs, @others ];
459 }
460
461 sub ol_date {
462         my $time = shift || CORE::time;
463         my (undef,undef, undef, $mday,$mon,$year) = localtime($time);
464         $mon++; $year   += 1900;
465         $mday   = "0$mday" unless $mday =~ /\d{2}/o;
466         $mon    = "0$mon" unless $mon   =~ /\d{2}/o;
467         return ($year, $mon, $mday);
468 }
469
470
471 # --------------------------------------------------------------------
472 # Moves all files from the pending directory to the archive directory
473 # and removes the pending directory
474 # --------------------------------------------------------------------
475 sub ol_archive_files {
476         my $session = shift;
477         my ($y, $m, $d) = ol_date();
478
479         my $dir = "$basedir/pending/$org/$seskey";
480         my $archdir = "$basedir/archive/$org/$seskey";
481         $logger->debug("offline: archiving files to $archdir");
482
483         # Tell the db the files are moving
484         $_->logfile($archdir.'/'.$_->workstation.'.log') for ($session->scripts);
485
486         qx/mkdir -p $archdir/;
487         qx/mv $_ $archdir/ for <$dir/*>;
488         qx/rmdir $dir/;
489 }
490
491
492 # --------------------------------------------------------------------
493 # Appends results to the results file.
494 # --------------------------------------------------------------------
495 my $rhandle;
496 sub ol_append_result {
497
498         my $obj = shift;
499         my $last = shift;
500
501         $obj = OpenSRF::Utils::JSON->perl2JSON($obj);
502
503         if(!$rhandle) {
504                 open($rhandle, ">>$basedir/pending/$org/$seskey/results") 
505                         or ol_handle_event('OFFLINE_FILE_ERROR');
506         }
507
508         print $rhandle "$obj\n";
509         close($rhandle) if $last;
510 }
511
512
513
514 # --------------------------------------------------------------------
515 # Runs the commands and returns the list of errors
516 # --------------------------------------------------------------------
517 sub ol_process_commands {
518
519         my $session      = shift;
520         my $commands = shift;
521         my $x        = 0;
522
523         $session->start_time(CORE::time);
524
525         for my $d ( @$commands ) {
526
527                 my $t           = $d->{type};
528                 my $last = ($x++ == scalar(@$commands) - 1) ? 1 : 0;
529                 my $res = { command => $d };
530                 my $err;
531
532                 while( 1 ) {
533
534                         $err = undef;
535                         $logger->debug("offline: top of execute loop : $t");
536
537                         try {
538                                 $res->{event} = ol_handle_checkin($d)   if $t eq 'checkin';
539                                 $res->{event} = ol_handle_inhouse($d)   if $t eq 'in_house_use';
540                                 $res->{event} = ol_handle_checkout($d) if $t eq 'checkout';
541                                 $res->{event} = ol_handle_renew($d)             if $t eq 'renew';
542                                 $res->{event} = ol_handle_register($d) if $t eq 'register';
543         
544                         } catch Error with { $err = shift; };
545
546                         if( $err ) {
547
548                                 if( ref($err) eq 'OpenSRF::EX::JabberDisconnected' ) {
549                                         $logger->error("offline: we lost jabber .. trying to reconnect");
550                                         ol_connect();
551
552                                 } else {
553                                         $res->{event} = OpenILS::Event->new('INTERNAL_SERVER_ERROR', debug => "$err");
554                                         last;
555                                 }
556
557                         } else { last; }
558
559                         sleep(1);
560                 }
561
562                 ol_append_result($res, $last);
563                 $session->num_complete( $session->num_complete + 1 );
564
565                 $logger->debug("offline: set session [".$session->key."] num_complete to ".$session->num_complete);
566         }
567
568         $session->end_time(CORE::time);
569         $session->in_process(0);
570 }
571
572
573 # --------------------------------------------------------------------
574 # Runs an in_house_use action
575 # --------------------------------------------------------------------
576 sub ol_handle_inhouse {
577
578         my $command             = shift;
579         my $realtime    = $command->{_realtime};
580         my $ws                  = $command->{_workstation};
581         my $barcode             = $command->{barcode};
582         my $count               = $command->{count} || 1;
583         my $use_time    = $command->{use_time} || "";
584
585         $logger->activity("offline: in_house_use : requestor=". $requestor->id.", realtime=$realtime, ".  
586                 "workstation=$ws, barcode=$barcode, count=$count, use_time=$use_time");
587
588         if( $count > 99 ) {
589                 return OpenILS::Event->new(
590                         'INTERNAL_SERVER_ERROR', payload => 'TOO MANY IN HOUSE USE');
591         }
592
593         my $ids = $U->simplereq(
594                 'open-ils.circ', 
595                 'open-ils.circ.in_house_use.create', $authtoken, 
596                 { barcode => $barcode, count => $count, location => $org, use_time => $use_time } );
597         
598         return OpenILS::Event->new('SUCCESS', payload => $ids) if( ref($ids) eq 'ARRAY' );
599         return $ids;
600 }
601
602
603
604 # --------------------------------------------------------------------
605 # Pulls the relevant circ args from the command, fetches data where 
606 # necessary
607 # --------------------------------------------------------------------
608 sub ol_circ_args_from_command {
609         my $command = shift;
610
611         my $type                        = $command->{type};
612         my $realtime    = $command->{_realtime};
613         my $ws                  = $command->{_workstation};
614         my $barcode             = $command->{barcode} || "";
615         my $cotime              = $command->{checkout_time} || "";
616         my $pbc                 = $command->{patron_barcode};
617         my $due_date    = $command->{due_date} || "";
618         my $noncat              = ($command->{noncat}) ? "yes" : "no"; # for logging
619
620         $logger->activity("offline: $type : requestor=". $requestor->id.
621                 ", realtime=$realtime, workstation=$ws, checkout_time=$cotime, ".
622                 "patron=$pbc, due_date=$due_date, noncat=$noncat");
623
624         my $args = { 
625                 permit_override => 1, 
626                 barcode                         => $barcode,            
627                 checkout_time           => $cotime, 
628                 patron_barcode          => $pbc,
629                 due_date                                => $due_date };
630
631         if( $command->{noncat} ) {
632                 $args->{noncat} = 1;
633                 $args->{noncat_type} = $command->{noncat_type};
634                 $args->{noncat_count} = $command->{noncat_count};
635         }
636
637         return $args;
638 }
639
640
641
642 # --------------------------------------------------------------------
643 # Performs a checkout action
644 # --------------------------------------------------------------------
645 sub ol_handle_checkout {
646         my $command     = shift;
647         my $args = ol_circ_args_from_command($command);
648
649         if( $args->{noncat} and $args->{noncat_count} > 99 ) {
650                 return OpenILS::Event->new(
651                         'INTERNAL_SERVER_ERROR', payload => 'TOO MANY NON CATS');
652         }
653
654     if( $args->{barcode} ) {
655         my( $c, $e ) = $U->fetch_copy_by_barcode($args->{barcode});
656         return $e if $e;
657     }
658
659         return $U->simplereq(
660                 'open-ils.circ', 'open-ils.circ.checkout', $authtoken, $args );
661 }
662
663
664 # --------------------------------------------------------------------
665 # Performs the renewal action
666 # --------------------------------------------------------------------
667 sub ol_handle_renew {
668         my $command = shift;
669         my $args = ol_circ_args_from_command($command);
670         my $t = time;
671         return $U->simplereq(
672                 'open-ils.circ', 'open-ils.circ.renew', $authtoken, $args );
673 }
674
675
676 # --------------------------------------------------------------------
677 # Runs a checkin action
678 # --------------------------------------------------------------------
679 sub ol_handle_checkin {
680
681         my $command             = shift;
682         my $realtime    = $command->{_realtime};
683         my $ws                  = $command->{_workstation};
684         my $barcode             = $command->{barcode};
685         my $backdate    = $command->{backdate} || "";
686
687         $logger->activity("offline: checkin : requestor=". $requestor->id.
688                 ", realtime=$realtime, ".  "workstation=$ws, barcode=$barcode, backdate=$backdate");
689
690         return $U->simplereq(
691                 'open-ils.circ', 
692                 'open-ils.circ.checkin', $authtoken,
693                 { barcode => $barcode, backdate => $backdate } );
694 }
695
696
697
698 # --------------------------------------------------------------------
699 # Registers a new patron
700 # --------------------------------------------------------------------
701 sub ol_handle_register {
702         my $command = shift;
703
704         my $barcode = $command->{user}->{card}->{barcode};
705         delete $command->{user}->{card}; 
706
707         $logger->info("offline: creating new user with barcode $barcode");
708
709         # now, create the user
710         my $actor       = Fieldmapper::actor::user->new;
711         my $card                = Fieldmapper::actor::card->new;
712
713
714         # username defaults to the barcode
715         $actor->usrname( ($actor->usrname) ? $actor->usrname : $barcode );
716
717         # Set up all of the virtual IDs, isnew, etc.
718         $actor->isnew(1);
719         $actor->id(-1);
720         $actor->card(-1);
721         $actor->cards([$card]);
722
723         $card->isnew(1);
724         $card->id(-1);
725         $card->usr(-1);
726         $card->barcode($barcode);
727
728         my $billing_address;
729         my $mailing_address;
730
731         my @sresp;
732         for my $resp (@{$command->{user}->{survey_responses}}) {
733                 my $sr = Fieldmapper::action::survey_response->new;
734                 $sr->$_( $resp->{$_} ) for keys %$resp;
735                 $sr->isnew(1);
736                 $sr->usr(-1);
737                 push(@sresp, $sr);
738                 $logger->debug("offline: created new survey response for survey ".$sr->survey);
739         }
740         delete $command->{user}->{survey_responses};
741         $actor->survey_responses(\@sresp) if @sresp;
742
743         # extract the billing address
744         if( my $addr = $command->{user}->{billing_address} ) {
745                 $billing_address = Fieldmapper::actor::user_address->new;
746                 $billing_address->$_($addr->{$_}) for keys %$addr;
747                 $billing_address->isnew(1);
748                 $billing_address->id(-1);
749                 $billing_address->usr(-1);
750                 delete $command->{user}->{billing_address};
751                 $logger->debug("offline: read billing address ".$billing_address->street1);
752         }
753
754         # extract the mailing address
755         if( my $addr = $command->{user}->{mailing_address} ) {
756                 $mailing_address = Fieldmapper::actor::user_address->new;
757                 $mailing_address->$_($addr->{$_}) for keys %$addr;
758                 $mailing_address->isnew(1);
759                 $mailing_address->id(-2);
760                 $mailing_address->usr(-1);
761                 delete $command->{user}->{mailing_address};
762                 $logger->debug("offline: read mailing address ".$mailing_address->street1);
763         }
764
765         # make sure we have values for both
766         $billing_address ||= $mailing_address;
767         $mailing_address ||= $billing_address;
768
769         $actor->billing_address($billing_address->id);
770         $actor->mailing_address($mailing_address->id);
771         $actor->addresses([$mailing_address]);
772
773         push( @{$actor->addresses}, $billing_address ) 
774                 unless $billing_address->id eq $mailing_address->id;
775         
776         # pull all of the rest of the data from the command blob
777         $actor->$_( $command->{user}->{$_} ) for keys %{$command->{user}};
778
779     # calculate the expire date for the patron based on the profile group
780     my ($grp) = grep {$_->id == $actor->profile} @$user_groups;
781     if($grp) {
782         my $seconds = OpenSRF::Utils->interval_to_seconds($grp->perm_interval);
783         my $expire_date = DateTime->from_epoch(epoch => DateTime->now->epoch + $seconds);
784                 $logger->debug("offline: setting expire date to $expire_date");
785         $actor->expire_date($U->epoch2ISO8601($expire_date));
786     }
787
788         $logger->debug("offline: creating user object...");
789         $actor = $U->simplereq(
790                 'open-ils.actor', 
791                 'open-ils.actor.patron.update', $authtoken, $actor);
792
793         return $actor if(ref($actor) eq 'HASH'); # an event occurred
794
795         return OpenILS::Event->new('SUCCESS', payload => $actor);
796 }
797
798
799
800
801
802
803