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