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{'areacode'} && $opt{'exchange'} ) { #return numbers
35 $hash{npa} = $opt{areacode};
36 $hash{nxx} = $opt{exchange};
38 return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
39 qsearch({ 'table' => 'phone_avail',
41 'order_by' => 'ORDER BY station',
45 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
47 $hash{npa} = $opt{areacode};
49 return [ map { '('. $_->npa. '-'. $_->nxx. '-XXXX)' }
50 qsearch({ 'select' => 'DISTINCT npa, nxx',
51 'table' => 'phone_avail',
53 'order_by' => 'ORDER BY nxx',
57 } elsif ( $opt{'state'} ) { #return aracodes
59 $hash{state} = $opt{state};
61 return [ map { $_->npa }
62 qsearch({ 'select' => 'DISTINCT npa',
63 'table' => 'phone_avail',
65 'order_by' => 'ORDER BY npa',
70 die "FS::part_export::internal_diddb::get_dids called without options\n";
75 sub _export_insert { #link phone_avail to svcnum
76 my( $self, $svc_phone ) = (shift, shift);
78 $svc_phone->phonenum =~ /^(\d{3})(\d{3})(\d+)$/
79 or return "unparsable phone number: ". $svc_phone->phonenum;
80 my( $npa, $nxx, $station ) = ($1, $2, $3);
82 my $phone_avail = qsearchs('phone_avail', {
83 'countrycode' => ( $self->option('countrycode') || '1' ),
84 'exportnum' => $self->exportnum,
88 'station' => $station,
91 return "number not available: ". $svc_phone->phonenum
94 $phone_avail->svcnum($svc_phone->svcnum);
96 $phone_avail->replace;
100 sub _export_delete { #unlink phone_avail from 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,
110 'svcnum' => $svc_phone->svcnum,
114 'station' => $station,
117 unless ( $phone_avail ) {
118 warn "WARNING: can't find number to return to availability: ".
119 $svc_phone->phonenum;
123 $phone_avail->svcnum('');
125 $phone_avail->replace;
129 sub _export_replace { ''; }
130 sub _export_suspend { ''; }
131 sub _export_unsuspend { ''; }