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