X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Fpart_export%2Fvitelity.pm;h=574592ff84837e70910290f995d53ca9bb64b3fe;hp=bec3837d9ebeea856e09cdb52d4f5945dbfb0dcf;hb=0c7672759a3254c4bd7317edb23f40d8880d99d6;hpb=0d957a0f6008f301af282e134681088ba952c6f5 diff --git a/FS/FS/part_export/vitelity.pm b/FS/FS/part_export/vitelity.pm index bec3837d9..574592ff8 100644 --- a/FS/FS/part_export/vitelity.pm +++ b/FS/FS/part_export/vitelity.pm @@ -1,82 +1,165 @@ package FS::part_export::vitelity; +use base qw( FS::part_export ); -use vars qw(@ISA %info); +use strict; +use vars qw( %info ); use Tie::IxHash; -use FS::Record qw(qsearch dbh); -use FS::part_export; +use Data::Dumper; +use Geo::StreetAddress::US; +use Net::Vitelity 0.05; +use FS::Record qw( qsearch dbh ); use FS::phone_avail; - -@ISA = qw(FS::part_export); +use FS::svc_phone; tie my %options, 'Tie::IxHash', - 'login' => { label=>'Vitelity API login' }, - 'pass' => { label=>'Vitelity API password' }, - 'dry_run' => { label=>"Test mode - don't actually provision" }, + 'login' => { label=>'Vitelity API login' }, + 'pass' => { label=>'Vitelity API password' }, + 'routesip' => { label=>'routesip (optional sub-account)' }, + 'type' => { label=>'type (optional DID type to order)' }, + 'fax' => { label=>'vfax service', type=>'checkbox' }, + 'restrict_selection' => { type => 'select', + label => 'Restrict DID Selection', + options => [ '', 'tollfree', 'non-tollfree' ], + }, + 'dry_run' => { label => "Test mode - don't actually provision", + type => 'checkbox', + }, + 'disable_e911' => { label => "Disable E911 provisioning", + type => 'checkbox', + }, + 'debug' => { label => 'Enable debugging', + type => 'checkbox', + value => 1, + }, ; %info = ( - 'svc' => 'svc_phone', - 'desc' => 'Provision phone numbers to Vitelity', - 'options' => \%options, - 'notes' => <<'END' -Requires installation of -Net::Vitelity -from CPAN. + 'svc' => 'svc_phone', + 'desc' => 'Provision phone numbers to Vitelity', + 'options' => \%options, + 'no_machine' => 1, + 'notes' => <<'END' +routesip - optional Vitelity sub-account to which newly ordered DIDs will be routed +
type - optional DID type (perminute, unlimited, or your-pri) END ); sub rebless { shift; } +sub can_get_dids { 1; } +sub get_dids_can_tollfree { 1; }; +sub can_lnp { 1; } + sub get_dids { my $self = shift; my %opt = ref($_[0]) ? %{$_[0]} : @_; - my %search = (); - # 'orderby' => 'npa', #but it doesn't seem to work :/ + if ( $opt{'tollfree'} ) { + my $command = 'listtollfree'; + $command = 'listdids' if $self->option('fax'); + my @tollfree = $self->vitelity_command($command); + my @ret = (); + + return [] if ( $tollfree[0] eq 'noneavailable' || $tollfree[0] eq 'none'); + + foreach my $did ( @tollfree ) { + $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable toll-free did $did\n"; + push @ret, $did; + } + + my @sorted_ret = sort @ret; + return \@sorted_ret; + + } elsif ( $opt{'ratecenter'} && $opt{'state'} ) { + + my %flushopts = ( 'state' => $opt{'state'}, + 'ratecenter' => $opt{'ratecenter'}, + 'exportnum' => $self->exportnum + ); + FS::phone_avail::flush( \%flushopts ); + + 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; + + my $errmsg = 'WARNING: error populating phone availability cache: '; + + my $command = 'listlocal'; + $command = 'listdids' if $self->option('fax'); + my @dids = $self->vitelity_command( $command, + 'state' => $opt{'state'}, + 'ratecenter' => $opt{'ratecenter'}, + ); + # XXX: Options: type=unlimited OR type=pri + + next if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' ); + die "missingdata error running Vitelity API" if $dids[0] eq 'missingdata'; - my $method = ''; + foreach my $did ( @dids ) { + $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable (state and ratecenter) did $did\n"; + my($npa, $nxx, $station) = ($1, $2, $3); - if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers + my $phone_avail = new FS::phone_avail { + 'exportnum' => $self->exportnum, + 'countrycode' => '1', # vitelity is US/CA only now + 'state' => $opt{'state'}, + 'npa' => $npa, + 'nxx' => $nxx, + 'station' => $station, + 'name' => $opt{'ratecenter'}, + }; + + my $error = $phone_avail->insert(); + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + die $errmsg.$error; + } + + } + $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit; return [ - map { join('-', $_->npx, $_->nxx, $_->station ) } + map { join('-', $_->npa, $_->nxx, $_->station ) } qsearch({ 'table' => 'phone_avail', 'hashref' => { 'exportnum' => $self->exportnum, - 'countrycode' => '1', - 'state' => $opt{'state'}, - 'npa' => $opt{'areacode'}, - 'nxx' => $opt{'exchange'}, + 'countrycode' => '1', # vitelity is US/CA only now + 'name' => $opt{'ratecenter'}, + 'state' => $opt{'state'}, }, - 'order_by' => 'ORDER BY name', #? + 'order_by' => 'ORDER BY npa, nxx, station', }) ]; - } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX) + } elsif ( $opt{'areacode'} ) { - return [ - map { $_->name. ' ('. $_->npa. '-'. $_->nxx. '-XXXX)' } + my @rc = map { $_->{'Hash'}->{name}.", ".$_->state } qsearch({ - 'select' => 'DISTINCT ON ( name, npa, nxx ) *', + 'select' => 'DISTINCT name, state', 'table' => 'phone_avail', 'hashref' => { 'exportnum' => $self->exportnum, - 'countrycode' => '1', - 'state' => $opt{'state'}, + 'countrycode' => '1', # vitelity is US/CA only now 'npa' => $opt{'areacode'}, }, - 'order_by' => 'ORDER BY name', #? - }) - ]; + }); - } elsif ( $opt{'state'} ) { #and not other things, then return areacode + my @sorted_rc = sort @rc; + return [ @sorted_rc ]; - #XXX need to flush the cache at some point :/ + } elsif ( $opt{'state'} ) { #and not other things, then return areacode my @avail = qsearch({ 'select' => 'DISTINCT npa', 'table' => 'phone_avail', 'hashref' => { 'exportnum' => $self->exportnum, - 'countrycode' => '1', #don't hardcode me when gp goes intl + 'countrycode' => '1', # vitelity is US/CA only now 'state' => $opt{'state'}, }, 'order_by' => 'ORDER BY npa', @@ -86,11 +169,14 @@ sub get_dids { #otherwise, search for em - my @ratecenters = $self->vitelity_command( 'listavailratecenters', + my $command = 'listavailratecenters'; + $command = 'listratecenters' if $self->option('fax'); + my @ratecenters = $self->vitelity_command( $command, 'state' => $opt{'state'}, ); + # XXX: Options: type=unlimited OR type=pri - if ( $ratecenters[0] eq 'unavailable' ) { + if ( $ratecenters[0] eq 'unavailable' || $ratecenters[0] eq 'none' ) { return []; } elsif ( $ratecenters[0] eq 'missingdata' ) { die "missingdata error running Vitelity API"; #die? @@ -112,25 +198,28 @@ sub get_dids { my %npa = (); foreach my $ratecenter (@ratecenters) { - my @dids = $self->vitelity_command( 'listlocal', + my $command = 'listlocal'; + $command = 'listdids' if $self->option('fax'); + my @dids = $self->vitelity_command( $command, 'state' => $opt{'state'}, 'ratecenter' => $ratecenter, ); + # XXX: Options: type=unlimited OR type=pri - if ( $dids[0] eq 'unavailable' ) { + if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' ) { next; } elsif ( $dids[0] eq 'missingdata' ) { die "missingdata error running Vitelity API"; #die? } foreach my $did ( @dids ) { - $did =~ /^(\d{3})(\d{3})(\d{4})$/ or die "unparsable did $did\n"; + $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable (state) did $did\n"; my($npa, $nxx, $station) = ($1, $2, $3); $npa{$npa}++; my $phone_avail = new FS::phone_avail { 'exportnum' => $self->exportnum, - 'countrycode' => '1', #don't hardcode me when vitelity goes int'l + 'countrycode' => '1', # vitelity is US/CA only now 'state' => $opt{'state'}, 'npa' => $npa, 'nxx' => $nxx, @@ -138,7 +227,7 @@ sub get_dids { 'name' => $ratecenter, }; - $error = $phone_avail->insert(); + my $error = $phone_avail->insert(); if ( $error ) { $dbh->rollback if $oldAutoCommit; die $errmsg.$error; @@ -151,8 +240,6 @@ sub get_dids { $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit; my @return = sort { $a <=> $b } keys %npa; - #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return; - return \@return; } else { @@ -164,13 +251,24 @@ sub get_dids { sub vitelity_command { my( $self, $command, @args ) = @_; - eval "use Net::Vitelity;"; - die $@ if $@; + my $vitelity = Net::Vitelity->new( + 'login' => $self->option('login'), + 'pass' => $self->option('pass'), + 'apitype' => $self->option('fax') ? 'fax' : 'api', + 'debug' => $self->option('debug'), + ); + + $vitelity->$command(@args); +} + +sub vitelity_lnp_command { + my( $self, $command, @args ) = @_; my $vitelity = Net::Vitelity->new( - 'login' => $self->option('login'), - 'pass' => $self->option('pass'), - #'debug' => $debug, + 'login' => $self->option('login'), + 'pass' => $self->option('pass'), + 'apitype' => 'lnp', + 'debug' => $self->option('debug'), ); $vitelity->$command(@args); @@ -183,25 +281,190 @@ sub _export_insert { #we want to provision and catch errors now, not queue - my $result = $self->vitelity_command('getlocaldid', - 'did' => $svc_phone->phonenum, -#XXX -#Options: type=perminute OR type=unlimited OR type=your-pri OR -# routesip=route_to_this_subaccount - ); + #porting a number in? different code path + if ( $svc_phone->lnp_status eq 'portingin' ) { + + my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main; + + return 'Customer company is required' + unless $cust_main->company; + + return 'Customer day phone (for contact, not porting) is required' + unless $cust_main->daytime; + + return 'LNP Other Provider is required' + unless $svc_phone->lnp_other_provider; + + return 'LNP Other Provider Account # is required' + unless $svc_phone->lnp_other_provider_account; + + my %location = $svc_phone->location_hash; + my $sa = Geo::StreetAddress::US->parse_location( $location{'address1'} ); + + my $result = $self->vitelity_lnp_command('addport', + 'portnumber' => $svc_phone->phonenum, + 'partial' => 'no', + 'wireless' => 'no', + 'carrier' => $svc_phone->lnp_other_provider, + 'company' => $cust_main->company, + 'accnumber' => $svc_phone->lnp_other_provider_account, + 'name' => $svc_phone->phone_name_or_cust, + 'streetnumber' => $sa->{number}, + 'streetprefix' => $sa->{prefix}, + 'streetname' => $sa->{street}. ' '. $sa->{type}, + 'streetsuffix' => $sa->{suffix}, + 'unit' => ( $sa->{sec_unit_num} + ? $sa->{sec_unit_type}. ' '. $sa->{sec_unit_num} + : '' + ), + 'city' => $location{'city'}, + 'state' => $location{'state'}, + 'zip' => $location{'zip'}, + 'billnumber' => $svc_phone->phonenum, #?? do we need a new field for this? + 'contactnumber' => $cust_main->daytime, + ); + warn "Vitelity response: $result" if $self->option('debug'); + + if ( $result =~ /^ok:/i ) { + my($ok, $portid, $sig, $bill) = split(':', $result); + $svc_phone->lnp_portid($portid); + $svc_phone->lnp_signature('Y') if $sig =~ /y/i; + $svc_phone->lnp_bill('Y') if $bill =~ /y/i; + local($FS::svc_Common::noexport_hack) = 1; + return $svc_phone->replace; + } else { + return "Error initiating Vitelity port: $result"; + } - if ( $result ne 'success' ) { - return "Error running Vitelity getlocaldid: $result"; + } + + ### + # 1. provision the DID + ### + + my %vparams = ( 'did' => $svc_phone->phonenum ); + $vparams{'routesip'} = $self->option('routesip') + if defined $self->option('routesip'); + $vparams{'type'} = $self->option('type') + if defined $self->option('type'); + + my $command = 'getlocaldid'; + my $success = 'success'; + + # this is OK as Vitelity for now is US/CA only; it's not a hack + $command = 'gettollfree' if $vparams{'did'} =~ /^800|^888|^877|^866|^855/; + + if ($self->option('fax')) { + $command = 'getdid'; + $success = 'ok'; + } + + my $result = $self->vitelity_command($command,%vparams); + + if ( $result ne $success ) { + return "Error running Vitelity $command: $result"; + } elsif ( $self->option('debug') ) { + warn "Vitelity response: $result"; + } + + ### + # 2. Provision CNAM + ### + + my $cnam_result = $self->vitelity_command('cnamenable', + 'did'=>$svc_phone->phonenum, + ); + if ( $result !~ /^(ok|success)/i ) { + #we already provisioned the DID, so... + warn "Vitelity error enabling CNAM for ". $svc_phone->phonenum. ": $result"; + } elsif ( $self->option('debug') ) { + warn "Vitelity response: $result"; + } + + ### + # 3. Provision E911 + ### + + my $e911_error = $self->e911_send($svc_phone); + + if ( $e911_error =~ /status=(missingdata|invalid)/i ) { + + my $status = $1; + if ( $e911_error =~ /error=(.*)/ ) { + $e911_error = "status=$status, error=$1"; + } + + #but we already provisioned the DID, so: + $self->vitelity_command('removedid', 'did'=> $svc_phone->phonenum,); + #and check the results? if it failed, then what? + + return $e911_error; } ''; } +sub e911_send { + my($self, $svc_phone) = (shift, shift); + + return '' if $self->option('disable_e911'); + + my %location = $svc_phone->location_hash; + $location{'zip'} =~ s/\-\d{4}$//; + my %e911send = ( + 'did' => $svc_phone->phonenum, + 'name' => $svc_phone->phone_name_or_cust, + 'address' => $location{'address1'}, + 'city' => $location{'city'}, + 'state' => $location{'state'}, + 'zip' => $location{'zip'}, + ); + if ( $location{address2} =~ /^\s*(\w+)\W*(\d+)\s*$/ ) { + $e911send{'unittype'} = $1; + $e911send{'unitnumber'} = $2; + } + + my $e911_result = $self->vitelity_command('e911send', %e911send); + + unless ( $e911_result =~ /status=(missingdata|invalid)/i ) { + warn "Vitelity response: $e911_result" if $self->option('debug'); + return ''; + } + + return "Vitelity error provisioning E911 for". $svc_phone->phonenum. + ": $e911_result"; +} + sub _export_replace { my( $self, $new, $old ) = (shift, shift, shift); - #hmm, what's to change? - ''; + # Call Forwarding + if ( $old->forwarddst ne $new->forwarddst ) { + my $result = $self->vitelity_command('callfw', + 'did' => $old->phonenum, + 'forward' => $new->forwarddst ? $new->forwarddst : 'none', + ); + if ( $result ne 'ok' ) { + return "Error running Vitelity callfw: $result"; + } elsif ( $self->option('debug') ) { + warn "Vitelity response: $result"; + } + } + + # vfax forwarding emails + if ( $old->email ne $new->email && $self->option('fax') ) { + my $result = $self->vitelity_command('changeemail', + 'did' => $old->phonenum, + 'emails' => $new->email ? $new->email : '', + ); + if ( $result ne 'ok' ) { + return "Error running Vitelity changeemail: $result"; + } elsif ( $self->option('debug') ) { + warn "Vitelity response: $result"; + } + } + + $self->e911_send($new); } sub _export_delete { @@ -212,12 +475,16 @@ sub _export_delete { #probably okay to queue the deletion...? #but hell, let's do it inline anyway, who wants phone numbers hanging around + return 'Deleting vfax DIDs is unsupported by Vitelity API' if $self->option('fax'); + my $result = $self->vitelity_command('removedid', 'did' => $svc_phone->phonenum, ); if ( $result ne 'success' ) { - return "Error running Vitelity getlocaldid: $result"; + return "Error running Vitelity removedid: $result"; + } elsif ( $self->option('debug') ) { + warn "Vitelity response: $result"; } ''; @@ -235,5 +502,53 @@ sub _export_unsuspend { ''; } +sub check_lnp { + my $self = shift; + + my @export_svc = $self->export_svc; + return unless @export_svc; + + my $in_svcpart = 'IN ('. join( ',', map $_->svcpart, @export_svc). ')'; + + foreach my $svc_phone ( + qsearch({ 'table' => 'svc_phone', + 'addl_from' => 'LEFT JOIN cust_svc USING (svcnum)', + 'hashref' => {lnp_status=>'portingin'}, + 'extra_sql' => "AND svcpart $in_svcpart", + }) + ) { + + my $result = $self->vitelity_lnp_command('checkstatus', + 'portid'=>$svc_phone->lnp_portid, + ); + + if ( $result =~ /^Complete/i ) { + + $svc_phone->lnp_status('portedin'); + my $error = $self->_export_insert($svc_phone); + if ( $error ) { + #XXX log this using our internal log instead, so we can alert on it + # properly + warn "ERROR provisioning ported-in DID ". $svc_phone->phonenum. ": $error"; + } else { + local($FS::svc_Common::noexport_hack) = 1; + $error = $svc_phone->replace; #to set the lnp_status + #XXX log this using our internal log instead, so we can alert on it + warn "ERROR setting lnp_status for DID ". $svc_phone->phonenum. ": $error" if $error; + } + + } elsif ( $result ne $svc_phone->lnp_reject_reason ) { + $svc_phone->lnp_reject_reason($result); + local($FS::svc_Common::noexport_hack) = 1; + my $error = $svc_phone->replace; + #XXX log this using our internal log instead, so we can alert on it + warn "ERROR setting lnp_reject_reason for DID ". $svc_phone->phonenum. ": $error" if $error; + + } + + } + +} + 1;