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)' },
21 'desc' => 'Provision phone numbers to Vitelity',
22 'options' => \%options,
24 Requires installation of
25 <a href="http://search.cpan.org/dist/Net-Vitelity">Net::Vitelity</a>
28 routesip - optional Vitelity sub-account to which newly ordered DIDs will be routed
29 <br>type - optional DID type (perminute, unlimited, or your-pri)
33 sub rebless { shift; }
37 my %opt = ref($_[0]) ? %{$_[0]} : @_;
39 # currently one of three cases: areacode+exchange, areacode, state
46 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers in format NPA-NXX-XXXX
49 map { join('-', $_->npa, $_->nxx, $_->station ) }
51 'table' => 'phone_avail',
52 'hashref' => { 'exportnum' => $self->exportnum,
53 'countrycode' => '1', # vitelity is US/CA only now
54 'npa' => $opt{'areacode'},
55 'nxx' => $opt{'exchange'},
57 'order_by' => 'ORDER BY station',
61 } elsif ( $opt{'areacode'} ) { #return exchanges in format NPA-NXX- literal 'XXXX'
63 # you can't call $->name .... that returns "(unlinked)"
64 # and in any case this is still major abuse of encapsulation, it just happens to work for the other fields
66 map { $_->{'Hash'}->{name}.' ('. $_->npa. '-'. $_->nxx. '-XXXX)' }
68 # i know this doesn't do the same thing as before, but now the sort works
69 'select' => 'DISTINCT npa,nxx,name',
70 'table' => 'phone_avail',
71 'hashref' => { 'exportnum' => $self->exportnum,
72 'countrycode' => '1', # vitelity is US/CA only now
73 'npa' => $opt{'areacode'},
75 'order_by' => 'ORDER BY nxx',
79 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
81 #XXX need to flush the cache at some point :/
84 'select' => 'DISTINCT npa',
85 'table' => 'phone_avail',
86 'hashref' => { 'exportnum' => $self->exportnum,
87 'countrycode' => '1', # vitelity is US/CA only now
88 'state' => $opt{'state'},
90 'order_by' => 'ORDER BY npa',
93 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
95 #otherwise, search for em
97 my @ratecenters = $self->vitelity_command( 'listavailratecenters',
98 'state' => $opt{'state'},
100 # XXX: Options: type=unlimited OR type=pri
102 if ( $ratecenters[0] eq 'unavailable' ) {
104 } elsif ( $ratecenters[0] eq 'missingdata' ) {
105 die "missingdata error running Vitelity API"; #die?
108 local $SIG{HUP} = 'IGNORE';
109 local $SIG{INT} = 'IGNORE';
110 local $SIG{QUIT} = 'IGNORE';
111 local $SIG{TERM} = 'IGNORE';
112 local $SIG{TSTP} = 'IGNORE';
113 local $SIG{PIPE} = 'IGNORE';
115 my $oldAutoCommit = $FS::UID::AutoCommit;
116 local $FS::UID::AutoCommit = 0;
119 my $errmsg = 'WARNING: error populating phone availability cache: ';
122 foreach my $ratecenter (@ratecenters) {
124 my @dids = $self->vitelity_command( 'listlocal',
125 'state' => $opt{'state'},
126 'ratecenter' => $ratecenter,
128 # XXX: Options: type=unlimited OR type=pri
130 if ( $dids[0] eq 'unavailable' ) {
132 } elsif ( $dids[0] eq 'missingdata' ) {
133 die "missingdata error running Vitelity API"; #die?
136 foreach my $did ( @dids ) {
137 $did =~ /^(\d{3})(\d{3})(\d{4}),/ or die "unparsable did $did\n";
138 my($npa, $nxx, $station) = ($1, $2, $3);
141 my $phone_avail = new FS::phone_avail {
142 'exportnum' => $self->exportnum,
143 'countrycode' => '1', # vitelity is US/CA only now
144 'state' => $opt{'state'},
147 'station' => $station,
148 'name' => $ratecenter,
151 $error = $phone_avail->insert();
153 $dbh->rollback if $oldAutoCommit;
161 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
163 my @return = sort { $a <=> $b } keys %npa;
164 #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
169 die "get_dids called without state or areacode options";
174 sub vitelity_command {
175 my( $self, $command, @args ) = @_;
177 eval "use Net::Vitelity;";
180 my $vitelity = Net::Vitelity->new(
181 'login' => $self->option('login'),
182 'pass' => $self->option('pass'),
186 $vitelity->$command(@args);
190 my( $self, $svc_phone ) = (shift, shift);
192 return '' if $self->option('dry_run');
194 #we want to provision and catch errors now, not queue
196 %vparams = ( 'did' => $svc_phone->phonenum );
197 $vparams{'routesip'} = $self->option('routesip')
198 if defined $self->option('routesip');
199 $vparams{'type'} = $self->option('type')
200 if defined $self->option('type');
202 my $result = $self->vitelity_command('getlocaldid',%vparams);
204 if ( $result ne 'success' ) {
205 return "Error running Vitelity getlocaldid: $result";
211 sub _export_replace {
212 my( $self, $new, $old ) = (shift, shift, shift);
214 #hmm, what's to change?
219 my( $self, $svc_phone ) = (shift, shift);
221 return '' if $self->option('dry_run');
223 #probably okay to queue the deletion...?
224 #but hell, let's do it inline anyway, who wants phone numbers hanging around
226 my $result = $self->vitelity_command('removedid',
227 'did' => $svc_phone->phonenum,
230 if ( $result ne 'success' ) {
231 return "Error running Vitelity getlocaldid: $result";
237 sub _export_suspend {
238 my( $self, $svc_phone ) = (shift, shift);
243 sub _export_unsuspend {
244 my( $self, $svc_phone ) = (shift, shift);