1 package FS::part_export::vitelity;
2 use base qw( FS::part_export );
8 use Geo::StreetAddress::US;
9 use Net::Vitelity 0.05;
10 use FS::Record qw( qsearch dbh );
14 tie my %options, 'Tie::IxHash',
15 'login' => { label=>'Vitelity API login' },
16 'pass' => { label=>'Vitelity API password' },
17 'routesip' => { label=>'routesip (optional sub-account)' },
18 'type' => { label=>'type (optional DID type to order)' },
19 'fax' => { label=>'vfax service', type=>'checkbox' },
20 'restrict_selection' => { type => 'select',
21 label => 'Restrict DID Selection',
22 options => [ '', 'tollfree', 'non-tollfree' ],
24 'dry_run' => { label => "Test mode - don't actually provision",
27 'disable_e911' => { label => "Disable E911 provisioning",
30 'debug' => { label => 'Enable debugging',
38 'desc' => 'Provision phone numbers to Vitelity',
39 'options' => \%options,
42 routesip - optional Vitelity sub-account to which newly ordered DIDs will be routed
43 <br>type - optional DID type (perminute, unlimited, or your-pri)
47 sub rebless { shift; }
49 sub can_get_dids { 1; }
50 sub get_dids_can_tollfree { 1; };
55 my %opt = ref($_[0]) ? %{$_[0]} : @_;
57 if ( $opt{'tollfree'} ) {
58 my $command = 'listtollfree';
59 $command = 'listdids' if $self->option('fax');
60 my @tollfree = $self->vitelity_command($command);
63 return [] if ( $tollfree[0] eq 'noneavailable' || $tollfree[0] eq 'none');
65 foreach my $did ( @tollfree ) {
66 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable toll-free did $did\n";
70 my @sorted_ret = sort @ret;
73 } elsif ( $opt{'ratecenter'} && $opt{'state'} ) {
75 my %flushopts = ( 'state' => $opt{'state'},
76 'ratecenter' => $opt{'ratecenter'},
77 'exportnum' => $self->exportnum
79 FS::phone_avail::flush( \%flushopts );
81 local $SIG{HUP} = 'IGNORE';
82 local $SIG{INT} = 'IGNORE';
83 local $SIG{QUIT} = 'IGNORE';
84 local $SIG{TERM} = 'IGNORE';
85 local $SIG{TSTP} = 'IGNORE';
86 local $SIG{PIPE} = 'IGNORE';
88 my $oldAutoCommit = $FS::UID::AutoCommit;
89 local $FS::UID::AutoCommit = 0;
92 my $errmsg = 'WARNING: error populating phone availability cache: ';
94 my $command = 'listlocal';
95 $command = 'listdids' if $self->option('fax');
96 my @dids = $self->vitelity_command( $command,
97 'state' => $opt{'state'},
98 'ratecenter' => $opt{'ratecenter'},
100 # XXX: Options: type=unlimited OR type=pri
102 next if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' );
103 die "missingdata error running Vitelity API" if $dids[0] eq 'missingdata';
105 foreach my $did ( @dids ) {
106 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable (state and ratecenter) did $did\n";
107 my($npa, $nxx, $station) = ($1, $2, $3);
109 my $phone_avail = new FS::phone_avail {
110 'exportnum' => $self->exportnum,
111 'countrycode' => '1', # vitelity is US/CA only now
112 'state' => $opt{'state'},
115 'station' => $station,
116 'name' => $opt{'ratecenter'},
119 my $error = $phone_avail->insert();
121 $dbh->rollback if $oldAutoCommit;
126 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
129 map { join('-', $_->npa, $_->nxx, $_->station ) }
131 'table' => 'phone_avail',
132 'hashref' => { 'exportnum' => $self->exportnum,
133 'countrycode' => '1', # vitelity is US/CA only now
134 'name' => $opt{'ratecenter'},
135 'state' => $opt{'state'},
137 'order_by' => 'ORDER BY npa, nxx, station',
141 } elsif ( $opt{'areacode'} ) {
143 my @rc = map { $_->{'Hash'}->{name}.", ".$_->state }
145 'select' => 'DISTINCT name, state',
146 'table' => 'phone_avail',
147 'hashref' => { 'exportnum' => $self->exportnum,
148 'countrycode' => '1', # vitelity is US/CA only now
149 'npa' => $opt{'areacode'},
153 my @sorted_rc = sort @rc;
154 return [ @sorted_rc ];
156 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
158 my @avail = qsearch({
159 'select' => 'DISTINCT npa',
160 'table' => 'phone_avail',
161 'hashref' => { 'exportnum' => $self->exportnum,
162 'countrycode' => '1', # vitelity is US/CA only now
163 'state' => $opt{'state'},
165 'order_by' => 'ORDER BY npa',
168 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
170 #otherwise, search for em
172 my $command = 'listavailratecenters';
173 $command = 'listratecenters' if $self->option('fax');
174 my @ratecenters = $self->vitelity_command( $command,
175 'state' => $opt{'state'},
177 # XXX: Options: type=unlimited OR type=pri
179 if ( $ratecenters[0] eq 'unavailable' || $ratecenters[0] eq 'none' ) {
181 } elsif ( $ratecenters[0] eq 'missingdata' ) {
182 die "missingdata error running Vitelity API"; #die?
185 local $SIG{HUP} = 'IGNORE';
186 local $SIG{INT} = 'IGNORE';
187 local $SIG{QUIT} = 'IGNORE';
188 local $SIG{TERM} = 'IGNORE';
189 local $SIG{TSTP} = 'IGNORE';
190 local $SIG{PIPE} = 'IGNORE';
192 my $oldAutoCommit = $FS::UID::AutoCommit;
193 local $FS::UID::AutoCommit = 0;
196 my $errmsg = 'WARNING: error populating phone availability cache: ';
199 foreach my $ratecenter (@ratecenters) {
201 my $command = 'listlocal';
202 $command = 'listdids' if $self->option('fax');
203 my @dids = $self->vitelity_command( $command,
204 'state' => $opt{'state'},
205 'ratecenter' => $ratecenter,
207 # XXX: Options: type=unlimited OR type=pri
209 if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' ) {
211 } elsif ( $dids[0] eq 'missingdata' ) {
212 die "missingdata error running Vitelity API"; #die?
215 foreach my $did ( @dids ) {
216 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable (state) did $did\n";
217 my($npa, $nxx, $station) = ($1, $2, $3);
220 my $phone_avail = new FS::phone_avail {
221 'exportnum' => $self->exportnum,
222 'countrycode' => '1', # vitelity is US/CA only now
223 'state' => $opt{'state'},
226 'station' => $station,
227 'name' => $ratecenter,
230 my $error = $phone_avail->insert();
232 $dbh->rollback if $oldAutoCommit;
240 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
242 my @return = sort { $a <=> $b } keys %npa;
246 die "get_dids called without state or areacode options";
251 sub vitelity_command {
252 my( $self, $command, @args ) = @_;
254 my $vitelity = Net::Vitelity->new(
255 'login' => $self->option('login'),
256 'pass' => $self->option('pass'),
257 'apitype' => $self->option('fax') ? 'fax' : 'api',
258 'debug' => $self->option('debug'),
261 $vitelity->$command(@args);
264 sub vitelity_lnp_command {
265 my( $self, $command, @args ) = @_;
267 my $vitelity = Net::Vitelity->new(
268 'login' => $self->option('login'),
269 'pass' => $self->option('pass'),
271 'debug' => $self->option('debug'),
274 $vitelity->$command(@args);
278 my( $self, $svc_phone ) = (shift, shift);
280 return '' if $self->option('dry_run');
282 #we want to provision and catch errors now, not queue
284 #porting a number in? different code path
285 if ( $svc_phone->lnp_status eq 'portingin' ) {
287 my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
289 return 'Customer company is required'
290 unless $cust_main->company;
292 return 'Customer day phone (for contact, not porting) is required'
293 unless $cust_main->daytime;
295 return 'LNP Other Provider is required'
296 unless $svc_phone->lnp_other_provider;
298 return 'LNP Other Provider Account # is required'
299 unless $svc_phone->lnp_other_provider_account;
301 my %location = $svc_phone->location_hash;
302 my $sa = Geo::StreetAddress::US->parse_location( $location{'address1'} );
304 my $result = $self->vitelity_lnp_command('addport',
305 'portnumber' => $svc_phone->phonenum,
308 'carrier' => $svc_phone->lnp_other_provider,
309 'company' => $cust_main->company,
310 'accnumber' => $svc_phone->lnp_other_provider_account,
311 'name' => $svc_phone->phone_name_or_cust,
312 'streetnumber' => $sa->{number},
313 'streetprefix' => $sa->{prefix},
314 'streetname' => $sa->{street}. ' '. $sa->{type},
315 'streetsuffix' => $sa->{suffix},
316 'unit' => ( $sa->{sec_unit_num}
317 ? $sa->{sec_unit_type}. ' '. $sa->{sec_unit_num}
320 'city' => $location{'city'},
321 'state' => $location{'state'},
322 'zip' => $location{'zip'},
323 'billnumber' => $svc_phone->phonenum, #?? do we need a new field for this?
324 'contactnumber' => $cust_main->daytime,
326 warn "Vitelity response: $result" if $self->option('debug');
328 if ( $result =~ /^ok:/i ) {
329 my($ok, $portid, $sig, $bill) = split(':', $result);
330 $svc_phone->lnp_portid($portid);
331 $svc_phone->lnp_signature('Y') if $sig =~ /y/i;
332 $svc_phone->lnp_bill('Y') if $bill =~ /y/i;
333 local($FS::svc_Common::noexport_hack) = 1;
334 return $svc_phone->replace;
336 return "Error initiating Vitelity port: $result";
342 # 1. provision the DID
345 my %vparams = ( 'did' => $svc_phone->phonenum );
346 $vparams{'routesip'} = $self->option('routesip')
347 if defined $self->option('routesip');
348 $vparams{'type'} = $self->option('type')
349 if defined $self->option('type');
351 my $command = 'getlocaldid';
352 my $success = 'success';
354 # this is OK as Vitelity for now is US/CA only; it's not a hack
355 $command = 'gettollfree' if $vparams{'did'} =~ /^800|^888|^877|^866|^855/;
357 if ($self->option('fax')) {
362 my $result = $self->vitelity_command($command,%vparams);
364 if ( $result ne $success ) {
365 return "Error running Vitelity $command: $result";
366 } elsif ( $self->option('debug') ) {
367 warn "Vitelity response: $result";
374 my $cnam_result = $self->vitelity_command('cnamenable',
375 'did'=>$svc_phone->phonenum,
377 if ( $result !~ /^(ok|success)/i ) {
378 #we already provisioned the DID, so...
379 warn "Vitelity error enabling CNAM for ". $svc_phone->phonenum. ": $result";
380 } elsif ( $self->option('debug') ) {
381 warn "Vitelity response: $result";
388 my $e911_error = $self->e911_send($svc_phone);
390 if ( $e911_error =~ /^(missingdata|invalid)/i ) {
391 #but we already provisioned the DID, so:
392 $self->vitelity_command('removedid', 'did'=> $svc_phone->phonenum,);
393 #and check the results? if it failed, then what?
402 my($self, $svc_phone) = (shift, shift);
404 return '' if $self->option('disable_e911');
406 my %location = $svc_phone->location_hash;
408 'did' => $svc_phone->phonenum,
409 'name' => $svc_phone->phone_name_or_cust,
410 'address' => $location{'address1'},
411 'city' => $location{'city'},
412 'state' => $location{'state'},
413 'zip' => $location{'zip'},
415 if ( $location{address2} =~ /^\s*(\w+)\W*(\d+)\s*$/ ) {
416 $e911send{'unittype'} = $1;
417 $e911send{'unitnumber'} = $2;
420 my $e911_result = $self->vitelity_command('e911send', %e911send);
422 unless ( $e911_result =~ /^(missingdata|invalid)/i ) {
423 warn "Vitelity response: $e911_result" if $self->option('debug');
427 return "Vitelity error provisioning E911 for". $svc_phone->phonenum.
431 sub _export_replace {
432 my( $self, $new, $old ) = (shift, shift, shift);
435 if ( $old->forwarddst ne $new->forwarddst ) {
436 my $result = $self->vitelity_command('callfw',
437 'did' => $old->phonenum,
438 'forward' => $new->forwarddst ? $new->forwarddst : 'none',
440 if ( $result ne 'ok' ) {
441 return "Error running Vitelity callfw: $result";
442 } elsif ( $self->option('debug') ) {
443 warn "Vitelity response: $result";
447 # vfax forwarding emails
448 if ( $old->email ne $new->email && $self->option('fax') ) {
449 my $result = $self->vitelity_command('changeemail',
450 'did' => $old->phonenum,
451 'emails' => $new->email ? $new->email : '',
453 if ( $result ne 'ok' ) {
454 return "Error running Vitelity changeemail: $result";
455 } elsif ( $self->option('debug') ) {
456 warn "Vitelity response: $result";
460 $self->e911_send($new);
464 my( $self, $svc_phone ) = (shift, shift);
466 return '' if $self->option('dry_run');
468 #probably okay to queue the deletion...?
469 #but hell, let's do it inline anyway, who wants phone numbers hanging around
471 return 'Deleting vfax DIDs is unsupported by Vitelity API' if $self->option('fax');
473 my $result = $self->vitelity_command('removedid',
474 'did' => $svc_phone->phonenum,
477 if ( $result ne 'success' ) {
478 return "Error running Vitelity removedid: $result";
479 } elsif ( $self->option('debug') ) {
480 warn "Vitelity response: $result";
486 sub _export_suspend {
487 my( $self, $svc_phone ) = (shift, shift);
492 sub _export_unsuspend {
493 my( $self, $svc_phone ) = (shift, shift);
501 my @export_svc = $self->export_svc;
502 return unless @export_svc;
504 my $in_svcpart = 'IN ('. join( ',', map $_->svcpart, @export_svc). ')';
506 foreach my $svc_phone (
507 qsearch({ 'table' => 'svc_phone',
508 'addl_from' => 'LEFT JOIN cust_svc USING (svcnum)',
509 'hashref' => {lnp_status=>'portingin'},
510 'extra_sql' => "AND svcpart $in_svcpart",
514 my $result = $self->vitelity_lnp_command('checkstatus',
515 'portid'=>$svc_phone->lnp_portid,
518 if ( $result =~ /^Complete/i ) {
520 $svc_phone->lnp_status('portedin');
521 my $error = $self->_export_insert($svc_phone);
523 #XXX log this using our internal log instead, so we can alert on it
525 warn "ERROR provisioning ported-in DID ". $svc_phone->phonenum. ": $error";
527 local($FS::svc_Common::noexport_hack) = 1;
528 $error = $svc_phone->replace; #to set the lnp_status
529 #XXX log this using our internal log instead, so we can alert on it
530 warn "ERROR setting lnp_status for DID ". $svc_phone->phonenum. ": $error" if $error;
533 } elsif ( $result ne $svc_phone->lnp_reject_reason ) {
534 $svc_phone->lnp_reject_reason($result);
535 local($FS::svc_Common::noexport_hack) = 1;
536 my $error = $svc_phone->replace;
537 #XXX log this using our internal log instead, so we can alert on it
538 warn "ERROR setting lnp_reject_reason for DID ". $svc_phone->phonenum. ": $error" if $error;