import torrus 1.0.9
[freeside.git] / FS / FS / part_export / ikano.pm
index 91953e3..fe645e4 100644 (file)
@@ -1,17 +1,17 @@
 package FS::part_export::ikano;
 
-use vars qw(@ISA %info %orderType %orderStatus %loopType $DEBUG $me);
+use strict;
+use warnings;
+use vars qw(@ISA %info %loopType $me);
 use Tie::IxHash;
 use Date::Format qw( time2str );
 use Date::Parse qw( str2time );
 use FS::Record qw(qsearch qsearchs dbh);
 use FS::part_export;
 use FS::svc_dsl;
-use FS::dsl_note;
 use Data::Dumper;
 
 @ISA = qw(FS::part_export);
-$DEBUG = 1;
 $me= '[' .  __PACKAGE__ . ']';
 
 tie my %options, 'Tie::IxHash',
@@ -23,6 +23,7 @@ tie my %options, 'Tie::IxHash',
   'check_networks' => { label => 'Check Networks',
                    default => 'ATT,BELLCA',
                    },
+  'debug' => { label => 'Debug Mode',  type => 'checkbox' },
 ;
 
 %info = (
@@ -35,16 +36,12 @@ Requires installation of
 END
 );
     
-%orderType = ( 'N' => 'NEW', 'X' => 'CANCEL', 'C' => 'CHANGE' );
-%orderStatus = ('N' => 'NEW',
-               'P' => 'PENDING',
-               'X' => 'CANCELLED',
-               'C' => 'COMPLETED',
-               'E' => 'ERROR' );
 %loopType = ( '' => 'Line-share', '0' => 'Standalone' );
 
 sub rebless { shift; }
 
+sub external_pkg_map { 1; }
+
 sub dsl_pull {
 # we distinguish between invalid new data (return error) versus data that
 # has legitimately changed (may eventually execute hooks; now just update)
@@ -54,17 +51,25 @@ sub dsl_pull {
 # 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) = (shift, shift);
+    my($self, $svc_dsl, $threshold) = (shift, shift, shift);
     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 '';
+    }
   
     $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 $DEBUG;
+    warn "$me pull OrderResponse hash:\n".Dumper($result) 
+       if $self->option('debug');
   
     return 'Invalid order response' unless defined $result->{'Order'};
     $result = $result->{'Order'};
@@ -84,13 +89,13 @@ sub dsl_pull {
     my $dbh = dbh;
 
     # 1. status 
-    my $new_order_status = $self->orderstatus_long2short($result->{'Status'});
-    return 'Invalid new status' if $new_order_status eq '';
-    if($svc_dsl->vendor_order_status ne $new_order_status) {
-       $svc_dsl->monitored(''
-           if ($new_order_status eq 'X' || $new_order_status eq 'C');
-       $svc_dsl->vendor_order_status($new_order_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',
@@ -99,6 +104,7 @@ sub dsl_pull {
                    'username' => 'Username',
                    'password' => 'Password' );
 
+    my($fsf, $ikanof);
     while (($fsf, $ikanof) = each %justUpdate) {
        $svc_dsl->$fsf($result->{$ikanof}) 
            if $result->{$ikanof} ne $svc_dsl->$fsf;
@@ -114,9 +120,9 @@ sub dsl_pull {
 # 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 'N' 
-               || $svc_dsl->vendor_order_type eq 'C')
-              && $svc_dsl->vendor_order_status eq 'C' ) {
+           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'; }
@@ -128,10 +134,10 @@ sub dsl_pull {
            if $product->{'PhoneNumber'} ne 'STANDALONE';
        my $tn = $product->{'VirtualPhoneNumber'};
        if($tn ne $svc_dsl->phonenum) {
-           if( ($svc_dsl->vendor_order_type eq 'N' 
-               || $svc_dsl->vendor_order_type eq 'C')
-             && $svc_dsl->vendor_order_status ne 'C'
-             && $svc_dsl->vendor_order_status ne 'X') {
+           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'; }
@@ -139,42 +145,61 @@ sub dsl_pull {
     }
     
     # 4. desired_due_date - may change if manually changed
-    if($svc_dsl->vendor_order_type eq 'N' 
-           || $svc_dsl->vendor_order_type eq 'C'){
+    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 != $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 'X'){
+    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 != $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 'N' 
-         || $svc_dsl->vendor_order_type eq 'C') {
+    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 'N') {
+       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 != $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 @statics = $result->{'StaticIps'};
-# XXX
+    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 = @$tnotes;
+    my @tnotes = defined $tnotes ? @$tnotes : ();
     my @inotes = (); # all Ikano OrderNotes as FS::dsl_note objects
     my $notesChanged = 0; 
-    foreach $tnote ( @tnotes ) {
+    foreach my $tnote ( @tnotes ) {
        my $inote = $self->ikano2fsnote($tnote,$svc_dsl->svcnum);
        return 'Cannot parse note' unless ref($inote);
        push @inotes, $inote;
@@ -183,9 +208,9 @@ sub dsl_pull {
     # 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
     my $error;
-    foreach $inote ( @inotes ) {
+    foreach my $inote ( @inotes ) {
        my $found = 0;
-       foreach $onote ( @onotes ) {
+       foreach my $onote ( @onotes ) {
            if($onote->date == $inote->date && $onote->note eq $inote->note) {
                $found = 1;
                last;
@@ -224,17 +249,99 @@ sub ikano2fsnote {
     $by = "Ikano" if $n->{'CompanyStaffId'} == -1 && $n->{'StaffId'} != -1;
     $by = "Us" if $n->{'StaffId'} == -1 && $n->{'CompanyStaffId'} != -1;
 
-    $fsnote = new FS::dsl_note( {
+    new FS::dsl_note( {
        'svcnum' => $svcnum,
-       'by' => $by,
+       'author' => $by,
        'priority' => $n->{'HighPriority'} eq 'false' ? 'N' : 'H',
-       'date' => int(str2time($n->{'Date'})),
+       '_date' => int(str2time($n->{'Date'})),
        'note' => $n->{'Text'},
      } );
 }
 
 sub qual {
-    '';
+    my($self,$qual) = (shift,shift);
+# address always required for Ikano qual, TN optional (assume dry if not given)
+
+    my %location_hash = $qual->location; 
+    return 'No address provided' unless %location_hash;
+    my $svctn = $qual->phonenum;
+
+    my $result = $self->ikano_command('PREQUAL',
+      { AddressLine1 => $location_hash{address1},
+       AddressUnitType => $location_hash{location_type},
+       AddressUnitValue => $location_hash{location_number},
+       AddressCity => $location_hash{city},
+       AddressState => $location_hash{state},
+       ZipCode => $location_hash{zip},
+       Country => $location_hash{country},
+       LocationType => $location_hash{location_kind},
+       PhoneNumber => length($svctn) > 1 ? $svctn : "STANDALONE",
+       RequestClientIP => '127.0.0.1',
+       CheckNetworks => $self->option('check_networks'),
+      } ); 
+    return $result unless ref($result); # error case
+    return 'Invalid prequal response' unless defined $result->{'PrequalId'};
+
+    my $qoptions = {};
+    # lame data structure traversal...
+    # don't spend much time here, just get TermsId and ProductCustomId
+    my $networks = $result->{'Network'};
+    my @networks = defined $networks ? @$networks : ();
+    my $netcount = 0;
+    foreach my $network ( @networks ) { 
+       my $productgroups = $network->{'ProductGroup'};
+       my @productgroups = defined $productgroups ? @$productgroups : ();
+       my $pgcount = 0;
+       foreach my $productgroup ( @productgroups ) {
+           my $prefix = "ikano_Network_$netcount"."_ProductGroup_$pgcount"."_";
+           $qoptions->{$prefix."TermsId"} = $productgroup->{'TermsId'};
+           my $products = $productgroup->{'Product'};
+           my @products = defined $products ? @$products : ();
+           my $prodcount = 0;
+           foreach my $product ( @products ) {
+               $qoptions->{$prefix."Product_$prodcount"."_ProductCustomId"} = $product->{'ProductCustomId'};
+               $prodcount++;
+           }
+           $pgcount++;
+       }
+       $netcount++;
+    }
+
+    {  'vendor_qual_id' => $result->{'PrequalId'},
+       'status' => scalar(@networks) ? 'Q' : 'D',
+       'options' => $qoptions,
+    };
+}
+
+sub qual_html {
+    my($self,$qual) = (shift,shift);
+    
+    my %qual_options = $qual->options;
+    my @externalids = ();
+    my( $optionname, $optionvalue );
+    while (($optionname, $optionvalue) = each %qual_options) {
+       push @externalids, $optionvalue 
+           if ( $optionname =~ /^ikano_Network_(\d+)_ProductGroup_(\d+)_Product_(\d+)_ProductCustomId$/
+               && $optionvalue ne '' );
+    }
+
+    # XXX: eventually perhaps this should return both the packages a link to
+    # order each package and go to the svc prov with the prequal id filled in
+    # but only if cust, not prospect!
+    my $list = "<B>Qualifying Packages:</B><UL>";
+    my @part_pkgs = qsearch( 'part_pkg', { 'disabled' => '' } );
+    foreach my $part_pkg ( @part_pkgs ) {
+       my %vendor_pkg_ids = $part_pkg->vendor_pkg_ids;
+       my $externalid = $vendor_pkg_ids{$self->exportnum} 
+           if defined $vendor_pkg_ids{$self->exportnum};
+       if ( $externalid ) {
+           $list .= "<LI>".$part_pkg->pkgpart.": ".$part_pkg->pkg." - "
+               .$part_pkg->comment."</LI>" 
+             if grep( $_ eq $externalid, @externalids );
+       }
+    }
+    $list .= "</UL>";
+    $list;
 }
 
 sub notes_html { 
@@ -244,7 +351,7 @@ sub notes_html {
     my @notes = $svc_dsl->notes;
     my $html = '<TABLE border="1" cellspacing="2" cellpadding="2" id="dsl_notes">
        <TR><TH>Date</TH><TH>By</TH><TH>Priority</TH><TH>Note</TH></TR>';
-    foreach $note ( @notes ) {
+    foreach my $note ( @notes ) {
        $html .= "<TR>
            <TD>".time2str("$date_format %H:%M",$note->date)."</TD>
            <TD>".$note->by."</TD>
@@ -260,60 +367,64 @@ sub loop_type_long { # sub, not a method
     return $loopType{$svc_dsl->loop_type};
 }
 
-sub status_line {
-    my($self,$svc_dsl) = (shift,shift);
-    return "Ikano ".$orderType{$svc_dsl->vendor_order_type}." order #"
-       . $svc_dsl->vendor_order_id . " (Status: " 
-       . $orderStatus{$svc_dsl->vendor_order_status} . ")";
-}
-
 sub ikano_command {
   my( $self, $command, $args ) = @_;
 
-  eval "use Net::Ikano;";
-  die $@ if $@;
+  $self->loadmod;
 
   my $ikano = Net::Ikano->new(
     'keyid' => $self->option('keyid'),
     'username'  => $self->option('username'),
     'password'  => $self->option('password'),
-    'debug'    => 1,
-    #'reqpreviewonly' => 1,
+    'debug'    => $self->option('debug'),
   );
 
   $ikano->$command($args);
 }
 
+sub loadmod {
+  eval "use Net::Ikano;";
+  die $@ if $@;
+}
+
 sub valid_order {
   my( $self, $svc_dsl, $action ) = (shift, shift, shift);
+  $self->loadmod;
   
-  warn "$me valid_order action=$action svc_dsl:\n". Dumper($svc_dsl) if $DEBUG;
+  warn "$me valid_order action=$action svc_dsl:\n". Dumper($svc_dsl)
+       if $self->option('debug');
 
   # common to all order types/status/loop_type
   my $error = !($svc_dsl->desired_due_date
-           &&  defined $orderType{$svc_dsl->vendor_order_type}
+           &&  grep($_ eq $svc_dsl->vendor_order_type, Net::Ikano->orderTypes)
            &&  $svc_dsl->first
            &&  $svc_dsl->last
            &&  defined $svc_dsl->loop_type
            &&  $svc_dsl->vendor_qual_id
            );
   return 'Missing or invalid order data' if $error;
-  
+  my %vendor_pkg_ids = $svc_dsl->cust_svc->cust_pkg->part_pkg->vendor_pkg_ids;
   return 'Package does not have an external id configured'
-    if $svc_dsl->cust_svc->cust_pkg->part_pkg->options('externalid',1) eq '';
+    unless defined $vendor_pkg_ids{$self->exportnum};
 
   return 'No valid qualification for this order' 
     unless qsearch( 'qual', { 'vendor_qual_id' => $svc_dsl->vendor_qual_id });
 
   # now go by order type
   # weird ifs & long lines for readability and ease of understanding - don't change
-  if($svc_dsl->vendor_order_type eq 'N') {
+  if($svc_dsl->vendor_order_type eq 'NEW') {
     if($svc_dsl->pushed) {
-       $error = !($action eq 'pull'
+       $error = !( ($action eq 'pull' || $action eq 'statuschg' 
+                       || $action eq 'delete' || $action eq 'expire')
            &&  length($svc_dsl->vendor_order_id) > 0
            &&  length($svc_dsl->vendor_order_status) > 0
                );
        return 'Invalid order data' if $error;
+
+       return 'Phone number required for status change'
+           if ($action eq 'statuschg' && length($svc_dsl->phonenum) < 1);
     }
     else { # unpushed New order - cannot do anything other than push it
        $error = !($action eq 'insert'
@@ -326,9 +437,9 @@ sub valid_order {
        return 'Invalid order data' if $error;
     }
   }
-  elsif($svc_dsl->vendor_order_type eq 'X') {
+  elsif($svc_dsl->vendor_order_type eq 'CANCEL') {
   }
-  elsif($svc_dsl->vendor_order_type eq 'C') {
+  elsif($svc_dsl->vendor_order_type eq 'CHANGE') {
   }
 
  '';
@@ -339,6 +450,7 @@ sub qual2termsid {
     my $qual = qsearchs( 'qual', { 'vendor_qual_id' => $vendor_qual_id });
     return '' unless $qual;
     my %qual_options = $qual->options;
+    my( $optionname, $optionvalue );
     while (($optionname, $optionvalue) = each %qual_options) {
        if ( $optionname =~ /^ikano_Network_(\d+)_ProductGroup_(\d+)_Product_(\d+)_ProductCustomId$/ 
            && $optionvalue eq $ProductCustomId ) {
@@ -350,13 +462,6 @@ sub qual2termsid {
     '';
 }
 
-sub orderstatus_long2short {
-    my ($self,$order_status) = (shift,shift);
-    my %rorderStatus = reverse %orderStatus;
-    return $rorderStatus{$order_status} if exists $rorderStatus{$order_status};
-    '';
-}
-
 sub _export_insert {
   my( $self, $svc_dsl ) = (shift, shift);
 
@@ -367,7 +472,8 @@ sub _export_insert {
   my $contactTN = $svc_dsl->cust_svc->cust_pkg->cust_main->daytime;
   $contactTN =~ s/[^0-9]//g;
 
-  my $ProductCustomId = $svc_dsl->cust_svc->cust_pkg->part_pkg->option('externalid',1);
+  my %vendor_pkg_ids = $svc_dsl->cust_svc->cust_pkg->part_pkg->vendor_pkg_ids;
+  my $ProductCustomId = $vendor_pkg_ids{$self->exportnum};
 
   my $args = {
        orderType => 'NEW',
@@ -396,44 +502,196 @@ sub _export_insert {
   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 _export_insert OrderResponse hash:\n".Dumper($result) if $DEBUG;
+  warn "$me _export_insert 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'};
+  return 'No/invalid order id or status returned' 
+    unless defined $result->{'Status'} && defined $result->{'OrderId'}
+       && grep($_ eq $result->{'Status'}, @Net::Ikano::orderStatus);
 
   $svc_dsl->pushed(time);
   $svc_dsl->last_pull((time)+1); 
   $svc_dsl->vendor_order_id($result->{'OrderId'});
-  $svc_dsl->vendor_order_status($self->orderstatus_long2short($result->{'Status'}));
+  $svc_dsl->vendor_order_status($result->{'Status'});
   $svc_dsl->username($result->{'Username'});
   local $FS::svc_Common::noexport_hack = 1;
-  local $FS::UID::AutoCommit = 0;
   $result = $svc_dsl->replace; 
-  return 'Error setting DSL fields' if $result;
+  return "Error setting DSL fields: $result" if $result;
   '';
 }
 
 sub _export_replace {
   my( $self, $new, $old ) = (shift, shift, shift);
+# XXX only supports password changes now, but should return error if 
+# another change is attempted?
+
+  if($new->password ne $old->password) {
+      my $result = $self->valid_order($new,'statuschg');
+      return $result unless $result eq '';
+      
+      $result = $self->ikano_command('PASSWORDCHANGE',
+           { DSLPhoneNumber => $new->phonenum,
+             NewPassword => $new->password,
+           } ); 
+      return $result unless ref($result); # scalar (string) is an error
+
+      return 'Error changing password' unless defined $result->{'Password'}
+       && $result->{'Password'} eq $new->password;
+  }
+
   '';
 }
 
 sub _export_delete {
   my( $self, $svc_dsl ) = (shift, shift);
+  
+  my $result = $self->valid_order($svc_dsl,'delete');
+  return $result unless $result eq '';
+
+  # for now allow an immediate cancel only on New orders in New/Pending status
+  #XXX: add support for Change and Cancel orders in New/Pending status later
+
+  if($svc_dsl->vendor_order_type eq 'NEW') {
+    if($svc_dsl->vendor_order_status eq 'NEW' 
+           || $svc_dsl->vendor_order_status eq 'PENDING') {
+       my $result = $self->ikano_command('CANCEL', 
+           { OrderId => $svc_dsl->vendor_order_id, } );
+       return $result unless ref($result); # scalar (string) is an error
+       return 'Unable to cancel order' unless $result->{'Order'};
+       $result = $result->{'Order'};
+       return 'Invalid cancellation response' 
+           unless $result->{'Status'} eq 'CANCELLED' 
+               && $result->{'OrderId'} eq $svc_dsl->vendor_order_id;
+
+       # we're supposed to do a pull, but it will break everything, so don't
+       # this is very wrong...
+    }
+    else {
+       return "Cannot cancel a NEW order unless it's in NEW or PENDING status";
+    }
+  }
+  elsif($svc_dsl->vendor_order_type eq 'CANCEL') {
+     return 'Cannot cancel a CANCEL order unless expire was set'
+       unless $svc_dsl->cust_svc->cust_pkg->expire > 0;
+  }
+  else {
+    return 'Canceling orders other than NEW orders is not currently implemented';
+  }
+
   '';
 }
 
+sub export_expire {
+  my($self, $svc_dsl, $date) = (shift, shift, shift);
+  
+  my $result = $self->valid_order($svc_dsl,'expire');
+  return $result unless $result eq '';
+  
+  # for now allow a proper cancel only on New orders in Completed status
+  #XXX: add support for some other cases in future
+  
+  if($svc_dsl->vendor_order_type eq 'NEW' 
+       && $svc_dsl->vendor_order_status eq 'COMPLETED') {
+    
+         my $contactTN = $svc_dsl->cust_svc->cust_pkg->cust_main->daytime;
+         $contactTN =~ s/[^0-9]//g;
+
+         my %vendor_pkg_ids = $svc_dsl->cust_svc->cust_pkg->part_pkg->vendor_pkg_ids;
+         my $ProductCustomId = $vendor_pkg_ids{$self->exportnum};
+
+         # we are now a cancel order
+         $svc_dsl->desired_due_date($date);
+         $svc_dsl->vendor_order_type('CANCEL');
+
+         my $args = {
+               orderType => 'CANCEL',
+               ProductCustomId => $ProductCustomId,
+               TermsId => $self->qual2termsid($svc_dsl->vendor_qual_id,$ProductCustomId),
+               DSLPhoneNumber => $svc_dsl->loop_type eq '0' ? 'STANDALONE'
+                                                           : $svc_dsl->phonenum,
+               Password => $svc_dsl->password,
+               PrequalId => $svc_dsl->vendor_qual_id,
+               CompanyName => $svc_dsl->company,
+               FirstName => $svc_dsl->first,
+               LastName => $svc_dsl->last,
+               MiddleName => '',
+               ContactMethod => 'PHONE',
+               ContactPhoneNumber => $contactTN,
+               ContactEmail => 'x@x.xx',
+               ContactFax => '',
+               DateToOrder => time2str("%Y-%m-%d",$date),
+               RequestClientIP => '127.0.0.1',
+               IspChange => 'NO',
+               IspPrevious => '',
+               CurrentProvider => '',
+         };
+
+         $args->{'VirtualPhoneNumber'} = $svc_dsl->phonenum 
+           if $svc_dsl->loop_type eq '0';
+
+         $result = $self->ikano_command('ORDER',$args); 
+         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 _export_insert OrderResponse hash:\n".Dumper($result)
+               if $self->option('debug');
+         
+         return 'Invalid order response' unless defined $result->{'Order'};
+         $result = $result->{'Order'};
+
+         return 'No/invalid order id or status returned' 
+           unless defined $result->{'Status'} && defined $result->{'OrderId'}
+               && grep($_ eq $result->{'Status'}, @Net::Ikano::orderStatus);
+
+         $svc_dsl->pushed(time);
+         $svc_dsl->last_pull((time)+1); 
+         $svc_dsl->vendor_order_id($result->{'OrderId'});
+         $svc_dsl->vendor_order_status($result->{'Status'});
+         $svc_dsl->monitored('Y');
+         local $FS::svc_Common::noexport_hack = 1;
+         $result = $svc_dsl->replace; 
+         return "Error setting DSL fields: $result" if $result;
+  }
+  else {
+    return "Cancelling anything other than NEW orders in COMPLETED status is "
+       . "not currently implemented";
+  }
+ '';
+}
+
+sub statuschg {
+  my( $self, $svc_dsl, $type ) = (shift, shift, shift);
+
+  my $result = $self->valid_order($svc_dsl,'statuschg');
+  return $result unless $result eq '';
+
+  # get the DSLServiceId
+  $result = $self->ikano_command('CUSTOMERLOOKUP',
+       { PhoneNumber => $svc_dsl->phonenum } ); 
+  return $result unless ref($result); # scalar (string) is an error
+  return 'No DSLServiceId found' unless defined $result->{'DSLServiceId'};
+  my $DSLServiceId = $result->{'DSLServiceId'};
+
+  $result = $self->ikano_command('ACCOUNTSTATUSCHANGE',
+       { DSLPhoneNumber => $svc_dsl->phonenum,
+         DSLServiceId => $DSLServiceId,
+         type => $type,
+       } ); 
+  return $result unless ref($result); # scalar (string) is an error
+  ''; 
+}
+
 sub _export_suspend {
   my( $self, $svc_dsl ) = (shift, shift);
-  '';
+  $self->statuschg($svc_dsl,"SUSPEND");
 }
 
 sub _export_unsuspend {
   my( $self, $svc_dsl ) = (shift, shift);
-  '';
+  $self->statuschg($svc_dsl,"UNSUSPEND");
 }
 
 1;