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 aracodes
86 $hash{state} = $opt{state};
88 return [ map { $_->npa }
89 qsearch({ 'select' => 'DISTINCT npa',
90 'table' => 'phone_avail',
92 'order_by' => 'ORDER BY npa',
97 die "FS::part_export::internal_diddb::get_dids called without options\n";
102 sub _export_insert { #link phone_avail to svcnum
103 my( $self, $svc_phone ) = (shift, shift);
105 $svc_phone->phonenum =~ /^(\d{3})(\d{3})(\d+)$/
106 or return "unparsable phone number: ". $svc_phone->phonenum;
107 my( $npa, $nxx, $station ) = ($1, $2, $3);
109 my $phone_avail = qsearchs('phone_avail', {
110 'countrycode' => ( $self->option('countrycode') || '1' ),
111 'exportnum' => $self->exportnum,
115 'station' => $station,
118 return "number not available: ". $svc_phone->phonenum
121 $phone_avail->svcnum($svc_phone->svcnum);
123 $phone_avail->replace;
127 sub _export_delete { #unlink phone_avail from svcnum
128 my( $self, $svc_phone ) = (shift, shift);
130 $svc_phone->phonenum =~ /^(\d{3})(\d{3})(\d+)$/
131 or return "unparsable phone number: ". $svc_phone->phonenum;
132 my( $npa, $nxx, $station ) = ($1, $2, $3);
134 my $phone_avail = qsearchs('phone_avail', {
135 'countrycode' => ( $self->option('countrycode') || '1'),
136 'exportnum' => $self->exportnum,
137 'svcnum' => $svc_phone->svcnum,
141 'station' => $station,
144 unless ( $phone_avail ) {
145 warn "WARNING: can't find number to return to availability: ".
146 $svc_phone->phonenum;
150 $phone_avail->svcnum('');
152 $phone_avail->replace;
156 sub _export_replace { ''; }
157 sub _export_suspend { ''; }
158 sub _export_unsuspend { ''; }