summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/internal_diddb.pm
blob: 8771ae88327203173c7ebee37506772ef6a6f787 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package FS::part_export::internal_diddb;

use vars qw(@ISA %info);
use Tie::IxHash;
use FS::Record qw(qsearch qsearchs);
use FS::part_export;
use FS::phone_avail;

@ISA = qw(FS::part_export);

tie my %options, 'Tie::IxHash',
  'countrycode' => { label => 'Country code', 'default' => '1', },
;

%info = (
  'svc'     => 'svc_phone',
  'desc'    => 'Provision phone numbers from the internal DID database',
  'notes'   => 'After adding the export, DIDs may be imported under Tools -> Importing -> Import phone numbers (DIDs)',
  'options' => \%options,
  'no_machine' => 1,
);

sub rebless { shift; }

sub can_get_dids { 1; }

sub get_dids {
  my $self = shift;
  my %opt = ref($_[0]) ? %{$_[0]} : @_;

  my %hash = ( 'countrycode' => ( $self->option('countrycode') || '1' ),
               'exportnum'   => $self->exportnum,
               'svcnum'      => '',
             );

  if ( $opt{'ratecenter'} && $opt{'state'} ) {
    my $rc = $opt{ratecenter};
    $rc =~ s/, [A-Z][A-Z]$//g;
    $hash{name} = $rc;
    $hash{state} = $opt{state};

    return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
                 qsearch({ 'table'    => 'phone_avail',
                           'hashref'  => \%hash,
                           'order_by' => 'ORDER BY station',
                        })
           ];
  }
  elsif ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers

    $hash{npa} = $opt{areacode};
    $hash{nxx} = $opt{exchange};

    return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
                 qsearch({ 'table'    => 'phone_avail',
                           'hashref'  => \%hash,
                           'order_by' => 'ORDER BY station',
                        })
           ];

  } elsif ( $opt{'areacode'} ) { 

    $hash{npa} = $opt{areacode};

    my @rc = qsearch({ 'select' => 'DISTINCT name, state',
                       'table'    => 'phone_avail',
                       'hashref'  => \%hash,
		    });

    if(scalar(@rc)) {
	my $first_phone_avail = $rc[0];
	return [ map { $_->get('name').", ".$_->state } @rc ]
	    if $first_phone_avail->get('name');
    }

    return [ map { '('. $_->npa. '-'. $_->nxx. '-XXXX)' } 
                 qsearch({ 'select'   => 'DISTINCT npa, nxx',
                           'table'    => 'phone_avail',
                           'hashref'  => \%hash,
                           'order_by' => 'ORDER BY nxx',
                        })
           ];

  } elsif ( $opt{'state'} ) { #return areacodes

    $hash{state} = $opt{state};

    return [ map { $_->npa }
                 qsearch({ 'select'   => 'DISTINCT npa',
                           'table'    => 'phone_avail',
                           'hashref'  => \%hash,
                           'order_by' => 'ORDER BY npa',
                        })
           ];

  } else { 

    #die "FS::part_export::internal_diddb::get_dids called without options\n";
    return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
                 qsearch({ 'table'    => 'phone_avail',
                           'hashref'  => \%hash,
                           'order_by' => 'ORDER BY station',
                        })
           ];

  }

}

sub _export_insert   { #link phone_avail to svcnum
  my( $self, $svc_phone ) = (shift, shift);

  $svc_phone->phonenum =~ /^(\d{3})(\d{3})(\d+)$/
    or return "unparsable phone number: ". $svc_phone->phonenum;
  my( $npa, $nxx, $station ) = ($1, $2, $3);

  my $phone_avail = qsearchs('phone_avail', {
    'countrycode' => ( $self->option('countrycode') || '1' ),
    'exportnum'   => $self->exportnum,
    'svcnum'      => '',
    'npa'         => $npa,
    'nxx'         => $nxx,
    'station'     => $station,
  });

  return "number not available: ". $svc_phone->phonenum
    unless $phone_avail;

  $phone_avail->svcnum($svc_phone->svcnum);

  $phone_avail->replace;

}

sub _export_delete   { #unlink phone_avail from svcnum
  my( $self, $svc_phone ) = (shift, shift);

  $svc_phone->phonenum =~ /^(\d{3})(\d{3})(\d+)$/
    or return "unparsable phone number: ". $svc_phone->phonenum;
  my( $npa, $nxx, $station ) = ($1, $2, $3);

  my $phone_avail = qsearchs('phone_avail', {
    'countrycode' => ( $self->option('countrycode') || '1'),
    'exportnum'   => $self->exportnum,
    'svcnum'      => $svc_phone->svcnum,
    #these too?
    'npa'         => $npa,
    'nxx'         => $nxx,
    'station'     => $station,
  });

  unless ( $phone_avail ) {
    warn "WARNING: can't find number to return to availability: ".
         $svc_phone->phonenum;
    return;
  }

  $phone_avail->svcnum('');

  $phone_avail->replace;

}

sub _export_replace  { ''; }
sub _export_suspend  { ''; }
sub _export_unsuspend  { ''; }

1;