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,
22 sub rebless { shift; }
26 my %opt = ref($_[0]) ? %{$_[0]} : @_;
28 my %hash = ( 'countrycode' => ( $self->option('countrycode') || '1' ),
29 'exportnum' => $self->exportnum,
33 if ( $opt{'ratecenter'} && $opt{'state'} ) {
34 my $rc = $opt{ratecenter};
35 $rc =~ s/, [A-Z][A-Z]$//g;
37 $hash{state} = $opt{state};
39 return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
40 qsearch({ 'table' => 'phone_avail',
42 'order_by' => 'ORDER BY station',
46 elsif ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
48 $hash{npa} = $opt{areacode};
49 $hash{nxx} = $opt{exchange};
51 return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
52 qsearch({ 'table' => 'phone_avail',
54 'order_by' => 'ORDER BY station',
58 } elsif ( $opt{'areacode'} ) {
60 $hash{npa} = $opt{areacode};
62 my @rc = qsearch({ 'select' => 'DISTINCT name, state',
63 'table' => 'phone_avail',
68 my $first_phone_avail = $rc[0];
69 return [ map { $_->get('name').", ".$_->state } @rc ]
70 if $first_phone_avail->get('name');
73 return [ map { '('. $_->npa. '-'. $_->nxx. '-XXXX)' }
74 qsearch({ 'select' => 'DISTINCT npa, nxx',
75 'table' => 'phone_avail',
77 'order_by' => 'ORDER BY nxx',
81 } elsif ( $opt{'state'} ) { #return aracodes
83 $hash{state} = $opt{state};
85 return [ map { $_->npa }
86 qsearch({ 'select' => 'DISTINCT npa',
87 'table' => 'phone_avail',
89 'order_by' => 'ORDER BY npa',
94 die "FS::part_export::internal_diddb::get_dids called without options\n";
99 sub _export_insert { #link phone_avail to svcnum
100 my( $self, $svc_phone ) = (shift, shift);
102 $svc_phone->phonenum =~ /^(\d{3})(\d{3})(\d+)$/
103 or return "unparsable phone number: ". $svc_phone->phonenum;
104 my( $npa, $nxx, $station ) = ($1, $2, $3);
106 my $phone_avail = qsearchs('phone_avail', {
107 'countrycode' => ( $self->option('countrycode') || '1' ),
108 'exportnum' => $self->exportnum,
112 'station' => $station,
115 return "number not available: ". $svc_phone->phonenum
118 $phone_avail->svcnum($svc_phone->svcnum);
120 $phone_avail->replace;
124 sub _export_delete { #unlink phone_avail from svcnum
125 my( $self, $svc_phone ) = (shift, shift);
127 $svc_phone->phonenum =~ /^(\d{3})(\d{3})(\d+)$/
128 or return "unparsable phone number: ". $svc_phone->phonenum;
129 my( $npa, $nxx, $station ) = ($1, $2, $3);
131 my $phone_avail = qsearchs('phone_avail', {
132 'countrycode' => ( $self->option('countrycode') || '1'),
133 'exportnum' => $self->exportnum,
134 'svcnum' => $svc_phone->svcnum,
138 'station' => $station,
141 unless ( $phone_avail ) {
142 warn "WARNING: can't find number to return to availability: ".
143 $svc_phone->phonenum;
147 $phone_avail->svcnum('');
149 $phone_avail->replace;
153 sub _export_replace { ''; }
154 sub _export_suspend { ''; }
155 sub _export_unsuspend { ''; }