1 package FS::part_export::vitelity;
3 use vars qw(@ISA %info);
5 use FS::Record qw(qsearch dbh);
9 @ISA = qw(FS::part_export);
11 tie my %options, 'Tie::IxHash',
12 'login' => { label=>'Vitelity API login' },
13 'pass' => { label=>'Vitelity API password' },
14 'dry_run' => { label=>"Test mode - don't actually provision" },
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' ],
27 'desc' => 'Provision phone numbers to Vitelity',
28 'options' => \%options,
31 Requires installation of
32 <a href="http://search.cpan.org/dist/Net-Vitelity">Net::Vitelity</a>
35 routesip - optional Vitelity sub-account to which newly ordered DIDs will be routed
36 <br>type - optional DID type (perminute, unlimited, or your-pri)
40 sub rebless { shift; }
42 sub get_dids_can_tollfree { 1; };
46 my %opt = ref($_[0]) ? %{$_[0]} : @_;
48 if ( $opt{'tollfree'} ) {
49 my $command = 'listtollfree';
50 $command = 'listdids' if $self->option('fax');
51 my @tollfree = $self->vitelity_command($command);
54 return [] if ( $tollfree[0] eq 'noneavailable' || $tollfree[0] eq 'none');
56 foreach my $did ( @tollfree ) {
57 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
61 my @sorted_ret = sort @ret;
64 } elsif ( $opt{'ratecenter'} && $opt{'state'} ) {
66 my %flushopts = ( 'state' => $opt{'state'},
67 'ratecenter' => $opt{'ratecenter'},
68 'exportnum' => $self->exportnum
70 FS::phone_avail::flush( \%flushopts );
72 local $SIG{HUP} = 'IGNORE';
73 local $SIG{INT} = 'IGNORE';
74 local $SIG{QUIT} = 'IGNORE';
75 local $SIG{TERM} = 'IGNORE';
76 local $SIG{TSTP} = 'IGNORE';
77 local $SIG{PIPE} = 'IGNORE';
79 my $oldAutoCommit = $FS::UID::AutoCommit;
80 local $FS::UID::AutoCommit = 0;
83 my $errmsg = 'WARNING: error populating phone availability cache: ';
85 my $command = 'listlocal';
86 $command = 'listdids' if $self->option('fax');
87 my @dids = $self->vitelity_command( $command,
88 'state' => $opt{'state'},
89 'ratecenter' => $opt{'ratecenter'},
91 # XXX: Options: type=unlimited OR type=pri
93 next if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' );
94 die "missingdata error running Vitelity API" if $dids[0] eq 'missingdata';
96 foreach my $did ( @dids ) {
97 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
98 my($npa, $nxx, $station) = ($1, $2, $3);
100 my $phone_avail = new FS::phone_avail {
101 'exportnum' => $self->exportnum,
102 'countrycode' => '1', # vitelity is US/CA only now
103 'state' => $opt{'state'},
106 'station' => $station,
107 'name' => $opt{'ratecenter'},
110 my $error = $phone_avail->insert();
112 $dbh->rollback if $oldAutoCommit;
117 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
120 map { join('-', $_->npa, $_->nxx, $_->station ) }
122 'table' => 'phone_avail',
123 'hashref' => { 'exportnum' => $self->exportnum,
124 'countrycode' => '1', # vitelity is US/CA only now
125 'name' => $opt{'ratecenter'},
126 'state' => $opt{'state'},
128 'order_by' => 'ORDER BY npa, nxx, station',
132 } elsif ( $opt{'areacode'} ) {
134 my @rc = map { $_->{'Hash'}->{name}.", ".$_->state }
136 'select' => 'DISTINCT name, state',
137 'table' => 'phone_avail',
138 'hashref' => { 'exportnum' => $self->exportnum,
139 'countrycode' => '1', # vitelity is US/CA only now
140 'npa' => $opt{'areacode'},
144 my @sorted_rc = sort @rc;
145 return [ @sorted_rc ];
147 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
149 my @avail = qsearch({
150 'select' => 'DISTINCT npa',
151 'table' => 'phone_avail',
152 'hashref' => { 'exportnum' => $self->exportnum,
153 'countrycode' => '1', # vitelity is US/CA only now
154 'state' => $opt{'state'},
156 'order_by' => 'ORDER BY npa',
159 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
161 #otherwise, search for em
163 my $command = 'listavailratecenters';
164 $command = 'listratecenters' if $self->option('fax');
165 my @ratecenters = $self->vitelity_command( $command,
166 'state' => $opt{'state'},
168 # XXX: Options: type=unlimited OR type=pri
170 if ( $ratecenters[0] eq 'unavailable' || $ratecenters[0] eq 'none' ) {
172 } elsif ( $ratecenters[0] eq 'missingdata' ) {
173 die "missingdata error running Vitelity API"; #die?
176 local $SIG{HUP} = 'IGNORE';
177 local $SIG{INT} = 'IGNORE';
178 local $SIG{QUIT} = 'IGNORE';
179 local $SIG{TERM} = 'IGNORE';
180 local $SIG{TSTP} = 'IGNORE';
181 local $SIG{PIPE} = 'IGNORE';
183 my $oldAutoCommit = $FS::UID::AutoCommit;
184 local $FS::UID::AutoCommit = 0;
187 my $errmsg = 'WARNING: error populating phone availability cache: ';
190 foreach my $ratecenter (@ratecenters) {
192 my $command = 'listlocal';
193 $command = 'listdids' if $self->option('fax');
194 my @dids = $self->vitelity_command( $command,
195 'state' => $opt{'state'},
196 'ratecenter' => $ratecenter,
198 # XXX: Options: type=unlimited OR type=pri
200 if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' ) {
202 } elsif ( $dids[0] eq 'missingdata' ) {
203 die "missingdata error running Vitelity API"; #die?
206 foreach my $did ( @dids ) {
207 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
208 my($npa, $nxx, $station) = ($1, $2, $3);
211 my $phone_avail = new FS::phone_avail {
212 'exportnum' => $self->exportnum,
213 'countrycode' => '1', # vitelity is US/CA only now
214 'state' => $opt{'state'},
217 'station' => $station,
218 'name' => $ratecenter,
221 my $error = $phone_avail->insert();
223 $dbh->rollback if $oldAutoCommit;
231 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
233 my @return = sort { $a <=> $b } keys %npa;
237 die "get_dids called without state or areacode options";
242 sub vitelity_command {
243 my( $self, $command, @args ) = @_;
245 eval "use Net::Vitelity;";
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);
259 my( $self, $svc_phone ) = (shift, shift);
261 return '' if $self->option('dry_run');
263 #we want to provision and catch errors now, not queue
265 my %vparams = ( 'did' => $svc_phone->phonenum );
266 $vparams{'routesip'} = $self->option('routesip')
267 if defined $self->option('routesip');
268 $vparams{'type'} = $self->option('type')
269 if defined $self->option('type');
271 my $command = 'getlocaldid';
272 my $success = 'success';
274 # this is OK as Vitelity for now is US/CA only; it's not a hack
275 $command = 'gettollfree' if $vparams{'did'} =~ /^800|^888|^877|^866|^855/;
277 if($self->option('fax')) {
282 my $result = $self->vitelity_command($command,%vparams);
284 if ( $result ne $success ) {
285 return "Error running Vitelity $command: $result";
291 sub _export_replace {
292 my( $self, $new, $old ) = (shift, shift, shift);
295 if( $old->forwarddst ne $new->forwarddst ) {
296 my $result = $self->vitelity_command('callfw',
297 'did' => $old->phonenum,
298 'forward' => $new->forwarddst ? $new->forwarddst : 'none',
300 if ( $result ne 'ok' ) {
301 return "Error running Vitelity callfw: $result";
305 # vfax forwarding emails
306 if( $old->email ne $new->email && $self->option('fax') ) {
307 my $result = $self->vitelity_command('changeemail',
308 'did' => $old->phonenum,
309 'emails' => $new->email ? $new->email : '',
311 if ( $result ne 'ok' ) {
312 return "Error running Vitelity changeemail: $result";
320 my( $self, $svc_phone ) = (shift, shift);
322 return '' if $self->option('dry_run');
324 #probably okay to queue the deletion...?
325 #but hell, let's do it inline anyway, who wants phone numbers hanging around
327 return 'Deleting vfax DIDs is unsupported by Vitelity API' if $self->option('fax');
329 my $result = $self->vitelity_command('removedid',
330 'did' => $svc_phone->phonenum,
333 if ( $result ne 'success' ) {
334 return "Error running Vitelity removedid: $result";
340 sub _export_suspend {
341 my( $self, $svc_phone ) = (shift, shift);
346 sub _export_unsuspend {
347 my( $self, $svc_phone ) = (shift, shift);