1 package FS::part_export::internal_diddb;
3 use vars qw(@ISA %info);
5 use FS::Record qw(qsearch qsearchs);
9 @ISA = qw(FS::part_export);
11 tie my %options, 'Tie::IxHash',
12 'countrycode' => { label => 'Country code', 'default' => '1', },
17 'desc' => 'Provision phone numbers from the internal DID database',
18 'notes' => 'After adding the export, DIDs may be imported under Tools -> Importing -> Import phone numbers (DIDs)',
19 'options' => \%options,
23 sub rebless { shift; }
27 my %opt = ref($_[0]) ? %{$_[0]} : @_;
29 my %hash = ( 'countrycode' => ( $self->option('countrycode') || '1' ),
30 'exportnum' => $self->exportnum,
34 if ( $opt{'ratecenter'} && $opt{'state'} ) {
35 my $rc = $opt{ratecenter};
36 $rc =~ s/, [A-Z][A-Z]$//g;
38 $hash{state} = $opt{state};
40 return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
41 qsearch({ 'table' => 'phone_avail',
43 'order_by' => 'ORDER BY station',
47 elsif ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
49 $hash{npa} = $opt{areacode};
50 $hash{nxx} = $opt{exchange};
52 return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
53 qsearch({ 'table' => 'phone_avail',
55 'order_by' => 'ORDER BY station',
59 } elsif ( $opt{'areacode'} ) {
61 $hash{npa} = $opt{areacode};
63 my @rc = qsearch({ 'select' => 'DISTINCT name, state',
64 'table' => 'phone_avail',
69 my $first_phone_avail = $rc[0];
70 return [ map { $_->get('name').", ".$_->state } @rc ]
71 if $first_phone_avail->get('name');
74 return [ map { '('. $_->npa. '-'. $_->nxx. '-XXXX)' }
75 qsearch({ 'select' => 'DISTINCT npa, nxx',
76 'table' => 'phone_avail',
78 'order_by' => 'ORDER BY nxx',
82 } elsif ( $opt{'state'} ) { #return aracodes
84 $hash{state} = $opt{state};
86 return [ map { $_->npa }
87 qsearch({ 'select' => 'DISTINCT npa',
88 'table' => 'phone_avail',
90 'order_by' => 'ORDER BY npa',
95 die "FS::part_export::internal_diddb::get_dids called without options\n";
100 sub _export_insert { #link phone_avail to svcnum
101 my( $self, $svc_phone ) = (shift, shift);
103 $svc_phone->phonenum =~ /^(\d{3})(\d{3})(\d+)$/
104 or return "unparsable phone number: ". $svc_phone->phonenum;
105 my( $npa, $nxx, $station ) = ($1, $2, $3);
107 my $phone_avail = qsearchs('phone_avail', {
108 'countrycode' => ( $self->option('countrycode') || '1' ),
109 'exportnum' => $self->exportnum,
113 'station' => $station,
116 return "number not available: ". $svc_phone->phonenum
119 $phone_avail->svcnum($svc_phone->svcnum);
121 $phone_avail->replace;
125 sub _export_delete { #unlink phone_avail from svcnum
126 my( $self, $svc_phone ) = (shift, shift);
128 $svc_phone->phonenum =~ /^(\d{3})(\d{3})(\d+)$/
129 or return "unparsable phone number: ". $svc_phone->phonenum;
130 my( $npa, $nxx, $station ) = ($1, $2, $3);
132 my $phone_avail = qsearchs('phone_avail', {
133 'countrycode' => ( $self->option('countrycode') || '1'),
134 'exportnum' => $self->exportnum,
135 'svcnum' => $svc_phone->svcnum,
139 'station' => $station,
142 unless ( $phone_avail ) {
143 warn "WARNING: can't find number to return to availability: ".
144 $svc_phone->phonenum;
148 $phone_avail->svcnum('');
150 $phone_avail->replace;
154 sub _export_replace { ''; }
155 sub _export_suspend { ''; }
156 sub _export_unsuspend { ''; }