1 package FS::part_export::vitelity;
2 use base qw( FS::part_export );
6 use FS::Record qw( qsearch dbh );
9 tie my %options, 'Tie::IxHash',
10 'login' => { label=>'Vitelity API login' },
11 'pass' => { label=>'Vitelity API password' },
12 'routesip' => { label=>'routesip (optional sub-account)' },
13 'type' => { label=>'type (optional DID type to order)' },
14 'fax' => { label=>'vfax service', type=>'checkbox' },
15 'restrict_selection' => { type => 'select',
16 label => 'Restrict DID Selection',
17 options => [ '', 'tollfree', 'non-tollfree' ],
19 'dry_run' => { label => "Test mode - don't actually provision",
22 'disable_e911' => { label => "Disable E911 provisioning",
29 'desc' => 'Provision phone numbers to Vitelity',
30 'options' => \%options,
33 Requires installation of
34 <a href="http://search.cpan.org/dist/Net-Vitelity">Net::Vitelity</a>
37 routesip - optional Vitelity sub-account to which newly ordered DIDs will be routed
38 <br>type - optional DID type (perminute, unlimited, or your-pri)
42 sub rebless { shift; }
44 sub get_dids_can_tollfree { 1; };
48 my %opt = ref($_[0]) ? %{$_[0]} : @_;
50 if ( $opt{'tollfree'} ) {
51 my $command = 'listtollfree';
52 $command = 'listdids' if $self->option('fax');
53 my @tollfree = $self->vitelity_command($command);
56 return [] if ( $tollfree[0] eq 'noneavailable' || $tollfree[0] eq 'none');
58 foreach my $did ( @tollfree ) {
59 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
63 my @sorted_ret = sort @ret;
66 } elsif ( $opt{'ratecenter'} && $opt{'state'} ) {
68 my %flushopts = ( 'state' => $opt{'state'},
69 'ratecenter' => $opt{'ratecenter'},
70 'exportnum' => $self->exportnum
72 FS::phone_avail::flush( \%flushopts );
74 local $SIG{HUP} = 'IGNORE';
75 local $SIG{INT} = 'IGNORE';
76 local $SIG{QUIT} = 'IGNORE';
77 local $SIG{TERM} = 'IGNORE';
78 local $SIG{TSTP} = 'IGNORE';
79 local $SIG{PIPE} = 'IGNORE';
81 my $oldAutoCommit = $FS::UID::AutoCommit;
82 local $FS::UID::AutoCommit = 0;
85 my $errmsg = 'WARNING: error populating phone availability cache: ';
87 my $command = 'listlocal';
88 $command = 'listdids' if $self->option('fax');
89 my @dids = $self->vitelity_command( $command,
90 'state' => $opt{'state'},
91 'ratecenter' => $opt{'ratecenter'},
93 # XXX: Options: type=unlimited OR type=pri
95 next if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' );
96 die "missingdata error running Vitelity API" if $dids[0] eq 'missingdata';
98 foreach my $did ( @dids ) {
99 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
100 my($npa, $nxx, $station) = ($1, $2, $3);
102 my $phone_avail = new FS::phone_avail {
103 'exportnum' => $self->exportnum,
104 'countrycode' => '1', # vitelity is US/CA only now
105 'state' => $opt{'state'},
108 'station' => $station,
109 'name' => $opt{'ratecenter'},
112 my $error = $phone_avail->insert();
114 $dbh->rollback if $oldAutoCommit;
119 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
122 map { join('-', $_->npa, $_->nxx, $_->station ) }
124 'table' => 'phone_avail',
125 'hashref' => { 'exportnum' => $self->exportnum,
126 'countrycode' => '1', # vitelity is US/CA only now
127 'name' => $opt{'ratecenter'},
128 'state' => $opt{'state'},
130 'order_by' => 'ORDER BY npa, nxx, station',
134 } elsif ( $opt{'areacode'} ) {
136 my @rc = map { $_->{'Hash'}->{name}.", ".$_->state }
138 'select' => 'DISTINCT name, state',
139 'table' => 'phone_avail',
140 'hashref' => { 'exportnum' => $self->exportnum,
141 'countrycode' => '1', # vitelity is US/CA only now
142 'npa' => $opt{'areacode'},
146 my @sorted_rc = sort @rc;
147 return [ @sorted_rc ];
149 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
151 my @avail = qsearch({
152 'select' => 'DISTINCT npa',
153 'table' => 'phone_avail',
154 'hashref' => { 'exportnum' => $self->exportnum,
155 'countrycode' => '1', # vitelity is US/CA only now
156 'state' => $opt{'state'},
158 'order_by' => 'ORDER BY npa',
161 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
163 #otherwise, search for em
165 my $command = 'listavailratecenters';
166 $command = 'listratecenters' if $self->option('fax');
167 my @ratecenters = $self->vitelity_command( $command,
168 'state' => $opt{'state'},
170 # XXX: Options: type=unlimited OR type=pri
172 if ( $ratecenters[0] eq 'unavailable' || $ratecenters[0] eq 'none' ) {
174 } elsif ( $ratecenters[0] eq 'missingdata' ) {
175 die "missingdata error running Vitelity API"; #die?
178 local $SIG{HUP} = 'IGNORE';
179 local $SIG{INT} = 'IGNORE';
180 local $SIG{QUIT} = 'IGNORE';
181 local $SIG{TERM} = 'IGNORE';
182 local $SIG{TSTP} = 'IGNORE';
183 local $SIG{PIPE} = 'IGNORE';
185 my $oldAutoCommit = $FS::UID::AutoCommit;
186 local $FS::UID::AutoCommit = 0;
189 my $errmsg = 'WARNING: error populating phone availability cache: ';
192 foreach my $ratecenter (@ratecenters) {
194 my $command = 'listlocal';
195 $command = 'listdids' if $self->option('fax');
196 my @dids = $self->vitelity_command( $command,
197 'state' => $opt{'state'},
198 'ratecenter' => $ratecenter,
200 # XXX: Options: type=unlimited OR type=pri
202 if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' ) {
204 } elsif ( $dids[0] eq 'missingdata' ) {
205 die "missingdata error running Vitelity API"; #die?
208 foreach my $did ( @dids ) {
209 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
210 my($npa, $nxx, $station) = ($1, $2, $3);
213 my $phone_avail = new FS::phone_avail {
214 'exportnum' => $self->exportnum,
215 'countrycode' => '1', # vitelity is US/CA only now
216 'state' => $opt{'state'},
219 'station' => $station,
220 'name' => $ratecenter,
223 my $error = $phone_avail->insert();
225 $dbh->rollback if $oldAutoCommit;
233 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
235 my @return = sort { $a <=> $b } keys %npa;
239 die "get_dids called without state or areacode options";
244 sub vitelity_command {
245 my( $self, $command, @args ) = @_;
247 eval "use Net::Vitelity;";
250 my $vitelity = Net::Vitelity->new(
251 'login' => $self->option('login'),
252 'pass' => $self->option('pass'),
253 'apitype' => $self->option('fax') ? 'fax' : 'api',
257 $vitelity->$command(@args);
261 my( $self, $svc_phone ) = (shift, shift);
263 return '' if $self->option('dry_run');
265 #we want to provision and catch errors now, not queue
268 # 1. provision the DID
271 my %vparams = ( 'did' => $svc_phone->phonenum );
272 $vparams{'routesip'} = $self->option('routesip')
273 if defined $self->option('routesip');
274 $vparams{'type'} = $self->option('type')
275 if defined $self->option('type');
277 my $command = 'getlocaldid';
278 my $success = 'success';
280 # this is OK as Vitelity for now is US/CA only; it's not a hack
281 $command = 'gettollfree' if $vparams{'did'} =~ /^800|^888|^877|^866|^855/;
283 if ($self->option('fax')) {
288 my $result = $self->vitelity_command($command,%vparams);
290 if ( $result ne $success ) {
291 return "Error running Vitelity $command: $result";
298 my $cnam_result = $self->vitelity_command('cnamenable',
299 'did'=>$svc_phone->phonenum,
301 if ( $result ne 'ok' ) {
302 #we already provisioned the DID, so...
303 warn "Vitelity error enabling CNAM for ". $svc_phone->phonenum. ": $result";
310 my $e911_error = $self->e911_send($svc_phone);
312 if ( $e911_error =~ /status=(missingdata|invalid)/i ) {
315 if ( $e911_error =~ /error=(.*)/ ) {
316 $e911_error = "status=$status, error=$1";
319 #but we already provisioned the DID, so:
320 $self->vitelity_command('removedid', 'did'=> $svc_phone->phonenum,);
321 #and check the results? if it failed, then what?
330 my($self, $svc_phone) = (shift, shift);
332 return '' if $self->option('disable_e911');
334 my %location = $svc_phone->location_hash;
335 $location{'zip'} =~ s/\-\d{4}$//;
337 'did' => $svc_phone->phonenum,
338 'address' => $location{'address1'},
339 'city' => $location{'city'},
340 'state' => $location{'state'},
341 'zip' => $location{'zip'},
343 if ( $svc_phone->phone_name ) {
344 $e911send{'name'} = $svc_phone->phone_name;
346 my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
347 $e911send{'name'} = $cust_main->company || $cust_main->first. ' '.
348 $cust_main->get('last');
350 if ( $location{address2} =~ /^\s*(\w+)\W*(\d+)\s*$/ ) {
351 $e911send{'unittype'} = $1;
352 $e911send{'unitnumber'} = $2;
355 my $e911_result = $self->vitelity_command('e911send', %e911send);
357 return '' unless $result =~ /^(missingdata|invalid)/i;
359 return "Vitelity error provisioning E911 for". $svc_phone->phonenum.
363 sub _export_replace {
364 my( $self, $new, $old ) = (shift, shift, shift);
367 if( $old->forwarddst ne $new->forwarddst ) {
368 my $result = $self->vitelity_command('callfw',
369 'did' => $old->phonenum,
370 'forward' => $new->forwarddst ? $new->forwarddst : 'none',
372 if ( $result ne 'ok' ) {
373 return "Error running Vitelity callfw: $result";
377 # vfax forwarding emails
378 if( $old->email ne $new->email && $self->option('fax') ) {
379 my $result = $self->vitelity_command('changeemail',
380 'did' => $old->phonenum,
381 'emails' => $new->email ? $new->email : '',
383 if ( $result ne 'ok' ) {
384 return "Error running Vitelity changeemail: $result";
388 $self->e911_send($new);
392 my( $self, $svc_phone ) = (shift, shift);
394 return '' if $self->option('dry_run');
396 #probably okay to queue the deletion...?
397 #but hell, let's do it inline anyway, who wants phone numbers hanging around
399 return 'Deleting vfax DIDs is unsupported by Vitelity API' if $self->option('fax');
401 my $result = $self->vitelity_command('removedid',
402 'did' => $svc_phone->phonenum,
405 if ( $result ne 'success' ) {
406 return "Error running Vitelity removedid: $result";
409 return '' if $self->option('disable_e911');
414 sub _export_suspend {
415 my( $self, $svc_phone ) = (shift, shift);
420 sub _export_unsuspend {
421 my( $self, $svc_phone ) = (shift, shift);