1 package FS::part_export::vitelity;
2 use base qw( FS::part_export );
6 use Geo::StreetAddress::US;
8 use FS::Record qw( qsearch dbh );
12 tie my %options, 'Tie::IxHash',
13 'login' => { label=>'Vitelity API login' },
14 'pass' => { label=>'Vitelity API password' },
15 'routesip' => { label=>'routesip (optional sub-account)' },
16 'type' => { label=>'type (optional DID type to order)' },
17 'fax' => { label=>'vfax service', type=>'checkbox' },
18 'restrict_selection' => { type => 'select',
19 label => 'Restrict DID Selection',
20 options => [ '', 'tollfree', 'non-tollfree' ],
22 'dry_run' => { label => "Test mode - don't actually provision",
25 'disable_e911' => { label => "Disable E911 provisioning",
32 'desc' => 'Provision phone numbers to Vitelity',
33 'options' => \%options,
36 routesip - optional Vitelity sub-account to which newly ordered DIDs will be routed
37 <br>type - optional DID type (perminute, unlimited, or your-pri)
41 sub rebless { shift; }
43 sub can_get_dids { 1; }
44 sub get_dids_can_tollfree { 1; };
49 my %opt = ref($_[0]) ? %{$_[0]} : @_;
51 if ( $opt{'tollfree'} ) {
52 my $command = 'listtollfree';
53 $command = 'listdids' if $self->option('fax');
54 my @tollfree = $self->vitelity_command($command);
57 return [] if ( $tollfree[0] eq 'noneavailable' || $tollfree[0] eq 'none');
59 foreach my $did ( @tollfree ) {
60 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable toll-free did $did\n";
64 my @sorted_ret = sort @ret;
67 } elsif ( $opt{'ratecenter'} && $opt{'state'} ) {
69 my %flushopts = ( 'state' => $opt{'state'},
70 'ratecenter' => $opt{'ratecenter'},
71 'exportnum' => $self->exportnum
73 FS::phone_avail::flush( \%flushopts );
75 local $SIG{HUP} = 'IGNORE';
76 local $SIG{INT} = 'IGNORE';
77 local $SIG{QUIT} = 'IGNORE';
78 local $SIG{TERM} = 'IGNORE';
79 local $SIG{TSTP} = 'IGNORE';
80 local $SIG{PIPE} = 'IGNORE';
82 my $oldAutoCommit = $FS::UID::AutoCommit;
83 local $FS::UID::AutoCommit = 0;
86 my $errmsg = 'WARNING: error populating phone availability cache: ';
88 my $command = 'listlocal';
89 $command = 'listdids' if $self->option('fax');
90 my @dids = $self->vitelity_command( $command,
91 'state' => $opt{'state'},
92 'ratecenter' => $opt{'ratecenter'},
94 # XXX: Options: type=unlimited OR type=pri
96 next if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' );
97 die "missingdata error running Vitelity API" if $dids[0] eq 'missingdata';
99 foreach my $did ( @dids ) {
100 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable (state and ratecenter) did $did\n";
101 my($npa, $nxx, $station) = ($1, $2, $3);
103 my $phone_avail = new FS::phone_avail {
104 'exportnum' => $self->exportnum,
105 'countrycode' => '1', # vitelity is US/CA only now
106 'state' => $opt{'state'},
109 'station' => $station,
110 'name' => $opt{'ratecenter'},
113 my $error = $phone_avail->insert();
115 $dbh->rollback if $oldAutoCommit;
120 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
123 map { join('-', $_->npa, $_->nxx, $_->station ) }
125 'table' => 'phone_avail',
126 'hashref' => { 'exportnum' => $self->exportnum,
127 'countrycode' => '1', # vitelity is US/CA only now
128 'name' => $opt{'ratecenter'},
129 'state' => $opt{'state'},
131 'order_by' => 'ORDER BY npa, nxx, station',
135 } elsif ( $opt{'areacode'} ) {
137 my @rc = map { $_->{'Hash'}->{name}.", ".$_->state }
139 'select' => 'DISTINCT name, state',
140 'table' => 'phone_avail',
141 'hashref' => { 'exportnum' => $self->exportnum,
142 'countrycode' => '1', # vitelity is US/CA only now
143 'npa' => $opt{'areacode'},
147 my @sorted_rc = sort @rc;
148 return [ @sorted_rc ];
150 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
152 my @avail = qsearch({
153 'select' => 'DISTINCT npa',
154 'table' => 'phone_avail',
155 'hashref' => { 'exportnum' => $self->exportnum,
156 'countrycode' => '1', # vitelity is US/CA only now
157 'state' => $opt{'state'},
159 'order_by' => 'ORDER BY npa',
162 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
164 #otherwise, search for em
166 my $command = 'listavailratecenters';
167 $command = 'listratecenters' if $self->option('fax');
168 my @ratecenters = $self->vitelity_command( $command,
169 'state' => $opt{'state'},
171 # XXX: Options: type=unlimited OR type=pri
173 if ( $ratecenters[0] eq 'unavailable' || $ratecenters[0] eq 'none' ) {
175 } elsif ( $ratecenters[0] eq 'missingdata' ) {
176 die "missingdata error running Vitelity API"; #die?
179 local $SIG{HUP} = 'IGNORE';
180 local $SIG{INT} = 'IGNORE';
181 local $SIG{QUIT} = 'IGNORE';
182 local $SIG{TERM} = 'IGNORE';
183 local $SIG{TSTP} = 'IGNORE';
184 local $SIG{PIPE} = 'IGNORE';
186 my $oldAutoCommit = $FS::UID::AutoCommit;
187 local $FS::UID::AutoCommit = 0;
190 my $errmsg = 'WARNING: error populating phone availability cache: ';
193 foreach my $ratecenter (@ratecenters) {
195 my $command = 'listlocal';
196 $command = 'listdids' if $self->option('fax');
197 my @dids = $self->vitelity_command( $command,
198 'state' => $opt{'state'},
199 'ratecenter' => $ratecenter,
201 # XXX: Options: type=unlimited OR type=pri
203 if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' ) {
205 } elsif ( $dids[0] eq 'missingdata' ) {
206 die "missingdata error running Vitelity API"; #die?
209 foreach my $did ( @dids ) {
210 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable (state) did $did\n";
211 my($npa, $nxx, $station) = ($1, $2, $3);
214 my $phone_avail = new FS::phone_avail {
215 'exportnum' => $self->exportnum,
216 'countrycode' => '1', # vitelity is US/CA only now
217 'state' => $opt{'state'},
220 'station' => $station,
221 'name' => $ratecenter,
224 my $error = $phone_avail->insert();
226 $dbh->rollback if $oldAutoCommit;
234 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
236 my @return = sort { $a <=> $b } keys %npa;
240 die "get_dids called without state or areacode options";
245 sub vitelity_command {
246 my( $self, $command, @args ) = @_;
248 my $vitelity = Net::Vitelity->new(
249 'login' => $self->option('login'),
250 'pass' => $self->option('pass'),
251 'apitype' => $self->option('fax') ? 'fax' : 'api',
255 $vitelity->$command(@args);
258 sub vitelity_lnp_command {
259 my( $self, $command, @args ) = @_;
261 my $vitelity = Net::Vitelity->new(
262 'login' => $self->option('login'),
263 'pass' => $self->option('pass'),
268 $vitelity->$command(@args);
272 my( $self, $svc_phone ) = (shift, shift);
274 return '' if $self->option('dry_run');
276 #we want to provision and catch errors now, not queue
278 #porting a number in? different code path
279 if ( $svc_phone->lnp_status eq 'portingin' ) {
281 my %location = $svc_phone->location_hash;
282 my $sa = Geo::StreetAddress::US->parse_location( $location{'address1'} );
284 my $result = $self->vitelity_lnp_command('addport',
285 'portnumber' => $svc_phone->phonenum,
288 'carrier' => $svc_phone->lnp_other_provider,
289 'company' => $svc_phone->cust_svc->cust_pkg->cust_main->company,
290 'accnumber' => $svc_phone->lnp_other_provider_account,
291 'name' => $svc_phone->phone_name_or_cust,
292 'streetnumber' => $sa->{number},
293 'streetprefix' => $sa->{prefix},
294 'streetname' => $sa->{street}. ' '. $street{type},
295 'streetsuffix' => $sa->{suffix},
296 'unit' => ( $sa->{sec_unit_num}
297 ? $sa->{sec_unit_type}. ' '. $sa->{sec_unit_num}
300 'city' => $location{'city'},
301 'state' => $location{'state'},
302 'zip' => $location{'zip'},
303 'billnumber' => $svc_phone->phonenum, #?? do we need a new field for this?
304 'contactnumber' => $svc_phone->cust_svc->cust_pkg->cust_main->daytime,
307 if ( $result =~ /^ok:/i ) {
308 my($ok, $portid, $sig, $bill) = split(':', $result);
309 $svc_phone->lnp_portid($portid);
310 $svc_phone->lnp_signature('Y') if $sig =~ /y/i;
311 $svc_phone->lnp_bill('Y') if $bill =~ /y/i;
312 return $svc_phone->replace;
314 return "Error initiating Vitelity port: $result";
320 # 1. provision the DID
323 my %vparams = ( 'did' => $svc_phone->phonenum );
324 $vparams{'routesip'} = $self->option('routesip')
325 if defined $self->option('routesip');
326 $vparams{'type'} = $self->option('type')
327 if defined $self->option('type');
329 my $command = 'getlocaldid';
330 my $success = 'success';
332 # this is OK as Vitelity for now is US/CA only; it's not a hack
333 $command = 'gettollfree' if $vparams{'did'} =~ /^800|^888|^877|^866|^855/;
335 if ($self->option('fax')) {
340 my $result = $self->vitelity_command($command,%vparams);
342 if ( $result ne $success ) {
343 return "Error running Vitelity $command: $result";
350 my $cnam_result = $self->vitelity_command('cnamenable',
351 'did'=>$svc_phone->phonenum,
353 if ( $result ne 'ok' ) {
354 #we already provisioned the DID, so...
355 warn "Vitelity error enabling CNAM for ". $svc_phone->phonenum. ": $result";
362 my $e911_error = $self->e911_send($svc_phone);
364 if ( $e911_error =~ /^(missingdata|invalid)/i ) {
365 #but we already provisioned the DID, so:
366 $self->vitelity_command('removedid', 'did'=> $svc_phone->phonenum,);
367 #and check the results? if it failed, then what?
376 my($self, $svc_phone) = (shift, shift);
378 return '' if $self->option('disable_e911');
380 my %location = $svc_phone->location_hash;
382 'did' => $svc_phone->phonenum,
383 'name' => $svc_phone->phone_name_or_cust,
384 'address' => $location{'address1'},
385 'city' => $location{'city'},
386 'state' => $location{'state'},
387 'zip' => $location{'zip'},
389 if ( $location{address2} =~ /^\s*(\w+)\W*(\d+)\s*$/ ) {
390 $e911send{'unittype'} = $1;
391 $e911send{'unitnumber'} = $2;
394 my $e911_result = $self->vitelity_command('e911send', %e911send);
396 return '' unless $result =~ /^(missingdata|invalid)/i;
398 return "Vitelity error provisioning E911 for". $svc_phone->phonenum.
402 sub _export_replace {
403 my( $self, $new, $old ) = (shift, shift, shift);
406 if( $old->forwarddst ne $new->forwarddst ) {
407 my $result = $self->vitelity_command('callfw',
408 'did' => $old->phonenum,
409 'forward' => $new->forwarddst ? $new->forwarddst : 'none',
411 if ( $result ne 'ok' ) {
412 return "Error running Vitelity callfw: $result";
416 # vfax forwarding emails
417 if( $old->email ne $new->email && $self->option('fax') ) {
418 my $result = $self->vitelity_command('changeemail',
419 'did' => $old->phonenum,
420 'emails' => $new->email ? $new->email : '',
422 if ( $result ne 'ok' ) {
423 return "Error running Vitelity changeemail: $result";
427 $self->e911_send($new);
431 my( $self, $svc_phone ) = (shift, shift);
433 return '' if $self->option('dry_run');
435 #probably okay to queue the deletion...?
436 #but hell, let's do it inline anyway, who wants phone numbers hanging around
438 return 'Deleting vfax DIDs is unsupported by Vitelity API' if $self->option('fax');
440 my $result = $self->vitelity_command('removedid',
441 'did' => $svc_phone->phonenum,
444 if ( $result ne 'success' ) {
445 return "Error running Vitelity removedid: $result";
448 return '' if $self->option('disable_e911');
453 sub _export_suspend {
454 my( $self, $svc_phone ) = (shift, shift);
459 sub _export_unsuspend {
460 my( $self, $svc_phone ) = (shift, shift);
468 my @export_svc = $self->export_svc;
469 return unless @export_svc;
471 my $in_svcpart = 'IN ('. join( ',', map $_->svcpart, @export_svc). ')';
473 foreach my $svc_phone (
474 qsearch({ 'table' => 'svc_phone',
475 'addl_from' => 'LEFT JOIN cust_svc USING (svcnum)',
476 'hashref' => {lnp_status=>'portingin'},
477 'extra_sql' => "AND svcpart $in_svcpart",
481 my $result = $self->vitelity_lnp_command('checkstatus',
482 'portid'=>$svc_phone->lnp_portid,
485 if ( $result =~ /^Complete/i ) {
487 $svc_phone->lnp_status('portedin');
488 my $error = $self->_export_insert($svc_phone);
490 #XXX log this using our internal log instead, so we can alert on it
492 warn "ERROR provisioning ported-in DID ". $svc_phone->phonenum. ": $error";
494 $error = $svc_phone->replace; #to set the lnp_status
495 #XXX log this using our internal log instead, so we can alert on it
496 warn "ERROR setting lnp_status for DID ". $svc_phone->phonenum. ": $error" if $error;
499 } elsif ( $result ne $svc_phone->lnp_reject_reason ) {
500 $svc_phone->lnp_reject_reason($result);
501 $error = $svc_phone->replace;
502 #XXX log this using our internal log instead, so we can alert on it
503 warn "ERROR setting lnp_reject_reason for DID ". $svc_phone->phonenum. ": $error" if $error;