1 package FS::part_export::vitelity;
2 use base qw( FS::part_export );
6 use Geo::StreetAddress::US;
7 use Net::Vitelity 0.05;
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",
28 'debug' => { label => 'Enable debugging',
36 'desc' => 'Provision phone numbers to Vitelity',
37 'options' => \%options,
40 routesip - optional Vitelity sub-account to which newly ordered DIDs will be routed
41 <br>type - optional DID type (perminute, unlimited, or your-pri)
45 sub rebless { shift; }
47 sub can_get_dids { 1; }
48 sub get_dids_can_tollfree { 1; };
53 my %opt = ref($_[0]) ? %{$_[0]} : @_;
55 if ( $opt{'tollfree'} ) {
56 my $command = 'listtollfree';
57 $command = 'listdids' if $self->option('fax');
58 my @tollfree = $self->vitelity_command($command);
61 return [] if ( $tollfree[0] eq 'noneavailable' || $tollfree[0] eq 'none');
63 foreach my $did ( @tollfree ) {
64 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable toll-free did $did\n";
68 my @sorted_ret = sort @ret;
71 } elsif ( $opt{'ratecenter'} && $opt{'state'} ) {
73 my %flushopts = ( 'state' => $opt{'state'},
74 'ratecenter' => $opt{'ratecenter'},
75 'exportnum' => $self->exportnum
77 FS::phone_avail::flush( \%flushopts );
79 local $SIG{HUP} = 'IGNORE';
80 local $SIG{INT} = 'IGNORE';
81 local $SIG{QUIT} = 'IGNORE';
82 local $SIG{TERM} = 'IGNORE';
83 local $SIG{TSTP} = 'IGNORE';
84 local $SIG{PIPE} = 'IGNORE';
86 my $oldAutoCommit = $FS::UID::AutoCommit;
87 local $FS::UID::AutoCommit = 0;
90 my $errmsg = 'WARNING: error populating phone availability cache: ';
92 my $command = 'listlocal';
93 $command = 'listdids' if $self->option('fax');
94 my @dids = $self->vitelity_command( $command,
95 'state' => $opt{'state'},
96 'ratecenter' => $opt{'ratecenter'},
98 # XXX: Options: type=unlimited OR type=pri
100 next if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' );
101 die "missingdata error running Vitelity API" if $dids[0] eq 'missingdata';
103 foreach my $did ( @dids ) {
104 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable (state and ratecenter) did $did\n";
105 my($npa, $nxx, $station) = ($1, $2, $3);
107 my $phone_avail = new FS::phone_avail {
108 'exportnum' => $self->exportnum,
109 'countrycode' => '1', # vitelity is US/CA only now
110 'state' => $opt{'state'},
113 'station' => $station,
114 'name' => $opt{'ratecenter'},
117 my $error = $phone_avail->insert();
119 $dbh->rollback if $oldAutoCommit;
124 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
127 map { join('-', $_->npa, $_->nxx, $_->station ) }
129 'table' => 'phone_avail',
130 'hashref' => { 'exportnum' => $self->exportnum,
131 'countrycode' => '1', # vitelity is US/CA only now
132 'name' => $opt{'ratecenter'},
133 'state' => $opt{'state'},
135 'order_by' => 'ORDER BY npa, nxx, station',
139 } elsif ( $opt{'areacode'} ) {
141 my @rc = map { $_->{'Hash'}->{name}.", ".$_->state }
143 'select' => 'DISTINCT name, state',
144 'table' => 'phone_avail',
145 'hashref' => { 'exportnum' => $self->exportnum,
146 'countrycode' => '1', # vitelity is US/CA only now
147 'npa' => $opt{'areacode'},
151 my @sorted_rc = sort @rc;
152 return [ @sorted_rc ];
154 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
156 my @avail = qsearch({
157 'select' => 'DISTINCT npa',
158 'table' => 'phone_avail',
159 'hashref' => { 'exportnum' => $self->exportnum,
160 'countrycode' => '1', # vitelity is US/CA only now
161 'state' => $opt{'state'},
163 'order_by' => 'ORDER BY npa',
166 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
168 #otherwise, search for em
170 my $command = 'listavailratecenters';
171 $command = 'listratecenters' if $self->option('fax');
172 my @ratecenters = $self->vitelity_command( $command,
173 'state' => $opt{'state'},
175 # XXX: Options: type=unlimited OR type=pri
177 if ( $ratecenters[0] eq 'unavailable' || $ratecenters[0] eq 'none' ) {
179 } elsif ( $ratecenters[0] eq 'missingdata' ) {
180 die "missingdata error running Vitelity API"; #die?
183 local $SIG{HUP} = 'IGNORE';
184 local $SIG{INT} = 'IGNORE';
185 local $SIG{QUIT} = 'IGNORE';
186 local $SIG{TERM} = 'IGNORE';
187 local $SIG{TSTP} = 'IGNORE';
188 local $SIG{PIPE} = 'IGNORE';
190 my $oldAutoCommit = $FS::UID::AutoCommit;
191 local $FS::UID::AutoCommit = 0;
194 my $errmsg = 'WARNING: error populating phone availability cache: ';
197 foreach my $ratecenter (@ratecenters) {
199 my $command = 'listlocal';
200 $command = 'listdids' if $self->option('fax');
201 my @dids = $self->vitelity_command( $command,
202 'state' => $opt{'state'},
203 'ratecenter' => $ratecenter,
205 # XXX: Options: type=unlimited OR type=pri
207 if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' ) {
209 } elsif ( $dids[0] eq 'missingdata' ) {
210 die "missingdata error running Vitelity API"; #die?
213 foreach my $did ( @dids ) {
214 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable (state) did $did\n";
215 my($npa, $nxx, $station) = ($1, $2, $3);
218 my $phone_avail = new FS::phone_avail {
219 'exportnum' => $self->exportnum,
220 'countrycode' => '1', # vitelity is US/CA only now
221 'state' => $opt{'state'},
224 'station' => $station,
225 'name' => $ratecenter,
228 my $error = $phone_avail->insert();
230 $dbh->rollback if $oldAutoCommit;
238 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
240 my @return = sort { $a <=> $b } keys %npa;
244 die "get_dids called without state or areacode options";
249 sub vitelity_command {
250 my( $self, $command, @args ) = @_;
252 my $vitelity = Net::Vitelity->new(
253 'login' => $self->option('login'),
254 'pass' => $self->option('pass'),
255 'apitype' => $self->option('fax') ? 'fax' : 'api',
256 'debug' => $self->option('debug'),
259 $vitelity->$command(@args);
262 sub vitelity_lnp_command {
263 my( $self, $command, @args ) = @_;
265 my $vitelity = Net::Vitelity->new(
266 'login' => $self->option('login'),
267 'pass' => $self->option('pass'),
269 'debug' => $self->option('debug'),
272 $vitelity->$command(@args);
276 my( $self, $svc_phone ) = (shift, shift);
278 return '' if $self->option('dry_run');
280 #we want to provision and catch errors now, not queue
282 #porting a number in? different code path
283 if ( $svc_phone->lnp_status eq 'portingin' ) {
285 my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
287 return 'Customer company is required'
288 unless $cust_main->company;
290 return 'Customer day phone (for contact, not porting) is required'
291 unless $cust_main->daytime;
293 return 'LNP Other Provider is required'
294 unless $svc_phone->lnp_other_provider;
296 return 'LNP Other Provider Account # is required'
297 unless $svc_phone->lnp_other_provider_account;
299 my %location = $svc_phone->location_hash;
300 my $sa = Geo::StreetAddress::US->parse_location( $location{'address1'} );
302 my $result = $self->vitelity_lnp_command('addport',
303 'portnumber' => $svc_phone->phonenum,
306 'carrier' => $svc_phone->lnp_other_provider,
307 'company' => $cust_main->company,
308 'accnumber' => $svc_phone->lnp_other_provider_account,
309 'name' => $svc_phone->phone_name_or_cust,
310 'streetnumber' => $sa->{number},
311 'streetprefix' => $sa->{prefix},
312 'streetname' => $sa->{street}. ' '. $street{type},
313 'streetsuffix' => $sa->{suffix},
314 'unit' => ( $sa->{sec_unit_num}
315 ? $sa->{sec_unit_type}. ' '. $sa->{sec_unit_num}
318 'city' => $location{'city'},
319 'state' => $location{'state'},
320 'zip' => $location{'zip'},
321 'billnumber' => $svc_phone->phonenum, #?? do we need a new field for this?
322 'contactnumber' => $cust_main->daytime,
325 if ( $result =~ /^ok:/i ) {
326 my($ok, $portid, $sig, $bill) = split(':', $result);
327 $svc_phone->lnp_portid($portid);
328 $svc_phone->lnp_signature('Y') if $sig =~ /y/i;
329 $svc_phone->lnp_bill('Y') if $bill =~ /y/i;
330 return $svc_phone->replace;
332 return "Error initiating Vitelity port: $result";
338 # 1. provision the DID
341 my %vparams = ( 'did' => $svc_phone->phonenum );
342 $vparams{'routesip'} = $self->option('routesip')
343 if defined $self->option('routesip');
344 $vparams{'type'} = $self->option('type')
345 if defined $self->option('type');
347 my $command = 'getlocaldid';
348 my $success = 'success';
350 # this is OK as Vitelity for now is US/CA only; it's not a hack
351 $command = 'gettollfree' if $vparams{'did'} =~ /^800|^888|^877|^866|^855/;
353 if ($self->option('fax')) {
358 my $result = $self->vitelity_command($command,%vparams);
360 if ( $result ne $success ) {
361 return "Error running Vitelity $command: $result";
368 my $cnam_result = $self->vitelity_command('cnamenable',
369 'did'=>$svc_phone->phonenum,
371 if ( $result ne 'ok' ) {
372 #we already provisioned the DID, so...
373 warn "Vitelity error enabling CNAM for ". $svc_phone->phonenum. ": $result";
380 my $e911_error = $self->e911_send($svc_phone);
382 if ( $e911_error =~ /^(missingdata|invalid)/i ) {
383 #but we already provisioned the DID, so:
384 $self->vitelity_command('removedid', 'did'=> $svc_phone->phonenum,);
385 #and check the results? if it failed, then what?
394 my($self, $svc_phone) = (shift, shift);
396 return '' if $self->option('disable_e911');
398 my %location = $svc_phone->location_hash;
400 'did' => $svc_phone->phonenum,
401 'name' => $svc_phone->phone_name_or_cust,
402 'address' => $location{'address1'},
403 'city' => $location{'city'},
404 'state' => $location{'state'},
405 'zip' => $location{'zip'},
407 if ( $location{address2} =~ /^\s*(\w+)\W*(\d+)\s*$/ ) {
408 $e911send{'unittype'} = $1;
409 $e911send{'unitnumber'} = $2;
412 my $e911_result = $self->vitelity_command('e911send', %e911send);
414 return '' unless $result =~ /^(missingdata|invalid)/i;
416 return "Vitelity error provisioning E911 for". $svc_phone->phonenum.
420 sub _export_replace {
421 my( $self, $new, $old ) = (shift, shift, shift);
424 if( $old->forwarddst ne $new->forwarddst ) {
425 my $result = $self->vitelity_command('callfw',
426 'did' => $old->phonenum,
427 'forward' => $new->forwarddst ? $new->forwarddst : 'none',
429 if ( $result ne 'ok' ) {
430 return "Error running Vitelity callfw: $result";
434 # vfax forwarding emails
435 if( $old->email ne $new->email && $self->option('fax') ) {
436 my $result = $self->vitelity_command('changeemail',
437 'did' => $old->phonenum,
438 'emails' => $new->email ? $new->email : '',
440 if ( $result ne 'ok' ) {
441 return "Error running Vitelity changeemail: $result";
445 $self->e911_send($new);
449 my( $self, $svc_phone ) = (shift, shift);
451 return '' if $self->option('dry_run');
453 #probably okay to queue the deletion...?
454 #but hell, let's do it inline anyway, who wants phone numbers hanging around
456 return 'Deleting vfax DIDs is unsupported by Vitelity API' if $self->option('fax');
458 my $result = $self->vitelity_command('removedid',
459 'did' => $svc_phone->phonenum,
462 if ( $result ne 'success' ) {
463 return "Error running Vitelity removedid: $result";
466 return '' if $self->option('disable_e911');
471 sub _export_suspend {
472 my( $self, $svc_phone ) = (shift, shift);
477 sub _export_unsuspend {
478 my( $self, $svc_phone ) = (shift, shift);
486 my @export_svc = $self->export_svc;
487 return unless @export_svc;
489 my $in_svcpart = 'IN ('. join( ',', map $_->svcpart, @export_svc). ')';
491 foreach my $svc_phone (
492 qsearch({ 'table' => 'svc_phone',
493 'addl_from' => 'LEFT JOIN cust_svc USING (svcnum)',
494 'hashref' => {lnp_status=>'portingin'},
495 'extra_sql' => "AND svcpart $in_svcpart",
499 my $result = $self->vitelity_lnp_command('checkstatus',
500 'portid'=>$svc_phone->lnp_portid,
503 if ( $result =~ /^Complete/i ) {
505 $svc_phone->lnp_status('portedin');
506 my $error = $self->_export_insert($svc_phone);
508 #XXX log this using our internal log instead, so we can alert on it
510 warn "ERROR provisioning ported-in DID ". $svc_phone->phonenum. ": $error";
512 $error = $svc_phone->replace; #to set the lnp_status
513 #XXX log this using our internal log instead, so we can alert on it
514 warn "ERROR setting lnp_status for DID ". $svc_phone->phonenum. ": $error" if $error;
517 } elsif ( $result ne $svc_phone->lnp_reject_reason ) {
518 $svc_phone->lnp_reject_reason($result);
519 $error = $svc_phone->replace;
520 #XXX log this using our internal log instead, so we can alert on it
521 warn "ERROR setting lnp_reject_reason for DID ". $svc_phone->phonenum. ": $error" if $error;