fix to internal_diddb provisioning
[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 %info = (
12   'svc'   => 'svc_phone',
13   'desc'  => 'Provision phone numbers from the internal DID database',
14   'notes' => 'After adding the export, DIDs may be imported under Tools -> Importing -> Import phone numbers (DIDs)',
15 );
16
17 sub rebless { shift; }
18
19 sub get_dids {
20   my $self = shift;
21   my %opt = ref($_[0]) ? %{$_[0]} : @_;
22
23   my %hash = ( 'countrycode' => 1, #XXX make an option or something
24                'exportnum'   => $self->exportnum,
25                'svcnum'      => '',
26              );
27
28   if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
29
30     $hash{npa} = $opt{areacode};
31     $hash{nxx} = $opt{exchange};
32
33     return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
34                  qsearch({ 'table'    => 'phone_avail',
35                            'hashref'  => \%hash,
36                            'order_by' => 'ORDER BY station',
37                         })
38            ];
39
40   } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
41
42     $hash{npa} = $opt{areacode};
43
44     return [ map { '('. $_->npa. '-'. $_->nxx. '-XXXX)' }
45                  qsearch({ 'select'   => 'DISTINCT npa, nxx',
46                            'table'    => 'phone_avail',
47                            'hashref'  => \%hash,
48                            'order_by' => 'ORDER BY nxx',
49                         })
50            ];
51
52   } elsif ( $opt{'state'} ) { #return aracodes
53
54     $hash{state} = $opt{state};
55
56     return [ map { $_->npa }
57                  qsearch({ 'select'   => 'DISTINCT npa',
58                            'table'    => 'phone_avail',
59                            'hashref'  => \%hash,
60                            'order_by' => 'ORDER BY npa',
61                         })
62            ];
63
64   } else { 
65     die "FS::part_export::internal_diddb::get_dids called without options\n";
66   }
67
68 }
69
70 sub _export_insert   { #link phone_avail to svcnum
71   my( $self, $svc_phone ) = (shift, shift);
72
73   $svc_phone->phonenum =~ /^(\d{3})(\d{3})(\d+)$/
74     or return "unparsable phone number: ". $svc_phone->phonenum;
75   my( $npa, $nxx, $station ) = ($1, $2, $3);
76
77   my $phone_avail = qsearchs('phone_avail', {
78     'countrycode' => 1, #XXX make an option or something
79     'exportnum'   => $self->exportnum,
80     'svcnum'      => '',
81     'npa'         => $npa,
82     'nxx'         => $nxx,
83     'station'     => $station,
84   });
85
86   return "number not available: ". $svc_phone->phonenum
87     unless $phone_avail;
88
89   $phone_avail->svcnum($svc_phone->svcnum);
90
91   $phone_avail->replace;
92
93 }
94
95 sub _export_delete   { #unlink phone_avail from svcnum
96   my( $self, $svc_phone ) = (shift, shift);
97
98   $svc_phone =~ /^(\d{3})(\d{3})(\d+)$/
99     or return "unparsable phone number: ". $svc_phone->phonenum;
100   my( $npa, $nxx, $station ) = ($1, $2, $3);
101
102   my $phone_avail = qsearchs('phone_avail', {
103     'countrycode' => 1, #XXX make an option or something
104     'exportnum'   => $self->exportnum,
105     'svcnum'      => $svc_phone->svcnum,
106     #these too?
107     'npa'         => $npa,
108     'nxx'         => $nxx,
109     'station'     => $station,
110   });
111
112   return "can't find number to return to availability: ". $svc_phone->phonenum
113     unless $phone_avail;
114
115   $phone_avail->svcnum('');
116
117   $phone_avail->replace;
118
119 }
120
121 sub _export_replace  { ''; }
122 sub _export_suspend  { ''; }
123 sub _export_unsuspend  { ''; }
124
125 1;
126