fix internal_diddb delete & return number to availability, RT#4603
[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{'areacode'} && $opt{'exchange'} ) { #return numbers
34
35     $hash{npa} = $opt{areacode};
36     $hash{nxx} = $opt{exchange};
37
38     return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
39                  qsearch({ 'table'    => 'phone_avail',
40                            'hashref'  => \%hash,
41                            'order_by' => 'ORDER BY station',
42                         })
43            ];
44
45   } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
46
47     $hash{npa} = $opt{areacode};
48
49     return [ map { '('. $_->npa. '-'. $_->nxx. '-XXXX)' }
50                  qsearch({ 'select'   => 'DISTINCT npa, nxx',
51                            'table'    => 'phone_avail',
52                            'hashref'  => \%hash,
53                            'order_by' => 'ORDER BY nxx',
54                         })
55            ];
56
57   } elsif ( $opt{'state'} ) { #return aracodes
58
59     $hash{state} = $opt{state};
60
61     return [ map { $_->npa }
62                  qsearch({ 'select'   => 'DISTINCT npa',
63                            'table'    => 'phone_avail',
64                            'hashref'  => \%hash,
65                            'order_by' => 'ORDER BY npa',
66                         })
67            ];
68
69   } else { 
70     die "FS::part_export::internal_diddb::get_dids called without options\n";
71   }
72
73 }
74
75 sub _export_insert   { #link phone_avail to svcnum
76   my( $self, $svc_phone ) = (shift, shift);
77
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);
81
82   my $phone_avail = qsearchs('phone_avail', {
83     'countrycode' => ( $self->option('countrycode') || '1' ),
84     'exportnum'   => $self->exportnum,
85     'svcnum'      => '',
86     'npa'         => $npa,
87     'nxx'         => $nxx,
88     'station'     => $station,
89   });
90
91   return "number not available: ". $svc_phone->phonenum
92     unless $phone_avail;
93
94   $phone_avail->svcnum($svc_phone->svcnum);
95
96   $phone_avail->replace;
97
98 }
99
100 sub _export_delete   { #unlink phone_avail from 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'      => $svc_phone->svcnum,
111     #these too?
112     'npa'         => $npa,
113     'nxx'         => $nxx,
114     'station'     => $station,
115   });
116
117   unless ( $phone_avail ) {
118     warn "WARNING: can't find number to return to availability: ".
119          $svc_phone->phonenum;
120     return;
121   }
122
123   $phone_avail->svcnum('');
124
125   $phone_avail->replace;
126
127 }
128
129 sub _export_replace  { ''; }
130 sub _export_suspend  { ''; }
131 sub _export_unsuspend  { ''; }
132
133 1;
134