1 package FS::part_export::vitelity;
2 use base qw( FS::part_export );
7 use Geo::StreetAddress::US;
8 use Net::Vitelity 0.05;
9 use FS::Record qw( qsearch dbh );
13 tie my %options, 'Tie::IxHash',
14 'login' => { label=>'Vitelity API login' },
15 'pass' => { label=>'Vitelity API password' },
16 'routesip' => { label=>'routesip (optional sub-account)' },
17 'type' => { label=>'type (optional DID type to order)' },
18 'fax' => { label=>'vfax service', type=>'checkbox' },
19 'restrict_selection' => { type => 'select',
20 label => 'Restrict DID Selection',
21 options => [ '', 'tollfree', 'non-tollfree' ],
23 'dry_run' => { label => "Test mode - don't actually provision",
26 'disable_e911' => { label => "Disable E911 provisioning",
29 'debug' => { label => 'Enable debugging',
37 'desc' => 'Provision phone numbers to Vitelity',
38 'options' => \%options,
41 routesip - optional Vitelity sub-account to which newly ordered DIDs will be routed
42 <br>type - optional DID type (perminute, unlimited, or your-pri)
46 sub rebless { shift; }
48 sub can_get_dids { 1; }
49 sub get_dids_can_tollfree { 1; };
54 my %opt = ref($_[0]) ? %{$_[0]} : @_;
56 if ( $opt{'tollfree'} ) {
57 my $command = 'listtollfree';
58 $command = 'listdids' if $self->option('fax');
59 my @tollfree = $self->vitelity_command($command);
62 return [] if ( $tollfree[0] eq 'noneavailable' || $tollfree[0] eq 'none');
64 foreach my $did ( @tollfree ) {
65 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable toll-free did $did\n";
69 my @sorted_ret = sort @ret;
72 } elsif ( $opt{'ratecenter'} && $opt{'state'} ) {
74 my %flushopts = ( 'state' => $opt{'state'},
75 'ratecenter' => $opt{'ratecenter'},
76 'exportnum' => $self->exportnum
78 FS::phone_avail::flush( \%flushopts );
80 local $SIG{HUP} = 'IGNORE';
81 local $SIG{INT} = 'IGNORE';
82 local $SIG{QUIT} = 'IGNORE';
83 local $SIG{TERM} = 'IGNORE';
84 local $SIG{TSTP} = 'IGNORE';
85 local $SIG{PIPE} = 'IGNORE';
87 my $oldAutoCommit = $FS::UID::AutoCommit;
88 local $FS::UID::AutoCommit = 0;
91 my $errmsg = 'WARNING: error populating phone availability cache: ';
93 my $command = 'listlocal';
94 $command = 'listdids' if $self->option('fax');
95 my @dids = $self->vitelity_command( $command,
96 'state' => $opt{'state'},
97 'ratecenter' => $opt{'ratecenter'},
99 # XXX: Options: type=unlimited OR type=pri
101 next if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' );
102 die "missingdata error running Vitelity API" if $dids[0] eq 'missingdata';
104 foreach my $did ( @dids ) {
105 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable (state and ratecenter) did $did\n";
106 my($npa, $nxx, $station) = ($1, $2, $3);
108 my $phone_avail = new FS::phone_avail {
109 'exportnum' => $self->exportnum,
110 'countrycode' => '1', # vitelity is US/CA only now
111 'state' => $opt{'state'},
114 'station' => $station,
115 'name' => $opt{'ratecenter'},
118 my $error = $phone_avail->insert();
120 $dbh->rollback if $oldAutoCommit;
125 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
128 map { join('-', $_->npa, $_->nxx, $_->station ) }
130 'table' => 'phone_avail',
131 'hashref' => { 'exportnum' => $self->exportnum,
132 'countrycode' => '1', # vitelity is US/CA only now
133 'name' => $opt{'ratecenter'},
134 'state' => $opt{'state'},
136 'order_by' => 'ORDER BY npa, nxx, station',
140 } elsif ( $opt{'areacode'} ) {
142 my @rc = map { $_->{'Hash'}->{name}.", ".$_->state }
144 'select' => 'DISTINCT name, state',
145 'table' => 'phone_avail',
146 'hashref' => { 'exportnum' => $self->exportnum,
147 'countrycode' => '1', # vitelity is US/CA only now
148 'npa' => $opt{'areacode'},
152 my @sorted_rc = sort @rc;
153 return [ @sorted_rc ];
155 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
157 my @avail = qsearch({
158 'select' => 'DISTINCT npa',
159 'table' => 'phone_avail',
160 'hashref' => { 'exportnum' => $self->exportnum,
161 'countrycode' => '1', # vitelity is US/CA only now
162 'state' => $opt{'state'},
164 'order_by' => 'ORDER BY npa',
167 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
169 #otherwise, search for em
171 my $command = 'listavailratecenters';
172 $command = 'listratecenters' if $self->option('fax');
173 my @ratecenters = $self->vitelity_command( $command,
174 'state' => $opt{'state'},
176 # XXX: Options: type=unlimited OR type=pri
178 if ( $ratecenters[0] eq 'unavailable' || $ratecenters[0] eq 'none' ) {
180 } elsif ( $ratecenters[0] eq 'missingdata' ) {
181 die "missingdata error running Vitelity API"; #die?
184 local $SIG{HUP} = 'IGNORE';
185 local $SIG{INT} = 'IGNORE';
186 local $SIG{QUIT} = 'IGNORE';
187 local $SIG{TERM} = 'IGNORE';
188 local $SIG{TSTP} = 'IGNORE';
189 local $SIG{PIPE} = 'IGNORE';
191 my $oldAutoCommit = $FS::UID::AutoCommit;
192 local $FS::UID::AutoCommit = 0;
195 my $errmsg = 'WARNING: error populating phone availability cache: ';
198 foreach my $ratecenter (@ratecenters) {
200 my $command = 'listlocal';
201 $command = 'listdids' if $self->option('fax');
202 my @dids = $self->vitelity_command( $command,
203 'state' => $opt{'state'},
204 'ratecenter' => $ratecenter,
206 # XXX: Options: type=unlimited OR type=pri
208 if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' ) {
210 } elsif ( $dids[0] eq 'missingdata' ) {
211 die "missingdata error running Vitelity API"; #die?
214 foreach my $did ( @dids ) {
215 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable (state) did $did\n";
216 my($npa, $nxx, $station) = ($1, $2, $3);
219 my $phone_avail = new FS::phone_avail {
220 'exportnum' => $self->exportnum,
221 'countrycode' => '1', # vitelity is US/CA only now
222 'state' => $opt{'state'},
225 'station' => $station,
226 'name' => $ratecenter,
229 my $error = $phone_avail->insert();
231 $dbh->rollback if $oldAutoCommit;
239 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
241 my @return = sort { $a <=> $b } keys %npa;
245 die "get_dids called without state or areacode options";
250 sub vitelity_command {
251 my( $self, $command, @args ) = @_;
253 my $vitelity = Net::Vitelity->new(
254 'login' => $self->option('login'),
255 'pass' => $self->option('pass'),
256 'apitype' => $self->option('fax') ? 'fax' : 'api',
257 'debug' => $self->option('debug'),
260 $vitelity->$command(@args);
263 sub vitelity_lnp_command {
264 my( $self, $command, @args ) = @_;
266 my $vitelity = Net::Vitelity->new(
267 'login' => $self->option('login'),
268 'pass' => $self->option('pass'),
270 'debug' => $self->option('debug'),
273 $vitelity->$command(@args);
277 my( $self, $svc_phone ) = (shift, shift);
279 return '' if $self->option('dry_run');
281 #we want to provision and catch errors now, not queue
283 #porting a number in? different code path
284 if ( $svc_phone->lnp_status eq 'portingin' ) {
286 my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
288 return 'Customer company is required'
289 unless $cust_main->company;
291 return 'Customer day phone (for contact, not porting) is required'
292 unless $cust_main->daytime;
294 return 'LNP Other Provider is required'
295 unless $svc_phone->lnp_other_provider;
297 return 'LNP Other Provider Account # is required'
298 unless $svc_phone->lnp_other_provider_account;
300 my %location = $svc_phone->location_hash;
301 my $sa = Geo::StreetAddress::US->parse_location( $location{'address1'} );
303 my $result = $self->vitelity_lnp_command('addport',
304 'portnumber' => $svc_phone->phonenum,
307 'carrier' => $svc_phone->lnp_other_provider,
308 'company' => $cust_main->company,
309 'accnumber' => $svc_phone->lnp_other_provider_account,
310 'name' => $svc_phone->phone_name_or_cust,
311 'streetnumber' => $sa->{number},
312 'streetprefix' => $sa->{prefix},
313 'streetname' => $sa->{street}. ' '. $street{type},
314 'streetsuffix' => $sa->{suffix},
315 'unit' => ( $sa->{sec_unit_num}
316 ? $sa->{sec_unit_type}. ' '. $sa->{sec_unit_num}
319 'city' => $location{'city'},
320 'state' => $location{'state'},
321 'zip' => $location{'zip'},
322 'billnumber' => $svc_phone->phonenum, #?? do we need a new field for this?
323 'contactnumber' => $cust_main->daytime,
326 warn "Vitelity reponse: $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";
372 my $cnam_result = $self->vitelity_command('cnamenable',
373 'did'=>$svc_phone->phonenum,
375 if ( $result ne 'ok' ) {
376 #we already provisioned the DID, so...
377 warn "Vitelity error enabling CNAM for ". $svc_phone->phonenum. ": $result";
384 my $e911_error = $self->e911_send($svc_phone);
386 if ( $e911_error =~ /^(missingdata|invalid)/i ) {
387 #but we already provisioned the DID, so:
388 $self->vitelity_command('removedid', 'did'=> $svc_phone->phonenum,);
389 #and check the results? if it failed, then what?
398 my($self, $svc_phone) = (shift, shift);
400 return '' if $self->option('disable_e911');
402 my %location = $svc_phone->location_hash;
404 'did' => $svc_phone->phonenum,
405 'name' => $svc_phone->phone_name_or_cust,
406 'address' => $location{'address1'},
407 'city' => $location{'city'},
408 'state' => $location{'state'},
409 'zip' => $location{'zip'},
411 if ( $location{address2} =~ /^\s*(\w+)\W*(\d+)\s*$/ ) {
412 $e911send{'unittype'} = $1;
413 $e911send{'unitnumber'} = $2;
416 my $e911_result = $self->vitelity_command('e911send', %e911send);
418 return '' unless $result =~ /^(missingdata|invalid)/i;
420 return "Vitelity error provisioning E911 for". $svc_phone->phonenum.
424 sub _export_replace {
425 my( $self, $new, $old ) = (shift, shift, shift);
428 if( $old->forwarddst ne $new->forwarddst ) {
429 my $result = $self->vitelity_command('callfw',
430 'did' => $old->phonenum,
431 'forward' => $new->forwarddst ? $new->forwarddst : 'none',
433 if ( $result ne 'ok' ) {
434 return "Error running Vitelity callfw: $result";
438 # vfax forwarding emails
439 if( $old->email ne $new->email && $self->option('fax') ) {
440 my $result = $self->vitelity_command('changeemail',
441 'did' => $old->phonenum,
442 'emails' => $new->email ? $new->email : '',
444 if ( $result ne 'ok' ) {
445 return "Error running Vitelity changeemail: $result";
449 $self->e911_send($new);
453 my( $self, $svc_phone ) = (shift, shift);
455 return '' if $self->option('dry_run');
457 #probably okay to queue the deletion...?
458 #but hell, let's do it inline anyway, who wants phone numbers hanging around
460 return 'Deleting vfax DIDs is unsupported by Vitelity API' if $self->option('fax');
462 my $result = $self->vitelity_command('removedid',
463 'did' => $svc_phone->phonenum,
466 if ( $result ne 'success' ) {
467 return "Error running Vitelity removedid: $result";
470 return '' if $self->option('disable_e911');
475 sub _export_suspend {
476 my( $self, $svc_phone ) = (shift, shift);
481 sub _export_unsuspend {
482 my( $self, $svc_phone ) = (shift, shift);
490 my @export_svc = $self->export_svc;
491 return unless @export_svc;
493 my $in_svcpart = 'IN ('. join( ',', map $_->svcpart, @export_svc). ')';
495 foreach my $svc_phone (
496 qsearch({ 'table' => 'svc_phone',
497 'addl_from' => 'LEFT JOIN cust_svc USING (svcnum)',
498 'hashref' => {lnp_status=>'portingin'},
499 'extra_sql' => "AND svcpart $in_svcpart",
503 my $result = $self->vitelity_lnp_command('checkstatus',
504 'portid'=>$svc_phone->lnp_portid,
507 if ( $result =~ /^Complete/i ) {
509 $svc_phone->lnp_status('portedin');
510 my $error = $self->_export_insert($svc_phone);
512 #XXX log this using our internal log instead, so we can alert on it
514 warn "ERROR provisioning ported-in DID ". $svc_phone->phonenum. ": $error";
516 local($FS::svc_Common::noexport_hack) = 1;
517 $error = $svc_phone->replace; #to set the lnp_status
518 #XXX log this using our internal log instead, so we can alert on it
519 warn "ERROR setting lnp_status for DID ". $svc_phone->phonenum. ": $error" if $error;
522 } elsif ( $result ne $svc_phone->lnp_reject_reason ) {
523 $svc_phone->lnp_reject_reason($result);
524 local($FS::svc_Common::noexport_hack) = 1;
525 $error = $svc_phone->replace;
526 #XXX log this using our internal log instead, so we can alert on it
527 warn "ERROR setting lnp_reject_reason for DID ". $svc_phone->phonenum. ": $error" if $error;