RT# 83450 - fixed rateplan export
[freeside.git] / FS / FS / part_export / voip_innovations3.pm
1 package FS::part_export::voip_innovations3;
2
3 use vars qw(@ISA %info);
4 use Tie::IxHash;
5 use FS::Record qw(qsearch dbh);
6 use FS::part_export;
7 use FS::phone_avail;
8 use Data::Dumper;
9
10 @ISA = qw(FS::part_export);
11
12 tie my %options, 'Tie::IxHash',
13   'login'         => { label=>'VoIP Innovations API login' },
14   'password'      => { label=>'VoIP Innovations API password' },
15   'endpointgroup' => { label=>'VoIP Innovations endpoint group number' },
16   'e911'          => { label=>'Provision E911 data',
17                        type=>'checkbox',
18                      },
19   'no_provision_did' => { label=>'Disable DID provisioning',
20                           type=>'checkbox',
21                         },
22 #not particularly useful unless we can_get_dids
23 #  'dry_run'       => { label=>"Test mode - don't actually provision",
24 #                       type=>'checkbox',
25 #                     },
26   'sandbox'       => { label=>'Communicatino with the VoIP Innovations sandbox'.
27                               ' instead of the live server',
28                        type => 'checkbox',
29                      },
30 ;
31
32 %info = (
33   'svc'     => 'svc_phone',
34   'desc'    => 'Provision phone numbers / E911 to VoIP Innovations (API 3.0)',
35   'options' => \%options,
36   'no_machine' => 1,
37   'notes'   => <<'END'
38 Requires installation of
39 <a href="http://search.cpan.org/dist/Net-VoIP_Innovations">Net::VoIP_Innovations</a>
40 from CPAN.
41 END
42 );
43
44 sub rebless { shift; }
45
46 sub can_get_dids { 0; } #with API 3.0?  not yet
47
48 sub vi_command {
49   my( $self, $command, @args ) = @_;
50
51   eval "use Net::VoIP_Innovations 3.00;";
52   if ( $@ ) {
53     warn $@;
54     die $@;
55   }
56
57   my $vi = Net::VoIP_Innovations->new(
58     'login'    => $self->option('login'),
59     'password' => $self->option('password'),
60     'sandbox'  => $self->option('sandbox'),
61   );
62
63   $vi->$command(@args);
64 }
65
66
67 sub _export_insert {
68   my( $self, $svc_phone ) = (shift, shift);
69
70   return '' if $self->option('dry_run');
71
72   #we want to provision and catch errors now, not queue
73
74   unless ( $self->option('no_provision_did') ) {
75
76     return "can't yet provision to VoIP Innovations v3 API"; #XXX
77
78     ###
79     # reserveDID
80     ###
81
82     my $r = $self->vi_command('reserveDID',
83       'did'           => $svc_phone->phonenum,
84       'minutes'       => 1,
85       'endpointgroup' => $self->option('endpointgroup'),
86     );
87
88     my $rdid = $r->{did};
89
90     if ( $rdid->{'statuscode'} != 100 ) {
91       return "Error running VoIP Innovations reserveDID: ".
92              $rdid->{'statuscode'}. ': '. $rdid->{'status'};
93     }
94
95     ###
96     # assignDID
97     ###
98
99     my $a = $self->vi_command('assignDID',
100       'did'           => $svc_phone->phonenum,
101       'endpointgroup' => $self->option('endpointgroup'),
102       #'rewrite'
103       #'cnam'
104     );
105
106     my $adid = $a->{did};
107
108     if ( $adid->{'statuscode'} != 100 ) {
109       return "Error running VoIP Innovations assignDID: ".
110              $adid->{'statuscode'}. ': '. $adid->{'status'};
111     }
112
113   }
114
115   ###
116   # insert911
117   ###
118
119   if ( $self->option('e911') ) {
120
121     my %location_hash = $svc_phone->location_hash;
122     my( $zip, $plus4 ) = split('-', $location_hash->{zip});
123     my $resp = $self->vi_command('insert911',
124       'did'        => $svc_phone->phonenum,
125       'address1'   => $location_hash{address1},
126       'address2'   => $location_hash{address2},
127       'city'       => $location_hash{city},
128       'state'      => $location_hash{state},
129       'zip'        => $zip,
130       'plusFour'   => $plus4,
131       'callerName' =>
132         $svc_phone->phone_name
133           || $svc_phone->cust_svc->cust_pkg->cust_main->contact_firstlast,
134     );
135
136     if ( $resp->{'responseCode'} != 100 ) {
137       return "Error running VoIP Innovations insert911: ".
138              $resp->{'responseCode'}. ': '. $resp->{'responseMessage'};
139     }
140
141   }
142
143   '';
144 }
145
146 sub _export_replace {
147   my( $self, $new, $old ) = (shift, shift, shift);
148
149   #hmm, anything to change besides E911 data?
150
151   ###
152   # 911Update
153   ###
154
155   if ( $self->option('e911') ) {
156
157     my %location_hash = $new->location_hash;
158     my( $zip, $plus4 ) = split('-', $location_hash->{zip});
159     my $resp = $self->vi_command('update911',
160       'did'        => $svc_phone->phonenum,
161       'address1'   => $location_hash{address1},
162       'address2'   => $location_hash{address2},
163       'city'       => $location_hash{city},
164       'state'      => $location_hash{state},
165       'zip'        => $zip,
166       'plusFour'   => $plus4,
167       'callerName' =>
168         $svc_phone->phone_name
169           || $svc_phone->cust_svc->cust_pkg->cust_main->contact_firstlast,
170     );
171
172     if ( $resp->{'responseCode'} != 100 ) {
173       return "Error running VoIP Innovations update911: ".
174              $resp->{'responseCode'}. ': '. $resp->{'responseMessage'};
175     }
176
177   }
178
179   '';
180 }
181
182 sub _export_delete {
183   my( $self, $svc_phone ) = (shift, shift);
184
185   return '' if $self->option('dry_run');
186
187   ###
188   # releaseDID
189   ###
190
191   unless ( $self->option('no_provision_did') ) {
192
193     return "can't yet provision to VoIP Innovations v3 API"; #XXX
194
195     #probably okay to queue the deletion...?
196     #but hell, let's do it inline anyway, who wants phone numbers hanging around
197
198     my $r = $self->vi_command('releaseDID',
199       'did'           => $svc_phone->phonenum,
200     );
201
202     my $rdid = $r->{did};
203
204     if ( $rdid->{'statuscode'} != 100 ) {
205       return "Error running VoIP Innovations releaseDID: ".
206              $rdid->{'statuscode'}. ': '. $rdid->{'status'};
207     }
208
209   }
210
211   ###
212   # remove911
213   ###
214
215   if ( $self->option('e911') ) {
216
217     my $resp = $self->vi_command('remove911',
218       'did'        => $svc_phone->phonenum,
219     );
220
221     if ( $resp->{'responseCode'} != 100 ) {
222       return "Error running VoIP Innovations remove911: ".
223              $resp->{'responseCode'}. ': '. $resp->{'responseMessage'};
224     }
225
226   }
227
228   '';
229 }
230
231 sub _export_suspend {
232   my( $self, $svc_phone ) = (shift, shift);
233   #nop for now
234   '';
235 }
236
237 sub _export_unsuspend {
238   my( $self, $svc_phone ) = (shift, shift);
239   #nop for now
240   '';
241 }
242
243 1;
244