X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_export%2Fvitelity.pm;h=12c3a7fcef87a95c77dbda56dca8c3be1c1dafc1;hb=b5c4237a34aef94976bc343c8d9e138664fc3984;hp=044387a6814ceab47e62ef89e980174da62ba69f;hpb=924231ef1979f87c12bcaaf6685e3da4543014b6;p=freeside.git diff --git a/FS/FS/part_export/vitelity.pm b/FS/FS/part_export/vitelity.pm index 044387a68..12c3a7fce 100644 --- a/FS/FS/part_export/vitelity.pm +++ b/FS/FS/part_export/vitelity.pm @@ -14,6 +14,12 @@ tie my %options, 'Tie::IxHash', 'dry_run' => { label=>"Test mode - don't actually provision" }, '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' ], + } + ; %info = ( @@ -36,14 +42,76 @@ sub get_dids { my $self = shift; my %opt = ref($_[0]) ? %{$_[0]} : @_; -# currently one of three cases: areacode+exchange, areacode, state -# name == ratecenter + if ( $opt{'tollfree'} ) { + my $command = 'listtollfree'; + $command = 'listdids' if $self->option('fax'); + my @tollfree = $self->vitelity_command($command); + my @ret = (); - my %search = (); + return [] if ( $tollfree[0] eq 'noneavailable' || $tollfree[0] eq 'none'); - my $method = ''; + foreach my $did ( @tollfree ) { + $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable 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'; + + foreach my $did ( @dids ) { + $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n"; + my($npa, $nxx, $station) = ($1, $2, $3); - if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers in format NPA-NXX-XXXX + 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('-', $_->npa, $_->nxx, $_->station ) } @@ -51,33 +119,29 @@ sub get_dids { 'table' => 'phone_avail', 'hashref' => { 'exportnum' => $self->exportnum, 'countrycode' => '1', # vitelity is US/CA only now - 'state' => $opt{'state'}, - 'npa' => $opt{'areacode'}, - 'nxx' => $opt{'exchange'}, + 'name' => $opt{'ratecenter'}, + 'state' => $opt{'state'}, }, - 'order_by' => 'ORDER BY station', + 'order_by' => 'ORDER BY npa, nxx, station', }) ]; - } elsif ( $opt{'areacode'} ) { #return exchanges in format NPA-NXX- literal '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', # vitelity is US/CA only now - 'state' => $opt{'state'}, 'npa' => $opt{'areacode'}, }, - 'order_by' => 'ORDER BY nxx', - }) - ]; + }); - } 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', @@ -93,12 +157,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? @@ -120,20 +186,22 @@ 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 did $did\n"; my($npa, $nxx, $station) = ($1, $2, $3); $npa{$npa}++; @@ -147,7 +215,7 @@ sub get_dids { 'name' => $ratecenter, }; - $error = $phone_avail->insert(); + my $error = $phone_avail->insert(); if ( $error ) { $dbh->rollback if $oldAutoCommit; die $errmsg.$error; @@ -160,8 +228,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 { @@ -179,6 +245,7 @@ sub vitelity_command { my $vitelity = Net::Vitelity->new( 'login' => $self->option('login'), 'pass' => $self->option('pass'), + 'apitype' => $self->option('fax') ? 'fax' : 'api', #'debug' => $debug, ); @@ -192,16 +259,27 @@ sub _export_insert { #we want to provision and catch errors now, not queue - %vparams = ( 'did' => $svc_phone->phonenum ); + 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 $result = $self->vitelity_command('getlocaldid',%vparams); + my $command = 'getlocaldid'; + my $success = 'success'; - if ( $result ne 'success' ) { - return "Error running Vitelity getlocaldid: $result"; + # 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"; } ''; @@ -210,7 +288,28 @@ sub _export_insert { 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"; + } + } + + # 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"; + } + } + ''; } @@ -222,12 +321,14 @@ 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"; } '';