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 'state' => $opt{'state'},
55 'npa' => $opt{'areacode'},
56 'nxx' => $opt{'exchange'},
58 'order_by' => 'ORDER BY station',
62 } elsif ( $opt{'areacode'} ) { #return exchanges in format NPA-NXX- literal 'XXXX'
65 map { $_->name. ' ('. $_->npa. '-'. $_->nxx. '-XXXX)' }
67 'select' => 'DISTINCT ON ( name, npa, nxx ) *',
68 'table' => 'phone_avail',
69 'hashref' => { 'exportnum' => $self->exportnum,
70 'countrycode' => '1', # vitelity is US/CA only now
71 'state' => $opt{'state'},
72 'npa' => $opt{'areacode'},
74 'order_by' => 'ORDER BY nxx',
78 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
80 #XXX need to flush the cache at some point :/
83 'select' => 'DISTINCT npa',
84 'table' => 'phone_avail',
85 'hashref' => { 'exportnum' => $self->exportnum,
86 'countrycode' => '1', # vitelity is US/CA only now
87 'state' => $opt{'state'},
89 'order_by' => 'ORDER BY npa',
92 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
94 #otherwise, search for em
96 my @ratecenters = $self->vitelity_command( 'listavailratecenters',
97 'state' => $opt{'state'},
99 # XXX: Options: type=unlimited OR type=pri
101 if ( $ratecenters[0] eq 'unavailable' ) {
103 } elsif ( $ratecenters[0] eq 'missingdata' ) {
104 die "missingdata error running Vitelity API"; #die?
107 local $SIG{HUP} = 'IGNORE';
108 local $SIG{INT} = 'IGNORE';
109 local $SIG{QUIT} = 'IGNORE';
110 local $SIG{TERM} = 'IGNORE';
111 local $SIG{TSTP} = 'IGNORE';
112 local $SIG{PIPE} = 'IGNORE';
114 my $oldAutoCommit = $FS::UID::AutoCommit;
115 local $FS::UID::AutoCommit = 0;
118 my $errmsg = 'WARNING: error populating phone availability cache: ';
121 foreach my $ratecenter (@ratecenters) {
123 my @dids = $self->vitelity_command( 'listlocal',
124 'state' => $opt{'state'},
125 'ratecenter' => $ratecenter,
127 # XXX: Options: type=unlimited OR type=pri
129 if ( $dids[0] eq 'unavailable' ) {
131 } elsif ( $dids[0] eq 'missingdata' ) {
132 die "missingdata error running Vitelity API"; #die?
135 foreach my $did ( @dids ) {
136 $did =~ /^(\d{3})(\d{3})(\d{4})$/ or die "unparsable did $did\n";
137 my($npa, $nxx, $station) = ($1, $2, $3);
140 my $phone_avail = new FS::phone_avail {
141 'exportnum' => $self->exportnum,
142 'countrycode' => '1', # vitelity is US/CA only now
143 'state' => $opt{'state'},
146 'station' => $station,
147 'name' => $ratecenter,
150 $error = $phone_avail->insert();
152 $dbh->rollback if $oldAutoCommit;
160 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
162 my @return = sort { $a <=> $b } keys %npa;
163 #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
168 die "get_dids called without state or areacode options";
173 sub vitelity_command {
174 my( $self, $command, @args ) = @_;
176 eval "use Net::Vitelity;";
179 my $vitelity = Net::Vitelity->new(
180 'login' => $self->option('login'),
181 'pass' => $self->option('pass'),
185 $vitelity->$command(@args);
189 my( $self, $svc_phone ) = (shift, shift);
191 return '' if $self->option('dry_run');
193 #we want to provision and catch errors now, not queue
195 %vparams = ( 'did' => $svc_phone->phonenum );
196 $vparams{'routesip'} = $self->option('routesip')
197 if defined $self->option('routesip');
198 $vparams{'type'} = $self->option('type')
199 if defined $self->option('type');
201 my $result = $self->vitelity_command('getlocaldid',%vparams);
203 if ( $result ne 'success' ) {
204 return "Error running Vitelity getlocaldid: $result";
210 sub _export_replace {
211 my( $self, $new, $old ) = (shift, shift, shift);
213 #hmm, what's to change?
218 my( $self, $svc_phone ) = (shift, shift);
220 return '' if $self->option('dry_run');
222 #probably okay to queue the deletion...?
223 #but hell, let's do it inline anyway, who wants phone numbers hanging around
225 my $result = $self->vitelity_command('removedid',
226 'did' => $svc_phone->phonenum,
229 if ( $result ne 'success' ) {
230 return "Error running Vitelity getlocaldid: $result";
236 sub _export_suspend {
237 my( $self, $svc_phone ) = (shift, shift);
242 sub _export_unsuspend {
243 my( $self, $svc_phone ) = (shift, shift);