X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_export%2Fikano.pm;h=23917bf9ea7cd951b143da09822ffd07afd4c58e;hb=3f7b5f5442d88a440b72e38f4911de4a996f86e4;hp=30e2bac7fd154554a6e3d82e79388dd9a23d6837;hpb=19f2731dbceb444c5dd45f57fb0a785dcaf9aa65;p=freeside.git
diff --git a/FS/FS/part_export/ikano.pm b/FS/FS/part_export/ikano.pm
index 30e2bac7f..23917bf9e 100644
--- a/FS/FS/part_export/ikano.pm
+++ b/FS/FS/part_export/ikano.pm
@@ -24,12 +24,14 @@ tie my %options, 'Tie::IxHash',
default => 'ATT,BELLCA',
},
'debug' => { label => 'Debug Mode', type => 'checkbox' },
+ 'import' => { label => 'Import Mode', type => 'checkbox' },
;
%info = (
'svc' => 'svc_dsl',
'desc' => 'Provision DSL to Ikano',
'options' => \%options,
+ 'no_machine' => 1,
'notes' => <<'END'
Requires installation of
Net::Ikano from CPAN.
@@ -42,6 +44,38 @@ sub rebless { shift; }
sub external_pkg_map { 1; }
+sub location_types {
+ (
+ '' => '(None)',
+ 'APT' => 'Apartment',
+ 'BLDG' => 'Building',
+ 'FLR' => 'Floor',
+ 'LOT' => 'Lot',
+ 'RM' => 'Room',
+ 'SLIP' => 'Slip',
+ 'SUIT' => 'Suite',
+ 'TRLR' => 'Trailer',
+ 'UNIT' => 'Unit',
+ 'WING' => 'Wing',
+ );
+}
+
+sub location_types_parse {
+ my $class = shift;
+ my %t = $class->location_types;
+ delete $t{''};
+ (
+ (map { $_ => $_ } keys %t),
+ (reverse %t),
+ 'STE' => 'SUIT', #USPS
+ );
+}
+
+sub import_mode {
+ my $self = shift;
+ $self->option('import');
+}
+
sub dsl_pull {
# we distinguish between invalid new data (return error) versus data that
# has legitimately changed (may eventually execute hooks; now just update)
@@ -51,10 +85,19 @@ 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, $threshold) = (shift, shift, shift);
- $self->loadmod;
- my $result = $self->valid_order($svc_dsl,'pull');
- return $result unless $result eq '';
+ 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) {
@@ -64,8 +107,8 @@ sub dsl_pull {
return '';
}
- $result = $self->ikano_command('ORDERSTATUS',
- { OrderId => $svc_dsl->vendor_order_id } );
+ 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
@@ -116,8 +159,20 @@ sub dsl_pull {
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($svc_dsl->loop_type eq '') { # line-share
+ 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) {
@@ -208,7 +263,6 @@ sub dsl_pull {
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
- my $error;
foreach my $inote ( @inotes ) {
my $found = 0;
foreach my $onote ( @onotes ) {
@@ -217,7 +271,7 @@ sub dsl_pull {
last;
}
}
- $error = $inote->insert unless ( $found );
+ my $error = $inote->insert unless ( $found );
if ( $error ) {
$dbh->rollback if $oldAutoCommit;
return "Cannot add note: $error";
@@ -226,7 +280,7 @@ sub dsl_pull {
$svc_dsl->last_pull((time));
local $FS::svc_Common::noexport_hack = 1;
- $error = $svc_dsl->replace;
+ my $error = $svc_dsl->replace;
if ( $error ) {
$dbh->rollback if $oldAutoCommit;
return "Cannot update DSL data: $error";
@@ -259,21 +313,29 @@ sub ikano2fsnote {
} );
}
+# address always required for Ikano qual, TN optional (assume dry if not given)
sub qual {
my($self,$qual) = (shift,shift);
-# address always required for Ikano qual, TN optional (assume dry if not given)
+
+ return 'Invalid operation - Import Mode is enabled' if $self->import_mode;
+
+ my %location_hash = $qual->location_hash;
+ return 'No address provided' unless keys %location_hash;
+
+ return 'Location kind is required' unless $location_hash{location_kind};
- my %location_hash = $qual->location;
- return 'No address provided' unless %location_hash;
my $svctn = $qual->phonenum;
+ my $zip = $location_hash{zip};
+ $zip =~ s/(\d{5})-\d{4}/$1/;
+
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},
+ ZipCode => $zip,
Country => $location_hash{country},
LocationType => $location_hash{location_kind},
PhoneNumber => length($svctn) > 1 ? $svctn : "STANDALONE",
@@ -314,7 +376,7 @@ sub qual {
};
}
-sub qual_html {
+sub qual_result {
my($self,$qual) = (shift,shift);
my %qual_options = $qual->options;
@@ -326,39 +388,58 @@ sub qual_html {
&& $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 = "Qualifying Packages:
";
+ my %pkglist = ();
+ my %found = ();
my @part_pkgs = qsearch( 'part_pkg', { 'disabled' => '' } );
foreach my $part_pkg ( @part_pkgs ) {
- my $externalid = $part_pkg->option('externalid',1);
- if ( $externalid ) {
- $list .= "- ".$part_pkg->pkgpart.": ".$part_pkg->pkg." - "
- .$part_pkg->comment."
"
- if grep( $_ eq $externalid, @externalids );
+ 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 && grep { $_ eq $externalid } @externalids ) {
+ $pkglist{$part_pkg->pkgpart} = $part_pkg->pkg_comment;
+ $found{$externalid}++;
}
}
- $list .= "
";
- $list;
+
+ my %not_avail = ();
+ foreach my $externalid ( grep !$found{$_}, @externalids ) {
+ $not_avail{$externalid} = $externalid; #a better label?
+ }
+
+ {
+ 'pkglist' => \%pkglist,
+ 'not_avail' => \%not_avail,
+ };
}
-sub notes_html {
- my($self,$svc_dsl) = (shift,shift);
- my $conf = new FS::Conf;
- my $date_format = $conf->config('date_format') || '%m/%d/%Y';
- my @notes = $svc_dsl->notes;
- my $html = '
- Date | By | Priority | Note |
';
- foreach my $note ( @notes ) {
- $html .= "
- ".time2str("$date_format %H:%M",$note->date)." |
- ".$note->by." |
- ". ($note->priority eq 'N' ? 'Normal' : 'High') ." |
- ".$note->note." |
";
+sub quals_by_cust_and_pkg {
+ my($self, $custnum, $pkgpart) = (shift,shift,shift);
+
+ die "invalid custnum or pkgpart"
+ unless ($custnum =~ /^\d+$/ && $pkgpart =~ /^\d+$/);
+
+ my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } );
+ die "no part_pkg found" unless $part_pkg;
+ my %vendor_pkg_ids = $part_pkg->vendor_pkg_ids;
+ my $external_id = $vendor_pkg_ids{$self->exportnum};
+ die "no vendor package id defined on this package" unless $external_id;
+
+ my $extra_sql = "where custnum = $custnum or locationnum in (select "
+ . "locationnum from cust_location where custnum = $custnum)";
+ my @quals = qsearch( { 'table' => 'qual', 'extra_sql' => $extra_sql, } );
+
+ my @filtered_quals;
+ foreach my $qual ( @quals ) {
+ my %qual_options = $qual->options;
+ my( $optionname, $optionvalue );
+ while (($optionname, $optionvalue) = each %qual_options) {
+ push @filtered_quals, $qual
+ if ( $optionname =~ /^ikano_Network_(\d+)_ProductGroup_(\d+)_Product_(\d+)_ProductCustomId$/
+ && $optionvalue eq $external_id );
+ }
}
- $html .= '
';
- $html;
+
+ @filtered_quals;
}
sub loop_type_long { # sub, not a method
@@ -395,17 +476,17 @@ sub valid_order {
if $self->option('debug');
# common to all order types/status/loop_type
- my $error = !($svc_dsl->desired_due_date
- && 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;
-
+ return 'No desired_due_date' unless $svc_dsl->desired_due_date;
+ return 'Unknown vendor_order_type'
+ unless grep $_ eq $svc_dsl->vendor_order_type, Net::Ikano->orderTypes;
+ return 'No first name' unless $svc_dsl->first;
+ return 'No last name' unless $svc_dsl->get('last');
+ return 'No loop type' unless defined $svc_dsl->loop_type;
+ return 'No vendor_qual_id' unless $svc_dsl->vendor_qual_id;
+
+ 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 });
@@ -414,8 +495,8 @@ sub valid_order {
# weird ifs & long lines for readability and ease of understanding - don't change
if($svc_dsl->vendor_order_type eq 'NEW') {
if($svc_dsl->pushed) {
- $error = !( ($action eq 'pull' || $action eq 'statuschg'
- || $action eq 'delete')
+ my $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
);
@@ -425,7 +506,7 @@ sub valid_order {
if ($action eq 'statuschg' && length($svc_dsl->phonenum) < 1);
}
else { # unpushed New order - cannot do anything other than push it
- $error = !($action eq 'insert'
+ my $error = !($action eq 'insert'
&& length($svc_dsl->vendor_order_id) < 1
&& length($svc_dsl->vendor_order_status) < 1
&& ( ($svc_dsl->phonenum eq '' && $svc_dsl->loop_type eq '0') # dry
@@ -463,16 +544,23 @@ sub qual2termsid {
sub _export_insert {
my( $self, $svc_dsl ) = (shift, shift);
- $self->loadmod;
+ if($self->import_mode) {
+ warn "$me _export_insert: import mode" if $self->option('debug');
+ $svc_dsl->pushed((time)-2);
+ $svc_dsl->last_pull((time)-1);
+ return $self->dsl_pull($svc_dsl,0,1);
+ }
my $result = $self->valid_order($svc_dsl,'insert');
return $result unless $result eq '';
my $isp_chg = $svc_dsl->isp_chg eq 'Y' ? 'YES' : 'NO';
- my $contactTN = $svc_dsl->cust_svc->cust_pkg->cust_main->daytime;
+ my $cust_main = $svc_dsl->cust_svc->cust_pkg->cust_main;
+ my $contactTN = $cust_main->daytime || $cust_main->night || '5555555555';
$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',
@@ -524,6 +612,9 @@ sub _export_insert {
sub _export_replace {
my( $self, $new, $old ) = (shift, shift, shift);
+
+ return 'Invalid operation - Import Mode is enabled' if $self->import_mode;
+
# XXX only supports password changes now, but should return error if
# another change is attempted?
@@ -547,11 +638,13 @@ sub _export_replace {
sub _export_delete {
my( $self, $svc_dsl ) = (shift, shift);
+ return 'Invalid operation - Import Mode is enabled' if $self->import_mode;
+
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 Chance and Cancel orders in New/Pending status later
+ #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'
@@ -572,6 +665,10 @@ sub _export_delete {
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';
}
@@ -579,8 +676,90 @@ sub _export_delete {
'';
}
+sub export_expire {
+ my($self, $svc_dsl, $date) = (shift, shift, shift);
+
+ return 'Invalid operation - Import Mode is enabled' if $self->import_mode;
+
+ 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);
+
+ return 'Invalid operation - Import Mode is enabled' if $self->import_mode;
my $result = $self->valid_order($svc_dsl,'statuschg');
return $result unless $result eq '';