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