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