This commit was generated by cvs2svn to compensate for changes in r11022,
[freeside.git] / FS / FS / part_export / internal_diddb.pm
1 package FS::part_export::internal_diddb;
2
3 use vars qw(@ISA %info);
4 use Tie::IxHash;
5 use FS::Record qw(qsearch qsearchs);
6 use FS::part_export;
7 use FS::phone_avail;
8
9 @ISA = qw(FS::part_export);
10
11 tie my %options, 'Tie::IxHash',
12   'countrycode' => { label => 'Country code', 'default' => '1', },
13 ;
14
15 %info = (
16   'svc'     => 'svc_phone',
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,
20 );
21
22 sub rebless { shift; }
23
24 sub get_dids {
25   my $self = shift;
26   my %opt = ref($_[0]) ? %{$_[0]} : @_;
27
28   my %hash = ( 'countrycode' => ( $self->option('countrycode') || '1' ),
29                'exportnum'   => $self->exportnum,
30                'svcnum'      => '',
31              );
32
33   if ( $opt{'ratecenter'} && $opt{'state'} ) {
34     my $rc = $opt{ratecenter};
35     $rc =~ s/, [A-Z][A-Z]$//g;
36     $hash{name} = $rc;
37     $hash{state} = $opt{state};
38
39     return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
40                  qsearch({ 'table'    => 'phone_avail',
41                            'hashref'  => \%hash,
42                            'order_by' => 'ORDER BY station',
43                         })
44            ];
45   }
46   elsif ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
47
48     $hash{npa} = $opt{areacode};
49     $hash{nxx} = $opt{exchange};
50
51     return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
52                  qsearch({ 'table'    => 'phone_avail',
53                            'hashref'  => \%hash,
54                            'order_by' => 'ORDER BY station',
55                         })
56            ];
57
58   } elsif ( $opt{'areacode'} ) { 
59
60     $hash{npa} = $opt{areacode};
61
62     my @rc = qsearch({ 'select' => 'DISTINCT name, state',
63                        'table'    => 'phone_avail',
64                        'hashref'  => \%hash,
65                     });
66
67     if(scalar(@rc)) {
68         my $first_phone_avail = $rc[0];
69         return [ map { $_->get('name').", ".$_->state } @rc ]
70             if $first_phone_avail->get('name');
71     }
72
73     return [ map { '('. $_->npa. '-'. $_->nxx. '-XXXX)' } 
74                  qsearch({ 'select'   => 'DISTINCT npa, nxx',
75                            'table'    => 'phone_avail',
76                            'hashref'  => \%hash,
77                            'order_by' => 'ORDER BY nxx',
78                         })
79            ];
80
81   } elsif ( $opt{'state'} ) { #return aracodes
82
83     $hash{state} = $opt{state};
84
85     return [ map { $_->npa }
86                  qsearch({ 'select'   => 'DISTINCT npa',
87                            'table'    => 'phone_avail',
88                            'hashref'  => \%hash,
89                            'order_by' => 'ORDER BY npa',
90                         })
91            ];
92
93   } else { 
94     die "FS::part_export::internal_diddb::get_dids called without options\n";
95   }
96
97 }
98
99 sub _export_insert   { #link phone_avail to svcnum
100   my( $self, $svc_phone ) = (shift, shift);
101
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);
105
106   my $phone_avail = qsearchs('phone_avail', {
107     'countrycode' => ( $self->option('countrycode') || '1' ),
108     'exportnum'   => $self->exportnum,
109     'svcnum'      => '',
110     'npa'         => $npa,
111     'nxx'         => $nxx,
112     'station'     => $station,
113   });
114
115   return "number not available: ". $svc_phone->phonenum
116     unless $phone_avail;
117
118   $phone_avail->svcnum($svc_phone->svcnum);
119
120   $phone_avail->replace;
121
122 }
123
124 sub _export_delete   { #unlink phone_avail from svcnum
125   my( $self, $svc_phone ) = (shift, shift);
126
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);
130
131   my $phone_avail = qsearchs('phone_avail', {
132     'countrycode' => ( $self->option('countrycode') || '1'),
133     'exportnum'   => $self->exportnum,
134     'svcnum'      => $svc_phone->svcnum,
135     #these too?
136     'npa'         => $npa,
137     'nxx'         => $nxx,
138     'station'     => $station,
139   });
140
141   unless ( $phone_avail ) {
142     warn "WARNING: can't find number to return to availability: ".
143          $svc_phone->phonenum;
144     return;
145   }
146
147   $phone_avail->svcnum('');
148
149   $phone_avail->replace;
150
151 }
152
153 sub _export_replace  { ''; }
154 sub _export_suspend  { ''; }
155 sub _export_unsuspend  { ''; }
156
157 1;
158