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; }
25 sub can_get_dids { 1; }
29 my %opt = ref($_[0]) ? %{$_[0]} : @_;
31 my %hash = ( 'countrycode' => ( $self->option('countrycode') || '1' ),
32 'exportnum' => $self->exportnum,
36 if ( $opt{'ratecenter'} && $opt{'state'} ) {
37 my $rc = $opt{ratecenter};
38 $rc =~ s/, [A-Z][A-Z]$//g;
40 $hash{state} = $opt{state};
42 return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
43 qsearch({ 'table' => 'phone_avail',
45 'order_by' => 'ORDER BY station',
49 elsif ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
51 $hash{npa} = $opt{areacode};
52 $hash{nxx} = $opt{exchange};
54 return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
55 qsearch({ 'table' => 'phone_avail',
57 'order_by' => 'ORDER BY station',
61 } elsif ( $opt{'areacode'} ) {
63 $hash{npa} = $opt{areacode};
65 my @rc = qsearch({ 'select' => 'DISTINCT name, state',
66 'table' => 'phone_avail',
71 my $first_phone_avail = $rc[0];
72 return [ map { $_->get('name').", ".$_->state } @rc ]
73 if $first_phone_avail->get('name');
76 return [ map { '('. $_->npa. '-'. $_->nxx. '-XXXX)' }
77 qsearch({ 'select' => 'DISTINCT npa, nxx',
78 'table' => 'phone_avail',
80 'order_by' => 'ORDER BY nxx',
84 } elsif ( $opt{'state'} ) { #return areacodes
86 $hash{state} = $opt{state};
88 return [ map { $_->npa }
89 qsearch({ 'select' => 'DISTINCT npa',
90 'table' => 'phone_avail',
92 'order_by' => 'ORDER BY npa',
98 #die "FS::part_export::internal_diddb::get_dids called without options\n";
99 return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
100 qsearch({ 'table' => 'phone_avail',
102 'order_by' => 'ORDER BY station',
110 sub _export_insert { #link phone_avail to svcnum
111 my( $self, $svc_phone ) = (shift, shift);
113 $svc_phone->phonenum =~ /^(\d{3})(\d{3})(\d+)$/
114 or return "unparsable phone number: ". $svc_phone->phonenum;
115 my( $npa, $nxx, $station ) = ($1, $2, $3);
117 my $phone_avail = qsearchs('phone_avail', {
118 'countrycode' => ( $self->option('countrycode') || '1' ),
119 'exportnum' => $self->exportnum,
123 'station' => $station,
126 return "number not available: ". $svc_phone->phonenum
129 $phone_avail->svcnum($svc_phone->svcnum);
131 $phone_avail->replace;
135 sub _export_delete { #unlink phone_avail from svcnum
136 my( $self, $svc_phone ) = (shift, shift);
138 $svc_phone->phonenum =~ /^(\d{3})(\d{3})(\d+)$/
139 or return "unparsable phone number: ". $svc_phone->phonenum;
140 my( $npa, $nxx, $station ) = ($1, $2, $3);
142 my $phone_avail = qsearchs('phone_avail', {
143 'countrycode' => ( $self->option('countrycode') || '1'),
144 'exportnum' => $self->exportnum,
145 'svcnum' => $svc_phone->svcnum,
149 'station' => $station,
152 unless ( $phone_avail ) {
153 warn "WARNING: can't find number to return to availability: ".
154 $svc_phone->phonenum;
158 $phone_avail->svcnum('');
160 $phone_avail->replace;
164 sub _export_replace { ''; }
165 sub _export_suspend { ''; }
166 sub _export_unsuspend { ''; }