+# we distinguish between invalid new data (return error) versus data that
+# has legitimately changed (may eventually execute hooks; now just update)
+# if we do add hooks later, we should work on a copy of svc_dsl and pass
+# the old and new svc_dsl to the hooks so they know what changed
+#
+# current assumptions of what won't change (from their side):
+# vendor_order_id, vendor_qual_id, vendor_order_type, pushed, monitored,
+# last_pull, address (from qual), contact info, ProductCustomId
+ my($self, $svc_dsl, $threshold, $import) = (shift, shift, shift, shift);
+
+ return 'Invalid operation - Import Mode is enabled'
+ if $self->import_mode && !$import;
+
+ return 'invalid arguments' if $import && !$self->import_mode;
+
+ warn "$me dsl_pull: import mode" if $self->option('debug');
+
+ unless ( $import ) {
+ my $result = $self->valid_order($svc_dsl,'pull');
+ return $result unless $result eq '';
+ }
+
+ my $now = time;
+ if($now - $svc_dsl->last_pull < $threshold) {
+ warn "$me skipping pull since threshold not reached (svcnum="
+ . $svc_dsl->svcnum . ",now=$now,threshold=$threshold,last_pull="
+ . $svc_dsl->last_pull .")" if $self->option('debug');
+ return '';
+ }
+
+ my $result = $self->ikano_command('ORDERSTATUS',
+ { OrderId => $svc_dsl->vendor_order_id } );
+ return $result unless ref($result); # scalar (string) is an error
+
+ # now we're getting an OrderResponse which should have one Order in it
+ warn "$me pull OrderResponse hash:\n".Dumper($result)
+ if $self->option('debug');
+
+ return 'Invalid order response' unless defined $result->{'Order'};
+ $result = $result->{'Order'};
+
+ return 'No order id or status returned'
+ unless defined $result->{'Status'} && defined $result->{'OrderId'};
+
+ local $SIG{HUP} = 'IGNORE';
+ local $SIG{INT} = 'IGNORE';
+ local $SIG{QUIT} = 'IGNORE';
+ local $SIG{TERM} = 'IGNORE';
+ local $SIG{TSTP} = 'IGNORE';
+ local $SIG{PIPE} = 'IGNORE';
+
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+ my $dbh = dbh;
+
+ # 1. status
+ my $order_status = grep($_ eq $result->{'Status'}, @Net::Ikano::orderStatus)
+ ? $result->{'Status'} : '';
+ return 'Invalid new status' if $order_status eq '';
+ $svc_dsl->vendor_order_status($order_status)
+ if($svc_dsl->vendor_order_status ne $order_status);
+ $svc_dsl->monitored('')
+ if ($order_status eq 'CANCELLED' || $order_status eq 'COMPLETED');
+
+ # 2. fields we don't care much about
+ my %justUpdate = ( 'first' => 'FirstName',
+ 'last' => 'LastName',
+ 'company' => 'CompanyName',
+ 'username' => 'Username',
+ 'password' => 'Password' );
+
+ my($fsf, $ikanof);
+ while (($fsf, $ikanof) = each %justUpdate) {
+ $svc_dsl->$fsf($result->{$ikanof})
+ if $result->{$ikanof} ne $svc_dsl->$fsf;
+ }
+
+ # let's look inside the <Product> response element
+ my @product = $result->{'Product'};
+ return 'Invalid number of products on order' if scalar(@product) != 1;
+ my $product = $result->{'Product'}[0];
+
+ $svc_dsl->vendor_order_type('NEW') if $import; # lame
+
+ # 3. phonenum
+ if ( $import ) {
+ if ( $product->{'PhoneNumber'} eq 'STANDALONE' ) {
+ $svc_dsl->phonenum($product->{'VirtualPhoneNumber'});
+ $svc_dsl->loop_type('0');
+ }
+ else {
+ $svc_dsl->phonenum($product->{'PhoneNumber'});
+ $svc_dsl->loop_type('');
+ }
+ }
+ elsif($svc_dsl->loop_type eq '') { # line-share
+# TN may change only if sub changes it and New or Change order in Completed status
+ my $tn = $product->{'PhoneNumber'};
+ if($tn ne $svc_dsl->phonenum) {
+ if( ($svc_dsl->vendor_order_type eq 'NEW'
+ || $svc_dsl->vendor_order_type eq 'CHANGE')
+ && $svc_dsl->vendor_order_status eq 'COMPLETED' ) {
+ $svc_dsl->phonenum($tn);
+ }
+ else { return 'TN has changed in an invalid state'; }
+ }
+ }
+ elsif($svc_dsl->loop_type eq '0') { # dry loop
+# TN may change only if it's assigned while a New or Change order is in progress
+ return 'Invalid PhoneNumber value for a dry loop'
+ if $product->{'PhoneNumber'} ne 'STANDALONE';
+ my $tn = $product->{'VirtualPhoneNumber'};
+ if($tn ne $svc_dsl->phonenum) {
+ if( ($svc_dsl->vendor_order_type eq 'NEW'
+ || $svc_dsl->vendor_order_type eq 'CHANGE')
+ && $svc_dsl->vendor_order_status ne 'COMPLETED'
+ && $svc_dsl->vendor_order_status ne 'CANCELLED') {
+ $svc_dsl->phonenum($tn);
+ }
+ else { return 'TN has changed in an invalid state'; }
+ }
+ }
+
+ # 4. desired_due_date - may change if manually changed
+ if($svc_dsl->vendor_order_type eq 'NEW'
+ || $svc_dsl->vendor_order_type eq 'CHANGE'){
+ my $f = str2time($product->{'DateToOrder'});
+ return 'Invalid DateToOrder' unless $f;
+ $svc_dsl->desired_due_date($f) if $svc_dsl->desired_due_date ne $f;
+ # XXX: optionally sync back to start_date or whatever...
+ }
+ elsif($svc_dsl->vendor_order_type eq 'CANCEL'){
+ my $f = str2time($product->{'DateToDisconnect'});
+ return 'Invalid DateToDisconnect' unless $f;
+ $svc_dsl->desired_due_date($f) if $svc_dsl->desired_due_date ne $f;
+ # XXX: optionally sync back to expire or whatever...
+ }
+
+ # 5. due_date
+ if($svc_dsl->vendor_order_type eq 'NEW'
+ || $svc_dsl->vendor_order_type eq 'CHANGE') {
+ my $f = str2time($product->{'ActivationDate'});
+ if($svc_dsl->vendor_order_status ne 'NEW'
+ && $svc_dsl->vendor_order_status ne 'CANCELLED') {
+ return 'Invalid ActivationDate' unless $f;
+ $svc_dsl->due_date($f) if $svc_dsl->due_date ne $f;
+ }
+ }
+ # Ikano API does not implement the proper disconnect date,
+ # so we can't do anything about it
+
+ # 6. staticips - for now just comma-separate them
+ my $tstatics = $result->{'StaticIps'};
+ my @istatics = defined $tstatics ? @$tstatics : ();
+ my $ostatics = $svc_dsl->staticips;
+ my @ostatics = split(',',$ostatics);
+ # more horrible search/sync code below...
+ my $staticsChanged = 0;
+ foreach my $istatic ( @istatics ) { # they have, we don't
+ unless ( grep($_ eq $istatic, @ostatics) ) {
+ push @ostatics, $istatic;
+ $staticsChanged = 1;
+ }
+ }
+ for(my $i=0; $i < scalar(@ostatics); $i++) {
+ unless ( grep($_ eq $ostatics[$i], @istatics) ) {
+ splice(@ostatics,$i,1);
+ $i--;
+ $staticsChanged = 1;
+ }
+ }
+ $svc_dsl->staticips(join(',',@ostatics)) if $staticsChanged;
+
+ # 7. notes - put them into the common format and compare
+ my $tnotes = $result->{'OrderNotes'};
+ my @tnotes = defined $tnotes ? @$tnotes : ();
+ my @inotes = (); # all Ikano OrderNotes as FS::dsl_note objects
+ my $notesChanged = 0;
+ foreach my $tnote ( @tnotes ) {
+ my $inote = $self->ikano2fsnote($tnote,$svc_dsl->svcnum);
+ return 'Cannot parse note' unless ref($inote);
+ push @inotes, $inote;
+ }
+ my @onotes = $svc_dsl->notes;
+ # assume notes we already have don't change & no notes added from our side
+ # so using the horrible code below just find what we're missing and add it
+ foreach my $inote ( @inotes ) {
+ my $found = 0;
+ foreach my $onote ( @onotes ) {
+ if($onote->date == $inote->date && $onote->note eq $inote->note) {
+ $found = 1;
+ last;
+ }
+ }
+ my $error = $inote->insert unless ( $found );
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ return "Cannot add note: $error";
+ }
+ }
+
+ $svc_dsl->last_pull((time));
+ local $FS::svc_Common::noexport_hack = 1;
+ my $error = $svc_dsl->replace;
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ return "Cannot update DSL data: $error";
+ }
+
+ $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+