agent-virtualize credit card surcharge percentage, RT#72961
[freeside.git] / FS / FS / part_export / fibernetics_did.pm
1 package FS::part_export::fibernetics_did;
2 use base qw( FS::part_export );
3
4 use strict;
5 use vars qw( %info $DEBUG );
6 use Data::Dumper;
7 use URI::Escape;
8 #use Locale::SubCountry;
9 #use FS::Record qw(qsearch dbh);
10 use XML::Simple;
11 #use Net::HTTPS::Any qw( 0.10 https_get );
12 use LWP::UserAgent;
13 use HTTP::Request::Common;
14
15 $DEBUG = 0;
16
17 tie my %options, 'Tie::IxHash',
18   'country' => { 'label' => 'Country', 'default' => 'CA', size=>2, },
19 ;
20
21 %info = (
22   'svc'        => 'svc_phone',
23   'desc'       => 'Provision phone numbers to Fibernetics web services API',
24   'options'    => \%options,
25   'notes'      => '',
26 );
27
28 sub rebless { shift; }
29
30 sub can_get_dids { 1; }
31 sub get_dids_can_tollfree { 0; };
32 sub get_dids_can_manual   { 1; };
33 sub get_dids_can_edit     { 1; };
34 sub get_dids_npa_select   { 0; };
35
36 # i guess we could get em from the API, but since its returning states without
37 #  availability, there's no advantage
38     # not really needed, we maintain our own list of provinces, but would
39     #  help to hide the ones without availability (need to fix the selector too)
40 our @states = (
41   'Alberta',
42   'British Columbia',
43   'Ontario',
44   'Quebec',
45   #'Saskatchewan',
46   #'The Territories',
47   #'PEI/Nova Scotia',
48   #'Manitoba',
49   #'Newfoundland',
50   #'New Brunswick',
51 );
52
53 sub get_dids {
54   my $self = shift;
55   my %opt = ref($_[0]) ? %{$_[0]} : @_;
56
57   if ( $opt{'tollfree'} ) {
58     warn 'Fibernetics DID provisioning does not yet support toll-free numbers';
59     return [];
60   }
61
62   my %query_hash = ();
63
64   #ratecenter + state: return numbers (more structured names, npa selection)
65   #areacode + exchange: return numbers
66   #areacode: return city/ratecenter/whatever
67   #state: return areacodes
68
69   #region + state: return numbers (arbitrary names, no npa selection)
70   #state: return regions
71
72 #  if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
73 #
74 #    $query_hash{'region'} = $opt{'exchange'};
75 #
76 #  } elsif ( $opt{'areacode'} ) {
77 #
78 #    $query_hash{'npa'} = $opt{'areacode'};
79
80   #if ( $opt{'state'} && $opt{'region'} ) { #return numbers
81   if ( $opt{'region'} ) { #return numbers
82
83     #$query_hash{'province'} = $country->full_name($opt{'state'});
84     $query_hash{'region'}   = $opt{'region'}
85
86   } elsif ( $opt{'state'} ) { #return regions
87
88     #my $country = new Locale::SubCountry( $self->option('country') );
89     #$query_hash{'province'}   = $country->full_name($opt{'state'});
90     $query_hash{'province'}   = $opt{'state'};
91     $query_hash{'listregion'} = 1;
92
93   } else { #nothing passed, return states (provinces)
94
95     return \@states;
96
97   }
98
99
100   my $url = 'http://'. $self->machine. '/porta/cgi-bin/porta_query.cgi';
101   if ( keys %query_hash ) {
102     $url .= '?'. join('&', map "$_=". uri_escape($query_hash{$_}),
103                              keys %query_hash
104                      );
105   }
106   warn $url if $DEBUG;
107
108   #my( $page, $response, %reply_headers) = https_get(
109   #  'host' => $self->machine,
110   #);
111
112   my $ua = LWP::UserAgent->new;
113   #my $response = $ua->$method(
114   #  $url, \%data,
115   #  'Content-Type'=>'application/x-www-form-urlencoded'
116   #);
117   my $req = HTTP::Request::Common::GET( $url );
118   my $response = $ua->request($req);
119
120   die $response->error_as_HTML if $response->is_error;
121
122   my $page = $response->content;
123
124   my $data = XMLin( $page );
125
126   warn Dumper($data) if $DEBUG;
127
128 #  if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
129 #
130 #    [ map $_->{'number'}, @{ $data->{'item'} } ];
131 #
132 #  } elsif ( $opt{'areacode'} ) {
133 #
134 #    [ map $_->{'region'}, @{ $data->{'item'} } ];
135 #
136 #  } elsif ( $opt{'state'} ) { #return areacodes
137 #
138 #    [ map $_->{'npa'}, @{ $data->{'item'} } ];
139
140   #if ( $opt{'state'} && $opt{'region'} ) { #return numbers
141   if ( $opt{'region'} ) { #return numbers
142
143     [ map { $_ =~ /^(\d?)(\d{3})(\d{3})(\d{4})$/
144               #? ($1 ? "$1 " : ''). "$2 $3 $4"
145               ? "$2 $3 $4"
146               : $_;
147           }
148         sort { $a <=> $b }
149           map $_->{'phone'},
150             @{ $data->{'item'} }
151     ];
152
153   } elsif ( $opt{'state'} ) { #return regions
154
155     #[ map $_->{'region'}, @{ $data->{'item'} } ];
156     my %regions = map { $_ => 1 } map $_->{'region'}, @{ $data->{'item'} };
157     [ sort keys %regions ];
158
159   #} else { #nothing passed, return states (provinces)
160     # not really needed, we maintain our own list of provinces, but would
161     #  help to hide the ones without availability (need to fix the selector too)
162   }
163
164
165 }
166
167 #insert, delete, etc... handled with shellcommands
168
169 sub _export_insert {
170   #my( $self, $svc_phone ) = (shift, shift);
171 }
172 sub _export_delete {
173   #my( $self, $svc_phone ) = (shift, shift);
174 }
175
176 sub _export_replace  { ''; }
177 sub _export_suspend  { ''; }
178 sub _export_unsuspend  { ''; }
179
180 1;