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 can_get_dids { 1; }
45 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 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 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 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 eval "use Net::Vitelity;";
251 my $vitelity = Net::Vitelity->new(
252 'login' => $self->option('login'),
253 'pass' => $self->option('pass'),
254 'apitype' => $self->option('fax') ? 'fax' : 'api',
258 $vitelity->$command(@args);
262 my( $self, $svc_phone ) = (shift, shift);
264 return '' if $self->option('dry_run');
266 #we want to provision and catch errors now, not queue
269 # 1. provision the DID
272 my %vparams = ( 'did' => $svc_phone->phonenum );
273 $vparams{'routesip'} = $self->option('routesip')
274 if defined $self->option('routesip');
275 $vparams{'type'} = $self->option('type')
276 if defined $self->option('type');
278 my $command = 'getlocaldid';
279 my $success = 'success';
281 # this is OK as Vitelity for now is US/CA only; it's not a hack
282 $command = 'gettollfree' if $vparams{'did'} =~ /^800|^888|^877|^866|^855/;
284 if ($self->option('fax')) {
289 my $result = $self->vitelity_command($command,%vparams);
291 if ( $result ne $success ) {
292 return "Error running Vitelity $command: $result";
299 my $cnam_result = $self->vitelity_command('cnamenable',
300 'did'=>$svc_phone->phonenum,
302 if ( $result ne 'ok' ) {
303 #we already provisioned the DID, so...
304 warn "Vitelity error enabling CNAM for ". $svc_phone->phonenum. ": $result";
311 my $e911_error = $self->e911_send($svc_phone);
313 if ( $e911_error =~ /^(missingdata|invalid)/i ) {
314 #but we already provisioned the DID, so:
315 $self->vitelity_command('removedid', 'did'=> $svc_phone->phonenum,);
316 #and check the results? if it failed, then what?
325 my($self, $svc_phone) = (shift, shift);
327 return '' if $self->option('disable_e911');
329 my %location = $svc_phone->location_hash;
331 'did' => $svc_phone->phonenum,
332 'address' => $location{'address1'},
333 'city' => $location{'city'},
334 'state' => $location{'state'},
335 'zip' => $location{'zip'},
337 if ( $svc_phone->phone_name ) {
338 $e911send{'name'} = $svc_phone->phone_name;
340 my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
341 $e911send{'name'} = $cust_main->company || $cust_main->first. ' '.
342 $cust_main->get('last');
344 if ( $location{address2} =~ /^\s*(\w+)\W*(\d+)\s*$/ ) {
345 $e911send{'unittype'} = $1;
346 $e911send{'unitnumber'} = $2;
349 my $e911_result = $self->vitelity_command('e911send', %e911send);
351 return '' unless $result =~ /^(missingdata|invalid)/i;
353 return "Vitelity error provisioning E911 for". $svc_phone->phonenum.
357 sub _export_replace {
358 my( $self, $new, $old ) = (shift, shift, shift);
361 if( $old->forwarddst ne $new->forwarddst ) {
362 my $result = $self->vitelity_command('callfw',
363 'did' => $old->phonenum,
364 'forward' => $new->forwarddst ? $new->forwarddst : 'none',
366 if ( $result ne 'ok' ) {
367 return "Error running Vitelity callfw: $result";
371 # vfax forwarding emails
372 if( $old->email ne $new->email && $self->option('fax') ) {
373 my $result = $self->vitelity_command('changeemail',
374 'did' => $old->phonenum,
375 'emails' => $new->email ? $new->email : '',
377 if ( $result ne 'ok' ) {
378 return "Error running Vitelity changeemail: $result";
382 $self->e911_send($new);
386 my( $self, $svc_phone ) = (shift, shift);
388 return '' if $self->option('dry_run');
390 #probably okay to queue the deletion...?
391 #but hell, let's do it inline anyway, who wants phone numbers hanging around
393 return 'Deleting vfax DIDs is unsupported by Vitelity API' if $self->option('fax');
395 my $result = $self->vitelity_command('removedid',
396 'did' => $svc_phone->phonenum,
399 if ( $result ne 'success' ) {
400 return "Error running Vitelity removedid: $result";
403 return '' if $self->option('disable_e911');
408 sub _export_suspend {
409 my( $self, $svc_phone ) = (shift, shift);
414 sub _export_unsuspend {
415 my( $self, $svc_phone ) = (shift, shift);