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,
30 Requires installation of
31 <a href="http://search.cpan.org/dist/Net-Vitelity">Net::Vitelity</a>
34 routesip - optional Vitelity sub-account to which newly ordered DIDs will be routed
35 <br>type - optional DID type (perminute, unlimited, or your-pri)
39 sub rebless { shift; }
41 sub get_dids_can_tollfree { 1; };
45 my %opt = ref($_[0]) ? %{$_[0]} : @_;
47 if ( $opt{'tollfree'} ) {
48 my $command = 'listtollfree';
49 $command = 'listdids' if $self->option('fax');
50 my @tollfree = $self->vitelity_command($command);
53 return [] if ( $tollfree[0] eq 'noneavailable' || $tollfree[0] eq 'none');
55 foreach my $did ( @tollfree ) {
56 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
60 my @sorted_ret = sort @ret;
63 } elsif ( $opt{'ratecenter'} && $opt{'state'} ) {
65 my %flushopts = ( 'state' => $opt{'state'},
66 'ratecenter' => $opt{'ratecenter'},
67 'exportnum' => $self->exportnum
69 FS::phone_avail::flush( \%flushopts );
71 local $SIG{HUP} = 'IGNORE';
72 local $SIG{INT} = 'IGNORE';
73 local $SIG{QUIT} = 'IGNORE';
74 local $SIG{TERM} = 'IGNORE';
75 local $SIG{TSTP} = 'IGNORE';
76 local $SIG{PIPE} = 'IGNORE';
78 my $oldAutoCommit = $FS::UID::AutoCommit;
79 local $FS::UID::AutoCommit = 0;
82 my $errmsg = 'WARNING: error populating phone availability cache: ';
84 my $command = 'listlocal';
85 $command = 'listdids' if $self->option('fax');
86 my @dids = $self->vitelity_command( $command,
87 'state' => $opt{'state'},
88 'ratecenter' => $opt{'ratecenter'},
90 # XXX: Options: type=unlimited OR type=pri
92 next if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' );
93 die "missingdata error running Vitelity API" if $dids[0] eq 'missingdata';
95 foreach my $did ( @dids ) {
96 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
97 my($npa, $nxx, $station) = ($1, $2, $3);
99 my $phone_avail = new FS::phone_avail {
100 'exportnum' => $self->exportnum,
101 'countrycode' => '1', # vitelity is US/CA only now
102 'state' => $opt{'state'},
105 'station' => $station,
106 'name' => $opt{'ratecenter'},
109 my $error = $phone_avail->insert();
111 $dbh->rollback if $oldAutoCommit;
116 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
119 map { join('-', $_->npa, $_->nxx, $_->station ) }
121 'table' => 'phone_avail',
122 'hashref' => { 'exportnum' => $self->exportnum,
123 'countrycode' => '1', # vitelity is US/CA only now
124 'name' => $opt{'ratecenter'},
125 'state' => $opt{'state'},
127 'order_by' => 'ORDER BY npa, nxx, station',
131 } elsif ( $opt{'areacode'} ) {
133 my @rc = map { $_->{'Hash'}->{name}.", ".$_->state }
135 'select' => 'DISTINCT name, state',
136 'table' => 'phone_avail',
137 'hashref' => { 'exportnum' => $self->exportnum,
138 'countrycode' => '1', # vitelity is US/CA only now
139 'npa' => $opt{'areacode'},
143 my @sorted_rc = sort @rc;
144 return [ @sorted_rc ];
146 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
148 my @avail = qsearch({
149 'select' => 'DISTINCT npa',
150 'table' => 'phone_avail',
151 'hashref' => { 'exportnum' => $self->exportnum,
152 'countrycode' => '1', # vitelity is US/CA only now
153 'state' => $opt{'state'},
155 'order_by' => 'ORDER BY npa',
158 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
160 #otherwise, search for em
162 my $command = 'listavailratecenters';
163 $command = 'listratecenters' if $self->option('fax');
164 my @ratecenters = $self->vitelity_command( $command,
165 'state' => $opt{'state'},
167 # XXX: Options: type=unlimited OR type=pri
169 if ( $ratecenters[0] eq 'unavailable' || $ratecenters[0] eq 'none' ) {
171 } elsif ( $ratecenters[0] eq 'missingdata' ) {
172 die "missingdata error running Vitelity API"; #die?
175 local $SIG{HUP} = 'IGNORE';
176 local $SIG{INT} = 'IGNORE';
177 local $SIG{QUIT} = 'IGNORE';
178 local $SIG{TERM} = 'IGNORE';
179 local $SIG{TSTP} = 'IGNORE';
180 local $SIG{PIPE} = 'IGNORE';
182 my $oldAutoCommit = $FS::UID::AutoCommit;
183 local $FS::UID::AutoCommit = 0;
186 my $errmsg = 'WARNING: error populating phone availability cache: ';
189 foreach my $ratecenter (@ratecenters) {
191 my $command = 'listlocal';
192 $command = 'listdids' if $self->option('fax');
193 my @dids = $self->vitelity_command( $command,
194 'state' => $opt{'state'},
195 'ratecenter' => $ratecenter,
197 # XXX: Options: type=unlimited OR type=pri
199 if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' ) {
201 } elsif ( $dids[0] eq 'missingdata' ) {
202 die "missingdata error running Vitelity API"; #die?
205 foreach my $did ( @dids ) {
206 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
207 my($npa, $nxx, $station) = ($1, $2, $3);
210 my $phone_avail = new FS::phone_avail {
211 'exportnum' => $self->exportnum,
212 'countrycode' => '1', # vitelity is US/CA only now
213 'state' => $opt{'state'},
216 'station' => $station,
217 'name' => $ratecenter,
220 my $error = $phone_avail->insert();
222 $dbh->rollback if $oldAutoCommit;
230 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
232 my @return = sort { $a <=> $b } keys %npa;
236 die "get_dids called without state or areacode options";
241 sub vitelity_command {
242 my( $self, $command, @args ) = @_;
244 eval "use Net::Vitelity;";
247 my $vitelity = Net::Vitelity->new(
248 'login' => $self->option('login'),
249 'pass' => $self->option('pass'),
250 'apitype' => $self->option('fax') ? 'fax' : 'api',
254 $vitelity->$command(@args);
258 my( $self, $svc_phone ) = (shift, shift);
260 return '' if $self->option('dry_run');
262 #we want to provision and catch errors now, not queue
264 my %vparams = ( 'did' => $svc_phone->phonenum );
265 $vparams{'routesip'} = $self->option('routesip')
266 if defined $self->option('routesip');
267 $vparams{'type'} = $self->option('type')
268 if defined $self->option('type');
270 my $command = 'getlocaldid';
271 my $success = 'success';
273 # this is OK as Vitelity for now is US/CA only; it's not a hack
274 $command = 'gettollfree' if $vparams{'did'} =~ /^800|^888|^877|^866|^855/;
276 if($self->option('fax')) {
281 my $result = $self->vitelity_command($command,%vparams);
283 if ( $result ne $success ) {
284 return "Error running Vitelity $command: $result";
290 sub _export_replace {
291 my( $self, $new, $old ) = (shift, shift, shift);
294 if( $old->forwarddst ne $new->forwarddst ) {
295 my $result = $self->vitelity_command('callfw',
296 'did' => $old->phonenum,
297 'forward' => $new->forwarddst ? $new->forwarddst : 'none',
299 if ( $result ne 'ok' ) {
300 return "Error running Vitelity callfw: $result";
304 # vfax forwarding emails
305 if( $old->email ne $new->email && $self->option('fax') ) {
306 my $result = $self->vitelity_command('changeemail',
307 'did' => $old->phonenum,
308 'emails' => $new->email ? $new->email : '',
310 if ( $result ne 'ok' ) {
311 return "Error running Vitelity changeemail: $result";
319 my( $self, $svc_phone ) = (shift, shift);
321 return '' if $self->option('dry_run');
323 #probably okay to queue the deletion...?
324 #but hell, let's do it inline anyway, who wants phone numbers hanging around
326 return 'Deleting vfax DIDs is unsupported by Vitelity API' if $self->option('fax');
328 my $result = $self->vitelity_command('removedid',
329 'did' => $svc_phone->phonenum,
332 if ( $result ne 'success' ) {
333 return "Error running Vitelity removedid: $result";
339 sub _export_suspend {
340 my( $self, $svc_phone ) = (shift, shift);
345 sub _export_unsuspend {
346 my( $self, $svc_phone ) = (shift, shift);