agent-virtualize credit card surcharge percentage, RT#72961
[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 areacodes
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
98     #die "FS::part_export::internal_diddb::get_dids called without options\n";
99     return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
100                  qsearch({ 'table'    => 'phone_avail',
101                            'hashref'  => \%hash,
102                            'order_by' => 'ORDER BY station',
103                         })
104            ];
105
106   }
107
108 }
109
110 sub _export_insert   { #link phone_avail to svcnum
111   my( $self, $svc_phone ) = (shift, shift);
112
113   $svc_phone->phonenum =~ /^(\d{3})(\d{3})(\d+)$/
114     or return "unparsable phone number: ". $svc_phone->phonenum;
115   my( $npa, $nxx, $station ) = ($1, $2, $3);
116
117   my $phone_avail = qsearchs('phone_avail', {
118     'countrycode' => ( $self->option('countrycode') || '1' ),
119     'exportnum'   => $self->exportnum,
120     'svcnum'      => '',
121     'npa'         => $npa,
122     'nxx'         => $nxx,
123     'station'     => $station,
124   });
125
126   return "number not available: ". $svc_phone->phonenum
127     unless $phone_avail;
128
129   $phone_avail->svcnum($svc_phone->svcnum);
130
131   $phone_avail->replace;
132
133 }
134
135 sub _export_delete   { #unlink phone_avail from svcnum
136   my( $self, $svc_phone ) = (shift, shift);
137
138   $svc_phone->phonenum =~ /^(\d{3})(\d{3})(\d+)$/
139     or return "unparsable phone number: ". $svc_phone->phonenum;
140   my( $npa, $nxx, $station ) = ($1, $2, $3);
141
142   my $phone_avail = qsearchs('phone_avail', {
143     'countrycode' => ( $self->option('countrycode') || '1'),
144     'exportnum'   => $self->exportnum,
145     'svcnum'      => $svc_phone->svcnum,
146     #these too?
147     'npa'         => $npa,
148     'nxx'         => $nxx,
149     'station'     => $station,
150   });
151
152   unless ( $phone_avail ) {
153     warn "WARNING: can't find number to return to availability: ".
154          $svc_phone->phonenum;
155     return;
156   }
157
158   $phone_avail->svcnum('');
159
160   $phone_avail->replace;
161
162 }
163
164 sub _export_replace  { ''; }
165 sub _export_suspend  { ''; }
166 sub _export_unsuspend  { ''; }
167
168 1;
169