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